Api.php 6.3 KB

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