WfsServerBase.php 82 KB

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