PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/api.php

https://gitlab.com/x33n/SellCloudMusic
PHP | 284 lines | 194 code | 67 blank | 23 comment | 52 complexity | 939020170e3f38f1bc2ee25075d2635f MD5 | raw file
  1. <?php
  2. require_once('kernel/db/class.dbcommon.php');
  3. require_once('kernel/class.form.php');
  4. require_once('kernel/class.elist.php');
  5. require_once('src/ent.global.php');
  6. require_once('src/ent.order.php');
  7. require_once('src/ent.list.php');
  8. include_once('./cfg/configuration.php');
  9. if (!@isset($_GET))
  10. exit;
  11. if (!isset($_GET['output']))
  12. $_GET['output'] = 'xml';
  13. // helpers
  14. function display($entity) {
  15. switch (strtolower($_GET['output'])) {
  16. case 'json':
  17. echo $entity->toJSON();
  18. break;
  19. case 'html':
  20. if ($entity instanceof Errors) {
  21. echo $entity->message;
  22. } else echo $entity->toHTML();
  23. break;
  24. default:
  25. echo $entity->toXML();
  26. break;
  27. }
  28. }
  29. function error($message) {
  30. display(new Errors(array("message" => $message)));
  31. exit;
  32. }
  33. // output either in JSON, XML or HTML
  34. switch (strtolower($_GET['output'])) {
  35. case 'json':
  36. header("Content-type: text/json; charset=utf-8");
  37. break;
  38. case 'html':
  39. $_GET['formwrap'] = true;
  40. break;
  41. default:
  42. header("Content-type: text/xml; charset=utf-8");
  43. break;
  44. }
  45. if (@isset($_GET['type'])) {
  46. $type = $_GET['type'];
  47. if (is_subclass_of($type, 'Entity')) {
  48. // forms requires session to save its token
  49. session_start();
  50. $ent = new $type();
  51. $frm = new Form($ent);
  52. // entity access level
  53. // PUBLIC (default) | PRIVILEGED (user logged in) | NONE (noone through API)
  54. $access = $ent->getGlobalData(Entity::LABEL_ACCESS);
  55. // additional formula to the end of the query
  56. // we are making sure it's being manipulated with records belonging
  57. // to currently logged in user
  58. $add2q = '';
  59. $method = $_SERVER['REQUEST_METHOD'];
  60. if ($access === 'privileged') {
  61. $authTokenUsed = @isset($_GET['auth_token']);
  62. if (!User::isStored() && !$authTokenUsed) {
  63. // noone who is not signed in can request existing record
  64. if (@isset($_GET['id']) && $_GET['id'] > 0) error("Unauthorized access!");
  65. } else {
  66. if ($authTokenUsed) {
  67. $authToken = new AuthToken();
  68. $authToken->auth_token = $_GET['auth_token'];
  69. // try to look for authToken in database
  70. $conn = new DBCommon();
  71. $r = $conn->findEntity($authToken);
  72. if ($r instanceof NTError || $authToken->getID() < 1) {
  73. error("Unauthorized access!");
  74. } else {
  75. $userID = $authToken->getID();
  76. }
  77. } else {
  78. $user = User::restore();
  79. $userID = $user->getID();
  80. }
  81. // AND id_user = id(user)
  82. $add2q = DBCommon::QUERY_CONJUCTION . sprintf(DBCommon::QUERY_PAIR, 'id_user', $userID);
  83. }
  84. }
  85. switch ($method) {
  86. case 'POST':
  87. if ($access === 'none')
  88. error("Access denied!");
  89. $ent->loadArray($_POST);
  90. if (!isset($_GET['formwrap'])) {
  91. // if we not dealing with forms we don't want to check token
  92. unset($ent->token);
  93. }
  94. if ($frm->dataFiltred()) {
  95. if ($add2q != '' && @isset($ent->id_user) && $ent->id_user != $userID) {
  96. error("Unauthorized access!");
  97. }
  98. $conn = new DBCommon();
  99. $e = $conn->saveEntity($ent, $add2q);
  100. if ($e instanceof DBError) {
  101. $frm->errors->db = @sprintf("%s %s.", @get_class($ent), $e->message);
  102. } elseif ($e instanceof NTError) {
  103. $frm->errors->{$e->slot} = @sprintf($e->message);
  104. }
  105. // empty passwords slots. we don't want them to be sent to client
  106. $ent->clear(FRM_FLG_PWD);
  107. }
  108. break;
  109. case 'GET':
  110. if (@isset($_GET['id']) && $_GET['id'] > 0) {
  111. if ($access === 'none')
  112. error("Access denied!");
  113. $ent->setID($_GET['id']);
  114. $conn = new DBCommon();
  115. $e = $conn->loadEntity($ent, $add2q);
  116. if ($e instanceof NTError) {
  117. $frm->errors->{$e->slot} = $e->message;
  118. } elseif (!$e) {
  119. error("Record not found");
  120. }
  121. $ent->clear(FRM_FLG_PWD);
  122. }
  123. break;
  124. case 'DELETE':
  125. // manage access
  126. if ($access === 'none')
  127. error("Access denied!");
  128. if (@isset($_GET['id'])) {
  129. // set entity id to be deleted
  130. $ent->setID($_GET['id']);
  131. // there in PHP are arrays assigned by copy
  132. $clone_GET = $_GET;
  133. // deleting reserved names for slots
  134. unset($clone_GET['id']);
  135. unset($clone_GET['type']);
  136. $ent->loadArray($clone_GET);
  137. $conn = new DBCommon();
  138. $e = $conn->deleteEntity($ent, $add2q);
  139. if ($e instanceof DBError) {
  140. $frm->errors->db = @sprintf("%s %s.", @get_class($ent), $e->message);
  141. } elseif ($e instanceof NTError) {
  142. $frm->errors->{$e->slot} = $e->message;
  143. } else {
  144. // set error if affected arrows equal 0
  145. if ($e == 0)
  146. $frm->errors->db = "Don't exists";
  147. // empty entity
  148. $ent->clear();
  149. }
  150. }
  151. break;
  152. }
  153. // update form status if any errors occured
  154. $frm->updateStatus();
  155. // send form or entity to output
  156. if (isset($_GET['formwrap'])) {
  157. // output entity wrapped with form
  158. display($frm);
  159. } else {
  160. // if there are any errors output them
  161. display($frm->errors->exist() ? $frm->errors : $ent);
  162. }
  163. } elseif (is_subclass_of($type, 'EList')) {
  164. if ($_GET['output'] === 'html') {
  165. error("Lists can't be displayed as HTML");
  166. }
  167. $list = new $type();
  168. // manage access
  169. switch ($list->entity->getGlobalData(Entity::LABEL_ACCESS)) {
  170. case 'none':
  171. error("Access denied!");
  172. break;
  173. case 'privileged':
  174. session_start();
  175. $authTokenUsed = @isset($_GET['auth_token']);
  176. if (!User::isStored() && !$authTokenUsed) {
  177. error("Unauthorized access!");
  178. } else {
  179. if ($authTokenUsed) {
  180. $authToken = new AuthToken();
  181. $authToken->auth_token = $_GET['auth_token'];
  182. // try to look for authToken in database
  183. $conn = new DBCommon();
  184. $r = $conn->findEntity($authToken);
  185. if ($r instanceof NTError || $authToken->getID() < 1) {
  186. error("Unauthorized access!");
  187. } else {
  188. $userID = $authToken->getID();
  189. }
  190. } else {
  191. $userID = User::restore()->getID();
  192. }
  193. }
  194. // make sure that we query records belonging just to logged user
  195. $_GET['id_user'] = $userID;
  196. break;
  197. }
  198. // load elist from db
  199. $conn = new DBCommon();
  200. $e = $conn->loadEList($list, $_GET);
  201. if ($e instanceof DBError) {
  202. error($e->message);
  203. } else {
  204. display($list);
  205. }
  206. } else {
  207. // neither entity neither elist exists
  208. error("Unknown type");
  209. }
  210. } else {
  211. error("Invalid request");
  212. }
  213. ?>