caseinsensitiv $reqParams = $query->getRequestParams(); $notice = new mb_notice("owsproxy id:".$query->getOwsproxyServiceId()); // check session session_regenerate_id(); session_destroy(); session_id($_REQUEST["sid"]); session_start(); if(!$_SESSION['mb_user_id']){ $notice = new mb_notice("Permission denied"); throwE("Permission denied"); die(); } //if($_SESSION['mb_user_ip'] != $_SERVER['REMOTE_ADDR']){ // throwE(array("No session data available.","Permission denied.","Please authenticate.")); // die(); //} /************* workflow ************/ $n = new administration(); switch (strtolower($reqParams['request'])) { case 'getcapabilities': $arrayOnlineresources = checkWmsPermission($query->getOwsproxyServiceId()); $query->setOnlineResource($arrayOnlineresources['wms_getcapabilities']); $request = $query->getRequest(); getCapabilities($request); break; case 'getfeatureinfo': $arrayOnlineresources = checkWmsPermission($query->getOwsproxyServiceId()); $query->setOnlineResource($arrayOnlineresources['wms_getfeatureinfo']); $request = $query->getRequest(); getFeatureInfo($request); break; case 'getmap': $arrayOnlineresources = checkWmsPermission($owsproxyService); $query->setOnlineResource($arrayOnlineresources['wms_getmap']); $layers = checkLayerPermission($arrayOnlineresources['wms_id'],$reqParams['layers']); if($layers===""){ throwE("Permission denied"); die(); } $query->setParam("layers",$layers); $request = $query->getRequest(); getImage($request); break; case 'map': $arrayOnlineresources = checkWmsPermission($owsproxyService); $query->setOnlineResource($arrayOnlineresources['wms_getmap']); $layers = checkLayerPermission($arrayOnlineresources['wms_id'],$reqParams['layers']); if($layers===""){ throwE("Permission denied"); die(); } $query->setParam("layers",$layers); $request = $query->getRequest(); getImage($request); break; case 'getlegendgraphic': $url = getLegendUrl($query->getOwsproxyServiceId()); getImage($url); break; case 'external': getExternalRequest($query->getOwsproxyServiceId()); break; case 'getfeature': $arrayFeatures = array($reqParams['typename']); $arrayOnlineresources = checkWfsPermission($query->getOwsproxyServiceId(), $arrayFeatures); $query->setOnlineResource($arrayOnlineresources['wfs_getfeature']); $request = $query->getRequest(); $request = stripslashes($request); getFeature($request); break; // case wfs transaction (because of raw POST the request param is empty) case '': $arrayFeatures = getWfsFeaturesFromTransaction($HTTP_RAW_POST_DATA); $arrayOnlineresources = checkWfsPermission($query->getOwsproxyServiceId(), $arrayFeatures); $query->setOnlineResource($arrayOnlineresources['wfs_transaction']); $request = $query->getRequest(); doTransaction($request, $HTTP_RAW_POST_DATA); break; default: } /*********************************************************/ function throwE($e){ global $reqParams, $imageformats; if(in_array($reqParams['format'],$imageformats)){ throwImage($e); } else{ throwText($e); } } function throwImage($e){ global $width,$height; $image = imagecreate($width,$height); $transparent = ImageColorAllocate($image,155,155,155); ImageFilledRectangle($image,0,0,$width,$height,$transparent); imagecolortransparent($image, $transparent); $text_color = ImageColorAllocate ($image, 233, 14, 91); for($i=0; $i 0){ $url .= "&"; } $url .= $mykeys[$i]."=".urlencode($reqParams[$mykeys[$i]]); } return $url; } /** * fetch and returns an image to client * * @param string the original url of the image to send */ function getImage($or){ global $reqParams; header("Content-Type: ".$reqParams['format']); echo getDocumentContent($or); } /** * fetchs and returns the content of the FeatureInfo Response * * @param string the url of the FeatureInfoRequest * @return string the content of the FeatureInfo document */ function getFeatureInfo($url){ global $info_format; //$e = new mb_notice("owsproxy: Try to fetch FeatureInfoRequest: ".$url); header("Content-Type: ".$info_format); $content = getDocumentContent($url); $content = matchUrls($content); echo $content; } /** * fetchs and returns the content of WFS GetFeature response * * @param string the url of the GetFeature request * @return echo the content of the GetFeature document */ function getFeature($url){ global $info_format; header("Content-Type: ".$info_format); $content = getDocumentContent($url); $content = matchUrls($content); echo $content; } /** * simulates a post request to host * * @param string host to send the request to * @param string port of host to send the request to * @param string method to send data (should be "POST") * @param string path on host * @param string data to send to host * @return string hosts response */ function sendToHost($host,$port,$method,$path,$data){ $buf = ''; if (empty($method)) $method = 'POST'; $method = mb_strtoupper($method); $fp = fsockopen($host, $port); fputs($fp, "$method $path HTTP/1.1\r\n"); fputs($fp, "Host: $host\r\n"); fputs($fp,"Content-type: application/xml\r\n"); fputs($fp, "Content-length: " . strlen($data) . "\r\n"); fputs($fp, "Connection: close\r\n\r\n"); if ($method == 'POST') fputs($fp, $data); while (!feof($fp)) $buf .= fgets($fp,4096); fclose($fp); return $buf; } /** * get wfs featurenames that are touched by a tansaction request defined in XML $data * * @param string XML that contains the tansaction request * @return array array of touched feature names */ function getWfsFeaturesFromTransaction($data){ $features = array(); $values = NULL; $tags = NULL; $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_parse_into_struct($parser,$data,$values,$tags); $code = xml_get_error_code ($parser); if ($code) { $line = xml_get_current_line_number($parser); $col = xml_get_current_column_number($parser); $mb_exception = new mb_exception("OWSPROXY invalid Tansaction XML: ".xml_error_string($code) . " in line " . $line. " at character ". $col); die(); } xml_parser_free($parser); $insert = false; $insertlevel = 0; foreach ($values as $element) { //features touched by insert if(strtoupper($element[tag]) == "WFS:INSERT" && $element[type] == "open"){ $insert = true; $insertlevel = $element[level]; } if($insert && $element[level] == $insertlevel + 1 && $element[type] == "open"){ array_push($features, $element[tag]); } if(strtoupper($element[tag]) == "WFS:INSERT" && $element[type] == "close"){ $insert = false; } //updated features if(strtoupper($element[tag]) == "WFS:UPDATE" && $element[type] == "open"){ array_push($features, $element[attributes]["typeName"]); } //deleted features if(strtoupper($element[tag]) == "WFS:DELETE" && $element[type] == "open"){ array_push($features, $element[attributes]["typeName"]); } } return $features; } /** * sends the data of WFS Transaction and echos the response * * @param string url to send the WFS Transaction to * @param string WFS Transaction data */ function doTransaction($url, $data){ $arURL = parse_url($url); $host = $arURL["host"]; $port = $arURL["port"]; if($port == '') $port = 80; $path = $arURL["path"]; $method = "POST"; $result = sendToHost($host,$port,$method,html_entity_decode($path),$data); //delete header from result $result = mb_eregi_replace("^[^<]*", "", $result); $result = mb_eregi_replace("[^>]*$", "", $result); echo $result; } function matchUrls($content){ if(!session_is_registered("owsproxyUrls")){ $_SESSION["owsproxyUrls"] = array(); $_SESSION["owsproxyUrls"]["id"] = array(); $_SESSION["owsproxyUrls"]["url"] = array(); } $pattern = "/[\"|\'](https*:\/\/[^\"|^\']*)[\"|\']/"; preg_match_all($pattern,$content,$matches); for($i=0; $igetGuisByPermission($_SESSION["mb_user_id"],true); $mywms = $n->getWmsByOwnGuis($myguis); $sql = "SELECT * FROM wms WHERE wms_owsproxy = $1"; $v = array($wms); $t = array("s"); $res = db_prep_query($sql, $v, $t); $service = array(); if($row = db_fetch_array($res)){ $service["wms_id"] = $row["wms_id"]; $service["wms_getcapabilities"] = $row["wms_getcapabilities"]; $service["wms_getmap"] = $row["wms_getmap"]; $service["wms_getfeatureinfo"] = $row["wms_getfeatureinfo"]; $service["wms_getcapabilities_doc"] = $row["wms_getcapabilities_doc"]; } if(!$row || count($mywms) == 0){ throwE(array("No wms data available.")); die(); } if(!in_array($service["wms_id"], $mywms)){ throwE(array("Permission denied."," -> ".$service["wms_id"], implode(",", $mywms))); die(); } return $service; } /** * validates the access permission by getting the appropriate wfs_conf * to each feature requested and check the wfs_conf permission * * @param string owsproxy md5 * @param array array of requested featuretype names * @return array array with detailed information on reqested wfs */ function checkWfsPermission($wfsOws, $features){ global $con, $n; $myconfs = $n->getWfsConfByPermission($_SESSION["mb_user_id"]); //check if we know the features requested if(count($features) == 0){ throwE(array("No wfs_feature data available.")); die(); } //get wfs $sql = "SELECT * FROM wfs WHERE wfs_owsproxy = $1"; $v = array($wfsOws); $t = array("s"); $res = db_prep_query($sql, $v, $t); $service = array(); if($row = db_fetch_array($res)){ $service["wfs_id"] = $row["wfs_id"]; $service["wfs_getcapabilities"] = $row["wfs_getcapabilities"]; $service["wfs_getfeature"] = $row["wfs_getfeature"]; $service["wfs_describefeaturetype"] = $row["wfs_describefeaturetype"]; $service["wfs_transaction"] = $row["wfs_transaction"]; $service["wfs_getcapabilities_doc"] = $row["wfs_getcapabilities_doc"]; } else{ throwE(array("No wfs data available.")); die(); } foreach($features as $feature){ //get appropriate wfs_conf $sql = "SELECT wfs_conf.wfs_conf_id FROM wfs_conf "; $sql.= "JOIN wfs_featuretype "; $sql.= "ON wfs_featuretype.featuretype_id = wfs_conf.fkey_featuretype_id "; $sql.= "WHERE wfs_featuretype.featuretype_name = $2 "; $sql.= "AND wfs_featuretype.fkey_wfs_id = $1"; $v = array($service["wfs_id"], $feature); $t = array("i","s"); $res = db_prep_query($sql, $v, $t); if(!($row = db_fetch_array($res))){ $notice = new mb_notice("Permissioncheck failed no wfs conf for wfs ".$service["wfs_id"]." with feturetype ".$feature); throwE(array("No wfs_conf data for featuretype ".$feature)); die(); } $conf_id = $row["wfs_conf_id"]; //check permission if(!in_array($conf_id, $myconfs)){ $notice = new mb_notice("Permissioncheck failed:".$conf_id." not in ".implode(",", $myconfs)); throwE(array("Permission denied."," -> ".$conf_id, implode(",", $myconfs))); die(); } } return $service; } function checkLayerPermission($wms_id,$l){ global $n, $owsproxyService; // $notice = new mb_notice("owsproxy: checkLayerpermission: wms: ".$wms_id.", layer: ".$l); $myl = split(",",$l); $r = array(); foreach($myl as $mysl){ if($n->getLayerPermission($wms_id, $mysl, $_SESSION["mb_user_id"]) === true){ array_push($r, $mysl); } } $ret = implode(",",$r); return $ret; } function getDocumentContent($url){ $d = new connector($url); return $d->file; } ?>