WfsServerBase.php 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  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->getRawLabel();
  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. }
  614. if (empty($usedSourceNsList)) {
  615. throw new Exception("Parse Request xml error #" . __LINE__ . ": not implemented");
  616. }
  617. $convertedTransaction = $this->_convertTransactionXml($requestXml, $usedSourceNsList);
  618. if($DBG){echo '$convertedTransaction:';print_r($convertedTransaction);echo "\n";}
  619. if (empty($convertedTransaction)) {
  620. throw new Exception("Parse Request xml error #" . __LINE__ . ": convert Transaction");
  621. }
  622. //echo "\ntags[0]:\n" . json_encode($requestXmlTags[0]) . "\n";
  623. //echo "\nconvertedTransaction:\n" . $convertedTransaction . "\n";
  624. //echo "\nsourceNsList:\n" . json_encode($sourceNsList) . "\n";
  625. if (!$this->_validateConvertedTransactionXml($convertedTransaction, $usedSourceNsList)) {
  626. throw new Exception("Parse Request xml error #" . __LINE__ . ": schema validation failed");
  627. }
  628. $parserXml = xml_parser_create();
  629. xml_parser_set_option($parserXml, XML_OPTION_CASE_FOLDING, 0);
  630. xml_parser_set_option($parserXml, XML_OPTION_SKIP_WHITE, 1);
  631. if (0 == xml_parse_into_struct($parserXml, $convertedTransaction, $tags)) {
  632. throw new Exception("Parse Request xml error #" . __LINE__ . ": parse converted transaction failed");
  633. }
  634. xml_parser_free($parserXml);
  635. if (empty($tags)) {
  636. throw new Exception("Parse Request xml error #" . __LINE__ . ": parse converted transaction returns empty structure");
  637. }
  638. // [{"tag":"Transaction","type":"open","level":1,"attributes":{"version":"1.0.0","service":"WFS"}},
  639. // {"tag":"Update","type":"open","level":2,"attributes":
  640. // {"typeName":"p5_default_db_13051:TEST_PERMS","featureId":"TEST_PERMS.25"}},
  641. // {"tag":"PARENT_ID","type":"complete","level":3,"value":"0"},
  642. // {"tag":"Update","type":"close","level":2},
  643. // {"tag":"Transaction","type":"close","level":1}]
  644. if($DBG){echo "\nTODO: tags L." . __LINE__ . ":\n"; print_r($tags);echo "\n";}
  645. $actionTag = null;
  646. $prevTagName = '';
  647. $theGeomField = 'the_geom';// TODO: get the geom field name from acl
  648. $itemPatchs = array();
  649. foreach ($tags as $tag) {
  650. switch (substr($tag['tag'], 0, 6)) {
  651. case 'Transa': continue; break;// Transaction
  652. case 'Update':
  653. case 'Insert':
  654. case 'Delete':
  655. case 'Native':
  656. if ('open' == $tag['type']) {
  657. $actionTag = array();
  658. $actionTag['tag'] = substr($tag['tag'], 0, 6);
  659. $actionTag['typeName'] = $tag['attributes']['typeName'];
  660. if ('Insert' == substr($tag['tag'], 0, 6)) {
  661. $actionTag['typeName'] = "p5_default_db:{$actionTag['typeName']}";
  662. }
  663. $featureEx = explode('.', $tag['attributes']['featureId'], 2);
  664. $actionTag['featureId'] = $featureEx[1];
  665. if ('Update' == substr($tag['tag'], 0, 6) && empty($actionTag['featureId'])) {
  666. throw new Api_WfsException("Syntax error - could not read feature id!");
  667. }
  668. $actionTag['itemPatch'] = array();
  669. } else {
  670. $itemPatchs[] = $actionTag;
  671. $actionTag = null;
  672. }
  673. break;
  674. default: {// fields
  675. if (3 != $tag['level'] && 'close' == $tag['type']) {
  676. $actionTag = null;
  677. }
  678. if (3 != $tag['level']) continue;
  679. if (empty($actionTag)) continue;
  680. if ('Update' == $actionTag['tag']) {
  681. if ($theGeomField == $tag['tag']) {
  682. $actionTag['itemPatch'][$tag['tag']] = $this->_typeConverter->convertGmlCoordinatesToWkt($tag['value']);
  683. } else {
  684. $actionTag['itemPatch'][$tag['tag']] = $tag['value'];
  685. }
  686. }
  687. else if ('Insert' == $actionTag['tag']) {
  688. if ($theGeomField == $tag['tag']) {
  689. $actionTag['itemPatch'][$tag['tag']] = $this->_typeConverter->convertGmlCoordinatesToWkt($tag['value']);
  690. } else {
  691. $actionTag['itemPatch'][$tag['tag']] = $tag['value'];
  692. }
  693. }
  694. }
  695. }
  696. if (empty($prevTagName)) $prevTagName = $tag['tag'];
  697. }
  698. if($DBG){echo "\nTODO: itemPatchs L." . __LINE__ . ":\n"; print_r($itemPatchs);echo "\n";}
  699. if($DBG){echo "\nTODO: _user_id L." . __LINE__ . ":\n"; print_r($this->_usrAcl->_user_id);echo "\n";}
  700. $changesList = array();
  701. if (!empty($itemPatchs)) {
  702. foreach ($itemPatchs as $itemPatchInfo) {
  703. if($DBG){echo "itemPatchInfo['itemPatch']:\n";print_r($itemPatchInfo['itemPatch']);}
  704. if (empty($itemPatchInfo['itemPatch'])) continue;
  705. $acl = $this->getAclFromTypeName($itemPatchInfo['typeName']);
  706. $itemPatch = $itemPatchInfo['itemPatch'];
  707. if ('Update' == $itemPatchInfo['tag']) {
  708. $itemPatch[$acl->getPrimaryKeyField()] = $itemPatchInfo['featureId'];
  709. }
  710. else if ('Insert' == $itemPatchInfo['tag']) {
  711. if (!empty($itemPatch[$acl->getPrimaryKeyField()])) {
  712. $itemPatchInfo['featureId'] = $itemPatch[$acl->getPrimaryKeyField()];
  713. }
  714. else {
  715. //throw new Exception("TODO: Insert #" . __LINE__ . ": Create new record");
  716. }
  717. }
  718. if($DBG){echo "TODO '" . ($itemPatchInfo['tag'])? 'Insert' : 'Update' . "' itemPatch:\n";print_r($itemPatch);}
  719. if ('Insert' == $itemPatchInfo['tag'] && empty($itemPatch[$acl->getPrimaryKeyField()])) {
  720. $newId = $acl->addItem($itemPatch);
  721. $changesList[$newId] = array('Status'=>(($newId > 0)? 'SUCCESS' : 'FAILED'), 'Message'=>"created {$newId}.");
  722. if ($newId > 0) {
  723. $changesList[$newId]['fid'] = $acl->getName() . '.' . $newId;
  724. }
  725. if($DBG){echo "created {$newId}.\n";}
  726. } else {
  727. $affected = $acl->updateItem($itemPatch);
  728. $changesList[$itemPatchInfo['featureId']] = array('Status'=>(($affected >= 0)? 'SUCCESS' : 'FAILED'), 'Message'=>"affected {$affected}.");
  729. if($DBG){echo "affected {$affected}.\n";}
  730. }
  731. }
  732. //throw new Exception("TODO: run query #" . __LINE__ . " \nitemPatchs:\n" . json_encode($itemPatchs) . " \ntags:\n" . json_encode($tags) . "\n");
  733. }
  734. else {
  735. throw new Exception("Parse Request xml error #" . __LINE__ . ": Nothing to change");
  736. }
  737. return $this->_transactionResponse($changesList);
  738. }
  739. public function _transactionResponse($changesList) {
  740. // <WFS_TransactionResponse>
  741. // <TransactionResult>
  742. // <Status> : SUCCESS / FAILED / PARTIAL
  743. // [<Locator]
  744. // [<Message]
  745. $messageTag = '';
  746. $statusTag = '';
  747. $statusIsFailed = false;
  748. $statusAll = null;
  749. $createdFetureId = array();
  750. foreach ($changesList as $featureId => $change) {
  751. if ('FAILED' == $change['Status']) {
  752. $statusIsFailed = true;
  753. }
  754. if ('SUCCESS' == $change['Status'] && !empty($change['fid'])) {
  755. $createdFetureId[] = $change['fid'];
  756. }
  757. if (!empty($change['Message'])) $messageTag .= "Feature '{$featureId}' {$change['Status']}: {$change['Message']}\n";
  758. }
  759. $statusTag = ($statusIsFailed)? 'FAILED' : 'SUCCESS';
  760. $statusTag = "<wfs:{$statusTag}/>";
  761. $messageTag = '';//"<wfs:Message>{$messageTag}</wfs:Message>";
  762. /* Example:
  763. <?xml version="1.0" encoding="UTF-8"?>
  764. <wfs:WFS_TransactionResponse version="1.0.0" xmlns:wfs="http://www.opengis.net/wfs"
  765. xmlns:ogc="http://www.opengis.net/ogc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  766. xsi:schemaLocation="http://www.opengis.net/wfs http://localhost:8080/geoserver/schemas/wfs/1.0.0/WFS-transaction.xsd">
  767. <wfs:InsertResult>
  768. <ogc:FeatureId fid="archsites.26" />
  769. </wfs:InsertResult>
  770. <wfs:TransactionResult handle="Updating Signature rock label">
  771. <wfs:Status>
  772. <wfs:SUCCESS />
  773. </wfs:Status>
  774. </wfs:TransactionResult>
  775. </wfs:WFS_TransactionResponse>*/
  776. // TODO: build xml by DOMDocument
  777. // TODO: xsi:schemaLocation="http://www.opengis.net/wfs http://localhost:8080/geoserver/schemas/wfs/1.0.0/WFS-transaction.xsd"
  778. $wfsInsertResult = '';
  779. if (!empty($createdFetureId)) {
  780. $wfsInsertResult = "\n<wfs:InsertResult>\n";
  781. foreach ($createdFetureId as $fid) {
  782. $wfsInsertResult .= '<ogc:FeatureId fid="' . $fid . '" xmlns:ogc="http://www.opengis.net/ogc"/>' . "\n";
  783. }
  784. $wfsInsertResult .= "\n</wfs:InsertResult>\n";
  785. EOF;
  786. }
  787. $tranRes = <<<EOF
  788. <wfs:WFS_TransactionResponse version="1.0.0"
  789. xmlns:wfs="http://www.opengis.net/wfs"
  790. xmlns:ogc="http://www.opengis.net/ogc"
  791. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  792. {$wfsInsertResult}
  793. <wfs:TransactionResult>
  794. <wfs:Status>{$statusTag}</wfs:Status>
  795. {$messageTag}
  796. </wfs:TransactionResult>
  797. </wfs:WFS_TransactionResponse>
  798. EOF;
  799. return $tranRes;
  800. }
  801. public function _convertTransactionXml($requestXmlString, $sourceNsList) {
  802. $DBG = (V::get('DBG_XSL', '', $_GET) > 0);// TODO: Profiler
  803. if($DBG){echo 'sourceNsList:';print_r($sourceNsList);echo "\n";}
  804. $updateActionsXsd = array();
  805. $insertActionsXsd = array();
  806. //<!-- TODO: create tag Update{X} where X is namespace index -->
  807. foreach ($sourceNsList as $nsInd => $sourceNs) {
  808. // <Update>
  809. $theGeomField = 'the_geom';// TODO: get from fields list
  810. $typeName = "p5_{$sourceNs[0]}:{$sourceNs[1]}";
  811. $updateElementName = "UpdateNs{$nsInd}";
  812. $geomCoordsUpdateXpath = "//wfs:Value/*/gml:outerBoundaryIs/gml:LinearRing/gml:coordinates";
  813. $geomCoordsInsertXpath = "//*/gml:outerBoundaryIs/gml:LinearRing/gml:coordinates";
  814. $acl = $this->getAclFromTypeName($typeName);
  815. $geomType = $acl->getGeomFieldType($theGeomField);
  816. if ('polygon' == $geomType) {
  817. $geomCoordsUpdateXpath = ".//wfs:Value/*/gml:outerBoundaryIs/gml:LinearRing/gml:coordinates";
  818. $geomCoordsUpdateXpath = "((<xsl:value-of select=\"{$geomCoordsUpdateXpath}\"/>))";
  819. $geomCoordsInsertXpath = ".//*/gml:outerBoundaryIs/gml:LinearRing/gml:coordinates";
  820. $geomCoordsInsertXpath = "((<xsl:value-of select=\"{$geomCoordsInsertXpath}\"/>))";
  821. } else if ('linestring' == $geomType) {
  822. $geomCoordsUpdateXpath = ".//wfs:Value/*/gml:coordinates";
  823. $geomCoordsUpdateXpath = "(<xsl:value-of select=\"{$geomCoordsUpdateXpath}\"/>)";
  824. $geomCoordsInsertXpath = ".//*/gml:coordinates";
  825. $geomCoordsInsertXpath = "(<xsl:value-of select=\"{$geomCoordsInsertXpath}\"/>)";
  826. } else if ('point' == $geomType) {
  827. $geomCoordsUpdateXpath = ".//wfs:Value/*/gml:coordinates";
  828. $geomCoordsUpdateXpath = "(<xsl:value-of select=\"{$geomCoordsUpdateXpath}\"/>)";
  829. $geomCoordsInsertXpath = ".//*/gml:coordinates";
  830. $geomCoordsInsertXpath = "(<xsl:value-of select=\"{$geomCoordsInsertXpath}\"/>)";
  831. }
  832. $actionXsd = <<<EOF
  833. <xsl:when test="@typeName = '{$typeName}'">
  834. <xsl:element name="{$updateElementName}">
  835. <xsl:attribute name="typeName"><xsl:value-of select="@typeName" /></xsl:attribute>
  836. <xsl:attribute name="featureId"><xsl:value-of select="ogc:Filter/ogc:FeatureId/@fid" /></xsl:attribute>
  837. <xsl:for-each select="wfs:Property">
  838. <xsl:element name="{wfs:Name}">
  839. <xsl:choose>
  840. <xsl:when test="wfs:Name = '{$theGeomField}'"><xsl:value-of select="local-name(//wfs:Value/*[1])"/>{$geomCoordsUpdateXpath}</xsl:when>
  841. <xsl:otherwise><xsl:value-of select="wfs:Value"/></xsl:otherwise>
  842. </xsl:choose>
  843. </xsl:element>
  844. </xsl:for-each>
  845. </xsl:element>
  846. </xsl:when>
  847. EOF;
  848. $updateActionsXsd[] = $actionXsd;
  849. $typeName = "{$sourceNs[1]}";//"p5_{$sourceNs[0]}:{$sourceNs[1]}";
  850. $insertElementName = "InsertNs{$nsInd}";
  851. $actionXsd = <<<EOF
  852. <xsl:when test="local-name() = '{$typeName}'">
  853. <xsl:element name="{$insertElementName}">
  854. <xsl:attribute name="typeName"><xsl:value-of select="local-name()" /></xsl:attribute>
  855. <xsl:for-each select="*">
  856. <xsl:element name="{local-name()}">
  857. <xsl:choose>
  858. <xsl:when test="local-name() = '{$theGeomField}'"><xsl:value-of select="local-name(*[1])"/>{$geomCoordsInsertXpath}</xsl:when>
  859. <xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
  860. </xsl:choose>
  861. </xsl:element>
  862. </xsl:for-each>
  863. </xsl:element>
  864. </xsl:when>
  865. EOF;
  866. $insertActionsXsd[] = $actionXsd;
  867. }
  868. if (!empty($updateActionsXsd)) {
  869. $updateActionsXsd = implode("\n", $updateActionsXsd);
  870. $updateActionsXsd = <<<EOF
  871. <xsl:choose>
  872. {$updateActionsXsd}
  873. </xsl:choose>
  874. EOF;
  875. } else {
  876. $updateActionsXsd = '';
  877. }
  878. if (!empty($insertActionsXsd)) {
  879. $insertActionsXsd = implode("\n", $insertActionsXsd);
  880. $insertActionsXsd = <<<EOF
  881. <xsl:choose>
  882. {$insertActionsXsd}
  883. </xsl:choose>
  884. EOF;
  885. } else {
  886. $insertActionsXsd = '';
  887. }
  888. $convertTransactionXslString = <<<EOF
  889. <?xml version="1.0"?>
  890. <xsl:transform version="1.0"
  891. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  892. xmlns:wfs="http://www.opengis.net/wfs"
  893. xmlns:ogc="http://www.opengis.net/ogc"
  894. xmlns:gml="http://www.opengis.net/gml">
  895. <xsl:template match="/">
  896. <xsl:for-each select="wfs:Transaction">
  897. <Transaction>
  898. <xsl:attribute name="version"><xsl:value-of select="@version" /></xsl:attribute>
  899. <xsl:attribute name="service"><xsl:value-of select="@service" /></xsl:attribute>
  900. <xsl:for-each select="wfs:Update">
  901. {$updateActionsXsd}
  902. </xsl:for-each>
  903. <!-- TODO: Insert -->
  904. <xsl:for-each select="wfs:Insert/*">
  905. {$insertActionsXsd}
  906. </xsl:for-each>
  907. <!-- TODO: Delete -->
  908. <!-- TODO: Native -->
  909. </Transaction>
  910. </xsl:for-each>
  911. </xsl:template>
  912. </xsl:transform>
  913. EOF;
  914. if($DBG){echo '$convertTransactionXslString:' . $convertTransactionXslString . "\n";}
  915. $requestXml = new DOMDocument();
  916. $requestXml->loadXml($requestXmlString);
  917. $convertTransactionXsl = new DOMDocument();
  918. $convertTransactionXsl->loadXml($convertTransactionXslString);
  919. $proc = new XSLTProcessor();
  920. $proc->importStylesheet($convertTransactionXsl);
  921. return $proc->transformToXML($requestXml);
  922. }
  923. public function _validateConvertedTransactionXml($convertedTransaction, $sourceNsList) {
  924. $DBG = (V::get('DBG_XSD', '', $_GET) > 0);// TODO: Profiler
  925. if($DBG){echo 'sourceNsList:';print_r($sourceNsList);echo "\n";}
  926. $dom = new DOMDocument('1.0', 'utf-8');
  927. $dom->formatOutput = true;
  928. $dom->preserveWhiteSpace = false;
  929. $rootNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:schema');
  930. $dom->appendChild($rootNode);
  931. // $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
  932. // $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' . $wfsNs, $wfsNsUri);
  933. $rootNode->setAttribute('elementFormDefault', 'qualified');
  934. // $rootNode->setAttribute('targetNamespace', $wfsNsUri);
  935. $rootNode->setAttribute('version', '1.0');
  936. {// <xsd:element name="Transaction" type="TransactionType">
  937. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  938. $rootNode->appendChild($elNode);
  939. $elNode->setAttribute('name', 'Transaction');
  940. $elNode->setAttribute('type', 'TransactionType');
  941. $cTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexType');
  942. $rootNode->appendChild($cTypeNode);
  943. $cTypeNode->setAttribute('name', 'TransactionType');
  944. $seqNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:sequence');
  945. $cTypeNode->appendChild($seqNode);
  946. $choiceNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:choice');
  947. $seqNode->appendChild($choiceNode);
  948. $choiceNode->setAttribute('minOccurs', '0');
  949. $choiceNode->setAttribute('maxOccurs', 'unbounded');
  950. // <!-- <xsd:element ref="Update"/> -->
  951. foreach ($sourceNsList as $nsInd => $sourceNs) {
  952. $typeName = "p5_{$sourceNs[0]}:{$sourceNs[1]}";
  953. $updateElementName = "UpdateNs{$nsInd}";
  954. $updateElementType = "UpdateNs{$nsInd}ElementType";
  955. $updateElemNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  956. $choiceNode->appendChild($updateElemNode);
  957. $updateElemNode->setAttribute('name', $updateElementName);
  958. $updateElemNode->setAttribute('type', $updateElementType);
  959. }
  960. // <!-- <xsd:element ref="Insert"/> -->
  961. foreach ($sourceNsList as $nsInd => $sourceNs) {
  962. $typeName = "p5_{$sourceNs[0]}:{$sourceNs[1]}";
  963. $insertElementName = "InsertNs{$nsInd}";
  964. $insertElementType = "InsertNs{$nsInd}ElementType";
  965. $insertElemNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  966. $choiceNode->appendChild($insertElemNode);
  967. $insertElemNode->setAttribute('name', $insertElementName);
  968. $insertElemNode->setAttribute('type', $insertElementType);
  969. }
  970. // <!-- <xsd:element ref="Delete"/> -->
  971. // <!-- <xsd:element ref="Native"/> -->
  972. $attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
  973. $cTypeNode->appendChild($attrNode);
  974. $attrNode->setAttribute('name', 'version');
  975. $attrNode->setAttribute('type', 'xsd:string');
  976. $attrNode->setAttribute('use', 'required');
  977. $attrNode->setAttribute('fixed', '1.0.0');
  978. $attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
  979. $cTypeNode->appendChild($attrNode);
  980. $attrNode->setAttribute('name', 'service');
  981. $attrNode->setAttribute('type', 'xsd:string');
  982. $attrNode->setAttribute('use', 'required');
  983. $attrNode->setAttribute('fixed', 'WFS');
  984. }
  985. foreach ($sourceNsList as $nsInd => $sourceNs) {
  986. $transactionTypesList = array();
  987. $transactionTypesList[] = 'Update';
  988. $transactionTypesList[] = 'Insert';
  989. foreach ($transactionTypesList as $transactionType) {
  990. $typeName = "p5_{$sourceNs[0]}:{$sourceNs[1]}";
  991. $acl = $this->getAclFromTypeName($typeName);
  992. $updateElementName = "{$transactionType}Ns{$nsInd}";
  993. $updateElementType = "{$transactionType}Ns{$nsInd}ElementType";
  994. /*
  995. <xsd:complexType name="{$updateElementType}">
  996. <xsd:sequence>
  997. <xsd:element name="PARENT_ID" minOccurs="0" maxOccurs="1" type="xsd:integer" />
  998. </xsd:sequence>
  999. */
  1000. $updateTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexType');
  1001. $rootNode->appendChild($updateTypeNode);
  1002. $updateTypeNode->setAttribute('name', $updateElementType);
  1003. $seqNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:all');
  1004. $updateTypeNode->appendChild($seqNode);
  1005. $pKeyField = $acl->getPrimaryKeyField();
  1006. $fldList = $this->_getFieldListFromAcl($acl);
  1007. foreach ($fldList as $fldName) {
  1008. if ($acl->isGeomField($fldName)) continue;
  1009. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1010. $seqNode->appendChild($elNode);
  1011. $elNode->setAttribute('name', $fldName);
  1012. $minOccurs = 0;
  1013. if ($pKeyField == $fldName) {
  1014. $minOccurs = '1';
  1015. } else {
  1016. $minOccurs = '0';
  1017. }
  1018. $elNode->setAttribute('minOccurs', $minOccurs);
  1019. $fldType = null;
  1020. if ($acl->isIntegerField($fldName)) {
  1021. $fldType = 'xsd:integer';
  1022. }
  1023. else if ($acl->isDecimalField($fldName)) {
  1024. $fldType = 'xsd:decimal';
  1025. }
  1026. else if ($acl->isDateField($fldName)) {
  1027. $fldType = 'xsd:date';
  1028. }
  1029. else if ($acl->isDateTimeField($fldName)) {
  1030. $fldType = 'xsd:dateTime';
  1031. }
  1032. else if ($acl->isGeomField($fldName)) {
  1033. //$fldType = 'gml:GeometryPropertyType';
  1034. // TODO: use geom types from gml to wkt
  1035. // TODO: pattern wg atrybutów gml:coordinates decimal="." cs="," ts=" "
  1036. $patternWkt = '';// TODO: error if empty - unsupported geom type
  1037. $patternNum = '\-?\d+\.?\d*';
  1038. $patternPoint = $patternNum . ',' . $patternNum;
  1039. $patternPoints = '(' . $patternPoint . ')( ' . $patternPoint . ')+';
  1040. $geomType = $acl->getGeomFieldType($fldName);
  1041. if ('polygon' == $geomType) {
  1042. // [a-zA-Z]+\(\((\-?\d+\.?\d*,\-?\d+\.?\d*)( (\-?\d+\.?\d*,\-?\d+\.?\d*))+\)\)
  1043. $patternWkt = '[a-zA-Z]+\(\(' . $patternPoints . '\)\)';
  1044. } else if ('linestring' == $geomType) {
  1045. // [a-zA-Z]+\((\-?\d+\.?\d*,\-?\d+\.?\d*)( (\-?\d+\.?\d*,\-?\d+\.?\d*))+\)
  1046. $patternWkt = '[a-zA-Z]+\(' . $patternPoints . '\)';
  1047. } else if ('point' == $geomType) {
  1048. // [a-zA-Z]+\(\-?\d\.?\d*,\-?\d\.?\d*\)
  1049. $patternWkt = '[a-zA-Z]+\(' . $patternPoint . '\)';
  1050. }
  1051. $simpleTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:simpleType');
  1052. $restrictionNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:restriction');
  1053. $patternNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:pattern');
  1054. $restrictionNode->setAttribute('base', 'xsd:string');
  1055. $patternNode->setAttribute('value', $patternWkt);
  1056. $restrictionNode->appendChild($patternNode);
  1057. $simpleTypeNode->appendChild($restrictionNode);
  1058. $elNode->appendChild($simpleTypeNode);
  1059. }
  1060. else {
  1061. $fldType = 'xsd:string';
  1062. }
  1063. if ($fldType) $elNode->setAttribute('type', $fldType);
  1064. $elNode->setAttribute('nillable', 'true');
  1065. $elNode->setAttribute('minOccurs', '0');
  1066. }
  1067. foreach ($fldList as $fldName) {
  1068. if (!$acl->isGeomField($fldName)) continue;
  1069. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1070. $seqNode->appendChild($elNode);
  1071. $elNode->setAttribute('name', $fldName);
  1072. $minOccurs = 0;
  1073. if ($pKeyField == $fldName) {
  1074. $minOccurs = '1';
  1075. } else {
  1076. $minOccurs = '0';
  1077. }
  1078. $elNode->setAttribute('minOccurs', $minOccurs);
  1079. if ($acl->isGeomField($fldName)) {
  1080. //$fldType = 'gml:GeometryPropertyType';
  1081. // TODO: use geom types from gml to wkt
  1082. // TODO: pattern wg atrybutów gml:coordinates decimal="." cs="," ts=" "
  1083. $patternWkt = '';// TODO: error if empty - unsupported geom type
  1084. $patternNum = '\-?\d+\.?\d*';
  1085. $patternPoint = $patternNum . ',' . $patternNum;
  1086. $patternPoints = '(' . $patternPoint . ')( ' . $patternPoint . ')+';
  1087. $geomType = $acl->getGeomFieldType($fldName);
  1088. if ('polygon' == $geomType) {
  1089. // [a-zA-Z]+\(\((\-?\d+\.?\d*,\-?\d+\.?\d*)( (\-?\d+\.?\d*,\-?\d+\.?\d*))+\)\)
  1090. $patternWkt = '[a-zA-Z]+\(\(' . $patternPoints . '\)\)';
  1091. } else if ('linestring' == $geomType) {
  1092. // [a-zA-Z]+\((\-?\d+\.?\d*,\-?\d+\.?\d*)( (\-?\d+\.?\d*,\-?\d+\.?\d*))+\)
  1093. $patternWkt = '[a-zA-Z]+\(' . $patternPoints . '\)';
  1094. } else if ('point' == $geomType) {
  1095. // [a-zA-Z]+\(\-?\d\.?\d*,\-?\d\.?\d*\)
  1096. $patternWkt = '[a-zA-Z]+\(' . $patternPoint . '\)';
  1097. }
  1098. $simpleTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:simpleType');
  1099. $restrictionNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:restriction');
  1100. $patternNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:pattern');
  1101. $restrictionNode->setAttribute('base', 'xsd:string');
  1102. $patternNode->setAttribute('value', $patternWkt);
  1103. $restrictionNode->appendChild($patternNode);
  1104. $simpleTypeNode->appendChild($restrictionNode);
  1105. $elNode->appendChild($simpleTypeNode);
  1106. }
  1107. $elNode->setAttribute('nillable', 'true');
  1108. $elNode->setAttribute('minOccurs', '0');
  1109. }
  1110. $attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
  1111. $updateTypeNode->appendChild($attrNode);
  1112. $attrNode->setAttribute('name', 'typeName');
  1113. $attrNode->setAttribute('type', 'xsd:token');
  1114. $attrNode->setAttribute('use', 'required');
  1115. if ($transactionType == 'Update') {
  1116. $attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
  1117. $updateTypeNode->appendChild($attrNode);
  1118. $attrNode->setAttribute('name', 'featureId');
  1119. $attrNode->setAttribute('use', 'required');
  1120. $sTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:simpleType');
  1121. $attrNode->appendChild($sTypeNode);
  1122. $resNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:restriction');
  1123. $sTypeNode->appendChild($resNode);
  1124. $resNode->setAttribute('base', 'xsd:string');
  1125. $patternNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:pattern');
  1126. $resNode->appendChild($patternNode);
  1127. $patternNode->setAttribute('value', '[a-zA-Z_][a-zA-Z0-9_]*\.[0-9]*');
  1128. }
  1129. }
  1130. }
  1131. $validateConvertedTransactionXsdString = $dom->saveXml();
  1132. if($DBG){echo '$validateConvertedTransactionXsdString:';print_r($validateConvertedTransactionXsdString);echo "\n";}
  1133. $reqXml = new DOMDocument();
  1134. $reqXml->loadXml($convertedTransaction);
  1135. // TODO: fetch PHP Warning: DOMDocument::schemaValidateSource(): Element 'PARENT_ID': 'abc' is not a valid value of the atomic type 'xs:integer'.
  1136. return $reqXml->schemaValidateSource($validateConvertedTransactionXsdString);
  1137. }
  1138. public function _getDescribeFeatureType($nsPrefix, $type, $simple = true) {
  1139. return $this->_getDescribeFeatureTypes(array(array($nsPrefix, $type)), $simple);
  1140. }
  1141. public function _parseDescribeFeatureTypeRequest($reqContent, $simple = true) {
  1142. $parserXml = xml_parser_create();
  1143. xml_parser_set_option($parserXml, XML_OPTION_CASE_FOLDING, 0);
  1144. xml_parser_set_option($parserXml, XML_OPTION_SKIP_WHITE, 1);
  1145. if (0 == xml_parse_into_struct($parserXml, $reqContent, $tags)) {
  1146. throw new Exception("Error parsing xml");
  1147. }
  1148. xml_parser_free($parserXml);
  1149. if (empty($tags)) {
  1150. throw new Exception("Empty structure from request");
  1151. }
  1152. $rootTagName = V::get('tag', '', $tags[0]);
  1153. if ('DescribeFeatureType' != $rootTagName) {
  1154. throw new Api_WfsException("Wrong xml root tag '{$rootTagName}' #" . __LINE__, 501);
  1155. }
  1156. $requestXmlTags = $tags;
  1157. $DBG = (V::get('DBG_XML', '', $_GET) > 0);// TODO: Profiler
  1158. $rootTagName = V::get('tag', '', $requestXmlTags[0]);
  1159. if ('DescribeFeatureType' != $rootTagName) {
  1160. throw new Exception("Parse Request xml error #" . __LINE__);
  1161. }
  1162. /*[1] => Array(
  1163. [tag] => TypeName
  1164. [type] => complete
  1165. [level] => 2
  1166. [value] => p5_default_db:Rozdzielcza_rurociag_wsg84)
  1167. */
  1168. $typeNames = array();
  1169. $totalTypes = count($requestXmlTags) - 1;
  1170. for ($i = 1; $i < $totalTypes; $i++) {
  1171. if($DBG){echo "TAG[{$i}]:" . json_encode($requestXmlTags[$i]) . "\n";}
  1172. $typeNames[] = explode(':', $requestXmlTags[$i]['value'], 2);
  1173. }
  1174. //echo "typeNames: " . json_encode($typeNames) . "\n";
  1175. return $this->_getDescribeFeatureTypes($typeNames, $simple);
  1176. }
  1177. public function _getDescribeFeatureAllTypes($simple = true) {
  1178. $db = DB::getDB();
  1179. $idDefaultDB = $db->_zasob_id;
  1180. $tblsAcl = $this->_getTableAclList();
  1181. foreach ($tblsAcl as $tblAcl) {
  1182. $dataSourceName = 'default_db';// TODO: getSourceName
  1183. $tblName = $tblAcl->getName();
  1184. $typeNames[] = array("p5_{$dataSourceName}", $tblName);
  1185. }
  1186. return $this->_getDescribeFeatureTypes($typeNames, $simple);
  1187. }
  1188. // @param $typeNames = array( array( $nsPrefix, $type ) )
  1189. public function _getDescribeFeatureTypes($typeNames, $simple = true) {
  1190. if (empty($typeNames)) throw new HttpException("Feature Type Names not defined", 400);
  1191. $this->DBG("types:" . json_encode($typeNames), __LINE__, __FUNCTION__, __CLASS__);
  1192. $baseNsUri = $this->getBaseNamespaceUri();
  1193. $rootWfsNs = 'p5';
  1194. $rootWfsNsUri = "{$baseNsUri}";
  1195. $wfsNs = 'p5_default_db';
  1196. $wfsNsUri = "{$baseNsUri}/default_db";
  1197. $featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&REQUEST=DescribeFeatureType";
  1198. header('Content-type: application/xml');
  1199. // <xsd:import namespace="http://www.opengis.net/gml" schemaLocation="http://webgis.regione.sardegna.it:80/geoserver/schemas/gml/2.1.2/feature.xsd"/>
  1200. // <xsd:element name="{type}" substitutionGroup="gml:_Feature" type="dbu:{typeName}"/>
  1201. $dom = new DOMDocument('1.0', 'utf-8');
  1202. $dom->formatOutput = true;
  1203. $dom->preserveWhiteSpace = false;
  1204. $rootNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:schema');
  1205. $dom->appendChild($rootNode);
  1206. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
  1207. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' . $wfsNs, $wfsNsUri);
  1208. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', "xmlns:{$rootWfsNs}", $rootWfsNsUri);
  1209. $rootNode->setAttribute('elementFormDefault', 'qualified');
  1210. $rootNode->setAttribute('targetNamespace', $wfsNsUri);
  1211. {// <xsd:import namespace="http://www.opengis.net/gml" schemaLocation="...../gml/2.1.2/feature.xsd"/>
  1212. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:import');
  1213. $rootNode->appendChild($elNode);
  1214. $elNode->setAttribute('namespace', "http://www.opengis.net/gml");
  1215. $elNode->setAttribute('schemaLocation', $this->getBaseUri() . "/../../../schema/gml/2.1.2/feature.xsd");
  1216. }
  1217. foreach ($typeNames as $typeNameEx) {
  1218. $nsPrefix = $typeNameEx[0];
  1219. $type = $typeNameEx[1];
  1220. $typeName = "{$nsPrefix}:{$type}";
  1221. $acl = $this->getAclFromTypeName($typeName);
  1222. if (!$this->isAllowedFeatureType($nsPrefix, $type)) {
  1223. throw new Api_WfsException("Could not find type: " . htmlspecialchars($type));
  1224. }
  1225. $typeName = $type . 'Type';
  1226. $fldList = $this->_getFieldListFromAcl($acl);
  1227. $cTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexType');
  1228. $rootNode->appendChild($cTypeNode);
  1229. $cTypeNode->setAttribute('name', $typeName);
  1230. $cConNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexContent');
  1231. $cTypeNode->appendChild($cConNode);
  1232. $extNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:extension');
  1233. $cConNode->appendChild($extNode);
  1234. $extNode->setAttribute('base', 'gml:AbstractFeatureType');
  1235. $seqNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:sequence');
  1236. $extNode->appendChild($seqNode);
  1237. // <xsd:element maxOccurs="1" minOccurs="0" name="{$fldName}" nillable="true" type="xsd:integer"/>
  1238. $pKeyField = $acl->getPrimaryKeyField();
  1239. $p5Attributes = ($simple)? array() : $acl->getAttributesFromZasoby();
  1240. foreach ($fldList as $fldName) {
  1241. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1242. $seqNode->appendChild($elNode);
  1243. $elNode->setAttribute('name', $fldName);
  1244. $minOccurs = 0;
  1245. if ($pKeyField == $fldName) {
  1246. $minOccurs = '1';
  1247. } else {
  1248. $minOccurs = '0';
  1249. }
  1250. $elNode->setAttribute('minOccurs', $minOccurs);
  1251. $elNode->setAttribute('maxOccurs', '1');
  1252. $elNode->setAttribute('nillable', 'true');
  1253. $fldType = 'xsd:string';
  1254. if ($acl->isIntegerField($fldName)) {
  1255. $fldType = 'xsd:integer';
  1256. }
  1257. else if ($acl->isDecimalField($fldName)) {
  1258. $fldType = 'xsd:decimal';
  1259. }
  1260. else if ($acl->isDateField($fldName)) {
  1261. $fldType = 'xsd:date';
  1262. }
  1263. else if ($acl->isDateTimeField($fldName)) {
  1264. $fldType = 'xsd:dateTime';
  1265. }
  1266. else if ($acl->isGeomField($fldName)) {
  1267. //$fldType = 'gml:GeometryPropertyType';
  1268. $geomType = $acl->getGeomFieldType($fldName);
  1269. if ('polygon' == $geomType) {
  1270. $fldType = 'gml:PolygonPropertyType';
  1271. } else if ('point' == $geomType) {
  1272. $fldType = 'gml:PointPropertyType';
  1273. } else if ('linestring' == $geomType) {
  1274. $fldType = 'gml:LineStringPropertyType';
  1275. } else {
  1276. $fldType = 'gml:GeometryPropertyType';
  1277. }
  1278. }
  1279. else if ($acl->isEnumerationField($fldName)) {
  1280. $fldType = ($simple)? 'xsd:string' : "{$nsPrefix}:{$fldName}Type";
  1281. }
  1282. $elNode->setAttribute('type', $fldType);
  1283. if (!$simple) {
  1284. if (!empty($p5Attributes[$fldName]['id_zasob'])) {
  1285. $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:id_zasob", $p5Attributes[$fldName]['id_zasob']);
  1286. }
  1287. if (!empty($p5Attributes[$fldName]['label'])) {
  1288. $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:label", $p5Attributes[$fldName]['label']);
  1289. }
  1290. if (!empty($p5Attributes[$fldName]['description'])) {
  1291. $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:description", $p5Attributes[$fldName]['description']);
  1292. }
  1293. }
  1294. }
  1295. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1296. $rootNode->appendChild($elNode);
  1297. $elNode->setAttribute('name', $type);
  1298. $elNode->setAttribute('type', $wfsNs . ':' . $typeName);
  1299. if ($simple) {
  1300. $elNode->setAttribute('substitutionGroup', 'gml:_Feature');
  1301. } else {
  1302. $elNode->setAttribute('substitutionGroup', 'gml:AbstractFeature');
  1303. }
  1304. if (!$simple) {
  1305. foreach ($fldList as $fldName) {
  1306. if ($acl->isEnumerationField($fldName)) {
  1307. $enum = $acl->getEnumerations($fldName);
  1308. $stNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:simpleType');
  1309. $stNode->setAttribute('name', "{$fldName}Type");//"{$nsPrefix}:{$fldName}Type");
  1310. $rootNode->appendChild($stNode);
  1311. $resNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:restriction');
  1312. $stNode->appendChild($resNode);
  1313. $resNode->setAttribute('base', 'xsd:string');
  1314. // TODO: if (!empty($p5Attributes[$fldName]['valuesMap'])) -> show only this values
  1315. foreach ($enum as $val => $label) {
  1316. $enumNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:enumeration');
  1317. $resNode->appendChild($enumNode);
  1318. $enumNode->setAttribute('value', "{$val}");
  1319. if (!empty($p5Attributes[$fldName]['valuesMap'][$val])) {
  1320. $enumNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:label", $p5Attributes[$fldName]['valuesMap'][$val]);
  1321. }
  1322. }
  1323. }
  1324. }
  1325. }
  1326. }
  1327. return $dom->saveXML();
  1328. }
  1329. public function _getTableAclList() {// Use only Tables from default_db
  1330. $tblAclList = array();
  1331. $db = DB::getDB();
  1332. $idDefaultDB = $db->_zasob_id;
  1333. $fullTblAclList = $this->_usrAcl->getTablesAcl();
  1334. foreach ($fullTblAclList as $tblAcl) {
  1335. $dataSourceName = 'default_db';// TODO: getSourceName
  1336. $tblName = $tblAcl->getName();
  1337. if ($idDefaultDB != $tblAcl->getDB()) {// hide non default_db tables
  1338. continue;
  1339. }
  1340. try {
  1341. $acl = $this->getAclFromTypeName($typeName = "p5_{$dataSourceName}:{$tblName}");
  1342. } catch (Exception $e) {
  1343. // TODO: error log $e->getMessage();
  1344. }
  1345. if (!$acl) {
  1346. // TODO: error log msg
  1347. continue;
  1348. }
  1349. $tblAclList[] = $tblAcl;
  1350. }
  1351. return $tblAclList;
  1352. }
  1353. public function _getFieldListFromAcl($acl) {
  1354. $fldList = $acl->getRealFieldList();
  1355. return $fldList;
  1356. }
  1357. public function setLogger($logger) {
  1358. $this->_logger = $logger;
  1359. }
  1360. public function DBG($reqLog, $lineNr = null, $funName = null, $className = null) {
  1361. if (!$this->_logger) return;
  1362. $this->_logger->DBG($reqLog, $lineNr, $funName, $className);
  1363. }
  1364. }