WfsServerBase.php 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  1. <?php
  2. Lib::loadClass('Api_WfsException');
  3. Lib::loadClass('Api_WfsGeomTypeConverter');
  4. Lib::loadClass('Api_WfsNs');
  5. Lib::loadClass('Request');
  6. Lib::loadClass('Core_AclHelper');
  7. Lib::loadClass('Core_XMLWriter');
  8. class Api_WfsServerBase {
  9. public $_usrAcl;
  10. public $_typeConverter;
  11. public $_apiBaseUri;
  12. protected $_logFile;
  13. public function __construct($usrAcl) {
  14. $this->_usrAcl = $usrAcl;
  15. $this->_typeConverter = new Api_WfsGeomTypeConverter();
  16. $this->_apiBaseUri = '';
  17. }
  18. public function setBaseUri($uri) {
  19. $this->_apiBaseUri = $uri;
  20. }
  21. public function getBaseUri() {// TODO: RMME
  22. return $this->_apiBaseUri;
  23. }
  24. /**
  25. * @param string $typeName - 'p5_default_db:TEST_PERMS'
  26. */
  27. public function getAclFromTypeName($typeName) {
  28. $typeEx = explode(':', $typeName);
  29. $sourceName = $typeEx[0];
  30. $objName = $typeEx[1];
  31. if (2 != count($typeEx)) throw new Api_WfsException("Could not get acl for '{$typeName}' - syntax error");
  32. if ('p5_' == substr($sourceName, 0, 3)) $sourceName = substr($sourceName, 3);// remove prefix 'p5_'
  33. $acl = $this->_usrAcl->getObjectAcl($sourceName, $objName);
  34. $forceTblAclInit = 0;//('1' == V::get('_force', '', $_GET));
  35. $acl->init($forceTblAclInit);
  36. return $acl;
  37. }
  38. /**
  39. * @param $_GET [ , POST Body - XML file ]
  40. * @return array $args
  41. * TODO: get $acl and check more restrictions
  42. */
  43. public function parseGetFeatureArgsFromRequest() {
  44. $rawArgs = $_GET;// $_REQUEST; ($_POST values not allowed, only xml in post body)
  45. $args = array();
  46. $args['xsd:type'] = null;// @from: TYPENAME, typeName (typename)
  47. $args['typePrefix'] = null;// @from: TYPENAME, typeName (typename)
  48. $args['typeName'] = null;// @from: TYPENAME, typeName (typename)
  49. $args['srsname'] = null;// @from: SRSNAME (srsname)
  50. $args['limit'] = 0;// @from: MAXFEATURES, maxFeatures, COUNT (maxfeatures, count)
  51. $args['offset'] = 0;// @from: startIndex (startindex, count)
  52. $args['ogc:filter'] = null;// @from: FILTER, Filter (filter)
  53. $args['sortBy'] = null;// @from: sortBy (sortby)
  54. $args['wfs:propertyName'] = null;// @from: propertyName (propertyname)
  55. $args['filterFields'] = array();// array of field names to show - @from: propertyName
  56. $args['resultType'] = null;// @from: resultType (resulttype)
  57. $args['bbox'] = null;// @from: BBOX (bbox)
  58. $args['wfs:featureID'] = null;// @from: featureID, featureId (featureid)
  59. $args['primaryKey'] = null;// primaryKey type - @from: featureID or $req[primaryKey]
  60. $lowerArgs = array(); foreach ($rawArgs as $name => $value) $lowerArgs[ strtolower($name) ] = trim($value);
  61. $args['xsd:type'] = V::get('typename', '', $lowerArgs);
  62. $exType = explode(':', $args['xsd:type']);
  63. if (count($exType) != 2) throw new HttpException("Wrong param TYPENAME", 400);
  64. $args['typePrefix'] = $exType[0];
  65. $args['typeName'] = $exType[1];
  66. $args['srsname'] = V::get('SRSNAME', '', $lowerArgs);// eg. EPSG:4326
  67. $args['limit'] = V::get('maxfeatures', $args['limit'], $lowerArgs, 'int');
  68. $args['limit'] = V::get('count', $args['limit'], $lowerArgs, 'int');
  69. $defaultFeaturesLimit = ('biuro.biall-net.pl' == V::get('SERVER_NAME', '', $_SERVER)) ? 50000 : 10000;
  70. if ($args['limit'] <= 0) $args['limit'] = $defaultFeaturesLimit;// default limit
  71. $args['offset'] = V::get('startindex', 0, $lowerArgs, 'int');
  72. $args['ogc:filter'] = urldecode(V::get('filter', '', $lowerArgs));
  73. if (empty($args['ogc:filter'])) {// read ogc:Filter from POST body if not defined in GET
  74. $reqBody = Request::getRequestBody();
  75. if (!empty($reqBody)) {
  76. $args['ogc:filter'] = $this->convertOgcFilterFromRequestBody($reqBody);
  77. if (empty($args['ogc:filter'])) throw new Api_WfsException("Error Processing ogc:Filter", 501);
  78. }
  79. }
  80. $args['sortBy'] = V::get('sortby', '', $lowerArgs);// TODO: split to array of array(fieldName, (asc | desc))
  81. $args['wfs:propertyName'] = trim(V::get('propertyname', '', $lowerArgs));// TODO: fields to show - split by ','
  82. if (strlen($args['wfs:propertyName']) > 0) {
  83. $args['filterFields'] = array();
  84. $exFields = explode(',', $args['wfs:propertyName']);
  85. foreach ($exFields as $fieldName) {
  86. $fieldName = trim($fieldName);
  87. if (!empty($fieldName)) $args['filterFields'][] = $fieldName;
  88. }
  89. }
  90. $args['resultType'] = V::get('resulttype', '', $lowerArgs);
  91. $args['bbox'] = V::get('bbox', '', $lowerArgs);
  92. if (!empty($args['bbox'])) {
  93. // BBOX may have EPSG at then end - example: ",EPSG:4326"
  94. $num = "\d+.?\d*?";// "\d+(.\d+)?"
  95. if (!preg_match("/^({$num}),({$num}),({$num}),({$num})(\,EPSG:\d+)?$/", $args['bbox'], $matches)) throw new Exception("Illegal BBOX format");
  96. // QGIS send BBOX in correct order: 54.23580872176457,18.46844302390853,54.25220902538883,18.492990600812696
  97. // first number should not be smaller then second
  98. // example $matches:
  99. // [0] => 18.492990600812696,54.23580872176457,18.46844302390853,54.25220902538883
  100. // [1] => 18.492990600812696
  101. // [2] => 54.23580872176457
  102. // [3] => 18.46844302390853
  103. // [4] => 54.25220902538883
  104. // (optional) EPGS
  105. $bboxPoints = ($matches[1] > $matches[2])
  106. ? [ $matches[1], $matches[2], $matches[3], $matches[4] ]
  107. : [ $matches[2], $matches[3], $matches[4], $matches[1] ];
  108. $args['bbox'] = implode(",", $bboxPoints);
  109. }
  110. $args['wfs:featureID'] = V::get('featureid', null, $lowerArgs);// TODO: allow multiply feature id (csv)
  111. if ($args['wfs:featureID']) {
  112. $dotPos = strpos($args['wfs:featureID'], '.');
  113. if (!$dotPos) throw new Exception("Wrong param featureID");
  114. if ($args['typeName'] != substr($args['wfs:featureID'], 0, $dotPos)) throw new Exception("Wrong typeName in param featureID");
  115. $args['primaryKey'] = substr($args['wfs:featureID'], $dotPos + 1);
  116. }
  117. $pk = V::get('primarykey', '', $lowerArgs);
  118. if ($pk) {
  119. $args['primaryKey'] = $pk;
  120. }
  121. if(DBG::isActive() && V::get('DBG', 0, $_GET)){ echo "\$lowerArgs:";print_r($lowerArgs); }
  122. if(DBG::isActive() && V::get('DBG', 0, $_GET)){ echo "\$args:";print_r($args); }
  123. return $args;
  124. }
  125. public function _getCapabilities($wfsServerUrl, $serviceTitle, $serviceDescription) {
  126. if (V::get('DBG_ACL', '', $_GET)) {
  127. {
  128. echo "Core_AclHelper::getAclList = [" . "\n";
  129. foreach (Core_AclHelper::getAclList() as $typeName) {
  130. echo "|\t{$typeName}" . "\n";
  131. }
  132. echo "]// .EOF Core_AclHelper::getAclList" . "\n";
  133. }
  134. $fullTblAclList = $this->_usrAcl->getTablesAcl();
  135. print_r($fullTblAclList);
  136. die("\n" . '.EOF - DBG_ACL');
  137. }
  138. header('Content-type: application/xml; charset=utf-8');
  139. $xmlWriter = new Core_XMLWriter();
  140. if (!$xmlWriter) throw new HttpException("Error no XMLWriter", 404);
  141. $xmlWriter->openUri('php://output');
  142. $xmlWriter->setIndent(true);
  143. $xmlWriter->startDocument('1.0','UTF-8');
  144. $xmlWriter->startElement('WFS_Capabilities');
  145. $xmlWriter->writeAttribute('xmlns', 'http://www.opengis.net/wfs');
  146. $xmlWriter->writeAttribute('xmlns:ogc', 'http://www.opengis.net/ogc');
  147. $xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  148. foreach (Api_WfsNs::getNsList() as $uri => $prefix) {
  149. $xmlWriter->writeAttribute("xmlns:{$prefix}", $uri);
  150. }
  151. $schemaLocations = [];
  152. //$schemaLocations[] = 'http://www.opengis.net/wfs http://webgis.regione.sardegna.it:80/geoserver/schemas/wfs/1.0.0/WFS-capabilities.xsd';// @from http://webgis.regione.sardegna.it/geoserver/ows?service=WFS&request=GetCapabilities
  153. if (!empty($schemaLocations)) $xmlWriter->writeAttribute('xsi:schemaLocation', implode(' ', $schemaLocations));
  154. $xmlWriter->writeAttribute('version', "1.0.0");
  155. $xmlWriter->h('Service', [
  156. ['Name', "WFS"],
  157. ['Title', $serviceTitle],
  158. ['Abstract', $serviceDescription],
  159. ['Keywords', "WFS, WMS"],
  160. ['OnlineResource', $wfsServerUrl],
  161. ['Fees', "NONE"],
  162. ['AccessConstraints', "NONE"],
  163. ]);
  164. $xmlWriter->h('Capability', [
  165. [ 'Request', [
  166. [ 'GetCapabilities', [
  167. [ 'DCPType', [
  168. [ 'HTTP', [
  169. [ 'Get', [ 'onlineResource' => "{$wfsServerUrl}?REQUEST=GetCapabilities" ], null ]
  170. ] ]
  171. ] ],
  172. [ 'DCPType', [
  173. [ 'HTTP', [
  174. [ 'Post', [ 'onlineResource' => "{$wfsServerUrl}" ], null ]
  175. ] ]
  176. ] ],
  177. ] ]
  178. ] ]
  179. ]);
  180. $xmlWriter->h('DescribeFeatureType', [
  181. [ 'SchemaDescriptionLanguage', [
  182. [ 'XMLSCHEMA', null ]
  183. ] ],
  184. [ 'DCPType', [
  185. [ 'HTTP', [
  186. [ 'Get', ['onlineResource' => "{$wfsServerUrl}?REQUEST=DescribeFeatureType"], null ]
  187. ] ]
  188. ] ],
  189. [ 'DCPType', [
  190. [ 'HTTP', [
  191. [ 'Post', ['onlineResource' => "{$wfsServerUrl}"], null ]
  192. ] ]
  193. ] ],
  194. ]);
  195. $xmlWriter->h('GetFeature', [// $this->_printGetFeatureXml($wfsServerUrl);
  196. [ 'ResultFormat', [
  197. 'WFSKMLOutputFormat',
  198. 'GML2',
  199. 'GML3',
  200. 'SHAPE-ZIP',
  201. 'CSV',
  202. 'JSON',
  203. ] ],
  204. [ 'DCPType', [
  205. [ 'HTTP', [
  206. [ 'Get', ['onlineResource' => "{$wfsServerUrl}?REQUEST=GetFeature"], null ]
  207. ] ]
  208. ] ],
  209. [ 'DCPType', [
  210. [ 'HTTP', [
  211. [ 'Post', ['onlineResource' => "{$wfsServerUrl}"], null ]
  212. ] ]
  213. ] ],
  214. ]);
  215. $xmlWriter->h('Transaction', [
  216. [ 'DCPType', [
  217. [ 'HTTP', [
  218. [ 'Get', ['onlineResource' => "{$wfsServerUrl}?REQUEST=Transaction"], null ]
  219. ] ]
  220. ] ],
  221. [ 'DCPType', [
  222. [ 'HTTP', [
  223. [ 'Post', ['onlineResource' => "{$wfsServerUrl}"], null ]
  224. ] ]
  225. ] ],
  226. ]);
  227. $xmlWriter->h('LockFeature', [
  228. [ 'DCPType', [
  229. [ 'HTTP', [
  230. [ 'Get', ['onlineResource' => "{$wfsServerUrl}?REQUEST=LockFeature"], null ]
  231. ] ]
  232. ] ],
  233. [ 'DCPType', [
  234. [ 'HTTP', [
  235. [ 'Post', ['onlineResource' => "{$wfsServerUrl}"], null ]
  236. ] ]
  237. ] ],
  238. ]);
  239. $xmlWriter->h('GetFeatureWithLock', [
  240. [ 'ResultFormat', [
  241. 'GML2'
  242. ] ],
  243. [ 'DCPType', [
  244. [ 'HTTP', [
  245. [ 'Get', ['onlineResource' => "{$wfsServerUrl}?REQUEST=GetFeatureWithLock"], null ]
  246. ] ]
  247. ] ],
  248. [ 'DCPType', [
  249. [ 'HTTP', [
  250. [ 'Post', ['onlineResource' => "{$wfsServerUrl}"], null ]
  251. ] ]
  252. ] ],
  253. ]);
  254. $featureTypeNodes = [];
  255. foreach ($this->_getTableAclList() as $tblAcl) {
  256. $dataSourceName = 'default_db';// TODO: $tblAcl->getSourceName()
  257. $prefix = "p5_{$dataSourceName}";
  258. $featureTypeNodes[] = [ 'FeatureType', [ "xmlns:{$prefix}" => Api_WfsNs::getNsUri($prefix) ], [
  259. [ 'Name', $prefix . ':' . $tblAcl->getName() ],
  260. [ 'Title', $tblAcl->getRawLabel() ],
  261. [ 'Abstract', $tblAcl->getRawOpis() ],
  262. [ 'Keywords', implode(', ', [ $tblAcl->getID(), $tblAcl->getName(), $tblAcl->getRawLabel() ]) ],
  263. [ 'SRS', 'EPSG:4326' ],
  264. [ 'LatLongBoundingBox', [
  265. 'minx' => "8.12328509871721",
  266. 'miny' => "38.8575126897477",
  267. 'maxx' => "9.838674658246807",
  268. 'maxy' => "41.31378404137082"], null ]
  269. ]];
  270. }
  271. foreach (Core_AclHelper::getAclList() as $typeName) {
  272. list($prefix, $name) = explode(':', $typeName);
  273. $featureTypeNodes[] = [ 'FeatureType', [ "xmlns:{$prefix}" => Api_WfsNs::getNsUri($prefix) ], [
  274. [ 'Name', $name ],
  275. [ 'Title', $name ],
  276. [ 'Abstract', $name ],
  277. [ 'Keywords', $name ],
  278. [ 'SRS', 'EPSG:4326' ],
  279. ]];
  280. }
  281. $xmlWriter->startElement('FeatureTypeList');
  282. $xmlWriter->h('Operations', ['Query', 'Insert', 'Update', 'Delete', 'Lock']);
  283. foreach ($featureTypeNodes as $node) {
  284. $xmlWriter->h($node);
  285. }
  286. $xmlWriter->endElement();// FeatureTypeList
  287. $xmlWriter->h('ogc:Filter_Capabilities', [
  288. [ 'ogc:Spatial_Capabilities', [
  289. [ 'ogc:Spatial_Operators', [
  290. 'ogc:Disjoint',
  291. 'ogc:Equals',
  292. 'ogc:DWithin',
  293. 'ogc:Beyond',
  294. 'ogc:Intersect',
  295. 'ogc:Touches',
  296. 'ogc:Crosses',
  297. 'ogc:Within',
  298. 'ogc:Contains',
  299. 'ogc:Overlaps',
  300. 'ogc:BBOX',
  301. ] ]
  302. ] ],
  303. [ 'ogc:Scalar_Capabilities', [
  304. 'ogc:Logical_Operators',
  305. [ 'ogc:Comparison_Operators', [
  306. 'ogc:Simple_Comparisons',
  307. 'ogc:Between',
  308. 'ogc:Like',
  309. 'ogc:NullCheck',
  310. ] ],
  311. [ 'ogc:Arithmetic_Operators', [
  312. 'ogc:Simple_Arithmetic',
  313. [ 'ogc:Functions', [
  314. [ 'ogc:Function_Names', [
  315. [ 'ogc:Function_Name', ['nArgs'=>"1"], "abs" ],
  316. [ 'ogc:Function_Name', ['nArgs'=>"1"], "abs_2" ],
  317. [ 'ogc:Function_Name', ['nArgs'=>"1"], "abs_3" ],
  318. [ 'ogc:Function_Name', ['nArgs'=>"1"], "abs_4" ],
  319. [ 'ogc:Function_Name', ['nArgs'=>"1"], "acos" ],
  320. [ 'ogc:Function_Name', ['nArgs'=>"2"], "AddCoverages" ],
  321. [ 'ogc:Function_Name', ['nArgs'=>"4"], "Aggregate" ],
  322. [ 'ogc:Function_Name', ['nArgs'=>"1"], "Area" ],
  323. [ 'ogc:Function_Name', ['nArgs'=>"1"], "area2" ],
  324. [ 'ogc:Function_Name', ['nArgs'=>"3"], "AreaGrid" ],
  325. [ 'ogc:Function_Name', ['nArgs'=>"1"], "asin" ],
  326. [ 'ogc:Function_Name', ['nArgs'=>"1"], "atan" ],
  327. [ 'ogc:Function_Name', ['nArgs'=>"2"], "atan2" ],
  328. [ 'ogc:Function_Name', ['nArgs'=>"14"], "BarnesSurface" ],
  329. [ 'ogc:Function_Name', ['nArgs'=>"3"], "between" ],
  330. [ 'ogc:Function_Name', ['nArgs'=>"1"], "boundary" ],
  331. [ 'ogc:Function_Name', ['nArgs'=>"1"], "boundaryDimension" ],
  332. [ 'ogc:Function_Name', ['nArgs'=>"1"], "Bounds" ],
  333. [ 'ogc:Function_Name', ['nArgs'=>"2"], "buffer" ],
  334. [ 'ogc:Function_Name', ['nArgs'=>"3"], "BufferFeatureCollection" ],
  335. [ 'ogc:Function_Name', ['nArgs'=>"3"], "bufferWithSegments" ],
  336. [ 'ogc:Function_Name', ['nArgs'=>"7"], "Categorize" ],
  337. [ 'ogc:Function_Name', ['nArgs'=>"1"], "ceil" ],
  338. [ 'ogc:Function_Name', ['nArgs'=>"1"], "Centroid" ],
  339. [ 'ogc:Function_Name', ['nArgs'=>"2"], "classify" ],
  340. [ 'ogc:Function_Name', ['nArgs'=>"2"], "Clip" ],
  341. [ 'ogc:Function_Name', ['nArgs'=>"1"], "CollectGeometries" ],
  342. [ 'ogc:Function_Name', ['nArgs'=>"1"], "Collection_Average" ],
  343. [ 'ogc:Function_Name', ['nArgs'=>"1"], "Collection_Bounds" ],
  344. [ 'ogc:Function_Name', ['nArgs'=>"0"], "Collection_Count" ],
  345. [ 'ogc:Function_Name', ['nArgs'=>"1"], "Collection_Max" ],
  346. [ 'ogc:Function_Name', ['nArgs'=>"1"], "Collection_Median" ],
  347. [ 'ogc:Function_Name', ['nArgs'=>"1"], "Collection_Min" ],
  348. [ 'ogc:Function_Name', ['nArgs'=>"1"], "Collection_Sum" ],
  349. [ 'ogc:Function_Name', ['nArgs'=>"1"], "Collection_Unique" ],
  350. [ 'ogc:Function_Name', ['nArgs'=>"1"], "Concatenate" ],
  351. [ 'ogc:Function_Name', ['nArgs'=>"2"], "contains" ],
  352. [ 'ogc:Function_Name', ['nArgs'=>"7"], "Contour" ],
  353. [ 'ogc:Function_Name', ['nArgs'=>"2"], "convert" ],
  354. [ 'ogc:Function_Name', ['nArgs'=>"1"], "convexHull" ],
  355. [ 'ogc:Function_Name', ['nArgs'=>"1"], "cos" ],
  356. [ 'ogc:Function_Name', ['nArgs'=>"1"], "Count" ],
  357. [ 'ogc:Function_Name', ['nArgs'=>"2"], "CropCoverage" ],
  358. [ 'ogc:Function_Name', ['nArgs'=>"2"], "crosses" ],
  359. [ 'ogc:Function_Name', ['nArgs'=>"2"], "dateFormat" ],
  360. [ 'ogc:Function_Name', ['nArgs'=>"2"], "dateParse" ],
  361. [ 'ogc:Function_Name', ['nArgs'=>"2"], "difference" ],
  362. [ 'ogc:Function_Name', ['nArgs'=>"1"], "dimension" ],
  363. [ 'ogc:Function_Name', ['nArgs'=>"2"], "disjoint" ],
  364. [ 'ogc:Function_Name', ['nArgs'=>"2"], "disjoint3D" ],
  365. [ 'ogc:Function_Name', ['nArgs'=>"2"], "distance" ],
  366. [ 'ogc:Function_Name', ['nArgs'=>"2"], "distance3D" ],
  367. [ 'ogc:Function_Name', ['nArgs'=>"1"], "double2bool" ],
  368. [ 'ogc:Function_Name', ['nArgs'=>"1"], "endAngle" ],
  369. [ 'ogc:Function_Name', ['nArgs'=>"1"], "endPoint" ],
  370. [ 'ogc:Function_Name', ['nArgs'=>"1"], "env" ],
  371. [ 'ogc:Function_Name', ['nArgs'=>"1"], "envelope" ],
  372. [ 'ogc:Function_Name', ['nArgs'=>"2"], "EqualInterval" ],
  373. [ 'ogc:Function_Name', ['nArgs'=>"2"], "equalsExact" ],
  374. [ 'ogc:Function_Name', ['nArgs'=>"3"], "equalsExactTolerance" ],
  375. [ 'ogc:Function_Name', ['nArgs'=>"2"], "equalTo" ],
  376. [ 'ogc:Function_Name', ['nArgs'=>"1"], "exp" ],
  377. [ 'ogc:Function_Name', ['nArgs'=>"1"], "exteriorRing" ],
  378. [ 'ogc:Function_Name', ['nArgs'=>"3"], "Feature" ],
  379. [ 'ogc:Function_Name', ['nArgs'=>"1"], "floor" ],
  380. [ 'ogc:Function_Name', ['nArgs'=>"1"], "geometryType" ],
  381. [ 'ogc:Function_Name', ['nArgs'=>"1"], "geomFromWKT" ],
  382. [ 'ogc:Function_Name', ['nArgs'=>"1"], "geomLength" ],
  383. [ 'ogc:Function_Name', ['nArgs'=>"2"], "getGeometryN" ],
  384. [ 'ogc:Function_Name', ['nArgs'=>"1"], "getX" ],
  385. [ 'ogc:Function_Name', ['nArgs'=>"1"], "getY" ],
  386. [ 'ogc:Function_Name', ['nArgs'=>"1"], "getz" ],
  387. [ 'ogc:Function_Name', ['nArgs'=>"2"], "greaterEqualThan" ],
  388. [ 'ogc:Function_Name', ['nArgs'=>"2"], "greaterThan" ],
  389. [ 'ogc:Function_Name', ['nArgs'=>"5"], "Grid" ],
  390. [ 'ogc:Function_Name', ['nArgs'=>"7"], "Heatmap" ],
  391. [ 'ogc:Function_Name', ['nArgs'=>"0"], "id" ],
  392. [ 'ogc:Function_Name', ['nArgs'=>"2"], "IEEEremainder" ],
  393. [ 'ogc:Function_Name', ['nArgs'=>"3"], "if_then_else" ],
  394. [ 'ogc:Function_Name', ['nArgs'=>"11"], "in10" ],
  395. [ 'ogc:Function_Name', ['nArgs'=>"3"], "in2" ],
  396. [ 'ogc:Function_Name', ['nArgs'=>"4"], "in3" ],
  397. [ 'ogc:Function_Name', ['nArgs'=>"5"], "in4" ],
  398. [ 'ogc:Function_Name', ['nArgs'=>"6"], "in5" ],
  399. [ 'ogc:Function_Name', ['nArgs'=>"7"], "in6" ],
  400. [ 'ogc:Function_Name', ['nArgs'=>"8"], "in7" ],
  401. [ 'ogc:Function_Name', ['nArgs'=>"9"], "in8" ],
  402. [ 'ogc:Function_Name', ['nArgs'=>"10"], "in9" ],
  403. [ 'ogc:Function_Name', ['nArgs'=>"2"], "InclusionFeatureCollection" ],
  404. [ 'ogc:Function_Name', ['nArgs'=>"1"], "int2bbool" ],
  405. [ 'ogc:Function_Name', ['nArgs'=>"1"], "int2ddouble" ],
  406. [ 'ogc:Function_Name', ['nArgs'=>"1"], "interiorPoint" ],
  407. [ 'ogc:Function_Name', ['nArgs'=>"2"], "interiorRingN" ],
  408. [ 'ogc:Function_Name', ['nArgs'=>"3"], "Interpolate" ],
  409. [ 'ogc:Function_Name', ['nArgs'=>"2"], "intersection" ],
  410. [ 'ogc:Function_Name', ['nArgs'=>"7"], "IntersectionFeatureCollection" ],
  411. [ 'ogc:Function_Name', ['nArgs'=>"2"], "intersects" ],
  412. [ 'ogc:Function_Name', ['nArgs'=>"2"], "intersects3D" ],
  413. [ 'ogc:Function_Name', ['nArgs'=>"1"], "isClosed" ],
  414. [ 'ogc:Function_Name', ['nArgs'=>"0"], "isCoverage" ],
  415. [ 'ogc:Function_Name', ['nArgs'=>"1"], "isEmpty" ],
  416. [ 'ogc:Function_Name', ['nArgs'=>"2"], "isLike" ],
  417. [ 'ogc:Function_Name', ['nArgs'=>"1"], "isNull" ],
  418. [ 'ogc:Function_Name', ['nArgs'=>"2"], "isometric" ],
  419. [ 'ogc:Function_Name', ['nArgs'=>"1"], "isRing" ],
  420. [ 'ogc:Function_Name', ['nArgs'=>"1"], "isSimple" ],
  421. [ 'ogc:Function_Name', ['nArgs'=>"1"], "isValid" ],
  422. [ 'ogc:Function_Name', ['nArgs'=>"3"], "isWithinDistance" ],
  423. [ 'ogc:Function_Name', ['nArgs'=>"3"], "isWithinDistance3D" ],
  424. [ 'ogc:Function_Name', ['nArgs'=>"2"], "Jenks" ],
  425. [ 'ogc:Function_Name', ['nArgs'=>"1"], "length" ],
  426. [ 'ogc:Function_Name', ['nArgs'=>"2"], "lessEqualThan" ],
  427. [ 'ogc:Function_Name', ['nArgs'=>"2"], "lessThan" ],
  428. [ 'ogc:Function_Name', ['nArgs'=>"1"], "list" ],
  429. [ 'ogc:Function_Name', ['nArgs'=>"1"], "log" ],
  430. [ 'ogc:Function_Name', ['nArgs'=>"4"], "LRSGeocode" ],
  431. [ 'ogc:Function_Name', ['nArgs'=>"5"], "LRSMeasure" ],
  432. [ 'ogc:Function_Name', ['nArgs'=>"5"], "LRSSegment" ],
  433. [ 'ogc:Function_Name', ['nArgs'=>"2"], "max" ],
  434. [ 'ogc:Function_Name', ['nArgs'=>"2"], "max_2" ],
  435. [ 'ogc:Function_Name', ['nArgs'=>"2"], "max_3" ],
  436. [ 'ogc:Function_Name', ['nArgs'=>"2"], "max_4" ],
  437. [ 'ogc:Function_Name', ['nArgs'=>"2"], "min" ],
  438. [ 'ogc:Function_Name', ['nArgs'=>"2"], "min_2" ],
  439. [ 'ogc:Function_Name', ['nArgs'=>"2"], "min_3" ],
  440. [ 'ogc:Function_Name', ['nArgs'=>"2"], "min_4" ],
  441. [ 'ogc:Function_Name', ['nArgs'=>"1"], "mincircle" ],
  442. [ 'ogc:Function_Name', ['nArgs'=>"1"], "minimumdiameter" ],
  443. [ 'ogc:Function_Name', ['nArgs'=>"1"], "minrectangle" ],
  444. [ 'ogc:Function_Name', ['nArgs'=>"2"], "modulo" ],
  445. [ 'ogc:Function_Name', ['nArgs'=>"2"], "MultiplyCoverages" ],
  446. [ 'ogc:Function_Name', ['nArgs'=>"3"], "Nearest" ],
  447. [ 'ogc:Function_Name', ['nArgs'=>"1"], "not" ],
  448. [ 'ogc:Function_Name', ['nArgs'=>"2"], "notEqualTo" ],
  449. [ 'ogc:Function_Name', ['nArgs'=>"2"], "numberFormat" ],
  450. [ 'ogc:Function_Name', ['nArgs'=>"5"], "numberFormat2" ],
  451. [ 'ogc:Function_Name', ['nArgs'=>"1"], "numGeometries" ],
  452. [ 'ogc:Function_Name', ['nArgs'=>"1"], "numInteriorRing" ],
  453. [ 'ogc:Function_Name', ['nArgs'=>"1"], "numPoints" ],
  454. [ 'ogc:Function_Name', ['nArgs'=>"1"], "octagonalenvelope" ],
  455. [ 'ogc:Function_Name', ['nArgs'=>"3"], "offset" ],
  456. [ 'ogc:Function_Name', ['nArgs'=>"2"], "overlaps" ],
  457. [ 'ogc:Function_Name', ['nArgs'=>"1"], "parameter" ],
  458. [ 'ogc:Function_Name', ['nArgs'=>"1"], "parseBoolean" ],
  459. [ 'ogc:Function_Name', ['nArgs'=>"1"], "parseDouble" ],
  460. [ 'ogc:Function_Name', ['nArgs'=>"1"], "parseInt" ],
  461. [ 'ogc:Function_Name', ['nArgs'=>"1"], "parseLong" ],
  462. [ 'ogc:Function_Name', ['nArgs'=>"0"], "pi" ],
  463. [ 'ogc:Function_Name', ['nArgs'=>"4"], "PointBuffers" ],
  464. [ 'ogc:Function_Name', ['nArgs'=>"2"], "pointN" ],
  465. [ 'ogc:Function_Name', ['nArgs'=>"7"], "PointStacker" ],
  466. [ 'ogc:Function_Name', ['nArgs'=>"6"], "PolygonExtraction" ],
  467. [ 'ogc:Function_Name', ['nArgs'=>"2"], "pow" ],
  468. [ 'ogc:Function_Name', ['nArgs'=>"1"], "property" ],
  469. [ 'ogc:Function_Name', ['nArgs'=>"1"], "PropertyExists" ],
  470. [ 'ogc:Function_Name', ['nArgs'=>"2"], "Quantile" ],
  471. [ 'ogc:Function_Name', ['nArgs'=>"3"], "Query" ],
  472. [ 'ogc:Function_Name', ['nArgs'=>"0"], "random" ],
  473. [ 'ogc:Function_Name', ['nArgs'=>"5"], "RangeLookup" ],
  474. [ 'ogc:Function_Name', ['nArgs'=>"1"], "RasterAsPointCollection" ],
  475. [ 'ogc:Function_Name', ['nArgs'=>"4"], "RasterZonalStatistics" ],
  476. [ 'ogc:Function_Name', ['nArgs'=>"5"], "Recode" ],
  477. [ 'ogc:Function_Name', ['nArgs'=>"2"], "RectangularClip" ],
  478. [ 'ogc:Function_Name', ['nArgs'=>"2"], "relate" ],
  479. [ 'ogc:Function_Name', ['nArgs'=>"3"], "relatePattern" ],
  480. [ 'ogc:Function_Name', ['nArgs'=>"3"], "Reproject" ],
  481. [ 'ogc:Function_Name', ['nArgs'=>"1"], "rint" ],
  482. [ 'ogc:Function_Name', ['nArgs'=>"1"], "round" ],
  483. [ 'ogc:Function_Name', ['nArgs'=>"1"], "round_2" ],
  484. [ 'ogc:Function_Name', ['nArgs'=>"1"], "roundDouble" ],
  485. [ 'ogc:Function_Name', ['nArgs'=>"6"], "ScaleCoverage" ],
  486. [ 'ogc:Function_Name', ['nArgs'=>"4"], "sdo_nn" ],
  487. [ 'ogc:Function_Name', ['nArgs'=>"2"], "setCRS" ],
  488. [ 'ogc:Function_Name', ['nArgs'=>"3"], "Simplify" ],
  489. [ 'ogc:Function_Name', ['nArgs'=>"1"], "sin" ],
  490. [ 'ogc:Function_Name', ['nArgs'=>"3"], "Snap" ],
  491. [ 'ogc:Function_Name', ['nArgs'=>"1"], "sqrt" ],
  492. [ 'ogc:Function_Name', ['nArgs'=>"2"], "StandardDeviation" ],
  493. [ 'ogc:Function_Name', ['nArgs'=>"1"], "startAngle" ],
  494. [ 'ogc:Function_Name', ['nArgs'=>"1"], "startPoint" ],
  495. [ 'ogc:Function_Name', ['nArgs'=>"1"], "strCapitalize" ],
  496. [ 'ogc:Function_Name', ['nArgs'=>"2"], "strConcat" ],
  497. [ 'ogc:Function_Name', ['nArgs'=>"2"], "strEndsWith" ],
  498. [ 'ogc:Function_Name', ['nArgs'=>"2"], "strEqualsIgnoreCase" ],
  499. [ 'ogc:Function_Name', ['nArgs'=>"2"], "strIndexOf" ],
  500. [ 'ogc:Function_Name', ['nArgs'=>"2"], "strLastIndexOf" ],
  501. [ 'ogc:Function_Name', ['nArgs'=>"1"], "strLength" ],
  502. [ 'ogc:Function_Name', ['nArgs'=>"2"], "strMatches" ],
  503. [ 'ogc:Function_Name', ['nArgs'=>"3"], "strPosition" ],
  504. [ 'ogc:Function_Name', ['nArgs'=>"4"], "strReplace" ],
  505. [ 'ogc:Function_Name', ['nArgs'=>"2"], "strStartsWith" ],
  506. [ 'ogc:Function_Name', ['nArgs'=>"3"], "strSubstring" ],
  507. [ 'ogc:Function_Name', ['nArgs'=>"2"], "strSubstringStart" ],
  508. [ 'ogc:Function_Name', ['nArgs'=>"1"], "strToLowerCase" ],
  509. [ 'ogc:Function_Name', ['nArgs'=>"1"], "strToUpperCase" ],
  510. [ 'ogc:Function_Name', ['nArgs'=>"1"], "strTrim" ],
  511. [ 'ogc:Function_Name', ['nArgs'=>"3"], "strTrim2" ],
  512. [ 'ogc:Function_Name', ['nArgs'=>"2"], "StyleCoverage" ],
  513. [ 'ogc:Function_Name', ['nArgs'=>"2"], "symDifference" ],
  514. [ 'ogc:Function_Name', ['nArgs'=>"1"], "tan" ],
  515. [ 'ogc:Function_Name', ['nArgs'=>"1"], "toDegrees" ],
  516. [ 'ogc:Function_Name', ['nArgs'=>"1"], "toRadians" ],
  517. [ 'ogc:Function_Name', ['nArgs'=>"2"], "touches" ],
  518. [ 'ogc:Function_Name', ['nArgs'=>"1"], "toWKT" ],
  519. [ 'ogc:Function_Name', ['nArgs'=>"2"], "Transform" ],
  520. [ 'ogc:Function_Name', ['nArgs'=>"2"], "union" ],
  521. [ 'ogc:Function_Name', ['nArgs'=>"2"], "UnionFeatureCollection" ],
  522. [ 'ogc:Function_Name', ['nArgs'=>"2"], "Unique" ],
  523. [ 'ogc:Function_Name', ['nArgs'=>"2"], "UniqueInterval" ],
  524. [ 'ogc:Function_Name', ['nArgs'=>"6"], "VectorToRaster" ],
  525. [ 'ogc:Function_Name', ['nArgs'=>"3"], "VectorZonalStatistics" ],
  526. [ 'ogc:Function_Name', ['nArgs'=>"1"], "vertices" ],
  527. [ 'ogc:Function_Name', ['nArgs'=>"2"], "within" ],
  528. ] ],
  529. ] ],
  530. ] ],
  531. ] ],
  532. ]);
  533. $xmlWriter->endElement();// WFS_Capabilities
  534. $xmlWriter->endDocument();
  535. exit;
  536. }
  537. public function _getXmlSchemaLocation() {
  538. $schemaLocations = array();
  539. //$schemaLocations[] = 'http://www.opengis.net/wfs http://webgis.regione.sardegna.it:80/geoserver/schemas/wfs/1.0.0/WFS-capabilities.xsd';// @from http://webgis.regione.sardegna.it/geoserver/ows?service=WFS&request=GetCapabilities
  540. return (!empty($schemaLocations))? 'xsi:schemaLocation="' . implode(' ', $schemaLocations) . '"' : '';
  541. }
  542. public function _printXmlNamespaceList() {
  543. $listNs = array();
  544. foreach (Api_WfsNs::getNsList() as $uri => $prefix) {
  545. $listNs[] = 'xmlns:' . $prefix . '="' . $uri . '"';
  546. }
  547. return implode("\n", $listNs);
  548. }
  549. public function _getSourceNsList() {
  550. $usrObjList = array();
  551. $tblsAcl = $this->_usrAcl->getTablesAcl();
  552. foreach ($tblsAcl as $tblAcl) {
  553. $dataSourceName = 'default_db';// TODO: getSourceName
  554. $tblName = $tblAcl->getName();
  555. $usrObjList[] = array($dataSourceName, $tblName);
  556. }
  557. $usrObjList[] = array('objects', 'File');
  558. $usrObjList[] = array('objects', 'TestPerms');
  559. $usrObjList[] = array('objects', 'Korespondencja');
  560. return $usrObjList;
  561. }
  562. public function _parseTransactionXmlStruct($requestXml, $requestXmlTags) {
  563. $DBG = V::get('DBG_XML', 0, $_GET, 'int');// TODO: Profiler
  564. $rootTagName = V::get('tag', '', $requestXmlTags[0]);
  565. if ('Transaction' != $rootTagName) throw new Api_WfsException("Parse Request XML Error - Missing Transaction as root xml tag", __LINE__, null, 'TransactionParseError', 'request');
  566. // TODO: special actions if action on nested objects
  567. // 1. convert request: wfs.transaction.convert-wfs-request.xsl
  568. // 2. validate converted request: wfs.transaction-converted-request.xsd
  569. // 3. execute request in data source
  570. // if($DBG){echo 'L.' . __LINE__ . ' $requestXmlTags:';print_r($requestXmlTags);echo "\n";}
  571. /*
  572. <Transaction
  573. xmlns="http://www.opengis.net/wfs"
  574. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  575. version="1.0.0"
  576. service="WFS"
  577. xmlns:p5_objects="https://biuro.biall-net.pl/wfs/objects"
  578. xmlns:gml="http://www.opengis.net/gml">
  579. <Insert xmlns="http://www.opengis.net/wfs">
  580. <TestPerms xmlns="https://biuro.biall-net.pl/wfs/objects">
  581. <plik xmlns="https://biuro.biall-net.pl/wfs/objects">
  582. <p5_objects:File>
  583. <p5_objects:name>blank-wfs.gif</p5_objects:name>
  584. <p5_objects:content>R0lGODlhAQABAIAAAP///////yH+EUNyZWF0ZWQgd2l0aCBHSU1QACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==</p5_objects:content>
  585. </p5_objects:File>
  586. </plik>
  587. </TestPerms>
  588. </Insert>
  589. </Transaction>
  590. */
  591. $actionXmlTags = array();// // [ 0 => [ 'action'=>Insert, 'typeName'=>str, 'tags'=>[] ]
  592. {// split xml for action tags (Insert, Update, Delete)
  593. $tagsCount = count($requestXmlTags);
  594. for ($i = 1, $actionTagName = null, $actionIdx = -1, $tagLvl = 0; $i < $tagsCount - 1; $i++) {// skip Transaction open/close tag
  595. $tag = $requestXmlTags[$i];
  596. if (null == $actionTagName) {
  597. $actionTagName = $tag['tag'];
  598. $tagLvl = $tag['level'];
  599. $actionIdx += 1;
  600. $actionXmlTags[$actionIdx] = array();
  601. $actionXmlTags[$actionIdx]['action'] = $actionTagName;
  602. $actionXmlTags[$actionIdx]['typeName'] = V::get('typeName', '', $tag['attributes']);
  603. $actionXmlTags[$actionIdx]['isDeepObject'] = null;// null - unknown, false - not seed, true - deep
  604. $actionXmlTags[$actionIdx]['tags'] = array();
  605. } else if ($tag['tag'] == $actionTagName && 'close' == $tag['type'] && $tagLvl == $tag['level']) {
  606. $actionTagName = null;
  607. } else {
  608. $actionXmlTags[$actionIdx]['tags'][] = $tag;
  609. }
  610. }
  611. }
  612. {// Validate Request: WFS allow multiple tags inside Insert tag
  613. // TODO: implement multiple tags in Insert tag if reauired. Use array_splice($actionXmlTags, $actionIdx, 0, $insertTags);
  614. {// throw (Not Implemented, 501) if found multiple tags in Insert tag
  615. foreach ($actionXmlTags as $actionIdx => $action) {
  616. if ('Insert' !== $action['action']) continue;
  617. $lvl = $action['tags'][0]['level'];
  618. for ($i = 1, $cnt = count($action['tags']); $i < $cnt - 1; $i++) {
  619. $tag = $action['tags'][$i];
  620. // if($DBG){echo 'L.' . __LINE__ . " actionXmlTags loop({$i}) \$tag:";print_r($tag);echo "\n";}
  621. if ($tag['level'] == $lvl) throw new Exception("Error Processing Request - multiple tags inside Insert tag is not implemented", 501);
  622. }
  623. }
  624. }
  625. }
  626. {// Insert tag - fix typeName from first tag, remove first and last tag - leave only fields
  627. foreach ($actionXmlTags as $actionIdx => $action) {
  628. if ('Insert' !== $action['action']) continue;
  629. array_pop($action['tags']);// remove last tag (close tag)
  630. $tag = array_shift($action['tags']);// remove last tag (close tag)
  631. $typeName = $tag['tag'];// eg. with prefix 'p5_objects:File' or without prefix but with @xmlns
  632. if (false === strpos($typeName, ':')) {
  633. $nsType = V::get('xmlns', '', $tag['attributes']);
  634. if (!$nsType) throw new Exception("Error Processing Request - Missing object namespace '{$tag['tag']}'");
  635. $prefix = Api_WfsNs::getNsPrefix($nsType);
  636. if (!$prefix) {
  637. if ($typeName == substr(rtrim($nsType, '/'), -1 * strlen($typeName))) {// typeName may be added to ns uri
  638. $nsBaseForType = substr(rtrim($nsType, '/'), 0, -1 * strlen($typeName) - 1);
  639. $prefix = Api_WfsNs::getNsPrefix($nsBaseForType);
  640. }
  641. }
  642. if (!$prefix) throw new Exception("Error Processing Request - Unrecognized namespace uri '{$nsType}' for object '{$tag['tag']}'");
  643. $typeName = "{$prefix}:{$typeName}";
  644. }
  645. $action['typeName'] = $typeName;
  646. $actionXmlTags[$actionIdx] = $action;
  647. }
  648. }
  649. {// validate
  650. // if($DBG){echo 'L.' . __LINE__ . ' before validate $actionXmlTags:';print_r($actionXmlTags);echo "\n";}
  651. foreach ($actionXmlTags as $actionIdx => $action) {
  652. if ('Insert' == $action['action']) {
  653. if (empty($action['typeName'])) throw new Exception("Error Processing Request - unknown object typeName to Insert");
  654. $acl = $this->getAclFromTypeName($action['typeName']);
  655. $actionXmlTags[$actionIdx] = $acl->validateInsertXml($action);
  656. } else if ('Update' == $action['action']) {
  657. $acl = $this->getAclFromTypeName($action['typeName']);
  658. $actionXmlTags[$actionIdx] = $acl->validateUpdateXml($action);
  659. } else if ('Delete' == $action['action']) {
  660. if($DBG>1){echo'<pre>$action: ';print_r($action);echo'</pre>';}
  661. $acl = $this->getAclFromTypeName($action['typeName']);
  662. $actionXmlTags[$actionIdx] = $acl->validateDeleteXml($action);
  663. } else {
  664. if($DBG>1){echo'<pre>$action: ';print_r($action);echo'</pre>';}
  665. throw new Exception("{$action['action']} action not implemented", 501);
  666. }
  667. // continue;// TODO: validate all by type
  668. }
  669. }
  670. if ('1' == V::get('DBG_DONT_CHANGE_DB', '', $_REQUEST)) {
  671. echo "----------------- action xml tags: ---------------" . "\n";
  672. print_r($actionXmlTags);
  673. die(".EOF\n");
  674. }
  675. {// execute
  676. $returnIds = array();
  677. $changesList = array();
  678. foreach ($actionXmlTags as $actionIdx => $action) {
  679. if ('Insert' == $action['action']) {
  680. if (empty($action['typeName'])) throw new Exception("Error Processing Request - unknown object typeName to Insert");
  681. $acl = $this->getAclFromTypeName($action['typeName']);
  682. $newId = $acl->insertXml($action);
  683. $returnIds[$actionIdx] = $newId;
  684. $changesList[$actionIdx] = array('Status'=>(($newId > 0)? 'SUCCESS' : 'FAILED'), 'Message'=>"created {$newId}.", 'Action' => $action['action']);
  685. if ($newId > 0) $changesList[$actionIdx]['fid'] = $acl->getName() . '.' . $newId;
  686. } else if ('Update' == $action['action']) {
  687. if($DBG>1){echo'<pre>$action: ';print_r($action);echo'</pre>';}
  688. $acl = $this->getAclFromTypeName($action['typeName']);
  689. $affected = $acl->updateXml($action);
  690. $changesList[$actionIdx] = array('Status'=>(($affected >= 0)? 'SUCCESS' : 'FAILED'), 'Message'=>"affected {$affected}.", 'Action' => $action['action']);
  691. } else if ('Delete' == $action['action']) {
  692. $acl = $this->getAclFromTypeName($action['typeName']);
  693. $affected = $acl->deleteXml($action);
  694. $changesList[$actionIdx] = array('Status'=>(($affected >= 0)? 'SUCCESS' : 'FAILED'), 'Message'=>"deleted {$affected}.", 'Action' => $action['action']);
  695. } else throw new Exception("TODO: {$action['action']} action not implemented", 501);
  696. }
  697. if($DBG){echo 'L.' . __LINE__ . ' $changesList:';print_r($changesList);echo "\n";}
  698. }
  699. return $this->_transactionResponse($changesList);
  700. }
  701. public function _transactionResponse($changesList) {
  702. // <WFS_TransactionResponse>
  703. // <TransactionResult>
  704. // <Status> : SUCCESS / FAILED / PARTIAL
  705. // [<Locator]
  706. // [<Message]
  707. $messageTag = '';
  708. $statusTag = '';
  709. $statusIsFailed = false;
  710. $statusAll = null;
  711. $createdFeatureId = array();
  712. foreach ($changesList as $featureId => $change) {
  713. if ('FAILED' == $change['Status']) {
  714. $statusIsFailed = true;
  715. }
  716. if ('SUCCESS' == $change['Status'] && !empty($change['fid'])) {
  717. $createdFeatureId[] = $change['fid'];
  718. }
  719. // if (!empty($change['Message'])) $messageTag .= "Feature '{$featureId}' {$change['Status']}: {$change['Message']}\n";
  720. }
  721. $statusTag = ($statusIsFailed)? 'FAILED' : 'SUCCESS';
  722. $statusTag = "<wfs:{$statusTag}/>";
  723. $messageTag = '';//"<wfs:Message>{$messageTag}</wfs:Message>";
  724. /* Example:
  725. <?xml version="1.0" encoding="UTF-8"?>
  726. <wfs:WFS_TransactionResponse version="1.0.0" xmlns:wfs="http://www.opengis.net/wfs"
  727. xmlns:ogc="http://www.opengis.net/ogc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  728. xsi:schemaLocation="http://www.opengis.net/wfs http://localhost:8080/geoserver/schemas/wfs/1.0.0/WFS-transaction.xsd">
  729. <wfs:InsertResult>
  730. <ogc:FeatureId fid="archsites.26" />
  731. </wfs:InsertResult>
  732. <wfs:TransactionResult handle="Updating Signature rock label">
  733. <wfs:Status>
  734. <wfs:SUCCESS />
  735. </wfs:Status>
  736. </wfs:TransactionResult>
  737. </wfs:WFS_TransactionResponse>*/
  738. // TODO: build xml by DOMDocument
  739. // TODO: xsi:schemaLocation="http://www.opengis.net/wfs http://localhost:8080/geoserver/schemas/wfs/1.0.0/WFS-transaction.xsd"
  740. $wfsInsertResult = '';
  741. if (!empty($createdFeatureId)) {
  742. $wfsInsertResult = "\n<wfs:InsertResult>\n";
  743. foreach ($createdFeatureId as $fid) {
  744. $wfsInsertResult .= '<ogc:FeatureId fid="' . $fid . '" xmlns:ogc="http://www.opengis.net/ogc"/>' . "\n";
  745. }
  746. $wfsInsertResult .= "\n</wfs:InsertResult>\n";
  747. EOF;
  748. }
  749. $tranRes = <<<EOF
  750. <wfs:WFS_TransactionResponse version="1.0.0"
  751. xmlns:wfs="http://www.opengis.net/wfs"
  752. xmlns:ogc="http://www.opengis.net/ogc"
  753. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  754. {$wfsInsertResult}
  755. <wfs:TransactionResult>
  756. <wfs:Status>{$statusTag}</wfs:Status>
  757. {$messageTag}
  758. </wfs:TransactionResult>
  759. </wfs:WFS_TransactionResponse>
  760. EOF;
  761. return $tranRes;
  762. }
  763. public function _convertTransactionXml($requestXmlString, $sourceNsList) {
  764. $DBG = (V::get('DBG_XSL', '', $_GET) > 0);// TODO: Profiler
  765. if($DBG){echo 'L.' . __LINE__ . ' sourceNsList:';print_r($sourceNsList);echo "\n";}
  766. $updateActionsXsd = array();
  767. $insertActionsXsd = array();
  768. $deleteActionsXsd = array();
  769. //<!-- TODO: create tag Update{X} where X is namespace index -->
  770. foreach ($sourceNsList as $nsInd => $sourceNs) {
  771. // <Update>
  772. $theGeomField = 'the_geom';// TODO: get from fields list
  773. $typeName = "p5_{$sourceNs[0]}:{$sourceNs[1]}";
  774. if($DBG){echo 'L.' . __LINE__ . ' typeName:';print_r($typeName);echo "\n";}
  775. $updateElementName = "UpdateNs{$nsInd}";
  776. $geomCoordsUpdateXpath = "//wfs:Value/*/gml:outerBoundaryIs/gml:LinearRing/gml:coordinates";
  777. $geomCoordsInsertXpath = "//*/gml:outerBoundaryIs/gml:LinearRing/gml:coordinates";
  778. $acl = $this->getAclFromTypeName($typeName);
  779. $geomType = $acl->getGeomFieldType($theGeomField);
  780. if ('polygon' == $geomType) {
  781. $geomCoordsUpdateXpath = ".//wfs:Value/*/gml:outerBoundaryIs/gml:LinearRing/gml:coordinates";
  782. $geomCoordsUpdateXpath = "((<xsl:value-of select=\"{$geomCoordsUpdateXpath}\"/>))";
  783. $geomCoordsInsertXpath = ".//*/gml:outerBoundaryIs/gml:LinearRing/gml:coordinates";
  784. $geomCoordsInsertXpath = "((<xsl:value-of select=\"{$geomCoordsInsertXpath}\"/>))";
  785. } else if ('linestring' == $geomType) {
  786. $geomCoordsUpdateXpath = ".//wfs:Value/*/gml:coordinates";
  787. $geomCoordsUpdateXpath = "(<xsl:value-of select=\"{$geomCoordsUpdateXpath}\"/>)";
  788. $geomCoordsInsertXpath = ".//*/gml:coordinates";
  789. $geomCoordsInsertXpath = "(<xsl:value-of select=\"{$geomCoordsInsertXpath}\"/>)";
  790. } else if ('point' == $geomType) {
  791. $geomCoordsUpdateXpath = ".//wfs:Value/*/gml:coordinates";
  792. $geomCoordsUpdateXpath = "(<xsl:value-of select=\"{$geomCoordsUpdateXpath}\"/>)";
  793. $geomCoordsInsertXpath = ".//*/gml:coordinates";
  794. $geomCoordsInsertXpath = "(<xsl:value-of select=\"{$geomCoordsInsertXpath}\"/>)";
  795. }
  796. $actionXsd = <<<EOF
  797. <xsl:when test="@typeName = '{$typeName}'">
  798. <xsl:element name="{$updateElementName}">
  799. <xsl:attribute name="typeName"><xsl:value-of select="@typeName" /></xsl:attribute>
  800. <xsl:attribute name="featureId"><xsl:value-of select="ogc:Filter/ogc:FeatureId/@fid" /></xsl:attribute>
  801. <xsl:for-each select="wfs:Property">
  802. <xsl:element name="{wfs:Name}">
  803. <xsl:choose>
  804. <xsl:when test="wfs:Name = '{$theGeomField}'"><xsl:value-of select="local-name(//wfs:Value/*[1])"/>{$geomCoordsUpdateXpath}</xsl:when>
  805. <xsl:otherwise><xsl:value-of select="wfs:Value"/></xsl:otherwise>
  806. </xsl:choose>
  807. </xsl:element>
  808. </xsl:for-each>
  809. </xsl:element>
  810. </xsl:when>
  811. EOF;
  812. $updateActionsXsd[] = $actionXsd;
  813. $typeName = "{$sourceNs[1]}";//"p5_{$sourceNs[0]}:{$sourceNs[1]}";
  814. $insertElementName = "InsertNs{$nsInd}";
  815. $actionXsd = <<<EOF
  816. <xsl:when test="local-name() = '{$typeName}'">
  817. <xsl:element name="{$insertElementName}">
  818. <xsl:attribute name="typeName"><xsl:value-of select="local-name()" /></xsl:attribute>
  819. <xsl:attribute name="typeNsUri"><xsl:value-of select="namespace-uri()" /></xsl:attribute>
  820. <xsl:for-each select="*">
  821. <xsl:element name="{local-name()}">
  822. <xsl:choose>
  823. <xsl:when test="local-name() = '{$theGeomField}'"><xsl:value-of select="local-name(*[1])"/>{$geomCoordsInsertXpath}</xsl:when>
  824. <xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
  825. </xsl:choose>
  826. </xsl:element>
  827. </xsl:for-each>
  828. </xsl:element>
  829. </xsl:when>
  830. EOF;
  831. $insertActionsXsd[] = $actionXsd;
  832. $deleteElementName = "DeleteNs{$nsInd}";
  833. $typeName = "p5_{$sourceNs[0]}:{$sourceNs[1]}";
  834. $actionXsd = <<<EOF
  835. <xsl:when test="@typeName = '{$typeName}'">
  836. <xsl:variable name="typeName" select="@typeName" />
  837. <xsl:for-each select="ogc:Filter/ogc:FeatureId">
  838. <xsl:element name="{$deleteElementName}">
  839. <xsl:attribute name="typeName"><xsl:value-of select="\$typeName" /></xsl:attribute>
  840. <xsl:attribute name="featureId"><xsl:value-of select="@fid" /></xsl:attribute>
  841. </xsl:element>
  842. </xsl:for-each>
  843. </xsl:when>
  844. EOF;
  845. $deleteActionsXsd[] = $actionXsd;
  846. }
  847. if (!empty($updateActionsXsd)) {
  848. $updateActionsXsd = implode("\n", $updateActionsXsd);
  849. $updateActionsXsd = <<<EOF
  850. <xsl:choose>
  851. {$updateActionsXsd}
  852. </xsl:choose>
  853. EOF;
  854. } else {
  855. $updateActionsXsd = '';
  856. }
  857. if (!empty($insertActionsXsd)) {
  858. $insertActionsXsd = implode("\n", $insertActionsXsd);
  859. $insertActionsXsd = <<<EOF
  860. <xsl:choose>
  861. {$insertActionsXsd}
  862. </xsl:choose>
  863. EOF;
  864. } else {
  865. $insertActionsXsd = '';
  866. }
  867. if (!empty($deleteActionsXsd)) {
  868. $deleteActionsXsd = implode("\n", $deleteActionsXsd);
  869. $deleteActionsXsd = <<<EOF
  870. <xsl:choose>
  871. {$deleteActionsXsd}
  872. </xsl:choose>
  873. EOF;
  874. } else {
  875. $deleteActionsXsd = '';
  876. }
  877. $convertTransactionXslString = '<?xml version="1.0"?>';
  878. $convertTransactionXslString .= <<<EOF
  879. <xsl:transform version="1.0"
  880. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  881. xmlns:wfs="http://www.opengis.net/wfs"
  882. xmlns:ogc="http://www.opengis.net/ogc"
  883. xmlns:gml="http://www.opengis.net/gml">
  884. <xsl:template match="/">
  885. <xsl:for-each select="wfs:Transaction">
  886. <Transaction>
  887. <xsl:attribute name="version"><xsl:value-of select="@version" /></xsl:attribute>
  888. <xsl:attribute name="service"><xsl:value-of select="@service" /></xsl:attribute>
  889. <xsl:for-each select="wfs:Update">
  890. {$updateActionsXsd}
  891. </xsl:for-each>
  892. <xsl:for-each select="wfs:Insert/*">
  893. {$insertActionsXsd}
  894. </xsl:for-each>
  895. <xsl:for-each select="wfs:Delete">
  896. {$deleteActionsXsd}
  897. </xsl:for-each>
  898. <!-- TODO: Native -->
  899. </Transaction>
  900. </xsl:for-each>
  901. </xsl:template>
  902. </xsl:transform>
  903. EOF;
  904. if($DBG){echo 'L.' . __LINE__ . ' $convertTransactionXslString:' . $convertTransactionXslString . "\n";}
  905. $requestXml = new DOMDocument();
  906. $requestXml->loadXml($requestXmlString);
  907. $convertTransactionXsl = new DOMDocument();
  908. $convertTransactionXsl->loadXml($convertTransactionXslString);
  909. $proc = new XSLTProcessor();
  910. $proc->importStylesheet($convertTransactionXsl);
  911. return $proc->transformToXML($requestXml);
  912. }
  913. public function _validateConvertedTransactionXml($convertedTransaction, $sourceNsList) {
  914. $DBG = (V::get('DBG_XSD', '', $_GET) > 0);// TODO: Profiler
  915. if($DBG){echo 'L.' . __LINE__ . ' sourceNsList:';print_r($sourceNsList);echo "\n";}
  916. $dom = new DOMDocument('1.0', 'utf-8');
  917. $dom->formatOutput = true;
  918. $dom->preserveWhiteSpace = false;
  919. $rootNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:schema');
  920. $dom->appendChild($rootNode);
  921. $rootNode->setAttribute('elementFormDefault', 'qualified');
  922. $rootNode->setAttribute('version', '1.0');
  923. {// <xsd:element name="Transaction" type="TransactionType">
  924. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  925. $rootNode->appendChild($elNode);
  926. $elNode->setAttribute('name', 'Transaction');
  927. $elNode->setAttribute('type', 'TransactionType');
  928. $cTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexType');
  929. $rootNode->appendChild($cTypeNode);
  930. $cTypeNode->setAttribute('name', 'TransactionType');
  931. $seqNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:sequence');
  932. $cTypeNode->appendChild($seqNode);
  933. $choiceNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:choice');
  934. $seqNode->appendChild($choiceNode);
  935. $choiceNode->setAttribute('minOccurs', '0');
  936. $choiceNode->setAttribute('maxOccurs', 'unbounded');
  937. // <!-- <xsd:element ref="Update"/> -->
  938. foreach ($sourceNsList as $nsInd => $sourceNs) {
  939. $typeName = "p5_{$sourceNs[0]}:{$sourceNs[1]}";
  940. $updateElementName = "UpdateNs{$nsInd}";
  941. $updateElementType = "UpdateNs{$nsInd}ElementType";
  942. $updateElemNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  943. $choiceNode->appendChild($updateElemNode);
  944. $updateElemNode->setAttribute('name', $updateElementName);
  945. $updateElemNode->setAttribute('type', $updateElementType);
  946. }
  947. // <!-- <xsd:element ref="Insert"/> -->
  948. foreach ($sourceNsList as $nsInd => $sourceNs) {
  949. $typeName = "p5_{$sourceNs[0]}:{$sourceNs[1]}";
  950. $insertElementName = "InsertNs{$nsInd}";
  951. $insertElementType = "InsertNs{$nsInd}ElementType";
  952. $insertElemNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  953. $choiceNode->appendChild($insertElemNode);
  954. $insertElemNode->setAttribute('name', $insertElementName);
  955. $insertElemNode->setAttribute('type', $insertElementType);
  956. }
  957. // <!-- <xsd:element ref="Delete"/> -->
  958. foreach ($sourceNsList as $nsInd => $sourceNs) {
  959. $typeName = "p5_{$sourceNs[0]}:{$sourceNs[1]}";
  960. $deleteElementName = "DeleteNs{$nsInd}";
  961. $deleteElementType = "DeleteNs{$nsInd}ElementType";
  962. $deleteElemNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  963. $choiceNode->appendChild($deleteElemNode);
  964. $deleteElemNode->setAttribute('name', $deleteElementName);
  965. $deleteElemNode->setAttribute('type', $deleteElementType);
  966. }
  967. // <!-- <xsd:element ref="Native"/> -->
  968. $attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
  969. $cTypeNode->appendChild($attrNode);
  970. $attrNode->setAttribute('name', 'version');
  971. $attrNode->setAttribute('type', 'xsd:string');
  972. $attrNode->setAttribute('use', 'required');
  973. $attrNode->setAttribute('fixed', '1.0.0');
  974. $attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
  975. $cTypeNode->appendChild($attrNode);
  976. $attrNode->setAttribute('name', 'service');
  977. $attrNode->setAttribute('type', 'xsd:string');
  978. $attrNode->setAttribute('use', 'required');
  979. $attrNode->setAttribute('fixed', 'WFS');
  980. }
  981. foreach ($sourceNsList as $nsInd => $sourceNs) {
  982. $transactionTypesList = array();
  983. $transactionTypesList[] = 'Update';
  984. $transactionTypesList[] = 'Insert';
  985. foreach ($transactionTypesList as $transactionType) {
  986. $typeName = "p5_{$sourceNs[0]}:{$sourceNs[1]}";
  987. if($DBG){echo 'L.' . __LINE__ . ' TODO: get acl typeName:';print_r($typeName);echo "\n";}
  988. $acl = $this->getAclFromTypeName($typeName);
  989. $updateElementName = "{$transactionType}Ns{$nsInd}";
  990. $updateElementType = "{$transactionType}Ns{$nsInd}ElementType";
  991. /*
  992. <xsd:complexType name="{$updateElementType}">
  993. <xsd:sequence>
  994. <xsd:element name="PARENT_ID" minOccurs="0" maxOccurs="1" type="xsd:integer" />
  995. </xsd:sequence>
  996. */
  997. $updateTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexType');
  998. $rootNode->appendChild($updateTypeNode);
  999. $updateTypeNode->setAttribute('name', $updateElementType);
  1000. {
  1001. $seqNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:all');
  1002. $updateTypeNode->appendChild($seqNode);
  1003. {
  1004. $pKeyField = $acl->getPrimaryKeyField();
  1005. $fldList = $this->_getFieldListFromAcl($acl);
  1006. // fields without geometry fields
  1007. foreach ($fldList as $fldName) {
  1008. if ($acl->isGeomField($fldName)) continue;
  1009. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1010. $seqNode->appendChild($elNode);
  1011. $elNode->setAttribute('name', $fldName);
  1012. $minOccurs = 0;
  1013. if ($pKeyField == $fldName) {
  1014. $minOccurs = '1';
  1015. } else {
  1016. $minOccurs = '0';
  1017. }
  1018. $elNode->setAttribute('minOccurs', $minOccurs);
  1019. $fldType = null;
  1020. if ($acl->isIntegerField($fldName)) {
  1021. $fldType = 'xsd:integer';
  1022. }
  1023. else if ($acl->isDecimalField($fldName)) {
  1024. $fldType = 'xsd:decimal';
  1025. }
  1026. else if ($acl->isDateField($fldName)) {
  1027. $fldType = 'xsd:date';
  1028. }
  1029. else if ($acl->isDateTimeField($fldName)) {
  1030. // $fldType = 'xsd:dateTime';
  1031. $fldType = null;// 'xsd:string';
  1032. $patternDataTime = "(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})(:(\d{2}))?";
  1033. // <xsd:element name="..." minOccurs="0" nillable="true">
  1034. // <xsd:simpleType>
  1035. // <xsd:restriction base="xsd:string">
  1036. // <xsd:pattern value="[a-zA-Z]+\(\((\-?\d+\.?\d*,\-?\d+\.?\d*)( \-?\d+\.?\d*,\-?\d+\.?\d*)+\)\)"/>
  1037. // </xsd:restriction>
  1038. // </xsd:simpleType>
  1039. // </xsd:element>
  1040. $simpleTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:simpleType');
  1041. $restrictionNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:restriction');
  1042. $patternNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:pattern');
  1043. $restrictionNode->setAttribute('base', 'xsd:string');
  1044. $patternNode->setAttribute('value', $patternDataTime);
  1045. $restrictionNode->appendChild($patternNode);
  1046. $simpleTypeNode->appendChild($restrictionNode);
  1047. $elNode->appendChild($simpleTypeNode);
  1048. // continue;// TODO: ? below added nillable = true, minOccurs = 0, type = $fldType
  1049. }
  1050. else if ($acl->isBinaryField($fldName)) {
  1051. $fldType = 'xsd:base64Binary';
  1052. }
  1053. else {
  1054. $fldType = 'xsd:string';
  1055. }
  1056. if ($fldType) $elNode->setAttribute('type', $fldType);
  1057. $elNode->setAttribute('nillable', 'true');
  1058. $elNode->setAttribute('minOccurs', '0');
  1059. }
  1060. // only geometry fields
  1061. foreach ($fldList as $fldName) {
  1062. if (!$acl->isGeomField($fldName)) continue;
  1063. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1064. $seqNode->appendChild($elNode);
  1065. $elNode->setAttribute('name', $fldName);
  1066. $minOccurs = 0;
  1067. if ($pKeyField == $fldName) {
  1068. $minOccurs = '1';
  1069. } else {
  1070. $minOccurs = '0';
  1071. }
  1072. $elNode->setAttribute('minOccurs', $minOccurs);
  1073. if ($acl->isGeomField($fldName)) {
  1074. //$fldType = 'gml:GeometryPropertyType';
  1075. // TODO: use geom types from gml to wkt
  1076. // TODO: pattern wg atrybutów gml:coordinates decimal="." cs="," ts=" "
  1077. $patternWkt = '';// TODO: error if empty - unsupported geom type
  1078. $patternNum = '\-?\d+\.?\d*';
  1079. $patternPoint = $patternNum . ',' . $patternNum;
  1080. $patternPoints = '(' . $patternPoint . ')( ' . $patternPoint . ')+';
  1081. $geomType = $acl->getGeomFieldType($fldName);
  1082. if ('polygon' == $geomType) {
  1083. // [a-zA-Z]+\(\((\-?\d+\.?\d*,\-?\d+\.?\d*)( (\-?\d+\.?\d*,\-?\d+\.?\d*))+\)\)
  1084. $patternWkt = '[a-zA-Z]+\(\(' . $patternPoints . '\)\)';
  1085. } else if ('linestring' == $geomType) {
  1086. // [a-zA-Z]+\((\-?\d+\.?\d*,\-?\d+\.?\d*)( (\-?\d+\.?\d*,\-?\d+\.?\d*))+\)
  1087. $patternWkt = '[a-zA-Z]+\(' . $patternPoints . '\)';
  1088. } else if ('point' == $geomType) {
  1089. // [a-zA-Z]+\(\-?\d\.?\d*,\-?\d\.?\d*\)
  1090. $patternWkt = '[a-zA-Z]+\(' . $patternPoint . '\)';
  1091. }
  1092. $simpleTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:simpleType');
  1093. $restrictionNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:restriction');
  1094. $patternNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:pattern');
  1095. $restrictionNode->setAttribute('base', 'xsd:string');
  1096. $patternNode->setAttribute('value', $patternWkt);
  1097. $restrictionNode->appendChild($patternNode);
  1098. $simpleTypeNode->appendChild($restrictionNode);
  1099. $elNode->appendChild($simpleTypeNode);
  1100. }
  1101. $elNode->setAttribute('nillable', 'true');
  1102. $elNode->setAttribute('minOccurs', '0');
  1103. }
  1104. }
  1105. }
  1106. $attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
  1107. $updateTypeNode->appendChild($attrNode);
  1108. $attrNode->setAttribute('name', 'typeName');
  1109. $attrNode->setAttribute('type', 'xsd:token');
  1110. $attrNode->setAttribute('use', 'required');
  1111. if ($transactionType == 'Insert') {
  1112. $attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
  1113. $updateTypeNode->appendChild($attrNode);
  1114. $attrNode->setAttribute('name', 'typeNsUri');
  1115. $attrNode->setAttribute('type', 'xsd:anyURI');
  1116. $attrNode->setAttribute('use', 'required');
  1117. }
  1118. if ($transactionType == 'Update') {
  1119. $attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
  1120. $updateTypeNode->appendChild($attrNode);
  1121. $attrNode->setAttribute('name', 'featureId');
  1122. $attrNode->setAttribute('use', 'required');
  1123. $sTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:simpleType');
  1124. $attrNode->appendChild($sTypeNode);
  1125. $resNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:restriction');
  1126. $sTypeNode->appendChild($resNode);
  1127. $resNode->setAttribute('base', 'xsd:string');
  1128. $patternNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:pattern');
  1129. $resNode->appendChild($patternNode);
  1130. $patternNode->setAttribute('value', '[a-zA-Z_][a-zA-Z0-9_]*\.[0-9]*');
  1131. }
  1132. }
  1133. {// 'Delete'
  1134. $typeName = "p5_{$sourceNs[0]}:{$sourceNs[1]}";
  1135. $acl = $this->getAclFromTypeName($typeName);
  1136. $deleteElementType = "DeleteNs{$nsInd}ElementType";
  1137. $deleteTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexType');
  1138. $rootNode->appendChild($deleteTypeNode);
  1139. $deleteTypeNode->setAttribute('name', $deleteElementType);
  1140. /* <xsd:attribute name="typeName" type="xsd:token" use="required"/> */
  1141. $attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
  1142. $deleteTypeNode->appendChild($attrNode);
  1143. $attrNode->setAttribute('name', 'typeName');
  1144. $attrNode->setAttribute('type', 'xsd:token');
  1145. $attrNode->setAttribute('use', 'required');
  1146. /* <xsd:attribute name="featureId" use="required"> */
  1147. $attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
  1148. $deleteTypeNode->appendChild($attrNode);
  1149. $attrNode->setAttribute('name', 'featureId');
  1150. $attrNode->setAttribute('use', 'required');
  1151. $sTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:simpleType');
  1152. $attrNode->appendChild($sTypeNode);
  1153. $resNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:restriction');
  1154. $sTypeNode->appendChild($resNode);
  1155. $resNode->setAttribute('base', 'xsd:string');
  1156. $patternNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:pattern');
  1157. $resNode->appendChild($patternNode);
  1158. $patternNode->setAttribute('value', '[a-zA-Z_][a-zA-Z0-9_]*\.[0-9]*');
  1159. }
  1160. }
  1161. $validateConvertedTransactionXsdString = $dom->saveXml();
  1162. if($DBG){echo 'L.' . __LINE__ . ' $validateConvertedTransactionXsdString:';print_r($validateConvertedTransactionXsdString);echo "\n";}
  1163. $reqXml = new DOMDocument();
  1164. $reqXml->loadXml($convertedTransaction);
  1165. // TODO: fetch PHP Warning: DOMDocument::schemaValidateSource(): Element 'PARENT_ID': 'abc' is not a valid value of the atomic type 'xs:integer'.
  1166. return $reqXml->schemaValidateSource($validateConvertedTransactionXsdString);
  1167. }
  1168. public function getInstanceFeatures($name, $args) {
  1169. $acl = $this->getAclFromTypeName("{$args['typePrefix']}:{$name}");
  1170. $baseNsUri = Api_WfsNs::getBaseWfsUri();
  1171. $rootWfsNs = 'p5';
  1172. $rootWfsNsUri = "{$baseNsUri}";
  1173. $wfsNs = $args['typePrefix'];
  1174. $wfsNsUri = "{$baseNsUri}/" . ('p5_' == substr($args['typePrefix'], 0, 3)) ? substr($args['typePrefix'], 3) : $args['typePrefix'];
  1175. $featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$args['xsd:type']}&REQUEST=DescribeFeatureType";
  1176. header('Content-type: application/xml; charset=utf-8');
  1177. $xmlWriter = new Core_XMLWriter();
  1178. if (!$xmlWriter) throw new HttpException("Error no XMLWriter", 404);
  1179. $xmlWriter->openUri('php://output');
  1180. $xmlWriter->setIndent(true);
  1181. $xmlWriter->startDocument('1.0','UTF-8');
  1182. $xmlWriter->startElement('wfs:FeatureCollection');
  1183. $xmlWriter->writeAttribute('xmlns:wfs', 'http://www.opengis.net/wfs');
  1184. $xmlWriter->writeAttribute('xmlns', 'http://www.opengis.net/wfs');
  1185. $xmlWriter->writeAttribute('xmlns:gml', 'http://www.opengis.net/gml');
  1186. $xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  1187. $xmlWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
  1188. $xmlWriter->writeAttribute("xmlns:{$wfsNs}", $wfsNsUri);
  1189. if (!$simple) $xmlWriter->writeAttribute("xmlns:{$rootWfsNs}", $rootWfsNsUri);
  1190. $xmlWriter->writeAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}");
  1191. $xmlWriter->writeComment("INSTANCE TABLE: " . Core_AclHelper::getInstanceTable($acl->getRootTableName()));
  1192. // <gml:featureMember>
  1193. // <p5_objects:TestPerms fid="TestPerms.64" p5:web_link="https://biuro.biall-net.pl/dev-pl/se-master/index.php?_route=ViewTableAjax&amp;namespace=default_objects/TestPerms#EDIT/64">
  1194. // <p5_objects:ID p5:allow_write="true">64</p5_objects:ID>
  1195. $xmlWriter->endElement();// wfs:FeatureCollection
  1196. $xmlWriter->endDocument();
  1197. exit;
  1198. }
  1199. public function _describeInstanceAttributeTable($nsPrefix, $name) {
  1200. $acl = $this->getAclFromTypeName("{$nsPrefix}:{$name}");
  1201. header('Content-type: application/xml; charset=utf-8');
  1202. $xmlWriter = new Core_XMLWriter();
  1203. if (!$xmlWriter) throw new HttpException("Error no XMLWriter", 404);
  1204. $xmlWriter->openUri('php://output');
  1205. $xmlWriter->setIndent(true);
  1206. $xmlWriter->startDocument('1.0','UTF-8');
  1207. $xmlWriter->startElement('xsd:schema');
  1208. $xmlWriter->writeAttribute('xmlns:xsd', 'http://www.w3.org/2001/XMLSchema');
  1209. $xmlWriter->writeAttribute('xmlns:gml', 'http://www.opengis.net/gml');
  1210. foreach (Api_WfsNs::getNsList() as $uri => $prefix) {
  1211. $xmlWriter->writeAttribute("xmlns:{$prefix}", $uri);
  1212. }
  1213. $xmlWriter->writeAttribute('elementFormDefault', 'qualified');
  1214. $xsdInstanceType = $acl->getName() . "_instanceType";
  1215. $xsdPrimaryKey = "xsd:string";// TODO: get from $acl
  1216. $xmlWriter->h('xsd:element', ['name'=>"instance"], [
  1217. [ 'xsd:complexType', [
  1218. [ 'xsd:sequence', [
  1219. [ 'xsd:element', ['name'=>"primaryKey", 'type'=>$xsdPrimaryKey], null ],
  1220. [ 'xsd:element', ['name'=>"instance", 'type'=>$xsdInstanceType], null ],
  1221. [ 'xsd:element', ['name'=>"type", 'type'=>"xsd:string"], null ],// TODO: instance, derived
  1222. [ 'xsd:element', ['name'=>"create_author", 'type'=>"xsd:string"], null ],
  1223. [ 'xsd:element', ['name'=>"create_date", 'type'=>"xsd:dateTime"], null ],
  1224. [ 'xsd:element', ['name'=>"update_author", 'type'=>"xsd:string"], null ],
  1225. [ 'xsd:element', ['name'=>"updage_date", 'type'=>"xsd:dateTime"], null ],
  1226. ] ]
  1227. ] ]
  1228. ]);
  1229. $instanceList = [];
  1230. $instanceList[] = [ 'xsd:enumeration', ['value'=>$acl->getName()], null ];
  1231. if (method_exists($acl, 'getInstanceList')) {
  1232. foreach ($acl->getInstanceList() as $instanceName) {
  1233. $instanceList[] = [ 'xsd:enumeration', ['value'=>$instanceName], null ];
  1234. }
  1235. }
  1236. $xmlWriter->h('xsd:simpleType', ['name'=>$xsdInstanceType], [
  1237. [ 'xsd:restriction', ['base'=>"xsd:string"], $instanceList],
  1238. ]);
  1239. $xmlWriter->endElement();// xsd:schema
  1240. $xmlWriter->endDocument();
  1241. exit;
  1242. }
  1243. public function _getDescribeFeatureType($nsPrefix, $type, $simple = true) {
  1244. return $this->_getDescribeFeatureTypes(array(array($nsPrefix, $type)), $simple);
  1245. }
  1246. public function _parseDescribeFeatureTypeRequest($reqContent, $simple = true) {
  1247. $parserXml = xml_parser_create();
  1248. xml_parser_set_option($parserXml, XML_OPTION_CASE_FOLDING, 0);
  1249. xml_parser_set_option($parserXml, XML_OPTION_SKIP_WHITE, 1);
  1250. if (0 == xml_parse_into_struct($parserXml, $reqContent, $tags)) {
  1251. throw new Exception("Error parsing xml");
  1252. }
  1253. xml_parser_free($parserXml);
  1254. if (empty($tags)) {
  1255. throw new Exception("Empty structure from request");
  1256. }
  1257. $rootTagName = V::get('tag', '', $tags[0]);
  1258. if ('DescribeFeatureType' != $rootTagName) {
  1259. throw new Api_WfsException("Wrong xml root tag '{$rootTagName}' #" . __LINE__, 501);
  1260. }
  1261. $requestXmlTags = $tags;
  1262. $DBG = (V::get('DBG_XML', '', $_GET) > 0);// TODO: Profiler
  1263. $rootTagName = V::get('tag', '', $requestXmlTags[0]);
  1264. if ('DescribeFeatureType' != $rootTagName) {
  1265. throw new Exception("Parse Request xml error #" . __LINE__);
  1266. }
  1267. /*[1] => Array(
  1268. [tag] => TypeName
  1269. [type] => complete
  1270. [level] => 2
  1271. [value] => p5_default_db:Rozdzielcza_rurociag_wsg84)
  1272. */
  1273. $typeNames = array();
  1274. $totalTypes = count($requestXmlTags) - 1;
  1275. for ($i = 1; $i < $totalTypes; $i++) {
  1276. if($DBG){echo "TAG[{$i}]:" . json_encode($requestXmlTags[$i]) . "\n";}
  1277. $typeNames[] = explode(':', $requestXmlTags[$i]['value'], 2);
  1278. }
  1279. //echo "typeNames: " . json_encode($typeNames) . "\n";
  1280. return $this->_getDescribeFeatureTypes($typeNames, $simple);
  1281. }
  1282. public function _getDescribeFeatureAllTypes($simple = true) {
  1283. $db = DB::getDB();
  1284. $idDefaultDB = $db->_zasob_id;
  1285. $tblsAcl = $this->_getTableAclList();
  1286. foreach ($tblsAcl as $tblAcl) {
  1287. $dataSourceName = 'default_db';// TODO: getSourceName
  1288. $tblName = $tblAcl->getName();
  1289. $typeNames[] = array("p5_{$dataSourceName}", $tblName);
  1290. }
  1291. return $this->_getDescribeFeatureTypes($typeNames, $simple);
  1292. }
  1293. // @param $typeNames = array( array( $nsPrefix, $type ) )
  1294. public function _getDescribeFeatureTypes($typeNames, $simple = true) {
  1295. if (empty($typeNames)) throw new HttpException("Feature Type Names not defined", 400);
  1296. $this->DBG("types:" . json_encode($typeNames), __LINE__, __FUNCTION__, __CLASS__);
  1297. $baseNsUri = Api_WfsNs::getBaseWfsUri();
  1298. $rootWfsNs = 'p5';
  1299. $rootWfsNsUri = "{$baseNsUri}";
  1300. $featureTypeUri = Api_WfsNs::getBaseWfsUri() . "?SERVICE=WFS&VERSION=1.0.0&REQUEST=DescribeFeatureType";
  1301. header('Content-type: application/xml');
  1302. // <xsd:import namespace="http://www.opengis.net/gml" schemaLocation="http://webgis.regione.sardegna.it:80/geoserver/schemas/gml/2.1.2/feature.xsd"/>
  1303. // <xsd:element name="{type}" substitutionGroup="gml:_Feature" type="dbu:{typeName}"/>
  1304. $nsMap = Api_WfsNs::getNsList();// uri => prefix
  1305. $dom = new DOMDocument('1.0', 'utf-8');
  1306. $dom->formatOutput = true;
  1307. $dom->preserveWhiteSpace = false;
  1308. $rootNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:schema');
  1309. $dom->appendChild($rootNode);
  1310. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
  1311. $rootNode->setAttribute('elementFormDefault', 'qualified');
  1312. // $rootNode->setAttribute('targetNamespace', Api_WfsNs::getNsUri('p5_default_db'));// TODO:? what targetNamespace if showing types from p5_objects and p5_default_db
  1313. {// <xsd:import namespace="http://www.opengis.net/gml" schemaLocation="...../gml/2.1.2/feature.xsd"/>
  1314. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:import');
  1315. $rootNode->appendChild($elNode);
  1316. $elNode->setAttribute('namespace', "http://www.opengis.net/gml");
  1317. $elNode->setAttribute('schemaLocation', Request::getPathUri() . "schema/gml/2.1.2/feature.xsd");
  1318. }
  1319. // TODO: fix namespace BUG for multiple types:
  1320. // - fetch namespace for first type
  1321. // - if another object has another namespace then -> import tag
  1322. foreach ($typeNames as $typeNameEx) {// $typeNameEx = [ nsPrefix , name => $objectName ]
  1323. $objectName = $typeNameEx[1];
  1324. $acl = $this->getAclFromTypeName("{$typeNameEx[0]}:{$objectName}");
  1325. {// xmlns and targetNamespace for root element
  1326. $aclNamespaceUri = Api_WfsNs::getNsUri($acl->getSourceName());
  1327. $rootNode->setAttribute('targetNamespace', $aclNamespaceUri);
  1328. // TODO: if targetNamespace already set then import tag
  1329. if (!array_key_exists($aclNamespaceUri, $nsMap)) $nsMap[$aclNamespaceUri] = $acl->getSourceName();
  1330. }
  1331. $objectXsdName = "{$objectName}Type";
  1332. if ($acl->hasSimpleSchema()) {
  1333. $simpleSchema = $acl->getSimpleSchema();
  1334. foreach ($simpleSchema as $ssName => $schema) {
  1335. if ('root' == $ssName) $ssName = $objectName;
  1336. list($nsUri, $prefix, $name) = Api_WfsNs::parseObjectNsUri($schema['@namespace']);
  1337. if (!array_key_exists($nsUri, $nsMap)) $nsMap[$nsUri] = $prefix;
  1338. }
  1339. foreach ($nsMap as $uri => $prefix) {
  1340. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', "xmlns:{$prefix}", $uri);
  1341. }
  1342. $aliasRefMap = array();// fieldName => namespace uri
  1343. foreach ($simpleSchema as $ssName => $schema) {
  1344. if ('root' == $ssName) $ssName = $objectName;
  1345. foreach ($schema as $fieldName => $field) {
  1346. if (is_array($field)
  1347. && !empty($field['@ref'])
  1348. && false !== strpos($field['@ref'], '/')
  1349. ) {// @ref_uri
  1350. // $elNode->setAttribute('ref', "{$tnsPrefix}:{$fieldName}");
  1351. $aliasRefMap[ "{$ssName}_{$fieldName}" ] = $field['@ref'];
  1352. }
  1353. }
  1354. }
  1355. if (!empty($aliasRefMap)) {
  1356. foreach ($aliasRefMap as $fieldName => $aliasNsUri) {
  1357. list($nsUri, $prefix, $name) = Api_WfsNs::parseObjectNsUri($aliasNsUri);
  1358. if (!array_key_exists($nsUri, $nsMap)) $nsMap[$nsUri] = $prefix;
  1359. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:import');
  1360. $rootNode->appendChild($elNode);
  1361. $elNode->setAttribute('namespace', $nsUri);
  1362. $elNode->setAttribute('schemaLocation', "{$nsUri}.xsd");// TODO: real file url -> DescribeFeatureType[Advanced]
  1363. }
  1364. }
  1365. foreach ($simpleSchema as $ssName => $schema) {
  1366. {// code from Code_AclSimpleSchema
  1367. if ('root' == $ssName) $ssName = $objectName;
  1368. // <xsd:complexType name="ZaliczkaPozycjaType">
  1369. // <xsd:complexContent>
  1370. // <xsd:extension base="gml:AbstractFeatureType">
  1371. // <xsd:sequence>
  1372. // <xsd:element minOccurs="0" maxOccurs="1" name="id" type="xsd:integer" nillable="true"/>
  1373. // <xsd:element minOccurs="0" maxOccurs="1" name="created" type="xsd:date" nillable="true"/>
  1374. // <xsd:element minOccurs="0" maxOccurs="1" ref="default_objects/AccessOwner" p5:name="worker"/>
  1375. // <xsd:element minOccurs="0" maxOccurs="1" name="kwota" type="xsd:decimal" nillable="true"/>
  1376. // <xsd:element minOccurs="0" maxOccurs="1" ref="ZaliczkaPozycja" p5:name="pozycja"/>
  1377. // </xsd:sequence>
  1378. // </xsd:extension>
  1379. // </xsd:complexContent>
  1380. // </xsd:complexType>
  1381. $cTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexType');
  1382. $rootNode->appendChild($cTypeNode);
  1383. $cTypeNode->setAttribute('name', "{$ssName}Type");
  1384. $seqNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:sequence');
  1385. $cTypeNode->appendChild($seqNode);
  1386. // [@namespace] => default_db/ZALICZKA_POZYCJA/ZaliczkaPozycja
  1387. // [id] => xsd:integer
  1388. // [kwota] => xsd:decimal
  1389. // [korespondencja] => [ '@ref' => Korespondencja ]
  1390. // [projekt] => [ '@ref' => Projekt ]
  1391. $tnsPrefix = $acl->getSourceName();
  1392. foreach ($schema as $fieldName => $field) {
  1393. // TODO: p5:field_name
  1394. if ('@' == substr($fieldName, 0, 1)) continue;// skip tags
  1395. // <xsd:element minOccurs="0" maxOccurs="1" name="id" type="xsd:integer" nillable="true"/>
  1396. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1397. $seqNode->appendChild($elNode);
  1398. if (!is_array($field)) throw new Exception("Error Processing simpleSchema: '{$ssName}/{$fieldName}'");
  1399. if (!empty($field['@type'])) {
  1400. $elNode->setAttribute('name', $fieldName);
  1401. $elNode->setAttribute('type', $field['@type']);
  1402. $elNode->setAttribute('nillable', "true");
  1403. } else if (!empty($field['@ref'])) {
  1404. if (false !== strpos($field['@ref'], '/')) {// @ref_uri
  1405. $elNode->setAttribute('ref', "{$tnsPrefix}:{$ssName}_{$fieldName}");
  1406. $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:name", $fieldName);
  1407. } else {
  1408. $elNode->setAttribute('ref', "{$tnsPrefix}:{$field['@ref']}");
  1409. if ($fieldName != $field['@ref']) {
  1410. $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:name", $fieldName);
  1411. }
  1412. }
  1413. } else {
  1414. throw new Exception("Error Processing simpleSchema - missing @type or @ref: '{$ssName}/{$fieldName}'");
  1415. }
  1416. if (array_key_exists('@minOccurs', $field)) $elNode->setAttribute('minOccurs', $field['@minOccurs']);
  1417. if (array_key_exists('@maxOccurs', $field)) $elNode->setAttribute('maxOccurs', $field['@maxOccurs']);
  1418. }
  1419. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1420. $rootNode->appendChild($elNode);
  1421. $elNode->setAttribute('name', $ssName);
  1422. $elNode->setAttribute('type', "{$tnsPrefix}:{$ssName}Type");
  1423. if (!$simple) {
  1424. if (!empty($schema['@namespace'])) {// TODO: @namespace is required?
  1425. list($nsUri, $prefix, $name) = Api_WfsNs::parseObjectNsUri($schema['@namespace']);
  1426. $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:namespace", '{' . $nsUri . '}' . $name);
  1427. }
  1428. }
  1429. }
  1430. }
  1431. if (!empty($aliasRefMap)) {
  1432. foreach ($aliasRefMap as $fieldName => $aliasNsUri) {
  1433. list($nsUri, $prefix, $name) = Api_WfsNs::parseObjectNsUri($aliasNsUri);
  1434. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1435. $rootNode->appendChild($elNode);
  1436. $elNode->setAttribute('name', $fieldName);
  1437. $elNode->setAttribute('type', "{$prefix}:{$name}Type");
  1438. }
  1439. }
  1440. if (V::get('DBG_SS', '', $_GET)) echo "\n<!--\n" . $acl . "\n-->\n";// TODO: DBG
  1441. continue;
  1442. }
  1443. $fldList = $this->_getFieldListFromAcl($acl);
  1444. DBG::_('DBG_XSD', '>1', "field list", $fldList, __CLASS__, __FUNCTION__, __LINE__);
  1445. $cTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexType');
  1446. $rootNode->appendChild($cTypeNode);
  1447. $cTypeNode->setAttribute('name', $objectXsdName);
  1448. if (!$simple) $cTypeNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:web_link", Request::getPathUri() . "index.php?_route=ViewTableAjax&namespace=" . $acl->getNamespace());
  1449. $cConNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexContent');
  1450. $cTypeNode->appendChild($cConNode);
  1451. $extNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:extension');
  1452. $cConNode->appendChild($extNode);
  1453. $extNode->setAttribute('base', 'gml:AbstractFeatureType');
  1454. $seqNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:sequence');
  1455. $extNode->appendChild($seqNode);
  1456. // <xsd:element maxOccurs="1" minOccurs="0" name="{$fldName}" nillable="true" type="xsd:integer"/>
  1457. $pKeyField = $acl->getPrimaryKeyField();
  1458. $p5Attributes = ($simple)? array() : $acl->getAttributesFromZasoby();
  1459. foreach ($fldList as $fldName) {
  1460. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1461. $seqNode->appendChild($elNode);
  1462. $minOccurs = 0;
  1463. if ($pKeyField == $fldName) {
  1464. $minOccurs = '1';
  1465. } else {
  1466. $minOccurs = '0';
  1467. }
  1468. $elNode->setAttribute('minOccurs', $minOccurs);
  1469. $elNode->setAttribute('maxOccurs', '1');
  1470. $fldType = $acl->getXsdFieldType($fldName);
  1471. if (!$simple && $acl->isEnumerationField($fldName)) {
  1472. $fldType = $acl->getSourceName() . ":{$fldName}Type";
  1473. }
  1474. if ('ref:' == substr($fldType, 0, 4)) {
  1475. $elNode->setAttribute('ref', substr($fldType, 4));
  1476. $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:name", $fldName);
  1477. } else if ('local_ref:' == substr($fldType, 0, 10)) {
  1478. $elNode->setAttribute('type', "{$fldName}Type");
  1479. $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:name", $fldName);
  1480. } else if ('alias_ref:' == substr($fldType, 0, 10)) {
  1481. $elNode->setAttribute('ref', $acl->getSourceName() . ":{$fldName}");
  1482. $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:name", $fldName);
  1483. } else {
  1484. $elNode->setAttribute('name', $fldName);
  1485. $elNode->setAttribute('type', $fldType);
  1486. $elNode->setAttribute('nillable', 'true');// nillable not allowed in ref
  1487. }
  1488. if (!$simple) {
  1489. if (!empty($p5Attributes[$fldName])) {
  1490. $p5attrs = $p5Attributes[$fldName];
  1491. if (!empty($p5attrs['id_zasob'])) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:id_zasob", $p5attrs['id_zasob']);
  1492. if (!empty($p5attrs['label'])) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:label", $p5attrs['label']);
  1493. if (!empty($p5attrs['description'])) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:description", $p5attrs['description']);
  1494. }
  1495. if ($acl->canWriteField($fldName)) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_write", "true");
  1496. if ($acl->canCreateField($fldName)) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_create", "true");
  1497. if (!$acl->canReadField($fldName)) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_read", "false");
  1498. }
  1499. }
  1500. if (!$simple) {
  1501. $instanceNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
  1502. $extNode->appendChild($instanceNode);
  1503. $instanceNode->setAttribute('name', 'instance');
  1504. $instanceNode->setAttribute('type', $acl->getSourceName() . ':' . "instanceType");
  1505. $instanceTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:simpleType');
  1506. $rootNode->appendChild($instanceTypeNode);
  1507. $instanceTypeNode->setAttribute('name', "instanceType");
  1508. $insResNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:restriction');
  1509. $instanceTypeNode->appendChild($insResNode);
  1510. $insResNode->setAttribute('base', "xsd:string");
  1511. $insEnumNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:enumeration');
  1512. $insResNode->appendChild($insEnumNode);
  1513. $insEnumNode->setAttribute('value', $acl->getName());
  1514. if (method_exists($acl, 'getInstanceList')) {
  1515. foreach ($acl->getInstanceList() as $instanceName) {
  1516. $insEnumNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:enumeration');
  1517. $insResNode->appendChild($insEnumNode);
  1518. $insEnumNode->setAttribute('value', $instanceName);
  1519. }
  1520. }
  1521. }
  1522. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1523. $rootNode->appendChild($elNode);
  1524. $elNode->setAttribute('name', $objectName);
  1525. $elNode->setAttribute('type', $acl->getSourceName() . ':' . $objectXsdName);
  1526. $elNode->setAttribute('substitutionGroup', 'gml:_Feature');// gml:AbstractFeature in gml 3
  1527. foreach ($fldList as $fldName) {
  1528. $fldType = $acl->getXsdFieldType($fldName);
  1529. if ('alias_ref:' == substr($fldType, 0, 10)) {
  1530. $localRefType = substr($fldType, 10);
  1531. // <xsd:element name="{$fldName}" ref="{$localRefType}">
  1532. $ctNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1533. $rootNode->appendChild($ctNode);
  1534. $ctNode->setAttribute('name', $fldName);
  1535. $ctNode->setAttribute('type', $localRefType);
  1536. } else if ('local_ref:' == substr($fldType, 0, 10)) {
  1537. $localRefType = substr($fldType, 10);
  1538. // <xsd:complexType name="produkt_Type">
  1539. // <xsd:sequence>
  1540. // <xsd:element name="idProd" type="xsd:integer" />
  1541. // <xsd:element name="nazwa" type="xsd:string" />
  1542. // <xsd:element name="cena" type="xsd:decimal" />
  1543. // </xsd:sequence>
  1544. // </xsd:complexType>
  1545. $ctNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexType');
  1546. $rootNode->appendChild($ctNode);
  1547. $ctNode->setAttribute('name', "{$fldName}Type");
  1548. $seqNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:sequence');
  1549. $ctNode->appendChild($seqNode);
  1550. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1551. $seqNode->appendChild($elNode);
  1552. $elNode->setAttribute('name', "test_id");
  1553. $elNode->setAttribute('type', "xsd:integer");
  1554. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1555. $seqNode->appendChild($elNode);
  1556. $elNode->setAttribute('name', "test_opis");
  1557. $elNode->setAttribute('type', "xsd:string");
  1558. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1559. $seqNode->appendChild($elNode);
  1560. $elNode->setAttribute('name', "test_decimal");
  1561. $elNode->setAttribute('type', "xsd:decimal");
  1562. }
  1563. }
  1564. if (!$simple) {
  1565. foreach ($fldList as $fldName) {
  1566. if ($acl->isEnumerationField($fldName)) {
  1567. $enum = $acl->getEnumerations($fldName);
  1568. $stNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:simpleType');
  1569. $stNode->setAttribute('name', "{$fldName}Type");// $acl->getSourceName() . ":{$fldName}Type"
  1570. $rootNode->appendChild($stNode);
  1571. $resNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:restriction');
  1572. $stNode->appendChild($resNode);
  1573. $resNode->setAttribute('base', 'xsd:string');
  1574. // TODO: if (!empty($p5Attributes[$fldName]['valuesMap'])) -> show only this values
  1575. foreach ($enum as $val => $label) {
  1576. $enumNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:enumeration');
  1577. $resNode->appendChild($enumNode);
  1578. $enumNode->setAttribute('value', "{$val}");
  1579. if (!empty($p5Attributes[$fldName]['valuesMap'][$val])) {
  1580. $enumNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:label", $p5Attributes[$fldName]['valuesMap'][$val]);
  1581. }
  1582. }
  1583. }
  1584. }
  1585. }
  1586. }
  1587. foreach ($nsMap as $uri => $prefix) {
  1588. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', "xmlns:{$prefix}", $uri);
  1589. }
  1590. return $dom->saveXML();
  1591. }
  1592. public function _getTableAclList() {// Use only Tables from default_db
  1593. $tblAclList = array();
  1594. $db = DB::getDB();
  1595. $idDefaultDB = $db->_zasob_id;
  1596. $fullTblAclList = $this->_usrAcl->getTablesAcl();
  1597. foreach ($fullTblAclList as $tblAcl) {
  1598. $dataSourceName = 'default_db';// TODO: getSourceName
  1599. $tblName = $tblAcl->getName();
  1600. if ($idDefaultDB != $tblAcl->getDB()) {// hide non default_db tables
  1601. continue;
  1602. }
  1603. try {
  1604. $acl = $this->getAclFromTypeName($typeName = "p5_{$dataSourceName}:{$tblName}");
  1605. } catch (Exception $e) {
  1606. // echo "Error for table({$tblName}): " . $e->getMessage() . "\n";
  1607. }
  1608. if (!$acl) {
  1609. // TODO: error log msg
  1610. continue;
  1611. }
  1612. $tblAclList[] = $tblAcl;
  1613. }
  1614. return $tblAclList;
  1615. }
  1616. public function _getFieldListFromAcl($acl) {
  1617. $fldList = $acl->getRealFieldListByIdZasob();
  1618. return $fldList;
  1619. }
  1620. public function setLogger($logger) {
  1621. $this->_logger = $logger;
  1622. }
  1623. public function DBG($reqLog, $lineNr = null, $funName = null, $className = null) {
  1624. if (!$this->_logger) return;
  1625. $this->_logger->DBG($reqLog, $lineNr, $funName, $className);
  1626. }
  1627. public function convertOgcFilterFromRequestBody($requestOgcFilter) {
  1628. $ogcFilter = '';
  1629. if (empty($requestOgcFilter)) return '';
  1630. {
  1631. $convertOgcFilterXslString .= <<<EOF
  1632. <xsl:transform version="1.0"
  1633. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  1634. xmlns:wfs="http://www.opengis.net/wfs"
  1635. xmlns:ogc="http://www.opengis.net/ogc"
  1636. xmlns:gml="http://www.opengis.net/gml">
  1637. <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
  1638. <xsl:template match="/">
  1639. <xsl:for-each select="//*[local-name() = 'GetFeature']">
  1640. <xsl:copy-of select="ogc:Filter"/>
  1641. </xsl:for-each>
  1642. </xsl:template>
  1643. </xsl:transform>
  1644. EOF;
  1645. DBG::_('DBG_XML', '>2', "convertOgcFilterXslString", $convertOgcFilterXslString, __CLASS__, __FUNCTION__, __LINE__);
  1646. $convertTransactionXsl = new DOMDocument();
  1647. $convertTransactionXsl->loadXml($convertOgcFilterXslString);
  1648. $requestXml = new DOMDocument();
  1649. $requestXml->loadXml($requestOgcFilter);
  1650. $proc = new XSLTProcessor();
  1651. $proc->importStylesheet($convertTransactionXsl);
  1652. $ogcFilter = $proc->transformToXML($requestXml);
  1653. DBG::_('DBG_XML', '>2', "ogcFilter", $ogcFilter, __CLASS__, __FUNCTION__, __LINE__);
  1654. }
  1655. return $ogcFilter;
  1656. }
  1657. }