WfsDataServer.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. Lib::loadClass('Api_WfsServerBase');
  3. Lib::loadClass('Api_WfsException');
  4. Lib::loadClass('Api_WfsGeomTypeConverter');
  5. Lib::loadClass('Api_WfsNs');
  6. Lib::loadClass('Core_XmlWriter');
  7. Lib::loadClass('DBG');
  8. Lib::loadClass('Api_Wfs_GetCapabilities');
  9. class Api_WfsDataServer extends Api_WfsServerBase {
  10. public function run($request) {
  11. $document = '';
  12. if ('WFS' != V::get('SERVICE', '', $request->query) && ('WFS' != V::get('service', '', $request->query))) {
  13. throw new Api_WfsException("Only WFS Service is allowed");
  14. }
  15. $req = V::get('REQUEST', '', $request->query);
  16. if (!empty($req)) {
  17. $methodName = "{$req}Action";
  18. if (!method_exists($this, $methodName)) {
  19. throw new Api_WfsException("Not Implemented " . htmlspecialchars($req), 501);
  20. }
  21. $this->DBG("WfsServer->{$methodName}() ...", __LINE__);
  22. $document = $this->$methodName($urlQuery);
  23. }
  24. else {
  25. $this->DBG("WfsServer->parseXMLRequest() ...", __LINE__);
  26. $document = $this->parseXMLRequest();
  27. header('Content-type: application/xml');
  28. echo '<?xml version="1.0" encoding="UTF-8"?>';
  29. echo $document; exit;// TODO: return $document;
  30. }
  31. 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>';}
  32. if ('raw' == V::get('outputFormat', '', $request->query)) {
  33. header('Content-type: text/plain; charset=utf-8');
  34. echo $document;
  35. } else {
  36. header('Content-type: application/xml');
  37. echo $document;
  38. }
  39. }
  40. public function parseXMLRequest() {
  41. $data = array();
  42. $reqContent = Request::getRequestBody();
  43. if (empty($reqContent)) {
  44. throw new Exception("Empty request");
  45. }
  46. $parserXml = xml_parser_create();
  47. xml_parser_set_option($parserXml, XML_OPTION_CASE_FOLDING, 0);
  48. xml_parser_set_option($parserXml, XML_OPTION_SKIP_WHITE, 1);
  49. if (0 == xml_parse_into_struct($parserXml, $reqContent, $tags)) {
  50. throw new Exception("Error parsing xml");
  51. }
  52. xml_parser_free($parserXml);
  53. if (empty($tags)) {
  54. throw new Exception("Empty structure from request");
  55. }
  56. $rootTagName = V::get('tag', '', $tags[0]);
  57. if ('Transaction' == $rootTagName) return $this->_parseTransactionXmlStruct($reqContent, $tags);
  58. throw new Api_WfsException("Not implemented '{$rootTagName}' #L." . __LINE__, 501);
  59. }
  60. public function getFeatureAction() {
  61. $args = $this->parseGetFeatureArgsFromRequest();
  62. if ('hits' == $args['resultType']) {
  63. return $this->getTotalFeatures($args, $simple = true);
  64. } else {
  65. return $this->getFeatures($args, $simple = true);
  66. }
  67. }
  68. public function getFeatureAdvancedAction() {
  69. $args = $this->parseGetFeatureArgsFromRequest();
  70. if ('hits' == $args['resultType']) {
  71. return $this->getTotalFeatures($args, $simple = false);
  72. } else {
  73. if ('/@instance' == strtolower(substr($args['typeName'], -1 * strlen('/@instance')))) {
  74. return $this->getInstanceFeatures(substr($args['typeName'], 0, -1 * strlen('/@instance')), $args);
  75. }
  76. return $this->getFeatures($args, $simple = false);
  77. }
  78. }
  79. public function testOgcFilterAction() {
  80. $type = V::get('TYPENAME', '', $_REQUEST);
  81. $typeEx = explode(':', $type);
  82. $maxFeatures = V::get('MAXFEATURES', '10000', $_REQUEST, 'int');// TODO: Set Deafult Limit
  83. $ogcFilter = V::get('Filter', '', $_REQUEST);
  84. $srsname = V::get('SRSNAME', '', $_REQUEST);// eg. EPSG:4326
  85. if (count($typeEx) == 2) {
  86. Lib::loadClass('ParseOgcFilter');
  87. $parser = new ParseOgcFilter();
  88. $parser->loadOgcFilter($ogcFilter);
  89. $queryWhereBuilder = $parser->convertToSqlQueryWhereBuilder();
  90. echo $queryWhereBuilder->getQueryWhere('t');
  91. } else {
  92. throw new HttpException("Wrong param TYPENAME", 400);
  93. }
  94. }
  95. public function getTotalFeatures($args, $simple = true) {
  96. DBG::log("typeName({$args['xsd:type']})");
  97. $acl = $this->getAclFromTypeName($args['xsd:type']);
  98. DBG::log([ 'msg'=>"typeName({$args['xsd:type']}) - acl(".get_class($acl).")", '$acl'=>$acl ]);
  99. $baseNsUri = Api_WfsNs::getBaseWfsUri();
  100. $rootWfsNs = 'p5';
  101. $rootWfsNsUri = "{$baseNsUri}";
  102. $wfsNs = $args['typePrefix'];
  103. $wfsNsUri = "{$baseNsUri}/" . ('p5_' == substr($args['typePrefix'], 0, 3)) ? substr($args['typePrefix'], 3) : $args['typePrefix'];
  104. $featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$args['xsd:type']}&REQUEST=DescribeFeatureType";
  105. DBG::log("ogcFilter(" . strlen($args['ogc:filter']) . "): {$args['ogc:filter']}");
  106. $searchParams = array();
  107. $searchParams['limit'] = $args['limit'];
  108. $searchParams['limitstart'] = $args['offset'];
  109. if (!empty($args['sortBy'])) {
  110. $searchParams['sortBy'] = $args['sortBy'];
  111. } else {
  112. $searchParams['order_by'] = $acl->getPrimaryKeyField();
  113. $searchParams['order_dir'] = 'DESC';
  114. }
  115. if (strlen($args['ogc:filter']) > 0) $searchParams['ogc:Filter'] = $args['ogc:filter'];
  116. if (!empty($args['filterFields'])) $searchParams['cols'] = $args['filterFields'];// propertyName
  117. if (!empty($args['primaryKey'])) $searchParams['primaryKey'] = $args['primaryKey'];// featureID
  118. if (!empty($args['bbox'])) $searchParams['f_the_geom'] = "BBOX:{$args['bbox']}";
  119. $queryFeatures = $acl->buildQuery($searchParams);
  120. $totalItems = $queryFeatures->getTotal();
  121. $xmlWriter = new XMLWriter();
  122. if (!$xmlWriter) throw new HttpException("Error no XMLWriter", 404);
  123. $xmlWriter->openUri('php://output');
  124. $xmlWriter->setIndent(true);
  125. $xmlWriter->startDocument('1.0','UTF-8');
  126. $xmlWriter->startElement('wfs:FeatureCollection');
  127. $xmlWriter->writeAttribute('xmlns:wfs', 'http://www.opengis.net/wfs/2.0');
  128. $xmlWriter->writeAttribute('xmlns', 'http://www.opengis.net/wfs/2.0');
  129. $xmlWriter->writeAttribute('xmlns:gml', 'http://www.opengis.net/gml');
  130. $xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  131. // $xmlWriter->writeAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}");
  132. $xmlWriter->writeAttribute('numberMatched', $totalItems);
  133. $xmlWriter->writeAttribute('numberReturned', 0);
  134. // $xmlWriter->writeAttribute('timeStamp', "TODO: timestamp like '2011-12-09T11:30:16'");
  135. $xmlWriter->endElement();// wfs:FeatureCollection
  136. $xmlWriter->endDocument();
  137. exit;
  138. }
  139. public function getFeatures($args, $simple = true) {
  140. $type = $args['typeName'];
  141. DBG::log("typeName({$args['xsd:type']})");
  142. $acl = $this->getAclFromTypeName($args['xsd:type']);
  143. DBG::log([ 'msg'=>"typeName({$args['xsd:type']}) - acl(".get_class($acl).")", '$acl'=>$acl ]);
  144. $baseNsUri = Api_WfsNs::getBaseWfsUri();
  145. $rootWfsNs = 'p5';
  146. $rootWfsNsUri = "{$baseNsUri}";
  147. $wfsNs = $args['typePrefix'];
  148. $wfsNsUri = "{$baseNsUri}/" . str_replace('__x3A__', '/', ('p5_' === substr($args['typePrefix'], 0, 3) ? substr($args['typePrefix'], 3) : $args['typePrefix']));
  149. $featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$args['xsd:type']}&REQUEST=DescribeFeatureType";
  150. DBG::log("ogcFilter(" . strlen($args['ogc:filter']) . "): {$args['ogc:filter']}");
  151. $searchParams = array();
  152. $searchParams['limit'] = $args['limit'];
  153. $searchParams['limitstart'] = $args['offset'];
  154. if (!empty($args['sortBy'])) {
  155. $searchParams['sortBy'] = $args['sortBy'];
  156. } else {
  157. $searchParams['order_by'] = $acl->getPrimaryKeyField();
  158. $searchParams['order_dir'] = 'DESC';
  159. }
  160. if (strlen($args['ogc:filter']) > 0) $searchParams['ogc:Filter'] = $args['ogc:filter'];
  161. if (!empty($args['filterFields'])) $searchParams['cols'] = $args['filterFields'];// PropertyName
  162. if (!empty($args['primaryKey'])) $searchParams['primaryKey'] = $args['primaryKey'];// featureID
  163. if (!empty($args['bbox'])) $searchParams['f_the_geom'] = "BBOX:{$args['bbox']}";
  164. $contextFieldList = []; // convert $args['filterFields'] to field list
  165. $schemaCache = array();
  166. try {
  167. $acl__getAllFieldNames = function ($listFields) {
  168. return array_map(function ($field) {
  169. return $field['fieldNamespace'];
  170. }, $listFields);
  171. };
  172. $acl__getLocalFieldNames = function ($listFields) {
  173. return array_map(function ($field) {
  174. return $field['fieldNamespace'];
  175. }, array_filter($listFields, function ($field) {
  176. return $field['isLocal'];
  177. }));
  178. };
  179. DBG::log($args, 'array', "\$args");
  180. DBG::log($acl->getFields(), 'array', "\$contextFieldList ACL fields");
  181. if (empty($args['filterFields'])) { // get all local fields
  182. // $contextFieldList = $acl__getLocalFieldNames($acl->getFields());
  183. $contextFieldList = $acl__getAllFieldNames($acl->getFields());
  184. } else {
  185. foreach ($args['filterFields'] as $fieldXPath) {
  186. if ('*' === $fieldXPath) {
  187. $contextFieldList = array_merge($contextFieldList, $acl__getLocalFieldNames($acl->getFields()));
  188. } else if (false === strpos($fieldXPath, '/') && false === strpos($fieldXPath, ':')) {
  189. $contextFieldList[] = $fieldXPath;
  190. } else if (false === strpos($fieldXPath, '/') && false !== strpos($fieldXPath, ':')) {
  191. $contextFieldList[] = $fieldXPath;
  192. $fieldNs = str_replace(['__x3A__', ':'], '/', $fieldXPath);
  193. $schemaCache[$fieldNs] = SchemaFactory::loadDefaultObject('SystemObject')->getItem($fieldNs, [ 'propertyName' => '*,field' ]);
  194. DBG::log($schemaCache[$fieldNs], 'array', "\$schemaCache[{$fieldNs}]");
  195. } else if ('/*' === substr($fieldXPath, -2) && false === strpos(substr($fieldXPath, 0, -2), '/')) {
  196. $fieldName = substr($fieldXPath, 0, -2);
  197. $contextFieldList[] = $fieldName;
  198. $xsdType = $acl->getXsdFieldType($fieldName);
  199. if ('ref:' !== substr($xsdType, 0, 4)) throw new Exception("Error Processing Request - field '{$fieldXPath}' type is not ref '/*' is not allowed");
  200. $fieldNs = str_replace(['__x3A__', ':'], '/', substr($xsdType, 4));
  201. if (!array_key_exists($fieldNs, $schemaCache)) {
  202. $schemaCache[$fieldNs] = SchemaFactory::loadDefaultObject('SystemObject')->getItem($fieldNs, [ 'propertyName' => '*,field' ]);
  203. DBG::log($schemaCache[$fieldNs], 'array', "\$schemaCache[{$fieldNs}]");
  204. }
  205. $fieldPrefix = "{$fieldName}";
  206. $contextFieldList = array_merge($contextFieldList, array_map(function ($fieldName) use ($fieldPrefix) {
  207. return "{$fieldPrefix}/{$fieldName}";
  208. }, $acl__getLocalFieldNames($schemaCache[$fieldNs]['field'])));
  209. } else {
  210. $fieldName = trim($fieldXPath, '*/');
  211. DBG::log(['$fieldXPath'=>$fieldXPath, '$fieldName'=>$fieldName], 'array', "\$contextFieldList TODO");
  212. if (false !== strpos($fieldName, '/')) {
  213. $xpathParts = explode('/', $fieldName);
  214. DBG::log($xpathParts, 'array', "\$xpathParts recurse TODO");
  215. $localFieldName = array_shift($xpathParts);
  216. DBG::log($localFieldName, 'array', "\$xpathParts recurse TODO \$localFieldName");
  217. if ('*' === end($xpathParts)) array_pop($xpathParts);
  218. if (!empty($xpathParts)) {
  219. foreach ($xpathParts as $part) {
  220. if (false !== strpos($part, ':')) {
  221. $fieldNs = str_replace(['__x3A__', ':'], '/', $part);
  222. if (!array_key_exists($fieldNs, $schemaCache)) {
  223. $schemaCache[$fieldNs] = SchemaFactory::loadDefaultObject('SystemObject')->getItem($fieldNs, [ 'propertyName' => '*,field' ]);
  224. DBG::log($schemaCache[$fieldNs], 'array', "\$schemaCache[{$fieldNs}] recurse TODO");
  225. }
  226. }
  227. }
  228. $contextFieldList[] = $localFieldName . "/" . implode("/", $xpathParts) . "/*";
  229. }
  230. // 'default_db__x3A__CRM_PROCES:PROCES/ID',
  231. // 'default_db__x3A__CRM_PROCES:PROCES/default_db__x3A__CRM_WSKAZNIK:CRM_WSKAZNIK/*',
  232. }
  233. }
  234. }
  235. }
  236. DBG::log($contextFieldList, 'array', "\$contextFieldList");
  237. DBG::log(array_keys($schemaCache), 'array', "\$schemaCache keys");
  238. $searchParams['cols'] = $contextFieldList;
  239. } catch (Exception $e) {
  240. DBG::log($e);
  241. throw $e;
  242. }
  243. DBG::log($searchParams, 'string', 'getItems - $searchParams');
  244. $queryFeatures = $acl->buildQuery($searchParams);
  245. $items = $queryFeatures->getItems();
  246. DBG::log($items, 'array', 'getItems - $items');
  247. header('Content-type: application/xml; charset=utf-8');
  248. $xmlWriter = new Core_XmlWriter();
  249. $xmlWriter->openUri('php://output');
  250. // $xmlWriter->openMemory();// DBG
  251. $xmlWriter->setIndent(true);
  252. if (!$xmlWriter) throw new HttpException("Error no XMLWriter", 404);
  253. $xmlWriter->startDocument('1.0','UTF-8');
  254. //$xmlWriter->startElementNS('wfs', 'FeatureCollection', 'http://www.opengis.net/wfs');
  255. $xmlWriter->startElement('wfs:FeatureCollection');
  256. // $xmlWriter->writeAttributeNS('xmlns', 'wfs', 'http://www.w3.org/2000/xmlns/', 'http://www.opengis.net/wfs');
  257. $xmlWriter->writeAttribute('xmlns:wfs', 'http://www.opengis.net/wfs');
  258. $xmlWriter->writeAttribute('xmlns', 'http://www.opengis.net/wfs');
  259. $xmlWriter->writeAttribute('xmlns:gml', 'http://www.opengis.net/gml');
  260. $xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  261. $xmlWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
  262. $xlmns = [];
  263. $xlmns[ $wfsNs ] = $wfsNsUri;
  264. if (!$simple) $xlmns[ $rootWfsNs ] = $rootWfsNsUri;
  265. foreach ($schemaCache as $childSchema) {
  266. $xlmns[ $childSchema['nsPrefix'] ] = "{$rootWfsNsUri}/" . str_replace('__x3A__', '/', $childSchema['nsPrefix']);
  267. }
  268. foreach ($xlmns as $prefixXmlns => $urlXmlns) {
  269. $xmlWriter->writeAttribute("xmlns:{$prefixXmlns}", $urlXmlns);
  270. }
  271. $xmlWriter->writeAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}"); // TODO: BUG $wfsNsUri
  272. $xmlWriter->writeAttribute('numberMatched', 'unknown'); // TODO: return total items if simple query (without prefix, small total number, maxFeatures set, etc.)
  273. // NOTE: for client: if numberMatched == 'unknown' then request with resultType = 'hits'
  274. $xmlWriter->writeAttribute('numberReturned', count($items));
  275. $tblName = $acl->getName();
  276. $primaryKeyField = $acl->getPrimaryKeyField();
  277. foreach ($items as $item) {
  278. $itemKey = V::get($primaryKeyField, '', $item);
  279. if (!is_array($item)) $item = (array)$item;
  280. if (!empty($geomFld)) DBG::log(['msg'=>"item[{$itemKey}] ({$geomFld})isEmpty(".empty($item[$geomFld])."):", '$item['.$geomFld.']'=>$item[$geomFld]]);
  281. DBG::log([ 'msg'=>">>> loop({$itemKey})", '$item'=>$item ]);
  282. $xmlWriter->startElement('gml:featureMember');
  283. Lib::loadClass('Api_Wfs_GetFeature');
  284. Api_Wfs_GetFeature::printXmlFeatureRecurse($xmlWriter, $acl, $item, $tagName = "{$wfsNs}:{$type}", array_merge(
  285. [
  286. 'fid' => "{$type}.{$itemKey}",
  287. ],
  288. (!$simple)
  289. ? [ "{$rootWfsNs}:web_link" => Request::getPathUri() . "index.php?_route=ViewTableAjax&namespace=" . $acl->getNamespace() . "#EDIT/{$itemKey}" ]
  290. : []
  291. ), $showAdvancedAttrs = !$simple, $schemaCache);
  292. $xmlWriter->endElement();// gml:featureMember
  293. }
  294. $xmlWriter->endElement();// wfs:FeatureCollection
  295. $xmlWriter->endDocument();
  296. exit;
  297. }
  298. public function describeFeatureTypeAction() {
  299. $type = V::get('TYPENAME', '', $_REQUEST);
  300. if (empty($type)) {
  301. $reqContent = Request::getRequestBody();
  302. if (!empty($reqContent)) {
  303. return $this->_parseDescribeFeatureTypeRequest($reqContent);
  304. } else {
  305. return $this->_getDescribeFeatureAllTypes();
  306. }
  307. //throw new HttpException("Wrong param TYPENAME", 400);
  308. }
  309. $typeEx = explode(':', $type);
  310. if (count($typeEx) != 2) throw new HttpException("Wrong param TYPENAME", 400);
  311. return $this->_getDescribeFeatureType($typeEx[0], $typeEx[1]);
  312. }
  313. public function describeFeatureTypeAdvancedAction() {
  314. $typeName = V::geti('TYPENAME', '', $_REQUEST);
  315. if (empty($typeName)) {
  316. $reqContent = Request::getRequestBody();
  317. if (!empty($reqContent)) {
  318. return $this->_parseDescribeFeatureTypeRequest($reqContent, $simple = false);
  319. } else {
  320. return $this->_getDescribeFeatureAllTypes($simple = false);
  321. }
  322. //throw new HttpException("Wrong param TYPENAME", 400);
  323. }
  324. if (false === strpos($typeName, ':')) throw new HttpException("Wrong param TYPENAME", 400);
  325. list($nsPrefix, $name) = explode(':', $typeName);
  326. if ('/@instance' == strtolower(substr($name, -1 * strlen('/@instance')))) {
  327. return $this->_describeInstanceAttributeTable($nsPrefix, substr($name, 0, -1 * strlen('/@instance')));
  328. }
  329. return $this->_getDescribeFeatureType($nsPrefix, $name, $simple = false);
  330. }
  331. public function getCapabilitiesAction() {
  332. $wfsServerUrl = $this->getBaseUri();
  333. $serviceTitle = "Web Feature Service";
  334. $serviceDescription = "This is the reference implementation of WFS 1.0.0 and WFS 1.1.0, supports all WFS operations including Transaction.";
  335. $idDefaultDB = DB::getPDO()->getZasobId();
  336. $aclList = array_filter($this->_usrAcl->getTablesAcl(), function ($acl) use ($idDefaultDB) {
  337. // // $dataSourceName = 'default_db';// TODO: getSourceName
  338. // // $tblName = $tblAcl->getName();
  339. // // try {
  340. // // $acl = $this->getAclFromTypeName($typeName = "p5_{$dataSourceName}:{$tblName}");
  341. // // } catch (Exception $e) {
  342. // // // echo "Error for table({$tblName}): " . $e->getMessage() . "\n";
  343. // // }
  344. // // if (!$acl) {
  345. // // // TODO: error log msg
  346. // // return false;
  347. // // }
  348. return ($idDefaultDB == $acl->getDB()); // hide non default_db tables
  349. });
  350. switch (V::get('outputFormat', 'xml', $_GET)) {
  351. case 'csv': {
  352. (new Api_Wfs_GetCapabilities)->getCapabilitiesCsv($wfsServerUrl, $serviceTitle, $serviceDescription, $aclList);
  353. } break;
  354. case 'xml': {
  355. (new Api_Wfs_GetCapabilities)->getCapabilitiesXml($wfsServerUrl, $serviceTitle, $serviceDescription, $aclList);
  356. } break;
  357. default: throw new Api_WfsException("Not Implemented outputFormat", 501); // , null, 'NotImplemented', 'request');
  358. }
  359. exit;
  360. }
  361. }