Api.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. class Api {
  3. private $_apiUser;
  4. private $_apiBaseUri;
  5. private $_request;
  6. public function setUser($user) {
  7. $this->_apiUser = $user;
  8. }
  9. public function setBaseUri($uri) {
  10. $this->_apiBaseUri = $uri;
  11. }
  12. public function execute($url) {
  13. header('Access-Control-Allow-Credentials: true');
  14. header('Access-Control-Allow-Origin: https://dev.procesy5.pl');
  15. // header('Access-Control-Allow-Origin: *');
  16. // header('Access-Control-Max-Age: 86400'); // cache for 1 day
  17. $request = $this->parseUrl($url);
  18. $apiRouterName = $request->apiRouterName;
  19. IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">apiRouterName (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($apiRouterName);echo'</pre>';}
  20. $apiRouterName = ucfirst(strtolower($apiRouterName));
  21. $apiRouterClassName = "Api_{$apiRouterName}";
  22. if (!Lib::tryLoadClass($apiRouterClassName)) {
  23. throw new HttpException("Route not exists", 404);
  24. }
  25. $apiRouter = new $apiRouterClassName();
  26. $apiRouter->setUser($this->_apiUser);
  27. $apiRouter->setBaseUri($this->_apiBaseUri);
  28. $responseDocument = $apiRouter->execute($request);
  29. if (empty($responseDocument)) {
  30. throw new HttpException("No result", 404);
  31. }
  32. $this->response($responseDocument);
  33. }
  34. private function parseUrl($url) {
  35. $request = new stdClass();
  36. $request->url = trim($url, '/ ');
  37. $urlParts = explode('?', $request->url);
  38. $request->path = $urlParts[0];
  39. $request->query = (count($urlParts) > 1)? $urlParts[1] : '';
  40. $request->args = $_REQUEST;
  41. $request->segments = array();
  42. $urlPathParts = explode('/', $request->path);
  43. foreach ($urlPathParts as $part) {
  44. if (!empty($part)) $request->segments[] = $part;
  45. }
  46. IF(V::get('DBG','',$_GET)>1){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">url (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'</pre>';}
  47. $request->format = array_shift($request->segments);
  48. if (empty($request->segments)) return null;
  49. IF(V::get('DBG','',$_GET)>1){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">responseFormat (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request->format);echo'</pre>';}
  50. if (!$this->checkResponseFormat($request->format)) {
  51. throw new HttpException("Response format not allowed", 400);
  52. }
  53. IF(V::get('DBG','',$_GET)>1){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">u (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'</pre>';}
  54. $request->apiRouterName = array_shift($request->segments);
  55. IF(V::get('DBG','',$_GET)>1){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">u (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'</pre>';}
  56. if (!empty($request->query)) {
  57. $queryArgs = array();
  58. parse_str($request->query, $queryArgs);
  59. $request->query = $queryArgs;
  60. }
  61. $this->_request = $request;
  62. return $this->_request;
  63. }
  64. private function checkResponseFormat($format) {
  65. $allowedTypes = array();
  66. $allowedTypes[] = 'xml';
  67. $allowedTypes[] = 'json';
  68. $allowedTypes[] = 'csv';
  69. $allowedTypes[] = 'csv_file';
  70. $allowedTypes[] = 'raw';
  71. //$allowedTypes[] = 'xml-ns';// xml with namespace
  72. return in_array($format, $allowedTypes);
  73. }
  74. private function response($document) {
  75. if ('xml' == $this->_request->format) {
  76. $this->responseXml($document);
  77. }
  78. else if ('json' == $this->_request->format) {
  79. $this->responseJson($document);
  80. }
  81. else if ('csv' == $this->_request->format) {
  82. $this->responseCsv($document);
  83. }
  84. else if ('csv_file' == $this->_request->format) {
  85. $this->responseCsvFile($document);
  86. }
  87. else if ('raw' == $this->_request->format) {
  88. echo $document;
  89. }
  90. }
  91. private function responseXml($document) {
  92. IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">document (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($document);echo'</pre>';}
  93. $xml = new DOMDocument('1.0', 'utf-8');
  94. $xml->formatOutput = true;
  95. $data = $xml->createElement('data');
  96. $xml->appendChild($data);
  97. $this->_xmlAddChildRec($document, $data, $xml);
  98. header('Content-type: application/xml; charset="utf-8"');
  99. echo $xml->saveXML();
  100. }
  101. private function _xmlAddChildRec($node, $parentNode, $dom) {
  102. if (!$node) return;
  103. if (is_object($node)) {
  104. foreach ($node as $childNodeName => $childNode) {
  105. $n = $dom->createElement($childNodeName);
  106. $parentNode->appendChild($n);
  107. $this->_xmlAddChildRec($childNode, $n, $dom);
  108. }
  109. }
  110. else if (is_array($node)) {
  111. foreach ($node as $childNodeIndeks => $childNode) {
  112. $this->_xmlAddChildRec($childNode, $parentNode, $dom);
  113. }
  114. }
  115. else if (false !== strpos($node, '&')) {
  116. $n = $dom->createCDATASection($node);
  117. $parentNode->appendChild($n);
  118. }
  119. else {
  120. $n = $dom->createTextNode($node);
  121. $parentNode->appendChild($n);
  122. }
  123. }
  124. private function responseJson($document) {
  125. IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">document (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($document);echo'</pre>';}
  126. header('Content-type: application/json; charset="utf-8"');
  127. echo json_encode($document);
  128. }
  129. private function responseCsv($document) {
  130. header('Content-Type: text/csv; charset=utf-8');
  131. if (empty($document)) return;
  132. $csvSeparator = ';';
  133. $this->_printCsvDocument($document, $csvSeparator);
  134. }
  135. private function responseCsvFile($document) {
  136. $csvFileName = 'output';//$this->_request
  137. if ('table' === $this->_request->apiRouterName) {
  138. $pathEx = explode('/', $this->_request->path);
  139. if (count($pathEx) > 3) {
  140. $csvFileName = $pathEx[3];
  141. }
  142. }
  143. header('Content-Type: text/csv; charset=utf-8');
  144. header("Content-Disposition: attachment; filename={$csvFileName}.csv");
  145. if (empty($document)) return;
  146. $csvSeparator = ';';
  147. $this->_printCsvDocument($document, $csvSeparator);
  148. }
  149. private function _printCsvDocument($document, $csvSeparator = ';') {
  150. if (empty($document)) return;
  151. $fldList = array();
  152. $labelsLine = array();
  153. foreach ($document->items[0]->item as $fldName => $value) {
  154. $fldList[] = $fldName;
  155. $labelsLine[] = '"' . addslashes($fldName) . '"';
  156. }
  157. echo implode($csvSeparator, $labelsLine) . "\n";
  158. foreach ($document->items as $itemsList) {
  159. foreach ($itemsList as $item) {
  160. $itemLine = array();
  161. foreach ($fldList as $fldName) {
  162. $itemLine[] = '"' . addslashes($item->{$fldName}) . '"';
  163. }
  164. echo implode($csvSeparator, $itemLine) . "\n";
  165. }
  166. }
  167. }
  168. }