api.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * Bibliotheca
  4. *
  5. * Copyright 2018-2023 Johannes Keßler
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.
  19. */
  20. require_once './config/config.php';
  21. mb_http_output('UTF-8');
  22. mb_internal_encoding('UTF-8');
  23. ini_set('error_reporting',-1); // E_ALL & E_STRICT
  24. # check request
  25. $_urlToParse = filter_var($_SERVER['QUERY_STRING'],FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW);
  26. if(!empty($_urlToParse)) {
  27. # see http://de2.php.net/manual/en/regexp.reference.unicode.php
  28. if(preg_match('/[\p{C}\p{M}\p{Sc}\p{Sk}\p{So}\p{Zl}\p{Zp}]/u',$_urlToParse) === 1) {
  29. die('Malformed request. Make sure you know what you are doing.');
  30. }
  31. }
  32. # set the error reporting
  33. ini_set('log_errors',true);
  34. if(DEBUG === true) {
  35. ini_set('display_errors',true);
  36. }
  37. else {
  38. ini_set('display_errors',false);
  39. }
  40. # time settings
  41. date_default_timezone_set(TIMEZONE);
  42. # static helper class
  43. require_once 'lib/summoner.class.php';
  44. # general includes
  45. require_once 'lib/doomguy.class.php';
  46. require_once 'lib/spectre.class.php';
  47. require_once 'lib/mancubus.class.php';
  48. require_once 'lib/manageentry.class.php';
  49. require_once 'lib/trite.class.php';
  50. ## DB connection
  51. $DB = new mysqli(DB_HOST, DB_USERNAME,DB_PASSWORD, DB_NAME);
  52. $driver = new mysqli_driver();
  53. $driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;
  54. if ($DB->connect_errno) exit('Can not connect to MySQL Server');
  55. $DB->set_charset("utf8mb4");
  56. $DB->query("SET collation_connection = 'utf8mb4_unicode_ci'");
  57. # user Object
  58. $Doomguy = new Doomguy($DB);
  59. # API object
  60. $Spectre = new Spectre($DB, $Doomguy);
  61. $_requestMode = "default";
  62. if(isset($_GET['p']) && !empty($_GET['p'])) {
  63. $_requestMode = trim($_GET['p']);
  64. $_requestMode = Summoner::validate($_requestMode,'nospace') ? $_requestMode : "default";
  65. if(!$Spectre->allowedRequests($_requestMode)) $_requestMode = "default";
  66. }
  67. $_authKey = '';
  68. if(isset($_GET['authKey']) && !empty($_GET['authKey'])) {
  69. $_authKey = trim($_GET['authKey']);
  70. $_authKey = Summoner::validate($_authKey,'nospace') ? $_authKey : '';
  71. }
  72. $_apiResult = array(
  73. 'message' => 'Nothing to see here.',
  74. 'status' => 200,
  75. 'data' => array()
  76. );
  77. switch ($_requestMode) {
  78. case 'list':
  79. # get the latest 10 entris for given collection
  80. $_msg = 'Missing parameter with value: collection';
  81. $_status = 404;
  82. $_data = array();
  83. $_collection = false;
  84. if(isset($_GET['collection']) && !empty($_GET['collection'])) {
  85. $_collection = trim($_GET['collection']);
  86. $_collection = Summoner::validate($_collection,'digit') ? $_collection : false;
  87. }
  88. if(!empty($_collection)) {
  89. $_msg = 'Invalid collection.';
  90. $Mancubus = new Mancubus($DB,$Doomguy);
  91. $Trite = new Trite($DB,$Doomguy);
  92. $collectionInfo = $Trite->load($_collection);
  93. $Mancubus->setCollection($Trite->param('id'));
  94. $Mancubus->setQueryOptions(array('limit' => 10));
  95. $entries = $Mancubus->getEntries();
  96. if(!empty($entries)) {
  97. $_msg = 'Latest entries for collection: '.$collectionInfo['name'];
  98. $_status = 200;
  99. $_data = $entries;
  100. }
  101. }
  102. $_apiResult = array(
  103. 'message' => $_msg,
  104. 'status' => $_status,
  105. 'data' => $_data
  106. );
  107. break;
  108. case 'add':
  109. # add a single new entry to given collection
  110. # authenticated by api token
  111. $_msg = 'Missing parameter with value: collection';
  112. $_status = 400;
  113. $_data = array();
  114. $Doomguy->authByApiToken($_authKey);
  115. if(!$Doomguy->isSignedIn()) {
  116. $_apiResult = array(
  117. 'message' => "Missing API token.",
  118. 'status' => 401,
  119. 'data' => $_data
  120. );
  121. break;
  122. }
  123. $_collection = false;
  124. if(isset($_GET['collection']) && !empty($_GET['collection'])) {
  125. $_collection = trim($_GET['collection']);
  126. $_collection = Summoner::validate($_collection,'digit') ? $_collection : false;
  127. }
  128. if(!empty($_collection)) {
  129. $_msg = 'Invalid POST data.';
  130. $_data = $_REQUEST;
  131. $Mancubus = new Mancubus($DB,$Doomguy);
  132. $ManangeEntry = new Manageentry($DB,$Doomguy);
  133. $ManangeEntry->setCollection($_collection);
  134. $editFields = $ManangeEntry->getEditFields();
  135. if(!empty($_POST) && !empty($editFields)) {
  136. $fdata = $_POST;
  137. if(!empty($_FILES)) {
  138. $fupload = $Spectre->prepareFilesArray($_FILES);
  139. }
  140. $_owner = $Doomguy->param('id');
  141. $_group = $Doomguy->param('baseGroupId');
  142. $_rights = 'rwxrwxr--';
  143. foreach ($editFields as $fieldId=>$fieldData) {
  144. if(isset($fupload['name'][$fieldData['identifier']])) {
  145. $fieldData['uploadData'] = $fupload;
  146. $_fieldsToSave[$fieldData['identifier']] = $fieldData;
  147. }
  148. elseif(isset($fdata[$fieldData['identifier']])) {
  149. $_value = trim($fdata[$fieldData['identifier']]);
  150. if(!empty($_value)) {
  151. $fieldData['valueToSave'] = trim($fdata[$fieldData['identifier']]);
  152. $_fieldsToSave[$fieldData['identifier']] = $fieldData;
  153. }
  154. }
  155. }
  156. // special case. Title field should be always available.
  157. if(!empty($_fieldsToSave) && isset($_fieldsToSave['title'])) {
  158. $do = $ManangeEntry->create($_fieldsToSave, $_owner, $_group, $_rights);
  159. if(!empty($do)) {
  160. $_msg = 'Added entry: '.$_fieldsToSave['title']['valueToSave'];
  161. $_status = 200;
  162. $_data = array($_fieldsToSave, $_owner, $_group, $_rights);
  163. }
  164. }
  165. }
  166. }
  167. $_apiResult = array(
  168. 'message' => $_msg,
  169. 'status' => $_status,
  170. 'data' => $_data
  171. );
  172. break;
  173. case 'addInfo':
  174. # return information about the given collection to create an ad call.
  175. $_msg = 'Missing parameter with value: collection';
  176. $_status = 404;
  177. $_data = array();
  178. $_collection = false;
  179. if(isset($_GET['collection']) && !empty($_GET['collection'])) {
  180. $_collection = trim($_GET['collection']);
  181. $_collection = Summoner::validate($_collection,'digit') ? $_collection : false;
  182. }
  183. if(!empty($_collection)) {
  184. $_msg = 'Invalid collection.';
  185. $Mancubus = new Mancubus($DB,$Doomguy);
  186. $Trite = new Trite($DB,$Doomguy);
  187. $collectionInfo = $Trite->load($_collection);
  188. $Mancubus->setCollection($Trite->param('id'));
  189. // just get one entry fpr given collection and then build the
  190. // json information about adding structure
  191. $entryStructure = $Mancubus->getEntryStructure();
  192. $structure = $Spectre->buildAddStructure($entryStructure['fields']);
  193. if(!empty($structure)) {
  194. $_msg = 'API POST and FILES data information for collection: '.$collectionInfo['name'];
  195. $_status = 200;
  196. $_data = $structure;
  197. }
  198. }
  199. $_apiResult = array(
  200. 'message' => $_msg,
  201. 'status' => $_status,
  202. 'data' => $_data
  203. );
  204. break;
  205. case 'default':
  206. default:
  207. // do nothing
  208. }
  209. # header information
  210. http_response_code($_apiResult['status']);
  211. header('Content-type: application/json; charset=UTF-8');
  212. echo json_encode($_apiResult);