WfsBiAudit.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. class Route_WfsBiAudit extends RouteBase {
  4. const maxResolveDepth = 3;
  5. private $dom, $tablesUsed = [];
  6. private static function output($output) {
  7. header("Content-Type: application/xml");
  8. header("Content-Transfer-Encoding: binary");
  9. header("Content-Length: " . strlen($output));
  10. echo $output;
  11. }
  12. private static function throwServiceException($message) {
  13. $xml = <<<EOT
  14. <?xml version="1.0" encoding="UTF-8"?>
  15. <ServiceExceptionReport xmlns="http://www.opengis.net/ogc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/ogc http://schemas.opengis.net/wfs/1.0.0/OGC-exception.xsd" version="1.2.0">
  16. <ServiceException>{$message}</ServiceException>
  17. </ServiceExceptionReport>
  18. EOT;
  19. self::output($xml);
  20. die();
  21. }
  22. private function addChild($node, $name, $value = null) {
  23. $child = $this->dom->createElement($name, $value);
  24. $node->appendChild($child);
  25. return $child;
  26. }
  27. private function addAttribute($node, $name, $value) {
  28. $attr = $this->dom->createAttribute($name);
  29. $attr->value = $value;
  30. $node->appendChild($attr);
  31. }
  32. private function findRelations($node, $ID, $resolveDepth) {
  33. $query = "select `REMOTE_TABLE`, `REMOTE_ID` from `BI_audit_ALL` where `ID` = {$ID}";
  34. if (!($row = DB::getPDO()->fetchFirst($query))) self::throwServiceException("Błąd danych");
  35. if (!in_array($row['REMOTE_TABLE'], $this->tablesUsed)) $this->tablesUsed[] = $row['REMOTE_TABLE'];
  36. if ($resolveDepth) {
  37. $feature = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
  38. $this->addAttribute($feature, 'fid', "{$row['REMOTE_TABLE']}.{$row['REMOTE_ID']}");
  39. $this->addChild($feature, "default_db__x3A__{$row['REMOTE_TABLE']}:ID", $row['REMOTE_ID']);
  40. $query = "select `ID2` from `BI_audit_ALL_ref` where `ID1` = {$ID}";
  41. $result = DB::getPDO()->fetchAll($query);
  42. foreach ($result as $row) $this->findRelations($feature, $row['ID2'], $resolveDepth - 1);
  43. } else {
  44. $xlink = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
  45. $this->addAttribute($xlink, 'xlink:href', "https://biuro.biall-net.pl/wfs/default_db/{$row['REMOTE_TABLE']}#{$row['REMOTE_TABLE']}.{$row['REMOTE_ID']}");
  46. }
  47. }
  48. public function defaultAction() {
  49. $TYPENAME = V::get('TYPENAME', '', $_GET);
  50. $primaryKey = V::get('primaryKey', 0, $_GET, 'int');
  51. $resolveDepth = V::get('resolveDepth', 1, $_GET, 'int');
  52. $TYPENAME_exploded = explode(":", $TYPENAME);
  53. if (!(count($TYPENAME_exploded) == 2 && $primaryKey && $resolveDepth >= 1 && $resolveDepth <= self::maxResolveDepth)) self::throwServiceException("WFS request error");
  54. $table = $TYPENAME_exploded[1];
  55. try {
  56. $this->dom = new DOMDocument('1.0', 'UTF-8');
  57. $this->dom->preserveWhiteSpace = false;
  58. $this->dom->formatOutput = true;
  59. $attrs = [
  60. 'xmlns:wfs' => 'http://www.opengis.net/wfs',
  61. 'xmlns' => 'http://www.opengis.net/wfs',
  62. 'xmlns:gml' => 'http://www.opengis.net/gml',
  63. 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
  64. 'xmlns:xlink' => 'http://www.w3.org/1999/xlink',
  65. ];
  66. $wfs = $this->addChild($this->dom, 'wfs:FeatureCollection');
  67. foreach ($attrs as $name => $value) $this->addAttribute($wfs, $name, $value);
  68. $gml = $this->addChild($wfs, 'gml:featureMember');
  69. $query = "select `ID` from `BI_audit_ALL` where `REMOTE_TABLE` = " . DB::getPDO()->quote($table) . " and `REMOTE_ID` = {$primaryKey}";
  70. if (!($ID = DB::getPDO()->fetchValue($query))) self::throwServiceException("Błąd danych");
  71. $this->findRelations($gml, $ID, $resolveDepth);
  72. foreach ($this->tablesUsed as $table) $this->addAttribute($wfs, "xmlns:default_db__x3A__{$table}", "https://biuro.biall-net.pl/wfs/default_db/{$table}");
  73. $attrs = [
  74. 'numberMatched' => 'unknown',
  75. 'numberReturned' => '1',
  76. ];
  77. foreach ($attrs as $name => $value) $this->addAttribute($wfs, $name, $value);
  78. $xml = $this->dom->saveXML();
  79. self::output($xml);
  80. } catch (Exception $e) {
  81. self::throwServiceException($e->getMessage());
  82. }
  83. }
  84. }