WfsServerBase.php 65 KB

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