WfsDataServer.php 17 KB

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