| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- Lib::loadClass('ApiDataSourceTodo');// TODO: @see Entity/Source/Mysql from feature-schema-install
- Lib::loadClass('Api_WfsException');
- Lib::loadClass('Api_WfsServer');
- Lib::loadClass('UserAcl');
- class Api_Wfs {
- private $_apiUser;
- private $_dataSourceName;
- private $_tblName;
- private $_tblSchema;
- public function setUser($user) {
- $this->_apiUser = $user;
- }
- private function reqDBG($request, $line) {
- $reqLog = "[" . date("Y-m-d H:m:s") . "] WFS: ---- {$_SERVER['REQUEST_METHOD']}: {$_SERVER['REQUEST_URI']}";
- if ($_SERVER['REQUEST_METHOD'] == 'POST') $reqLog .= "\n------------ POST:\n" . file_get_contents("php://input");
- if (!empty($request)) $reqLog .= "\n------------ request: " . json_encode($request);
- $reqLog .= "\n------------ END.";
- $this->DBG($reqLog, $line);
- }
- private function DBG($reqLog, $line) {
- $errorLogFile = APP_PATH_ROOT . "/wfs.log";
- if (!is_writable($errorLogFile)) {
- $fp = @fopen($errorLogFile, "w");
- if ($fp === false) {
- return;
- }
- @fclose($fp);
- }
- error_log("L.{$line}:{$reqLog}\n", 3, $errorLogFile);
- }
- public function execute($request) {
- $this->reqDBG($request, __LINE__);
- /* TODO: return response xml document
- $responseDocument = null;
- try {
- $responseDocument = $this->wfsServerAction($request);
- } catch (Api_WfsException $e) {// TODO: create WfsException - http://schemas.opengis.net/wfs/1.0.0/OGC-exception.xsd
- //} catch (Exception $e) {// TODO: create WfsException - http://schemas.opengis.net/wfs/1.0.0/OGC-exception.xsd
- $responseDocument = $this->wfsExceptionAction($request, $e);
- }
- return $responseDocument;
- */
- if (empty($request->segments)) {
- //$this->mainWpsAction($request);// show list of posible data source
- throw new HttpException("Bad Request", 400);
- } else {
- $this->_dataSourceName = array_shift($request->segments);
- $this->DBG("dataSourceAction({$this->_dataSourceName}) ...", __LINE__);
- try {
- $this->dataSourceAction($request);
- } catch (Api_WfsException $e) {
- $responseDocument = $this->wfsExceptionAction($e);
- header('Content-type: application/xml');
- echo $responseDocument;
- } catch (Exception $e) {
- $responseDocument = $this->wfsExceptionAction($e);
- header('Content-type: application/xml');
- echo $responseDocument;
- }
- $this->DBG("dataSourceAction() END", __LINE__);
- }
- exit;
- // return document tree - array of arrays
- }
- private function wfsServerAction($request) {
- }
- private function wfsExceptionAction($e) {
- $dom = new DOMDocument('1.0', 'utf-8');
- $dom->formatOutput = true;
- $dom->preserveWhiteSpace = false;
- $rootNode = $dom->createElementNS('http://www.opengis.net/ogc', 'ServiceExceptionReport');
- $dom->appendChild($rootNode);
- $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
- $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');
- $rootNode->setAttribute('version', '1.2.0');
- $srvExNode = $dom->createElement('ServiceException', $e->getMessage());
- $rootNode->appendChild($srvExNode);
- return $dom->saveXML();
- }
- private function dataSourceAction($request) {
- $document = '';
- //$userAcl = User::getAcl();
- 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>';}
- $userAcl = new UserAcl($this->_apiUser->getID(), $use_cache = true);
- $userAcl->fetchGroups();
- $userAcl->fetchAllPerms(true);
- 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>';}
- $this->DBG("WfsServer(" . $this->_apiUser->getID() . ") ...", __LINE__);
- $wfsServer = new Api_WfsServer($userAcl);
- if ('WFS' != V::get('SERVICE', '', $request->query)) {
- throw new Api_WfsException("Only WFS Service is allowed");
- }
- $req = V::get('REQUEST', '', $request->query);
- if (!empty($req)) {
- $methodName = "{$req}Action";
- if (!method_exists($wfsServer, $methodName)) {
- throw new Api_WfsException("Not Implemented " . htmlspecialchars($req), 501);
- }
- $this->DBG("WfsServer->{$methodName}() ...", __LINE__);
- $document = $wfsServer->$methodName($urlQuery);
- }
- else {
- $this->DBG("WfsServer->parseXMLRequest() ...", __LINE__);
- $document = $wfsServer->parseXMLRequest();
- header('Content-type: application/xml');
- echo '<?xml version="1.0" encoding="UTF-8"?>';
- echo $document; exit;// TODO: return $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/xml');
- echo $document; exit;// TODO: return $document;
- exit;
- // TODO: return $document;
- }
- private function mainWpsAction($request) {
- return array('TODO:'=>'TODO: Show main WPS GetCapabilities');
- }
- }
|