WfsServerBase.php 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  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 "L." . __LINE__ . " 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 "L." . __LINE__ . " 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. {
  1068. $seqNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:all');
  1069. $updateTypeNode->appendChild($seqNode);
  1070. {
  1071. $pKeyField = $acl->getPrimaryKeyField();
  1072. $fldList = $this->_getFieldListFromAcl($acl);
  1073. // fields without geometry fields
  1074. foreach ($fldList as $fldName) {
  1075. if ($acl->isGeomField($fldName)) continue;
  1076. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1077. $seqNode->appendChild($elNode);
  1078. $elNode->setAttribute('name', $fldName);
  1079. $minOccurs = 0;
  1080. if ($pKeyField == $fldName) {
  1081. $minOccurs = '1';
  1082. } else {
  1083. $minOccurs = '0';
  1084. }
  1085. $elNode->setAttribute('minOccurs', $minOccurs);
  1086. $fldType = null;
  1087. if ($acl->isIntegerField($fldName)) {
  1088. $fldType = 'xsd:integer';
  1089. }
  1090. else if ($acl->isDecimalField($fldName)) {
  1091. $fldType = 'xsd:decimal';
  1092. }
  1093. else if ($acl->isDateField($fldName)) {
  1094. $fldType = 'xsd:date';
  1095. }
  1096. else if ($acl->isDateTimeField($fldName)) {
  1097. // $fldType = 'xsd:dateTime';
  1098. $fldType = null;// 'xsd:string';
  1099. $patternDataTime = "(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})(:(\d{2}))?";
  1100. // <xsd:element name="..." minOccurs="0" nillable="true">
  1101. // <xsd:simpleType>
  1102. // <xsd:restriction base="xsd:string">
  1103. // <xsd:pattern value="[a-zA-Z]+\(\((\-?\d+\.?\d*,\-?\d+\.?\d*)( \-?\d+\.?\d*,\-?\d+\.?\d*)+\)\)"/>
  1104. // </xsd:restriction>
  1105. // </xsd:simpleType>
  1106. // </xsd:element>
  1107. $simpleTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:simpleType');
  1108. $restrictionNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:restriction');
  1109. $patternNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:pattern');
  1110. $restrictionNode->setAttribute('base', 'xsd:string');
  1111. $patternNode->setAttribute('value', $patternDataTime);
  1112. $restrictionNode->appendChild($patternNode);
  1113. $simpleTypeNode->appendChild($restrictionNode);
  1114. $elNode->appendChild($simpleTypeNode);
  1115. // continue;// TODO: ? below added nillable = true, minOccurs = 0, type = $fldType
  1116. }
  1117. else {
  1118. $fldType = 'xsd:string';
  1119. }
  1120. if ($fldType) $elNode->setAttribute('type', $fldType);
  1121. $elNode->setAttribute('nillable', 'true');
  1122. $elNode->setAttribute('minOccurs', '0');
  1123. }
  1124. // only geometry fields
  1125. foreach ($fldList as $fldName) {
  1126. if (!$acl->isGeomField($fldName)) continue;
  1127. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1128. $seqNode->appendChild($elNode);
  1129. $elNode->setAttribute('name', $fldName);
  1130. $minOccurs = 0;
  1131. if ($pKeyField == $fldName) {
  1132. $minOccurs = '1';
  1133. } else {
  1134. $minOccurs = '0';
  1135. }
  1136. $elNode->setAttribute('minOccurs', $minOccurs);
  1137. if ($acl->isGeomField($fldName)) {
  1138. //$fldType = 'gml:GeometryPropertyType';
  1139. // TODO: use geom types from gml to wkt
  1140. // TODO: pattern wg atrybutów gml:coordinates decimal="." cs="," ts=" "
  1141. $patternWkt = '';// TODO: error if empty - unsupported geom type
  1142. $patternNum = '\-?\d+\.?\d*';
  1143. $patternPoint = $patternNum . ',' . $patternNum;
  1144. $patternPoints = '(' . $patternPoint . ')( ' . $patternPoint . ')+';
  1145. $geomType = $acl->getGeomFieldType($fldName);
  1146. if ('polygon' == $geomType) {
  1147. // [a-zA-Z]+\(\((\-?\d+\.?\d*,\-?\d+\.?\d*)( (\-?\d+\.?\d*,\-?\d+\.?\d*))+\)\)
  1148. $patternWkt = '[a-zA-Z]+\(\(' . $patternPoints . '\)\)';
  1149. } else if ('linestring' == $geomType) {
  1150. // [a-zA-Z]+\((\-?\d+\.?\d*,\-?\d+\.?\d*)( (\-?\d+\.?\d*,\-?\d+\.?\d*))+\)
  1151. $patternWkt = '[a-zA-Z]+\(' . $patternPoints . '\)';
  1152. } else if ('point' == $geomType) {
  1153. // [a-zA-Z]+\(\-?\d\.?\d*,\-?\d\.?\d*\)
  1154. $patternWkt = '[a-zA-Z]+\(' . $patternPoint . '\)';
  1155. }
  1156. $simpleTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:simpleType');
  1157. $restrictionNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:restriction');
  1158. $patternNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:pattern');
  1159. $restrictionNode->setAttribute('base', 'xsd:string');
  1160. $patternNode->setAttribute('value', $patternWkt);
  1161. $restrictionNode->appendChild($patternNode);
  1162. $simpleTypeNode->appendChild($restrictionNode);
  1163. $elNode->appendChild($simpleTypeNode);
  1164. }
  1165. $elNode->setAttribute('nillable', 'true');
  1166. $elNode->setAttribute('minOccurs', '0');
  1167. }
  1168. }
  1169. }
  1170. $attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
  1171. $updateTypeNode->appendChild($attrNode);
  1172. $attrNode->setAttribute('name', 'typeName');
  1173. $attrNode->setAttribute('type', 'xsd:token');
  1174. $attrNode->setAttribute('use', 'required');
  1175. if ($transactionType == 'Update') {
  1176. $attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
  1177. $updateTypeNode->appendChild($attrNode);
  1178. $attrNode->setAttribute('name', 'featureId');
  1179. $attrNode->setAttribute('use', 'required');
  1180. $sTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:simpleType');
  1181. $attrNode->appendChild($sTypeNode);
  1182. $resNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:restriction');
  1183. $sTypeNode->appendChild($resNode);
  1184. $resNode->setAttribute('base', 'xsd:string');
  1185. $patternNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:pattern');
  1186. $resNode->appendChild($patternNode);
  1187. $patternNode->setAttribute('value', '[a-zA-Z_][a-zA-Z0-9_]*\.[0-9]*');
  1188. }
  1189. }
  1190. {// 'Delete'
  1191. $typeName = "p5_{$sourceNs[0]}:{$sourceNs[1]}";
  1192. $acl = $this->getAclFromTypeName($typeName);
  1193. $deleteElementType = "DeleteNs{$nsInd}ElementType";
  1194. $deleteTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexType');
  1195. $rootNode->appendChild($deleteTypeNode);
  1196. $deleteTypeNode->setAttribute('name', $deleteElementType);
  1197. /* <xsd:attribute name="typeName" type="xsd:token" use="required"/> */
  1198. $attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
  1199. $deleteTypeNode->appendChild($attrNode);
  1200. $attrNode->setAttribute('name', 'typeName');
  1201. $attrNode->setAttribute('type', 'xsd:token');
  1202. $attrNode->setAttribute('use', 'required');
  1203. /* <xsd:attribute name="featureId" use="required"> */
  1204. $attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
  1205. $deleteTypeNode->appendChild($attrNode);
  1206. $attrNode->setAttribute('name', 'featureId');
  1207. $attrNode->setAttribute('use', 'required');
  1208. $sTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:simpleType');
  1209. $attrNode->appendChild($sTypeNode);
  1210. $resNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:restriction');
  1211. $sTypeNode->appendChild($resNode);
  1212. $resNode->setAttribute('base', 'xsd:string');
  1213. $patternNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:pattern');
  1214. $resNode->appendChild($patternNode);
  1215. $patternNode->setAttribute('value', '[a-zA-Z_][a-zA-Z0-9_]*\.[0-9]*');
  1216. }
  1217. }
  1218. $validateConvertedTransactionXsdString = $dom->saveXml();
  1219. if($DBG){echo 'L.' . __LINE__ . ' $validateConvertedTransactionXsdString:';print_r($validateConvertedTransactionXsdString);echo "\n";}
  1220. $reqXml = new DOMDocument();
  1221. $reqXml->loadXml($convertedTransaction);
  1222. // TODO: fetch PHP Warning: DOMDocument::schemaValidateSource(): Element 'PARENT_ID': 'abc' is not a valid value of the atomic type 'xs:integer'.
  1223. return $reqXml->schemaValidateSource($validateConvertedTransactionXsdString);
  1224. }
  1225. public function _getDescribeFeatureType($nsPrefix, $type, $simple = true) {
  1226. return $this->_getDescribeFeatureTypes(array(array($nsPrefix, $type)), $simple);
  1227. }
  1228. public function _parseDescribeFeatureTypeRequest($reqContent, $simple = true) {
  1229. $parserXml = xml_parser_create();
  1230. xml_parser_set_option($parserXml, XML_OPTION_CASE_FOLDING, 0);
  1231. xml_parser_set_option($parserXml, XML_OPTION_SKIP_WHITE, 1);
  1232. if (0 == xml_parse_into_struct($parserXml, $reqContent, $tags)) {
  1233. throw new Exception("Error parsing xml");
  1234. }
  1235. xml_parser_free($parserXml);
  1236. if (empty($tags)) {
  1237. throw new Exception("Empty structure from request");
  1238. }
  1239. $rootTagName = V::get('tag', '', $tags[0]);
  1240. if ('DescribeFeatureType' != $rootTagName) {
  1241. throw new Api_WfsException("Wrong xml root tag '{$rootTagName}' #" . __LINE__, 501);
  1242. }
  1243. $requestXmlTags = $tags;
  1244. $DBG = (V::get('DBG_XML', '', $_GET) > 0);// TODO: Profiler
  1245. $rootTagName = V::get('tag', '', $requestXmlTags[0]);
  1246. if ('DescribeFeatureType' != $rootTagName) {
  1247. throw new Exception("Parse Request xml error #" . __LINE__);
  1248. }
  1249. /*[1] => Array(
  1250. [tag] => TypeName
  1251. [type] => complete
  1252. [level] => 2
  1253. [value] => p5_default_db:Rozdzielcza_rurociag_wsg84)
  1254. */
  1255. $typeNames = array();
  1256. $totalTypes = count($requestXmlTags) - 1;
  1257. for ($i = 1; $i < $totalTypes; $i++) {
  1258. if($DBG){echo "TAG[{$i}]:" . json_encode($requestXmlTags[$i]) . "\n";}
  1259. $typeNames[] = explode(':', $requestXmlTags[$i]['value'], 2);
  1260. }
  1261. //echo "typeNames: " . json_encode($typeNames) . "\n";
  1262. return $this->_getDescribeFeatureTypes($typeNames, $simple);
  1263. }
  1264. public function _getDescribeFeatureAllTypes($simple = true) {
  1265. $db = DB::getDB();
  1266. $idDefaultDB = $db->_zasob_id;
  1267. $tblsAcl = $this->_getTableAclList();
  1268. foreach ($tblsAcl as $tblAcl) {
  1269. $dataSourceName = 'default_db';// TODO: getSourceName
  1270. $tblName = $tblAcl->getName();
  1271. $typeNames[] = array("p5_{$dataSourceName}", $tblName);
  1272. }
  1273. return $this->_getDescribeFeatureTypes($typeNames, $simple);
  1274. }
  1275. // @param $typeNames = array( array( $nsPrefix, $type ) )
  1276. public function _getDescribeFeatureTypes($typeNames, $simple = true) {
  1277. if (empty($typeNames)) throw new HttpException("Feature Type Names not defined", 400);
  1278. $this->DBG("types:" . json_encode($typeNames), __LINE__, __FUNCTION__, __CLASS__);
  1279. $baseNsUri = $this->getBaseNamespaceUri();
  1280. $rootWfsNs = 'p5';
  1281. $rootWfsNsUri = "{$baseNsUri}";
  1282. $wfsNs = 'p5_default_db';
  1283. $wfsNsUri = "{$baseNsUri}/default_db";
  1284. $featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&REQUEST=DescribeFeatureType";
  1285. header('Content-type: application/xml');
  1286. // <xsd:import namespace="http://www.opengis.net/gml" schemaLocation="http://webgis.regione.sardegna.it:80/geoserver/schemas/gml/2.1.2/feature.xsd"/>
  1287. // <xsd:element name="{type}" substitutionGroup="gml:_Feature" type="dbu:{typeName}"/>
  1288. $dom = new DOMDocument('1.0', 'utf-8');
  1289. $dom->formatOutput = true;
  1290. $dom->preserveWhiteSpace = false;
  1291. $rootNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:schema');
  1292. $dom->appendChild($rootNode);
  1293. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
  1294. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' . $wfsNs, $wfsNsUri);
  1295. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', "xmlns:{$rootWfsNs}", $rootWfsNsUri);
  1296. $rootNode->setAttribute('elementFormDefault', 'qualified');
  1297. $rootNode->setAttribute('targetNamespace', $wfsNsUri);
  1298. {// <xsd:import namespace="http://www.opengis.net/gml" schemaLocation="...../gml/2.1.2/feature.xsd"/>
  1299. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:import');
  1300. $rootNode->appendChild($elNode);
  1301. $elNode->setAttribute('namespace', "http://www.opengis.net/gml");
  1302. $elNode->setAttribute('schemaLocation', $this->getBaseUri() . "/../../../schema/gml/2.1.2/feature.xsd");
  1303. }
  1304. foreach ($typeNames as $typeNameEx) {
  1305. $nsPrefix = $typeNameEx[0];
  1306. $type = $typeNameEx[1];
  1307. $typeName = "{$nsPrefix}:{$type}";
  1308. $acl = $this->getAclFromTypeName($typeName);
  1309. if (!$this->isAllowedFeatureType($nsPrefix, $type)) {
  1310. throw new Api_WfsException("Could not find type: " . htmlspecialchars($type));
  1311. }
  1312. $typeName = $type . 'Type';
  1313. $fldList = $this->_getFieldListFromAcl($acl);
  1314. $cTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexType');
  1315. $rootNode->appendChild($cTypeNode);
  1316. $cTypeNode->setAttribute('name', $typeName);
  1317. $cConNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexContent');
  1318. $cTypeNode->appendChild($cConNode);
  1319. $extNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:extension');
  1320. $cConNode->appendChild($extNode);
  1321. $extNode->setAttribute('base', 'gml:AbstractFeatureType');
  1322. $seqNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:sequence');
  1323. $extNode->appendChild($seqNode);
  1324. // <xsd:element maxOccurs="1" minOccurs="0" name="{$fldName}" nillable="true" type="xsd:integer"/>
  1325. $pKeyField = $acl->getPrimaryKeyField();
  1326. $p5Attributes = ($simple)? array() : $acl->getAttributesFromZasoby();
  1327. foreach ($fldList as $fldName) {
  1328. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1329. $seqNode->appendChild($elNode);
  1330. $elNode->setAttribute('name', $fldName);
  1331. $minOccurs = 0;
  1332. if ($pKeyField == $fldName) {
  1333. $minOccurs = '1';
  1334. } else {
  1335. $minOccurs = '0';
  1336. }
  1337. $elNode->setAttribute('minOccurs', $minOccurs);
  1338. $elNode->setAttribute('maxOccurs', '1');
  1339. $elNode->setAttribute('nillable', 'true');
  1340. $fldType = 'xsd:string';
  1341. if ($acl->isIntegerField($fldName)) {
  1342. $fldType = 'xsd:integer';
  1343. }
  1344. else if ($acl->isDecimalField($fldName)) {
  1345. $fldType = 'xsd:decimal';
  1346. }
  1347. else if ($acl->isDateField($fldName)) {
  1348. $fldType = 'xsd:date';
  1349. }
  1350. else if ($acl->isDateTimeField($fldName)) {
  1351. $fldType = 'xsd:dateTime';
  1352. }
  1353. else if ($acl->isGeomField($fldName)) {
  1354. //$fldType = 'gml:GeometryPropertyType';
  1355. $geomType = $acl->getGeomFieldType($fldName);
  1356. if ('polygon' == $geomType) {
  1357. $fldType = 'gml:PolygonPropertyType';
  1358. } else if ('point' == $geomType) {
  1359. $fldType = 'gml:PointPropertyType';
  1360. } else if ('linestring' == $geomType) {
  1361. $fldType = 'gml:LineStringPropertyType';
  1362. } else {
  1363. $fldType = 'gml:GeometryPropertyType';
  1364. }
  1365. }
  1366. else if ($acl->isEnumerationField($fldName)) {
  1367. $fldType = ($simple)? 'xsd:string' : "{$nsPrefix}:{$fldName}Type";
  1368. }
  1369. $elNode->setAttribute('type', $fldType);
  1370. if (!$simple) {
  1371. $idZasob = (!empty($p5Attributes[$fldName]['id_zasob']))? (int)$p5Attributes[$fldName]['id_zasob'] : null;
  1372. if ($idZasob > 0) {
  1373. $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:id_zasob", $idZasob);
  1374. }
  1375. if (!empty($p5Attributes[$fldName]['label'])) {
  1376. $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:label", $p5Attributes[$fldName]['label']);
  1377. }
  1378. if (!empty($p5Attributes[$fldName]['description'])) {
  1379. $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:description", $p5Attributes[$fldName]['description']);
  1380. }
  1381. if ($idZasob > 0 && $acl->hasFieldPerm($idZasob, 'W')) {
  1382. $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_write", "true");
  1383. }
  1384. if ($idZasob > 0 && $acl->hasFieldPerm($idZasob, 'C')) {
  1385. $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_create", "true");
  1386. }
  1387. }
  1388. }
  1389. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  1390. $rootNode->appendChild($elNode);
  1391. $elNode->setAttribute('name', $type);
  1392. $elNode->setAttribute('type', $wfsNs . ':' . $typeName);
  1393. if ($simple) {
  1394. $elNode->setAttribute('substitutionGroup', 'gml:_Feature');
  1395. } else {
  1396. $elNode->setAttribute('substitutionGroup', 'gml:AbstractFeature');
  1397. }
  1398. if (!$simple) {
  1399. foreach ($fldList as $fldName) {
  1400. if ($acl->isEnumerationField($fldName)) {
  1401. $enum = $acl->getEnumerations($fldName);
  1402. $stNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:simpleType');
  1403. $stNode->setAttribute('name', "{$fldName}Type");//"{$nsPrefix}:{$fldName}Type");
  1404. $rootNode->appendChild($stNode);
  1405. $resNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:restriction');
  1406. $stNode->appendChild($resNode);
  1407. $resNode->setAttribute('base', 'xsd:string');
  1408. // TODO: if (!empty($p5Attributes[$fldName]['valuesMap'])) -> show only this values
  1409. foreach ($enum as $val => $label) {
  1410. $enumNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:enumeration');
  1411. $resNode->appendChild($enumNode);
  1412. $enumNode->setAttribute('value', "{$val}");
  1413. if (!empty($p5Attributes[$fldName]['valuesMap'][$val])) {
  1414. $enumNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:label", $p5Attributes[$fldName]['valuesMap'][$val]);
  1415. }
  1416. }
  1417. }
  1418. }
  1419. }
  1420. }
  1421. return $dom->saveXML();
  1422. }
  1423. public function _getTableAclList() {// Use only Tables from default_db
  1424. $tblAclList = array();
  1425. $db = DB::getDB();
  1426. $idDefaultDB = $db->_zasob_id;
  1427. $fullTblAclList = $this->_usrAcl->getTablesAcl();
  1428. foreach ($fullTblAclList as $tblAcl) {
  1429. $dataSourceName = 'default_db';// TODO: getSourceName
  1430. $tblName = $tblAcl->getName();
  1431. if ($idDefaultDB != $tblAcl->getDB()) {// hide non default_db tables
  1432. continue;
  1433. }
  1434. try {
  1435. $acl = $this->getAclFromTypeName($typeName = "p5_{$dataSourceName}:{$tblName}");
  1436. } catch (Exception $e) {
  1437. // TODO: error log $e->getMessage();
  1438. }
  1439. if (!$acl) {
  1440. // TODO: error log msg
  1441. continue;
  1442. }
  1443. $tblAclList[] = $tblAcl;
  1444. }
  1445. return $tblAclList;
  1446. }
  1447. public function _getFieldListFromAcl($acl) {
  1448. $fldList = $acl->getRealFieldList();
  1449. return $fldList;
  1450. }
  1451. public function setLogger($logger) {
  1452. $this->_logger = $logger;
  1453. }
  1454. public function DBG($reqLog, $lineNr = null, $funName = null, $className = null) {
  1455. if (!$this->_logger) return;
  1456. $this->_logger->DBG($reqLog, $lineNr, $funName, $className);
  1457. }
  1458. public function convertOgcFilterFromRequestBody($requestOgcFilter) {
  1459. $ogcFilter = '';
  1460. if (empty($requestOgcFilter)) return '';
  1461. {
  1462. $convertOgcFilterXslString .= <<<EOF
  1463. <xsl:transform version="1.0"
  1464. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  1465. xmlns:wfs="http://www.opengis.net/wfs"
  1466. xmlns:ogc="http://www.opengis.net/ogc"
  1467. xmlns:gml="http://www.opengis.net/gml">
  1468. <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
  1469. <xsl:template match="/">
  1470. <xsl:for-each select="//*[local-name() = 'GetFeature']">
  1471. <xsl:copy-of select="ogc:Filter"/>
  1472. </xsl:for-each>
  1473. </xsl:template>
  1474. </xsl:transform>
  1475. EOF;
  1476. DBG::_('DBG_XML', '>2', "convertOgcFilterXslString", $convertOgcFilterXslString, __CLASS__, __FUNCTION__, __LINE__);
  1477. $convertTransactionXsl = new DOMDocument();
  1478. $convertTransactionXsl->loadXml($convertOgcFilterXslString);
  1479. $requestXml = new DOMDocument();
  1480. $requestXml->loadXml($requestOgcFilter);
  1481. $proc = new XSLTProcessor();
  1482. $proc->importStylesheet($convertTransactionXsl);
  1483. $ogcFilter = $proc->transformToXML($requestXml);
  1484. DBG::_('DBG_XML', '>2', "ogcFilter", $ogcFilter, __CLASS__, __FUNCTION__, __LINE__);
  1485. }
  1486. return $ogcFilter;
  1487. }
  1488. }