WfsServerBase.php 78 KB

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