session_id($_REQUEST["sid"]);
session_start();
if (!$_SESSION['mb_user_id']) {
    $notice = new mb_notice("Permission denied");
    throwE("Permission denied");
    die;
}
$n = new administration();
//if($_SESSION['mb_user_ip'] != $_SERVER['REMOTE_ADDR']){
//	throwE(array("No session data available.","Permission denied.","Please authenticate."));
//	die();
//}
$wmsId = $n->getWmsIdFromOwsproxyString($query->getOwsproxyServiceId());
#$notice = new mb_notice("wmsid:".$wmsId);
//get authentication infos if they are available in wms table! if not $auth = false
$auth = $n->getAuthInfoOfWMS($wmsId);
#$mb_exception = new mb_exception("auth: ".$auth['username']);
if ($auth['auth_type'] == '') {
    unset($auth);
}
/*************  workflow ************/
$n = new administration();
switch (strtolower($reqParams['request'])) {
    case 'getcapabilities':
        $arrayOnlineresources = checkWmsPermission($query->getOwsproxyServiceId());
        $query->setOnlineResource($arrayOnlineresources['wms_getcapabilities']);
        $request = $query->getRequest();
        if (isset($auth)) {
            getCapabilities($request, $auth);
            #$mb_exception = new mb_exception("auth: ".$auth['auth_type']);
        } else {
 if ($admin->getWMSOWSstring($ownwms[$i]) == false) {
     $status_proxy = 0;
 } else {
     $status_proxy = 1;
 }
 if ($admin->getWmsLogTag($ownwms[$i]) == 1) {
     $status_log = 1;
 } else {
     $status_log = 0;
 }
 if ($admin->getWmsPrice($ownwms[$i]) != 0) {
     $status_price = $admin->getWmsPrice($ownwms[$i]);
 } else {
     $status_price = 0;
 }
 $auth = $admin->getAuthInfoOfWMS($ownwms[$i]);
 if ($auth['auth_type'] == '') {
     $status_auth = 0;
 } else {
     $status_auth = 1;
 }
 echo "<tr>";
 echo "<td>" . $ownwms[$i] . "</td>";
 echo "<td";
 if ($status_auth == 1) {
     echo " bgcolor=\"#FF0000\"";
 }
 echo ">" . $admin->getWmsTitleByWmsId($ownwms[$i]);
 echo "<td>";
 #for owsproxy
 echo "<input type='checkbox' id='wms_" . $ownwms[$i] . "_proxy' name='wms_" . $ownwms[$i] . "_proxy' onclick='if(this.checked){document.getElementById(\"wms_\"+" . $ownwms[$i] . "+\"_log\").disabled=false;document.getElementById(\"wms_\"+" . $ownwms[$i] . "+\"_price\").disabled=true;document.getElementById(\"status_\"+" . $ownwms[$i] . "+\"_proxy\").value=\"1\"}else{document.getElementById(\"wms_\"+" . $ownwms[$i] . "+\"_log\").checked=false;document.getElementById(\"wms_\"+" . $ownwms[$i] . "+\"_log\").disabled=true;document.getElementById(\"wms_\"+" . $ownwms[$i] . "+\"_price\").disabled=true;document.getElementById(\"wms_\"+" . $ownwms[$i] . "+\"_price\").value=\"0\";document.getElementById(\"status_\"+" . $ownwms[$i] . "+\"_proxy\").value=\"0\";document.getElementById(\"status_\"+" . $ownwms[$i] . "+\"_log\").value=\"0\"}'";
 function __construct($reportFile, $autoUpdate, $tmpDir)
 {
     $this->tmpDir = $tmpDir;
     $this->reportFile = $tmpDir . $reportFile;
     //$this->reportFile = $reportFile;
     $this->wmsId = $this->getTagOutOfXML($this->reportFile, 'wms_id');
     $this->uploadId = $this->getTagOutOfXML($this->reportFile, 'upload_id');
     $this->autoUpdate = $autoUpdate;
     $e = new mb_notice("Monitor Report File: " . $this->reportFile);
     $e = new mb_notice("WMS ID: " . $this->wmsId);
     $this->capabilitiesURL = urldecode($this->getTagOutOfXML($this->reportFile, 'getcapurl'));
     //read out from xml
     $e = new mb_notice("GetCapURL: " . $this->capabilitiesURL);
     set_time_limit(TIME_LIMIT);
     $this->timestamp = microtime(TRUE);
     //get authentication info for service
     $admin = new administration();
     $auth = $admin->getAuthInfoOfWMS($this->wmsId);
     if ($auth['auth_type'] == '') {
         unset($auth);
     }
     if ($this->capabilitiesURL) {
         //$remoteWms = new wms(); #exchange by other handling
         $this->timestamp_cap_begin = microtime(TRUE);
         //ok
         //$remoteWms->createObjFromXML($this->capabilitiesURL);#exchange by other handling
         //$this->remoteXML = $remoteWms->wms_getcapabilities_doc;
         if (isset($auth)) {
             $capObject = new connector($this->capabilitiesURL, $auth);
         } else {
             $capObject = new connector($this->capabilitiesURL);
         }
         //decode and encode to have the same behavior as loading caps to database
         $this->remoteXML = $capObject->file;
         $this->timestamp_cap_end = microtime(TRUE);
         //read local copy out of xml
         $this->localXML = urldecode($this->getTagOutOfXML($this->reportFile, 'getcapdoclocal'));
         //			$e=new mb_notice("Remote Caps: ".$this->remoteXML);
         // service unreachable
         if (!$this->remoteXML) {
             $this->result = -1;
             $this->comment = "Connection failed.";
             //$e=new mb_exception("Connection failed");
         } elseif (!$this->localXML) {
             $this->result = 0;
             //$e=new mb_exception("No local Copy of Caps available");
         } else {
             /*
              * compare to local capabilities document
              */
             // capabilities files match
             if ($this->localXML == $this->remoteXML) {
                 $this->result = 1;
                 $this->comment = "WMS is stable.";
                 //$e=new mb_exception("Compare ok - Docs ident");
             } else {
                 $this->result = 0;
                 $this->comment = "WMS is not up to date.";
                 $localXMLArray = explode("\n", htmlentities($this->localXML));
                 $remoteXMLArray = explode("\n", htmlentities($this->remoteXML));
                 $this->capabilitiesDiff = $this->outputDiffHtml($localXMLArray, $remoteXMLArray);
                 //$e=new mb_exception("Problem Docs are out of sync");
             }
         }
         /*
          * if the WMS is available,
          * 1) get a map image
          * 2) update the local backup of the capabilities doc if necessary
          */
         if ($this->result != -1) {
             $this->mapURL = urldecode($this->getTagOutOfXML($this->reportFile, 'getmapurl'));
             //$e=new mb_exception("mapurl:".$this->mapURL);
             if (isset($auth)) {
                 if ($this->isImage($this->mapURL, $auth)) {
                     $this->returnsImage = 1;
                 } else {
                     $this->returnsImage = -1;
                 }
             } else {
                 if ($this->isImage($this->mapURL)) {
                     $this->returnsImage = 1;
                     //$e=new mb_exception("Returns image");
                 } else {
                     $this->returnsImage = -1;
                     //$e=new mb_exception("Returns no image!");
                 }
             }
             //Check for valid XML - validate it again wms 1.1.1 -some problems occur?
             #$dtd = "../schemas/capabilities_1_1_1.dtd";
             #$dom = new domDocument;
             #$dom->loadXML($this->remoteXML);
             #if (!$dom->validate($dtd)) {
             #$this->result = -1;
             #$this->comment = "Invalid getCapabilities request/document or service exception.";
             #}
             #else {
             #$this->comment = "WMS is not up to date but valid!";
             #}
             //Do a simple check if <WMT_MS_Capabilities version="1.1 is part of the remote Cap Dokument
             $searchString = 'WMT_MS_Capabilities';
             $pos = strpos($this->remoteXML, $searchString);
             if ($pos === false) {
                 $this->result = -1;
                 $this->comment = "Invalid getCapabilities request/document or service exception.";
             }
             /*
              * if the local backup of the capabilities document
              * is deprecated, update the local backup
              */
             #if ($this->result == 0) {
             //$mywms = new wms();
             /* 
              * if the capabilities document is valid,
              * update it OR mark it as "not up to date"
              */
             #if ($this->localXML==) {//check validation of capabilities document
             #if ($this->autoUpdate) {
             #$mywms->updateObjInDB($this->wmsId);
             #$this->updated = "1";
             #$this->comment = "WMS has been updated.";
             #}
             #else {
             #	$this->comment = "WMS is not up to date.";
             #}
             #}
             // capabilities document is invalid
             #else {
             #	$this->result = -1;
             #	$this->comment = "Invalid getCapabilities request/document or service exception.";
             #}
             #}
         }
     } else {
         $this->result = -1;
         $this->comment = "Invalid upload URL.";
     }
     #$e = new mb_notice("class_monitor: constructor: result = " . $this->result);
     #$e = new mb_notice("class_monitor: constructor: comment = " . $this->comment);
     #$e = new mb_notice("class_monitor: constructor: returnsImage = " . $this->returnsImage);
 }