/**
  * Encode a header string to best of Q, B, quoted or none.  
  * @access private
  * @return string
  */
 function EncodeHeader($str, $position = 'text')
 {
     $x = 0;
     if (!empty($this->MultibyteFlag) && function_exists('mb_encode_mimeheader')) {
         $mimeencoding = isset($this->MimeHeader) && $this->MimeHeader == 'Q' ? 'Q' : 'B';
         return mb_encode_mimeheader($str, $this->CharSet, $mimeencoding, $this->LE);
     } elseif (function_exists('mb_utf8_decode')) {
         $str = mb_utf8_decode($str, $this->CharSet);
     }
     switch (strtolower($position)) {
         case 'phrase':
             if ('ASCII' == mb_detect_encoding($str)) {
                 // Can't use addslashes as we don't know what value has magic_quotes_sybase.
                 $encoded = addcslashes($str, "..\\\"");
                 if ($str == $encoded && !preg_match('/[^A-Za-z0-9!#$%&\'*+\\/=?^_`{|}~ -]/', $str)) {
                     return $encoded;
                 } else {
                     return "\"{$encoded}\"";
                 }
             }
             $x = preg_match_all('/[^\\040\\041\\043-\\133\\135-\\176]/', $str, $matches);
             break;
         case 'comment':
             $x = preg_match_all('/[()"]/', $str, $matches);
             // Fall-through
         // Fall-through
         case 'text':
         default:
             $x += preg_match_all('/[\\000-\\010\\013\\014\\016-\\037\\177-\\377]/', $str, $matches);
             break;
     }
     if ($x == 0) {
         return $str;
     }
     $maxlen = 75 - 7 - strlen($this->CharSet);
     // Try to select the encoding which should produce the shortest output
     if ('Q' != $this->MimeHeader && (strlen($str) / 3 < $x || 'B' == $this->MimeHeader)) {
         $encoding = 'B';
         $encoded = base64_encode($str);
         $maxlen -= $maxlen % 4;
         $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
     } else {
         $encoding = 'Q';
         $encoded = $this->EncodeQ($str, $position);
         $encoded = $this->WrapText($encoded, $maxlen, true);
         $encoded = str_replace("=" . $this->LE, "\n", trim($encoded));
     }
     $encoded = preg_replace('/^(.*)$/m', " =?" . $this->CharSet . "?{$encoding}?\\1?=", $encoded);
     $encoded = trim(str_replace("\n", $this->LE, $encoded));
     return $encoded;
 }
 /**
  * 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;
 }