WfsServerBase.php 80 KB

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