Wfs.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. Lib::loadClass('ApiDataSourceTodo');// TODO: @see Entity/Source/Mysql from feature-schema-install
  3. Lib::loadClass('Api_WfsException');
  4. Lib::loadClass('Api_WfsServer');
  5. Lib::loadClass('UserAcl');
  6. class Api_Wfs {
  7. private $_apiUser;
  8. private $_dataSourceName;
  9. private $_tblName;
  10. private $_tblSchema;
  11. public function setUser($user) {
  12. $this->_apiUser = $user;
  13. }
  14. private function reqDBG($request, $line) {
  15. $reqLog = "[" . date("Y-m-d H:m:s") . "] WFS: ---- {$_SERVER['REQUEST_METHOD']}: {$_SERVER['REQUEST_URI']}";
  16. if ($_SERVER['REQUEST_METHOD'] == 'POST') $reqLog .= "\n------------ POST:\n" . file_get_contents("php://input");
  17. if (!empty($request)) $reqLog .= "\n------------ request: " . json_encode($request);
  18. $reqLog .= "\n------------ END.";
  19. $this->DBG($reqLog, $line);
  20. }
  21. private function DBG($reqLog, $line) {
  22. $errorLogFile = APP_PATH_ROOT . "/wfs.log";
  23. if (!is_writable($errorLogFile)) {
  24. $fp = @fopen($errorLogFile, "w");
  25. if ($fp === false) {
  26. return;
  27. }
  28. @fclose($fp);
  29. }
  30. error_log("L.{$line}:{$reqLog}\n", 3, $errorLogFile);
  31. }
  32. public function execute($request) {
  33. $this->reqDBG($request, __LINE__);
  34. /* TODO: return response xml document
  35. $responseDocument = null;
  36. try {
  37. $responseDocument = $this->wfsServerAction($request);
  38. } catch (Api_WfsException $e) {// TODO: create WfsException - http://schemas.opengis.net/wfs/1.0.0/OGC-exception.xsd
  39. //} catch (Exception $e) {// TODO: create WfsException - http://schemas.opengis.net/wfs/1.0.0/OGC-exception.xsd
  40. $responseDocument = $this->wfsExceptionAction($request, $e);
  41. }
  42. return $responseDocument;
  43. */
  44. if (empty($request->segments)) {
  45. //$this->mainWpsAction($request);// show list of posible data source
  46. throw new HttpException("Bad Request", 400);
  47. } else {
  48. $this->_dataSourceName = array_shift($request->segments);
  49. $this->DBG("dataSourceAction({$this->_dataSourceName}) ...", __LINE__);
  50. try {
  51. $this->dataSourceAction($request);
  52. } catch (Api_WfsException $e) {
  53. $responseDocument = $this->wfsExceptionAction($e);
  54. header('Content-type: application/xml');
  55. echo $responseDocument;
  56. } catch (Exception $e) {
  57. $responseDocument = $this->wfsExceptionAction($e);
  58. header('Content-type: application/xml');
  59. echo $responseDocument;
  60. }
  61. $this->DBG("dataSourceAction() END", __LINE__);
  62. }
  63. exit;
  64. // return document tree - array of arrays
  65. }
  66. private function wfsServerAction($request) {
  67. }
  68. private function wfsExceptionAction($e) {
  69. $dom = new DOMDocument('1.0', 'utf-8');
  70. $dom->formatOutput = true;
  71. $dom->preserveWhiteSpace = false;
  72. $rootNode = $dom->createElementNS('http://www.opengis.net/ogc', 'ServiceExceptionReport');
  73. $dom->appendChild($rootNode);
  74. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  75. $rootNode->setAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation', 'http://www.opengis.net/ogc http://schemas.opengis.net/wfs/1.0.0/OGC-exception.xsd');
  76. $rootNode->setAttribute('version', '1.2.0');
  77. $srvExNode = $dom->createElement('ServiceException', $e->getMessage());
  78. $rootNode->appendChild($srvExNode);
  79. return $dom->saveXML();
  80. }
  81. private function dataSourceAction($request) {
  82. $document = '';
  83. //$userAcl = User::getAcl();
  84. IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">user (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($this->_apiUser);echo'</pre>';}
  85. $userAcl = new UserAcl($this->_apiUser->getID(), $use_cache = true);
  86. $userAcl->fetchGroups();
  87. $userAcl->fetchAllPerms(true);
  88. IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">$userAcl (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($userAcl);echo'</pre>';}
  89. $this->DBG("WfsServer(" . $this->_apiUser->getID() . ") ...", __LINE__);
  90. $wfsServer = new Api_WfsServer($userAcl);
  91. if ('WFS' != V::get('SERVICE', '', $request->query)) {
  92. throw new Api_WfsException("Only WFS Service is allowed");
  93. }
  94. $req = V::get('REQUEST', '', $request->query);
  95. if (!empty($req)) {
  96. $methodName = "{$req}Action";
  97. if (!method_exists($wfsServer, $methodName)) {
  98. throw new Api_WfsException("Not Implemented " . htmlspecialchars($req), 501);
  99. }
  100. $this->DBG("WfsServer->{$methodName}() ...", __LINE__);
  101. $document = $wfsServer->$methodName($urlQuery);
  102. }
  103. else {
  104. $this->DBG("WfsServer->parseXMLRequest() ...", __LINE__);
  105. $document = $wfsServer->parseXMLRequest();
  106. header('Content-type: application/xml');
  107. echo '<?xml version="1.0" encoding="UTF-8"?>';
  108. echo $document; exit;// TODO: return $document;
  109. }
  110. 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>';}
  111. header('Content-type: application/xml');
  112. echo $document; exit;// TODO: return $document;
  113. exit;
  114. // TODO: return $document;
  115. }
  116. private function mainWpsAction($request) {
  117. return array('TODO:'=>'TODO: Show main WPS GetCapabilities');
  118. }
  119. }