Api.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. switch (strtolower($apiRouterName)) {
  21. case 'wfsdata': $apiRouterName = 'WfsData'; break;
  22. case 'wfsqgis': $apiRouterName = 'WfsQgis'; break;
  23. default: $apiRouterName = ucfirst(strtolower($apiRouterName)); break;
  24. }
  25. $apiRouterClassName = "Api_{$apiRouterName}";
  26. if (!Lib::tryLoadClass($apiRouterClassName)) {
  27. throw new HttpException("Route not exists", 404);
  28. }
  29. $apiRouter = new $apiRouterClassName();
  30. $apiRouter->setUser($this->_apiUser);
  31. $apiRouter->setBaseUri($this->_apiBaseUri);
  32. $responseDocument = $apiRouter->execute($request);
  33. if (empty($responseDocument)) {
  34. throw new HttpException("No result", 404);
  35. }
  36. $this->response($responseDocument);
  37. }
  38. private function parseUrl($url) {
  39. $request = new stdClass();
  40. $request->url = trim($url, '/ ');
  41. $urlParts = explode('?', $request->url);
  42. $request->path = $urlParts[0];
  43. $request->query = (count($urlParts) > 1)? $urlParts[1] : '';
  44. $request->args = $_REQUEST;
  45. $request->segments = array();
  46. $urlPathParts = explode('/', $request->path);
  47. foreach ($urlPathParts as $part) {
  48. if (!empty($part)) $request->segments[] = $part;
  49. }
  50. 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>';}
  51. $request->format = array_shift($request->segments);
  52. if (empty($request->segments)) return null;
  53. 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>';}
  54. if (!$this->checkResponseFormat($request->format)) {
  55. throw new HttpException("Response format not allowed", 400);
  56. }
  57. 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>';}
  58. $request->apiRouterName = array_shift($request->segments);
  59. 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>';}
  60. if (!empty($request->query)) {
  61. $queryArgs = array();
  62. parse_str($request->query, $queryArgs);
  63. $request->query = $queryArgs;
  64. }
  65. $this->_request = $request;
  66. return $this->_request;
  67. }
  68. private function checkResponseFormat($format) {
  69. $allowedTypes = array();
  70. $allowedTypes[] = 'xml';
  71. $allowedTypes[] = 'json';
  72. $allowedTypes[] = 'csv';
  73. $allowedTypes[] = 'csv_file';
  74. $allowedTypes[] = 'raw';
  75. //$allowedTypes[] = 'xml-ns';// xml with namespace
  76. return in_array($format, $allowedTypes);
  77. }
  78. private function response($document) {
  79. if ('xml' == $this->_request->format) {
  80. $this->responseXml($document);
  81. }
  82. else if ('json' == $this->_request->format) {
  83. $this->responseJson($document);
  84. }
  85. else if ('csv' == $this->_request->format) {
  86. $this->responseCsv($document);
  87. }
  88. else if ('csv_file' == $this->_request->format) {
  89. $this->responseCsvFile($document);
  90. }
  91. else if ('raw' == $this->_request->format) {
  92. echo $document;
  93. }
  94. }
  95. private function responseXml($document) {
  96. 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>';}
  97. $xml = new DOMDocument('1.0', 'utf-8');
  98. $xml->formatOutput = true;
  99. $data = $xml->createElement('data');
  100. $xml->appendChild($data);
  101. $this->_xmlAddChildRec($document, $data, $xml);
  102. header('Content-type: application/xml; charset="utf-8"');
  103. echo $xml->saveXML();
  104. }
  105. private function _xmlAddChildRec($node, $parentNode, $dom) {
  106. if (!$node) return;
  107. if (is_object($node)) {
  108. foreach ($node as $childNodeName => $childNode) {
  109. $n = $dom->createElement($childNodeName);
  110. $parentNode->appendChild($n);
  111. $this->_xmlAddChildRec($childNode, $n, $dom);
  112. }
  113. }
  114. else if (is_array($node)) {
  115. foreach ($node as $childNodeIndeks => $childNode) {
  116. $this->_xmlAddChildRec($childNode, $parentNode, $dom);
  117. }
  118. }
  119. else if (false !== strpos($node, '&')) {
  120. $n = $dom->createCDATASection($node);
  121. $parentNode->appendChild($n);
  122. }
  123. else {
  124. $n = $dom->createTextNode($node);
  125. $parentNode->appendChild($n);
  126. }
  127. }
  128. private function responseJson($document) {
  129. 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>';}
  130. header('Content-type: application/json; charset="utf-8"');
  131. echo json_encode($document);
  132. }
  133. private function responseCsv($document) {
  134. header('Content-Type: text/csv; charset=utf-8');
  135. if (empty($document)) return;
  136. $csvSeparator = ';';
  137. $this->_printCsvDocument($document, $csvSeparator);
  138. }
  139. private function responseCsvFile($document) {
  140. $csvFileName = 'output';//$this->_request
  141. if ('table' === $this->_request->apiRouterName) {
  142. $pathEx = explode('/', $this->_request->path);
  143. if (count($pathEx) > 3) {
  144. $csvFileName = $pathEx[3];
  145. }
  146. }
  147. header('Content-Type: text/csv; charset=utf-8');
  148. header("Content-Disposition: attachment; filename={$csvFileName}.csv");
  149. if (empty($document)) return;
  150. $csvSeparator = ';';
  151. $this->_printCsvDocument($document, $csvSeparator);
  152. }
  153. private function _printCsvDocument($document, $csvSeparator = ';') {
  154. if (empty($document)) return;
  155. $fldList = array();
  156. $labelsLine = array();
  157. foreach ($document->items[0]->item as $fldName => $value) {
  158. $fldList[] = $fldName;
  159. $labelsLine[] = '"' . addslashes($fldName) . '"';
  160. }
  161. echo implode($csvSeparator, $labelsLine) . "\n";
  162. foreach ($document->items as $itemsList) {
  163. foreach ($itemsList as $item) {
  164. $itemLine = array();
  165. foreach ($fldList as $fldName) {
  166. $itemLine[] = '"' . addslashes($item->{$fldName}) . '"';
  167. }
  168. echo implode($csvSeparator, $itemLine) . "\n";
  169. }
  170. }
  171. }
  172. }