/**
  * Parses the capabilities document for the WFS 
  * version number and returns it.
  * 
  * @return String
  * @param $xml String
  */
 private function getVersionFromXml($xml)
 {
     $admin = new administration();
     $values = $admin->parseXml($xml);
     foreach ($values as $element) {
         if ($this->sepNameSpace(strtoupper($element[tag])) == "WFS_CAPABILITIES" && $element[type] == "open") {
             return $element[attributes][version];
         }
     }
     throw new Exception("WFS version could not be determined from XML.");
 }
 /**
  * Creates WFS 1.0 objects from a capabilities documents.
  * 
  * @return Wfs_1_0
  * @param $xml String
  */
 public function createFromXml($xml)
 {
     try {
         $myWfs = new Wfs_1_0();
         $admin = new administration();
         $values = $admin->parseXml($xml);
         $myWfs->getCapabilitiesDoc = $admin->char_encode($xml);
         $myWfs->id = $this->createId();
         foreach ($values as $element) {
             $tag = strtoupper($element[tag]);
             if ($tag == "WFS_CAPABILITIES" && $element[type] == "open") {
                 $myWfs->version = $element[attributes][version];
                 if ($myWfs->version !== "1.0.0") {
                     throw new Exception("Not a WFS 1.0.0 capabilities document.");
                 }
             }
             if ($tag == "NAME" && $element[level] == '3') {
                 $myWfs->name = $element[value];
             }
             if ($tag == "TITLE" && $element[level] == '3') {
                 $myWfs->title = $this->stripEndlineAndCarriageReturn($element[value]);
             }
             if ($tag == "ABSTRACT" && $element[level] == '3') {
                 $myWfs->summary = $this->stripEndlineAndCarriageReturn($element[value]);
             }
             if ($tag == "FEES") {
                 $myWfs->fees = $element[value];
             }
             if ($tag == "ACCESSCONSTRAINTS") {
                 $myWfs->accessconstraints = $element[value];
             }
             # getCapabilities
             if ($tag == "GETCAPABILITIES" && $element[type] == "open") {
                 $section = "getcapabilities";
             }
             if ($section == "getcapabilities" && $tag == "GET") {
                 $myWfs->getCapabilities = $element[attributes][onlineResource];
             }
             # descriptFeatureType
             if ($tag == "DESCRIBEFEATURETYPE" && $element[type] == "open") {
                 $section = "describefeaturetype";
                 $myWfs->describeFeatureType = $element[attributes][onlineResource];
             }
             if ($section == "describefeaturetype" && $tag == "POST") {
                 $myWfs->describeFeatureType = $element[attributes][onlineResource];
             }
             # getFeature
             if ($tag == "GETFEATURE" && $element[type] == "open") {
                 $section = "getfeature";
             }
             if ($section == "getfeature" && $tag == "POST") {
                 $myWfs->getFeature = $element[attributes][onlineResource];
             }
             if ($tag == "GETFEATURE" && $element[type] == "close") {
                 $section = "";
             }
             # transaction
             if ($tag == "TRANSACTION" && $element[type] == "open") {
                 $section = "transaction";
             }
             if ($section == "transaction" && $tag == "POST") {
                 $myWfs->transaction = $element[attributes][onlineResource];
             }
             if ($tag == "TRANSACTION" && $element[type] == "close") {
                 $section = "";
             }
             if ($tag == "FEATURETYPE" && $element[type] == "open") {
                 $section = "featuretype";
             }
             if ($section == "featuretype" && $tag == "NAME") {
                 $featuretype_name = $element[value];
             }
             if ($section == "featuretype" && $tag == "TITLE") {
                 $featuretype_title = $this->stripEndlineAndCarriageReturn($element[value]);
             }
             if ($section == "featuretype" && $tag == "ABSTRACT") {
                 $featuretype_abstract = $element[value];
             }
             if ($section == "featuretype" && $tag == "SRS") {
                 $featuretype_srs = $element[value];
                 // Do not add defective featuretypes
                 try {
                     $currentFeatureType = $this->createFeatureTypeFromUrl($myWfs, $featuretype_name);
                     if ($currentFeatureType !== null) {
                         $currentFeatureType->name = $featuretype_name;
                         $currentFeatureType->title = $featuretype_title;
                         $currentFeatureType->summary = $featuretype_abstract;
                         $currentFeatureType->srs = $featuretype_srs;
                         $myWfs->addFeatureType($currentFeatureType);
                     }
                 } catch (Exception $e) {
                     new mb_exception("Failed to load featuretype " . $featuretype_name);
                 }
             }
         }
         return $myWfs;
     } catch (Exception $e) {
         $e = new mb_exception($e);
         return null;
     }
 }
 /**
  * Loads a WMC from an actual WMC XML document.
  * Uses WMS class.
  *
  * @param string $data the data from the XML file
  */
 protected function createObjFromWMC_xml($data)
 {
     // store xml
     $this->xml = $data;
     $values = administration::parseXml($data);
     if (!$values) {
         throw new Exception("WMC document could not be parsed.");
     }
     //
     // Local variables that indicate which section of the WMC
     // is currently parsed.
     //
     $extension = false;
     $general = false;
     $layerlist = false;
     $layer = false;
     $formatlist = false;
     $dataurl = false;
     $metadataurl = false;
     $stylelist = false;
     //
     // reset WMC data
     //
     $this->mainMap = new Map();
     $this->overviewMap = null;
     $this->generalExtensionArray = array();
     $layerlistArray = array();
     $layerlistArray["main"] = array();
     $layerlistArray["overview"] = array();
     foreach ($values as $element) {
         $tag = strtoupper(administration::sepNameSpace($element[tag]));
         $tagLowerCase = administration::sepNameSpace($element[tag]);
         $type = $element[type];
         $attributes = $element[attributes];
         $value = mb_utf8_decode(html_entity_decode($element[value]));
         if ($tag == "VIEWCONTEXT" && $type == "open") {
             $this->wmc_id = $attributes["id"];
             $this->wmc_version = $attributes["version"];
         }
         if ($tag == "GENERAL" && $type == "open") {
             $general = true;
         }
         if ($tag == "LAYERLIST" && $type == "open") {
             $layerlist = true;
         }
         if ($general) {
             if ($tag == "WINDOW") {
                 $this->mainMap->setWidth($attributes["width"]);
                 $this->mainMap->setHeight($attributes["height"]);
             }
             if ($tag == "BOUNDINGBOX") {
                 $bbox = new Mapbender_bbox($attributes["minx"], $attributes["miny"], $attributes["maxx"], $attributes["maxy"], $attributes["SRS"]);
                 $this->mainMap->setExtent($bbox);
             }
             if ($tag == "NAME") {
                 $this->wmc_name = $value;
             }
             if ($tag == "TITLE") {
                 $this->wmc_title = $value;
             }
             if ($tag == "ABSTRACT") {
                 $this->wmc_abstract = $value;
             }
             if ($tag == "CONTACTINFORMATION" && $type == "open") {
                 $contactinformation = true;
             }
             if ($contactinformation) {
                 if ($tag == "CONTACTPOSITION") {
                     $this->wmc_contactposition = $value;
                 }
                 if ($tag == "CONTACTVOICETELEPHONE") {
                     $this->wmc_contactvoicetelephone = $value;
                 }
                 if ($tag == "CONTACTFACSIMILETELEPHONE") {
                     $this->wmc_contactfacsimiletelephone = $value;
                 }
                 if ($tag == "CONTACTELECTRONICMAILADDRESS") {
                     $this->wmc_contactemail = $value;
                 }
                 if ($tag == "CONTACTPERSONPRIMARY" && $type == "open") {
                     $contactpersonprimary = true;
                 }
                 if ($contactpersonprimary) {
                     if ($tag == "CONTACTPERSON") {
                         $this->wmc_contactperson = $value;
                     }
                     if ($tag == "CONTACTORGANIZATION") {
                         $this->wmc_contactorganization = $value;
                     }
                     if ($tag == "CONTACTPERSONPRIMARY" && $type == "close") {
                         $contactpersonprimary = false;
                     }
                 }
                 if ($tag == "CONTACTADDRESS" && $type == "open") {
                     $contactaddress = true;
                 }
                 if ($contactaddress) {
                     if ($tag == "ADDRESSTYPE") {
                         $this->wmc_contactaddresstype = $value;
                     }
                     if ($tag == "ADDRESS") {
                         $this->wmc_contactaddress = $value;
                     }
                     if ($tag == "CITY") {
                         $this->wmc_contactcity = $value;
                     }
                     if ($tag == "STATEORPROVINCE") {
                         $this->wmc_contactstateorprovince = $value;
                     }
                     if ($tag == "POSTCODE") {
                         $this->wmc_contactpostcode = $value;
                     }
                     if ($tag == "COUNTRY") {
                         $this->wmc_contactcountry = $value;
                     }
                     if ($tag == "CONTACTADDRESS" && $type == "close") {
                         $contactaddress = false;
                     }
                 }
             }
             if ($tag == "LOGOURL" && $type == "open") {
                 $logourl = true;
                 $this->wmc_logourl_width = $attributes["width"];
                 $this->wmc_logourl_height = $attributes["height"];
                 $this->wmc_logourl_format = $attributes["format"];
             }
             if ($logourl) {
                 if ($tag == "LOGOURL" && $type == "close") {
                     $logourl = false;
                 }
                 if ($tag == "ONLINERESOURCE") {
                     $this->wmc_logourl_type = $attributes["xlink:type"];
                     $this->wmc_logourl = $attributes["xlink:href"];
                 }
             }
             if ($tag == "DESCRIPTIONURL" && $type == "open") {
                 $descriptionurl = true;
                 $this->wmc_descriptionurl_format = $attributes["format"];
             }
             if ($descriptionurl) {
                 if ($tag == "DESCRIPTIONURL" && $type == "close") {
                     $descriptionurl = false;
                 }
                 if ($tag == "ONLINERESOURCE") {
                     $this->wmc_descriptionurl_type = $attributes["xlink:type"];
                     $this->wmc_descriptionurl = $attributes["xlink:href"];
                 }
             }
             if ($tag == "KEYWORDLIST" && $type == "open") {
                 $keywordlist = true;
             }
             if ($keywordlist) {
                 if ($tag == "KEYWORDLIST" && $type == "close") {
                     $keywordlist = false;
                     $cnt_keyword = -1;
                 }
                 if ($tag == "KEYWORD") {
                     $cnt_keyword++;
                     $this->wmc_keyword[$cnt_keyword] = $value;
                 }
             }
             if ($tag == "EXTENSION" && $type == "close") {
                 $generalExtension = false;
                 //
                 // After the general extension tag is closed,
                 // we have all necessary information to CREATE
                 // the map objects that are contained in this
                 // WMC.
                 //
                 $this->setMapData();
             }
             if ($generalExtension) {
                 if ($value !== "") {
                     if (isset($this->generalExtensionArray[$tag])) {
                         if (!is_array($this->generalExtensionArray[$tag])) {
                             $firstValue = $this->generalExtensionArray[$tag];
                             $this->generalExtensionArray[$tag] = array();
                             array_push($this->generalExtensionArray[$tag], $firstValue);
                         }
                         array_push($this->generalExtensionArray[$tag], $value);
                     } else {
                         $this->generalExtensionArray[$tag] = $value;
                     }
                 }
             }
             if ($tag == "EXTENSION" && $type == "open") {
                 $generalExtension = true;
             }
             if ($tag == "GENERAL" && $type == "close") {
                 $general = false;
             }
         }
         if ($layerlist) {
             if ($tag == "LAYERLIST" && $type == "close") {
                 $layerlist = false;
             }
             if ($tag == "LAYER" && $type == "open") {
                 //
                 // The associative array currentLayer holds all
                 // data of the currently processed layer.
                 // The data will be set in the classes' WMS
                 // object when the layer tag is closed.
                 //
                 $currentLayer = array();
                 $currentLayer["queryable"] = $attributes["queryable"];
                 if ($attributes["hidden"] == "1") {
                     $currentLayer["visible"] = 0;
                 } else {
                     $currentLayer["visible"] = 1;
                 }
                 $currentLayer["format"] = array();
                 $currentLayer["style"] = array();
                 $layer = true;
             }
             if ($layer) {
                 if ($tag == "LAYER" && $type == "close") {
                     //
                     // After a layer tag is closed,
                     // we have all necessary information to CREATE
                     // a layer object and append it to the WMS object
                     //
                     if (isset($currentLayer["extension"]["OVERVIEWHIDDEN"])) {
                         array_push($layerlistArray["overview"], $currentLayer);
                     }
                     $modifiedLayer = $currentLayer;
                     unset($modifiedLayer["extension"]["OVERVIEWHIDDEN"]);
                     array_push($layerlistArray["main"], $modifiedLayer);
                     $layer = false;
                 }
                 if ($formatlist) {
                     if ($tag == "FORMAT") {
                         array_push($currentLayer["format"], array("current" => $attributes["current"], "name" => $value));
                         if ($attributes["current"] == "1") {
                             $currentLayer["formatIndex"] = count($currentLayer["format"]) - 1;
                         }
                     }
                     if ($tag == "FORMATLIST" && $type == "close") {
                         $formatlist = false;
                     }
                 } elseif ($metadataurl) {
                     if ($tag == "ONLINERESOURCE") {
                         $currentLayer["metadataurl"] = $attributes["xlink:href"];
                     }
                     if ($tag == "METADATAURL" && $type == "close") {
                         $metadataurl = false;
                     }
                 } elseif ($dataurl) {
                     if ($tag == "ONLINERESOURCE") {
                         $currentLayer["dataurl"] = $attributes["xlink:href"];
                     }
                     if ($tag == "DATAURL" && $type == "close") {
                         $dataurl = false;
                     }
                 } elseif ($stylelist) {
                     if ($style) {
                         $index = count($currentLayer["style"]) - 1;
                         if ($tag == "STYLE" && $type == "close") {
                             $style = false;
                         }
                         if ($tag == "SLD" && $type == "open") {
                             $sld = true;
                         }
                         if ($sld) {
                             if ($tag == "SLD" && $type == "close") {
                                 $sld = false;
                             }
                             if ($tag == "ONLINERESOURCE") {
                                 $currentLayer["style"][$index]["sld_type"] = $attributes["xlink:type"];
                                 $currentLayer["style"][$index]["sld_url"] = $attributes["xlink:href"];
                             }
                             if ($tag == "TITLE") {
                                 $currentLayer["style"][$index]["sld_title"] = $value;
                             }
                         } else {
                             if ($tag == "NAME") {
                                 $currentLayer["style"][$index]["name"] = $value ? $value : "default";
                             }
                             if ($tag == "TITLE") {
                                 $currentLayer["style"][$index]["title"] = $value ? $value : "default";
                             }
                             if ($legendurl) {
                                 if ($tag == "LEGENDURL" && $type == "close") {
                                     $legendurl = false;
                                 }
                                 if ($tag == "ONLINERESOURCE") {
                                     $currentLayer["style"][$index]["legendurl_type"] = $attributes["xlink:type"];
                                     $currentLayer["style"][$index]["legendurl"] = $attributes["xlink:href"];
                                 }
                             }
                             if ($tag == "LEGENDURL" && $type == "open") {
                                 $legendurl = true;
                                 $currentLayer["style"][$index]["legendurl_width"] = $attributes["width"];
                                 $currentLayer["style"][$index]["legendurl_height"] = $attributes["height"];
                                 $currentLayer["style"][$index]["legendurl_format"] = $attributes["format"];
                             }
                         }
                     }
                     if ($tag == "STYLE" && $type == "open") {
                         $style = true;
                         array_push($currentLayer["style"], array("current" => $attributes["current"]));
                         if ($attributes["current"] == "1") {
                             $currentLayer["styleIndex"] = count($currentLayer["style"]) - 1;
                         }
                     }
                     if ($tag == "STYLELIST" && $type == "close") {
                         $stylelist = false;
                     }
                 } else {
                     if ($tag == "SERVER" && $type == "open") {
                         $server = true;
                         $currentLayer["service"] = $attributes["service"];
                         $currentLayer["version"] = $attributes["version"];
                         $currentLayer["wms_title"] = $attributes["title"];
                     }
                     if ($server) {
                         if ($tag == "SERVER" && $type == "close") {
                             $server = false;
                         }
                         if ($tag == "ONLINERESOURCE") {
                             $currentLayer["url"] = $attributes["xlink:href"];
                         }
                     }
                     if ($tag == "NAME") {
                         $currentLayer["name"] = $value;
                     }
                     if ($tag == "TITLE") {
                         $currentLayer["title"] = $value;
                     }
                     if ($tag == "ABSTRACT") {
                         $currentLayer["abstract"] = $value;
                     }
                     if ($tag == "SRS") {
                         $currentLayer["epsg"] = explode(" ", $value);
                     }
                     if ($tag == "EXTENSION" && $type == "close") {
                         $extension = false;
                     }
                     if ($extension == true) {
                         //							if ($value !== "") {
                         if (isset($currentLayer["extension"][$tag])) {
                             if (!is_array($currentLayer["extension"][$tag])) {
                                 $firstValue = $currentLayer["extension"][$tag];
                                 $currentLayer["extension"][$tag] = array();
                                 array_push($currentLayer["extension"][$tag], $firstValue);
                             }
                             array_push($currentLayer["extension"][$tag], $value);
                         } else {
                             $currentLayer["extension"][$tag] = $value;
                         }
                         //							}
                     }
                     if ($tag == "EXTENSION" && $type == "open") {
                         $currentLayer["extension"] = array();
                         $extension = true;
                     }
                     if ($tag == "METADATAURL" && $type == "open") {
                         $metadataurl = true;
                     }
                     if ($tag == "DATAURL" && $type == "open") {
                         $dataurl = true;
                     }
                     if ($tag == "FORMATLIST" && $type == "open") {
                         $formatlist = true;
                     }
                     if ($tag == "STYLELIST" && $type == "open") {
                         $stylelist = true;
                     }
                 }
             }
         }
     }
     // set WMS data
     $layerlistCompleteArray = array_merge($layerlistArray["main"], $layerlistArray["overview"]);
     for ($i = 0; $i < count($layerlistCompleteArray); $i++) {
         $this->setLayerData($layerlistCompleteArray[$i]);
     }
     $wmsArr = $this->mainMap->getWmsArray();
     for ($i = 0; $i < count($wmsArr); $i++) {
         $wmsArr[$i]->updateAllOwsProxyUrls();
     }
     return true;
 }
 /**
  * Creates WFS 1.0 objects from a capabilities documents.
  *
  * @return Wfs_1_1
  * @param $xml String
  */
 public function createFromXml($xml)
 {
     try {
         $myWfs = new Wfs_1_1();
         $admin = new administration();
         $values = $admin->parseXml($xml);
         $myWfs->getCapabilitiesDoc = $admin->char_encode($xml);
         $myWfs->id = $this->createId();
         foreach ($values as $element) {
             $tag = $this->sepNameSpace(strtoupper($element[tag]));
             if ($tag == "WFS_CAPABILITIES" && $element[type] == "open") {
                 $myWfs->version = $element[attributes][version];
                 if ($myWfs->version !== "1.1.0") {
                     throw new Exception("Not a WFS 1.1.0 capabilities document.");
                 }
             }
             if ($tag == "NAME" && $element[level] == '3') {
                 $myWfs->name = $element[value];
             }
             if ($tag == "TITLE" && $element[level] == '3') {
                 $myWfs->title = $this->stripEndlineAndCarriageReturn($element[value]);
             }
             if ($tag == "ABSTRACT" && $element[level] == '3') {
                 $myWfs->summary = $this->stripEndlineAndCarriageReturn($element[value]);
             }
             if ($tag == "FEES") {
                 $myWfs->fees = $element[value];
             }
             if ($tag == "ACCESSCONSTRAINTS") {
                 $myWfs->accessconstraints = $element[value];
             }
             if ($tag == "OPERATION" && $element[type] == "open") {
                 switch ($element[attributes][name]) {
                     case "GetCapabilities":
                         $section = "getcapabilities";
                         break;
                     case "DescribeFeatureType":
                         $section = "describefeaturetype";
                         break;
                     case "GetFeature":
                         $section = "getfeature";
                         break;
                     case "Transaction":
                         $section = "transaction";
                         break;
                 }
             }
             # getCapabilities
             if ($section == "getcapabilities" && $tag == "GET") {
                 $myWfs->getCapabilities = html_entity_decode($element[attributes]["xlink:href"]);
             }
             # descriptFeatureType
             #				if($section == "describefeaturetype" && $tag == "POST"){
             #					$myWfs->describeFeatureType = html_entity_decode($element[attributes]["xlink:href"]);
             #				}
             # descriptFeatureType
             if ($section == "describefeaturetype" && $tag == "GET") {
                 $myWfs->describeFeatureType = html_entity_decode($element[attributes]["xlink:href"]);
             }
             # getFeature
             if ($section == "getfeature" && $tag == "POST") {
                 $myWfs->getFeature = html_entity_decode($element[attributes]["xlink:href"]);
             }
             # transaction
             if ($section == "transaction" && $tag == "POST") {
                 $myWfs->transaction = html_entity_decode($element[attributes]["xlink:href"]);
             }
             if ($tag == "OPERATION" && $element[type] == "close") {
                 $section = "";
             }
             if ($tag == "FEATURETYPE" && $element[type] == "open") {
                 $section = "featuretype";
                 $featureTypeNsArray = $element[attributes];
             }
             if ($section == "featuretype" && $tag == "NAME") {
                 $featuretype_name = $element[value];
             }
             if ($section == "featuretype" && $tag == "TITLE") {
                 $featuretype_title = $this->stripEndlineAndCarriageReturn($element[value]);
             }
             if ($section == "featuretype" && $tag == "ABSTRACT") {
                 $featuretype_abstract = $element[value];
             }
             if ($section == "featuretype" && $tag == "DEFAULTSRS") {
                 $featuretype_srs = $element[value];
                 // Do not add defective featuretypes
                 try {
                     $currentFeatureType = $this->createFeatureTypeFromUrlGet($myWfs, $featuretype_name, $featureTypeNsArray);
                     if ($currentFeatureType !== null) {
                         $currentFeatureType->name = $featuretype_name;
                         $currentFeatureType->title = $featuretype_title;
                         $currentFeatureType->summary = $featuretype_abstract;
                         $currentFeatureType->srs = $featuretype_srs;
                         $myWfs->addFeatureType($currentFeatureType);
                     }
                 } catch (Exception $e) {
                     new mb_exception("Failed to load featuretype " . $featuretype_name);
                 }
             }
         }
         if (!$myWfs->title) {
             $myWfs->title = "Untitled";
         }
         return $myWfs;
     } catch (Exception $e) {
         $e = new mb_exception($e);
         return null;
     }
 }