|
|
@@ -0,0 +1,188 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+class Api {
|
|
|
+
|
|
|
+ private $_apiUser;
|
|
|
+ private $_request;
|
|
|
+
|
|
|
+ public function setUser($user) {
|
|
|
+ $this->_apiUser = $user;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function execute($url) {
|
|
|
+ $request = $this->parseUrl($url);
|
|
|
+
|
|
|
+ $apiRouterName = $request->apiRouterName;
|
|
|
+ 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>';}
|
|
|
+ $apiRouterName = ucfirst(strtolower($apiRouterName));
|
|
|
+ $apiRouterClassName = "Api_{$apiRouterName}";
|
|
|
+ if (!Lib::tryLoadClass($apiRouterClassName)) {
|
|
|
+ throw new HttpException("Route not exists", 404);
|
|
|
+ }
|
|
|
+ $apiRouter = new $apiRouterClassName();
|
|
|
+ $apiRouter->setUser($this->_apiUser);
|
|
|
+ $responseDocument = $apiRouter->execute($request);
|
|
|
+ if (empty($responseDocument)) {
|
|
|
+ throw new HttpException("No result", 404);
|
|
|
+ }
|
|
|
+ $this->response($responseDocument);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function parseUrl($url) {
|
|
|
+ $request = new stdClass();
|
|
|
+ $request->url = trim($url, '/ ');
|
|
|
+ $urlParts = explode('?', $request->url);
|
|
|
+ $request->path = $urlParts[0];
|
|
|
+ $request->query = (count($urlParts) > 1)? $urlParts[1] : '';
|
|
|
+ $request->args = $_REQUEST;
|
|
|
+ $request->segments = array();
|
|
|
+ $urlPathParts = explode('/', $request->path);
|
|
|
+ foreach ($urlPathParts as $part) {
|
|
|
+ if (!empty($part)) $request->segments[] = $part;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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>';}
|
|
|
+ $request->format = array_shift($request->segments);
|
|
|
+ if (empty($request->segments)) return null;
|
|
|
+
|
|
|
+ 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>';}
|
|
|
+ if (!$this->checkResponseFormat($request->format)) {
|
|
|
+ throw new HttpException("Response format not allowed", 400);
|
|
|
+ }
|
|
|
+ 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>';}
|
|
|
+
|
|
|
+ $request->apiRouterName = array_shift($request->segments);
|
|
|
+ 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>';}
|
|
|
+
|
|
|
+ if (!empty($request->query)) {
|
|
|
+ $queryArgs = array();
|
|
|
+ parse_str($request->query, $queryArgs);
|
|
|
+ $request->query = $queryArgs;
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->_request = $request;
|
|
|
+ return $this->_request;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function checkResponseFormat($format) {
|
|
|
+ $allowedTypes = array();
|
|
|
+ $allowedTypes[] = 'xml';
|
|
|
+ $allowedTypes[] = 'json';
|
|
|
+ $allowedTypes[] = 'csv';
|
|
|
+ $allowedTypes[] = 'csv_file';
|
|
|
+ $allowedTypes[] = 'raw';
|
|
|
+ //$allowedTypes[] = 'xml-ns';// xml with namespace
|
|
|
+ return in_array($format, $allowedTypes);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function response($document) {
|
|
|
+ if ('xml' == $this->_request->format) {
|
|
|
+ $this->responseXml($document);
|
|
|
+ }
|
|
|
+ else if ('json' == $this->_request->format) {
|
|
|
+ $this->responseJson($document);
|
|
|
+ }
|
|
|
+ else if ('csv' == $this->_request->format) {
|
|
|
+ $this->responseCsv($document);
|
|
|
+ }
|
|
|
+ else if ('csv_file' == $this->_request->format) {
|
|
|
+ $this->responseCsvFile($document);
|
|
|
+ }
|
|
|
+ else if ('raw' == $this->_request->format) {
|
|
|
+ echo $document;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function responseXml($document) {
|
|
|
+ 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>';}
|
|
|
+
|
|
|
+ $xml = new DOMDocument('1.0', 'utf-8');
|
|
|
+ $xml->formatOutput = true;
|
|
|
+ $data = $xml->createElement('data');
|
|
|
+ $xml->appendChild($data);
|
|
|
+ $this->_xmlAddChildRec($document, $data, $xml);
|
|
|
+
|
|
|
+ header('Content-type: application/xml; charset="utf-8"');
|
|
|
+ echo $xml->saveXML();
|
|
|
+ }
|
|
|
+
|
|
|
+ private function _xmlAddChildRec($node, $parentNode, $dom) {
|
|
|
+ if (!$node) return;
|
|
|
+ if (is_object($node)) {
|
|
|
+ foreach ($node as $childNodeName => $childNode) {
|
|
|
+ $n = $dom->createElement($childNodeName);
|
|
|
+ $parentNode->appendChild($n);
|
|
|
+ $this->_xmlAddChildRec($childNode, $n, $dom);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (is_array($node)) {
|
|
|
+ foreach ($node as $childNodeIndeks => $childNode) {
|
|
|
+ $this->_xmlAddChildRec($childNode, $parentNode, $dom);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (false !== strpos($node, '&')) {
|
|
|
+ $n = $dom->createCDATASection($node);
|
|
|
+ $parentNode->appendChild($n);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $n = $dom->createTextNode($node);
|
|
|
+ $parentNode->appendChild($n);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function responseJson($document) {
|
|
|
+ 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>';}
|
|
|
+ header('Content-type: application/json; charset="utf-8"');
|
|
|
+ echo json_encode($document);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function responseCsv($document) {
|
|
|
+ header('Content-Type: text/csv; charset=utf-8');
|
|
|
+
|
|
|
+ if (empty($document)) return;
|
|
|
+
|
|
|
+ $csvSeparator = ';';
|
|
|
+ $this->_printCsvDocument($document, $csvSeparator);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function responseCsvFile($document) {
|
|
|
+ $csvFileName = 'output';//$this->_request
|
|
|
+ if ('table' === $this->_request->apiRouterName) {
|
|
|
+ $pathEx = explode('/', $this->_request->path);
|
|
|
+ if (count($pathEx) > 3) {
|
|
|
+ $csvFileName = $pathEx[3];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ header('Content-Type: text/csv; charset=utf-8');
|
|
|
+ header("Content-Disposition: attachment; filename={$csvFileName}.csv");
|
|
|
+
|
|
|
+ if (empty($document)) return;
|
|
|
+
|
|
|
+ $csvSeparator = ';';
|
|
|
+ $this->_printCsvDocument($document, $csvSeparator);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function _printCsvDocument($document, $csvSeparator = ';') {
|
|
|
+ if (empty($document)) return;
|
|
|
+
|
|
|
+ $fldList = array();
|
|
|
+ $labelsLine = array();
|
|
|
+ foreach ($document->items[0]->item as $fldName => $value) {
|
|
|
+ $fldList[] = $fldName;
|
|
|
+ $labelsLine[] = '"' . addslashes($fldName) . '"';
|
|
|
+ }
|
|
|
+ echo implode($csvSeparator, $labelsLine) . "\n";
|
|
|
+
|
|
|
+ foreach ($document->items as $itemsList) {
|
|
|
+ foreach ($itemsList as $item) {
|
|
|
+ $itemLine = array();
|
|
|
+ foreach ($fldList as $fldName) {
|
|
|
+ $itemLine[] = '"' . addslashes($item->{$fldName}) . '"';
|
|
|
+ }
|
|
|
+ echo implode($csvSeparator, $itemLine) . "\n";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|