[Mapbender-commits] r2483 - branches/2.5/http/classes

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Mon Jun 9 04:52:06 EDT 2008


Author: astrid_emde
Date: 2008-06-09 04:52:06 -0400 (Mon, 09 Jun 2008)
New Revision: 2483

Modified:
   branches/2.5/http/classes/class_wms.php
Log:
#$this->objLayer[$i]->""; If the Layer Name is empty it will be handled as empty.

Modified: branches/2.5/http/classes/class_wms.php
===================================================================
--- branches/2.5/http/classes/class_wms.php	2008-06-05 07:59:43 UTC (rev 2482)
+++ branches/2.5/http/classes/class_wms.php	2008-06-09 08:52:06 UTC (rev 2483)
@@ -1,1772 +1,1772 @@
-<?php
-# $Id$
-# http://www.mapbender.org/index.php/class_wms
-# Copyright (C) 2002 CCGIS 
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2, or (at your option)
-# any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-include_once(dirname(__FILE__)."/../../conf/mapbender.conf");
-require_once(dirname(__FILE__)."/class_connector.php");
-require_once(dirname(__FILE__)."/class_mb_exception.php");
-require_once(dirname(__FILE__)."/class_administration.php");
-
-$con = db_connect(DBSERVER,OWNER,PW);
-db_select_db(DB,$con);
-
-  
-class wms {
-	var $lastURL;
-	var $wms_id;
-	var $wms_status;
-	var $wms_version;
-	var $wms_title;
-	var $wms_abstract;
-	var $wms_getcapabilities;
-	var $wms_getcapabilities_doc;
-	var $wms_getmap;
-	var $wms_getfeatureinfo;
-	var $wms_getlegendurl;
-	var $wms_upload_url;
-	  
-	var $fees;
-	var $accessconstraints;
-	var $contactperson;
-	var $contactposition;
-	var $contactorganization;
-	var $address;
-	var $city;
-	var $stateorprovince;
-	var $postcode;
-	var $country;
-	var $contactvoicetelephone;
-	var $contactfacsimiletelephone;
-	var $contactelectronicmailaddress;
-	  
-	var $wms_keyword = array();
-	var $data_type = array(); 
-	var $data_format = array();
-	var $objLayer = array(); 
-	  
-	var $wms_supportsld;
-	var $wms_userlayer;
-	var $wms_userstyle;
-	var $wms_remotewfs;
-		
-	var $gui_wms_mapformat;
-	var $gui_wms_featureinfoformat;
-	var $gui_wms_exceptionformat;
-	var $gui_wms_epsg;
-	var $gui_wms_sldurl;
-	  
-	var $default_epsg = 0;
-	var $overwrite = true;
-	  
-	function wms() {
-	} 
-	  
-	function createObjFromXML($url){
-	
-		$x = new connector($url);
-		$data = $x->file;
-		
-		if(!$data){
-			$this->wms_status = false;
-			return false;
-		}
-		else {
-			$this->wms_status = true;
-		}
-			
-		$values = null;
-		$tags = null;
-		$admin = new administration();
-		$this->wms_getcapabilities_doc = $admin->char_encode($data);
-		$this->wms_upload_url = $url;
-		
-		$this->wms_id = "";
-		$parser = xml_parser_create("");
-		xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
-		xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
-		xml_parser_set_option($parser,XML_OPTION_TARGET_ENCODING,CHARSET);
-		xml_parse_into_struct($parser,$this->wms_getcapabilities_doc,$values,$tags);
-
-		$code = xml_get_error_code($parser);
-		if ($code) {
-			$line = xml_get_current_line_number($parser); 
-			$mb_exception = new mb_exception(xml_error_string($code) .  " in line " . $line);
-		}
-		
-		xml_parser_free($parser);
-		
-		$section = null;
-		$format = null;
-		$cnt_format = 0;
-		$parent = array();
-		$myParent = array();
-		$cnt_layer = -1;
-		$request = null; 
-		$layer_style = array();
-		$cnt_styles = -1;
-		
-		foreach ($values as $element) {
-			if(mb_strtoupper($element[tag]) == "WMT_MS_CAPABILITIES" && $element[type] == "open"){
-				$this->wms_version = $element[attributes][version];
-			}
-			if(mb_strtoupper($element[tag]) == "TITLE" && $element[level] == '3'){
-				$this->wms_title = $this->stripEndlineAndCarriageReturn($element[value]);
-			}
-			if(mb_strtoupper($element[tag]) == "ABSTRACT" && $element[level] == '3'){
-				$this->wms_abstract = $this->stripEndlineAndCarriageReturn($element[value]);
-			}
-			if(mb_strtolower($element[tag]) == "fees"){
-				$this->fees = $element[value];
-			}
-			if(mb_strtolower($element[tag]) == "accessconstraints"){
-				$this->accessconstraints = $element[value];
-			}
-			if(mb_strtolower($element[tag]) == "contactperson"){
-				$this->contactperson = $element[value];
-			}
-			if(mb_strtolower($element[tag]) == "contactposition"){
-				$this->contactposition = $element[value];
-			}
-			if(mb_strtolower($element[tag]) == "contactorganization"){
-				$this->contactorganization = $element[value];
-			}
-			if(mb_strtolower($element[tag]) == "address"){
-				$this->address = $element[value];
-			}
-			if(mb_strtolower($element[tag]) == "city"){
-				$this->city = $element[value];
-			}
-			if(mb_strtolower($element[tag]) == "stateorprovince"){
-				$this->stateorprovince = $element[value];
-			}
-			if(mb_strtolower($element[tag]) == "postcode"){
-				$this->postcode = $element[value];
-			}
-			if(mb_strtolower($element[tag]) == "country"){
-				$this->country = $element[value];
-			}
-			if(mb_strtolower($element[tag]) == "contactvoicetelephone"){
-				$this->contactvoicetelephone = $element[value];
-			}
-			if(mb_strtolower($element[tag]) == "contactfacsimiletelephone"){
-				$this->contactfacsimiletelephone = $element[value];
-			}
-			if(mb_strtolower($element[tag]) == "contactelectronicmailaddress"){
-				$this->contactelectronicmailaddress = $element[value];
-			}
-	  		if(mb_strtolower($element[tag]) == "keyword" && $section != 'layer'){
-				$this->wms_keyword[count($this->wms_keyword)] = $element[value];
-			}
-			
-			/*map section*/
-			if($this->wms_version == "1.0.0"){
-		 		if(mb_strtoupper($element[tag]) == "MAP" && $element[type] == "open"){
-					$section = "map";
-				}
-				if($section == "map" && mb_strtoupper($element[tag]) == "GET"){
-					$this->wms_getmap = $element[attributes][onlineResource];
-				}
-				if($section == "map" && mb_strtoupper($element[tag]) == "FORMAT" && $element[type] == "open"){
-					$format = "map";
-				}
-				if(mb_strtoupper($element[tag]) != "FORMAT" && $section == "map" && $format == "map"){
-					$this->data_type[$cnt_format] = "map";
-					$this->data_format[$cnt_format] = trim($element[tag]);
-					$cnt_format++;
-				}
-				if(mb_strtoupper($element[tag]) == "FORMAT" && $element[type] == "close"){
-					$format = "";
-				}
-				if(mb_strtoupper($element[tag]) == "MAP" && $element[type] == "close"){
-					$section = "";
-				}
-			}
-			else{
-				if(mb_strtoupper($element[tag]) == "GETMAP" && $element[type] == "open"){
-					$section = "map";
-				}
-				if($section == "map" && mb_strtoupper($element[tag]) == "GET" && $element[type] == "open"){
-					$request = "get";
-				}
-				if($section == "map" && $request == "get" && mb_strtoupper($element[tag]) == "ONLINERESOURCE"){
-					$this->wms_getmap = $element[attributes]["xlink:href"];
-				}
-				if($section == "map" && mb_strtoupper($element[tag]) == "FORMAT"){
-					$this->data_type[$cnt_format] = "map";
-					$this->data_format[$cnt_format] = trim($element[value]);
-					$cnt_format++;
-				}
-				if($section == "map" && mb_strtoupper($element[tag]) == "GET" && $element[type] == "close"){
-					$request = "";
-				}
-				if(mb_strtoupper($element[tag]) == "GETMAP" && $element[type] == "close"){
-					$section = "";
-				}
-			}
-			/*capabilities section*/
-			if($this->wms_version == "1.0.0"){
-				if(mb_strtoupper($element[tag]) == "CAPABILITIES" && $element[type] == "open"){
-					$section = "capabilities";
-				}
-				if($section == "capabilities" && mb_strtoupper($element[tag]) == "GET"){
-					$this->wms_getcapabilities = $element[attributes][onlineResource];
-				}
-				if(mb_strtoupper($element[tag]) == "CAPABILITIES" && $element[type] == "close"){
-					$section = "";
-				}
-			}
-			else{
-				if(mb_strtoupper($element[tag]) == "GETCAPABILITIES" && $element[type] == "open"){
-					$section = "capabilities";
-				}
-				if($section == "capabilities" && mb_strtoupper($element[tag]) == "GET" && $element[type] == "open"){
-					$request = "get";
-				}
-				if($section == "capabilities" && $request == "get" && mb_strtoupper($element[tag]) == "ONLINERESOURCE"){
-					$this->wms_getcapabilities = $element[attributes]["xlink:href"];
-				}
-				if($section == "capabilities" && mb_strtoupper($element[tag]) == "GET" && $element[type] == "close"){
-					$request = "";
-				}
-				if(mb_strtoupper($element[tag]) == "GETCAPABILITIES" && $element[type] == "close"){
-					$section = "";
-				}
-			}
-			/*featureInfo section*/
-			if($this->wms_version == "1.0.0"){
-				if(mb_strtoupper($element[tag]) == "FEATUREINFO" && $element[type] == "open"){
-					$section = "featureinfo";
-				}
-				if($section == "featureinfo" && mb_strtoupper($element[tag]) == "GET"){
-					$this->wms_getfeatureinfo = $element[attributes][onlineResource];
-				}
-				if($section == "featureinfo" && mb_strtoupper($element[tag]) == "FORMAT" && $element[type] == "open"){
-					$format = "featureinfo";
-				}
-				if(mb_strtoupper($element[tag]) != "FORMAT" && $section == "featureinfo" && $format == "featureinfo"){
-					$this->data_type[$cnt_format] = "featureinfo";
-					$this->data_format[$cnt_format] = trim($element[tag]);
-					$cnt_format++;
-				}
-				if(mb_strtoupper($element[tag]) == "FORMAT" && $element[type] == "close"){
-					$format = "";
-				}
-				if(mb_strtoupper($element[tag]) == "FEATUREINFO" && $element[type] == "close"){
-					$section = "";
-				}
-			}
-			else{
-				if(mb_strtoupper($element[tag]) == "GETFEATUREINFO" && $element[type] == "open"){
-					$section = "featureinfo";
-				}
-				if($section == "featureinfo" && mb_strtoupper($element[tag]) == "GET" && $element[type] == "open"){
-					$request = "get";
-				}
-				if($section == "featureinfo" && $request == "get" && mb_strtoupper($element[tag]) == "ONLINERESOURCE"){
-					$this->wms_getfeatureinfo = $element[attributes]["xlink:href"];
-				}
-				if($section == "featureinfo" && mb_strtoupper($element[tag]) == "FORMAT"){
-					$this->data_type[$cnt_format] = "featureinfo";
-					$this->data_format[$cnt_format] = trim($element[value]);
-					$cnt_format++;
-				}
-				if($section == "featureinfo" && mb_strtoupper($element[tag]) == "GET" && $element[type] == "close"){
-					$request = "";
-				}
-				if(mb_strtoupper($element[tag]) == "GETFEATUREINFO" && $element[type] == "close"){
-					$section = "";
-				}
-			}
-			/*exception section*/
-			if($this->wms_version == "1.0.0"){
-				if(mb_strtoupper($element[tag]) == "EXCEPTION" && $element[type] == "open"){
-					$section = "exception";
-				}
-				if($section == "exception" && mb_strtoupper($element[tag]) == "FORMAT" && $element[type] == "open"){
-					$format = "exception";
-				}
-				if(mb_strtoupper($element[tag]) != "FORMAT" && $section == "exception" && $format == "exception"){
-					$this->data_type[$cnt_format] = "exception";
-					$this->data_format[$cnt_format] = trim($element[tag]);
-					$cnt_format++;
-				}
-				if($section == "exception" && mb_strtoupper($element[tag]) == "FORMAT" && $element[type] == "close"){
-					$format = "";
-				}
-				if(mb_strtoupper($element[tag]) == "EXCEPTION" && $element[type] == "close"){
-					$section = "";
-				}
-			}
-			else{
-				if(mb_strtoupper($element[tag]) == "EXCEPTION" && $element[type] == "open"){
-					$section = "exception";
-				}
-				if($section == "exception" && mb_strtoupper($element[tag]) == "FORMAT"){
-					$this->data_type[$cnt_format] = "exception";
-					$this->data_format[$cnt_format] = trim($element[value]);
-					$cnt_format++;
-				}
-				if(mb_strtoupper($element[tag]) == "EXCEPTION" && $element[type] == "close"){
-					$section = "";
-				}
-			}
-	      /*legend section*/
-	      if($this->wms_version == "1.0.0"){
-	      
-	      }
-	      else{
-	        if(mb_strtoupper($element[tag]) == "GETLEGENDGRAPHIC" && $element[type] == "open"){
-				$section = "legend";
-			}
-	        if($section == "legend" && mb_strtoupper($element[tag]) == "GET" && $element[type] == "open"){
-				$request = "get";
-			}
-			if($section == "legend" && $request == "get" && mb_strtoupper($element[tag]) == "ONLINERESOURCE"){
-				$this->wms_getlegendurl = $element[attributes]["xlink:href"];
-			}
-	        if($section == "legend" && mb_strtoupper($element[tag]) == "GET" && $element[type] == "close"){
-				$request = "";
-			}
-			if(mb_strtoupper($element[tag]) == "GETLEGENDGRAPHIC" && $element[type] == "close"){
-				$section = "";
-			}         
-	      }
-			/* sld section */	      
-			if(mb_strtoupper($element[tag]) == "USERDEFINEDSYMBOLIZATION" && $element[type] == "complete"){
-				$this->wms_supportsld = $element[attributes]["SupportSLD"];
-				$this->wms_userlayer = $element[attributes]["UserLayer"];
-				$this->wms_userstyle = $element[attributes]["UserStyle"];
-				$this->wms_remotewfs = $element[attributes]["RemoteWFS"];
-			}
-	      	      
-			/*layer section*/				
-			if(mb_strtoupper($element[tag]) == "LAYER"){
-				$section = "layer";
-				if ($element[type] == "open") {
-					$cnt_epsg = -1;
-					$cnt_layer++;
-					$parent[$element[level]+1] = $cnt_layer;
-					$myParent[$cnt_layer]= $parent[$element[level]];
-					$this->addLayer($cnt_layer,$myParent[$cnt_layer]);
-					$this->objLayer[$cnt_layer]->layer_queryable = $element[attributes][queryable];
-				}
-				if ($element[type] == "close") {
-				
-				}
-			}
-			/* attribution */
-			if(mb_strtoupper($element[tag]) == "ATTRIBUTION"){
-				if ($element[type] == "open") {
-					$section = "attribution";
-				}
-				if ($element[type] == "close") {
-					$section = "layer";
-				}
-			}
-			/* styles */
-			if(mb_strtoupper($element[tag]) == "STYLE"){
-				$section = "style";
-				if($cnt_layer != $layer_style){
-					$layer_style = $cnt_layer;
-					$cnt_styles = -1;
-				}
-				if ($element[type] == "open") {
-					$cnt_styles++;
-				}
-				if ($element[type] == "close") {
-					$section = "layer";
-				}
-			}
-			if($section == "style"){
-				if(mb_strtoupper($element[tag]) == "NAME"){
-					$this->objLayer[$cnt_layer]->layer_style[$cnt_styles]["name"] = $element[value];
-				}
-				if(mb_strtoupper($element[tag]) == "TITLE"){
-					$this->objLayer[$cnt_layer]->layer_style[$cnt_styles]["title"] = $element[value];
-				}
-	      		if(mb_strtoupper($element[tag]) == "LEGENDURL" && $element[type] == "open"){
-					$legendurl = true;
-				}
-				if($legendurl && mb_strtoupper($element[tag]) == "FORMAT"){
-					$this->objLayer[$cnt_layer]->layer_style[$cnt_styles]["legendurlformat"] = $element[value];
-				}
-				if($legendurl && mb_strtoupper($element[tag]) == "ONLINERESOURCE"){
-					$this->objLayer[$cnt_layer]->layer_style[$cnt_styles]["legendurl"] = $element[attributes]["xlink:href"];
-				}
-			    if(mb_strtoupper($element[tag]) == "LEGENDURL" && $element[type] == "close"){
-					$legendurl = false;
-				}   
-			}
-			/* end of styles */
-			if($section == "layer"){
-				if(mb_strtoupper($element[tag]) == "NAME"){
-					$this->objLayer[$cnt_layer]->layer_name = $element[value];
-				}
-				if(mb_strtoupper($element[tag]) == "TITLE"){
-					$this->objLayer[$cnt_layer]->layer_title = $this->stripEndlineAndCarriageReturn($element[value]);
-				}
-				if(mb_strtoupper($element[tag]) == "ABSTRACT"){
-					$this->objLayer[$cnt_layer]->layer_abstract = $this->stripEndlineAndCarriageReturn($element[value]);
-				}
-				if(mb_strtoupper($element[tag]) == "KEYWORD"){
-					array_push($this->objLayer[$cnt_layer]->layer_keyword, trim($element[value]));
-				}
-	      		if(mb_strtoupper($element[tag]) == "DATAURL" && $element[type] == "open"){
-					$dataurl = true;
-				}
-				if($dataurl && mb_strtoupper($element[tag]) == "ONLINERESOURCE"){
-					$this->objLayer[$cnt_layer]->layer_dataurl_href = $element[attributes]["xlink:href"];
-				}
-			    if(mb_strtoupper($element[tag]) == "DATAURL" && $element[type] == "close"){
-					$dataurl = false;
-				}   
-				
-				if(mb_strtoupper($element[tag]) == "METADATAURL" && $element[type] == "open"){
-					$metadataurl = true;
-				}
-				if($metadataurl && mb_strtoupper($element[tag]) == "ONLINERESOURCE"){
-					$this->objLayer[$cnt_layer]->layer_metadataurl = $element[attributes]["xlink:href"];
-				}
-			    if(mb_strtoupper($element[tag]) == "METADATAURL" && $element[type] == "close"){
-					$metadataurl = false;
-				}   
-				
-				if(mb_strtoupper($element[tag]) == "SRS"){
-	  				$this->objLayer[$cnt_layer]->wms_srs1 = $element[value];
-					// unique srs only, see http://www.mapbender.org/index.php/Arrays_with_unique_entries
-					$this->wms_srs = array_keys(array_flip(explode(" ", $this->objLayer[0]->wms_srs1)));  				
-				}						      
-				if(mb_strtoupper($element[tag]) == "LATLONBOUNDINGBOX"){
-					$cnt_epsg++;
-					$this->objLayer[$cnt_layer]->layer_epsg[$cnt_epsg]["epsg"] = "EPSG:4326";
-					$this->objLayer[$cnt_layer]->layer_epsg[$cnt_epsg]["minx"] = $element[attributes][minx];
-					$this->objLayer[$cnt_layer]->layer_epsg[$cnt_epsg]["miny"] = $element[attributes][miny];
-					$this->objLayer[$cnt_layer]->layer_epsg[$cnt_epsg]["maxx"] = $element[attributes][maxx];
-					$this->objLayer[$cnt_layer]->layer_epsg[$cnt_epsg]["maxy"] = $element[attributes][maxy];
-				}
-				if(mb_strtoupper($element[tag]) == "BOUNDINGBOX" && $element[attributes][SRS] != "EPSG:4326"){
-					$cnt_epsg++;
-					$this->objLayer[$cnt_layer]->layer_epsg[$cnt_epsg]["epsg"] = $element[attributes][SRS];
-					$this->objLayer[$cnt_layer]->layer_epsg[$cnt_epsg]["minx"] = $element[attributes][minx];
-					$this->objLayer[$cnt_layer]->layer_epsg[$cnt_epsg]["miny"] = $element[attributes][miny];
-					$this->objLayer[$cnt_layer]->layer_epsg[$cnt_epsg]["maxx"] = $element[attributes][maxx];
-					$this->objLayer[$cnt_layer]->layer_epsg[$cnt_epsg]["maxy"] = $element[attributes][maxy];
-					// a default epsg for mapbender
-					if($cnt_layer == 0 && $this->default_epsg == 0 && mb_strlen(trim($element[attributes][SRS]))>= 10){
-						$this->default_epsg = $cnt_epsg;
-					}
-				}
-				if(mb_strtoupper($element[tag]) == "SCALEHINT"){
-					if($element[attributes][max]>1000) $max = 0; else $max = $element[attributes][max]; 	
-					if($element[attributes][min]>1000) $min = 0; else $min = $element[attributes][min]; 	
-					$this->objLayer[$cnt_layer]->layer_minscale = round(($min * 2004.3976484406788493955738891127));
-					$this->objLayer[$cnt_layer]->layer_maxscale = round(($max * 2004.3976484406788493955738891127));
-				}
-			} 
-			else {
-				continue;
-			}
-		}
-		if(!$this->wms_title || $this->wms_title == "" || !$this->wms_getmap || $this->wms_getmap == ""){
-			$this->wms_status = false;
-			$this->optimizeWMS();
-			
-			return false;
-		}
-		else{
-			$this->wms_status = true;
-			$this->optimizeWMS();
-
-			return true;
-		}
-	}
-	/**
-	 * private function
-	 */
-	function optimizeWMS() {
-		/*define defaults for wms-version 1.0.0*/
-		$map_default_ok = false;
-		$featureinfo_default_ok = false;
-		$exception_default_ok = false;
-		if($this->wms_version == "1.0.0"){
-			$map_default = "PNG";
-			$featureinfo_default = "MIME";
-			$exception_default = "INIMAGE";
-		}
-		/*define defaults for wms-version 1.1.0 and 1.1.1*/
-		else{
-			$map_default = "image/png";
-			$featureinfo_default = "text/html";
-			$exception_default = "application/vnd.ogc.se_inimage";
-		}
-		#some default
-		$this->gui_wms_visible = 1;
-		$this->gui_wms_opacity = 100;
-		/*if the rootlayer has no epsg...*/
-		if($this->objLayer[0]->layer_epsg[0]["epsg"] == ""){
-			$this->objLayer[0]->layer_epsg = $this->objLayer[1]->layer_epsg;
-			for($i=0;$i<count($this->objLayer[0]->layer_epsg);$i++){
-				for($j=1; $j<count($this->objLayer); $j++){
-					if($this->objLayer[0]->layer_epsg[$i]["epsg"] == $this->objLayer[$j]->layer_epsg[$i]["epsg"]){
-						if($this->objLayer[$j]->layer_epsg[$i]["minx"]<$this->objLayer[0]->layer_epsg[$i]["minx"]){
-							$this->objLayer[0]->layer_epsg[$i]["minx"] = $this->objLayer[$j]->layer_epsg[$i]["minx"];
-						}
-						if($this->objLayer[$j]->layer_epsg[$i]["miny"]<$this->objLayer[0]->layer_epsg[$i]["miny"]){
-							$this->objLayer[0]->layer_epsg[$i]["miny"] = $this->objLayer[$j]->layer_epsg[$i]["miny"];
-						}
-						if($this->objLayer[$j]->layer_epsg[$i]["maxx"]>$this->objLayer[0]->layer_epsg[$i]["maxx"]){
-							$this->objLayer[0]->layer_epsg[$i]["maxx"] = $this->objLayer[$j]->layer_epsg[$i]["maxx"];
-						}
-						if($this->objLayer[$j]->layer_epsg[$i]["maxy"]>$this->objLayer[0]->layer_epsg[$i]["maxy"]){
-							$this->objLayer[0]->layer_epsg[$i]["maxy"] = $this->objLayer[$j]->layer_epsg[$i]["maxy"];
-						}
-					}
-				}
-			}
-		}
-		for($i=0;$i<count($this->objLayer);$i++){
-			if(count($this->objLayer[$i]->layer_epsg) == 0 && count($this->objLayer[0]->layer_epsg) > 0){
-				$this->objLayer[$i]->layer_epsg = $this->objLayer[0]->layer_epsg; 
-			}
-			if(!is_int($this->objLayer[$i]->layer_parent)){
-				$this->objLayer[$i]->layer_abstract = $this->wms_abstract;
-				for ($r = 0; $r < count($this->wms_keyword); $r++) {
-					array_push($this->objLayer[$i]->layer_keyword, trim($this->wms_keyword[$r]));
-				}
-			}
-			if($this->objLayer[$i]->layer_name == ""){
-				$this->objLayer[$i]->layer_name = $this->objLayer[$i]->layer_title;
-			}
-			if($this->objLayer[$i]->layer_minscale == ""){
-				$this->objLayer[$i]->layer_minscale = 0;
-			}
-			if($this->objLayer[$i]->layer_maxscale == ""){
-				$this->objLayer[$i]->layer_maxscale = 0;
-			}
-			if($this->objLayer[$i]->layer_queryable == ""){
-				$this->objLayer[$i]->layer_queryable = 0;
-			}
-			$this->objLayer[$i]->gui_layer_minscale = $this->objLayer[$i]->layer_minscale;
-			$this->objLayer[$i]->gui_layer_maxscale = $this->objLayer[$i]->layer_maxscale;
-		}
-		for($i=0;$i<count($this->data_format);$i++){
-			if(mb_strtolower($this->data_type[$i]) == 'map' && mb_strtoupper($this->data_format[$i]) == mb_strtoupper($map_default)){
-				$this->gui_wms_mapformat = mb_strtolower($map_default);
-				$map_default_ok = true;
-			}
-			if(mb_strtolower($this->data_type[$i]) == 'featureinfo' && mb_strtoupper($this->data_format[$i]) == mb_strtoupper($featureinfo_default)){
-				$this->gui_wms_featureinfoformat = mb_strtolower($featureinfo_default);
-				$featureinfo_default_ok = true;
-			}		
-			if(mb_strtolower($this->data_type[$i]) == 'exception' && mb_strtolower($this->data_format[$i]) == mb_strtolower($exception_default)){
-				$this->gui_wms_exceptionformat = mb_strtolower($exception_default);
-				$exception_default_ok = true;
-			}		
-		}
-		if($map_default_ok == false){
-			for($i=0;$i<count($this->data_format);$i++){
-				if(mb_strtolower($this->data_type[$i]) == "map" ){$this->gui_wms_mapformat = $this->data_format[$i]; break;}
-			}
-		}
-		if($featureinfo_default_ok == false){
-			for($i=0;$i<count($this->data_format);$i++){
-				if(mb_strtolower($this->data_type[$i]) == "featureinfo" ){$this->gui_wms_featureinfoformat = $this->data_format[$i]; break;}
-			}
-		}
-		if($exception_default_ok == false){
-			for($i=0;$i<count($this->data_format);$i++){
-				if(mb_strtolower($this->data_type[$i]) == "exception" ){$this->gui_wms_exceptionformat = $this->data_format[$i]; break;}
-			}
-		}
-		
-		if(count($this->objLayer[0]->layer_epsg)>1){
-			$this->gui_wms_epsg = $this->objLayer[0]->layer_epsg[$this->default_epsg][epsg];
-		}
-		else{
-			$this->gui_wms_epsg = $this->objLayer[0]->layer_epsg[0][epsg];
-		}
-		/*the queryable layers*/
-		for($i=0; $i<count($this->objLayer); $i++){
-			if($this->objLayer[$i]->layer_queryable == 1){
-				$this->objLayer[$i]->gui_layer_queryable = 1;
-			}
-			else{
-				$this->objLayer[$i]->gui_layer_queryable = 0;
-			}
-		}
-		for($i=0; $i<count($this->objLayer); $i++){
-				$this->objLayer[$i]->layer_pos=$i;
-		}
-		
-		/* fill sld variables when empty */
-		if($this->wms_supportsld == ""){
-				$this->wms_supportsld = 0;
-		}
-		if($this->wms_userlayer == ""){
-				$this->wms_userlayer = 0;
-		}
-		if($this->wms_userstyle == ""){
-				$this->wms_userstyle = 0;
-		}
-		if($this->wms_remotewfs == ""){
-				$this->wms_remotewfs = 0;
-		}
-	  }
-	
-	function displayWMS(){
-		echo "<br>id: " . $this->wms_id . " <br>";
-		echo "version: " . $this->wms_version . " <br>";
-		echo "title: " . $this->wms_title . " <br>";
-		echo "abstract: " . $this->wms_abstract . " <br>";
-		echo "maprequest: " . $this->wms_getmap . " <br>";
-		echo "capabilitiesrequest: " . $this->wms_getcapabilities . " <br>";
-		echo "featureinforequest: " . $this->wms_getfeatureinfo . " <br>";
-		echo "gui_wms_mapformat: " . $this->gui_wms_mapformat . " <br>";
-		echo "gui_wms_featureinfoformat: " . $this->gui_wms_featureinfoformat . " <br>";
-		echo "gui_wms_exceptionformat: " . $this->gui_wms_exceptionformat . " <br>";	
-		echo "gui_wms_epsg: " . $this->gui_wms_epsg . " <br>";
-		echo "wms_srs: " . $this->objLayer[0]->wms_srs1 . " <br>";		
-		echo "gui_wms_visible: " . $this->gui_wms_visible . " <br>";
-		echo "gui_wms_opacity: " . $this->gui_wms_opacity . " <br>";
-		echo "support_sld: " . $this->wms_supportsld . " <br>";
-		
-		for($i=0; $i<count($this->data_type);$i++){
-			echo $this->data_type[$i]. " -> ".$this->data_format[$i]. "<br>";
-		}
-		for($i=0; $i<count($this->objLayer); $i++){
-			echo "<hr>";
-			echo "id: <b>".$this->objLayer[$i]->layer_id ."</b> parent: <b>".$this->objLayer[$i]->layer_parent."</b> name: <b>".$this->objLayer[$i]->layer_name;
-			echo "</b> title: <b>".$this->objLayer[$i]->layer_title. "</b> queryable: <b>".$this->objLayer[$i]->layer_queryable."</b> minScale: <b>". $this->objLayer[$i]->layer_minscale."</b> maxScale: <b>".$this->objLayer[$i]->layer_maxscale."</b>";
-			echo "<br>dataurl: <b>".$this->objLayer[$i]->layer_dataurl_href. "</b>";
-			echo "<br>metadataurl: <b>".$this->objLayer[$i]->layer_metadataurl. "</b>";
-			echo "<table border='1'>";
-			for($j=0; $j<count($this->objLayer[$i]->layer_epsg);$j++){
-				echo "<tr><td>".$this->objLayer[$i]->layer_epsg[$j][epsg]."</td><td>".$this->objLayer[$i]->layer_epsg[$j][minx]."</td>";
-				echo "<td>".$this->objLayer[$i]->layer_epsg[$j][miny]."</td><td>".$this->objLayer[$i]->layer_epsg[$j][maxx]."</td>";
-				echo "<td>".$this->objLayer[$i]->layer_epsg[$j][maxy]."</td></tr>";
-			}
-			echo "</table>";
-			echo "layerstyle:";
-			echo "<table border='1'>";
-			echo "<tr><td>name</td><td>title</td><td>legendurl</td><td>legendurlformat</td></tr>";
-			for($j=0; $j<count($this->objLayer[$i]->layer_style);$j++){
-				echo "<tr><td>".$this->objLayer[$i]->layer_style[$j][name]."</td><td>".$this->objLayer[$i]->layer_style[$j][title]."</td><td>".$this->objLayer[$i]->layer_style[$j][legendurl]."</td><td>".$this->objLayer[$i]->layer_style[$j][legendurlformat]."</td></tr>";
-			}
-			echo "</table>";
-	        echo "<hr>";
-	        echo "<hr>";
-		}
-	} 
-	  function addLayer($id,$parent){	
-		$this->objLayer[count($this->objLayer)] = new layer($id,$parent);
-	  }
-	  /**
-	   * private function
-	   */
-	  function stripEndlineAndCarriageReturn($string) {
-	  	return preg_replace("/\n/", "", preg_replace("/\r/", " ", $string));
-	  }
-	  function createJsObjFromWMS($parent=0){
-	  	if(!$this->wms_title || $this->wms_title == ""){
-			echo "alert('Error: no valid capabilities-document !!');";
-			die; exit;
-		}
-			if($parent){
-				echo "parent.";
-			}
-			// wms_title and abstract have previously been urlencoded
-			// this solution may not yet be the ultimate one
-			
-			$add_wms_string = "add_wms(" .
-					"'" . $this->wms_id ."'," .
-					"'" . $this->wms_version ."'," .
-					"'" . addslashes($this->wms_title) . "'," .
-					"'" . addslashes($this->wms_abstract) ."'," .
-					"'" . $this->wms_getmap ."'," .
-					"'" . $this->wms_getfeatureinfo ."'," .
-					"'" . $this->wms_getlegendurl ."'," .
-					"'" . $this->wms_filter ."'," .
-					"'" . $this->gui_wms_mapformat . "'," .
-					"'" . $this->gui_wms_featureinfoformat . "'," .
-					"'" . $this->gui_wms_exceptionformat . "'," .
-					"'" . $this->gui_wms_epsg ."'," .
-					"'" . $this->gui_wms_visible ."'," .
-					"'" . $this->gui_wms_opacity ."'," .
-					"'" . $this->gui_wms_sldurl ."" .
-					"');";
-			echo $add_wms_string;
-			
-		for($i=0;$i<count($this->data_format);$i++){
-			if($parent){
-				echo "parent.";
-			}		
-			echo "wms_add_data_type_format('". $this->data_type[$i] ."','". $this->data_format[$i] ."');\n";		
-		}
-		for($i=0; $i<count($this->objLayer); $i++){
-			if($parent){
-				echo "parent.";
-			}
-			print ("wms_add_layer('". 
-				$this->objLayer[$i]->layer_parent ."','". 
-				$this->objLayer[$i]->layer_uid ."','". 
-				addslashes($this->objLayer[$i]->layer_name) . "','". 
-				addslashes($this->objLayer[$i]->layer_title) ."','". 
-				$this->objLayer[$i]->layer_dataurl_href ."','". 
-				$this->objLayer[$i]->layer_pos ."','". 
-				$this->objLayer[$i]->layer_queryable ."','". 
-				$this->objLayer[$i]->layer_minscale . "','". 
-				$this->objLayer[$i]->layer_maxscale ."','". 
-				$this->objLayer[$i]->layer_metadataurl ."','". 
-				$this->objLayer[$i]->gui_layer_wms_id ."','". 
-				$this->objLayer[$i]->gui_layer_status ."','".
-				$this->objLayer[$i]->gui_layer_style ."','".  
-				$this->objLayer[$i]->gui_layer_selectable ."','". 
-				$this->objLayer[$i]->gui_layer_visible ."','". 
-				$this->objLayer[$i]->gui_layer_queryable ."','". 
-				$this->objLayer[$i]->gui_layer_querylayer ."','". 
-				$this->objLayer[$i]->gui_layer_minscale ."','". 
-				$this->objLayer[$i]->gui_layer_maxscale ."','".
-				$this->objLayer[$i]->gui_layer_wfs_featuretype ."');\n");
-				
-			for($j=0; $j<count($this->objLayer[$i]->layer_epsg);$j++){
-				if($i==0){
-					if($parent){
-					echo "parent.";
-					}
-					print("wms_addSRS('". 
-						$this->objLayer[$i]->layer_epsg[$j]["epsg"] ."','". 
-						$this->objLayer[$i]->layer_epsg[$j]["minx"] ."','". 
-						$this->objLayer[$i]->layer_epsg[$j]["miny"] ."','". 
-						$this->objLayer[$i]->layer_epsg[$j]["maxx"] ."','". 
-						$this->objLayer[$i]->layer_epsg[$j]["maxy"] ."');\n");
-				}
-				if($parent){
-				echo "parent.";
-				}
-				print("layer_addEpsg('". 
-					$this->objLayer[$i]->layer_epsg[$j]["epsg"] ."','". 
-					$this->objLayer[$i]->layer_epsg[$j]["minx"] ."','". 
-					$this->objLayer[$i]->layer_epsg[$j]["miny"] ."','". 
-					$this->objLayer[$i]->layer_epsg[$j]["maxx"] ."','". 
-					$this->objLayer[$i]->layer_epsg[$j]["maxy"] ."');\n");
-			}
-			for($j=0; $j<count($this->objLayer[$i]->layer_style);$j++){
-				if($parent){
-				echo "parent.";
-				}
-				print("wms_addLayerStyle('".$this->objLayer[$i]->layer_style[$j]["name"]."', '".$this->objLayer[$i]->layer_style[$j]["title"]."', ".$j.",".$i.",'".$this->objLayer[$i]->layer_style[$j]["legendurl"]."', '".$this->objLayer[$i]->layer_style[$j]["legendformat"]."');\n");
-			}
-		}
-	  }
-	  
-	  function createJsLayerObjFromWMS($parent=0, $layer_name){
-	  	if(!$this->wms_title || $this->wms_title == ""){
-			echo " alert('Error: no valid capabilities-document !!');";
-			die; exit;
-		}
-			if($parent){
-				echo "parent.";
-			}
-			// wms_title and abstract have previously been urlencoded
-			// this solution may not yet be the ultimate one
-			print("add_wms('". 
-			$this->wms_id ."','".
-			$this->wms_version ."','".
-			preg_replace("/'/", "", $this->wms_title) ."','".
-			preg_replace("/'/", "", $this->wms_abstract) ."','". 
-			$this->wms_getmap ."','" .
-			$this->wms_getfeatureinfo ."','".
-			$this->wms_getlegendurl ."','".
-			$this->wms_filter ."','".
-			$this->gui_wms_mapformat ."','". 
-			$this->gui_wms_featureinfoformat ."','". 
-			$this->gui_wms_exceptionformat . "','". 
-			$this->gui_wms_epsg ."','". 
-			$this->gui_wms_visible ."','".
-			$this->gui_wms_opacity ."','".
-			$this->gui_wms_sldurl ."');\n");
-			
-		for($i=0;$i<count($this->data_format);$i++){
-			if($parent){
-				echo "parent.";
-			}		
-			echo "wms_add_data_type_format('". $this->data_type[$i] ."','". $this->data_format[$i] ."');\n";		
-		}
-		for($i=0; $i<count($this->objLayer); $i++){
-			if($this->objLayer[$i]->layer_name == $layer_name|| $this->objLayer[$i]->layer_pos == 0){
-			
-				if($parent){
-					echo "parent.";
-				}
-			 print ("wms_add_layer('". 
-				$this->objLayer[$i]->layer_parent ."','". 
-				$this->objLayer[$i]->layer_uid ."','". 
-				$this->objLayer[$i]->layer_name . "','". 
-				addslashes($this->objLayer[$i]->layer_title) ."','". 
-				$this->objLayer[$i]->layer_dataurl_href ."','". 
-				$this->objLayer[$i]->layer_pos ."','". 
-				$this->objLayer[$i]->layer_queryable ."','". 
-				$this->objLayer[$i]->layer_minscale . "','". 
-				$this->objLayer[$i]->layer_maxscale ."','". 
-				$this->objLayer[$i]->layer_metadataurl ."','". 
-				$this->objLayer[$i]->gui_layer_wms_id ."','". 
-				$this->objLayer[$i]->gui_layer_status ."','".
-				$this->objLayer[$i]->gui_layer_style ."','". 
-				$this->objLayer[$i]->gui_layer_selectable ."','". 
-				$this->objLayer[$i]->gui_layer_visible ."','". 
-				$this->objLayer[$i]->gui_layer_queryable ."','". 
-				$this->objLayer[$i]->gui_layer_querylayer ."','". 
-				$this->objLayer[$i]->gui_layer_minscale ."','". 
-				$this->objLayer[$i]->gui_layer_maxscale ."','".
-				$this->objLayer[$i]->gui_layer_wfs_featuretype ."');\n");
-			for($j=0; $j<count($this->objLayer[$i]->layer_epsg);$j++){
-				if($i==0){
-					if($parent){
-					echo "parent.";
-					}
-					print("wms_addSRS('". 
-						$this->objLayer[$i]->layer_epsg[$j]["epsg"] ."','". 
-						$this->objLayer[$i]->layer_epsg[$j]["minx"] ."','". 
-						$this->objLayer[$i]->layer_epsg[$j]["miny"] ."','". 
-						$this->objLayer[$i]->layer_epsg[$j]["maxx"] ."','". 
-						$this->objLayer[$i]->layer_epsg[$j]["maxy"] ."');\n");
-				}
-				if($parent){
-				echo "parent.";
-				}
-				print("layer_addEpsg('". 
-					$this->objLayer[$i]->layer_epsg[$j]["epsg"] ."','". 
-					$this->objLayer[$i]->layer_epsg[$j]["minx"] ."','". 
-					$this->objLayer[$i]->layer_epsg[$j]["miny"] ."','". 
-					$this->objLayer[$i]->layer_epsg[$j]["maxx"] ."','". 
-					$this->objLayer[$i]->layer_epsg[$j]["maxy"] ."');\n");
-			}
-			for($j=0; $j<count($this->objLayer[$i]->layer_style);$j++){
-				if($parent){
-				echo "parent.";
-				}
-				print("wms_addLayerStyle('".$this->objLayer[$i]->layer_style[$j]["name"]."', '".$this->objLayer[$i]->layer_style[$j]["title"]."', ".$j.",".$i.",'".$this->objLayer[$i]->layer_style[$j]["legendurl"]."', '".$this->objLayer[$i]->layer_style[$j]["legendformat"]."');\n");
-			}
-		   }	
-		}
-	  }
-	  
-	  
-	/**
-	* writeObjInDB
-	*
-	* this function exports the information from the xml to the mapbender database 
-	*/
-	function writeObjInDB($gui_id){
-		global $con;
-		
-		$this->checkObj();
-		db_begin();
-	
-		# TABLE wms
-		$sql = "INSERT INTO wms (wms_version, wms_title, wms_abstract, wms_getcapabilities, wms_getmap, ";
-		$sql.= "wms_getfeatureinfo, wms_getlegendurl, wms_getcapabilities_doc, wms_upload_url, fees, ";
-		$sql .= "accessconstraints, contactperson, contactposition, contactorganization, address, city, ";
-		$sql .= "stateorprovince, postcode, country, contactvoicetelephone, contactfacsimiletelephone, contactelectronicmailaddress, ";
-		$sql .= "wms_owner,wms_timestamp, ";
-		$sql .= "wms_supportsld, wms_userlayer, wms_userstyle, wms_remotewfs) ";
-		$sql .= "VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28)";
-		$v = array($this->wms_version,$this->wms_title,$this->wms_abstract,$this->wms_getcapabilities,
-			$this->wms_getmap,$this->wms_getfeatureinfo,$this->wms_getlegendurl,$this->wms_getcapabilities_doc,
-			$this->wms_upload_url,$this->fees,$this->accessconstraints,$this->contactperson,$this->contactposition,
-			$this->contactorganization,$this->address,$this->city,$this->stateorprovince,$this->postcode,$this->country,
-			$this->contactvoicetelephone,$this->contactfacsimiletelephone,$this->contactelectronicmailaddress,
-			$_SESSION['mb_user_id'],strtotime("now"),
-			$this->wms_supportsld,$this->wms_userlayer,$this->wms_userstyle,$this->wms_remotewfs );
-		$t = array('s','s','s','s','s','s','s','s','s','s','s','s','s','s','s','s','s','s','s','s','s','s','i','i','s','s','s','s');
-		$res = db_prep_query($sql,$v,$t);
-		if(!$res){
-			db_rollback();
-		}
-		
-		$myWMS = db_insert_id($con,'wms', 'wms_id');
-		
-		# TABLE layer and gui_layer
-		
-		for($i=0; $i<count($this->objLayer); $i++){
-			$this->insertLayer($i,$myWMS,$gui_id);
-			$this->insertGuiLayer($i,$myWMS,$gui_id);
-		}	
-			
-		
-		#TABLE wms_srs
-		$this->insertSRS($myWMS);	
-		
-		# TABLE wms_format	
-		$this->insertFormat($myWMS);	
-			
-		# TABLE gui_wms
-		
-		$sql ="SELECT MAX(gui_wms_position) AS pos FROM gui_wms WHERE fkey_gui_id = $1";
-		$v = array($gui_id);
-		$t = array('s');
-		$res = db_prep_query($sql,$v,$t);
-		if(db_result($res, 0,"pos") > -1){
-			$position = db_result($res, 0,"pos") + 1;
-		} else{ $position = 0; }
-		
-		$sql ="INSERT INTO gui_wms (fkey_gui_id, fkey_wms_id, gui_wms_position, gui_wms_mapformat, ";
-		$sql .= "gui_wms_featureinfoformat, gui_wms_exceptionformat, gui_wms_epsg)";
-		$sql .= "VALUES($1,$2,$3,$4,$5,$6,$7)";
-		$v = array($gui_id,$myWMS,$position,$this->gui_wms_mapformat,$this->gui_wms_featureinfoformat,
-				$this->gui_wms_exceptionformat,$this->gui_wms_epsg
-				);
-		$t = array('s','i','i','s','s','s','s');
-		$res = db_prep_query($sql,$v,$t);
-		if(!$res){
-			db_rollback();	
-		}
-		db_commit();
-	    
-	    #Changes JW
-	    $this->wms_id = $myWMS;
-	}
-	function insertLayer($i,$myWMS){
-		global $con;
-		$sql = "INSERT INTO layer(fkey_wms_id, layer_pos, layer_parent, layer_name, layer_title, ";
-		$sql .= " layer_queryable, layer_minscale, layer_maxscale,layer_dataurl,layer_metadataurl,layer_abstract) ";
-		$sql .= "VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)";
-		if($this->objLayer[$i]->layer_id != null){
-			$tmpPos =  $this->objLayer[$i]->layer_id;
-		}
-		else {
-			$tmpPos .= 0;
-		}
-		if($this->objLayer[$i]->layer_parent == '' && $this->objLayer[$i]->layer_parent != '0'){
-			$this->objLayer[$i]->layer_parent = '';
-		}
-		$v = array($myWMS,$tmpPos,$this->objLayer[$i]->layer_parent,$this->objLayer[$i]->layer_name,
-				$this->objLayer[$i]->layer_title,
-				$this->objLayer[$i]->layer_queryable,$this->objLayer[$i]->layer_minscale,
-				$this->objLayer[$i]->layer_maxscale,$this->objLayer[$i]->layer_dataurl_href,
-				$this->objLayer[$i]->layer_metadataurl,$this->objLayer[$i]->layer_abstract);
-		$t = array('i','i','s','s','s','i','i','i','s','s','s');
-		$res = db_prep_query($sql,$v,$t);
-		if(!$res){
-			db_rollback();	
-		}
-		else {
-			# save the id of each layer: set param2 true		
-			$this->objLayer[$i]->db_id = db_insert_id($con, 'layer','layer_id');
-			$this->insertLayerEPSG($i);
-			
-			# TABLE layer_style for each layer
-			$this->insertLayerStyle($i);
-			
-			# insert Keywords
-			$this->insertLayerKeyword($i);	
-		
-		}
-	}
-	function updateLayer($i,$myWMS){
-		$sql = "SELECT layer_id FROM layer WHERE fkey_wms_id = $1 AND layer_name = $2";
-		$v = array($myWMS,$this->objLayer[$i]->layer_name);
-		$t = array('i','s');
-		$res = db_prep_query($sql,$v,$t);
-		if($row = db_fetch_array($res)){
-			$l_id = $row['layer_id'];	
-		}
-		else{
-			db_rollback();
-			$e = new mb_exception("Not found: ".$this->objLayer[$i]->layer_name);
-			return;	
-		}	
-		
-		$sql = "UPDATE layer SET ";
-		$sql .= "layer_pos = $1, ";
-		$sql .= "layer_parent = $2, ";
-		$sql .= "layer_title = $3, ";
-		$sql .= "layer_queryable = $4, ";
-		$sql .= "layer_minscale = $5, ";
-		$sql .= "layer_maxscale = $6, ";
-		$sql .= "layer_dataurl = $7, ";
-		$sql .= "layer_metadataurl = $8, ";
-		$sql .= "layer_abstract = $9 ";
-		$sql .= "WHERE layer_id = $10";
-		
-		if($this->objLayer[$i]->layer_id != null){
-			$tmpPos =  $this->objLayer[$i]->layer_id;
-		}
-		else {
-			$tmpPos .= 0;
-		}
-		if($this->objLayer[$i]->layer_parent == '' && $this->objLayer[$i]->layer_parent != '0'){
-			$this->objLayer[$i]->layer_parent = '';
-		}
-		$v = array($tmpPos,$this->objLayer[$i]->layer_parent,
-				$this->objLayer[$i]->layer_title,
-				$this->objLayer[$i]->layer_queryable,$this->objLayer[$i]->layer_minscale,
-				$this->objLayer[$i]->layer_maxscale,$this->objLayer[$i]->layer_dataurl_href,
-				$this->objLayer[$i]->layer_metadataurl,$this->objLayer[$i]->layer_abstract, $l_id		
-			);
-		$t = array('i','s','s','i','i','i','s','s','s','i');
-		$res = db_prep_query($sql,$v,$t);
-		if($this->overwrite == true){
-			$sql = "UPDATE layer SET ";
-			$sql .= "layer_title = $1, ";
-			$sql .= "layer_abstract = $2 ";
-			$sql .= "WHERE layer_id = $3";
-			
-			$v = array($this->objLayer[$i]->layer_title,$this->objLayer[$i]->layer_abstract, $l_id);
-			$t = array('s','s','i');
-			$res = db_prep_query($sql,$v,$t);
-		}
-		if(!$res){
-			db_rollback();	
-		}
-		else {
-			
-			# save the id of each layer: set param2 true
-			$this->objLayer[$i]->db_id = $l_id;
-			$this->insertLayerEPSG($i);
-			
-			# TABLE layer_style for each layer
-			$this->insertLayerStyle($i);
-			if($this->overwrite == true){
-				$this->insertLayerKeyword($i);
-			}
-		}
-	}
-	function insertGuiLayer($i,$myWMS,$gui_id){
-		# table gui_layer
-		
-		$sql = "INSERT INTO gui_layer (fkey_gui_id, fkey_layer_id, gui_layer_wms_id, ";
-		$sql .= "gui_layer_status, gui_layer_selectable, gui_layer_visible, gui_layer_queryable, ";
-		$sql .= "gui_layer_querylayer,gui_layer_minscale,gui_layer_maxscale, gui_layer_priority, gui_layer_style) ";
-		$sql .= "VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12)";
-		if(count($this->objLayer[$i]->layer_style)>0){
-			$layer_style_name = $this->objLayer[$i]->layer_style[0]["name"];
-		}
-		else{
-			$layer_style_name = NULL;
-		}
-		$v = array($gui_id,$this->objLayer[$i]->db_id,$myWMS,1,1,1,$this->objLayer[$i]->layer_queryable,
-			$this->objLayer[$i]->layer_queryable,$this->objLayer[$i]->layer_minscale,$this->objLayer[$i]->layer_maxscale,$i,$layer_style_name);
-		$t = array('s','i','i','i','i','i','i','i','i','i','i','s');
-		$res = db_prep_query($sql,$v,$t);
-		#$e = new mb_exception("name des insert styles und fkey_layer_id: ".$layer_style_name." --- ".$this->objLayer[$i]->db_id);
-		if(!$res){
-			db_rollback();	
-		}	
-	}
-	function appendGuiLayer($i,$myWMS,$gui_id){
-		# table gui_layer
-		
-		$sql = "INSERT INTO gui_layer (fkey_gui_id, fkey_layer_id, gui_layer_wms_id, ";
-		$sql .= "gui_layer_status, gui_layer_selectable, gui_layer_visible, gui_layer_queryable, ";
-		$sql .= "gui_layer_querylayer,gui_layer_minscale,gui_layer_maxscale, gui_layer_priority, gui_layer_style) ";
-		$sql .= "VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12)";
-		if(count($this->objLayer[$i]->layer_style)>0){
-			$layer_style_name = $this->objLayer[$i]->layer_style[0]["name"];
-		}
-		else{
-			$layer_style_name = NULL;
-		}
-		$v = array($gui_id,$this->objLayer[$i]->db_id,$myWMS,0,0,0,$this->objLayer[$i]->layer_queryable,
-			$this->objLayer[$i]->layer_queryable,$this->objLayer[$i]->layer_minscale,$this->objLayer[$i]->layer_maxscale,$i,$layer_style_name);
-		$t = array('s','i','i','i','i','i','i','i','i','i','i','s');
-		$res = db_prep_query($sql,$v,$t);
-		if(!$res){
-			db_rollback();	
-		}	
-	}
-	function insertSRS($myWMS){
-		for($i=0; $i<count($this->wms_srs);$i++){
-			$sql ="INSERT INTO wms_srs (fkey_wms_id, wms_srs) values($1,$2)";		
-			$v = array($myWMS,mb_strtoupper($this->wms_srs[$i]));
-			$t = array('i','s');		
-			$res = db_prep_query($sql,$v,$t);
-			if(!$res){
-				db_rollback();	
-			}
-		}	
-	}
-	function insertFormat($myWMS){
-		for($i=0; $i<count($this->data_type);$i++){
-			$sql ="INSERT INTO wms_format (fkey_wms_id, data_type, data_format) ";
-			$sql .= " VALUES($1,$2,$3)";
-			$v = array($myWMS,$this->data_type[$i],$this->data_format[$i]);
-			$t = array('i','s','s');
-			$res = db_prep_query($sql,$v,$t);
-			if(!$res){
-				db_rollback();	
-			}
-		}	
-	}
-	function insertLayerEPSG($i){
-		$sql = "DELETE FROM layer_epsg WHERE fkey_layer_id = $1";
-		$v = array($this->objLayer[$i]->db_id);
-		$t = array('i');
-		$res = db_prep_query($sql,$v,$t);
-		for($j=0; $j<count($this->objLayer[$i]->layer_epsg);$j++){
-			$sql = "INSERT INTO layer_epsg (fkey_layer_id, epsg, minx, miny, maxx, maxy) ";
-			$sql .= "VALUES($1,$2,$3,$4,$5,$6)";
-			$v = array($this->objLayer[$i]->db_id,$this->objLayer[$i]->layer_epsg[$j][epsg],
-				$this->objLayer[$i]->layer_epsg[$j][minx],$this->objLayer[$i]->layer_epsg[$j][miny],
-				$this->objLayer[$i]->layer_epsg[$j][maxx],$this->objLayer[$i]->layer_epsg[$j][maxy]
-				); 
-			$t = array('i','s','d','d','d','d');
-			$res = db_prep_query($sql,$v,$t);
-			if(!$res){
-				db_rollback();	
-			}
-		}
-	}
-	function insertLayerStyle($i){
-		$sql = "DELETE FROM layer_style WHERE fkey_layer_id = $1";
-		$v = array($this->objLayer[$i]->db_id);
-		$t = array('i');
-		$res = db_prep_query($sql,$v,$t);
-		for($j=0; $j<count($this->objLayer[$i]->layer_style);$j++){
-			$sql = "INSERT INTO layer_style (fkey_layer_id, name, title, legendurl, legendurlformat) ";
-			$sql .= "VALUES($1,$2,$3,$4,$5)";
-			$v = array($this->objLayer[$i]->db_id,$this->objLayer[$i]->layer_style[$j]["name"],
-					$this->objLayer[$i]->layer_style[$j]["title"],$this->objLayer[$i]->layer_style[$j]["legendurl"],
-					$this->objLayer[$i]->layer_style[$j]["legendurlformat"]				
-				);
-			$t = array('i','s','s','s','s');
-			$res = db_prep_query($sql,$v,$t);
-			if(!$res){
-				db_rollback();	
-			}
-		}
-	}
-	function insertLayerKeyword($i){
-		global $con;
-		$sql = "DELETE FROM layer_keyword WHERE fkey_layer_id = $1";
-		$v = array($this->objLayer[$i]->db_id);
-		$t = array('i');
-		$res = db_prep_query($sql,$v,$t);
-		
-//		var_dump($this);
-		$k = $this->objLayer[$i]->layer_keyword;
-//		var_dump($k);
-		for($j=0; $j<count($k); $j++){
-			$keyword_id = "";
-			
-			while ($keyword_id == "") {
-				$sql = "SELECT keyword_id FROM keyword WHERE UPPER(keyword) = UPPER($1)";
-				$v = array($k[$j]);
-				$t = array('s');
-				$res = db_prep_query($sql,$v,$t);
-				$row = db_fetch_array($res);
-			//print_r($row);
-				if ($row) {
-					$keyword_id = $row["keyword_id"];	
-				}
-				else {
-					$sql_insertKeyword = "INSERT INTO keyword (keyword)";
-					$sql_insertKeyword .= "VALUES ($1)";
-					$v1 = array($k[$j]);
-					$t1 = array('s');
-					$res_insertKeyword = db_prep_query($sql_insertKeyword,$v1,$t1);
-					if(!$res_insertKeyword){
-						db_rollback();	
-					}
-				}
-			}
-
-			// check if layer/keyword combination already exists
-			$sql_layerKeywordExists = "SELECT * FROM layer_keyword WHERE fkey_layer_id = $1 AND fkey_keyword_id = $2";
-			$v = array($this->objLayer[$i]->db_id, $keyword_id);
-			$t = array('i', 'i');
-			$res_layerKeywordExists = db_prep_query($sql_layerKeywordExists, $v, $t);
-			$row = db_fetch_array($res_layerKeywordExists);
-			//print_r($row);
-			if (!$row) {
-				$sql1 = "INSERT INTO layer_keyword (fkey_keyword_id,fkey_layer_id)";
-				$sql1 .= "VALUES ($1,$2)";
-				$v1 = array($keyword_id,$this->objLayer[$i]->db_id);
-				$t1 = array('i','i');
-				$res1 = db_prep_query($sql1,$v1,$t1);
-				if(!$res1){
-					db_rollback();	
-				}
-			}
-		}
-	}
-	function updateObjInDB($myWMS){
-		db_begin();
-		
-		$sql = "UPDATE wms SET ";
-		$sql .= "wms_version = $1 ,";
-		$sql .= "wms_getcapabilities  = $2 ,";
-		$sql .= "wms_getmap  = $3 ,";
-		$sql .= "wms_getfeatureinfo  = $4 ,";
-		$sql .= "wms_getlegendurl  = $5 ,";
-		$sql .= "wms_getcapabilities_doc = $6 ,";
-		$sql .= "wms_upload_url = $7,  ";
-		$sql .= "wms_owner = $8, ";
-		$sql .= "wms_timestamp = $9, ";
-		$sql .= "wms_supportsld = $10, ";
-		$sql .= "wms_userlayer = $11, ";
-		$sql .= "wms_userstyle = $12, ";
-		$sql .= "wms_remotewfs = $13 ";
-		$sql .= " WHERE wms_id = $14";
-	
-		$v = array($this->wms_version,$this->wms_getcapabilities,
-			$this->wms_getmap,$this->wms_getfeatureinfo,$this->wms_getlegendurl,
-			$this->wms_getcapabilities_doc,$this->wms_upload_url,$_SESSION["mb_user_id"],strtotime("now"),
-			$this->wms_supportsld,$this->wms_userlayer,$this->wms_userstyle,$this->wms_remotewfs,$myWMS);
-		$t = array('s','s','s','s','s','s','s','i','i','s','s','s','s','i');
-	
-		$res = db_prep_query($sql,$v,$t);
-		if(!$res){
-			db_rollback();	
-		}
-		
-		if($this->overwrite == true){
-			$sql = "UPDATE wms SET ";
-			$sql .= "wms_title  = $1 ,";
-			$sql .= "wms_abstract  = $2 ,";
-			$sql .= "fees = $3, ";
-			$sql .= "accessconstraints = $4, ";
-			$sql .= "contactperson = $5, ";
-			$sql .= "contactposition = $6, ";
-			$sql .= "contactorganization = $7, ";
-			$sql .= "address = $8, ";
-			$sql .= "city = $9, ";
-			$sql .= "stateorprovince = $10, ";
-			$sql .= "postcode = $11, ";
-			$sql .= "country = $12, ";
-			$sql .= "contactvoicetelephone = $13, ";
-			$sql .= "contactfacsimiletelephone = $14, ";
-			$sql .= "contactelectronicmailaddress = $15 ";
-			$sql .= " WHERE wms_id = $16";
-		
-			$v = array($this->wms_title,$this->wms_abstract,$this->fees,$this->accessconstraints,
-				$this->contactperson,$this->contactposition,$this->contactorganization,$this->address,
-				$this->city,$this->stateorprovince,$this->postcode,$this->country,$this->contactvoicetelephone,
-				$this->contactfacsimiletelephone,$this->contactelectronicmailaddress,$myWMS);
-			$t = array('s','s','s','s','s','s','s','s','s','s','s','s','s','s','s','i');
-			$res = db_prep_query($sql,$v,$t);
-			if(!$res){
-				db_rollback();	
-			}
-		}
-		
-		# delete and refill srs and formats
-		$sql = "DELETE FROM wms_srs WHERE fkey_wms_id = $1 ";
-		$v = array($myWMS);
-		$t = array('i');
-		$res = db_prep_query($sql,$v,$t);
-		if(!$res){
-			db_rollback();	
-		}
-		$this->insertSRS($myWMS);
-		
-		$sql = "DELETE FROM wms_format WHERE fkey_wms_id = $1 ";
-		$v = array($myWMS);
-		$t = array('i');
-		$res = db_prep_query($sql,$v,$t);
-		if(!$res){
-			db_rollback();	
-		}
-		$this->insertFormat($myWMS);
-		
-		# update gui_wms
-		$this->update_gui_wms($myWMS);
-		
-		# update TABLE layer	
-		# delete all layer which are outdated
-		$v = array($myWMS);
-		$t = array('i');
-		$c = 2;
-		$sql = "DELETE FROM layer WHERE fkey_wms_id = $1 AND NOT layer_name IN(";
-		for($i=0; $i<count($this->objLayer); $i++){
-			if($i>0){$sql .= ',';}
-			$sql .= "$".$c;
-			array_push($v,$this->objLayer[$i]->layer_name);
-			array_push($t,'s');		
-			$c++;
-		}
-		$sql .= ")";
-		
-		$res = db_prep_query($sql,$v,$t);
-		if(!$res){
-			db_rollback();	
-		}
-			
-		# update or insert?
-		$sql = "SELECT layer_name FROM layer WHERE fkey_wms_id = $1";
-		$v = array($myWMS);
-		$t = array('i');
-		$res = db_prep_query($sql,$v,$t);
-		$exLayer = array();
-		while($row = db_fetch_array($res)){
-			array_push($exLayer,$row["layer_name"]);
-		}
-		
-		$sql = "SELECT fkey_gui_id FROM gui_wms WHERE fkey_wms_id = $1";
-		$v = array($myWMS);
-		$t = array('i');
-		$res = db_prep_query($sql,$v,$t);
-		$exGui = array();
-		while($row = db_fetch_array($res)){
-			array_push($exGui,$row["fkey_gui_id"]);
-		}
-		
-		for($i=0; $i<count($this->objLayer); $i++){
-			if(in_array($this->objLayer[$i]->layer_name,$exLayer)){
-				//echo "<br>update: ".$this->objLayer[$i]->layer_name;
-				$this->updateLayer($i,$myWMS);
-				for($j=0; $j<count($exGui); $j++){
-					$this->updateGuiLayer($i,$myWMS,$exGui[$j]);
-				}
-			}
-			else{
-				//echo "<br>append: ".$this->objLayer[$i]->layer_name;
-				$this->insertLayer($i,$myWMS);
-				for($j=0; $j<count($exGui); $j++){
-					$this->appendGuiLayer($i,$myWMS,$exGui[$j]);
-				}
-			}
-		}
-		db_commit();
-		return;	
-	}
-	function updateGuiLayer($i,$myWMS,$gui_id){
-		$sql = "SELECT layer_id FROM layer WHERE fkey_wms_id = $1 AND layer_name = $2";
-		$v = array($myWMS,$this->objLayer[$i]->layer_name);
-		$t = array('i','s');
-		$res = db_prep_query($sql,$v,$t);
-		if($row = db_fetch_array($res)){
-			$l_id = $row['layer_id'];	
-		}
-		else{
-			db_rollback();
-			$e = new mb_exception("Not found: ".$this->objLayer[$i]->layer_name. " in gui: ".$gui_id);
-			return;	
-		}
-		
-		$sql = "SELECT * FROM gui_layer WHERE fkey_layer_id = $1 and fkey_gui_id = $2";
-		$v = array($l_id,$gui_id);
-		$t = array('i','s');
-		$res = db_prep_query($sql,$v,$t);		
-		while($row = db_fetch_array($res)){
-			if($this->objLayer[$i]->layer_queryable == 0){
-				$sql1 = "UPDATE gui_layer set gui_layer_queryable = 0, gui_layer_querylayer = 0 ";
-				$sql1 .= "WHERE fkey_layer_id = $1 and fkey_gui_id = $2";
-				$v = array($l_id,$gui_id);
-				$t = array('i','s');
-				$res1 = db_prep_query($sql1,$v,$t);
-				if(!$res1){
-					
-				db_rollback();
-				}
-			}
-			if($this->objLayer[$i]->layer_queryable == 1){
-				$sql1 = "UPDATE gui_layer set gui_layer_queryable = 1 ";
-				$sql1 .= "WHERE fkey_layer_id = $1 and fkey_gui_id = $2";
-				$v = array($l_id,$gui_id);
-				$t = array('i','s');
-				$res1 = db_prep_query($sql1,$v,$t);
-				if(!$res1){
-					
-					db_rollback();
-				}
-			}
-			if($row["gui_layer_minscale"] < $this->objLayer[$i]->layer_minscale){
-				$sql1 = "UPDATE gui_layer set gui_layer_minscale = $1 ";
-				$sql1 .= "WHERE fkey_layer_id = $2 and fkey_gui_id = $3";
-				$v = array($this->objLayer[$i]->layer_minscale,$l_id,$gui_id);
-				$t = array('i','i','s');
-				$res1 = db_prep_query($sql1,$v,$t);
-				if(!$res1){db_rollback();
-				}
-			}
-			if($row["gui_layer_maxscale"] > $this->objLayer[$i]->layer_maxscale){
-				$sql1 = "UPDATE gui_layer set gui_layer_maxscale = $1 ";
-				$sql1 .= "WHERE fkey_layer_id = $2 and fkey_gui_id = $3";
-				$v = array($this->objLayer[$i]->layer_maxscale,$l_id,$gui_id);
-				$t = array('i','i','s');
-				$res1 = db_prep_query($sql1,$v,$t);
-				if(!$res1){db_rollback();
-				}
-			}		
-		}
-	}
-	function update_gui_wms($myWMS){
-		$mySubmit = null;
-		$sql = "SELECT * FROM gui_wms where fkey_wms_id = $1";
-		$v = array($myWMS);
-		$t = array('i');
-		$res = db_prep_query($sql,$v,$t);
-		$cnt = 0;
-		while($row = db_fetch_array($res)){	
-			unset($mySubmit);
-			$myGUI[$cnt] = $row["fkey_gui_id"];
-
-			$sql = "UPDATE gui_wms SET ";
-			$v = array();
-			$t = array();
-			$paramCount = 0;		
-
-			for($i=0; $i<count($this->data_type); $i++){
-				# gui_wms_mapformat
-				if(mb_strtolower($this->data_type[$i]) == "map" && mb_strtolower($this->data_format[$i]) == mb_strtolower($row["gui_wms_mapformat"])){
-					$myMapFormat = true;
-				}
-				# gui_wms_featureinfoformat
-				if(mb_strtolower($this->data_type[$i]) == "featureinfo" && mb_strtolower($this->data_format[$i]) == mb_strtolower($row["gui_wms_featureinfoformat"])){
-					$myFeatureInfoFormat = true;
-				}
-				# gui_wms_exceptionformat
-				if(mb_strtolower($this->data_type[$i]) == "exception" && mb_strtolower($this->data_format[$i]) == mb_strtolower($row["gui_wms_exceptionformat"])){
-					$myExceptionFormat = true;
-				}
-			}
-			if(!$myMapFormat){
-				$paramCount++;
-				$sql .= "gui_wms_mapformat = $" . $paramCount . " ";
-				$mySubmit = true;
-				array_push($v, $this->gui_wms_mapformat);
-				array_push($t, "s");
-			}
-			if(!$myFeatureInfoFormat){
-				if($mySubmit){ $sql .= ",";}
-				$paramCount++;
-				$sql .= "gui_wms_featureinfoformat = $" . $paramCount . " ";
-				array_push($v, $this->gui_wms_featureinfoformat);
-				array_push($t, "s");
-				$mySubmit = true;
-			}
-			if(!$myExceptionFormat){
-				if($mySubmit){ $sql .= ",";}
-				$paramCount++;
-				$sql .= "gui_wms_exceptionformat = $" . $paramCount ." ";
-				array_push($v, $this->gui_wms_exceptionformat);
-				array_push($t, "s");
-				$mySubmit = true;
-			}
-				
-			# gui_wms_epsg
-			for($j=0; $j<count($this->objLayer[0]->layer_epsg);$j++){
-				if($this->objLayer[0]->layer_epsg[$j][epsg] == mb_strtoupper($row["gui_wms_epsg"])){
-					$myGUI_EPSG = true;
-				}
-			}
-			if(!$myGUI_EPSG){
-				if($mySubmit){ $sql .= ",";}
-				$paramCount++;
-				$sql .= "gui_wms_epsg = $" . $paramCount . " ";
-				array_push($v, $this->gui_wms_epsg);
-				array_push($t, "s");
-				$mySubmit = true;
-			}
-			$paramCount++;
-			$sql .= " WHERE fkey_gui_id = $" . $paramCount . " ";
-			array_push($v, $row["fkey_gui_id"]);
-			array_push($t, "s");
-
-			$paramCount++;
-			$sql .= "AND fkey_wms_id = $" . $paramCount;
-			array_push($v, $myWMS);
-			array_push($t, "i");
-			if($mySubmit){
-				$res = db_prep_query($sql,$v,$t);
-				if(!$res){
-					db_rollback();	
-					echo "<pre>".$sql."</pre><br> <br><p>";
-				 	echo db_error(); 
-				 	echo "<br /> UPDATE ERROR -> KILL PROCESS AND ROLLBACK....................no update<br><br>";
-					$e = new mb_exception("class_wms.php: transaction: Transaction aborted, rollback.");
-				}
-			}
-			$cnt++;
-		}	
-	}
-	function getVersion() {
-		return $this->wms_version;
-	}
-	
-	function getCapabilities() {
-		return $this->wms_getcapabilities;
-	}
-	
-	function getCapabilitiesDoc() {
-		return $this->wms_getcapabilities_doc;
-	}
-
-	/**
-	* creatObjfromDB
-	*
-	*/ 
-	  function createObjFromDB($gui_id,$wms_id){
-	
-		$sql = "Select * from gui_wms where fkey_wms_id = $1 AND fkey_gui_id = $2";
-		$v = array($wms_id,$gui_id);
-		$t = array('i','s');
-		$res = db_prep_query($sql,$v,$t);
-		
-		$count=0;
-		#$res_count=db_num_rows($res);
-	    
-	
-		while($row = db_fetch_array($res)){
-			$this->gui_wms_mapformat=$row["gui_wms_mapformat"];
-			$this->gui_wms_featureinfoformat=$row["gui_wms_featureinfoformat"];
-			$this->gui_wms_exceptionformat=$row["gui_wms_exceptionformat"];
-			$this->gui_wms_epsg=$row["gui_wms_epsg"];
-			$this->gui_wms_visible = $row["gui_wms_visible"];
-			$this->gui_wms_opacity = $row["gui_wms_opacity"];
-			$this->gui_wms_sldurl = $row["gui_wms_sldurl"];
-	  
-			$sql = "Select * from wms where wms_id = $1 ";
-			$v = array($wms_id);
-			$t = array('i');
-			$res_wms = db_prep_query($sql,$v,$t);
-			$count_wms=0;
-			while($row2 = db_fetch_array($res_wms)){
-				$this->wms_id = $row2["wms_id"];
-				$this->wms_version = $row2["wms_version"];
-				$this->wms_title = $this->stripEndlineAndCarriageReturn($row2["wms_title"]);
-				$this->wms_abstract = $this->stripEndlineAndCarriageReturn($row2["wms_abstract"]);
-				$wmsowsproxy = $row2["wms_owsproxy"];
-				#$wmsowsproxy = "test";
-				if($wmsowsproxy != ""){
-					$owsproxyurl = OWSPROXY."/".session_id()."/".$wmsowsproxy."?";
-					$this->wms_getmap = $owsproxyurl;
-					$this->wms_getcapabilities =  $owsproxyurl;
-					$this->wms_getfeatureinfo = $owsproxyurl;
-					$this->wms_getlegendurl = $owsproxyurl;
-				}
-				else{
-					$this->wms_getmap =  $row2["wms_getmap"];
-					$this->wms_getcapabilities =  $row2["wms_getcapabilities"];
-					$this->wms_getfeatureinfo = $row2["wms_getfeatureinfo"];
-					$this->wms_getlegendurl = $row2["wms_getlegendurl"];
-				}			
-				$this->wms_getcapabilities_doc = $row2["wms_getcapabilities_doc"];
-				$this->wms_filter = $row2["wms_filter"];
-				$this->wms_supportsld = $row2["wms_supportsld"];
-				$this->wms_userlayer = $row2["wms_userlayer"];
-				$this->wms_userstyle = $row2["wms_userstyle"];
-				$this->wms_remotewfs = $row2["wms_remotewfs"];
-				
-				$count_wms++;
-			}
-	
-			### formats
-			$sql = "SELECT * FROM wms_format WHERE fkey_wms_id = $1 ";
-			$v = array($wms_id);
-			$t = array('i'); 
-			$res_wms = db_prep_query($sql,$v,$t);
-			$count_format=0;		
-			while($row3 = db_fetch_array($res_wms)){		
-				$this->data_type[$count_format] = $row3["data_type"];
-				$this->data_format[$count_format] = $row3["data_format"];
-				$count_format++;
-			}
-			$count++;
-		}
-		
-		#layer
-		$sql = "Select * from gui_layer where gui_layer_wms_id = $1 AND fkey_gui_id = $2 ";
-		$sql .= " AND gui_layer_status = 1 ORDER BY gui_layer_priority;";
-		$v = array($wms_id,$gui_id);
-		$t = array('i','s');
-		$res = db_prep_query($sql,$v,$t);
-		$count=0;
-		
-		while($row = db_fetch_array($res)){
-			$layer_id = $row["fkey_layer_id"];		
-			$sql = "Select * from layer where layer_id = $1";
-			$v = array($layer_id);
-			$t = array('i');
-			$res_layer = db_prep_query($sql,$v,$t);
-			$count_layer=0;
-			while($row2 = db_fetch_array($res_layer)){
-				$this->addLayer($row2["layer_pos"],$row2["layer_parent"]);
-				$layer_cnt=count($this->objLayer)-1;
-				$this->objLayer[$layer_cnt]->layer_uid = $layer_id;
-				$this->objLayer[$layer_cnt]->layer_name =$row2["layer_name"];
-				$this->objLayer[$layer_cnt]->layer_title =$row2["layer_title"];			
-				$this->objLayer[$layer_cnt]->layer_dataurl_href =$row2["layer_dataurl"];
-				$this->objLayer[$layer_cnt]->layer_metadataurl =$row2["layer_metadataurl"];
-				$this->objLayer[$layer_cnt]->layer_pos =$row2["layer_pos"];						
-				$this->objLayer[$layer_cnt]->layer_queryable =$row2["layer_pos"];
-				$this->objLayer[$layer_cnt]->layer_queryable =$row2["layer_queryable"];
-				$this->objLayer[$layer_cnt]->layer_minscale =$row2["layer_minscale"];
-				$this->objLayer[$layer_cnt]->layer_maxscale = $row2["layer_maxscale"];
-				$count_layer++;
-			}
-			$this->objLayer[$layer_cnt]->layer_uid = $layer_id;
-			$this->objLayer[$layer_cnt]->gui_layer_wms_id = $row["gui_layer_wms_id"];
-			$this->objLayer[$layer_cnt]->gui_layer_selectable = $row["gui_layer_selectable"];
-			$this->objLayer[$layer_cnt]->gui_layer_visible = $row["gui_layer_visible"];
-			$this->objLayer[$layer_cnt]->gui_layer_queryable = $row["gui_layer_queryable"];
-			$this->objLayer[$layer_cnt]->gui_layer_querylayer = $row["gui_layer_querylayer"];
-			$this->objLayer[$layer_cnt]->gui_layer_minscale = $row["gui_layer_minscale"];
-			$this->objLayer[$layer_cnt]->gui_layer_maxscale = $row["gui_layer_maxscale"];
-			$this->objLayer[$layer_cnt]->gui_layer_style = $row["gui_layer_style"];
-			$this->objLayer[$layer_cnt]->gui_layer_wfs_featuretype = $row["gui_layer_wfs_featuretype"];
-			
-			$sql = "Select * from layer_epsg where fkey_layer_id = $1 ORDER BY fkey_layer_id";
-			$v = array($layer_id);
-			$t = array('i');
-			$res_layer_epsg = db_prep_query($sql,$v,$t);
-			
-			$count_layer_epsg=0;
-			while($row2 = db_fetch_array($res_layer_epsg)){
-				$this->objLayer[$layer_cnt]->layer_epsg[$count_layer_epsg]["epsg"]=$row2["epsg"];
-				$this->objLayer[$layer_cnt]->layer_epsg[$count_layer_epsg]["minx"]=$row2["minx"];
-				$this->objLayer[$layer_cnt]->layer_epsg[$count_layer_epsg]["miny"]=$row2["miny"];
-				$this->objLayer[$layer_cnt]->layer_epsg[$count_layer_epsg]["maxx"]=$row2["maxx"];
-				$this->objLayer[$layer_cnt]->layer_epsg[$count_layer_epsg]["maxy"]=$row2["maxy"];
-				$count_layer_epsg++;
-			}
-			
-			### handle styles
-			$sql = "SELECT * FROM layer_style WHERE fkey_layer_id = $1 ";
-			$v = array($layer_id);
-			$t = array('i');
-			$res_style = db_prep_query($sql,$v,$t);
-			$count_layer_style = 0;
-			while($row2 = db_fetch_array($res_style)){
-				$this->objLayer[$layer_cnt]->layer_style[$count_layer_style]["name"]=$row2["name"];
-				$this->objLayer[$layer_cnt]->layer_style[$count_layer_style]["title"]=$row2["title"];
-				if($wmsowsproxy != ""){
-					if($row2["legendurl"]!=''){
-						$this->objLayer[$layer_cnt]->layer_style[$count_layer_style]["legendurl"]=$owsproxyurl.
-						"REQUEST=getlegendgraphic&VERSION=".$this->wms_version."&LAYER=".$this->objLayer[$layer_cnt]->layer_name."&FORMAT=".$row2["legendurlformat"].
-						"&STYLE=".$row2["name"];
-					}
-				}
-				else{
-					if($row2["legendurl"]!=''){
-						$this->objLayer[$layer_cnt]->layer_style[$count_layer_style]["legendurl"]=$row2["legendurl"];
-						#$e = new mb_exception("legendurl = ".$this->objLayer[$layer_cnt]->layer_style[$count_layer_style]["legendurl"]);
-					}
-				}
-				$this->objLayer[$layer_cnt]->layer_style[$count_layer_style]["legendurlformat"]=$row2["legendurlformat"];
-				$count_layer_style++;
-			}
-			$count++;
-		}
-	   }
-	/** end createObjfromDB **/
-	
-	  /**
-	* function checkObjExistsInDB()
-	*
-	* this function checks wether the onlineresource already exists in the database.
-	*/ 
-	function checkObjExistsInDB(){
-	
-		$sql = "Select * from wms where wms_getcapabilities = $1";
-		$v = array($this->wms_getcapabilities);
-		$t = array('s');
-		$res = db_prep_query($sql,$v,$t);
-		$res_count= db_num_rows($res);	  
-		$wms_id=0;
-		if($res_count>0){
-			$count=0;
-			while($row = db_fetch_array($res)){
-				$wms_id=$row["wms_id"];
-				$count++;
-			}
-		}
-		return $wms_id;
-	}
-	
-	function displayDBInformation(){
-		echo $this->wms_getcapabilities;
-		$sql="Select * from wms where wms_getcapabilities = $1";
-		$v = array($this->wms_getcapabilities);
-		$t = array('s');
-		$res = db_prep_query($sql,$v,$t);
-		$count=0;
-		while($row = db_fetch_array($res)){
-		echo "count: ".$count."<br>";
-			$wms_id=$row["wms_id"];
-			echo "version: " .$wms_id." <br>";
-			echo "title: " .$row["wms_version"]. " <br>";
-			echo "abstract: " . $row["wms_title"] . " <br>";
-			echo "maprequest: " .$row["wms_abstract"] . " <br>";
-			echo "capabilitiesrequest: " . $row["wms_getcapabilities"] . " <br>";
-			echo "featureinforequest: " . $row["wms_getmap"]. " <br>";
-			echo "gui_wms_mapformat: " . $row["wms_getfeatureinfo"] . " <br>---------<br>";
-			$count++;
-		}
-	   echo "----<br> wms_id: ".$wms_id."<br>";
-	   
-	   $sql = "Select * from gui_wms where fkey_wms_id = $1";
-	   $v = array($wms_id);
-	   $t = array('i');
-	   echo "sql: ".$sql." <br>---------<br>";
-	   $res = db_prep_query($sql,$v,$t);
-	   $res_count= db_num_rows($res); 
-	   echo "result count: ".$res_count." <br>---------<br>";
-	   
-	   $count=0;
-	   while($row = db_fetch_array($res)){
-	    	echo "gui_wms_featureinfoformat: " . $row["gui_wms_featureinfoformat"]." <br>";
-	    	echo "gui_wms_exceptionformat: " .  $row["gui_wms_exceptionformat"]. " <br>";
-	    	echo "gui_wms_epsg: " .  $row["gui_wms_epsg"]. " <br>";
-	      $count++;
-	   }
-		
-	   #db_close($connect);
-	}
-
-	function checkObj(){
-		if($this->wms_getcapabilities == '' || $this->wms_getmap == '' ){
-			echo "<br>Missing parameters: <br>";
-			$this->displayWMS();
-			echo "<br> Data not commited<br>";
-			die();
-		}
-	}
-}
-class layer extends wms {	
-	var $layer_id;
-	var $layer_parent;
-	var $layer_name;
-	var $layer_title;
-	var $layer_abstract;
-	var $layer_pos;
-	var $layer_queryable;
-	var $layer_minscale;
-	var $layer_maxscale;	
-    var $layer_dataurl_href;
-    var $layer_metadataurl;
-    var $layer_keyword = array();
-	var $layer_epsg = array();
-	var $layer_style = array();
-	
-	var $gui_layer_wms_id;
-	var $gui_layer_status = 1;
-	var $gui_layer_selectable = 1;
-	var $gui_layer_visible = 0;
-	var $gui_layer_queryable = 0;
-	var $gui_layer_querylayer = 0;
-	var $gui_layer_style = NULL;	
-	
-	function layer($id,$parent){
-		$this->layer_id = $id;
-		$this->layer_parent = $parent;	
-		//var_dump($this);	
-	}
-}
+<?php
+# $Id$
+# http://www.mapbender.org/index.php/class_wms
+# Copyright (C) 2002 CCGIS
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+include_once(dirname(__FILE__)."/../../conf/mapbender.conf");
+require_once(dirname(__FILE__)."/class_connector.php");
+require_once(dirname(__FILE__)."/class_mb_exception.php");
+require_once(dirname(__FILE__)."/class_administration.php");
+
+$con = db_connect(DBSERVER,OWNER,PW);
+db_select_db(DB,$con);
+
+
+class wms {
+	var $lastURL;
+	var $wms_id;
+	var $wms_status;
+	var $wms_version;
+	var $wms_title;
+	var $wms_abstract;
+	var $wms_getcapabilities;
+	var $wms_getcapabilities_doc;
+	var $wms_getmap;
+	var $wms_getfeatureinfo;
+	var $wms_getlegendurl;
+	var $wms_upload_url;
+
+	var $fees;
+	var $accessconstraints;
+	var $contactperson;
+	var $contactposition;
+	var $contactorganization;
+	var $address;
+	var $city;
+	var $stateorprovince;
+	var $postcode;
+	var $country;
+	var $contactvoicetelephone;
+	var $contactfacsimiletelephone;
+	var $contactelectronicmailaddress;
+
+	var $wms_keyword = array();
+	var $data_type = array();
+	var $data_format = array();
+	var $objLayer = array();
+
+	var $wms_supportsld;
+	var $wms_userlayer;
+	var $wms_userstyle;
+	var $wms_remotewfs;
+
+	var $gui_wms_mapformat;
+	var $gui_wms_featureinfoformat;
+	var $gui_wms_exceptionformat;
+	var $gui_wms_epsg;
+	var $gui_wms_sldurl;
+
+	var $default_epsg = 0;
+	var $overwrite = true;
+
+	function wms() {
+	}
+
+	function createObjFromXML($url){
+
+		$x = new connector($url);
+		$data = $x->file;
+
+		if(!$data){
+			$this->wms_status = false;
+			return false;
+		}
+		else {
+			$this->wms_status = true;
+		}
+
+		$values = null;
+		$tags = null;
+		$admin = new administration();
+		$this->wms_getcapabilities_doc = $admin->char_encode($data);
+		$this->wms_upload_url = $url;
+
+		$this->wms_id = "";
+		$parser = xml_parser_create("");
+		xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
+		xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
+		xml_parser_set_option($parser,XML_OPTION_TARGET_ENCODING,CHARSET);
+		xml_parse_into_struct($parser,$this->wms_getcapabilities_doc,$values,$tags);
+
+		$code = xml_get_error_code($parser);
+		if ($code) {
+			$line = xml_get_current_line_number($parser);
+			$mb_exception = new mb_exception(xml_error_string($code) .  " in line " . $line);
+		}
+
+		xml_parser_free($parser);
+
+		$section = null;
+		$format = null;
+		$cnt_format = 0;
+		$parent = array();
+		$myParent = array();
+		$cnt_layer = -1;
+		$request = null;
+		$layer_style = array();
+		$cnt_styles = -1;
+
+		foreach ($values as $element) {
+			if(mb_strtoupper($element[tag]) == "WMT_MS_CAPABILITIES" && $element[type] == "open"){
+				$this->wms_version = $element[attributes][version];
+			}
+			if(mb_strtoupper($element[tag]) == "TITLE" && $element[level] == '3'){
+				$this->wms_title = $this->stripEndlineAndCarriageReturn($element[value]);
+			}
+			if(mb_strtoupper($element[tag]) == "ABSTRACT" && $element[level] == '3'){
+				$this->wms_abstract = $this->stripEndlineAndCarriageReturn($element[value]);
+			}
+			if(mb_strtolower($element[tag]) == "fees"){
+				$this->fees = $element[value];
+			}
+			if(mb_strtolower($element[tag]) == "accessconstraints"){
+				$this->accessconstraints = $element[value];
+			}
+			if(mb_strtolower($element[tag]) == "contactperson"){
+				$this->contactperson = $element[value];
+			}
+			if(mb_strtolower($element[tag]) == "contactposition"){
+				$this->contactposition = $element[value];
+			}
+			if(mb_strtolower($element[tag]) == "contactorganization"){
+				$this->contactorganization = $element[value];
+			}
+			if(mb_strtolower($element[tag]) == "address"){
+				$this->address = $element[value];
+			}
+			if(mb_strtolower($element[tag]) == "city"){
+				$this->city = $element[value];
+			}
+			if(mb_strtolower($element[tag]) == "stateorprovince"){
+				$this->stateorprovince = $element[value];
+			}
+			if(mb_strtolower($element[tag]) == "postcode"){
+				$this->postcode = $element[value];
+			}
+			if(mb_strtolower($element[tag]) == "country"){
+				$this->country = $element[value];
+			}
+			if(mb_strtolower($element[tag]) == "contactvoicetelephone"){
+				$this->contactvoicetelephone = $element[value];
+			}
+			if(mb_strtolower($element[tag]) == "contactfacsimiletelephone"){
+				$this->contactfacsimiletelephone = $element[value];
+			}
+			if(mb_strtolower($element[tag]) == "contactelectronicmailaddress"){
+				$this->contactelectronicmailaddress = $element[value];
+			}
+	  		if(mb_strtolower($element[tag]) == "keyword" && $section != 'layer'){
+				$this->wms_keyword[count($this->wms_keyword)] = $element[value];
+			}
+
+			/*map section*/
+			if($this->wms_version == "1.0.0"){
+		 		if(mb_strtoupper($element[tag]) == "MAP" && $element[type] == "open"){
+					$section = "map";
+				}
+				if($section == "map" && mb_strtoupper($element[tag]) == "GET"){
+					$this->wms_getmap = $element[attributes][onlineResource];
+				}
+				if($section == "map" && mb_strtoupper($element[tag]) == "FORMAT" && $element[type] == "open"){
+					$format = "map";
+				}
+				if(mb_strtoupper($element[tag]) != "FORMAT" && $section == "map" && $format == "map"){
+					$this->data_type[$cnt_format] = "map";
+					$this->data_format[$cnt_format] = trim($element[tag]);
+					$cnt_format++;
+				}
+				if(mb_strtoupper($element[tag]) == "FORMAT" && $element[type] == "close"){
+					$format = "";
+				}
+				if(mb_strtoupper($element[tag]) == "MAP" && $element[type] == "close"){
+					$section = "";
+				}
+			}
+			else{
+				if(mb_strtoupper($element[tag]) == "GETMAP" && $element[type] == "open"){
+					$section = "map";
+				}
+				if($section == "map" && mb_strtoupper($element[tag]) == "GET" && $element[type] == "open"){
+					$request = "get";
+				}
+				if($section == "map" && $request == "get" && mb_strtoupper($element[tag]) == "ONLINERESOURCE"){
+					$this->wms_getmap = $element[attributes]["xlink:href"];
+				}
+				if($section == "map" && mb_strtoupper($element[tag]) == "FORMAT"){
+					$this->data_type[$cnt_format] = "map";
+					$this->data_format[$cnt_format] = trim($element[value]);
+					$cnt_format++;
+				}
+				if($section == "map" && mb_strtoupper($element[tag]) == "GET" && $element[type] == "close"){
+					$request = "";
+				}
+				if(mb_strtoupper($element[tag]) == "GETMAP" && $element[type] == "close"){
+					$section = "";
+				}
+			}
+			/*capabilities section*/
+			if($this->wms_version == "1.0.0"){
+				if(mb_strtoupper($element[tag]) == "CAPABILITIES" && $element[type] == "open"){
+					$section = "capabilities";
+				}
+				if($section == "capabilities" && mb_strtoupper($element[tag]) == "GET"){
+					$this->wms_getcapabilities = $element[attributes][onlineResource];
+				}
+				if(mb_strtoupper($element[tag]) == "CAPABILITIES" && $element[type] == "close"){
+					$section = "";
+				}
+			}
+			else{
+				if(mb_strtoupper($element[tag]) == "GETCAPABILITIES" && $element[type] == "open"){
+					$section = "capabilities";
+				}
+				if($section == "capabilities" && mb_strtoupper($element[tag]) == "GET" && $element[type] == "open"){
+					$request = "get";
+				}
+				if($section == "capabilities" && $request == "get" && mb_strtoupper($element[tag]) == "ONLINERESOURCE"){
+					$this->wms_getcapabilities = $element[attributes]["xlink:href"];
+				}
+				if($section == "capabilities" && mb_strtoupper($element[tag]) == "GET" && $element[type] == "close"){
+					$request = "";
+				}
+				if(mb_strtoupper($element[tag]) == "GETCAPABILITIES" && $element[type] == "close"){
+					$section = "";
+				}
+			}
+			/*featureInfo section*/
+			if($this->wms_version == "1.0.0"){
+				if(mb_strtoupper($element[tag]) == "FEATUREINFO" && $element[type] == "open"){
+					$section = "featureinfo";
+				}
+				if($section == "featureinfo" && mb_strtoupper($element[tag]) == "GET"){
+					$this->wms_getfeatureinfo = $element[attributes][onlineResource];
+				}
+				if($section == "featureinfo" && mb_strtoupper($element[tag]) == "FORMAT" && $element[type] == "open"){
+					$format = "featureinfo";
+				}
+				if(mb_strtoupper($element[tag]) != "FORMAT" && $section == "featureinfo" && $format == "featureinfo"){
+					$this->data_type[$cnt_format] = "featureinfo";
+					$this->data_format[$cnt_format] = trim($element[tag]);
+					$cnt_format++;
+				}
+				if(mb_strtoupper($element[tag]) == "FORMAT" && $element[type] == "close"){
+					$format = "";
+				}
+				if(mb_strtoupper($element[tag]) == "FEATUREINFO" && $element[type] == "close"){
+					$section = "";
+				}
+			}
+			else{
+				if(mb_strtoupper($element[tag]) == "GETFEATUREINFO" && $element[type] == "open"){
+					$section = "featureinfo";
+				}
+				if($section == "featureinfo" && mb_strtoupper($element[tag]) == "GET" && $element[type] == "open"){
+					$request = "get";
+				}
+				if($section == "featureinfo" && $request == "get" && mb_strtoupper($element[tag]) == "ONLINERESOURCE"){
+					$this->wms_getfeatureinfo = $element[attributes]["xlink:href"];
+				}
+				if($section == "featureinfo" && mb_strtoupper($element[tag]) == "FORMAT"){
+					$this->data_type[$cnt_format] = "featureinfo";
+					$this->data_format[$cnt_format] = trim($element[value]);
+					$cnt_format++;
+				}
+				if($section == "featureinfo" && mb_strtoupper($element[tag]) == "GET" && $element[type] == "close"){
+					$request = "";
+				}
+				if(mb_strtoupper($element[tag]) == "GETFEATUREINFO" && $element[type] == "close"){
+					$section = "";
+				}
+			}
+			/*exception section*/
+			if($this->wms_version == "1.0.0"){
+				if(mb_strtoupper($element[tag]) == "EXCEPTION" && $element[type] == "open"){
+					$section = "exception";
+				}
+				if($section == "exception" && mb_strtoupper($element[tag]) == "FORMAT" && $element[type] == "open"){
+					$format = "exception";
+				}
+				if(mb_strtoupper($element[tag]) != "FORMAT" && $section == "exception" && $format == "exception"){
+					$this->data_type[$cnt_format] = "exception";
+					$this->data_format[$cnt_format] = trim($element[tag]);
+					$cnt_format++;
+				}
+				if($section == "exception" && mb_strtoupper($element[tag]) == "FORMAT" && $element[type] == "close"){
+					$format = "";
+				}
+				if(mb_strtoupper($element[tag]) == "EXCEPTION" && $element[type] == "close"){
+					$section = "";
+				}
+			}
+			else{
+				if(mb_strtoupper($element[tag]) == "EXCEPTION" && $element[type] == "open"){
+					$section = "exception";
+				}
+				if($section == "exception" && mb_strtoupper($element[tag]) == "FORMAT"){
+					$this->data_type[$cnt_format] = "exception";
+					$this->data_format[$cnt_format] = trim($element[value]);
+					$cnt_format++;
+				}
+				if(mb_strtoupper($element[tag]) == "EXCEPTION" && $element[type] == "close"){
+					$section = "";
+				}
+			}
+	      /*legend section*/
+	      if($this->wms_version == "1.0.0"){
+
+	      }
+	      else{
+	        if(mb_strtoupper($element[tag]) == "GETLEGENDGRAPHIC" && $element[type] == "open"){
+				$section = "legend";
+			}
+	        if($section == "legend" && mb_strtoupper($element[tag]) == "GET" && $element[type] == "open"){
+				$request = "get";
+			}
+			if($section == "legend" && $request == "get" && mb_strtoupper($element[tag]) == "ONLINERESOURCE"){
+				$this->wms_getlegendurl = $element[attributes]["xlink:href"];
+			}
+	        if($section == "legend" && mb_strtoupper($element[tag]) == "GET" && $element[type] == "close"){
+				$request = "";
+			}
+			if(mb_strtoupper($element[tag]) == "GETLEGENDGRAPHIC" && $element[type] == "close"){
+				$section = "";
+			}
+	      }
+			/* sld section */
+			if(mb_strtoupper($element[tag]) == "USERDEFINEDSYMBOLIZATION" && $element[type] == "complete"){
+				$this->wms_supportsld = $element[attributes]["SupportSLD"];
+				$this->wms_userlayer = $element[attributes]["UserLayer"];
+				$this->wms_userstyle = $element[attributes]["UserStyle"];
+				$this->wms_remotewfs = $element[attributes]["RemoteWFS"];
+			}
+
+			/*layer section*/
+			if(mb_strtoupper($element[tag]) == "LAYER"){
+				$section = "layer";
+				if ($element[type] == "open") {
+					$cnt_epsg = -1;
+					$cnt_layer++;
+					$parent[$element[level]+1] = $cnt_layer;
+					$myParent[$cnt_layer]= $parent[$element[level]];
+					$this->addLayer($cnt_layer,$myParent[$cnt_layer]);
+					$this->objLayer[$cnt_layer]->layer_queryable = $element[attributes][queryable];
+				}
+				if ($element[type] == "close") {
+
+				}
+			}
+			/* attribution */
+			if(mb_strtoupper($element[tag]) == "ATTRIBUTION"){
+				if ($element[type] == "open") {
+					$section = "attribution";
+				}
+				if ($element[type] == "close") {
+					$section = "layer";
+				}
+			}
+			/* styles */
+			if(mb_strtoupper($element[tag]) == "STYLE"){
+				$section = "style";
+				if($cnt_layer != $layer_style){
+					$layer_style = $cnt_layer;
+					$cnt_styles = -1;
+				}
+				if ($element[type] == "open") {
+					$cnt_styles++;
+				}
+				if ($element[type] == "close") {
+					$section = "layer";
+				}
+			}
+			if($section == "style"){
+				if(mb_strtoupper($element[tag]) == "NAME"){
+					$this->objLayer[$cnt_layer]->layer_style[$cnt_styles]["name"] = $element[value];
+				}
+				if(mb_strtoupper($element[tag]) == "TITLE"){
+					$this->objLayer[$cnt_layer]->layer_style[$cnt_styles]["title"] = $element[value];
+				}
+	      		if(mb_strtoupper($element[tag]) == "LEGENDURL" && $element[type] == "open"){
+					$legendurl = true;
+				}
+				if($legendurl && mb_strtoupper($element[tag]) == "FORMAT"){
+					$this->objLayer[$cnt_layer]->layer_style[$cnt_styles]["legendurlformat"] = $element[value];
+				}
+				if($legendurl && mb_strtoupper($element[tag]) == "ONLINERESOURCE"){
+					$this->objLayer[$cnt_layer]->layer_style[$cnt_styles]["legendurl"] = $element[attributes]["xlink:href"];
+				}
+			    if(mb_strtoupper($element[tag]) == "LEGENDURL" && $element[type] == "close"){
+					$legendurl = false;
+				}
+			}
+			/* end of styles */
+			if($section == "layer"){
+				if(mb_strtoupper($element[tag]) == "NAME"){
+					$this->objLayer[$cnt_layer]->layer_name = $element[value];
+				}
+				if(mb_strtoupper($element[tag]) == "TITLE"){
+					$this->objLayer[$cnt_layer]->layer_title = $this->stripEndlineAndCarriageReturn($element[value]);
+				}
+				if(mb_strtoupper($element[tag]) == "ABSTRACT"){
+					$this->objLayer[$cnt_layer]->layer_abstract = $this->stripEndlineAndCarriageReturn($element[value]);
+				}
+				if(mb_strtoupper($element[tag]) == "KEYWORD"){
+					array_push($this->objLayer[$cnt_layer]->layer_keyword, trim($element[value]));
+				}
+	      		if(mb_strtoupper($element[tag]) == "DATAURL" && $element[type] == "open"){
+					$dataurl = true;
+				}
+				if($dataurl && mb_strtoupper($element[tag]) == "ONLINERESOURCE"){
+					$this->objLayer[$cnt_layer]->layer_dataurl_href = $element[attributes]["xlink:href"];
+				}
+			    if(mb_strtoupper($element[tag]) == "DATAURL" && $element[type] == "close"){
+					$dataurl = false;
+				}
+
+				if(mb_strtoupper($element[tag]) == "METADATAURL" && $element[type] == "open"){
+					$metadataurl = true;
+				}
+				if($metadataurl && mb_strtoupper($element[tag]) == "ONLINERESOURCE"){
+					$this->objLayer[$cnt_layer]->layer_metadataurl = $element[attributes]["xlink:href"];
+				}
+			    if(mb_strtoupper($element[tag]) == "METADATAURL" && $element[type] == "close"){
+					$metadataurl = false;
+				}
+
+				if(mb_strtoupper($element[tag]) == "SRS"){
+	  				$this->objLayer[$cnt_layer]->wms_srs1 = $element[value];
+					// unique srs only, see http://www.mapbender.org/index.php/Arrays_with_unique_entries
+					$this->wms_srs = array_keys(array_flip(explode(" ", $this->objLayer[0]->wms_srs1)));
+				}
+				if(mb_strtoupper($element[tag]) == "LATLONBOUNDINGBOX"){
+					$cnt_epsg++;
+					$this->objLayer[$cnt_layer]->layer_epsg[$cnt_epsg]["epsg"] = "EPSG:4326";
+					$this->objLayer[$cnt_layer]->layer_epsg[$cnt_epsg]["minx"] = $element[attributes][minx];
+					$this->objLayer[$cnt_layer]->layer_epsg[$cnt_epsg]["miny"] = $element[attributes][miny];
+					$this->objLayer[$cnt_layer]->layer_epsg[$cnt_epsg]["maxx"] = $element[attributes][maxx];
+					$this->objLayer[$cnt_layer]->layer_epsg[$cnt_epsg]["maxy"] = $element[attributes][maxy];
+				}
+				if(mb_strtoupper($element[tag]) == "BOUNDINGBOX" && $element[attributes][SRS] != "EPSG:4326"){
+					$cnt_epsg++;
+					$this->objLayer[$cnt_layer]->layer_epsg[$cnt_epsg]["epsg"] = $element[attributes][SRS];
+					$this->objLayer[$cnt_layer]->layer_epsg[$cnt_epsg]["minx"] = $element[attributes][minx];
+					$this->objLayer[$cnt_layer]->layer_epsg[$cnt_epsg]["miny"] = $element[attributes][miny];
+					$this->objLayer[$cnt_layer]->layer_epsg[$cnt_epsg]["maxx"] = $element[attributes][maxx];
+					$this->objLayer[$cnt_layer]->layer_epsg[$cnt_epsg]["maxy"] = $element[attributes][maxy];
+					// a default epsg for mapbender
+					if($cnt_layer == 0 && $this->default_epsg == 0 && mb_strlen(trim($element[attributes][SRS]))>= 10){
+						$this->default_epsg = $cnt_epsg;
+					}
+				}
+				if(mb_strtoupper($element[tag]) == "SCALEHINT"){
+					if($element[attributes][max]>1000) $max = 0; else $max = $element[attributes][max];
+					if($element[attributes][min]>1000) $min = 0; else $min = $element[attributes][min];
+					$this->objLayer[$cnt_layer]->layer_minscale = round(($min * 2004.3976484406788493955738891127));
+					$this->objLayer[$cnt_layer]->layer_maxscale = round(($max * 2004.3976484406788493955738891127));
+				}
+			}
+			else {
+				continue;
+			}
+		}
+		if(!$this->wms_title || $this->wms_title == "" || !$this->wms_getmap || $this->wms_getmap == ""){
+			$this->wms_status = false;
+			$this->optimizeWMS();
+
+			return false;
+		}
+		else{
+			$this->wms_status = true;
+			$this->optimizeWMS();
+
+			return true;
+		}
+	}
+	/**
+	 * private function
+	 */
+	function optimizeWMS() {
+		/*define defaults for wms-version 1.0.0*/
+		$map_default_ok = false;
+		$featureinfo_default_ok = false;
+		$exception_default_ok = false;
+		if($this->wms_version == "1.0.0"){
+			$map_default = "PNG";
+			$featureinfo_default = "MIME";
+			$exception_default = "INIMAGE";
+		}
+		/*define defaults for wms-version 1.1.0 and 1.1.1*/
+		else{
+			$map_default = "image/png";
+			$featureinfo_default = "text/html";
+			$exception_default = "application/vnd.ogc.se_inimage";
+		}
+		#some default
+		$this->gui_wms_visible = 1;
+		$this->gui_wms_opacity = 100;
+		/*if the rootlayer has no epsg...*/
+		if($this->objLayer[0]->layer_epsg[0]["epsg"] == ""){
+			$this->objLayer[0]->layer_epsg = $this->objLayer[1]->layer_epsg;
+			for($i=0;$i<count($this->objLayer[0]->layer_epsg);$i++){
+				for($j=1; $j<count($this->objLayer); $j++){
+					if($this->objLayer[0]->layer_epsg[$i]["epsg"] == $this->objLayer[$j]->layer_epsg[$i]["epsg"]){
+						if($this->objLayer[$j]->layer_epsg[$i]["minx"]<$this->objLayer[0]->layer_epsg[$i]["minx"]){
+							$this->objLayer[0]->layer_epsg[$i]["minx"] = $this->objLayer[$j]->layer_epsg[$i]["minx"];
+						}
+						if($this->objLayer[$j]->layer_epsg[$i]["miny"]<$this->objLayer[0]->layer_epsg[$i]["miny"]){
+							$this->objLayer[0]->layer_epsg[$i]["miny"] = $this->objLayer[$j]->layer_epsg[$i]["miny"];
+						}
+						if($this->objLayer[$j]->layer_epsg[$i]["maxx"]>$this->objLayer[0]->layer_epsg[$i]["maxx"]){
+							$this->objLayer[0]->layer_epsg[$i]["maxx"] = $this->objLayer[$j]->layer_epsg[$i]["maxx"];
+						}
+						if($this->objLayer[$j]->layer_epsg[$i]["maxy"]>$this->objLayer[0]->layer_epsg[$i]["maxy"]){
+							$this->objLayer[0]->layer_epsg[$i]["maxy"] = $this->objLayer[$j]->layer_epsg[$i]["maxy"];
+						}
+					}
+				}
+			}
+		}
+		for($i=0;$i<count($this->objLayer);$i++){
+			if(count($this->objLayer[$i]->layer_epsg) == 0 && count($this->objLayer[0]->layer_epsg) > 0){
+				$this->objLayer[$i]->layer_epsg = $this->objLayer[0]->layer_epsg;
+			}
+			if(!is_int($this->objLayer[$i]->layer_parent)){
+				$this->objLayer[$i]->layer_abstract = $this->wms_abstract;
+				for ($r = 0; $r < count($this->wms_keyword); $r++) {
+					array_push($this->objLayer[$i]->layer_keyword, trim($this->wms_keyword[$r]));
+				}
+			}
+			if($this->objLayer[$i]->layer_name == ""){
+				$this->objLayer[$i]->layer_name = "";
+			}
+			if($this->objLayer[$i]->layer_minscale == ""){
+				$this->objLayer[$i]->layer_minscale = 0;
+			}
+			if($this->objLayer[$i]->layer_maxscale == ""){
+				$this->objLayer[$i]->layer_maxscale = 0;
+			}
+			if($this->objLayer[$i]->layer_queryable == ""){
+				$this->objLayer[$i]->layer_queryable = 0;
+			}
+			$this->objLayer[$i]->gui_layer_minscale = $this->objLayer[$i]->layer_minscale;
+			$this->objLayer[$i]->gui_layer_maxscale = $this->objLayer[$i]->layer_maxscale;
+		}
+		for($i=0;$i<count($this->data_format);$i++){
+			if(mb_strtolower($this->data_type[$i]) == 'map' && mb_strtoupper($this->data_format[$i]) == mb_strtoupper($map_default)){
+				$this->gui_wms_mapformat = mb_strtolower($map_default);
+				$map_default_ok = true;
+			}
+			if(mb_strtolower($this->data_type[$i]) == 'featureinfo' && mb_strtoupper($this->data_format[$i]) == mb_strtoupper($featureinfo_default)){
+				$this->gui_wms_featureinfoformat = mb_strtolower($featureinfo_default);
+				$featureinfo_default_ok = true;
+			}
+			if(mb_strtolower($this->data_type[$i]) == 'exception' && mb_strtolower($this->data_format[$i]) == mb_strtolower($exception_default)){
+				$this->gui_wms_exceptionformat = mb_strtolower($exception_default);
+				$exception_default_ok = true;
+			}
+		}
+		if($map_default_ok == false){
+			for($i=0;$i<count($this->data_format);$i++){
+				if(mb_strtolower($this->data_type[$i]) == "map" ){$this->gui_wms_mapformat = $this->data_format[$i]; break;}
+			}
+		}
+		if($featureinfo_default_ok == false){
+			for($i=0;$i<count($this->data_format);$i++){
+				if(mb_strtolower($this->data_type[$i]) == "featureinfo" ){$this->gui_wms_featureinfoformat = $this->data_format[$i]; break;}
+			}
+		}
+		if($exception_default_ok == false){
+			for($i=0;$i<count($this->data_format);$i++){
+				if(mb_strtolower($this->data_type[$i]) == "exception" ){$this->gui_wms_exceptionformat = $this->data_format[$i]; break;}
+			}
+		}
+
+		if(count($this->objLayer[0]->layer_epsg)>1){
+			$this->gui_wms_epsg = $this->objLayer[0]->layer_epsg[$this->default_epsg][epsg];
+		}
+		else{
+			$this->gui_wms_epsg = $this->objLayer[0]->layer_epsg[0][epsg];
+		}
+		/*the queryable layers*/
+		for($i=0; $i<count($this->objLayer); $i++){
+			if($this->objLayer[$i]->layer_queryable == 1){
+				$this->objLayer[$i]->gui_layer_queryable = 1;
+			}
+			else{
+				$this->objLayer[$i]->gui_layer_queryable = 0;
+			}
+		}
+		for($i=0; $i<count($this->objLayer); $i++){
+				$this->objLayer[$i]->layer_pos=$i;
+		}
+
+		/* fill sld variables when empty */
+		if($this->wms_supportsld == ""){
+				$this->wms_supportsld = 0;
+		}
+		if($this->wms_userlayer == ""){
+				$this->wms_userlayer = 0;
+		}
+		if($this->wms_userstyle == ""){
+				$this->wms_userstyle = 0;
+		}
+		if($this->wms_remotewfs == ""){
+				$this->wms_remotewfs = 0;
+		}
+	  }
+
+	function displayWMS(){
+		echo "<br>id: " . $this->wms_id . " <br>";
+		echo "version: " . $this->wms_version . " <br>";
+		echo "title: " . $this->wms_title . " <br>";
+		echo "abstract: " . $this->wms_abstract . " <br>";
+		echo "maprequest: " . $this->wms_getmap . " <br>";
+		echo "capabilitiesrequest: " . $this->wms_getcapabilities . " <br>";
+		echo "featureinforequest: " . $this->wms_getfeatureinfo . " <br>";
+		echo "gui_wms_mapformat: " . $this->gui_wms_mapformat . " <br>";
+		echo "gui_wms_featureinfoformat: " . $this->gui_wms_featureinfoformat . " <br>";
+		echo "gui_wms_exceptionformat: " . $this->gui_wms_exceptionformat . " <br>";
+		echo "gui_wms_epsg: " . $this->gui_wms_epsg . " <br>";
+		echo "wms_srs: " . $this->objLayer[0]->wms_srs1 . " <br>";
+		echo "gui_wms_visible: " . $this->gui_wms_visible . " <br>";
+		echo "gui_wms_opacity: " . $this->gui_wms_opacity . " <br>";
+		echo "support_sld: " . $this->wms_supportsld . " <br>";
+
+		for($i=0; $i<count($this->data_type);$i++){
+			echo $this->data_type[$i]. " -> ".$this->data_format[$i]. "<br>";
+		}
+		for($i=0; $i<count($this->objLayer); $i++){
+			echo "<hr>";
+			echo "id: <b>".$this->objLayer[$i]->layer_id ."</b> parent: <b>".$this->objLayer[$i]->layer_parent."</b> name: <b>".$this->objLayer[$i]->layer_name;
+			echo "</b> title: <b>".$this->objLayer[$i]->layer_title. "</b> queryable: <b>".$this->objLayer[$i]->layer_queryable."</b> minScale: <b>". $this->objLayer[$i]->layer_minscale."</b> maxScale: <b>".$this->objLayer[$i]->layer_maxscale."</b>";
+			echo "<br>dataurl: <b>".$this->objLayer[$i]->layer_dataurl_href. "</b>";
+			echo "<br>metadataurl: <b>".$this->objLayer[$i]->layer_metadataurl. "</b>";
+			echo "<table border='1'>";
+			for($j=0; $j<count($this->objLayer[$i]->layer_epsg);$j++){
+				echo "<tr><td>".$this->objLayer[$i]->layer_epsg[$j][epsg]."</td><td>".$this->objLayer[$i]->layer_epsg[$j][minx]."</td>";
+				echo "<td>".$this->objLayer[$i]->layer_epsg[$j][miny]."</td><td>".$this->objLayer[$i]->layer_epsg[$j][maxx]."</td>";
+				echo "<td>".$this->objLayer[$i]->layer_epsg[$j][maxy]."</td></tr>";
+			}
+			echo "</table>";
+			echo "layerstyle:";
+			echo "<table border='1'>";
+			echo "<tr><td>name</td><td>title</td><td>legendurl</td><td>legendurlformat</td></tr>";
+			for($j=0; $j<count($this->objLayer[$i]->layer_style);$j++){
+				echo "<tr><td>".$this->objLayer[$i]->layer_style[$j][name]."</td><td>".$this->objLayer[$i]->layer_style[$j][title]."</td><td>".$this->objLayer[$i]->layer_style[$j][legendurl]."</td><td>".$this->objLayer[$i]->layer_style[$j][legendurlformat]."</td></tr>";
+			}
+			echo "</table>";
+	        echo "<hr>";
+	        echo "<hr>";
+		}
+	}
+	  function addLayer($id,$parent){
+		$this->objLayer[count($this->objLayer)] = new layer($id,$parent);
+	  }
+	  /**
+	   * private function
+	   */
+	  function stripEndlineAndCarriageReturn($string) {
+	  	return preg_replace("/\n/", "", preg_replace("/\r/", " ", $string));
+	  }
+	  function createJsObjFromWMS($parent=0){
+	  	if(!$this->wms_title || $this->wms_title == ""){
+			echo "alert('Error: no valid capabilities-document !!');";
+			die; exit;
+		}
+			if($parent){
+				echo "parent.";
+			}
+			// wms_title and abstract have previously been urlencoded
+			// this solution may not yet be the ultimate one
+
+			$add_wms_string = "add_wms(" .
+					"'" . $this->wms_id ."'," .
+					"'" . $this->wms_version ."'," .
+					"'" . addslashes($this->wms_title) . "'," .
+					"'" . addslashes($this->wms_abstract) ."'," .
+					"'" . $this->wms_getmap ."'," .
+					"'" . $this->wms_getfeatureinfo ."'," .
+					"'" . $this->wms_getlegendurl ."'," .
+					"'" . $this->wms_filter ."'," .
+					"'" . $this->gui_wms_mapformat . "'," .
+					"'" . $this->gui_wms_featureinfoformat . "'," .
+					"'" . $this->gui_wms_exceptionformat . "'," .
+					"'" . $this->gui_wms_epsg ."'," .
+					"'" . $this->gui_wms_visible ."'," .
+					"'" . $this->gui_wms_opacity ."'," .
+					"'" . $this->gui_wms_sldurl ."" .
+					"');";
+			echo $add_wms_string;
+
+		for($i=0;$i<count($this->data_format);$i++){
+			if($parent){
+				echo "parent.";
+			}
+			echo "wms_add_data_type_format('". $this->data_type[$i] ."','". $this->data_format[$i] ."');\n";
+		}
+		for($i=0; $i<count($this->objLayer); $i++){
+			if($parent){
+				echo "parent.";
+			}
+			print ("wms_add_layer('".
+				$this->objLayer[$i]->layer_parent ."','".
+				$this->objLayer[$i]->layer_uid ."','".
+				addslashes($this->objLayer[$i]->layer_name) . "','".
+				addslashes($this->objLayer[$i]->layer_title) ."','".
+				$this->objLayer[$i]->layer_dataurl_href ."','".
+				$this->objLayer[$i]->layer_pos ."','".
+				$this->objLayer[$i]->layer_queryable ."','".
+				$this->objLayer[$i]->layer_minscale . "','".
+				$this->objLayer[$i]->layer_maxscale ."','".
+				$this->objLayer[$i]->layer_metadataurl ."','".
+				$this->objLayer[$i]->gui_layer_wms_id ."','".
+				$this->objLayer[$i]->gui_layer_status ."','".
+				$this->objLayer[$i]->gui_layer_style ."','".
+				$this->objLayer[$i]->gui_layer_selectable ."','".
+				$this->objLayer[$i]->gui_layer_visible ."','".
+				$this->objLayer[$i]->gui_layer_queryable ."','".
+				$this->objLayer[$i]->gui_layer_querylayer ."','".
+				$this->objLayer[$i]->gui_layer_minscale ."','".
+				$this->objLayer[$i]->gui_layer_maxscale ."','".
+				$this->objLayer[$i]->gui_layer_wfs_featuretype ."');\n");
+
+			for($j=0; $j<count($this->objLayer[$i]->layer_epsg);$j++){
+				if($i==0){
+					if($parent){
+					echo "parent.";
+					}
+					print("wms_addSRS('".
+						$this->objLayer[$i]->layer_epsg[$j]["epsg"] ."','".
+						$this->objLayer[$i]->layer_epsg[$j]["minx"] ."','".
+						$this->objLayer[$i]->layer_epsg[$j]["miny"] ."','".
+						$this->objLayer[$i]->layer_epsg[$j]["maxx"] ."','".
+						$this->objLayer[$i]->layer_epsg[$j]["maxy"] ."');\n");
+				}
+				if($parent){
+				echo "parent.";
+				}
+				print("layer_addEpsg('".
+					$this->objLayer[$i]->layer_epsg[$j]["epsg"] ."','".
+					$this->objLayer[$i]->layer_epsg[$j]["minx"] ."','".
+					$this->objLayer[$i]->layer_epsg[$j]["miny"] ."','".
+					$this->objLayer[$i]->layer_epsg[$j]["maxx"] ."','".
+					$this->objLayer[$i]->layer_epsg[$j]["maxy"] ."');\n");
+			}
+			for($j=0; $j<count($this->objLayer[$i]->layer_style);$j++){
+				if($parent){
+				echo "parent.";
+				}
+				print("wms_addLayerStyle('".$this->objLayer[$i]->layer_style[$j]["name"]."', '".$this->objLayer[$i]->layer_style[$j]["title"]."', ".$j.",".$i.",'".$this->objLayer[$i]->layer_style[$j]["legendurl"]."', '".$this->objLayer[$i]->layer_style[$j]["legendformat"]."');\n");
+			}
+		}
+	  }
+
+	  function createJsLayerObjFromWMS($parent=0, $layer_name){
+	  	if(!$this->wms_title || $this->wms_title == ""){
+			echo " alert('Error: no valid capabilities-document !!');";
+			die; exit;
+		}
+			if($parent){
+				echo "parent.";
+			}
+			// wms_title and abstract have previously been urlencoded
+			// this solution may not yet be the ultimate one
+			print("add_wms('".
+			$this->wms_id ."','".
+			$this->wms_version ."','".
+			preg_replace("/'/", "", $this->wms_title) ."','".
+			preg_replace("/'/", "", $this->wms_abstract) ."','".
+			$this->wms_getmap ."','" .
+			$this->wms_getfeatureinfo ."','".
+			$this->wms_getlegendurl ."','".
+			$this->wms_filter ."','".
+			$this->gui_wms_mapformat ."','".
+			$this->gui_wms_featureinfoformat ."','".
+			$this->gui_wms_exceptionformat . "','".
+			$this->gui_wms_epsg ."','".
+			$this->gui_wms_visible ."','".
+			$this->gui_wms_opacity ."','".
+			$this->gui_wms_sldurl ."');\n");
+
+		for($i=0;$i<count($this->data_format);$i++){
+			if($parent){
+				echo "parent.";
+			}
+			echo "wms_add_data_type_format('". $this->data_type[$i] ."','". $this->data_format[$i] ."');\n";
+		}
+		for($i=0; $i<count($this->objLayer); $i++){
+			if($this->objLayer[$i]->layer_name == $layer_name|| $this->objLayer[$i]->layer_pos == 0){
+
+				if($parent){
+					echo "parent.";
+				}
+			 print ("wms_add_layer('".
+				$this->objLayer[$i]->layer_parent ."','".
+				$this->objLayer[$i]->layer_uid ."','".
+				$this->objLayer[$i]->layer_name . "','".
+				addslashes($this->objLayer[$i]->layer_title) ."','".
+				$this->objLayer[$i]->layer_dataurl_href ."','".
+				$this->objLayer[$i]->layer_pos ."','".
+				$this->objLayer[$i]->layer_queryable ."','".
+				$this->objLayer[$i]->layer_minscale . "','".
+				$this->objLayer[$i]->layer_maxscale ."','".
+				$this->objLayer[$i]->layer_metadataurl ."','".
+				$this->objLayer[$i]->gui_layer_wms_id ."','".
+				$this->objLayer[$i]->gui_layer_status ."','".
+				$this->objLayer[$i]->gui_layer_style ."','".
+				$this->objLayer[$i]->gui_layer_selectable ."','".
+				$this->objLayer[$i]->gui_layer_visible ."','".
+				$this->objLayer[$i]->gui_layer_queryable ."','".
+				$this->objLayer[$i]->gui_layer_querylayer ."','".
+				$this->objLayer[$i]->gui_layer_minscale ."','".
+				$this->objLayer[$i]->gui_layer_maxscale ."','".
+				$this->objLayer[$i]->gui_layer_wfs_featuretype ."');\n");
+			for($j=0; $j<count($this->objLayer[$i]->layer_epsg);$j++){
+				if($i==0){
+					if($parent){
+					echo "parent.";
+					}
+					print("wms_addSRS('".
+						$this->objLayer[$i]->layer_epsg[$j]["epsg"] ."','".
+						$this->objLayer[$i]->layer_epsg[$j]["minx"] ."','".
+						$this->objLayer[$i]->layer_epsg[$j]["miny"] ."','".
+						$this->objLayer[$i]->layer_epsg[$j]["maxx"] ."','".
+						$this->objLayer[$i]->layer_epsg[$j]["maxy"] ."');\n");
+				}
+				if($parent){
+				echo "parent.";
+				}
+				print("layer_addEpsg('".
+					$this->objLayer[$i]->layer_epsg[$j]["epsg"] ."','".
+					$this->objLayer[$i]->layer_epsg[$j]["minx"] ."','".
+					$this->objLayer[$i]->layer_epsg[$j]["miny"] ."','".
+					$this->objLayer[$i]->layer_epsg[$j]["maxx"] ."','".
+					$this->objLayer[$i]->layer_epsg[$j]["maxy"] ."');\n");
+			}
+			for($j=0; $j<count($this->objLayer[$i]->layer_style);$j++){
+				if($parent){
+				echo "parent.";
+				}
+				print("wms_addLayerStyle('".$this->objLayer[$i]->layer_style[$j]["name"]."', '".$this->objLayer[$i]->layer_style[$j]["title"]."', ".$j.",".$i.",'".$this->objLayer[$i]->layer_style[$j]["legendurl"]."', '".$this->objLayer[$i]->layer_style[$j]["legendformat"]."');\n");
+			}
+		   }
+		}
+	  }
+
+
+	/**
+	* writeObjInDB
+	*
+	* this function exports the information from the xml to the mapbender database
+	*/
+	function writeObjInDB($gui_id){
+		global $con;
+
+		$this->checkObj();
+		db_begin();
+
+		# TABLE wms
+		$sql = "INSERT INTO wms (wms_version, wms_title, wms_abstract, wms_getcapabilities, wms_getmap, ";
+		$sql.= "wms_getfeatureinfo, wms_getlegendurl, wms_getcapabilities_doc, wms_upload_url, fees, ";
+		$sql .= "accessconstraints, contactperson, contactposition, contactorganization, address, city, ";
+		$sql .= "stateorprovince, postcode, country, contactvoicetelephone, contactfacsimiletelephone, contactelectronicmailaddress, ";
+		$sql .= "wms_owner,wms_timestamp, ";
+		$sql .= "wms_supportsld, wms_userlayer, wms_userstyle, wms_remotewfs) ";
+		$sql .= "VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28)";
+		$v = array($this->wms_version,$this->wms_title,$this->wms_abstract,$this->wms_getcapabilities,
+			$this->wms_getmap,$this->wms_getfeatureinfo,$this->wms_getlegendurl,$this->wms_getcapabilities_doc,
+			$this->wms_upload_url,$this->fees,$this->accessconstraints,$this->contactperson,$this->contactposition,
+			$this->contactorganization,$this->address,$this->city,$this->stateorprovince,$this->postcode,$this->country,
+			$this->contactvoicetelephone,$this->contactfacsimiletelephone,$this->contactelectronicmailaddress,
+			$_SESSION['mb_user_id'],strtotime("now"),
+			$this->wms_supportsld,$this->wms_userlayer,$this->wms_userstyle,$this->wms_remotewfs );
+		$t = array('s','s','s','s','s','s','s','s','s','s','s','s','s','s','s','s','s','s','s','s','s','s','i','i','s','s','s','s');
+		$res = db_prep_query($sql,$v,$t);
+		if(!$res){
+			db_rollback();
+		}
+
+		$myWMS = db_insert_id($con,'wms', 'wms_id');
+
+		# TABLE layer and gui_layer
+
+		for($i=0; $i<count($this->objLayer); $i++){
+			$this->insertLayer($i,$myWMS,$gui_id);
+			$this->insertGuiLayer($i,$myWMS,$gui_id);
+		}
+
+
+		#TABLE wms_srs
+		$this->insertSRS($myWMS);
+
+		# TABLE wms_format
+		$this->insertFormat($myWMS);
+
+		# TABLE gui_wms
+
+		$sql ="SELECT MAX(gui_wms_position) AS pos FROM gui_wms WHERE fkey_gui_id = $1";
+		$v = array($gui_id);
+		$t = array('s');
+		$res = db_prep_query($sql,$v,$t);
+		if(db_result($res, 0,"pos") > -1){
+			$position = db_result($res, 0,"pos") + 1;
+		} else{ $position = 0; }
+
+		$sql ="INSERT INTO gui_wms (fkey_gui_id, fkey_wms_id, gui_wms_position, gui_wms_mapformat, ";
+		$sql .= "gui_wms_featureinfoformat, gui_wms_exceptionformat, gui_wms_epsg)";
+		$sql .= "VALUES($1,$2,$3,$4,$5,$6,$7)";
+		$v = array($gui_id,$myWMS,$position,$this->gui_wms_mapformat,$this->gui_wms_featureinfoformat,
+				$this->gui_wms_exceptionformat,$this->gui_wms_epsg
+				);
+		$t = array('s','i','i','s','s','s','s');
+		$res = db_prep_query($sql,$v,$t);
+		if(!$res){
+			db_rollback();
+		}
+		db_commit();
+
+	    #Changes JW
+	    $this->wms_id = $myWMS;
+	}
+	function insertLayer($i,$myWMS){
+		global $con;
+		$sql = "INSERT INTO layer(fkey_wms_id, layer_pos, layer_parent, layer_name, layer_title, ";
+		$sql .= " layer_queryable, layer_minscale, layer_maxscale,layer_dataurl,layer_metadataurl,layer_abstract) ";
+		$sql .= "VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)";
+		if($this->objLayer[$i]->layer_id != null){
+			$tmpPos =  $this->objLayer[$i]->layer_id;
+		}
+		else {
+			$tmpPos .= 0;
+		}
+		if($this->objLayer[$i]->layer_parent == '' && $this->objLayer[$i]->layer_parent != '0'){
+			$this->objLayer[$i]->layer_parent = '';
+		}
+		$v = array($myWMS,$tmpPos,$this->objLayer[$i]->layer_parent,$this->objLayer[$i]->layer_name,
+				$this->objLayer[$i]->layer_title,
+				$this->objLayer[$i]->layer_queryable,$this->objLayer[$i]->layer_minscale,
+				$this->objLayer[$i]->layer_maxscale,$this->objLayer[$i]->layer_dataurl_href,
+				$this->objLayer[$i]->layer_metadataurl,$this->objLayer[$i]->layer_abstract);
+		$t = array('i','i','s','s','s','i','i','i','s','s','s');
+		$res = db_prep_query($sql,$v,$t);
+		if(!$res){
+			db_rollback();
+		}
+		else {
+			# save the id of each layer: set param2 true
+			$this->objLayer[$i]->db_id = db_insert_id($con, 'layer','layer_id');
+			$this->insertLayerEPSG($i);
+
+			# TABLE layer_style for each layer
+			$this->insertLayerStyle($i);
+
+			# insert Keywords
+			$this->insertLayerKeyword($i);
+
+		}
+	}
+	function updateLayer($i,$myWMS){
+		$sql = "SELECT layer_id FROM layer WHERE fkey_wms_id = $1 AND layer_name = $2";
+		$v = array($myWMS,$this->objLayer[$i]->layer_name);
+		$t = array('i','s');
+		$res = db_prep_query($sql,$v,$t);
+		if($row = db_fetch_array($res)){
+			$l_id = $row['layer_id'];
+		}
+		else{
+			db_rollback();
+			$e = new mb_exception("Not found: ".$this->objLayer[$i]->layer_name);
+			return;
+		}
+
+		$sql = "UPDATE layer SET ";
+		$sql .= "layer_pos = $1, ";
+		$sql .= "layer_parent = $2, ";
+		$sql .= "layer_title = $3, ";
+		$sql .= "layer_queryable = $4, ";
+		$sql .= "layer_minscale = $5, ";
+		$sql .= "layer_maxscale = $6, ";
+		$sql .= "layer_dataurl = $7, ";
+		$sql .= "layer_metadataurl = $8, ";
+		$sql .= "layer_abstract = $9 ";
+		$sql .= "WHERE layer_id = $10";
+
+		if($this->objLayer[$i]->layer_id != null){
+			$tmpPos =  $this->objLayer[$i]->layer_id;
+		}
+		else {
+			$tmpPos .= 0;
+		}
+		if($this->objLayer[$i]->layer_parent == '' && $this->objLayer[$i]->layer_parent != '0'){
+			$this->objLayer[$i]->layer_parent = '';
+		}
+		$v = array($tmpPos,$this->objLayer[$i]->layer_parent,
+				$this->objLayer[$i]->layer_title,
+				$this->objLayer[$i]->layer_queryable,$this->objLayer[$i]->layer_minscale,
+				$this->objLayer[$i]->layer_maxscale,$this->objLayer[$i]->layer_dataurl_href,
+				$this->objLayer[$i]->layer_metadataurl,$this->objLayer[$i]->layer_abstract, $l_id
+			);
+		$t = array('i','s','s','i','i','i','s','s','s','i');
+		$res = db_prep_query($sql,$v,$t);
+		if($this->overwrite == true){
+			$sql = "UPDATE layer SET ";
+			$sql .= "layer_title = $1, ";
+			$sql .= "layer_abstract = $2 ";
+			$sql .= "WHERE layer_id = $3";
+
+			$v = array($this->objLayer[$i]->layer_title,$this->objLayer[$i]->layer_abstract, $l_id);
+			$t = array('s','s','i');
+			$res = db_prep_query($sql,$v,$t);
+		}
+		if(!$res){
+			db_rollback();
+		}
+		else {
+
+			# save the id of each layer: set param2 true
+			$this->objLayer[$i]->db_id = $l_id;
+			$this->insertLayerEPSG($i);
+
+			# TABLE layer_style for each layer
+			$this->insertLayerStyle($i);
+			if($this->overwrite == true){
+				$this->insertLayerKeyword($i);
+			}
+		}
+	}
+	function insertGuiLayer($i,$myWMS,$gui_id){
+		# table gui_layer
+
+		$sql = "INSERT INTO gui_layer (fkey_gui_id, fkey_layer_id, gui_layer_wms_id, ";
+		$sql .= "gui_layer_status, gui_layer_selectable, gui_layer_visible, gui_layer_queryable, ";
+		$sql .= "gui_layer_querylayer,gui_layer_minscale,gui_layer_maxscale, gui_layer_priority, gui_layer_style) ";
+		$sql .= "VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12)";
+		if(count($this->objLayer[$i]->layer_style)>0){
+			$layer_style_name = $this->objLayer[$i]->layer_style[0]["name"];
+		}
+		else{
+			$layer_style_name = NULL;
+		}
+		$v = array($gui_id,$this->objLayer[$i]->db_id,$myWMS,1,1,1,$this->objLayer[$i]->layer_queryable,
+			$this->objLayer[$i]->layer_queryable,$this->objLayer[$i]->layer_minscale,$this->objLayer[$i]->layer_maxscale,$i,$layer_style_name);
+		$t = array('s','i','i','i','i','i','i','i','i','i','i','s');
+		$res = db_prep_query($sql,$v,$t);
+		#$e = new mb_exception("name des insert styles und fkey_layer_id: ".$layer_style_name." --- ".$this->objLayer[$i]->db_id);
+		if(!$res){
+			db_rollback();
+		}
+	}
+	function appendGuiLayer($i,$myWMS,$gui_id){
+		# table gui_layer
+
+		$sql = "INSERT INTO gui_layer (fkey_gui_id, fkey_layer_id, gui_layer_wms_id, ";
+		$sql .= "gui_layer_status, gui_layer_selectable, gui_layer_visible, gui_layer_queryable, ";
+		$sql .= "gui_layer_querylayer,gui_layer_minscale,gui_layer_maxscale, gui_layer_priority, gui_layer_style) ";
+		$sql .= "VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12)";
+		if(count($this->objLayer[$i]->layer_style)>0){
+			$layer_style_name = $this->objLayer[$i]->layer_style[0]["name"];
+		}
+		else{
+			$layer_style_name = NULL;
+		}
+		$v = array($gui_id,$this->objLayer[$i]->db_id,$myWMS,0,0,0,$this->objLayer[$i]->layer_queryable,
+			$this->objLayer[$i]->layer_queryable,$this->objLayer[$i]->layer_minscale,$this->objLayer[$i]->layer_maxscale,$i,$layer_style_name);
+		$t = array('s','i','i','i','i','i','i','i','i','i','i','s');
+		$res = db_prep_query($sql,$v,$t);
+		if(!$res){
+			db_rollback();
+		}
+	}
+	function insertSRS($myWMS){
+		for($i=0; $i<count($this->wms_srs);$i++){
+			$sql ="INSERT INTO wms_srs (fkey_wms_id, wms_srs) values($1,$2)";
+			$v = array($myWMS,mb_strtoupper($this->wms_srs[$i]));
+			$t = array('i','s');
+			$res = db_prep_query($sql,$v,$t);
+			if(!$res){
+				db_rollback();
+			}
+		}
+	}
+	function insertFormat($myWMS){
+		for($i=0; $i<count($this->data_type);$i++){
+			$sql ="INSERT INTO wms_format (fkey_wms_id, data_type, data_format) ";
+			$sql .= " VALUES($1,$2,$3)";
+			$v = array($myWMS,$this->data_type[$i],$this->data_format[$i]);
+			$t = array('i','s','s');
+			$res = db_prep_query($sql,$v,$t);
+			if(!$res){
+				db_rollback();
+			}
+		}
+	}
+	function insertLayerEPSG($i){
+		$sql = "DELETE FROM layer_epsg WHERE fkey_layer_id = $1";
+		$v = array($this->objLayer[$i]->db_id);
+		$t = array('i');
+		$res = db_prep_query($sql,$v,$t);
+		for($j=0; $j<count($this->objLayer[$i]->layer_epsg);$j++){
+			$sql = "INSERT INTO layer_epsg (fkey_layer_id, epsg, minx, miny, maxx, maxy) ";
+			$sql .= "VALUES($1,$2,$3,$4,$5,$6)";
+			$v = array($this->objLayer[$i]->db_id,$this->objLayer[$i]->layer_epsg[$j][epsg],
+				$this->objLayer[$i]->layer_epsg[$j][minx],$this->objLayer[$i]->layer_epsg[$j][miny],
+				$this->objLayer[$i]->layer_epsg[$j][maxx],$this->objLayer[$i]->layer_epsg[$j][maxy]
+				);
+			$t = array('i','s','d','d','d','d');
+			$res = db_prep_query($sql,$v,$t);
+			if(!$res){
+				db_rollback();
+			}
+		}
+	}
+	function insertLayerStyle($i){
+		$sql = "DELETE FROM layer_style WHERE fkey_layer_id = $1";
+		$v = array($this->objLayer[$i]->db_id);
+		$t = array('i');
+		$res = db_prep_query($sql,$v,$t);
+		for($j=0; $j<count($this->objLayer[$i]->layer_style);$j++){
+			$sql = "INSERT INTO layer_style (fkey_layer_id, name, title, legendurl, legendurlformat) ";
+			$sql .= "VALUES($1,$2,$3,$4,$5)";
+			$v = array($this->objLayer[$i]->db_id,$this->objLayer[$i]->layer_style[$j]["name"],
+					$this->objLayer[$i]->layer_style[$j]["title"],$this->objLayer[$i]->layer_style[$j]["legendurl"],
+					$this->objLayer[$i]->layer_style[$j]["legendurlformat"]
+				);
+			$t = array('i','s','s','s','s');
+			$res = db_prep_query($sql,$v,$t);
+			if(!$res){
+				db_rollback();
+			}
+		}
+	}
+	function insertLayerKeyword($i){
+		global $con;
+		$sql = "DELETE FROM layer_keyword WHERE fkey_layer_id = $1";
+		$v = array($this->objLayer[$i]->db_id);
+		$t = array('i');
+		$res = db_prep_query($sql,$v,$t);
+
+//		var_dump($this);
+		$k = $this->objLayer[$i]->layer_keyword;
+//		var_dump($k);
+		for($j=0; $j<count($k); $j++){
+			$keyword_id = "";
+
+			while ($keyword_id == "") {
+				$sql = "SELECT keyword_id FROM keyword WHERE UPPER(keyword) = UPPER($1)";
+				$v = array($k[$j]);
+				$t = array('s');
+				$res = db_prep_query($sql,$v,$t);
+				$row = db_fetch_array($res);
+			//print_r($row);
+				if ($row) {
+					$keyword_id = $row["keyword_id"];
+				}
+				else {
+					$sql_insertKeyword = "INSERT INTO keyword (keyword)";
+					$sql_insertKeyword .= "VALUES ($1)";
+					$v1 = array($k[$j]);
+					$t1 = array('s');
+					$res_insertKeyword = db_prep_query($sql_insertKeyword,$v1,$t1);
+					if(!$res_insertKeyword){
+						db_rollback();
+					}
+				}
+			}
+
+			// check if layer/keyword combination already exists
+			$sql_layerKeywordExists = "SELECT * FROM layer_keyword WHERE fkey_layer_id = $1 AND fkey_keyword_id = $2";
+			$v = array($this->objLayer[$i]->db_id, $keyword_id);
+			$t = array('i', 'i');
+			$res_layerKeywordExists = db_prep_query($sql_layerKeywordExists, $v, $t);
+			$row = db_fetch_array($res_layerKeywordExists);
+			//print_r($row);
+			if (!$row) {
+				$sql1 = "INSERT INTO layer_keyword (fkey_keyword_id,fkey_layer_id)";
+				$sql1 .= "VALUES ($1,$2)";
+				$v1 = array($keyword_id,$this->objLayer[$i]->db_id);
+				$t1 = array('i','i');
+				$res1 = db_prep_query($sql1,$v1,$t1);
+				if(!$res1){
+					db_rollback();
+				}
+			}
+		}
+	}
+	function updateObjInDB($myWMS){
+		db_begin();
+
+		$sql = "UPDATE wms SET ";
+		$sql .= "wms_version = $1 ,";
+		$sql .= "wms_getcapabilities  = $2 ,";
+		$sql .= "wms_getmap  = $3 ,";
+		$sql .= "wms_getfeatureinfo  = $4 ,";
+		$sql .= "wms_getlegendurl  = $5 ,";
+		$sql .= "wms_getcapabilities_doc = $6 ,";
+		$sql .= "wms_upload_url = $7,  ";
+		$sql .= "wms_owner = $8, ";
+		$sql .= "wms_timestamp = $9, ";
+		$sql .= "wms_supportsld = $10, ";
+		$sql .= "wms_userlayer = $11, ";
+		$sql .= "wms_userstyle = $12, ";
+		$sql .= "wms_remotewfs = $13 ";
+		$sql .= " WHERE wms_id = $14";
+
+		$v = array($this->wms_version,$this->wms_getcapabilities,
+			$this->wms_getmap,$this->wms_getfeatureinfo,$this->wms_getlegendurl,
+			$this->wms_getcapabilities_doc,$this->wms_upload_url,$_SESSION["mb_user_id"],strtotime("now"),
+			$this->wms_supportsld,$this->wms_userlayer,$this->wms_userstyle,$this->wms_remotewfs,$myWMS);
+		$t = array('s','s','s','s','s','s','s','i','i','s','s','s','s','i');
+
+		$res = db_prep_query($sql,$v,$t);
+		if(!$res){
+			db_rollback();
+		}
+
+		if($this->overwrite == true){
+			$sql = "UPDATE wms SET ";
+			$sql .= "wms_title  = $1 ,";
+			$sql .= "wms_abstract  = $2 ,";
+			$sql .= "fees = $3, ";
+			$sql .= "accessconstraints = $4, ";
+			$sql .= "contactperson = $5, ";
+			$sql .= "contactposition = $6, ";
+			$sql .= "contactorganization = $7, ";
+			$sql .= "address = $8, ";
+			$sql .= "city = $9, ";
+			$sql .= "stateorprovince = $10, ";
+			$sql .= "postcode = $11, ";
+			$sql .= "country = $12, ";
+			$sql .= "contactvoicetelephone = $13, ";
+			$sql .= "contactfacsimiletelephone = $14, ";
+			$sql .= "contactelectronicmailaddress = $15 ";
+			$sql .= " WHERE wms_id = $16";
+
+			$v = array($this->wms_title,$this->wms_abstract,$this->fees,$this->accessconstraints,
+				$this->contactperson,$this->contactposition,$this->contactorganization,$this->address,
+				$this->city,$this->stateorprovince,$this->postcode,$this->country,$this->contactvoicetelephone,
+				$this->contactfacsimiletelephone,$this->contactelectronicmailaddress,$myWMS);
+			$t = array('s','s','s','s','s','s','s','s','s','s','s','s','s','s','s','i');
+			$res = db_prep_query($sql,$v,$t);
+			if(!$res){
+				db_rollback();
+			}
+		}
+
+		# delete and refill srs and formats
+		$sql = "DELETE FROM wms_srs WHERE fkey_wms_id = $1 ";
+		$v = array($myWMS);
+		$t = array('i');
+		$res = db_prep_query($sql,$v,$t);
+		if(!$res){
+			db_rollback();
+		}
+		$this->insertSRS($myWMS);
+
+		$sql = "DELETE FROM wms_format WHERE fkey_wms_id = $1 ";
+		$v = array($myWMS);
+		$t = array('i');
+		$res = db_prep_query($sql,$v,$t);
+		if(!$res){
+			db_rollback();
+		}
+		$this->insertFormat($myWMS);
+
+		# update gui_wms
+		$this->update_gui_wms($myWMS);
+
+		# update TABLE layer
+		# delete all layer which are outdated
+		$v = array($myWMS);
+		$t = array('i');
+		$c = 2;
+		$sql = "DELETE FROM layer WHERE fkey_wms_id = $1 AND NOT layer_name IN(";
+		for($i=0; $i<count($this->objLayer); $i++){
+			if($i>0){$sql .= ',';}
+			$sql .= "$".$c;
+			array_push($v,$this->objLayer[$i]->layer_name);
+			array_push($t,'s');
+			$c++;
+		}
+		$sql .= ")";
+
+		$res = db_prep_query($sql,$v,$t);
+		if(!$res){
+			db_rollback();
+		}
+
+		# update or insert?
+		$sql = "SELECT layer_name FROM layer WHERE fkey_wms_id = $1";
+		$v = array($myWMS);
+		$t = array('i');
+		$res = db_prep_query($sql,$v,$t);
+		$exLayer = array();
+		while($row = db_fetch_array($res)){
+			array_push($exLayer,$row["layer_name"]);
+		}
+
+		$sql = "SELECT fkey_gui_id FROM gui_wms WHERE fkey_wms_id = $1";
+		$v = array($myWMS);
+		$t = array('i');
+		$res = db_prep_query($sql,$v,$t);
+		$exGui = array();
+		while($row = db_fetch_array($res)){
+			array_push($exGui,$row["fkey_gui_id"]);
+		}
+
+		for($i=0; $i<count($this->objLayer); $i++){
+			if(in_array($this->objLayer[$i]->layer_name,$exLayer)){
+				//echo "<br>update: ".$this->objLayer[$i]->layer_name;
+				$this->updateLayer($i,$myWMS);
+				for($j=0; $j<count($exGui); $j++){
+					$this->updateGuiLayer($i,$myWMS,$exGui[$j]);
+				}
+			}
+			else{
+				//echo "<br>append: ".$this->objLayer[$i]->layer_name;
+				$this->insertLayer($i,$myWMS);
+				for($j=0; $j<count($exGui); $j++){
+					$this->appendGuiLayer($i,$myWMS,$exGui[$j]);
+				}
+			}
+		}
+		db_commit();
+		return;
+	}
+	function updateGuiLayer($i,$myWMS,$gui_id){
+		$sql = "SELECT layer_id FROM layer WHERE fkey_wms_id = $1 AND layer_name = $2";
+		$v = array($myWMS,$this->objLayer[$i]->layer_name);
+		$t = array('i','s');
+		$res = db_prep_query($sql,$v,$t);
+		if($row = db_fetch_array($res)){
+			$l_id = $row['layer_id'];
+		}
+		else{
+			db_rollback();
+			$e = new mb_exception("Not found: ".$this->objLayer[$i]->layer_name. " in gui: ".$gui_id);
+			return;
+		}
+
+		$sql = "SELECT * FROM gui_layer WHERE fkey_layer_id = $1 and fkey_gui_id = $2";
+		$v = array($l_id,$gui_id);
+		$t = array('i','s');
+		$res = db_prep_query($sql,$v,$t);
+		while($row = db_fetch_array($res)){
+			if($this->objLayer[$i]->layer_queryable == 0){
+				$sql1 = "UPDATE gui_layer set gui_layer_queryable = 0, gui_layer_querylayer = 0 ";
+				$sql1 .= "WHERE fkey_layer_id = $1 and fkey_gui_id = $2";
+				$v = array($l_id,$gui_id);
+				$t = array('i','s');
+				$res1 = db_prep_query($sql1,$v,$t);
+				if(!$res1){
+
+				db_rollback();
+				}
+			}
+			if($this->objLayer[$i]->layer_queryable == 1){
+				$sql1 = "UPDATE gui_layer set gui_layer_queryable = 1 ";
+				$sql1 .= "WHERE fkey_layer_id = $1 and fkey_gui_id = $2";
+				$v = array($l_id,$gui_id);
+				$t = array('i','s');
+				$res1 = db_prep_query($sql1,$v,$t);
+				if(!$res1){
+
+					db_rollback();
+				}
+			}
+			if($row["gui_layer_minscale"] < $this->objLayer[$i]->layer_minscale){
+				$sql1 = "UPDATE gui_layer set gui_layer_minscale = $1 ";
+				$sql1 .= "WHERE fkey_layer_id = $2 and fkey_gui_id = $3";
+				$v = array($this->objLayer[$i]->layer_minscale,$l_id,$gui_id);
+				$t = array('i','i','s');
+				$res1 = db_prep_query($sql1,$v,$t);
+				if(!$res1){db_rollback();
+				}
+			}
+			if($row["gui_layer_maxscale"] > $this->objLayer[$i]->layer_maxscale){
+				$sql1 = "UPDATE gui_layer set gui_layer_maxscale = $1 ";
+				$sql1 .= "WHERE fkey_layer_id = $2 and fkey_gui_id = $3";
+				$v = array($this->objLayer[$i]->layer_maxscale,$l_id,$gui_id);
+				$t = array('i','i','s');
+				$res1 = db_prep_query($sql1,$v,$t);
+				if(!$res1){db_rollback();
+				}
+			}
+		}
+	}
+	function update_gui_wms($myWMS){
+		$mySubmit = null;
+		$sql = "SELECT * FROM gui_wms where fkey_wms_id = $1";
+		$v = array($myWMS);
+		$t = array('i');
+		$res = db_prep_query($sql,$v,$t);
+		$cnt = 0;
+		while($row = db_fetch_array($res)){
+			unset($mySubmit);
+			$myGUI[$cnt] = $row["fkey_gui_id"];
+
+			$sql = "UPDATE gui_wms SET ";
+			$v = array();
+			$t = array();
+			$paramCount = 0;
+
+			for($i=0; $i<count($this->data_type); $i++){
+				# gui_wms_mapformat
+				if(mb_strtolower($this->data_type[$i]) == "map" && mb_strtolower($this->data_format[$i]) == mb_strtolower($row["gui_wms_mapformat"])){
+					$myMapFormat = true;
+				}
+				# gui_wms_featureinfoformat
+				if(mb_strtolower($this->data_type[$i]) == "featureinfo" && mb_strtolower($this->data_format[$i]) == mb_strtolower($row["gui_wms_featureinfoformat"])){
+					$myFeatureInfoFormat = true;
+				}
+				# gui_wms_exceptionformat
+				if(mb_strtolower($this->data_type[$i]) == "exception" && mb_strtolower($this->data_format[$i]) == mb_strtolower($row["gui_wms_exceptionformat"])){
+					$myExceptionFormat = true;
+				}
+			}
+			if(!$myMapFormat){
+				$paramCount++;
+				$sql .= "gui_wms_mapformat = $" . $paramCount . " ";
+				$mySubmit = true;
+				array_push($v, $this->gui_wms_mapformat);
+				array_push($t, "s");
+			}
+			if(!$myFeatureInfoFormat){
+				if($mySubmit){ $sql .= ",";}
+				$paramCount++;
+				$sql .= "gui_wms_featureinfoformat = $" . $paramCount . " ";
+				array_push($v, $this->gui_wms_featureinfoformat);
+				array_push($t, "s");
+				$mySubmit = true;
+			}
+			if(!$myExceptionFormat){
+				if($mySubmit){ $sql .= ",";}
+				$paramCount++;
+				$sql .= "gui_wms_exceptionformat = $" . $paramCount ." ";
+				array_push($v, $this->gui_wms_exceptionformat);
+				array_push($t, "s");
+				$mySubmit = true;
+			}
+
+			# gui_wms_epsg
+			for($j=0; $j<count($this->objLayer[0]->layer_epsg);$j++){
+				if($this->objLayer[0]->layer_epsg[$j][epsg] == mb_strtoupper($row["gui_wms_epsg"])){
+					$myGUI_EPSG = true;
+				}
+			}
+			if(!$myGUI_EPSG){
+				if($mySubmit){ $sql .= ",";}
+				$paramCount++;
+				$sql .= "gui_wms_epsg = $" . $paramCount . " ";
+				array_push($v, $this->gui_wms_epsg);
+				array_push($t, "s");
+				$mySubmit = true;
+			}
+			$paramCount++;
+			$sql .= " WHERE fkey_gui_id = $" . $paramCount . " ";
+			array_push($v, $row["fkey_gui_id"]);
+			array_push($t, "s");
+
+			$paramCount++;
+			$sql .= "AND fkey_wms_id = $" . $paramCount;
+			array_push($v, $myWMS);
+			array_push($t, "i");
+			if($mySubmit){
+				$res = db_prep_query($sql,$v,$t);
+				if(!$res){
+					db_rollback();
+					echo "<pre>".$sql."</pre><br> <br><p>";
+				 	echo db_error();
+				 	echo "<br /> UPDATE ERROR -> KILL PROCESS AND ROLLBACK....................no update<br><br>";
+					$e = new mb_exception("class_wms.php: transaction: Transaction aborted, rollback.");
+				}
+			}
+			$cnt++;
+		}
+	}
+	function getVersion() {
+		return $this->wms_version;
+	}
+
+	function getCapabilities() {
+		return $this->wms_getcapabilities;
+	}
+
+	function getCapabilitiesDoc() {
+		return $this->wms_getcapabilities_doc;
+	}
+
+	/**
+	* creatObjfromDB
+	*
+	*/
+	  function createObjFromDB($gui_id,$wms_id){
+
+		$sql = "Select * from gui_wms where fkey_wms_id = $1 AND fkey_gui_id = $2";
+		$v = array($wms_id,$gui_id);
+		$t = array('i','s');
+		$res = db_prep_query($sql,$v,$t);
+
+		$count=0;
+		#$res_count=db_num_rows($res);
+
+
+		while($row = db_fetch_array($res)){
+			$this->gui_wms_mapformat=$row["gui_wms_mapformat"];
+			$this->gui_wms_featureinfoformat=$row["gui_wms_featureinfoformat"];
+			$this->gui_wms_exceptionformat=$row["gui_wms_exceptionformat"];
+			$this->gui_wms_epsg=$row["gui_wms_epsg"];
+			$this->gui_wms_visible = $row["gui_wms_visible"];
+			$this->gui_wms_opacity = $row["gui_wms_opacity"];
+			$this->gui_wms_sldurl = $row["gui_wms_sldurl"];
+
+			$sql = "Select * from wms where wms_id = $1 ";
+			$v = array($wms_id);
+			$t = array('i');
+			$res_wms = db_prep_query($sql,$v,$t);
+			$count_wms=0;
+			while($row2 = db_fetch_array($res_wms)){
+				$this->wms_id = $row2["wms_id"];
+				$this->wms_version = $row2["wms_version"];
+				$this->wms_title = $this->stripEndlineAndCarriageReturn($row2["wms_title"]);
+				$this->wms_abstract = $this->stripEndlineAndCarriageReturn($row2["wms_abstract"]);
+				$wmsowsproxy = $row2["wms_owsproxy"];
+				#$wmsowsproxy = "test";
+				if($wmsowsproxy != ""){
+					$owsproxyurl = OWSPROXY."/".session_id()."/".$wmsowsproxy."?";
+					$this->wms_getmap = $owsproxyurl;
+					$this->wms_getcapabilities =  $owsproxyurl;
+					$this->wms_getfeatureinfo = $owsproxyurl;
+					$this->wms_getlegendurl = $owsproxyurl;
+				}
+				else{
+					$this->wms_getmap =  $row2["wms_getmap"];
+					$this->wms_getcapabilities =  $row2["wms_getcapabilities"];
+					$this->wms_getfeatureinfo = $row2["wms_getfeatureinfo"];
+					$this->wms_getlegendurl = $row2["wms_getlegendurl"];
+				}
+				$this->wms_getcapabilities_doc = $row2["wms_getcapabilities_doc"];
+				$this->wms_filter = $row2["wms_filter"];
+				$this->wms_supportsld = $row2["wms_supportsld"];
+				$this->wms_userlayer = $row2["wms_userlayer"];
+				$this->wms_userstyle = $row2["wms_userstyle"];
+				$this->wms_remotewfs = $row2["wms_remotewfs"];
+
+				$count_wms++;
+			}
+
+			### formats
+			$sql = "SELECT * FROM wms_format WHERE fkey_wms_id = $1 ";
+			$v = array($wms_id);
+			$t = array('i');
+			$res_wms = db_prep_query($sql,$v,$t);
+			$count_format=0;
+			while($row3 = db_fetch_array($res_wms)){
+				$this->data_type[$count_format] = $row3["data_type"];
+				$this->data_format[$count_format] = $row3["data_format"];
+				$count_format++;
+			}
+			$count++;
+		}
+
+		#layer
+		$sql = "Select * from gui_layer where gui_layer_wms_id = $1 AND fkey_gui_id = $2 ";
+		$sql .= " AND gui_layer_status = 1 ORDER BY gui_layer_priority;";
+		$v = array($wms_id,$gui_id);
+		$t = array('i','s');
+		$res = db_prep_query($sql,$v,$t);
+		$count=0;
+
+		while($row = db_fetch_array($res)){
+			$layer_id = $row["fkey_layer_id"];
+			$sql = "Select * from layer where layer_id = $1";
+			$v = array($layer_id);
+			$t = array('i');
+			$res_layer = db_prep_query($sql,$v,$t);
+			$count_layer=0;
+			while($row2 = db_fetch_array($res_layer)){
+				$this->addLayer($row2["layer_pos"],$row2["layer_parent"]);
+				$layer_cnt=count($this->objLayer)-1;
+				$this->objLayer[$layer_cnt]->layer_uid = $layer_id;
+				$this->objLayer[$layer_cnt]->layer_name =$row2["layer_name"];
+				$this->objLayer[$layer_cnt]->layer_title =$row2["layer_title"];
+				$this->objLayer[$layer_cnt]->layer_dataurl_href =$row2["layer_dataurl"];
+				$this->objLayer[$layer_cnt]->layer_metadataurl =$row2["layer_metadataurl"];
+				$this->objLayer[$layer_cnt]->layer_pos =$row2["layer_pos"];
+				$this->objLayer[$layer_cnt]->layer_queryable =$row2["layer_pos"];
+				$this->objLayer[$layer_cnt]->layer_queryable =$row2["layer_queryable"];
+				$this->objLayer[$layer_cnt]->layer_minscale =$row2["layer_minscale"];
+				$this->objLayer[$layer_cnt]->layer_maxscale = $row2["layer_maxscale"];
+				$count_layer++;
+			}
+			$this->objLayer[$layer_cnt]->layer_uid = $layer_id;
+			$this->objLayer[$layer_cnt]->gui_layer_wms_id = $row["gui_layer_wms_id"];
+			$this->objLayer[$layer_cnt]->gui_layer_selectable = $row["gui_layer_selectable"];
+			$this->objLayer[$layer_cnt]->gui_layer_visible = $row["gui_layer_visible"];
+			$this->objLayer[$layer_cnt]->gui_layer_queryable = $row["gui_layer_queryable"];
+			$this->objLayer[$layer_cnt]->gui_layer_querylayer = $row["gui_layer_querylayer"];
+			$this->objLayer[$layer_cnt]->gui_layer_minscale = $row["gui_layer_minscale"];
+			$this->objLayer[$layer_cnt]->gui_layer_maxscale = $row["gui_layer_maxscale"];
+			$this->objLayer[$layer_cnt]->gui_layer_style = $row["gui_layer_style"];
+			$this->objLayer[$layer_cnt]->gui_layer_wfs_featuretype = $row["gui_layer_wfs_featuretype"];
+
+			$sql = "Select * from layer_epsg where fkey_layer_id = $1 ORDER BY fkey_layer_id";
+			$v = array($layer_id);
+			$t = array('i');
+			$res_layer_epsg = db_prep_query($sql,$v,$t);
+
+			$count_layer_epsg=0;
+			while($row2 = db_fetch_array($res_layer_epsg)){
+				$this->objLayer[$layer_cnt]->layer_epsg[$count_layer_epsg]["epsg"]=$row2["epsg"];
+				$this->objLayer[$layer_cnt]->layer_epsg[$count_layer_epsg]["minx"]=$row2["minx"];
+				$this->objLayer[$layer_cnt]->layer_epsg[$count_layer_epsg]["miny"]=$row2["miny"];
+				$this->objLayer[$layer_cnt]->layer_epsg[$count_layer_epsg]["maxx"]=$row2["maxx"];
+				$this->objLayer[$layer_cnt]->layer_epsg[$count_layer_epsg]["maxy"]=$row2["maxy"];
+				$count_layer_epsg++;
+			}
+
+			### handle styles
+			$sql = "SELECT * FROM layer_style WHERE fkey_layer_id = $1 ";
+			$v = array($layer_id);
+			$t = array('i');
+			$res_style = db_prep_query($sql,$v,$t);
+			$count_layer_style = 0;
+			while($row2 = db_fetch_array($res_style)){
+				$this->objLayer[$layer_cnt]->layer_style[$count_layer_style]["name"]=$row2["name"];
+				$this->objLayer[$layer_cnt]->layer_style[$count_layer_style]["title"]=$row2["title"];
+				if($wmsowsproxy != ""){
+					if($row2["legendurl"]!=''){
+						$this->objLayer[$layer_cnt]->layer_style[$count_layer_style]["legendurl"]=$owsproxyurl.
+						"REQUEST=getlegendgraphic&VERSION=".$this->wms_version."&LAYER=".$this->objLayer[$layer_cnt]->layer_name."&FORMAT=".$row2["legendurlformat"].
+						"&STYLE=".$row2["name"];
+					}
+				}
+				else{
+					if($row2["legendurl"]!=''){
+						$this->objLayer[$layer_cnt]->layer_style[$count_layer_style]["legendurl"]=$row2["legendurl"];
+						#$e = new mb_exception("legendurl = ".$this->objLayer[$layer_cnt]->layer_style[$count_layer_style]["legendurl"]);
+					}
+				}
+				$this->objLayer[$layer_cnt]->layer_style[$count_layer_style]["legendurlformat"]=$row2["legendurlformat"];
+				$count_layer_style++;
+			}
+			$count++;
+		}
+	   }
+	/** end createObjfromDB **/
+
+	  /**
+	* function checkObjExistsInDB()
+	*
+	* this function checks wether the onlineresource already exists in the database.
+	*/
+	function checkObjExistsInDB(){
+
+		$sql = "Select * from wms where wms_getcapabilities = $1";
+		$v = array($this->wms_getcapabilities);
+		$t = array('s');
+		$res = db_prep_query($sql,$v,$t);
+		$res_count= db_num_rows($res);
+		$wms_id=0;
+		if($res_count>0){
+			$count=0;
+			while($row = db_fetch_array($res)){
+				$wms_id=$row["wms_id"];
+				$count++;
+			}
+		}
+		return $wms_id;
+	}
+
+	function displayDBInformation(){
+		echo $this->wms_getcapabilities;
+		$sql="Select * from wms where wms_getcapabilities = $1";
+		$v = array($this->wms_getcapabilities);
+		$t = array('s');
+		$res = db_prep_query($sql,$v,$t);
+		$count=0;
+		while($row = db_fetch_array($res)){
+		echo "count: ".$count."<br>";
+			$wms_id=$row["wms_id"];
+			echo "version: " .$wms_id." <br>";
+			echo "title: " .$row["wms_version"]. " <br>";
+			echo "abstract: " . $row["wms_title"] . " <br>";
+			echo "maprequest: " .$row["wms_abstract"] . " <br>";
+			echo "capabilitiesrequest: " . $row["wms_getcapabilities"] . " <br>";
+			echo "featureinforequest: " . $row["wms_getmap"]. " <br>";
+			echo "gui_wms_mapformat: " . $row["wms_getfeatureinfo"] . " <br>---------<br>";
+			$count++;
+		}
+	   echo "----<br> wms_id: ".$wms_id."<br>";
+
+	   $sql = "Select * from gui_wms where fkey_wms_id = $1";
+	   $v = array($wms_id);
+	   $t = array('i');
+	   echo "sql: ".$sql." <br>---------<br>";
+	   $res = db_prep_query($sql,$v,$t);
+	   $res_count= db_num_rows($res);
+	   echo "result count: ".$res_count." <br>---------<br>";
+
+	   $count=0;
+	   while($row = db_fetch_array($res)){
+	    	echo "gui_wms_featureinfoformat: " . $row["gui_wms_featureinfoformat"]." <br>";
+	    	echo "gui_wms_exceptionformat: " .  $row["gui_wms_exceptionformat"]. " <br>";
+	    	echo "gui_wms_epsg: " .  $row["gui_wms_epsg"]. " <br>";
+	      $count++;
+	   }
+
+	   #db_close($connect);
+	}
+
+	function checkObj(){
+		if($this->wms_getcapabilities == '' || $this->wms_getmap == '' ){
+			echo "<br>Missing parameters: <br>";
+			$this->displayWMS();
+			echo "<br> Data not commited<br>";
+			die();
+		}
+	}
+}
+class layer extends wms {
+	var $layer_id;
+	var $layer_parent;
+	var $layer_name;
+	var $layer_title;
+	var $layer_abstract;
+	var $layer_pos;
+	var $layer_queryable;
+	var $layer_minscale;
+	var $layer_maxscale;
+    var $layer_dataurl_href;
+    var $layer_metadataurl;
+    var $layer_keyword = array();
+	var $layer_epsg = array();
+	var $layer_style = array();
+
+	var $gui_layer_wms_id;
+	var $gui_layer_status = 1;
+	var $gui_layer_selectable = 1;
+	var $gui_layer_visible = 0;
+	var $gui_layer_queryable = 0;
+	var $gui_layer_querylayer = 0;
+	var $gui_layer_style = NULL;
+
+	function layer($id,$parent){
+		$this->layer_id = $id;
+		$this->layer_parent = $parent;
+		//var_dump($this);
+	}
+}
 ?>
\ No newline at end of file



More information about the Mapbender_commits mailing list