GetFeature.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. <?php
  2. Lib::loadClass('Api_WfsGeomTypeConverter');
  3. Lib::loadClass('Core_AclHelper');
  4. class Api_Wfs_GetFeature {
  5. /**
  6. * @param $_GET [ , POST Body - XML file ]
  7. * @return array $args
  8. * TODO: get $acl and check more restrictions
  9. */
  10. public static function parseGetFeatureArgsFromRequest() {
  11. $rawArgs = $_GET;// $_REQUEST; ($_POST values not allowed, only xml in post body)
  12. $args = array();
  13. $args['root'] = ('0' === User::get('ADM_ADMIN_LEVEL') && 1 === V::get('root', 0, $rawArgs, 'int')); // NOTE: require Admin perms
  14. $args['xsd:type'] = null;// @from: TYPENAME, typeName (typename)
  15. $args['typePrefix'] = null;// @from: TYPENAME, typeName (typename)
  16. $args['typeName'] = null;// @from: TYPENAME, typeName (typename)
  17. $args['srsname'] = null;// @from: SRSNAME (srsname)
  18. $args['limit'] = 0;// @from: MAXFEATURES, maxFeatures, COUNT (maxfeatures, count)
  19. $args['offset'] = 0;// @from: startIndex (startindex, count)
  20. $args['ogc:filter'] = null;// @from: FILTER, Filter (filter)
  21. $args['sortBy'] = null;// @from: sortBy (sortby)
  22. $args['wfs:propertyName'] = null;// @from: propertyName (propertyname)
  23. $args['filterFields'] = array();// array of field names to show - @from: propertyName
  24. $args['resultType'] = null;// @from: resultType (resulttype)
  25. $args['bbox'] = null;// @from: BBOX (bbox)
  26. $args['wfs:featureID'] = null;// @from: featureID, featureId (featureid)
  27. $args['primaryKey'] = null;// primaryKey type - @from: featureID or $req[primaryKey]
  28. $args['xlink'] = V::get('xlink', '', $rawArgs); // xlink = "https://biuro.biall-net.pl/wfs/default_db/CRM_PROCES#PROCES.100" but with '#' replaced with '/'
  29. $args['outputBlobFormat'] = V::get('outputBlobFormat', 'base64', $rawArgs); // how to print blob fields ('base64', 'link') - default base64
  30. $args['resolve'] = V::get('resolve', '', $rawArgs);
  31. $args['resolveDepth'] = V::get('resolveDepth', 0, $rawArgs, 'int'); // TODO: if ('*' === resolveDepth) - recurse resolve
  32. $lowerArgs = array(); foreach ($rawArgs as $name => $value) $lowerArgs[ strtolower($name) ] = trim($value);
  33. $args['xsd:type'] = V::get('typename', '', $lowerArgs);
  34. $exType = explode(':', $args['xsd:type']);
  35. if (count($exType) != 2) throw new HttpException("Wrong param TYPENAME", 400);
  36. $args['typePrefix'] = $exType[0];
  37. $args['typeName'] = $exType[1];
  38. $args['srsname'] = V::get('SRSNAME', '', $lowerArgs);// eg. EPSG:4326
  39. $args['limit'] = V::get('maxfeatures', $args['limit'], $lowerArgs, 'int');
  40. $args['limit'] = V::get('count', $args['limit'], $lowerArgs, 'int');
  41. $defaultFeaturesLimit = ('biuro.biall-net.pl' == V::get('SERVER_NAME', '', $_SERVER)) ? 50000 : 10000;
  42. if ($args['limit'] <= 0) $args['limit'] = $defaultFeaturesLimit;// default limit
  43. $args['offset'] = V::get('startindex', 0, $lowerArgs, 'int');
  44. $args['ogc:filter'] = urldecode(V::get('filter', '', $lowerArgs));
  45. if (empty($args['ogc:filter'])) {// read ogc:Filter from POST body if not defined in GET
  46. $reqBody = Request::getRequestBody();
  47. if (!empty($reqBody)) {
  48. $parsedRequest = self::parseOgcFilterRequest($reqBody);
  49. DBG::log($parsedRequest, 'array', 'parsed ogc query request');
  50. if (!empty($parsedRequest['ogc:Filter'])) $args['ogc:filter'] = $parsedRequest['ogc:Filter'];
  51. if (!empty($parsedRequest['wfs:PropertyName'])) {
  52. $args['filterFields'] = array_merge($args['filterFields'], $parsedRequest['wfs:PropertyName']);
  53. }
  54. } else if ($args['resolve'] && $args['resolveDepth'] > 0) {
  55. $args['filterFields'][] = '*' . str_repeat("/*", $args['resolveDepth'] - 1);
  56. }
  57. }
  58. $args['sortBy'] = V::get('sortby', '', $lowerArgs);// TODO: split to array of array(fieldName, (asc | desc))
  59. $args['wfs:propertyName'] = trim(V::get('propertyname', '', $lowerArgs));
  60. if (strlen($args['wfs:propertyName']) > 0) {
  61. $exFields = explode(',', $args['wfs:propertyName']);
  62. $exFields = array_map('trim', $exFields);
  63. $exFields = array_filter($exFields, [ 'V', 'filterNotEmpty' ]);
  64. $args['filterFields'] = array_merge($args['filterFields'], $exFields);
  65. }
  66. $args['resultType'] = V::get('resulttype', '', $lowerArgs);
  67. $args['bbox'] = V::get('bbox', '', $lowerArgs);
  68. if (!empty($args['bbox'])) {
  69. // BBOX may have EPSG at then end - example: ",EPSG:4326"
  70. $num = "\d+.?\d*?";// "\d+(.\d+)?"
  71. if (!preg_match("/^({$num}),({$num}),({$num}),({$num})(\,EPSG:\d+)?$/", $args['bbox'], $matches)) throw new Exception("Illegal BBOX format");
  72. // QGIS send BBOX in correct order: 54.23580872176457,18.46844302390853,54.25220902538883,18.492990600812696
  73. // first number should not be smaller then second
  74. // example $matches:
  75. // [0] => 18.492990600812696,54.23580872176457,18.46844302390853,54.25220902538883
  76. // [1] => 18.492990600812696
  77. // [2] => 54.23580872176457
  78. // [3] => 18.46844302390853
  79. // [4] => 54.25220902538883
  80. // (optional) EPGS
  81. $bboxPoints = ($matches[1] > $matches[2])
  82. ? [ $matches[1], $matches[2], $matches[3], $matches[4] ]
  83. : [ $matches[2], $matches[3], $matches[4], $matches[1] ];
  84. $args['bbox'] = implode(",", $bboxPoints);
  85. }
  86. $args['wfs:featureID'] = V::get('featureid', null, $lowerArgs);// TODO: allow multiply feature id (csv)
  87. if ($args['wfs:featureID']) {
  88. $dotPos = strpos($args['wfs:featureID'], '.');
  89. if (!$dotPos) throw new Exception("Wrong param featureID");
  90. if ($args['typeName'] != substr($args['wfs:featureID'], 0, $dotPos)) throw new Exception("Wrong typeName in param featureID");
  91. $args['primaryKey'] = substr($args['wfs:featureID'], $dotPos + 1);
  92. }
  93. $pk = V::get('primarykey', '', $lowerArgs);
  94. if ($pk) {
  95. $args['primaryKey'] = $pk;
  96. }
  97. if(DBG::isActive() && V::get('DBG', 0, $_GET)){ echo "\$lowerArgs:";print_r($lowerArgs); }
  98. if(DBG::isActive() && V::get('DBG', 0, $_GET)){ echo "\$args:";print_r($args); }
  99. return $args;
  100. }
  101. public static function parseOgcFilterRequest($requestOgcFilter) {
  102. // GetFeature: @maxFeatures, @traverseXlinkDepth, @traverseXlinkExpiry
  103. // \-- (1..) wfs:Query: @typeName
  104. // \--- (0..) ---: wfs:PropertyName
  105. // : wfs:XlinkPropertyName
  106. // : ogc:Function
  107. // ogc:Filter
  108. // ogc:SortBy
  109. // \--- (1..) ogc:SortProperty
  110. // \---: ogc:PropertyName
  111. // : ogc:SortOrder ( DESC / ASC )
  112. // <ogc:SortBy>
  113. // <ogc:SortProperty>
  114. // <ogc:PropertyName>ASTA</ogc:PropertyName>
  115. // <ogc:SortOrder>ASC</ogc:SortOrder>
  116. // </ogc:SortProperty>
  117. // </ogc:SortBy>
  118. if (empty($requestOgcFilter)) return '';
  119. $requestXml = new DOMDocument();
  120. $requestXml->loadXml($requestOgcFilter);
  121. $rootNode = $requestXml->documentElement;
  122. // if ($rootNode->getAttribute('resolve'))
  123. DBG::log([$rootNode->getAttribute('resolve'), $rootNode->getAttribute('resolveDepth')], 'array', "TODO: use wfs:GetFeature @resolve, @resolveDepth");
  124. $nodesQuery = [];
  125. foreach ($requestXml->getElementsByTagNameNS('http://www.opengis.net/wfs', 'Query') as $element) {
  126. DBG::log($element->nodeName, 'array', "main loop - wfs:Query");
  127. $nodesQuery[] = $element;
  128. }
  129. if (empty($nodesQuery)) { // legacy - try to find 'ogc:Filter'
  130. $tagsFilter = [];
  131. foreach ($requestXml->getElementsByTagNameNS('http://www.opengis.net/ogc', 'Filter') as $element) {
  132. DBG::log($element->nodeName, 'array', "loop element (0 * wfs:Query)");
  133. $tagsFilter[] = $requestXml->saveXML($element);
  134. }
  135. DBG::log($tagsFilter, 'array', "\$tagsFilter (0 * wfs:Query)");
  136. return [
  137. 'ogc:Filter' => (!empty($tagsFilter))
  138. ? reset($tagsFilter) // TODO: only one ogc:Filter allowed
  139. : ''
  140. ];
  141. }
  142. if (1 === count($nodesQuery)) {
  143. $tagsFilter = [];
  144. foreach ($requestXml->getElementsByTagNameNS('http://www.opengis.net/ogc', 'Filter') as $element) {
  145. // DBG::log($element->nodeName, 'array', "loop ogc:Filter (1 * wfs:Query)");
  146. $tagsFilter[] = $requestXml->saveXML($element);
  147. }
  148. DBG::log($tagsFilter, 'array', "\$tagsFilter (1 * wfs:Query)");
  149. $tagsWfsPropertyName = [];
  150. foreach ($requestXml->getElementsByTagNameNS('http://www.opengis.net/wfs', 'PropertyName') as $element) {
  151. // DBG::log($element->nodeValue, 'array', "loop wfs:PropertyName (1 * wfs:Query)");
  152. if ($element->getAttribute('resolve')) DBG::log([$element->getAttribute('resolve'), $element->getAttribute('resolveDepth'), $element->getAttribute('resolvePath')], 'array', "TODO: use wfs:PropertyName @resolve, @resolveDepth, @resolvePath");
  153. $value = $element->nodeValue;
  154. if (in_array($element->getAttribute('resolve'), ['all', 'local', 'remote'])) {
  155. $depth = $element->getAttribute('resolveDepth');
  156. if ('*' !== substr($value, -1)) { // TODO: propertyName is not regex
  157. if ('*' === $depth) $value .= "/**"; // TODO: resolveDepth="*" - resolve all
  158. else if ((int)$depth > 0) $value .= str_repeat("/*", $depth);
  159. }
  160. }
  161. $tagsWfsPropertyName[] = $value;
  162. }
  163. DBG::log($tagsWfsPropertyName, 'array', "\$tagsWfsPropertyName (1 * wfs:Query)");
  164. return array_filter([
  165. 'ogc:Filter' => (!empty($tagsFilter))
  166. ? reset($tagsFilter)
  167. : '',
  168. 'wfs:PropertyName' => (!empty($tagsWfsPropertyName))
  169. ? $tagsWfsPropertyName
  170. : '',
  171. ], function ($value) { return !empty($value); });
  172. }
  173. throw new Exception("Not imeplemented multiple ogc:Query"); // multiple ogc:Query require ogc:Query/@typeName
  174. }
  175. public static function convertOgcPropertyListToFeatureQueryCols(&$schemaCache, $ogcPropertyList, $acl, $isRoot = false) {
  176. return self::convertOgcPropsRecurse($schemaCache, $ogcPropertyList, $acl, $isRoot, $dbgLoopNr = 0, $dbgXpath = $acl->getNamespace());
  177. }
  178. public static function convertOgcPropsRecurse(&$schemaCache, $ogcPropertyList, $aclOrSchema, $isRoot = false, $dbgLoopNr = 0, $dbgXpath = '') {
  179. if ($dbgLoopNr > 10) {
  180. DBG::log($ogcPropertyList, 'array', 'convertOgcPropsRecurse: LOOP LIMIT 10! - return []');
  181. return [];
  182. }
  183. $acl = null;
  184. if (is_array($aclOrSchema) && !empty($aclOrSchema['namespace'])) {
  185. $acl = Core_AclHelper::getAclByNamespace($aclOrSchema['namespace'], false, $aclOrSchema);
  186. } else if ($aclOrSchema instanceof Core_AclBase) {
  187. $acl = $aclOrSchema;
  188. } else throw new Exception("Missing acl");
  189. if (empty($ogcPropertyList)) {
  190. $ogcPropertyList = array_values($acl->getFieldListByIdZasob());
  191. $ogcPropertyList = array_filter($ogcPropertyList, function ($fieldName) use ($acl, $isRoot) {
  192. return ($isRoot || $acl->canReadField($fieldName));
  193. });
  194. }
  195. $aclFields = array_filter($ogcPropertyList, function ($prop) { return ( false === strpos($prop, '/') ); });
  196. $nestedFields = array_reduce(
  197. array_filter($ogcPropertyList, function ($prop) { return ( false !== strpos($prop, '/') ); }),
  198. function ($ret, $propNested) {
  199. list($childName, $nestedPart) = explode('/', $propNested, 2);
  200. if (!array_key_exists($childName, $ret)) $ret[ $childName ] = [];
  201. $ret[ $childName ][] = $nestedPart;
  202. return $ret;
  203. },
  204. []
  205. );
  206. if (!empty($nestedFields)) {
  207. if (array_key_exists('*', $nestedFields)) { // add missing local fields if xpath like '*/...'
  208. $aclFields[] = '*';
  209. }
  210. }
  211. if (!empty($nestedFields)) { // add nested part to refs if xpath like '*/...'
  212. if (array_key_exists('*', $nestedFields)) {
  213. $nestedParts = $nestedFields['*'];
  214. unset($nestedFields['*']);
  215. DBG::log($nestedParts, 'array', "convertOgcPropsRecurse: '*' in \$nestedFields - \$nestedParts ns({$dbgXpath})");
  216. $canReadRefs = array_values($acl->getFieldListByIdZasob());
  217. $canReadRefs = array_filter($canReadRefs, function ($fieldName) use ($acl, $isRoot) {
  218. return (false !== strpos($fieldName, ":") && ($isRoot || $acl->canReadField($fieldName)));
  219. });
  220. foreach ($canReadRefs as $refField) {
  221. foreach ($nestedParts as $nestedPart) {
  222. $nestedFields[ $refField ][] = $nestedPart;
  223. }
  224. }
  225. }
  226. }
  227. if (!empty($nestedFields)) {
  228. $aclFields = array_unique(array_merge($aclFields, array_keys($nestedFields)));
  229. }
  230. if (in_array('*', $aclFields)) {
  231. $aclFields = array_filter($aclFields, function ($fieldName) { return ( '*' !== $fieldName); });
  232. $allAclFields = array_values($acl->getFieldListByIdZasob());
  233. $allAclFields = array_filter($allAclFields, function ($fieldName) use ($acl, $isRoot) {
  234. return ($isRoot || $acl->canReadField($fieldName));
  235. });
  236. if ($allAclFields) $aclFields = array_unique(array_merge($aclFields, $allAclFields));
  237. }
  238. DBG::log([$aclFields, $nestedFields, $acl->getFieldListByIdZasob()], 'array', "convertOgcPropsRecurse: splited to acl fields and nested fields ns({$dbgXpath})");
  239. foreach ($aclFields as $fieldName) {
  240. if (!$isRoot && !$acl->canReadField($fieldName)) throw new Exception("Access Denied to read field '{$fieldName}' from '" . $acl->getNamespace() . "'");
  241. }
  242. foreach ($aclFields as $fieldName) {
  243. if (false !== strpos($fieldName, ':')) {
  244. $childNs = str_replace(['__x3A__', ':'], '/', $fieldName);
  245. $schemaCache[$childNs] = SchemaFactory::loadDefaultObject('SystemObject')->getItem($childNs, [ 'propertyName' => '*,field' ]);
  246. }
  247. }
  248. $contextFieldList = $aclFields;
  249. foreach ($nestedFields as $childName => $nestedProps) {
  250. $childNs = str_replace(['__x3A__', ':'], '/', $childName);
  251. if (!array_key_exists($childNs, $schemaCache)) {
  252. $schemaCache[$childNs] = SchemaFactory::loadDefaultObject('SystemObject')->getItem($childNs, [ 'propertyName' => '*,field' ]);
  253. }
  254. $childCtxFields = array_map(function ($childCtxFld) use ($childName) {
  255. return "{$childName}/{$childCtxFld}";
  256. }, self::convertOgcPropsRecurse($schemaCache, $nestedProps, $schemaCache[$childNs], $dbgLoopNr + 1, "{$dbgXpath}/{$childName}"));
  257. DBG::log([ $contextFieldList, $childCtxFields ], 'array', "convertOgcPropsRecurse: array merge ns({$dbgXpath})");
  258. $contextFieldList = array_merge( $contextFieldList, $childCtxFields );
  259. DBG::log($contextFieldList, 'array', "convertOgcPropsRecurse: array merged ns({$dbgXpath})");
  260. }
  261. DBG::log($contextFieldList, 'array', "convertOgcPropsRecurse: return \$contextFieldList ns({$dbgXpath})");
  262. return $contextFieldList;
  263. }
  264. // TODO: add $contextAcl and context xpath to check for special perms by contextAcl
  265. /** @example:
  266. * Api_Wfs_GetFeature::printXmlFeatureRecurse($xmlWriter, $acl, $item, [
  267. * 'fields' => $searchParams['cols'],
  268. * 'tagName' => "{$wfsNs}:{$type}",
  269. * 'showAdvancedAttrs' => !$simple,
  270. * 'xsdAttributes' => array_merge(
  271. * [ 'fid' => "{$type}.{$itemKey}" ],
  272. * (!$simple)
  273. * ? [ "{$rootWfsNs}:web_link" => Request::getPathUri() . "index.php?_route=ViewTableAjax&namespace=" . $acl->getNamespace() . "#EDIT/{$itemKey}" ]
  274. * : []
  275. * )
  276. * ], $schemaCache, $printedFidLog);
  277. */
  278. public static function printXmlFeatureRecurse($xmlWriter, $acl, $item, $params = [], $schemaCache = [], $printedFidLog = []) {
  279. $showFields = V::get('fields', [], $params, 'array'); // TODO: if empty $showFields ?
  280. $tagName = V::get('tagName', '', $params);
  281. $attrs = V::get('xsdAttributes', [], $params, 'array');
  282. $showAdvancedAttrs = V::get('showAdvancedAttrs', false, $params, 'bool');
  283. $dbgFid = V::get('fid', 0, $attrs);
  284. $outputBlobFormat = V::get('outputBlobFormat', '', $params, 'word');
  285. if(V::get('DBG_XML', '', $_GET))$xmlWriter->writeComment("DBG: print Xml Feature Recurse... '{$tagName}'" . ( $dbgFid ? " fid='{$dbgFid}'" : "" )); // TODO: DBG
  286. // DBG::log($acl, 'array', "DBG: print Xml Feature Recurse( ... {$tagName}, \$acl)" . ( $dbgFid ? " fid='{$dbgFid}'" : "" ) . " \$acl");
  287. DBG::log($item, 'array', "DBG: print Xml Feature Recurse( ... {$tagName}, \$item)" . ( $dbgFid ? " fid='{$dbgFid}'" : "" ) . " \$item");
  288. DBG::log($showFields, 'array', "DBG: print Xml Feature Recurse( ... {$tagName}, \$item)" . ( $dbgFid ? " fid='{$dbgFid}'" : "" ) . " \$showFields");
  289. DBG::log([$attrs, $showAdvancedAttrs, array_keys($schemaCache), $printedFidLog], 'array', "DBG: print Xml Feature Recurse( ... {$tagName}, \$attrs, \$showAdvancedAttrs, keys(\$schemaCache), \$printedFidLog)");
  290. if ($dbgFid) {
  291. if (in_array($dbgFid, $printedFidLog)) {
  292. DBG::log("TODO: in_array({$dbgFid}, \$printedFidLog)");
  293. }
  294. }
  295. if ($dbgFid) $printedFidLog[] = $dbgFid;
  296. // $rootWfsNs = 'p5';
  297. list($itemPrefix, $localName) = explode(':', $tagName);
  298. if (1 === count($item) && !empty($item['xlink'])) {
  299. // @example 'xlink' => 'https://biuro.biall-net.pl/wfs/default_db/CRM_PROCES#PROCES.857'
  300. $xlink = $item['xlink'];
  301. list($xlinkUrl, $xlinkFid) = explode('#', $xlink);
  302. DBG::log($item[$fldName], 'array', "xlinks for '{$tagName}'");
  303. $xmlWriter->startElement($tagName);
  304. foreach ($attrs as $name => $value) {
  305. $xmlWriter->writeAttribute($name, $value);
  306. }
  307. if ($showAdvancedAttrs && !$acl->canReadObjectField($fldName, (object)$item)) {
  308. $xmlWriter->writeAttribute("p5:allow_read", "false");
  309. }
  310. if ($showAdvancedAttrs && $acl->canWriteObjectField($fldName, (object)$item)) {
  311. $xmlWriter->writeAttribute("p5:allow_write", "true");
  312. }
  313. $xmlWriter->writeAttribute('xlink:href', $xlink);
  314. $xmlWriter->endElement();// {$itemPrefix}:{$fldName}
  315. return;
  316. }
  317. $xmlWriter->startElement($tagName);
  318. foreach ($attrs as $name => $value) {
  319. $xmlWriter->writeAttribute($name, $value);
  320. }
  321. // $fldList = $acl->getRealFieldListByIdZasob();
  322. $fldList = $acl->getFieldListByIdZasob();
  323. $geomFld = null;
  324. foreach ($fldList as $fldName) {
  325. if ($acl->isGeomField($fldName)) {
  326. $geomFld = $fldName;
  327. }
  328. }
  329. DBG::log($fldList, 'array', ">>> loop start fields(".count($fldList).")");
  330. // DBG::log($showFields, 'array', "DBG: \$showLocalFields \$showFields 0");
  331. $showLocalFields = ($showFields) // if empty filter cols show all local fields
  332. ? array_filter($showFields, function ($showFieldName) {
  333. return (false === strpos($showFieldName, "/"));
  334. })
  335. : $fldList;
  336. DBG::log($showLocalFields, 'array', "DBG: \$showLocalFields");
  337. $fldList = array_filter($fldList, function ($fieldName) use ($showLocalFields) {
  338. return (in_array($fieldName, $showLocalFields));
  339. });
  340. DBG::log($fldList, 'array', ">>> loop start filtered fields(".count($fldList).")");
  341. foreach ($fldList as $idZasob => $fldName) {
  342. DBG::log(">>> loop {$idZasob} => {$fldName}...");
  343. $fldType = $acl->getXsdFieldType($fldName);
  344. DBG::log(">>> loop '{$fldName}' xsdType: '{$fldType}'");
  345. if (!$acl->canReadObjectField($fldName, (object)$item)) if(V::get('DBG_XML', '', $_GET))$xmlWriter->writeComment("DBG: skip - !canReadObjectField('{$fldName}')"); // TODO: DBG
  346. if (!$acl->canReadObjectField($fldName, (object)$item)) continue;
  347. DBG::log(">>> loop '{$fldName}' can read...");
  348. if ($geomFld != null && $fldName == $geomFld) {
  349. $xmlWriter->startElement("{$itemPrefix}:{$fldName}");
  350. if ($showAdvancedAttrs && !$acl->canReadObjectField($fldName, (object)$item)) {
  351. $xmlWriter->writeAttribute("p5:allow_read", "false");
  352. }
  353. if ($showAdvancedAttrs && $acl->canWriteObjectField($fldName, (object)$item)) {
  354. $xmlWriter->writeAttribute("p5:allow_write", "true");
  355. }
  356. (new Api_WfsGeomTypeConverter())->createGmlFromWkt_xmlWriter($item[$fldName], $xmlWriter);
  357. $xmlWriter->endElement();// {$itemPrefix}:{$fldName}
  358. } else if (is_array($item[$fldName])) {// TODO: by struct - REF field
  359. DBG::log($item[$fldName], 'array', ">>> loop({$itemKey}) REF item[{$itemKey}][{$fldName}]");
  360. if (empty($item[$fldName])) { // SKIP empty fields
  361. if(V::get('DBG_XML', '', $_GET))$xmlWriter->writeComment("DBG: skip empty field '{$fldName}'"); // TODO: DBG
  362. // $xmlWriter->h($fldName);
  363. } else {
  364. if(V::get('DBG_XML', '', $_GET))$xmlWriter->writeComment("DBG: TODO: array field... '{$fldName}'"); // TODO: DBG
  365. // $xmlWriter->writeComment("TODO: ".$acl->getName().".{$itemKey}/{$fldName} ...");
  366. $fieldNs = str_replace(['__x3A__', ':'], '/', $fldName); // substr($xsdType, 4));
  367. if (!array_key_exists($fieldNs, $schemaCache)) {
  368. // maybe only xlinks - acl not needed
  369. $firstItem = reset($item[$fldName]);
  370. if (1 === count($firstItem) && !empty($firstItem['xlink'])) { // TODO: $schemaCache[$fieldNs] must exists for xlinks - xlmns is required
  371. foreach ($item[$fldName] as $childItem) {
  372. self::printXmlFeatureRecurse($xmlWriter, $childAcl = null, $childItem, [
  373. 'fields' => [],
  374. 'tagName' => $fldName,
  375. 'xsdAttributes' => [],
  376. 'showAdvancedAttrs' => $showAdvancedAttrs,
  377. 'outputBlobFormat' => $outputBlobFormat,
  378. ], $schemaCache, $printedFidLog);
  379. }
  380. } else {
  381. DBG::log($schemaCache, 'array', "Error Processing Request - field is not ref or missing acl ".$acl->getName().".{$itemKey}/{$fldName}");
  382. if(V::get('DBG_XML', '', $_GET))$xmlWriter->writeComment("Error Processing Request - field is not ref or missing acl ".$acl->getName().".{$itemKey}/{$fldName}");
  383. }
  384. } else {
  385. DBG::log($schemaCache[$fieldNs], 'array', "fetch child acl ".$acl->getName().".{$itemKey}/{$fldName}");
  386. $childAcl = Core_AclHelper::getAclByNamespace($schemaCache[$fieldNs]['namespace'], false, $schemaCache[$fieldNs]);
  387. $childName = $schemaCache[$fieldNs]['name'];
  388. foreach ($item[$fldName] as $childItem) {
  389. // DBG::log($showFields, 'array', "DBG: fld({$fldName}) \$showFields 0");
  390. $showChildFields = array_filter($showFields, function ($showFieldName) use ($fldName) {
  391. return ("{$fldName}/" === substr("{$showFieldName}", 0, strlen($fldName) + 1));
  392. });
  393. // DBG::log($showChildFields, 'array', "DBG: fld({$fldName}) \$showChildFields 1");
  394. $showChildFields = array_map(function ($showFieldName) use ($fldName) {
  395. return substr($showFieldName, strlen($fldName) + 1);
  396. }, $showChildFields);
  397. // DBG::log($showChildFields, 'array', "DBG: fld({$fldName}) \$showChildFields 2");
  398. $childPK = V::get($childAcl->getPrimaryKeyField(), '', $childItem);
  399. self::printXmlFeatureRecurse($xmlWriter, $childAcl, $childItem, [
  400. 'fields' => $showChildFields,
  401. 'tagName' => $fldName,
  402. 'xsdAttributes' => ($childPK) ? [ 'fid' => "{$childName}.{$childPK}" ] : [],
  403. 'showAdvancedAttrs' => $showAdvancedAttrs,
  404. 'outputBlobFormat' => $outputBlobFormat,
  405. ], $schemaCache, $printedFidLog);
  406. }
  407. // foreach ($item[$fldName] as $refItem) {
  408. // DBG::log($refItem, 'array', "\$refItem fld({$fldName})");
  409. // if (1 == count($refItem) && !empty($refItem['xlink'])) {
  410. // $xmlWriter->startElement($schemaCache[$fieldNs]['typeName']);
  411. // $xmlWriter->writeAttribute("xlink:href", $refItem['xlink']);
  412. // $xmlWriter->endElement();
  413. // } else {
  414. // $xmlWriter->writeComment("DBG: array field ref ... '{$fldName}'"); // TODO: DBG
  415. // $xmlWriter->startElement($schemaCache[$fieldNs]['typeName']);
  416. // foreach ($schemaCache[$fieldNs]['field'] as $field) {
  417. // if (array_key_exists($field['fieldNamespace'], $refItem)) {
  418. // $xmlWriter->writeComment("REF field ({$field['fieldNamespace']}) value({$refItem[$field['fieldNamespace']]}) TODO: get xsdType - TODO: recurse");
  419. // DBG::log($refItem[$field['fieldNamespace']], 'array', "REF field ({$field['fieldNamespace']}) TODO: get xsdType - TODO: recurse");
  420. // if (false !== strpos($field['fieldNamespace'], ':')) { // is ref - TODO: better check by xsdType
  421. // $xmlWriter->startElement($field['fieldNamespace']);
  422. // $xmlWriter->writeComment("TODO: recurse ...");
  423. // // $xmlWriter->text($refItem[$field['fieldNamespace']]);
  424. // $xmlWriter->endElement();
  425. // } else {
  426. // $xmlWriter->startElement("{$schemaCache[$fieldNs]['nsPrefix']}:{$field['fieldNamespace']}");
  427. // $xmlWriter->text($refItem[$field['fieldNamespace']]);
  428. // $xmlWriter->endElement();
  429. // }
  430. // }
  431. // }
  432. // $xmlWriter->endElement();
  433. // }
  434. // }
  435. }
  436. }
  437. } else if ('xsd:base64Binary' === $acl->getXsdFieldType($fldName)) {
  438. if (empty($item[$fldName])) continue; // && '0' !== $item[$fldName]
  439. $xmlWriter->startElement("{$itemPrefix}:{$fldName}");
  440. if ($showAdvancedAttrs && !$acl->canReadObjectField($fldName, (object)$item)) {
  441. $xmlWriter->writeAttribute("p5:allow_read", "false");
  442. }
  443. if ($showAdvancedAttrs && $acl->canWriteObjectField($fldName, (object)$item)) {
  444. $xmlWriter->writeAttribute("p5:allow_write", "true");
  445. }
  446. $pk = V::get($acl->getPrimaryKeyField(), '', $item);
  447. if ($showAdvancedAttrs) {
  448. $xmlWriter->writeAttribute("p5:blobUrl", Request::getPathUri() . "wfs-data.php?" . implode("&", [
  449. "SERVICE=WFS", "VERSION=1.0.0", "REQUEST=GetBlob",
  450. "namespace=" . $acl->getNamespace(),
  451. "primaryKey={$pk}",
  452. "fieldName={$fldName}",
  453. ]));
  454. }
  455. if (in_array($item[$fldName], ['0', '1'])) {
  456. if ('base64' === $outputBlobFormat) {
  457. $content = DB::getPDO( $acl->getDatabaseID() )->getBlob( $acl->getRootTableName(), $fldName, $acl->getPrimaryKeyField(), $pk );
  458. $xmlWriter->text(base64_encode($content));
  459. } else if ('link' === $outputBlobFormat) {
  460. $xmlWriter->text($item[$fldName]);
  461. } else { // hide
  462. throw new Exception("Not implemented outputBlobFormat - expected base64 or link");
  463. }
  464. } else {
  465. $xmlWriter->text(base64_encode($item[$fldName]));
  466. }
  467. $xmlWriter->endElement();// {$itemPrefix}:{$fldName}
  468. } else {
  469. $value = str_replace('&', '&amp;', $item[$fldName]);
  470. if (empty($value) && '0' !== $value) {
  471. continue;
  472. } else {
  473. $xmlWriter->startElement("{$itemPrefix}:{$fldName}");
  474. if ($showAdvancedAttrs && !$acl->canReadObjectField($fldName, (object)$item)) {
  475. $xmlWriter->writeAttribute("p5:allow_read", "false");
  476. }
  477. if ($showAdvancedAttrs && $acl->canWriteObjectField($fldName, (object)$item)) {
  478. $xmlWriter->writeAttribute("p5:allow_write", "true");
  479. }
  480. $xmlWriter->text($value);
  481. $xmlWriter->endElement();// {$itemPrefix}:{$fldName}
  482. }
  483. }
  484. }
  485. $xmlWriter->endElement();
  486. }
  487. }