WfsServerBase.php 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. <?php
  2. Lib::loadClass('Api_WfsException');
  3. Lib::loadClass('Api_WfsGeomTypeConverter');
  4. class Api_WfsServerBase {
  5. public $_usrAcl;
  6. public $_typeConverter;
  7. public $_apiBaseUri;
  8. public function __construct($usrAcl) {
  9. $this->_usrAcl = $usrAcl;
  10. $this->_typeConverter = new Api_WfsGeomTypeConverter();
  11. $this->_apiBaseUri = '';
  12. }
  13. public function setBaseUri($uri) {
  14. $this->_apiBaseUri = $uri;
  15. }
  16. public function getBaseUri() {
  17. return $this->_apiBaseUri;
  18. }
  19. public function getBaseNamespaceUri() {
  20. $baseNsUri = $this->_apiBaseUri;
  21. $protocolType = '';
  22. if (!empty($baseNsUri)) {
  23. if ('https://' == substr($baseNsUri, 0, 8)) {
  24. $protocolType = 'https';
  25. $baseNsUri = substr($baseNsUri, 8);
  26. } else if ('http://' == substr($baseNsUri, 0, 7)) {
  27. $protocolType = 'http';
  28. $baseNsUri = substr($baseNsUri, 6);
  29. }
  30. }
  31. if (!empty($baseNsUri)) {
  32. if (false !== ($pos = strpos($baseNsUri, '/'))) {
  33. $baseNsUri = substr($baseNsUri, 0, $pos);
  34. }
  35. }
  36. return "{$protocolType}://{$baseNsUri}/wfs";
  37. }
  38. public function isAllowedFeatureType($nsPrefix, $type) {
  39. if ('p5_' != substr($nsPrefix, 0, 3)) return false;
  40. if ('p5_default_db' == $nsPrefix) {
  41. $typeName = "p5_default_db:{$type}";
  42. try {
  43. $acl = $this->getAclFromTypeName($typeName);
  44. } catch (Exception $e) {
  45. return false;
  46. }
  47. if ($acl) {
  48. return true;
  49. }
  50. }
  51. return false;
  52. }
  53. /**
  54. * @param string $typeName - 'p5_default_db:TEST_PERMS'
  55. */
  56. public function getAclFromTypeName($typeName) {
  57. $typeEx = explode(':', $typeName);
  58. if (2 != count($typeEx)) {
  59. throw new Api_WfsException("Could not get acl for '{$typeName}' - syntax error");
  60. }
  61. if ('p5_' != substr($typeEx[0], 0, 3)) {
  62. throw new Api_WfsException("Could not get acl for '{$typeName}' - prefix error");
  63. }
  64. $sourceName = substr($typeEx[0], 3);
  65. $objName = $typeEx[1];
  66. $acl = $this->_usrAcl->getObjectAcl($sourceName, $objName);
  67. if (!$acl) {
  68. throw new Api_WfsException("Could not get acl for '{$typeName}'");
  69. }
  70. $forceTblAclInit = 0;//('1' == V::get('_force', '', $_GET));
  71. $acl->init($forceTblAclInit);
  72. return $acl;
  73. }
  74. public function _getCapabilities($wfsServerUrl, $serviceTitle, $serviceDescription) {
  75. echo '<?xml version="1.0" encoding="UTF-8"?>';
  76. ?>
  77. <WFS_Capabilities
  78. xmlns="http://www.opengis.net/wfs"
  79. xmlns:ogc="http://www.opengis.net/ogc"
  80. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  81. <?php echo $this->_getXmlNamespaceList(); ?>
  82. <?php echo $this->_getXmlSchemaLocation(); ?>
  83. version="1.0.0">
  84. <Service>
  85. <Name>WFS</Name>
  86. <Title><?php echo $serviceTitle; ?></Title>
  87. <Abstract><?php echo $serviceDescription; ?></Abstract>
  88. <Keywords>WFS, WMS</Keywords>
  89. <OnlineResource><?php echo $wfsServerUrl; ?></OnlineResource>
  90. <Fees>NONE</Fees>
  91. <AccessConstraints>NONE</AccessConstraints>
  92. </Service>
  93. <Capability>
  94. <Request>
  95. <?php $this->_printGetCapabilitiesXml($wfsServerUrl); ?>
  96. <?php $this->_printDescribeFeatureTypeXml($wfsServerUrl); ?>
  97. <?php $this->_printGetFeatureXml($wfsServerUrl); ?>
  98. <?php $this->_printTransactionXml($wfsServerUrl); ?>
  99. <?php $this->_printLockFeatureXml($wfsServerUrl); ?>
  100. <?php $this->_printGetFeatureWithLockXml($wfsServerUrl); ?>
  101. </Request>
  102. </Capability>
  103. <FeatureTypeList>
  104. <Operations>
  105. <Query />
  106. <Insert />
  107. <Update />
  108. <Delete />
  109. <Lock />
  110. </Operations>
  111. <?php $this->_printFeatureTypeListXml(); ?>
  112. </FeatureTypeList>
  113. <ogc:Filter_Capabilities>
  114. <ogc:Spatial_Capabilities>
  115. <ogc:Spatial_Operators>
  116. <ogc:Disjoint />
  117. <ogc:Equals />
  118. <ogc:DWithin />
  119. <ogc:Beyond />
  120. <ogc:Intersect />
  121. <ogc:Touches />
  122. <ogc:Crosses />
  123. <ogc:Within />
  124. <ogc:Contains />
  125. <ogc:Overlaps />
  126. <ogc:BBOX />
  127. </ogc:Spatial_Operators>
  128. </ogc:Spatial_Capabilities>
  129. <ogc:Scalar_Capabilities>
  130. <ogc:Logical_Operators />
  131. <ogc:Comparison_Operators>
  132. <ogc:Simple_Comparisons />
  133. <ogc:Between />
  134. <ogc:Like />
  135. <ogc:NullCheck />
  136. </ogc:Comparison_Operators>
  137. <ogc:Arithmetic_Operators>
  138. <ogc:Simple_Arithmetic />
  139. <ogc:Functions>
  140. <ogc:Function_Names>
  141. <ogc:Function_Name nArgs="1">abs</ogc:Function_Name>
  142. <ogc:Function_Name nArgs="1">abs_2</ogc:Function_Name>
  143. <ogc:Function_Name nArgs="1">abs_3</ogc:Function_Name>
  144. <ogc:Function_Name nArgs="1">abs_4</ogc:Function_Name>
  145. <ogc:Function_Name nArgs="1">acos</ogc:Function_Name>
  146. <ogc:Function_Name nArgs="2">AddCoverages</ogc:Function_Name>
  147. <ogc:Function_Name nArgs="4">Aggregate</ogc:Function_Name>
  148. <ogc:Function_Name nArgs="1">Area</ogc:Function_Name>
  149. <ogc:Function_Name nArgs="1">area2</ogc:Function_Name>
  150. <ogc:Function_Name nArgs="3">AreaGrid</ogc:Function_Name>
  151. <ogc:Function_Name nArgs="1">asin</ogc:Function_Name>
  152. <ogc:Function_Name nArgs="1">atan</ogc:Function_Name>
  153. <ogc:Function_Name nArgs="2">atan2</ogc:Function_Name>
  154. <ogc:Function_Name nArgs="14">BarnesSurface</ogc:Function_Name>
  155. <ogc:Function_Name nArgs="3">between</ogc:Function_Name>
  156. <ogc:Function_Name nArgs="1">boundary</ogc:Function_Name>
  157. <ogc:Function_Name nArgs="1">boundaryDimension</ogc:Function_Name>
  158. <ogc:Function_Name nArgs="1">Bounds</ogc:Function_Name>
  159. <ogc:Function_Name nArgs="2">buffer</ogc:Function_Name>
  160. <ogc:Function_Name nArgs="3">BufferFeatureCollection</ogc:Function_Name>
  161. <ogc:Function_Name nArgs="3">bufferWithSegments</ogc:Function_Name>
  162. <ogc:Function_Name nArgs="7">Categorize</ogc:Function_Name>
  163. <ogc:Function_Name nArgs="1">ceil</ogc:Function_Name>
  164. <ogc:Function_Name nArgs="1">Centroid</ogc:Function_Name>
  165. <ogc:Function_Name nArgs="2">classify</ogc:Function_Name>
  166. <ogc:Function_Name nArgs="2">Clip</ogc:Function_Name>
  167. <ogc:Function_Name nArgs="1">CollectGeometries</ogc:Function_Name>
  168. <ogc:Function_Name nArgs="1">Collection_Average</ogc:Function_Name>
  169. <ogc:Function_Name nArgs="1">Collection_Bounds</ogc:Function_Name>
  170. <ogc:Function_Name nArgs="0">Collection_Count</ogc:Function_Name>
  171. <ogc:Function_Name nArgs="1">Collection_Max</ogc:Function_Name>
  172. <ogc:Function_Name nArgs="1">Collection_Median</ogc:Function_Name>
  173. <ogc:Function_Name nArgs="1">Collection_Min</ogc:Function_Name>
  174. <ogc:Function_Name nArgs="1">Collection_Sum</ogc:Function_Name>
  175. <ogc:Function_Name nArgs="1">Collection_Unique</ogc:Function_Name>
  176. <ogc:Function_Name nArgs="1">Concatenate</ogc:Function_Name>
  177. <ogc:Function_Name nArgs="2">contains</ogc:Function_Name>
  178. <ogc:Function_Name nArgs="7">Contour</ogc:Function_Name>
  179. <ogc:Function_Name nArgs="2">convert</ogc:Function_Name>
  180. <ogc:Function_Name nArgs="1">convexHull</ogc:Function_Name>
  181. <ogc:Function_Name nArgs="1">cos</ogc:Function_Name>
  182. <ogc:Function_Name nArgs="1">Count</ogc:Function_Name>
  183. <ogc:Function_Name nArgs="2">CropCoverage</ogc:Function_Name>
  184. <ogc:Function_Name nArgs="2">crosses</ogc:Function_Name>
  185. <ogc:Function_Name nArgs="2">dateFormat</ogc:Function_Name>
  186. <ogc:Function_Name nArgs="2">dateParse</ogc:Function_Name>
  187. <ogc:Function_Name nArgs="2">difference</ogc:Function_Name>
  188. <ogc:Function_Name nArgs="1">dimension</ogc:Function_Name>
  189. <ogc:Function_Name nArgs="2">disjoint</ogc:Function_Name>
  190. <ogc:Function_Name nArgs="2">disjoint3D</ogc:Function_Name>
  191. <ogc:Function_Name nArgs="2">distance</ogc:Function_Name>
  192. <ogc:Function_Name nArgs="2">distance3D</ogc:Function_Name>
  193. <ogc:Function_Name nArgs="1">double2bool</ogc:Function_Name>
  194. <ogc:Function_Name nArgs="1">endAngle</ogc:Function_Name>
  195. <ogc:Function_Name nArgs="1">endPoint</ogc:Function_Name>
  196. <ogc:Function_Name nArgs="1">env</ogc:Function_Name>
  197. <ogc:Function_Name nArgs="1">envelope</ogc:Function_Name>
  198. <ogc:Function_Name nArgs="2">EqualInterval</ogc:Function_Name>
  199. <ogc:Function_Name nArgs="2">equalsExact</ogc:Function_Name>
  200. <ogc:Function_Name nArgs="3">equalsExactTolerance</ogc:Function_Name>
  201. <ogc:Function_Name nArgs="2">equalTo</ogc:Function_Name>
  202. <ogc:Function_Name nArgs="1">exp</ogc:Function_Name>
  203. <ogc:Function_Name nArgs="1">exteriorRing</ogc:Function_Name>
  204. <ogc:Function_Name nArgs="3">Feature</ogc:Function_Name>
  205. <ogc:Function_Name nArgs="1">floor</ogc:Function_Name>
  206. <ogc:Function_Name nArgs="1">geometryType</ogc:Function_Name>
  207. <ogc:Function_Name nArgs="1">geomFromWKT</ogc:Function_Name>
  208. <ogc:Function_Name nArgs="1">geomLength</ogc:Function_Name>
  209. <ogc:Function_Name nArgs="2">getGeometryN</ogc:Function_Name>
  210. <ogc:Function_Name nArgs="1">getX</ogc:Function_Name>
  211. <ogc:Function_Name nArgs="1">getY</ogc:Function_Name>
  212. <ogc:Function_Name nArgs="1">getz</ogc:Function_Name>
  213. <ogc:Function_Name nArgs="2">greaterEqualThan</ogc:Function_Name>
  214. <ogc:Function_Name nArgs="2">greaterThan</ogc:Function_Name>
  215. <ogc:Function_Name nArgs="5">Grid</ogc:Function_Name>
  216. <ogc:Function_Name nArgs="7">Heatmap</ogc:Function_Name>
  217. <ogc:Function_Name nArgs="0">id</ogc:Function_Name>
  218. <ogc:Function_Name nArgs="2">IEEEremainder</ogc:Function_Name>
  219. <ogc:Function_Name nArgs="3">if_then_else</ogc:Function_Name>
  220. <ogc:Function_Name nArgs="11">in10</ogc:Function_Name>
  221. <ogc:Function_Name nArgs="3">in2</ogc:Function_Name>
  222. <ogc:Function_Name nArgs="4">in3</ogc:Function_Name>
  223. <ogc:Function_Name nArgs="5">in4</ogc:Function_Name>
  224. <ogc:Function_Name nArgs="6">in5</ogc:Function_Name>
  225. <ogc:Function_Name nArgs="7">in6</ogc:Function_Name>
  226. <ogc:Function_Name nArgs="8">in7</ogc:Function_Name>
  227. <ogc:Function_Name nArgs="9">in8</ogc:Function_Name>
  228. <ogc:Function_Name nArgs="10">in9</ogc:Function_Name>
  229. <ogc:Function_Name nArgs="2">InclusionFeatureCollection</ogc:Function_Name>
  230. <ogc:Function_Name nArgs="1">int2bbool</ogc:Function_Name>
  231. <ogc:Function_Name nArgs="1">int2ddouble</ogc:Function_Name>
  232. <ogc:Function_Name nArgs="1">interiorPoint</ogc:Function_Name>
  233. <ogc:Function_Name nArgs="2">interiorRingN</ogc:Function_Name>
  234. <ogc:Function_Name nArgs="3">Interpolate</ogc:Function_Name>
  235. <ogc:Function_Name nArgs="2">intersection</ogc:Function_Name>
  236. <ogc:Function_Name nArgs="7">IntersectionFeatureCollection</ogc:Function_Name>
  237. <ogc:Function_Name nArgs="2">intersects</ogc:Function_Name>
  238. <ogc:Function_Name nArgs="2">intersects3D</ogc:Function_Name>
  239. <ogc:Function_Name nArgs="1">isClosed</ogc:Function_Name>
  240. <ogc:Function_Name nArgs="0">isCoverage</ogc:Function_Name>
  241. <ogc:Function_Name nArgs="1">isEmpty</ogc:Function_Name>
  242. <ogc:Function_Name nArgs="2">isLike</ogc:Function_Name>
  243. <ogc:Function_Name nArgs="1">isNull</ogc:Function_Name>
  244. <ogc:Function_Name nArgs="2">isometric</ogc:Function_Name>
  245. <ogc:Function_Name nArgs="1">isRing</ogc:Function_Name>
  246. <ogc:Function_Name nArgs="1">isSimple</ogc:Function_Name>
  247. <ogc:Function_Name nArgs="1">isValid</ogc:Function_Name>
  248. <ogc:Function_Name nArgs="3">isWithinDistance</ogc:Function_Name>
  249. <ogc:Function_Name nArgs="3">isWithinDistance3D</ogc:Function_Name>
  250. <ogc:Function_Name nArgs="2">Jenks</ogc:Function_Name>
  251. <ogc:Function_Name nArgs="1">length</ogc:Function_Name>
  252. <ogc:Function_Name nArgs="2">lessEqualThan</ogc:Function_Name>
  253. <ogc:Function_Name nArgs="2">lessThan</ogc:Function_Name>
  254. <ogc:Function_Name nArgs="1">list</ogc:Function_Name>
  255. <ogc:Function_Name nArgs="1">log</ogc:Function_Name>
  256. <ogc:Function_Name nArgs="4">LRSGeocode</ogc:Function_Name>
  257. <ogc:Function_Name nArgs="5">LRSMeasure</ogc:Function_Name>
  258. <ogc:Function_Name nArgs="5">LRSSegment</ogc:Function_Name>
  259. <ogc:Function_Name nArgs="2">max</ogc:Function_Name>
  260. <ogc:Function_Name nArgs="2">max_2</ogc:Function_Name>
  261. <ogc:Function_Name nArgs="2">max_3</ogc:Function_Name>
  262. <ogc:Function_Name nArgs="2">max_4</ogc:Function_Name>
  263. <ogc:Function_Name nArgs="2">min</ogc:Function_Name>
  264. <ogc:Function_Name nArgs="2">min_2</ogc:Function_Name>
  265. <ogc:Function_Name nArgs="2">min_3</ogc:Function_Name>
  266. <ogc:Function_Name nArgs="2">min_4</ogc:Function_Name>
  267. <ogc:Function_Name nArgs="1">mincircle</ogc:Function_Name>
  268. <ogc:Function_Name nArgs="1">minimumdiameter</ogc:Function_Name>
  269. <ogc:Function_Name nArgs="1">minrectangle</ogc:Function_Name>
  270. <ogc:Function_Name nArgs="2">modulo</ogc:Function_Name>
  271. <ogc:Function_Name nArgs="2">MultiplyCoverages</ogc:Function_Name>
  272. <ogc:Function_Name nArgs="3">Nearest</ogc:Function_Name>
  273. <ogc:Function_Name nArgs="1">not</ogc:Function_Name>
  274. <ogc:Function_Name nArgs="2">notEqualTo</ogc:Function_Name>
  275. <ogc:Function_Name nArgs="2">numberFormat</ogc:Function_Name>
  276. <ogc:Function_Name nArgs="5">numberFormat2</ogc:Function_Name>
  277. <ogc:Function_Name nArgs="1">numGeometries</ogc:Function_Name>
  278. <ogc:Function_Name nArgs="1">numInteriorRing</ogc:Function_Name>
  279. <ogc:Function_Name nArgs="1">numPoints</ogc:Function_Name>
  280. <ogc:Function_Name nArgs="1">octagonalenvelope</ogc:Function_Name>
  281. <ogc:Function_Name nArgs="3">offset</ogc:Function_Name>
  282. <ogc:Function_Name nArgs="2">overlaps</ogc:Function_Name>
  283. <ogc:Function_Name nArgs="1">parameter</ogc:Function_Name>
  284. <ogc:Function_Name nArgs="1">parseBoolean</ogc:Function_Name>
  285. <ogc:Function_Name nArgs="1">parseDouble</ogc:Function_Name>
  286. <ogc:Function_Name nArgs="1">parseInt</ogc:Function_Name>
  287. <ogc:Function_Name nArgs="1">parseLong</ogc:Function_Name>
  288. <ogc:Function_Name nArgs="0">pi</ogc:Function_Name>
  289. <ogc:Function_Name nArgs="4">PointBuffers</ogc:Function_Name>
  290. <ogc:Function_Name nArgs="2">pointN</ogc:Function_Name>
  291. <ogc:Function_Name nArgs="7">PointStacker</ogc:Function_Name>
  292. <ogc:Function_Name nArgs="6">PolygonExtraction</ogc:Function_Name>
  293. <ogc:Function_Name nArgs="2">pow</ogc:Function_Name>
  294. <ogc:Function_Name nArgs="1">property</ogc:Function_Name>
  295. <ogc:Function_Name nArgs="1">PropertyExists</ogc:Function_Name>
  296. <ogc:Function_Name nArgs="2">Quantile</ogc:Function_Name>
  297. <ogc:Function_Name nArgs="3">Query</ogc:Function_Name>
  298. <ogc:Function_Name nArgs="0">random</ogc:Function_Name>
  299. <ogc:Function_Name nArgs="5">RangeLookup</ogc:Function_Name>
  300. <ogc:Function_Name nArgs="1">RasterAsPointCollection</ogc:Function_Name>
  301. <ogc:Function_Name nArgs="4">RasterZonalStatistics</ogc:Function_Name>
  302. <ogc:Function_Name nArgs="5">Recode</ogc:Function_Name>
  303. <ogc:Function_Name nArgs="2">RectangularClip</ogc:Function_Name>
  304. <ogc:Function_Name nArgs="2">relate</ogc:Function_Name>
  305. <ogc:Function_Name nArgs="3">relatePattern</ogc:Function_Name>
  306. <ogc:Function_Name nArgs="3">Reproject</ogc:Function_Name>
  307. <ogc:Function_Name nArgs="1">rint</ogc:Function_Name>
  308. <ogc:Function_Name nArgs="1">round</ogc:Function_Name>
  309. <ogc:Function_Name nArgs="1">round_2</ogc:Function_Name>
  310. <ogc:Function_Name nArgs="1">roundDouble</ogc:Function_Name>
  311. <ogc:Function_Name nArgs="6">ScaleCoverage</ogc:Function_Name>
  312. <ogc:Function_Name nArgs="4">sdo_nn</ogc:Function_Name>
  313. <ogc:Function_Name nArgs="2">setCRS</ogc:Function_Name>
  314. <ogc:Function_Name nArgs="3">Simplify</ogc:Function_Name>
  315. <ogc:Function_Name nArgs="1">sin</ogc:Function_Name>
  316. <ogc:Function_Name nArgs="3">Snap</ogc:Function_Name>
  317. <ogc:Function_Name nArgs="1">sqrt</ogc:Function_Name>
  318. <ogc:Function_Name nArgs="2">StandardDeviation</ogc:Function_Name>
  319. <ogc:Function_Name nArgs="1">startAngle</ogc:Function_Name>
  320. <ogc:Function_Name nArgs="1">startPoint</ogc:Function_Name>
  321. <ogc:Function_Name nArgs="1">strCapitalize</ogc:Function_Name>
  322. <ogc:Function_Name nArgs="2">strConcat</ogc:Function_Name>
  323. <ogc:Function_Name nArgs="2">strEndsWith</ogc:Function_Name>
  324. <ogc:Function_Name nArgs="2">strEqualsIgnoreCase</ogc:Function_Name>
  325. <ogc:Function_Name nArgs="2">strIndexOf</ogc:Function_Name>
  326. <ogc:Function_Name nArgs="2">strLastIndexOf</ogc:Function_Name>
  327. <ogc:Function_Name nArgs="1">strLength</ogc:Function_Name>
  328. <ogc:Function_Name nArgs="2">strMatches</ogc:Function_Name>
  329. <ogc:Function_Name nArgs="3">strPosition</ogc:Function_Name>
  330. <ogc:Function_Name nArgs="4">strReplace</ogc:Function_Name>
  331. <ogc:Function_Name nArgs="2">strStartsWith</ogc:Function_Name>
  332. <ogc:Function_Name nArgs="3">strSubstring</ogc:Function_Name>
  333. <ogc:Function_Name nArgs="2">strSubstringStart</ogc:Function_Name>
  334. <ogc:Function_Name nArgs="1">strToLowerCase</ogc:Function_Name>
  335. <ogc:Function_Name nArgs="1">strToUpperCase</ogc:Function_Name>
  336. <ogc:Function_Name nArgs="1">strTrim</ogc:Function_Name>
  337. <ogc:Function_Name nArgs="3">strTrim2</ogc:Function_Name>
  338. <ogc:Function_Name nArgs="2">StyleCoverage</ogc:Function_Name>
  339. <ogc:Function_Name nArgs="2">symDifference</ogc:Function_Name>
  340. <ogc:Function_Name nArgs="1">tan</ogc:Function_Name>
  341. <ogc:Function_Name nArgs="1">toDegrees</ogc:Function_Name>
  342. <ogc:Function_Name nArgs="1">toRadians</ogc:Function_Name>
  343. <ogc:Function_Name nArgs="2">touches</ogc:Function_Name>
  344. <ogc:Function_Name nArgs="1">toWKT</ogc:Function_Name>
  345. <ogc:Function_Name nArgs="2">Transform</ogc:Function_Name>
  346. <ogc:Function_Name nArgs="2">union</ogc:Function_Name>
  347. <ogc:Function_Name nArgs="2">UnionFeatureCollection</ogc:Function_Name>
  348. <ogc:Function_Name nArgs="2">Unique</ogc:Function_Name>
  349. <ogc:Function_Name nArgs="2">UniqueInterval</ogc:Function_Name>
  350. <ogc:Function_Name nArgs="6">VectorToRaster</ogc:Function_Name>
  351. <ogc:Function_Name nArgs="3">VectorZonalStatistics</ogc:Function_Name>
  352. <ogc:Function_Name nArgs="1">vertices</ogc:Function_Name>
  353. <ogc:Function_Name nArgs="2">within</ogc:Function_Name>
  354. </ogc:Function_Names>
  355. </ogc:Functions>
  356. </ogc:Arithmetic_Operators>
  357. </ogc:Scalar_Capabilities>
  358. </ogc:Filter_Capabilities>
  359. </WFS_Capabilities>
  360. <?php
  361. }
  362. public function _getXmlSchemaLocation() {
  363. $schemaLocations = array();
  364. //$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
  365. return (!empty($schemaLocations))? 'xsi:schemaLocation="' . implode(' ', $schemaLocations) . '"' : '';
  366. }
  367. public function _getXmlNamespaceList() {
  368. $baseNsUri = $this->getBaseNamespaceUri();
  369. $ns = 'p5_default_db';
  370. $uri = "{$baseNsUri}/default_db";
  371. return 'xmlns:' . $ns . '="' . $uri . '"';
  372. }
  373. public function _getXmlNamespaceList__OLD() {
  374. $baseNsUri = $this->getBaseNamespaceUri();
  375. $namespaceList = $this->_getSourceNsList();
  376. $namespaceListOut = array();
  377. foreach ($namespaceList as $nsObj) {
  378. $ns = "p5_{$nsObj[0]}_{$nsObj[1]}";
  379. $uri = "{$baseNsUri}/{$nsObj[0]}/{$nsObj[1]}";
  380. $namespaceListOut[] = 'xmlns:' . $ns . '="' . $uri . '"';
  381. }
  382. return implode(' ', $namespaceListOut);
  383. }
  384. public function _getSourceNsList() {
  385. $usrObjList = array();
  386. $tblsAcl = $this->_usrAcl->getTablesAcl();
  387. foreach ($tblsAcl as $tblAcl) {
  388. $dataSourceName = 'default_db';// TODO: getSourceName
  389. $tblName = $tblAcl->getName();
  390. $usrObjList[] = array($dataSourceName, $tblName);
  391. }
  392. return $usrObjList;
  393. }
  394. public function _printGetCapabilitiesXml($wfsServerUrl) {
  395. ?>
  396. <GetCapabilities>
  397. <DCPType>
  398. <HTTP>
  399. <Get onlineResource="<?php echo $wfsServerUrl; ?>?REQUEST=GetCapabilities" />
  400. </HTTP>
  401. </DCPType>
  402. <DCPType>
  403. <HTTP>
  404. <Post onlineResource="<?php echo $wfsServerUrl; ?>" />
  405. </HTTP>
  406. </DCPType>
  407. </GetCapabilities>
  408. <?php
  409. }
  410. public function _printDescribeFeatureTypeXml($wfsServerUrl) {
  411. ?>
  412. <DescribeFeatureType>
  413. <SchemaDescriptionLanguage>
  414. <XMLSCHEMA />
  415. </SchemaDescriptionLanguage>
  416. <DCPType>
  417. <HTTP>
  418. <Get onlineResource="<?php echo $wfsServerUrl; ?>?REQUEST=DescribeFeatureType" />
  419. </HTTP>
  420. </DCPType>
  421. <DCPType>
  422. <HTTP>
  423. <Post onlineResource="<?php echo $wfsServerUrl; ?>" />
  424. </HTTP>
  425. </DCPType>
  426. </DescribeFeatureType>
  427. <?php
  428. }
  429. public function _printGetFeatureXml($wfsServerUrl) {
  430. ?>
  431. <GetFeature>
  432. <ResultFormat>
  433. <WFSKMLOutputFormat />
  434. <GML2 />
  435. <GML3 />
  436. <SHAPE-ZIP />
  437. <CSV />
  438. <JSON />
  439. </ResultFormat>
  440. <DCPType>
  441. <HTTP>
  442. <Get onlineResource="<?php echo $wfsServerUrl; ?>?REQUEST=GetFeature" />
  443. </HTTP>
  444. </DCPType>
  445. <DCPType>
  446. <HTTP>
  447. <Post onlineResource="<?php echo $wfsServerUrl; ?>" />
  448. </HTTP>
  449. </DCPType>
  450. </GetFeature>
  451. <?php
  452. }
  453. public function _printTransactionXml($wfsServerUrl) {
  454. ?>
  455. <Transaction>
  456. <DCPType>
  457. <HTTP>
  458. <Get onlineResource="<?php echo $wfsServerUrl; ?>?request=Transaction" />
  459. </HTTP>
  460. </DCPType>
  461. <DCPType>
  462. <HTTP>
  463. <Post onlineResource="<?php echo $wfsServerUrl; ?>" />
  464. </HTTP>
  465. </DCPType>
  466. </Transaction>
  467. <?php
  468. }
  469. public function _printLockFeatureXml($wfsServerUrl) {
  470. ?>
  471. <LockFeature>
  472. <DCPType>
  473. <HTTP>
  474. <Get onlineResource="<?php echo $wfsServerUrl; ?>?REQUEST=LockFeature" />
  475. </HTTP>
  476. </DCPType>
  477. <DCPType>
  478. <HTTP>
  479. <Post onlineResource="<?php echo $wfsServerUrl; ?>" />
  480. </HTTP>
  481. </DCPType>
  482. </LockFeature>
  483. <?php
  484. }
  485. public function _printGetFeatureWithLockXml($wfsServerUrl) {
  486. ?>
  487. <GetFeatureWithLock>
  488. <ResultFormat>
  489. <GML2 />
  490. </ResultFormat>
  491. <DCPType>
  492. <HTTP>
  493. <Get onlineResource="<?php echo $wfsServerUrl; ?>?REQUEST=GetFeatureWithLock" />
  494. </HTTP>
  495. </DCPType>
  496. <DCPType>
  497. <HTTP>
  498. <Post onlineResource="<?php echo $wfsServerUrl; ?>" />
  499. </HTTP>
  500. </DCPType>
  501. </GetFeatureWithLock>
  502. <?php
  503. }
  504. public function _printFeatureTypeListXml() {
  505. $featureTypes = array();
  506. $tblsAcl = $this->_getTableAclList();
  507. foreach ($tblsAcl as $tblAcl) {
  508. $dataSourceName = 'default_db';// TODO: getSourceName
  509. $tblName = $tblAcl->getName();
  510. $usrObjList[] = array($dataSourceName, $tblName);
  511. $featureType = array();
  512. $featureType['ns'] = "p5_{$dataSourceName}";
  513. $featureType['Title'] = $tblAcl->getRawLabel();
  514. $featureType['Abstract'] = $tblAcl->getRawLabel();
  515. $featureType['Keywords'] = array();
  516. $featureType['Keywords'][] = $tblAcl->getID();
  517. $featureType['Keywords'][] = $tblName;
  518. $featureType['Keywords'][] = $tblAcl->getRawLabel();
  519. $featureType['Keywords'] = implode(", ", $featureType['Keywords']);
  520. $featureType['SRS'] = "EPSG:4326";
  521. $featureType['LatLongBoundingBox'] = array();// TODO: feature LatLongBoundingBox
  522. $featureType['LatLongBoundingBox']['minx'] = "8.12328509871721";
  523. $featureType['LatLongBoundingBox']['miny'] = "38.8575126897477";
  524. $featureType['LatLongBoundingBox']['maxx'] = "9.838674658246807";
  525. $featureType['LatLongBoundingBox']['maxy'] = "41.31378404137082";
  526. $featureTypes[$tblName] = $featureType;
  527. }
  528. /*
  529. <FeatureType>
  530. <Name>ppr06:AMBITIPAESAGGIO</Name>
  531. <Title>AMBITIPAESAGGIO</Title>
  532. <Abstract />
  533. <Keywords>features, AMBITIPAESAGGIO</Keywords>
  534. <SRS>EPSG:4326</SRS>
  535. <LatLongBoundingBox minx="8.12328509871721" miny="38.8575126897477" maxx="9.838674658246807" maxy="41.31378404137082" />
  536. </FeatureType>
  537. */
  538. $featureTypesXml = '';
  539. foreach ($featureTypes as $tblName => $feature) {
  540. $featureTypesXml .= " " . '<FeatureType>' . "\n";
  541. $featureTypesXml .= " " . '<Name>' . "{$feature['ns']}:{$tblName}" . '</Name>' . "\n";
  542. $featureTypesXml .= " " . '<Title>' . "{$feature['Title']}" . '</Title>' . "\n";
  543. if (!empty($feature['Abstract'])) {
  544. $featureTypesXml .= " " . '<Abstract>' . "{$feature['Abstract']}" . '</Abstract>' . "\n";
  545. } else {
  546. $featureTypesXml .= " " . '<Abstract/>' . "\n";
  547. }
  548. if (!empty($feature['Keywords'])) {
  549. $featureTypesXml .= " " . '<Keywords>' . "{$feature['Keywords']}" . '</Keywords>' . "\n";
  550. } else {
  551. $featureTypesXml .= " " . '<Keywords/>' . "\n";
  552. }
  553. $featureTypesXml .= " " . '<SRS>' . "{$feature['SRS']}" . '</SRS>' . "\n";
  554. if (!empty($feature['LatLongBoundingBox'])) {
  555. $latLongBoundingBoxOut = array();
  556. $latLongBoundingBoxOut[] = 'minx="' . $feature['LatLongBoundingBox']['minx'] . '"';
  557. $latLongBoundingBoxOut[] = 'miny="' . $feature['LatLongBoundingBox']['miny'] . '"';
  558. $latLongBoundingBoxOut[] = 'maxx="' . $feature['LatLongBoundingBox']['maxx'] . '"';
  559. $latLongBoundingBoxOut[] = 'maxy="' . $feature['LatLongBoundingBox']['maxy'] . '"';
  560. $featureTypesXml .= " " . '<LatLongBoundingBox ' . implode(' ', $latLongBoundingBoxOut) . ' />' . "\n";
  561. }
  562. $featureTypesXml .= " " . '</FeatureType>' . "\n";
  563. }
  564. echo $featureTypesXml;
  565. }
  566. public function _parseTransactionXmlStruct($requestXml, $requestXmlTags) {
  567. $DBG = (V::get('DBG_XML', '', $_GET) > 0);// TODO: Profiler
  568. $rootTagName = V::get('tag', '', $requestXmlTags[0]);
  569. if ('Transaction' != $rootTagName) {
  570. throw new Exception("Parse Request xml error #" . __LINE__);
  571. }
  572. // 1. convert request: wfs.transaction.convert-wfs-request.xsl
  573. // 2. validate converted request: wfs.transaction-converted-request.xsd
  574. // 3. execute request in data source
  575. // ? acl check?
  576. $usedSourceNsList = array();
  577. $sourceNsList = $this->_getSourceNsList();
  578. //$sourceNsList = array();
  579. //foreach ($requestXmlTags[0]['attributes'] as $attrName => $attrValue) {
  580. // if ('xmlns:p5_' == substr($attrName, 0, 9)) {
  581. // $sourceNsList[substr($attrName, 6)] = $attrValue;
  582. // }
  583. //}
  584. {// get used typeNames
  585. $usedTypeNames = array();
  586. // <Update xmlns="http://www.opengis.net/wfs" typeName="p5_default_db:TEST_PERMS">
  587. foreach ($requestXmlTags as $tag) {
  588. if ('Update' == $tag['tag'] && 'open' == $tag['type']) {
  589. $typeName = $tag['attributes']['typeName'];
  590. foreach ($sourceNsList as $nsInd => $ns) {
  591. if ("p5_{$ns[0]}:{$ns[1]}" == $typeName) {
  592. $usedSourceNsList[$nsInd] = $ns;
  593. }
  594. }
  595. }
  596. }
  597. // TODO: check: <Transaction xmlns:p5_default_db="https://biuro.biall-net.pl/wfs/default_db/TEST_PERMS"
  598. // <Insert xmlns="http://www.opengis.net/wfs">
  599. // <TEST_PERMS xmlns="https://biuro.biall-net.pl/wfs/default_db/TEST_PERMS">
  600. $lastTagName = '';
  601. foreach ($requestXmlTags as $tag) {
  602. if ('Insert' == $lastTagName) {
  603. $typeName = $tag['tag'];
  604. foreach ($sourceNsList as $nsInd => $ns) {
  605. if ("{$ns[1]}" == $typeName) {
  606. $usedSourceNsList[$nsInd] = $ns;
  607. }
  608. }
  609. }
  610. $lastTagName = $tag['tag'];
  611. }
  612. }
  613. if (empty($usedSourceNsList)) {
  614. throw new Exception("Parse Request xml error #" . __LINE__ . ": not implemented");
  615. }
  616. $convertedTransaction = $this->_convertTransactionXml($requestXml, $usedSourceNsList);
  617. if($DBG){echo '$convertedTransaction:';print_r($convertedTransaction);echo "\n";}
  618. if (empty($convertedTransaction)) {
  619. throw new Exception("Parse Request xml error #" . __LINE__ . ": convert Transaction");
  620. }
  621. //echo "\ntags[0]:\n" . json_encode($requestXmlTags[0]) . "\n";
  622. //echo "\nconvertedTransaction:\n" . $convertedTransaction . "\n";
  623. //echo "\nsourceNsList:\n" . json_encode($sourceNsList) . "\n";
  624. if (!$this->_validateConvertedTransactionXml($convertedTransaction, $usedSourceNsList)) {
  625. throw new Exception("Parse Request xml error #" . __LINE__ . ": schema validation failed");
  626. }
  627. $parserXml = xml_parser_create();
  628. xml_parser_set_option($parserXml, XML_OPTION_CASE_FOLDING, 0);
  629. xml_parser_set_option($parserXml, XML_OPTION_SKIP_WHITE, 1);
  630. if (0 == xml_parse_into_struct($parserXml, $convertedTransaction, $tags)) {
  631. throw new Exception("Parse Request xml error #" . __LINE__ . ": parse converted transaction failed");
  632. }
  633. xml_parser_free($parserXml);
  634. if (empty($tags)) {
  635. throw new Exception("Parse Request xml error #" . __LINE__ . ": parse converted transaction returns empty structure");
  636. }
  637. // [{"tag":"Transaction","type":"open","level":1,"attributes":{"version":"1.0.0","service":"WFS"}},
  638. // {"tag":"Update","type":"open","level":2,"attributes":
  639. // {"typeName":"p5_default_db_13051:TEST_PERMS","featureId":"TEST_PERMS.25"}},
  640. // {"tag":"PARENT_ID","type":"complete","level":3,"value":"0"},
  641. // {"tag":"Update","type":"close","level":2},
  642. // {"tag":"Transaction","type":"close","level":1}]
  643. if($DBG){echo "\nTODO: tags L." . __LINE__ . ":\n"; print_r($tags);echo "\n";}
  644. $actionTag = null;
  645. $prevTagName = '';
  646. $theGeomField = 'the_geom';// TODO: get the geom field name from acl
  647. $itemPatchs = array();
  648. foreach ($tags as $tag) {
  649. switch (substr($tag['tag'], 0, 6)) {
  650. case 'Transa': continue; break;// Transaction
  651. case 'Update':
  652. case 'Insert':
  653. case 'Delete':
  654. case 'Native':
  655. if ('open' == $tag['type']) {
  656. $actionTag = array();
  657. $actionTag['tag'] = substr($tag['tag'], 0, 6);
  658. $actionTag['typeName'] = $tag['attributes']['typeName'];
  659. if ('Insert' == substr($tag['tag'], 0, 6)) {
  660. $actionTag['typeName'] = "p5_default_db:{$actionTag['typeName']}";
  661. }
  662. $featureEx = explode('.', $tag['attributes']['featureId'], 2);
  663. $actionTag['featureId'] = $featureEx[1];
  664. if ('Update' == substr($tag['tag'], 0, 6) && empty($actionTag['featureId'])) {
  665. throw new Api_WfsException("Syntax error - could not read feature id!");
  666. }
  667. $actionTag['itemPatch'] = array();
  668. } else {
  669. $itemPatchs[] = $actionTag;
  670. $actionTag = null;
  671. }
  672. break;
  673. default: {// fields
  674. if (3 != $tag['level'] && 'close' == $tag['type']) {
  675. $actionTag = null;
  676. }
  677. if (3 != $tag['level']) continue;
  678. if (empty($actionTag)) continue;
  679. if ('Update' == $actionTag['tag']) {
  680. if ($theGeomField == $tag['tag']) {
  681. $actionTag['itemPatch'][$tag['tag']] = $this->_typeConverter->convertGmlCoordinatesToWkt($tag['value']);
  682. } else {
  683. $actionTag['itemPatch'][$tag['tag']] = $tag['value'];
  684. }
  685. }
  686. else if ('Insert' == $actionTag['tag']) {
  687. if ($theGeomField == $tag['tag']) {
  688. $actionTag['itemPatch'][$tag['tag']] = $this->_typeConverter->convertGmlCoordinatesToWkt($tag['value']);
  689. } else {
  690. $actionTag['itemPatch'][$tag['tag']] = $tag['value'];
  691. }
  692. }
  693. }
  694. }
  695. if (empty($prevTagName)) $prevTagName = $tag['tag'];
  696. }
  697. if($DBG){echo "\nTODO: itemPatchs L." . __LINE__ . ":\n"; print_r($itemPatchs);echo "\n";}
  698. if($DBG){echo "\nTODO: _user_id L." . __LINE__ . ":\n"; print_r($this->_usrAcl->_user_id);echo "\n";}
  699. $changesList = array();
  700. if (!empty($itemPatchs)) {
  701. foreach ($itemPatchs as $itemPatchInfo) {
  702. if($DBG){echo "itemPatchInfo['itemPatch']:\n";print_r($itemPatchInfo['itemPatch']);}
  703. if (empty($itemPatchInfo['itemPatch'])) continue;
  704. $acl = $this->getAclFromTypeName($itemPatchInfo['typeName']);
  705. $itemPatch = $itemPatchInfo['itemPatch'];
  706. if ('Update' == $itemPatchInfo['tag']) {
  707. $itemPatch[$acl->getPrimaryKeyField()] = $itemPatchInfo['featureId'];
  708. }
  709. else if ('Insert' == $itemPatchInfo['tag']) {
  710. if (!empty($itemPatch[$acl->getPrimaryKeyField()])) {
  711. $itemPatchInfo['featureId'] = $itemPatch[$acl->getPrimaryKeyField()];
  712. }
  713. else {
  714. //throw new Exception("TODO: Insert #" . __LINE__ . ": Create new record");
  715. }
  716. }
  717. if($DBG){echo "TODO '" . ($itemPatchInfo['tag'])? 'Insert' : 'Update' . "' itemPatch:\n";print_r($itemPatch);}
  718. if ('Insert' == $itemPatchInfo['tag'] && empty($itemPatch[$acl->getPrimaryKeyField()])) {
  719. $newId = $acl->addItem($itemPatch);
  720. $changesList[$newId] = array('Status'=>(($newId > 0)? 'SUCCESS' : 'FAILED'), 'Message'=>"created {$newId}.");
  721. if ($newId > 0) {
  722. $changesList[$newId]['fid'] = $acl->getName() . '.' . $newId;
  723. }
  724. if($DBG){echo "created {$newId}.\n";}
  725. } else {
  726. $affected = $acl->updateItem($itemPatch);
  727. $changesList[$itemPatchInfo['featureId']] = array('Status'=>(($affected >= 0)? 'SUCCESS' : 'FAILED'), 'Message'=>"affected {$affected}.");
  728. if($DBG){echo "affected {$affected}.\n";}
  729. }
  730. }
  731. //throw new Exception("TODO: run query #" . __LINE__ . " \nitemPatchs:\n" . json_encode($itemPatchs) . " \ntags:\n" . json_encode($tags) . "\n");
  732. }
  733. else {
  734. throw new Exception("Parse Request xml error #" . __LINE__ . ": Nothing to change");
  735. }
  736. return $this->_transactionResponse($changesList);
  737. }
  738. public function _transactionResponse($changesList) {
  739. // <WFS_TransactionResponse>
  740. // <TransactionResult>
  741. // <Status> : SUCCESS / FAILED / PARTIAL
  742. // [<Locator]
  743. // [<Message]
  744. $messageTag = '';
  745. $statusTag = '';
  746. $statusIsFailed = false;
  747. $statusAll = null;
  748. $createdFetureId = '';
  749. foreach ($changesList as $featureId => $change) {
  750. if ('FAILED' == $change['Status']) {
  751. $statusIsFailed = true;
  752. }
  753. if ('SUCCESS' == $change['Status'] && !empty($change['fid'])) {
  754. $createdFetureId = $change['fid'];
  755. }
  756. if (!empty($change['Message'])) $messageTag .= "Feature '{$featureId}' {$change['Status']}: {$change['Message']}\n";
  757. }
  758. $statusTag = ($statusIsFailed)? 'FAILED' : 'SUCCESS';
  759. $statusTag = "<wfs:{$statusTag}/>";
  760. $messageTag = '';//"<wfs:Message>{$messageTag}</wfs:Message>";
  761. /* Example:
  762. <?xml version="1.0" encoding="UTF-8"?>
  763. <wfs:WFS_TransactionResponse version="1.0.0" xmlns:wfs="http://www.opengis.net/wfs"
  764. xmlns:ogc="http://www.opengis.net/ogc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  765. xsi:schemaLocation="http://www.opengis.net/wfs http://localhost:8080/geoserver/schemas/wfs/1.0.0/WFS-transaction.xsd">
  766. <wfs:InsertResult>
  767. <ogc:FeatureId fid="archsites.26" />
  768. </wfs:InsertResult>
  769. <wfs:TransactionResult handle="Updating Signature rock label">
  770. <wfs:Status>
  771. <wfs:SUCCESS />
  772. </wfs:Status>
  773. </wfs:TransactionResult>
  774. </wfs:WFS_TransactionResponse>*/
  775. // TODO: build xml by DOMDocument
  776. // TODO: xsi:schemaLocation="http://www.opengis.net/wfs http://localhost:8080/geoserver/schemas/wfs/1.0.0/WFS-transaction.xsd"
  777. $wfsInsertResult = '';
  778. if (!empty($createdFetureId)) {
  779. $wfsInsertResult = <<<EOF
  780. <wfs:InsertResult>
  781. <ogc:FeatureId fid="{$createdFetureId}" xmlns:ogc="http://www.opengis.net/ogc"/>
  782. </wfs:InsertResult>
  783. EOF;
  784. }
  785. $tranRes = <<<EOF
  786. <wfs:WFS_TransactionResponse version="1.0.0"
  787. xmlns:wfs="http://www.opengis.net/wfs"
  788. xmlns:ogc="http://www.opengis.net/ogc"
  789. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  790. {$wfsInsertResult}
  791. <wfs:TransactionResult>
  792. <wfs:Status>{$statusTag}</wfs:Status>
  793. {$messageTag}
  794. </wfs:TransactionResult>
  795. </wfs:WFS_TransactionResponse>
  796. EOF;
  797. return $tranRes;
  798. }
  799. public function _convertTransactionXml($requestXmlString, $sourceNsList) {
  800. $DBG = (V::get('DBG_XSL', '', $_GET) > 0);// TODO: Profiler
  801. if($DBG){echo 'sourceNsList:';print_r($sourceNsList);echo "\n";}
  802. $updateActionsXsd = array();
  803. $insertActionsXsd = array();
  804. //<!-- TODO: create tag Update{X} where X is namespace index -->
  805. foreach ($sourceNsList as $nsInd => $sourceNs) {
  806. // <Update>
  807. $theGeomField = 'the_geom';// TODO: get from fields list
  808. $typeName = "p5_{$sourceNs[0]}:{$sourceNs[1]}";
  809. $updateElementName = "UpdateNs{$nsInd}";
  810. $geomCoordsUpdateXpath = "//wfs:Value/*/gml:outerBoundaryIs/gml:LinearRing/gml:coordinates";
  811. $geomCoordsInsertXpath = "//*/gml:outerBoundaryIs/gml:LinearRing/gml:coordinates";
  812. $acl = $this->getAclFromTypeName($typeName);
  813. $geomType = $acl->getGeomFieldType($theGeomField);
  814. if ('polygon' == $geomType) {
  815. $geomCoordsUpdateXpath = "//wfs:Value/*/gml:outerBoundaryIs/gml:LinearRing/gml:coordinates";
  816. $geomCoordsUpdateXpath = "((<xsl:value-of select=\"{$geomCoordsUpdateXpath}\"/>))";
  817. $geomCoordsInsertXpath = "//*/gml:outerBoundaryIs/gml:LinearRing/gml:coordinates";
  818. $geomCoordsInsertXpath = "((<xsl:value-of select=\"{$geomCoordsInsertXpath}\"/>))";
  819. } else if ('linestring' == $geomType) {
  820. $geomCoordsUpdateXpath = "//wfs:Value/*/gml:coordinates";
  821. $geomCoordsUpdateXpath = "(<xsl:value-of select=\"{$geomCoordsUpdateXpath}\"/>)";
  822. $geomCoordsInsertXpath = "//*/gml:coordinates";
  823. $geomCoordsInsertXpath = "(<xsl:value-of select=\"{$geomCoordsInsertXpath}\"/>)";
  824. } else if ('point' == $geomType) {
  825. $geomCoordsUpdateXpath = "//wfs:Value/*/gml:coordinates";
  826. $geomCoordsUpdateXpath = "(<xsl:value-of select=\"{$geomCoordsUpdateXpath}\"/>)";
  827. $geomCoordsInsertXpath = "//*/gml:coordinates";
  828. $geomCoordsInsertXpath = "(<xsl:value-of select=\"{$geomCoordsInsertXpath}\"/>)";
  829. }
  830. $actionXsd = <<<EOF
  831. <xsl:when test="@typeName = '{$typeName}'">
  832. <xsl:element name="{$updateElementName}">
  833. <xsl:attribute name="typeName"><xsl:value-of select="@typeName" /></xsl:attribute>
  834. <xsl:attribute name="featureId"><xsl:value-of select="ogc:Filter/ogc:FeatureId/@fid" /></xsl:attribute>
  835. <xsl:for-each select="wfs:Property">
  836. <xsl:element name="{wfs:Name}">
  837. <xsl:choose>
  838. <xsl:when test="wfs:Name = '{$theGeomField}'"><xsl:value-of select="local-name(//wfs:Value/*[1])"/>{$geomCoordsUpdateXpath}</xsl:when>
  839. <xsl:otherwise><xsl:value-of select="wfs:Value"/></xsl:otherwise>
  840. </xsl:choose>
  841. </xsl:element>
  842. </xsl:for-each>
  843. </xsl:element>
  844. </xsl:when>
  845. EOF;
  846. $updateActionsXsd[] = $actionXsd;
  847. $typeName = "{$sourceNs[1]}";//"p5_{$sourceNs[0]}:{$sourceNs[1]}";
  848. $insertElementName = "InsertNs{$nsInd}";
  849. $actionXsd = <<<EOF
  850. <xsl:when test="local-name() = '{$typeName}'">
  851. <xsl:element name="{$insertElementName}">
  852. <xsl:attribute name="typeName"><xsl:value-of select="local-name()" /></xsl:attribute>
  853. <xsl:for-each select="*">
  854. <xsl:element name="{local-name()}">
  855. <xsl:choose>
  856. <xsl:when test="local-name() = '{$theGeomField}'"><xsl:value-of select="local-name(*[1])"/>{$geomCoordsInsertXpath}</xsl:when>
  857. <xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
  858. </xsl:choose>
  859. </xsl:element>
  860. </xsl:for-each>
  861. </xsl:element>
  862. </xsl:when>
  863. EOF;
  864. $insertActionsXsd[] = $actionXsd;
  865. }
  866. if (!empty($updateActionsXsd)) {
  867. $updateActionsXsd = implode("\n", $updateActionsXsd);
  868. $updateActionsXsd = <<<EOF
  869. <xsl:choose>
  870. {$updateActionsXsd}
  871. </xsl:choose>
  872. EOF;
  873. } else {
  874. $updateActionsXsd = '';
  875. }
  876. if (!empty($insertActionsXsd)) {
  877. $insertActionsXsd = implode("\n", $insertActionsXsd);
  878. $insertActionsXsd = <<<EOF
  879. <xsl:choose>
  880. {$insertActionsXsd}
  881. </xsl:choose>
  882. EOF;
  883. } else {
  884. $insertActionsXsd = '';
  885. }
  886. $convertTransactionXslString = <<<EOF
  887. <?xml version="1.0"?>
  888. <xsl:transform version="1.0"
  889. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  890. xmlns:wfs="http://www.opengis.net/wfs"
  891. xmlns:ogc="http://www.opengis.net/ogc"
  892. xmlns:gml="http://www.opengis.net/gml">
  893. <xsl:template match="/">
  894. <xsl:for-each select="wfs:Transaction">
  895. <Transaction>
  896. <xsl:attribute name="version"><xsl:value-of select="@version" /></xsl:attribute>
  897. <xsl:attribute name="service"><xsl:value-of select="@service" /></xsl:attribute>
  898. <xsl:for-each select="wfs:Update">
  899. {$updateActionsXsd}
  900. </xsl:for-each>
  901. <!-- TODO: Insert -->
  902. <xsl:for-each select="wfs:Insert/*">
  903. {$insertActionsXsd}
  904. </xsl:for-each>
  905. <!-- TODO: Delete -->
  906. <!-- TODO: Native -->
  907. </Transaction>
  908. </xsl:for-each>
  909. </xsl:template>
  910. </xsl:transform>
  911. EOF;
  912. if($DBG){echo '$convertTransactionXslString:' . $convertTransactionXslString . "\n";}
  913. $requestXml = new DOMDocument();
  914. $requestXml->loadXml($requestXmlString);
  915. $convertTransactionXsl = new DOMDocument();
  916. $convertTransactionXsl->loadXml($convertTransactionXslString);
  917. $proc = new XSLTProcessor();
  918. $proc->importStylesheet($convertTransactionXsl);
  919. return $proc->transformToXML($requestXml);
  920. }
  921. public function _validateConvertedTransactionXml($convertedTransaction, $sourceNsList) {
  922. $DBG = (V::get('DBG_XSD', '', $_GET) > 0);// TODO: Profiler
  923. if($DBG){echo 'sourceNsList:';print_r($sourceNsList);echo "\n";}
  924. $dom = new DOMDocument('1.0', 'utf-8');
  925. $dom->formatOutput = true;
  926. $dom->preserveWhiteSpace = false;
  927. $rootNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:schema');
  928. $dom->appendChild($rootNode);
  929. // $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
  930. // $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' . $wfsNs, $wfsNsUri);
  931. $rootNode->setAttribute('elementFormDefault', 'qualified');
  932. // $rootNode->setAttribute('targetNamespace', $wfsNsUri);
  933. $rootNode->setAttribute('version', '1.0');
  934. {// <xsd:element name="Transaction" type="TransactionType">
  935. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  936. $rootNode->appendChild($elNode);
  937. $elNode->setAttribute('name', 'Transaction');
  938. $elNode->setAttribute('type', 'TransactionType');
  939. $cTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexType');
  940. $rootNode->appendChild($cTypeNode);
  941. $cTypeNode->setAttribute('name', 'TransactionType');
  942. $seqNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:sequence');
  943. $cTypeNode->appendChild($seqNode);
  944. $choiceNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:choice');
  945. $seqNode->appendChild($choiceNode);
  946. $choiceNode->setAttribute('minOccurs', '0');
  947. $choiceNode->setAttribute('maxOccurs', 'unbounded');
  948. // <!-- <xsd:element ref="Update"/> -->
  949. foreach ($sourceNsList as $nsInd => $sourceNs) {
  950. $typeName = "p5_{$sourceNs[0]}:{$sourceNs[1]}";
  951. $updateElementName = "UpdateNs{$nsInd}";
  952. $updateElementType = "UpdateNs{$nsInd}ElementType";
  953. $updateElemNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  954. $choiceNode->appendChild($updateElemNode);
  955. $updateElemNode->setAttribute('name', $updateElementName);
  956. $updateElemNode->setAttribute('type', $updateElementType);
  957. }
  958. // <!-- <xsd:element ref="Insert"/> -->
  959. foreach ($sourceNsList as $nsInd => $sourceNs) {
  960. $typeName = "p5_{$sourceNs[0]}:{$sourceNs[1]}";
  961. $insertElementName = "InsertNs{$nsInd}";
  962. $insertElementType = "InsertNs{$nsInd}ElementType";
  963. $insertElemNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  964. $choiceNode->appendChild($insertElemNode);
  965. $insertElemNode->setAttribute('name', $insertElementName);
  966. $insertElemNode->setAttribute('type', $insertElementType);
  967. }
  968. // <!-- <xsd:element ref="Delete"/> -->
  969. // <!-- <xsd:element ref="Native"/> -->
  970. $attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
  971. $cTypeNode->appendChild($attrNode);
  972. $attrNode->setAttribute('name', 'version');
  973. $attrNode->setAttribute('type', 'xsd:string');
  974. $attrNode->setAttribute('use', 'required');
  975. $attrNode->setAttribute('fixed', '1.0.0');
  976. $attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
  977. $cTypeNode->appendChild($attrNode);
  978. $attrNode->setAttribute('name', 'service');
  979. $attrNode->setAttribute('type', 'xsd:string');
  980. $attrNode->setAttribute('use', 'required');
  981. $attrNode->setAttribute('fixed', 'WFS');
  982. }
  983. foreach ($sourceNsList as $nsInd => $sourceNs) {
  984. $transactionTypesList = array();
  985. $transactionTypesList[] = 'Update';
  986. $transactionTypesList[] = 'Insert';
  987. foreach ($transactionTypesList as $transactionType) {
  988. $typeName = "p5_{$sourceNs[0]}:{$sourceNs[1]}";
  989. $acl = $this->getAclFromTypeName($typeName);
  990. $updateElementName = "{$transactionType}Ns{$nsInd}";
  991. $updateElementType = "{$transactionType}Ns{$nsInd}ElementType";
  992. /*
  993. <xsd:complexType name="{$updateElementType}">
  994. <xsd:sequence>
  995. <xsd:element name="PARENT_ID" minOccurs="0" maxOccurs="1" type="xsd:integer" />
  996. </xsd:sequence>
  997. */
  998. $updateTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexType');
  999. $rootNode->appendChild($updateTypeNode);
  1000. $updateTypeNode->setAttribute('name', $updateElementType);
  1001. $seqNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:sequence');
  1002. $updateTypeNode->appendChild($seqNode);
  1003. $pKeyField = $acl->getPrimaryKeyField();
  1004. $fldList = $this->_getFieldListFromAcl($acl);
  1005. foreach ($fldList as $fldName) {
  1006. if ($acl->isGeomField($fldName)) continue;
  1007. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1008. $seqNode->appendChild($elNode);
  1009. $elNode->setAttribute('name', $fldName);
  1010. $minOccurs = 0;
  1011. if ($pKeyField == $fldName) {
  1012. $minOccurs = '1';
  1013. } else {
  1014. $minOccurs = '0';
  1015. }
  1016. $elNode->setAttribute('minOccurs', $minOccurs);
  1017. $fldType = null;
  1018. if ($acl->isIntegerField($fldName)) {
  1019. $fldType = 'xsd:integer';
  1020. }
  1021. else if ($acl->isDecimalField($fldName)) {
  1022. $fldType = 'xsd:decimal';
  1023. }
  1024. else if ($acl->isDateField($fldName)) {
  1025. $fldType = 'xsd:date';
  1026. }
  1027. else if ($acl->isDateTimeField($fldName)) {
  1028. $fldType = 'xsd:dateTime';
  1029. }
  1030. else if ($acl->isGeomField($fldName)) {
  1031. //$fldType = 'gml:GeometryPropertyType';
  1032. // TODO: use geom types from gml to wkt
  1033. // TODO: pattern wg atrybutów gml:coordinates decimal="." cs="," ts=" "
  1034. $patternWkt = '';// TODO: error if empty - unsupported geom type
  1035. $patternNum = '\-?\d+\.?\d*';
  1036. $patternPoint = $patternNum . ',' . $patternNum;
  1037. $patternPoints = '(' . $patternPoint . ')( ' . $patternPoint . ')+';
  1038. $geomType = $acl->getGeomFieldType($fldName);
  1039. if ('polygon' == $geomType) {
  1040. // [a-zA-Z]+\(\((\-?\d+\.?\d*,\-?\d+\.?\d*)( (\-?\d+\.?\d*,\-?\d+\.?\d*))+\)\)
  1041. $patternWkt = '[a-zA-Z]+\(\(' . $patternPoints . '\)\)';
  1042. } else if ('linestring' == $geomType) {
  1043. // [a-zA-Z]+\((\-?\d+\.?\d*,\-?\d+\.?\d*)( (\-?\d+\.?\d*,\-?\d+\.?\d*))+\)
  1044. $patternWkt = '[a-zA-Z]+\(' . $patternPoints . '\)';
  1045. } else if ('point' == $geomType) {
  1046. // [a-zA-Z]+\(\-?\d\.?\d*,\-?\d\.?\d*\)
  1047. $patternWkt = '[a-zA-Z]+\(' . $patternPoint . '\)';
  1048. }
  1049. $simpleTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:simpleType');
  1050. $restrictionNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:restriction');
  1051. $patternNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:pattern');
  1052. $restrictionNode->setAttribute('base', 'xsd:string');
  1053. $patternNode->setAttribute('value', $patternWkt);
  1054. $restrictionNode->appendChild($patternNode);
  1055. $simpleTypeNode->appendChild($restrictionNode);
  1056. $elNode->appendChild($simpleTypeNode);
  1057. }
  1058. else {
  1059. $fldType = 'xsd:string';
  1060. }
  1061. if ($fldType) $elNode->setAttribute('type', $fldType);
  1062. $elNode->setAttribute('nillable', 'true');
  1063. $elNode->setAttribute('minOccurs', '0');
  1064. }
  1065. foreach ($fldList as $fldName) {
  1066. if (!$acl->isGeomField($fldName)) continue;
  1067. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1068. $seqNode->appendChild($elNode);
  1069. $elNode->setAttribute('name', $fldName);
  1070. $minOccurs = 0;
  1071. if ($pKeyField == $fldName) {
  1072. $minOccurs = '1';
  1073. } else {
  1074. $minOccurs = '0';
  1075. }
  1076. $elNode->setAttribute('minOccurs', $minOccurs);
  1077. if ($acl->isGeomField($fldName)) {
  1078. //$fldType = 'gml:GeometryPropertyType';
  1079. // TODO: use geom types from gml to wkt
  1080. // TODO: pattern wg atrybutów gml:coordinates decimal="." cs="," ts=" "
  1081. $patternWkt = '';// TODO: error if empty - unsupported geom type
  1082. $patternNum = '\-?\d+\.?\d*';
  1083. $patternPoint = $patternNum . ',' . $patternNum;
  1084. $patternPoints = '(' . $patternPoint . ')( ' . $patternPoint . ')+';
  1085. $geomType = $acl->getGeomFieldType($fldName);
  1086. if ('polygon' == $geomType) {
  1087. // [a-zA-Z]+\(\((\-?\d+\.?\d*,\-?\d+\.?\d*)( (\-?\d+\.?\d*,\-?\d+\.?\d*))+\)\)
  1088. $patternWkt = '[a-zA-Z]+\(\(' . $patternPoints . '\)\)';
  1089. } else if ('linestring' == $geomType) {
  1090. // [a-zA-Z]+\((\-?\d+\.?\d*,\-?\d+\.?\d*)( (\-?\d+\.?\d*,\-?\d+\.?\d*))+\)
  1091. $patternWkt = '[a-zA-Z]+\(' . $patternPoints . '\)';
  1092. } else if ('point' == $geomType) {
  1093. // [a-zA-Z]+\(\-?\d\.?\d*,\-?\d\.?\d*\)
  1094. $patternWkt = '[a-zA-Z]+\(' . $patternPoint . '\)';
  1095. }
  1096. $simpleTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:simpleType');
  1097. $restrictionNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:restriction');
  1098. $patternNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:pattern');
  1099. $restrictionNode->setAttribute('base', 'xsd:string');
  1100. $patternNode->setAttribute('value', $patternWkt);
  1101. $restrictionNode->appendChild($patternNode);
  1102. $simpleTypeNode->appendChild($restrictionNode);
  1103. $elNode->appendChild($simpleTypeNode);
  1104. }
  1105. $elNode->setAttribute('nillable', 'true');
  1106. $elNode->setAttribute('minOccurs', '0');
  1107. }
  1108. $attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
  1109. $updateTypeNode->appendChild($attrNode);
  1110. $attrNode->setAttribute('name', 'typeName');
  1111. $attrNode->setAttribute('type', 'xsd:token');
  1112. $attrNode->setAttribute('use', 'required');
  1113. if ($transactionType == 'Update') {
  1114. $attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
  1115. $updateTypeNode->appendChild($attrNode);
  1116. $attrNode->setAttribute('name', 'featureId');
  1117. $attrNode->setAttribute('use', 'required');
  1118. $sTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:simpleType');
  1119. $attrNode->appendChild($sTypeNode);
  1120. $resNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:restriction');
  1121. $sTypeNode->appendChild($resNode);
  1122. $resNode->setAttribute('base', 'xsd:string');
  1123. $patternNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:pattern');
  1124. $resNode->appendChild($patternNode);
  1125. $patternNode->setAttribute('value', '[a-zA-Z_][a-zA-Z0-9_]*\.[0-9]*');
  1126. }
  1127. }
  1128. }
  1129. $validateConvertedTransactionXsdString = $dom->saveXml();
  1130. if($DBG){echo '$validateConvertedTransactionXsdString:';print_r($validateConvertedTransactionXsdString);echo "\n";}
  1131. $reqXml = new DOMDocument();
  1132. $reqXml->loadXml($convertedTransaction);
  1133. // TODO: fetch PHP Warning: DOMDocument::schemaValidateSource(): Element 'PARENT_ID': 'abc' is not a valid value of the atomic type 'xs:integer'.
  1134. return $reqXml->schemaValidateSource($validateConvertedTransactionXsdString);
  1135. }
  1136. public function _getDescribeFeatureType($nsPrefix, $type, $simple = true) {
  1137. return $this->_getDescribeFeatureTypes(array(array($nsPrefix, $type)), $simple);
  1138. }
  1139. public function _parseDescribeFeatureTypeRequest($reqContent, $simple = true) {
  1140. $parserXml = xml_parser_create();
  1141. xml_parser_set_option($parserXml, XML_OPTION_CASE_FOLDING, 0);
  1142. xml_parser_set_option($parserXml, XML_OPTION_SKIP_WHITE, 1);
  1143. if (0 == xml_parse_into_struct($parserXml, $reqContent, $tags)) {
  1144. throw new Exception("Error parsing xml");
  1145. }
  1146. xml_parser_free($parserXml);
  1147. if (empty($tags)) {
  1148. throw new Exception("Empty structure from request");
  1149. }
  1150. $rootTagName = V::get('tag', '', $tags[0]);
  1151. if ('DescribeFeatureType' != $rootTagName) {
  1152. throw new Api_WfsException("Wrong xml root tag '{$rootTagName}' #" . __LINE__, 501);
  1153. }
  1154. $requestXmlTags = $tags;
  1155. $DBG = (V::get('DBG_XML', '', $_GET) > 0);// TODO: Profiler
  1156. $rootTagName = V::get('tag', '', $requestXmlTags[0]);
  1157. if ('DescribeFeatureType' != $rootTagName) {
  1158. throw new Exception("Parse Request xml error #" . __LINE__);
  1159. }
  1160. /*[1] => Array(
  1161. [tag] => TypeName
  1162. [type] => complete
  1163. [level] => 2
  1164. [value] => p5_default_db:Rozdzielcza_rurociag_wsg84)
  1165. */
  1166. $typeNames = array();
  1167. $totalTypes = count($requestXmlTags) - 1;
  1168. for ($i = 1; $i < $totalTypes; $i++) {
  1169. if($DBG){echo "TAG[{$i}]:" . json_encode($requestXmlTags[$i]) . "\n";}
  1170. $typeNames[] = explode(':', $requestXmlTags[$i]['value'], 2);
  1171. }
  1172. //echo "typeNames: " . json_encode($typeNames) . "\n";
  1173. return $this->_getDescribeFeatureTypes($typeNames, $simple);
  1174. }
  1175. public function _getDescribeFeatureAllTypes($simple = true) {
  1176. $db = DB::getDB();
  1177. $idDefaultDB = $db->_zasob_id;
  1178. $tblsAcl = $this->_getTableAclList();
  1179. foreach ($tblsAcl as $tblAcl) {
  1180. $dataSourceName = 'default_db';// TODO: getSourceName
  1181. $tblName = $tblAcl->getName();
  1182. $typeNames[] = array("p5_{$dataSourceName}", $tblName);
  1183. }
  1184. return $this->_getDescribeFeatureTypes($typeNames, $simple);
  1185. }
  1186. // @param $typeNames = array( array( $nsPrefix, $type ) )
  1187. public function _getDescribeFeatureTypes($typeNames, $simple = true) {
  1188. if (empty($typeNames)) {
  1189. throw new HttpException("Feature Type Names not defined", 400);
  1190. }
  1191. $baseNsUri = $this->getBaseNamespaceUri();
  1192. $rootWfsNs = 'p5';
  1193. $rootWfsNsUri = "{$baseNsUri}";
  1194. $wfsNs = 'p5_default_db';
  1195. $wfsNsUri = "{$baseNsUri}/default_db";
  1196. $featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&REQUEST=DescribeFeatureType";
  1197. header('Content-type: application/xml');
  1198. // <xsd:import namespace="http://www.opengis.net/gml" schemaLocation="http://webgis.regione.sardegna.it:80/geoserver/schemas/gml/2.1.2/feature.xsd"/>
  1199. // <xsd:element name="{type}" substitutionGroup="gml:_Feature" type="dbu:{typeName}"/>
  1200. $dom = new DOMDocument('1.0', 'utf-8');
  1201. $dom->formatOutput = true;
  1202. $dom->preserveWhiteSpace = false;
  1203. $rootNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:schema');
  1204. $dom->appendChild($rootNode);
  1205. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
  1206. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' . $wfsNs, $wfsNsUri);
  1207. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', "xmlns:{$rootWfsNs}", $rootWfsNsUri);
  1208. $rootNode->setAttribute('elementFormDefault', 'qualified');
  1209. $rootNode->setAttribute('targetNamespace', $wfsNsUri);
  1210. {// <xsd:import namespace="http://www.opengis.net/gml" schemaLocation="...../gml/2.1.2/feature.xsd"/>
  1211. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:import');
  1212. $rootNode->appendChild($elNode);
  1213. $elNode->setAttribute('namespace', "http://www.opengis.net/gml");
  1214. $elNode->setAttribute('schemaLocation', $this->getBaseUri() . "/../../../schema/gml/2.1.2/feature.xsd");
  1215. }
  1216. foreach ($typeNames as $typeNameEx) {
  1217. $nsPrefix = $typeNameEx[0];
  1218. $type = $typeNameEx[1];
  1219. $typeName = "{$nsPrefix}:{$type}";
  1220. $acl = $this->getAclFromTypeName($typeName);
  1221. if (!$this->isAllowedFeatureType($nsPrefix, $type)) {
  1222. throw new Api_WfsException("Could not find type: " . htmlspecialchars($type));
  1223. }
  1224. $typeName = $type . 'Type';
  1225. $fldList = $this->_getFieldListFromAcl($acl);
  1226. $cTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexType');
  1227. $rootNode->appendChild($cTypeNode);
  1228. $cTypeNode->setAttribute('name', $typeName);
  1229. $cConNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexContent');
  1230. $cTypeNode->appendChild($cConNode);
  1231. $extNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:extension');
  1232. $cConNode->appendChild($extNode);
  1233. $extNode->setAttribute('base', 'gml:AbstractFeatureType');
  1234. $seqNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:sequence');
  1235. $extNode->appendChild($seqNode);
  1236. // <xsd:element maxOccurs="1" minOccurs="0" name="{$fldName}" nillable="true" type="xsd:integer"/>
  1237. $pKeyField = $acl->getPrimaryKeyField();
  1238. $p5Attributes = ($simple)? array() : $acl->getAttributesFromZasoby();
  1239. foreach ($fldList as $fldName) {
  1240. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1241. $seqNode->appendChild($elNode);
  1242. $elNode->setAttribute('name', $fldName);
  1243. $minOccurs = 0;
  1244. if ($pKeyField == $fldName) {
  1245. $minOccurs = '1';
  1246. } else {
  1247. $minOccurs = '0';
  1248. }
  1249. $elNode->setAttribute('minOccurs', $minOccurs);
  1250. $elNode->setAttribute('maxOccurs', '1');
  1251. $elNode->setAttribute('nillable', 'true');
  1252. $fldType = 'xsd:string';
  1253. if ($acl->isIntegerField($fldName)) {
  1254. $fldType = 'xsd:integer';
  1255. }
  1256. else if ($acl->isDecimalField($fldName)) {
  1257. $fldType = 'xsd:decimal';
  1258. }
  1259. else if ($acl->isDateField($fldName)) {
  1260. $fldType = 'xsd:date';
  1261. }
  1262. else if ($acl->isDateTimeField($fldName)) {
  1263. $fldType = 'xsd:dateTime';
  1264. }
  1265. else if ($acl->isGeomField($fldName)) {
  1266. //$fldType = 'gml:GeometryPropertyType';
  1267. $geomType = $acl->getGeomFieldType($fldName);
  1268. if ('polygon' == $geomType) {
  1269. $fldType = 'gml:PolygonPropertyType';
  1270. } else if ('point' == $geomType) {
  1271. $fldType = 'gml:PointPropertyType';
  1272. } else if ('linestring' == $geomType) {
  1273. $fldType = 'gml:LineStringPropertyType';
  1274. } else {
  1275. $fldType = 'gml:GeometryPropertyType';
  1276. }
  1277. }
  1278. else if ($acl->isEnumerationField($fldName)) {
  1279. $fldType = ($simple)? 'xsd:string' : "{$nsPrefix}:{$fldName}Type";
  1280. }
  1281. $elNode->setAttribute('type', $fldType);
  1282. if (!$simple) {
  1283. if (!empty($p5Attributes[$fldName]['id_zasob'])) {
  1284. $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:id_zasob", $p5Attributes[$fldName]['id_zasob']);
  1285. }
  1286. if (!empty($p5Attributes[$fldName]['label'])) {
  1287. $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:label", $p5Attributes[$fldName]['label']);
  1288. }
  1289. if (!empty($p5Attributes[$fldName]['description'])) {
  1290. $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:description", $p5Attributes[$fldName]['description']);
  1291. }
  1292. }
  1293. }
  1294. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1295. $rootNode->appendChild($elNode);
  1296. $elNode->setAttribute('name', $type);
  1297. $elNode->setAttribute('type', $wfsNs . ':' . $typeName);
  1298. if ($simple) {
  1299. $elNode->setAttribute('substitutionGroup', 'gml:_Feature');
  1300. } else {
  1301. $elNode->setAttribute('substitutionGroup', 'gml:AbstractFeature');
  1302. }
  1303. if (!$simple) {
  1304. foreach ($fldList as $fldName) {
  1305. if ($acl->isEnumerationField($fldName)) {
  1306. $enum = $acl->getEnumerations($fldName);
  1307. $stNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:simpleType');
  1308. $stNode->setAttribute('name', "{$fldName}Type");//"{$nsPrefix}:{$fldName}Type");
  1309. $rootNode->appendChild($stNode);
  1310. $resNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:restriction');
  1311. $stNode->appendChild($resNode);
  1312. $resNode->setAttribute('base', 'xsd:string');
  1313. // TODO: if (!empty($p5Attributes[$fldName]['valuesMap'])) -> show only this values
  1314. foreach ($enum as $val => $label) {
  1315. $enumNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:enumeration');
  1316. $resNode->appendChild($enumNode);
  1317. $enumNode->setAttribute('value', "{$val}");
  1318. if (!empty($p5Attributes[$fldName]['valuesMap'][$val])) {
  1319. $enumNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:label", $p5Attributes[$fldName]['valuesMap'][$val]);
  1320. }
  1321. }
  1322. }
  1323. }
  1324. }
  1325. }
  1326. return $dom->saveXML();
  1327. }
  1328. public function _getTableAclList() {// Use only Tables from default_db
  1329. $tblAclList = array();
  1330. $db = DB::getDB();
  1331. $idDefaultDB = $db->_zasob_id;
  1332. $fullTblAclList = $this->_usrAcl->getTablesAcl();
  1333. foreach ($fullTblAclList as $tblAcl) {
  1334. $dataSourceName = 'default_db';// TODO: getSourceName
  1335. $tblName = $tblAcl->getName();
  1336. if ($idDefaultDB != $tblAcl->getDB()) {// hide non default_db tables
  1337. continue;
  1338. }
  1339. try {
  1340. $acl = $this->getAclFromTypeName($typeName = "p5_{$dataSourceName}:{$tblName}");
  1341. } catch (Exception $e) {
  1342. // TODO: error log $e->getMessage();
  1343. }
  1344. if (!$acl) {
  1345. // TODO: error log msg
  1346. continue;
  1347. }
  1348. $tblAclList[] = $tblAcl;
  1349. }
  1350. return $tblAclList;
  1351. }
  1352. public function _getFieldListFromAcl($acl) {
  1353. $fldList = $acl->getRealFieldList();
  1354. return $fldList;
  1355. }
  1356. }