WfsDataServer.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. $fldList = $this->_getFieldListFromAcl($acl);
  100. $baseNsUri = Api_WfsNs::getBaseWfsUri();
  101. $rootWfsNs = 'p5';
  102. $rootWfsNsUri = "{$baseNsUri}";
  103. $wfsNs = $args['typePrefix'];
  104. $wfsNsUri = "{$baseNsUri}/" . ('p5_' == substr($args['typePrefix'], 0, 3)) ? substr($args['typePrefix'], 3) : $args['typePrefix'];
  105. $featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$args['xsd:type']}&REQUEST=DescribeFeatureType";
  106. // get BBox from geom_field (only one geom fld is allowed)
  107. $geomFld = null;
  108. {
  109. foreach ($fldList as $fldName) {
  110. if ($acl->isGeomField($fldName)) {
  111. $geomFld = $fldName;
  112. }
  113. }
  114. }
  115. DBG::log("ogcFilter(" . strlen($args['ogc:filter']) . "): {$args['ogc:filter']}");
  116. $searchParams = array();
  117. $searchParams['limit'] = $args['limit'];
  118. $searchParams['limitstart'] = $args['offset'];
  119. if (!empty($args['sortBy'])) {
  120. $searchParams['sortBy'] = $args['sortBy'];
  121. } else {
  122. $searchParams['order_by'] = $acl->getPrimaryKeyField();
  123. $searchParams['order_dir'] = 'DESC';
  124. }
  125. if (strlen($args['ogc:filter']) > 0) $searchParams['ogc:Filter'] = $args['ogc:filter'];
  126. if (!empty($args['filterFields'])) $searchParams['cols'] = $args['filterFields'];// propertyName
  127. if (!empty($args['primaryKey'])) $searchParams['primaryKey'] = $args['primaryKey'];// featureID
  128. if (!empty($args['bbox'])) $searchParams['f_the_geom'] = "BBOX:{$args['bbox']}";
  129. $queryFeatures = $acl->buildQuery($searchParams);
  130. $totalItems = $queryFeatures->getTotal();
  131. $xmlWriter = new XMLWriter();
  132. if (!$xmlWriter) throw new HttpException("Error no XMLWriter", 404);
  133. $xmlWriter->openUri('php://output');
  134. $xmlWriter->setIndent(true);
  135. $xmlWriter->startDocument('1.0','UTF-8');
  136. $xmlWriter->startElement('wfs:FeatureCollection');
  137. $xmlWriter->writeAttribute('xmlns:wfs', 'http://www.opengis.net/wfs/2.0');
  138. $xmlWriter->writeAttribute('xmlns', 'http://www.opengis.net/wfs/2.0');
  139. $xmlWriter->writeAttribute('xmlns:gml', 'http://www.opengis.net/gml');
  140. $xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  141. // $xmlWriter->writeAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}");
  142. $xmlWriter->writeAttribute('numberMatched', $totalItems);
  143. $xmlWriter->writeAttribute('numberReturned', 0);
  144. // $xmlWriter->writeAttribute('timeStamp', "TODO: timestamp like '2011-12-09T11:30:16'");
  145. $xmlWriter->endElement();// wfs:FeatureCollection
  146. $xmlWriter->endDocument();
  147. exit;
  148. }
  149. public function getFeatures($args, $simple = true) {
  150. $type = $args['typeName'];
  151. DBG::log("typeName({$args['xsd:type']})");
  152. $acl = $this->getAclFromTypeName($args['xsd:type']);
  153. DBG::log([ 'msg'=>"typeName({$args['xsd:type']}) - acl(".get_class($acl).")", '$acl'=>$acl ]);
  154. $fldList = $this->_getFieldListFromAcl($acl);
  155. $baseNsUri = Api_WfsNs::getBaseWfsUri();
  156. $rootWfsNs = 'p5';
  157. $rootWfsNsUri = "{$baseNsUri}";
  158. $wfsNs = $args['typePrefix'];
  159. $wfsNsUri = "{$baseNsUri}/" . ('p5_' === substr($args['typePrefix'], 0, 3) ? substr($args['typePrefix'], 3) : $args['typePrefix']);
  160. $featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$args['xsd:type']}&REQUEST=DescribeFeatureType";
  161. // get BBox from geom_field (only one geom fld is allowed)
  162. $geomFld = null;
  163. {
  164. foreach ($fldList as $fldName) {
  165. if ($acl->isGeomField($fldName)) {
  166. $geomFld = $fldName;
  167. }
  168. }
  169. }
  170. DBG::log("ogcFilter(" . strlen($args['ogc:filter']) . "): {$args['ogc:filter']}");
  171. $searchParams = array();
  172. $searchParams['limit'] = $args['limit'];
  173. $searchParams['limitstart'] = $args['offset'];
  174. if (!empty($args['sortBy'])) {
  175. $searchParams['sortBy'] = $args['sortBy'];
  176. } else {
  177. $searchParams['order_by'] = $acl->getPrimaryKeyField();
  178. $searchParams['order_dir'] = 'DESC';
  179. }
  180. if (strlen($args['ogc:filter']) > 0) $searchParams['ogc:Filter'] = $args['ogc:filter'];
  181. if (!empty($args['filterFields'])) $searchParams['cols'] = $args['filterFields'];// PropertyName
  182. if (!empty($args['primaryKey'])) $searchParams['primaryKey'] = $args['primaryKey'];// featureID
  183. if (!empty($args['bbox'])) $searchParams['f_the_geom'] = "BBOX:{$args['bbox']}";
  184. $contextFieldList = []; // convert $args['filterFields'] to field list
  185. $schemaCache = array();
  186. try {
  187. $acl__getAllFieldNames = function ($listFields) {
  188. return array_map(function ($field) {
  189. return $field['fieldNamespace'];
  190. }, $listFields);
  191. };
  192. $acl__getLocalFieldNames = function ($listFields) {
  193. return array_map(function ($field) {
  194. return $field['fieldNamespace'];
  195. }, array_filter($listFields, function ($field) {
  196. return $field['isLocal'];
  197. }));
  198. };
  199. DBG::log($acl->getFields(), 'array', "\$contextFieldList ACL fields");
  200. if (empty($args['filterFields'])) { // get all local fields
  201. // $contextFieldList = $acl__getLocalFieldNames($acl->getFields());
  202. $contextFieldList = $acl__getAllFieldNames($acl->getFields());
  203. } else {
  204. foreach ($args['filterFields'] as $fieldXPath) {
  205. if ('*' === $fieldXPath) {
  206. $contextFieldList = array_merge($contextFieldList, $acl__getLocalFieldNames($acl->getFields()));
  207. } else if (false === strpos($fieldXPath, '/') && false === strpos($fieldXPath, ':')) {
  208. $contextFieldList[] = $fieldXPath;
  209. } else if (false === strpos($fieldXPath, '/') && false !== strpos($fieldXPath, ':')) {
  210. $contextFieldList[] = $fieldXPath;
  211. $fieldNs = str_replace(['__x3A__', ':'], '/', $fieldXPath);
  212. $schemaCache[$fieldNs] = SchemaFactory::loadDefaultObject('SystemObject')->getItem($fieldNs, [ 'propertyName' => '*,field' ]);
  213. DBG::log($schemaCache[$fieldNs], 'array', "\$schemaCache[{$fieldNs}]");
  214. } else if ('/*' === substr($fieldXPath, -2) && false === strpos(substr($fieldXPath, 0, -2), '/')) {
  215. $fieldName = substr($fieldXPath, 0, -2);
  216. $contextFieldList[] = $fieldName;
  217. $xsdType = $acl->getXsdFieldType($fieldName);
  218. if ('ref:' !== substr($xsdType, 0, 4)) throw new Exception("Error Processing Request - field '{$fieldXPath}' type is not ref '/*' is not allowed");
  219. $fieldNs = str_replace(['__x3A__', ':'], '/', substr($xsdType, 4));
  220. if (!array_key_exists($fieldNs, $schemaCache)) {
  221. $schemaCache[$fieldNs] = SchemaFactory::loadDefaultObject('SystemObject')->getItem($fieldNs, [ 'propertyName' => '*,field' ]);
  222. DBG::log($schemaCache[$fieldNs], 'array', "\$schemaCache[{$fieldNs}]");
  223. }
  224. $fieldPrefix = "{$fieldName}";
  225. $contextFieldList = array_merge($contextFieldList, array_map(function ($fieldName) use ($fieldPrefix) {
  226. return "{$fieldPrefix}/{$fieldName}";
  227. }, $acl__getLocalFieldNames($schemaCache[$fieldNs]['field'])));
  228. } else {
  229. $fieldName = trim($fieldXPath, '*/');
  230. DBG::log(['$fieldXPath'=>$fieldXPath, '$fieldName'=>$fieldName], 'array', "\$contextFieldList TODO");
  231. }
  232. }
  233. }
  234. DBG::log($contextFieldList, 'array', "\$contextFieldList");
  235. $searchParams['cols'] = $contextFieldList;
  236. } catch (Exception $e) {
  237. DBG::log($e);
  238. throw $e;
  239. }
  240. DBG::log([ 'msg'=>'getItems - $searchParams', '$searchParams'=>$searchParams ]);
  241. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">get_total (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($jsonData->total);echo'</pre>';}
  242. $queryFeatures = $acl->buildQuery($searchParams);
  243. $items = $queryFeatures->getItems();
  244. DBG::log([ 'msg'=>'getItems - $items', '$items'=>$items ]);
  245. header('Content-type: application/xml; charset=utf-8');
  246. $xmlWriter = new Core_XmlWriter();
  247. $xmlWriter->openUri('php://output');
  248. // $xmlWriter->openMemory();// DBG
  249. $xmlWriter->setIndent(true);
  250. if (!$xmlWriter) throw new HttpException("Error no XMLWriter", 404);
  251. $xmlWriter->startDocument('1.0','UTF-8');
  252. //$xmlWriter->startElementNS('wfs', 'FeatureCollection', 'http://www.opengis.net/wfs');
  253. $xmlWriter->startElement('wfs:FeatureCollection');
  254. // $xmlWriter->writeAttributeNS('xmlns', 'wfs', 'http://www.w3.org/2000/xmlns/', 'http://www.opengis.net/wfs');
  255. $xmlWriter->writeAttribute('xmlns:wfs', 'http://www.opengis.net/wfs');
  256. $xmlWriter->writeAttribute('xmlns', 'http://www.opengis.net/wfs');
  257. $xmlWriter->writeAttribute('xmlns:gml', 'http://www.opengis.net/gml');
  258. $xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  259. $xmlWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
  260. $xmlWriter->writeAttribute("xmlns:{$wfsNs}", $wfsNsUri); // TODO: BUG $wfsNsUri
  261. if (!$simple) $xmlWriter->writeAttribute("xmlns:{$rootWfsNs}", $rootWfsNsUri);
  262. foreach ($schemaCache as $childSchema) {
  263. $xmlWriter->writeAttribute("xmlns:{$childSchema['nsPrefix']}", "{$rootWfsNsUri}/" . str_replace('__x3A__', '/', $childSchema['nsPrefix']));
  264. }
  265. $xmlWriter->writeAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}"); // TODO: BUG $wfsNsUri
  266. $xmlWriter->writeAttribute('numberMatched', 'unknown'); // TODO: return total items if simple query (without prefix, small total number, maxFeatures set, etc.)
  267. // NOTE: for client: if numberMatched == 'unknown' then request with resultType = 'hits'
  268. $xmlWriter->writeAttribute('numberReturned', count($items));
  269. $tblName = $acl->getName();
  270. $primaryKeyField = $acl->getPrimaryKeyField();
  271. foreach ($items as $item) {
  272. $itemKey = V::get($primaryKeyField, '', $item);
  273. if (!is_array($item)) $item = (array)$item;
  274. if (!empty($geomFld)) DBG::log(['msg'=>"item[{$itemKey}] ({$geomFld})isEmpty(".empty($item[$geomFld])."):", '$item['.$geomFld.']'=>$item[$geomFld]]);
  275. DBG::log([ 'msg'=>">>> loop({$itemKey})", '$item'=>$item ]);
  276. $xmlWriter->startElement('gml:featureMember');
  277. $xmlWriter->startElement("{$wfsNs}:{$type}");
  278. $xmlWriter->writeAttribute('fid', "{$type}.{$itemKey}");
  279. if (!$simple) $xmlWriter->writeAttribute("{$rootWfsNs}:web_link", Request::getPathUri() . "index.php?_route=ViewTableAjax&namespace=" . $acl->getNamespace() . "#EDIT/{$itemKey}");
  280. foreach ($fldList as $idZasob => $fldName) {
  281. if(V::get('DBG_LOOP','',$_GET))DBG::log([ 'msg'=>">>> loop({$itemKey}) item({$item['ID']}) fld({$fldName})", '$item'=>$item[$fldName] ]);
  282. $fldType = $acl->getXsdFieldType($fldName);
  283. if (!$acl->canReadObjectField($fldName, (object)$item)) continue;
  284. if ($geomFld != null && $fldName == $geomFld) {
  285. $xmlWriter->startElement("{$wfsNs}:{$fldName}");
  286. if (!$simple && !$acl->canReadObjectField($fldName, (object)$item)) {
  287. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_read", "false");
  288. }
  289. if (!$simple && $acl->canWriteObjectField($fldName, (object)$item)) {
  290. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_write", "true");
  291. }
  292. $this->_typeConverter->createGmlFromWkt_xmlWriter($item[$fldName], $xmlWriter);
  293. $xmlWriter->endElement();// {$wfsNs}:{$fldName}
  294. } else if (is_array($item[$fldName])) {// TODO: by struct - REF field
  295. DBG::log([ 'msg'=>">>> loop({$itemKey}) REF item[{$itemKey}][{$fldName}]", '$item'=>$item[$fldName] ]);
  296. if (empty($item[$fldName])) {
  297. $xmlWriter->h($fldName);
  298. // } else if (1 == count($item[$fldName]) && !empty($item[$fldName][0]['xlink'])) {
  299. // $xlink = $item[$fldName][0]['xlink'];
  300. // $xlinkParts = explode(':', $xlink);
  301. // if (2 != count($xlinkParts)) throw new Exception("Error Processing Request - wrong xlink format for ".$acl->getName().".{$itemKey}/{$fldName}");
  302. // $xlinkParts[0] = Api_WfsNs::getNsUri($xlinkParts[0]);
  303. // $xlink = implode('#', $xlinkParts);
  304. // $xmlWriter->startElement("{$wfsNs}:{$fldName}");
  305. // if (!$simple && !$acl->canReadObjectField($fldName, (object)$item)) {
  306. // $xmlWriter->writeAttribute("{$rootWfsNs}:allow_read", "false");
  307. // }
  308. // if (!$simple && $acl->canWriteObjectField($fldName, (object)$item)) {
  309. // $xmlWriter->writeAttribute("{$rootWfsNs}:allow_write", "true");
  310. // }
  311. // $xmlWriter->writeAttribute('xlink:href', $xlink);
  312. // $xmlWriter->endElement();// {$wfsNs}:{$fldName}
  313. } else {
  314. // $xmlWriter->writeComment("TODO: ".$acl->getName().".{$itemKey}/{$fldName} ...");
  315. $fieldNs = str_replace(['__x3A__', ':'], '/', $fldName); // substr($xsdType, 4));
  316. if (!array_key_exists($fieldNs, $schemaCache)) {
  317. $xmlWriter->writeComment("Error Processing Request - field is not ref ".$acl->getName().".{$itemKey}/{$fldName}");
  318. } else {
  319. foreach ($item[$fldName] as $refItem) {
  320. DBG::log($refItem, 'array', "\$refItem fld({$fldName})");
  321. if (1 == count($refItem) && !empty($refItem['xlink'])) {
  322. $xmlWriter->startElement($schemaCache[$fieldNs]['typeName']);
  323. $xmlWriter->writeAttribute("xlink:href", $refItem['xlink']);
  324. $xmlWriter->endElement();
  325. } else {
  326. $xmlWriter->startElement($schemaCache[$fieldNs]['typeName']);
  327. foreach ($schemaCache[$fieldNs]['field'] as $field) {
  328. // $xmlWriter->writeComment("REF field ({$field['fieldNamespace']}) value({$refItem[$field['fieldNamespace']]})");
  329. if (array_key_exists($field['fieldNamespace'], $refItem)) {
  330. $xmlWriter->startElement("{$schemaCache[$fieldNs]['nsPrefix']}:{$field['fieldNamespace']}");
  331. $xmlWriter->text($refItem[$field['fieldNamespace']]);
  332. $xmlWriter->endElement();
  333. }
  334. }
  335. $xmlWriter->endElement();
  336. }
  337. }
  338. }
  339. }
  340. } else if ('xsd:base64Binary' === $acl->getXsdFieldType($fldName)) {
  341. if (empty($item[$fldName]) && '0' !== $item[$fldName]) continue;
  342. $xmlWriter->startElement("{$wfsNs}:{$fldName}");
  343. if (!$simple && !$acl->canReadObjectField($fldName, (object)$item)) {
  344. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_read", "false");
  345. }
  346. if (!$simple && $acl->canWriteObjectField($fldName, (object)$item)) {
  347. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_write", "true");
  348. }
  349. $xmlWriter->text(base64_encode($item[$fldName]));
  350. $xmlWriter->endElement();// {$wfsNs}:{$fldName}
  351. } else {
  352. $value = str_replace('&', '&amp;', $item[$fldName]);
  353. if (empty($value) && '0' !== $value) {
  354. continue;
  355. } else {
  356. $xmlWriter->startElement("{$wfsNs}:{$fldName}");
  357. if (!$simple && !$acl->canReadObjectField($fldName, (object)$item)) {
  358. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_read", "false");
  359. }
  360. if (!$simple && $acl->canWriteObjectField($fldName, (object)$item)) {
  361. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_write", "true");
  362. }
  363. $xmlWriter->text($value);
  364. $xmlWriter->endElement();// {$wfsNs}:{$fldName}
  365. }
  366. }
  367. }
  368. $xmlWriter->endElement();// {$wfsNs}:{$type}
  369. $xmlWriter->endElement();// gml:featureMember
  370. }
  371. $xmlWriter->endElement();// wfs:FeatureCollection
  372. $xmlWriter->endDocument();
  373. exit;
  374. }
  375. public function describeFeatureTypeAction() {
  376. $type = V::get('TYPENAME', '', $_REQUEST);
  377. if (empty($type)) {
  378. $reqContent = Request::getRequestBody();
  379. if (!empty($reqContent)) {
  380. return $this->_parseDescribeFeatureTypeRequest($reqContent);
  381. } else {
  382. return $this->_getDescribeFeatureAllTypes();
  383. }
  384. //throw new HttpException("Wrong param TYPENAME", 400);
  385. }
  386. $typeEx = explode(':', $type);
  387. if (count($typeEx) != 2) throw new HttpException("Wrong param TYPENAME", 400);
  388. return $this->_getDescribeFeatureType($typeEx[0], $typeEx[1]);
  389. }
  390. public function describeFeatureTypeAdvancedAction() {
  391. $typeName = V::geti('TYPENAME', '', $_REQUEST);
  392. if (empty($typeName)) {
  393. $reqContent = Request::getRequestBody();
  394. if (!empty($reqContent)) {
  395. return $this->_parseDescribeFeatureTypeRequest($reqContent, $simple = false);
  396. } else {
  397. return $this->_getDescribeFeatureAllTypes($simple = false);
  398. }
  399. //throw new HttpException("Wrong param TYPENAME", 400);
  400. }
  401. if (false === strpos($typeName, ':')) throw new HttpException("Wrong param TYPENAME", 400);
  402. list($nsPrefix, $name) = explode(':', $typeName);
  403. if ('/@instance' == strtolower(substr($name, -1 * strlen('/@instance')))) {
  404. return $this->_describeInstanceAttributeTable($nsPrefix, substr($name, 0, -1 * strlen('/@instance')));
  405. }
  406. return $this->_getDescribeFeatureType($nsPrefix, $name, $simple = false);
  407. }
  408. public function getCapabilitiesAction() {
  409. $wfsServerUrl = $this->getBaseUri();
  410. $serviceTitle = "Web Feature Service";
  411. $serviceDescription = "This is the reference implementation of WFS 1.0.0 and WFS 1.1.0, supports all WFS operations including Transaction.";
  412. $idDefaultDB = DB::getPDO()->getZasobId();
  413. $aclList = array_filter($this->_usrAcl->getTablesAcl(), function ($acl) use ($idDefaultDB) {
  414. // // $dataSourceName = 'default_db';// TODO: getSourceName
  415. // // $tblName = $tblAcl->getName();
  416. // // try {
  417. // // $acl = $this->getAclFromTypeName($typeName = "p5_{$dataSourceName}:{$tblName}");
  418. // // } catch (Exception $e) {
  419. // // // echo "Error for table({$tblName}): " . $e->getMessage() . "\n";
  420. // // }
  421. // // if (!$acl) {
  422. // // // TODO: error log msg
  423. // // return false;
  424. // // }
  425. return ($idDefaultDB == $acl->getDB()); // hide non default_db tables
  426. });
  427. //header('Content-type: application/xml; charset="utf-8"');
  428. header('Content-type: application/xml');
  429. (new Api_Wfs_GetCapabilities)->getCapabilitiesXml($wfsServerUrl, $serviceTitle, $serviceDescription, $aclList);
  430. }
  431. }