AclBase.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. <?php
  2. Lib::loadClass('Api_WfsNs');
  3. class Core_AclBase {
  4. public function getSourceName() { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  5. public function getName() { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  6. public function getRootTableName() { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  7. public function getXsdFieldType($fldName) { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  8. // TODO: get more xsd restrictions like minOccurs, maxOccurs, nillable and restrictions
  9. public function getID() { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }// TODO: Legacy
  10. public function init($force = false) { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  11. public function isInitialized() { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  12. public function getRealFieldListByIdZasob() { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  13. public function getFieldIdByName($fieldName) { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  14. public function isIntegerField($fieldName) { return ('xsd:integer' == $this->getXsdFieldType($fldName)); }
  15. public function isDecimalField($fieldName) { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  16. public function isGeomField($fldName) { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  17. public function isDateField($fldName) { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  18. public function isDateTimeField($fldName) { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  19. public function isStringField($fieldName) { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  20. public function isTextField($fldName) { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  21. public function isBinaryField($fieldName) { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  22. public function isEnumerationField($fldName) { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  23. public function getFieldType($colName) { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  24. public function isAllowed($idZasob, $taskPerm, $record = null) { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  25. public function hasFieldPerm($idZasob, $taskPerm) { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  26. public function getItems($params = array()) { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  27. public function addItem($todoItem) { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  28. public function updateItem($itemPatch) { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  29. public function getGeomFieldType($fldName) { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  30. public function getPrimaryKeyField() { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  31. public function getAttributesFromZasoby() { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  32. public function validateFieldAction($fieldName, $perms, $record = null) { throw new HttpException("Acl function " . __FUNCTION__ . " Not implemented", 501); }
  33. public function validateDeleteXml($action) {// @returns action tags, throws Exceptions
  34. $DBG = V::get('DBG_XML', 0, $_GET, 'int');
  35. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " Delete action \$action:";print_r($action);echo "\n";}
  36. if (empty($action['tags'])) throw new Exception("Error Processing Delete Request - wrong Delete tag format");
  37. if ('open' != $action['tags'][0]['type']) throw new Exception("Error Processing Delete Request - wrong Delete tag format");
  38. if ('Filter' != $action['tags'][0]['tag']) throw new Exception("Error Processing Delete Request - wrong Delete tag format");
  39. $filterLvl = $action['tags'][0]['level'];
  40. array_shift($action['tags']);// remove first openig tag Filter
  41. array_pop($action['tags']);// remove last closing tag Filter
  42. if (empty($action['tags'])) throw new Exception("Error Processing Delete Request - missing FeatureId in Delete tag");
  43. $action['Filter'] = array();
  44. $featureName = $this->getName();
  45. foreach ($action['tags'] as $idx => $tag) {
  46. if ($tag['level'] <= $filterLvl) throw new Exception("Error Processing Delete Request - wrong Delete tag format Delete/Filter/{$tag['tag']}[{$idx}]");
  47. if ('FeatureId' != $tag['tag']) throw new Exception("Error Processing Delete Request - wrong Delete tag format - expected 'FeatureId' but '{$tag['tag']}' found");
  48. if (empty($tag['attributes'])) throw new Exception("Error Processing Delete Request - missing attributes in Delete/Filter/FeatureId[{$idx}]");
  49. if (empty($tag['attributes']['fid'])) throw new Exception("Error Processing Delete Request - missing @fid attribute in Delete/Filter/FeatureId[{$idx}]");
  50. $idFeature = $tag['attributes']['fid'];
  51. if ("{$featureName}." != substr($idFeature, 0, strlen("{$featureName}."))) throw new Exception("Error Processing Delete Request - wrong typeName in Delete/Filter/FeatureId[{$idx}]/@fid");
  52. $idFeature = substr($idFeature, strlen("{$featureName}."));
  53. if (!$this->checkPrimaryKeyFormat($idFeature)) throw new Exception("Error Processing Delete Request - wrong primary key format in Delete/Filter/FeatureId[{$idx}]/@fid");
  54. $action['Filter'][] = $idFeature;
  55. }
  56. $action['fields'] = array();
  57. $action['fields']['the_geom'] = array();
  58. $action['fields']['the_geom'][] = array('type'=>'complete', 'value'=>'NULL');
  59. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " Delete action validate return \$action:";print_r($action);echo "\n";}
  60. return $action;
  61. }
  62. public function validateUpdateXml($action) {// @returns action tags, throws Exceptions
  63. $DBG = V::get('DBG_XML', 0, $_GET, 'int');
  64. // split Update tag to Property and Filter
  65. $fieldsLvl = $action['tags'][0]['level'];
  66. $totalTags = count($action['tags']);
  67. $tagsToSplice = array();// args for splice(offset, length to remove, new values)
  68. for ($i = 0, $cnt = $totalTags, $lastIdxToSplice = 0; $i < $cnt; $i++) {
  69. $tag = $action['tags'][$i];
  70. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " Insert to flat fields loop({$i}) \$lastIdxToSplice({$lastIdxToSplice}) \$tag('{$tag['tag']}', '{$tag['type']}', '{$tag['level']}')"."\n";}
  71. if ($fieldsLvl == $tag['level'] && 'complete' == $tag['type']) {// field - flat value
  72. } else if ($fieldsLvl == $tag['level'] && 'open' == $tag['type']) {// field - nested - start
  73. $lastIdxToSplice = $i;
  74. $tagsToSplice[$lastIdxToSplice] = $tag;
  75. $tagsToSplice[$lastIdxToSplice]['action'] = 'Insert -- TODO: L.' . __LINE__;
  76. $tagsToSplice[$lastIdxToSplice]['tags'] = array();
  77. $tagsToSplice[$lastIdxToSplice]['tags'][] = $tag;
  78. } else if (null !== $lastIdxToSplice && $fieldsLvl == $tag['level'] && 'close' == $tag['type']) {// field - nested - end
  79. $tagsToSplice[$lastIdxToSplice]['tags'][] = $tag;
  80. $lastIdxToSplice = null;
  81. } else if (null !== $lastIdxToSplice) {
  82. $tagsToSplice[$lastIdxToSplice]['tags'][] = $tag;
  83. } else {
  84. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " BUG: Update to flat fields loop({$i}) \$tag('{$tag['tag']}', '{$tag['type']}', '{$tag['level']}')"."\n";}
  85. throw new Exception("Error Processing Update Request at Update tag for type '{$action['typeName']}'");
  86. }
  87. }
  88. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . ' TODO: FIX $action:';print_r($action);echo "\n";}
  89. if (empty($tagsToSplice)) throw new Exception("Error Processing Update Request - missing Property or Filter tags");
  90. if (!empty($tagsToSplice)) {// Update
  91. // if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " Insert to flat fields \$tagsToSplice:";print_r($tagsToSplice);echo "\n";}
  92. $tagsToSplice = array_reverse($tagsToSplice, $preserve_keys = true);
  93. foreach ($tagsToSplice as $offset => $childTag) {
  94. $toRemove = count($childTag['tags']);
  95. array_pop($childTag['tags']);// remove last closing tag
  96. $tag = array_shift($childTag['tags']);
  97. $childTag['typeName'] = $action['typeName'];
  98. $childTag['action'] = 'Update -- TODO L.' . __LINE__;
  99. array_splice($action['tags'], $offset, $toRemove, array($childTag));
  100. }
  101. // if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " Insert to flat fields \$action:";print_r($action);echo "\n";}
  102. }
  103. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " Update action after first split \$action:";print_r($action);echo "\n";}
  104. if (count($action['tags']) < 2) throw new Exception("Error Processing Update Request - missing Property or Filter tags");
  105. $filterTag = array_pop($action['tags']);
  106. $action['Filter'] = $this->validateUpdateFilterTag($filterTag);
  107. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " Update action after validate Filter \$action['Filter']:";print_r($action['Filter']);echo "\n";}
  108. $action['fields'] = $this->validateUpdatePropertyTags($action['tags']);
  109. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " Update action after validate Property \$action['fields']:";print_r($action['fields']);echo "\n";}
  110. foreach ($action['fields'] as $fieldName => $propertyTag) {
  111. if (!$this->validateFieldAction($fieldName, 'W')) throw new Api_WfsException("Access Denied to Update field '{$fieldName}' in object '{$action['typeName']}'", __LINE__, null, 'MissingFieldPermWrite', 'request');
  112. $value = $propertyTag['value'];
  113. $this->validateFieldRestrictions($fieldName, $value);
  114. }
  115. return $action;
  116. }
  117. public function validateUpdateFilterTag($filterTag) {// @returns Filter tag (feature id), throws Exceptions
  118. // TODO: allow ogc filter for update multiple rows
  119. $DBG = V::get('DBG_XML', 0, $_GET, 'int');
  120. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " Update validateUpdateFilterTag \$filterTag:";print_r($filterTag);echo "\n";}
  121. if ('Filter' != $filterTag['tag']) throw new Exception("Error Processing Update Request - missing Filter tag");
  122. $idFeature = null;
  123. if (empty($filterTag['tags'])) throw new Exception("Error Processing Update Request - wrong Filter tag format");
  124. if ('FeatureId' != $filterTag['tags'][0]['tag']) throw new Exception("Error Processing Update Request - wrong Filter tag format");
  125. if (empty($filterTag['tags'][0]['attributes']['fid'])) throw new Exception("Error Processing Update Request - missing Filter tag @fid");
  126. $idFeature = $filterTag['tags'][0]['attributes']['fid'];
  127. $featureName = $this->getName();
  128. if ("{$featureName}." != substr($idFeature, 0, strlen("{$featureName}."))) throw new Exception("Error Processing Update Request - wrong typeName in Filter/@fid");
  129. $idFeature = substr($idFeature, strlen("{$featureName}."));
  130. if (!$this->checkPrimaryKeyFormat($idFeature)) throw new Exception("Error Processing Update Request - wrong primary key format in Filter/@fid");
  131. return $idFeature;
  132. }
  133. public function checkPrimaryKeyFormat($idFeature) {// @returns bool
  134. return ((string)$idFeature === (string)intval($idFeature));
  135. }
  136. public function validateUpdatePropertyTags($tags) {// @returns Property tags (fields), throws Exceptions
  137. $DBG = V::get('DBG_XML', 0, $_GET, 'int');
  138. $fields = array();
  139. if($DBG>3){echo 'C.'.get_class($this).' L.' . __LINE__ . " Update validateUpdatePropertyTags \$fields:";print_r($tags);echo "\n";}
  140. foreach ($tags as $idx => $tag) {
  141. if ('Property' != $tag['tag']) throw new Exception("Error Processing Update Request - tag '{$tag['tag']}' is not allowed in Update tag");
  142. if (count($tag['tags']) < 2) throw new Exception("Error Processing Update Request - wrong format in Update/Property[{$idx}]");
  143. $tagName = array_shift($tag['tags']);
  144. if ('Name' != $tagName['tag']) throw new Exception("Error Processing Update Request - missing Name tag in Update/Property[{$idx}]");
  145. if (empty($tagName['value'])) throw new Exception("Error Processing Update Request - empty field name in Update/Property[{$idx}]");
  146. if (false !== strpos($tagName['value'], '/')) throw new Exception("Error Processing Update Request - xpath in Update/Property[{$idx}] field name not implemented", 501);
  147. // TODO: check field name as xpath, eg. File/content
  148. $fieldName = $tagName['value'];
  149. $tagValue = array_shift($tag['tags']);
  150. $fieldType = $this->getXsdFieldType($fieldName);
  151. if ('Value' != $tagValue['tag']) throw new Exception("Error Processing Update Request - missing Value tag in Update/Property[{$idx}]");
  152. if ('open' == $tagValue['type']) {
  153. array_pop($tag['tags']);
  154. $tagValue['tags'] = $tag['tags'];
  155. if (empty($tagValue['tags'])) throw new Exception("Error Processing Update Request - wrong Value tag format in Update/Property[{$idx}]");
  156. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " F.".__FUNCTION__." validate fields loop({$idx}) loop({$fieldName}) \$fieldType({$fieldType})"."\n";}
  157. if ('gml:' == substr($fieldType, 0, 4)) {
  158. $tagValue['value'] = $this->convertGmlTagsToWkt($fieldType, $tagValue['tags']);
  159. } else {
  160. // TODO: return $fields[$fieldName][] = array('type'=>'open', 'typeName'=>$fieldType, 'tags'=>...
  161. throw new Exception("Error Processing Update Request - wrong Value tag format for field '{$fieldName}' (Update/Property[{$idx}])");
  162. }
  163. } else if ('complete' == $tagValue['type']) {
  164. if (!empty($tag['tags'])) throw new Exception("Error Processing Update Request - wrong Value tag format in Update/Property[{$idx}]");
  165. } else throw new Exception("Error Processing Update Request - missing Value tag in Update/Property[{$idx}]");
  166. $value = V::get('value', '', $tagValue);
  167. $fields[$fieldName][] = array('type'=>'complete', 'value'=>$value);
  168. }
  169. return $fields;
  170. }
  171. public function validateInsertXml($action) {// @returns action tag, throws Exceptions
  172. $DBG = V::get('DBG_XML', 0, $_GET, 'int');
  173. // if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " TODO: F.".__FUNCTION__." \$action:";print_r($action);echo "\n";}
  174. // split Insert tags by first level fields - nested structures like gml (the_geom)
  175. $fieldsLvl = $action['tags'][0]['level'];
  176. $totalTags = count($action['tags']);
  177. $tagsToSplice = array();// args for splice(offset, length to remove, new values)
  178. for ($i = 0, $cnt = $totalTags, $lastIdxToSplice = 0; $i < $cnt; $i++) {
  179. $tag = $action['tags'][$i];
  180. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " Insert to flat fields loop({$i}) \$lastIdxToSplice({$lastIdxToSplice}) \$tag('{$tag['tag']}', '{$tag['type']}', '{$tag['level']}')"."\n";}
  181. if ($fieldsLvl == $tag['level'] && 'complete' == $tag['type']) {// field - flat value
  182. } else if ($fieldsLvl == $tag['level'] && 'open' == $tag['type']) {// field - nested - start
  183. $lastIdxToSplice = $i;
  184. $tagsToSplice[$lastIdxToSplice] = $tag;
  185. $tagsToSplice[$lastIdxToSplice]['action'] = 'Insert';
  186. $tagsToSplice[$lastIdxToSplice]['tags'] = array();
  187. $tagsToSplice[$lastIdxToSplice]['tags'][] = $tag;
  188. } else if (null !== $lastIdxToSplice && $fieldsLvl == $tag['level'] && 'close' == $tag['type']) {// field - nested - end
  189. $tagsToSplice[$lastIdxToSplice]['tags'][] = $tag;
  190. $lastIdxToSplice = null;
  191. } else if (null !== $lastIdxToSplice) {
  192. $tagsToSplice[$lastIdxToSplice]['tags'][] = $tag;
  193. } else {
  194. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " BUG: Insert to flat fields loop({$i}) \$tag('{$tag['tag']}', '{$tag['type']}', '{$tag['level']}')"."\n";}
  195. throw new Exception("Error Processing Request at Insert tag for type '{$action['typeName']}'");
  196. }
  197. }
  198. // if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . ' TODO: FIX $action:';print_r($action);echo "\n";}
  199. if (!empty($tagsToSplice)) {// Insert deep object
  200. // if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " Insert to flat fields \$tagsToSplice:";print_r($tagsToSplice);echo "\n";}
  201. $tagsToSplice = array_reverse($tagsToSplice, $preserve_keys = true);
  202. foreach ($tagsToSplice as $offset => $childTag) {
  203. $toRemove = count($childTag['tags']);
  204. array_pop($childTag['tags']);// remove last closing tag
  205. $tag = array_shift($childTag['tags']);
  206. $childTag['action'] = 'Insert';
  207. {// TODO: duplicate code - get prefix for typeName
  208. $typeName = $tag['tag'];
  209. if (false === strpos($typeName, ':')) {
  210. $nsType = V::get('xmlns', '', $tag['attributes']);
  211. if (!$nsType) throw new Exception("Error Processing Request - Missing object namespace '{$tag['tag']}'");
  212. $prefix = Api_WfsNs::getNsPrefix($nsType);
  213. if (!$prefix) {
  214. if ($typeName == substr(rtrim($nsType, '/'), -1 * strlen($typeName))) {// typeName may be added to ns uri
  215. $nsBaseForType = substr(rtrim($nsType, '/'), 0, -1 * strlen($typeName) - 1);
  216. $prefix = Api_WfsNs::getNsPrefix($nsBaseForType);
  217. }
  218. }
  219. if (!$prefix) throw new Exception("Error Processing Request - Unrecognized namespace uri '{$nsType}' for object '{$tag['tag']}'");
  220. $typeName = "{$prefix}:{$typeName}";
  221. }
  222. $childTag['typeName'] = $typeName;
  223. }
  224. array_splice($action['tags'], $offset, $toRemove, array($childTag));
  225. $action['isDeepObject'] = true;
  226. }
  227. // if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " Insert to flat fields \$action:";print_r($action);echo "\n";}
  228. }
  229. // TODO: validate sequence order from schema - needed?
  230. // validate fields
  231. $action['fields'] = array();
  232. foreach ($action['tags'] as $idx => $tag) {
  233. $fieldName = $tag['tag'];
  234. if (false !== strpos($fieldName, ':')) $fieldName = substr($fieldName, strpos($fieldName, ':') + 1);
  235. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " F.".__FUNCTION__." validate fields loop({$idx}) \$fieldName:";print_r($fieldName);echo "\n";}
  236. $field = array();
  237. $field['tag'] = $tag['tag'];
  238. $field['type'] = $tag['type'];
  239. if (!empty($tag['typeName'])) $field['typeName'] = $tag['typeName'];
  240. if (!empty($tag['action'])) $field['action'] = $tag['action'];
  241. if (!empty($tag['value'])) $field['value'] = $tag['value'];
  242. if (!empty($tag['tags'])) $field['tags'] = $tag['tags'];
  243. $action['fields'][$fieldName][] = $field;
  244. }
  245. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " F.".__FUNCTION__." validate fields loop({$idx}) \$action['fields']:";print_r($action['fields']);echo "\n";}
  246. foreach ($action['fields'] as $fieldName => $childFields) {
  247. foreach ($childFields as $idx => $field) {
  248. // TODO: validate minOccurs, maxOccurs
  249. }
  250. }
  251. $fieldPK = $this->getPrimaryKeyField();
  252. foreach ($action['fields'] as $fieldName => $childFields) {
  253. if ($fieldName == $fieldPK) continue;
  254. foreach ($childFields as $idx => $field) {
  255. if (!$this->validateFieldAction($fieldName, 'C')) throw new Api_WfsException("Access Denied to Create field '{$fieldName}' in object '{$action['typeName']}'", __LINE__, null, 'MissingFieldPermCreate', 'request');
  256. if ('open' == $field['type']) {
  257. $fieldType = $this->getXsdFieldType($fieldName);
  258. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " F.".__FUNCTION__." validate fields loop({$idx}) loop({$fieldName}) \$fieldType({$fieldType})"."\n";}
  259. if ('gml:' == substr($fieldType, 0, 4)) {
  260. $action['fields'][$fieldName][$idx]['value'] = $this->convertGmlTagsToWkt($fieldType, $field['tags']);
  261. unset($action['fields'][$fieldName][$idx]['tags']);
  262. unset($action['fields'][$fieldName][$idx]['typeName']);
  263. unset($action['fields'][$fieldName][$idx]['action']);
  264. $action['fields'][$fieldName][$idx]['type'] = 'complete';
  265. } else if ('p5_objects:' == substr($fieldType, 0, 11)) {
  266. // TODO: read value recursive? (like geom?)
  267. } else if ('ref:p5_objects:' == substr($fieldType, 0, 15)) {
  268. // TODO: read value recursive? - validate recursive
  269. }
  270. }
  271. $this->validateFieldRestrictions($fieldName, $value);
  272. }
  273. }
  274. // TODO: validate nillable
  275. // TODO: validate recursive fields with type 'p5_objects:*' (without 'value')
  276. foreach ($action['fields'] as $fieldName => $childFields) {
  277. foreach ($childFields as $idx => $field) {
  278. if ('open' == $field['type']) {
  279. $fieldType = $this->getXsdFieldType($fieldName);
  280. if ('ref:p5_objects:' == substr($fieldType, 0, 15)) {
  281. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " F.".__FUNCTION__." TODO: -------> validateInsertXml(\$field) \$field:";print_r($field);echo "\n";}
  282. $acl = $this->getAclFromTypeName(substr($fieldType, 4));
  283. $action['fields'][$fieldName][$idx] = $acl->validateInsertXml($field);
  284. }
  285. }
  286. }
  287. }
  288. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " F.".__FUNCTION__." return ============ \$action:";print_r($action);echo "\n";}
  289. return $action;
  290. }
  291. public function validateFieldRestrictions($fieldName, $value) {// @returns null, throws Exceptions
  292. // TODO: restrictions
  293. }
  294. public function checkFieldRestrictions($fieldName, $value) {// @returns array of error msgs
  295. // TODO: restrictions
  296. }
  297. // like Api_WfsGeomTypeConverter::convertGmlCoordinatesToWkt($gmlCoordinates) where $gmlCoordinates is from converted wfs request
  298. public function convertGmlTagsToWkt($fieldType, $tags) {
  299. $DBG = V::get('DBG_XML', 0, $_GET, 'int');
  300. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " TODO: F.".__FUNCTION__." \$fieldType({$fieldType}) \$tags:";print_r($tags);echo "\n";}
  301. $cs = null; $ts = null; $value = null; $wktType = null;
  302. if ('gml:LineStringPropertyType' == $fieldType) {
  303. // <gml:LineString srsName="EPSG:4326">
  304. // <gml:coordinates cs="," ts=" ">18.25240580856418049,54.48879768607960017 18.27014261382555915,54.46219247818753217</gml:coordinates>
  305. // </gml:LineString>
  306. if (3 != count($tags)) throw new Exception("#L".__LINE__." Error Processing Request - expected type 'gml:LineString'");
  307. if ('gml:LineString' !== $tags[0]['tag']) throw new Exception("#L".__LINE__." Error Processing Request - expected type 'gml:LineString'");
  308. if ('open' !== $tags[0]['type']) throw new Exception("#L".__LINE__." Error Processing Request - expected type 'gml:LineString'");
  309. if (empty($tags[1]['attributes']['cs'])) throw new Exception("#L".__LINE__." Error Processing Request - expected type 'gml:LineString'");
  310. if (empty($tags[1]['attributes']['ts'])) throw new Exception("#L".__LINE__." Error Processing Request - expected type 'gml:LineString'");
  311. if (empty($tags[1]['value'])) throw new Exception("#L".__LINE__." Error Processing Request - expected type 'gml:LineString'");
  312. $cs = $tags[1]['attributes']['cs'];
  313. $ts = $tags[1]['attributes']['ts'];
  314. $value = $tags[1]['value'];
  315. $wktType = 'LINESTRING';
  316. } else if ('gml:PointPropertyType' == $fieldType) {
  317. // <gml:Point srsDimension="1" srsName="http://www.opengis.net/gml/srs/epsg.xml#3857">
  318. // <gml:coordinates decimal="." cs="," ts=" ">-33.7291,-56.3353820</gml:coordinates>
  319. // </gml:Point>
  320. // TODO: @decimal
  321. if (3 != count($tags)) throw new Exception("#L".__LINE__." Error Processing Request - expected type 'gml:Point'");
  322. if ('gml:Point' !== $tags[0]['tag']) throw new Exception("#L".__LINE__." Error Processing Request - expected type 'gml:Point'");
  323. if ('open' !== $tags[0]['type']) throw new Exception("#L".__LINE__." Error Processing Request - expected type 'gml:Point'");
  324. if (empty($tags[1]['attributes']['cs'])) throw new Exception("#L".__LINE__." Error Processing Request - expected type 'gml:Point'");
  325. if (empty($tags[1]['attributes']['ts'])) throw new Exception("#L".__LINE__." Error Processing Request - expected type 'gml:Point'");
  326. if (empty($tags[1]['value'])) throw new Exception("#L".__LINE__." Error Processing Request - expected type 'gml:Point'");
  327. $cs = $tags[1]['attributes']['cs'];
  328. $ts = $tags[1]['attributes']['ts'];
  329. $value = $tags[1]['value'];
  330. $wktType = 'POINT';
  331. } else if ('gml:PolygonPropertyType' == $fieldType) {
  332. if (7 != count($tags)) throw new Exception("#L".__LINE__." Error Processing Request - expected type 'gml:Polygon'");
  333. if ('gml:Polygon' !== $tags[0]['tag']) throw new Exception("#L".__LINE__." Error Processing Request - expected type 'gml:Polygon'");
  334. if ('open' !== $tags[0]['type']) throw new Exception("#L".__LINE__." Error Processing Request - expected type 'gml:Polygon'");
  335. if ('gml:outerBoundaryIs' !== $tags[1]['tag']) throw new Exception("#L".__LINE__." Error Processing Request - expected type 'gml:Polygon'");
  336. if ('gml:LinearRing' !== $tags[2]['tag']) throw new Exception("#L".__LINE__." Error Processing Request - expected type 'gml:Polygon'");
  337. if ('gml:coordinates' !== $tags[3]['tag']) throw new Exception("#L".__LINE__." Error Processing Request - expected type 'gml:Polygon'");
  338. if (empty($tags[3]['attributes']['cs'])) throw new Exception("#L".__LINE__." Error Processing Request - expected type 'gml:Polygon'");
  339. if (empty($tags[3]['attributes']['ts'])) throw new Exception("#L".__LINE__." Error Processing Request - expected type 'gml:Polygon'");
  340. if (empty($tags[3]['value'])) throw new Exception("#L".__LINE__." Error Processing Request - expected type 'gml:Polygon'");
  341. $cs = $tags[3]['attributes']['cs'];
  342. $ts = $tags[3]['attributes']['ts'];
  343. $value = $tags[3]['value'];
  344. $wktType = 'POLYGON';
  345. } else {
  346. throw new Exception("Error Processing Request - type '{$fieldType}' not supported");
  347. }
  348. $wkt = $value;
  349. $wkt = str_replace(array($cs, $ts), array('#cs#', '#ts#'), $wkt);
  350. $wkt = str_replace(array('#cs#', '#ts#'), array(' ', ','), $wkt);
  351. if ('POLYGON' == $wktType) $wkt = "({$wkt})";// POINT(1 1), LINESTRING(0 0,1 1,2 2), POLYGON((0 0,10 0,10 10,0 10,0 0))
  352. return "{$wktType}({$wkt})";
  353. }
  354. public function getAclFromTypeName($typeName) {// TODO: mv to another class, duplicate from Api_WfsServerBase::getAclFromTypeName($typeName)
  355. $typeEx = explode(':', $typeName);
  356. if (2 != count($typeEx)) throw new Api_WfsException("Could not get acl for '{$typeName}' - syntax error");
  357. if ('p5_' != substr($typeEx[0], 0, 3)) throw new Api_WfsException("Could not get acl for '{$typeName}' - prefix error");
  358. $sourceName = substr($typeEx[0], 3);
  359. $objName = $typeEx[1];
  360. $acl = User::getAcl()->getObjectAcl($sourceName, $objName);
  361. if (!$acl) throw new Api_WfsException("Could not get acl for '{$typeName}'");
  362. $forceTblAclInit = 0;//('1' == V::get('_force', '', $_GET));
  363. $acl->init($forceTblAclInit);
  364. return $acl;
  365. }
  366. public function insertXml($action) {// @param $action = [ 'typeName' => '*:*', 'fields'=>[ `name`=>[`fld`, `fld`, ...], ... ] ]
  367. $DBG = V::get('DBG_XML', 0, $_GET, 'int');
  368. if (empty($action['typeName'])) throw new Exception("Error Processing Request - missing typeName", 500);
  369. if (empty($action['fields'])) throw new Exception("Error Processing Request - missing fields for type '{$action['typeName']}'", 500);
  370. {// Insert with primaryKey -> Update
  371. $fieldPK = $this->getPrimaryKeyField();
  372. if (array_key_exists($fieldPK, $action['fields'])) {
  373. $pk = $action['fields'][$fieldPK][0]['value'];
  374. $action['Filter'] = $pk;
  375. $affected = $this->updateXml($action);
  376. return ($affected >= 0) ? $pk : -1;
  377. }
  378. }
  379. $sqlInsert = array();
  380. $sqlChildInsert = array();
  381. foreach ($action['fields'] as $fieldName => $childFields) {
  382. foreach ($childFields as $idx => $field) {
  383. if ('complete' == $field['type']) {
  384. $sqlInsert[$fieldName][$idx] = $field['value'];
  385. } else if ('open' == $field['type']) {
  386. $childAcl = $this->getAclFromTypeName($field['typeName']);// TODO: or $fieldType = $this->getXsdFieldType($fieldName);// TODO: ref:p5_objects:File
  387. $sqlChildInsert[$fieldName][$idx] = $childAcl->insertXml($field);
  388. } else throw new Exception("Error Processing Request - BUG Unrecognized type '{$field['type']}'", 500);
  389. }
  390. }
  391. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " F.".__FUNCTION__." \$sqlInsert:";print_r($sqlInsert);echo "\n";}
  392. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " F.".__FUNCTION__." \$sqlChildInsert:";print_r($sqlChildInsert);echo "\n";}
  393. $sqlObj = array();
  394. foreach ($sqlInsert as $fieldName => $childFields) {
  395. $value = $childFields[0];
  396. // foreach ($childFields as $idx => $value) // TODO: check maxOcures
  397. if ($this->isGeomField($fieldName)) $value = "GeomFromText('{$value}')";
  398. $sqlObj["{$fieldName}"] = $value;
  399. }
  400. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " F.".__FUNCTION__." addItem(\$sqlObj) \$sqlObj:";print_r($sqlObj);echo "\n";}
  401. $insertedId = $this->addItem($sqlObj);
  402. foreach ($sqlChildInsert as $fieldName => $childFields) {
  403. foreach ($childFields as $idx => $childInsertedId) {
  404. $childAcl = $this->getAclFromTypeName($action['fields'][$fieldName][$idx]['typeName']);// TODO: or $fieldType = $this->getXsdFieldType($fieldName);// TODO: ref:p5_objects:File
  405. $this->_insertRef($this->getRootTableName(), $childAcl->getRootTableName(), $insertedId, $childInsertedId);
  406. }
  407. }
  408. return $insertedId;
  409. }
  410. public function _insertRef($tableName, $childTableName, $pk, $childPk) {// TODO: $idTransaction
  411. $refTable = $this->createRefTable($tableName, $childTableName);
  412. $sqlPk = DB::getPDO()->quote($pk, PDO::PARAM_STR);
  413. $sqlChildPk = DB::getPDO()->quote($childPk, PDO::PARAM_STR);
  414. DB::getPDO()->exec("
  415. insert into `{$refTable}` (`PRIMARY_KEY`, `REMOTE_PRIMARY_KEY`)
  416. values ({$sqlPk}, {$sqlChildPk})
  417. ");
  418. }
  419. public function createRefTable($tableName, $childTableName) {// TODO: $idTransaction
  420. static $tables = array();
  421. $refTable = "{$tableName}__#REF__{$childTableName}";
  422. if (array_key_exists($refTable, $tables)) return $refTable;
  423. DB::getPDO()->exec("
  424. CREATE TABLE IF NOT EXISTS `{$refTable}` (
  425. `PRIMARY_KEY` int(11) NOT NULL,
  426. `REMOTE_PRIMARY_KEY` int(11) NOT NULL,
  427. -- TODO `TRANACTION_ID` int(11) NOT NULL
  428. KEY `PRIMARY_KEY` (`PRIMARY_KEY`),
  429. KEY `REMOTE_PRIMARY_KEY` (`REMOTE_PRIMARY_KEY`)
  430. ) ENGINE=MyISAM DEFAULT CHARSET=latin2;
  431. ");
  432. $tables[$refTable] = true;
  433. return $refTable;
  434. }
  435. public function updateXml($action) {// @param $action = [ 'typeName' => '*:*', 'Filter'=>int, 'fields'=>[ `name`=>[`fld`, `fld`, ...], ... ] ]
  436. $DBG = V::get('DBG_XML', 0, $_GET, 'int');
  437. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " updateXml action \$action:";print_r($action);echo "\n";}
  438. if (empty($action['typeName'])) throw new Exception("Error Processing Request - missing typeName", 500);
  439. if (empty($action['Filter'])) throw new Exception("Error Processing Request - missing Filter for type '{$action['typeName']}'", 500);
  440. if (!$this->checkPrimaryKeyFormat($action['Filter'])) throw new Exception("Error Processing Request - wrong Filter format for type '{$action['typeName']}'", 501);
  441. if (empty($action['fields'])) throw new Exception("Error Processing Request - missing Property for type '{$action['typeName']}'", 500);
  442. // TODO: check acl user to update record ID = $action['Filter']
  443. $itemPatch = array();
  444. foreach ($action['fields'] as $fieldName => $childFields) {
  445. foreach ($childFields as $idx => $field) {
  446. if ('complete' != $field['type']) continue;// skip child nodes, REF
  447. $value = $field['value'];
  448. if ($this->isGeomField($fieldName)) $value = "GeomFromText('{$value}')";
  449. $itemPatch[$fieldName] = $value;
  450. }
  451. }
  452. $itemPatch[$this->getPrimaryKeyField()] = $action['Filter'];
  453. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " updateXml action \$itemPatch:";print_r($itemPatch);echo "\n";}
  454. $affected = $this->updateItem($itemPatch);
  455. // TODO: update/insert child nodes, REF
  456. return $affected;
  457. }
  458. public function deleteXml($action) {// @param $action = [ 'typeName' => '*:*', 'Filter'=>[int, ...], 'fields'=>[ `name`=>[`fld`, `fld`, ...], ... ] ]
  459. $DBG = V::get('DBG_XML', 0, $_GET, 'int');
  460. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " updateXml action \$action:";print_r($action);echo "\n";}
  461. if (empty($action['typeName'])) throw new Exception("Error Processing Request - missing typeName", 500);
  462. if (!is_array($action['Filter']) || empty($action['Filter'])) throw new Exception("Error Processing Request - missing Filter for type '{$action['typeName']}'", 500);
  463. foreach ($action['Filter'] as $fid) {
  464. if (!$this->checkPrimaryKeyFormat($fid)) throw new Exception("Error Processing Request - wrong Filter format for type '{$action['typeName']}'", 501);
  465. }
  466. if (empty($action['fields'])) throw new Exception("Error Processing Request - missing Property for type '{$action['typeName']}'", 500);
  467. // TODO: check acl user to update record ID = $action['Filter']
  468. $itemPatch = array();
  469. foreach ($action['fields'] as $fieldName => $childFields) {
  470. foreach ($childFields as $idx => $field) {
  471. if ('complete' != $field['type']) continue;// skip child nodes, REF
  472. $value = $field['value'];
  473. if ($this->isGeomField($fieldName)) $value = "GeomFromText('{$value}')";
  474. $itemPatch[$fieldName] = $value;
  475. }
  476. }
  477. $fieldPK = $this->getPrimaryKeyField();
  478. $deleted = 0;
  479. foreach ($action['Filter'] as $fid) {
  480. $itemPatch[$fieldPK] = $fid;
  481. if($DBG){echo 'C.'.get_class($this).' L.' . __LINE__ . " updateXml action \$itemPatch:";print_r($itemPatch);echo "\n";}
  482. $affected = $this->updateItem($itemPatch);
  483. if ($affected >= 0) $deleted += 1;
  484. }
  485. // TODO: update/insert child nodes, REF
  486. return $deleted;
  487. }
  488. }