function validateInspireMetadataFromData($iso19139Xml)
{
    $validatorUrl = 'http://www.inspire-geoportal.eu/INSPIREValidatorService/resources/validation/inspire';
    #$validatorUrl2 = 'http://localhost/mapbender/x_geoportal/log_requests.php'; //for debugging purposes
    //send inspire xml to validator and push the result to requesting user
    $validatorInterfaceObject = new connector();
    $validatorInterfaceObject->set('httpType', 'POST');
    $validatorInterfaceObject->set('httpContentType', 'multipart/form-data');
    # maybe given automatically
    //first test with data from ram - doesn't function :-(
    $fields = array('dataFile' => urlencode($iso19139Xml));
    //generate file identifier:
    $fileId = guid();
    //generate temporary file under tmp
    if ($h = fopen(TMPDIR . "/" . $fileId . "iso19139_validate_tmp.xml", "w")) {
        if (!fwrite($h, $iso19139Xml)) {
            $e = new mb_exception("geoportal/mod_readOpenSearchResultsDetail.php: cannot write to file: " . TMPDIR . "iso19139_validate_tmp.xml");
        }
        fclose($h);
    }
    //send file as post like described under http://www.tecbrat.com/?itemid=13&catid=1
    $fields['dataFile'] = '@' . TMPDIR . '/' . $fileId . 'iso19139_validate_tmp.xml';
    $postData = $fields;
    $validatorInterfaceObject->set('httpPostFieldsNumber', count($postData));
    $validatorInterfaceObject->set('curlSendCustomHeaders', false);
    $validatorInterfaceObject->set('httpPostData', $postData);
    #give an array
    $validatorInterfaceObject->load($validatorUrl);
    header("Content-type: text/html; charset=UTF-8");
    echo $validatorInterfaceObject->file;
    //delete file in tmp
    //TODO - this normally done by a cronjob
    die;
}
     $spatial_res_value = $data->spatial_res_value;
 }
 if (isset($data->inspire_charset)) {
     $inspire_charset = $data->inspire_charset;
 }
 if (isset($data->update_frequency)) {
     $update_frequency = $data->update_frequency;
 }
 $randomid = new Uuid();
 //Check if origin is external and export2csw is activated!
 if ($origin == 'external' && $export2csw == 't') {
     //harvest link from location, parse the content for datasetid and push xml into data column
     //load metadata from link TODO: function from class_wms - generate a class for metadata management and include it here and in class_wms
     $metadataConnector = new connector();
     $metadataConnector->set("timeOut", "5");
     $metaData = $metadataConnector->load($link);
     //$e = new mb_exception($metaData);
     if (!$metaData) {
         abort(_mb("Could not load metadata from source url!"));
     }
     //***
     //write metadata to temporary file:
     $randomFileId = new Uuid();
     $tmpMetadataFile = fopen(TMPDIR . '/link_metadata_file_' . $randomFileId . '.xml', 'w');
     fwrite($tmpMetadataFile, $metaData);
     fclose($tmpMetadataFile);
     $e = new mb_exception("File which has been written: link_metadata_file_" . $randomFileId . ".xml");
     //read out objects from xml structure
     if (file_exists(TMPDIR . '/link_metadata_file_' . $randomFileId . '.xml')) {
         $iso19139Xml = simplexml_load_file(TMPDIR . '/link_metadata_file_' . $randomFileId . '.xml');
         //$metaData = file_get_contents(TMPDIR.'/link_metadata_file_'.$randomFileId.'.xml');
# 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.
require_once dirname(__FILE__) . "/../php/mb_validateSession.php";
require_once dirname(__FILE__) . "/../classes/class_connector.php";
require_once dirname(__FILE__) . "/../classes/class_kml_ows.php";
//FIXME: what about projections?
//header("Content-Type: text/x-json");
if (isset($_REQUEST["url"])) {
    $url = $_REQUEST["url"];
} else {
    echo "{}";
    exit;
}
$kml = new KML();
$con = new connector();
$kmlString = $con->load($url);
try {
    if ($kml->parseKml($kmlString)) {
        $geojson = $kml->toGeoJSON();
        echo $geojson;
    } else {
        echo "{}";
    }
} catch (Exception $e) {
    echo $e;
    die;
}
 protected final function post($url, $postData)
 {
     $connection = new connector();
     $connection->set("httpType", "post");
     $connection->set("httpContentType", "xml");
     $connection->set("httpPostData", $postData);
     $e = new mb_notice("OWS REQUEST: " . $url . "\n\n" . $postData);
     $data = $connection->load($url);
     if (!$data) {
         $e = new mb_exception("OWS request returned no result: " . $url . "\n" . $postData);
         return null;
     }
     return $data;
 }
function getrecords_post($url, $postData)
{
    $connection = new connector();
    $connection->set("httpType", "post");
    $connection->set("httpContentType", "text/xml");
    $connection->set("httpPostData", $postData);
    $e = new mb_notice("CAT REQUEST: " . $url . "\n\n" . $postData);
    $data = $connection->load($url);
    if (!$data) {
        $e = new mb_exception("CAT getrecords returned no result: " . $url . "\n" . $postData);
        return null;
    }
    return $data;
}
 /**
  * Create cswrecord object from GetRecords XML response
  * @return unknown_type
  * @param $url URL of getrecords
  * @param $xml Post, SOAP XML
  * @todo handle XML for POST,SOAP
  */
 public function createCSWRecordFromXML($url, $xml = null)
 {
     //create connector
     $data = null;
     //@todo handle post,soap
     if ($xml != null) {
         $connection = new connector();
         $connection->set("httpType", "post");
         $connection->set("httpContentType", "text/xml");
         $connection->set("httpPostData", $xml);
         $data = $connection->load($url);
         //$e = new mb_exception("class_cswrecord:url:".$url);
         $e = new mb_exception("class_cswrecord:xml:" . $xml);
         //$e = new mb_exception("class_cswrecord:data:".$data);
         $e = new mb_exception("class_cswrecord.php: responded data: " . $data);
     } else {
         $x = new connector($url);
         //$e = new mb_exception("class_cswrecord.php: requested url: ".$url);
         $data = $x->file;
         $e = new mb_exception("class_cswrecord.php: responded data: " . $data);
     }
     if (!$data) {
         $this->getrecords_status = false;
         $e = new mb_exception("CAT getrecords returned no result: " . $url . "\n" . $postData);
         return false;
     } else {
         $this->getrecords_status = true;
     }
     //check if returned string has an exeption defined
     $testException = strpos($data, "ows:Exception");
     if ($testException === false) {
     } else {
         $this->getrecords_status = true;
         $this->getrecords_exception = true;
         $this->getrecords_exception_text = urlencode($data);
         $e = new mb_exception("CAT getrecords returned an ows:exception!");
         return false;
     }
     //arrays to hold xml struct values and index
     $value_array = null;
     $index_array = null;
     //operational vars
     $op_type = null;
     //get-capabilities, getrecords ...
     $op_sub_type = null;
     //get,post,....
     $op_constraint = false;
     //Store XML response
     //@todo cache this
     $this->getRecordsDoc = $data;
     $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, $data, $value_array, $index_array);
     //echo "values:".print_r($value_array);
     //echo "index:".print_r($vindex_array);
     $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);
     foreach ($value_array as $element) {
         //Version 2.0.2
         //@todo: handle other profiles
         if ((mb_strtoupper($element[tag]) == "CSW:SEARCHRESULTS" or mb_strtoupper($element[tag]) == "SEARCHRESULTS") && $element[type] == "open") {
             $this->elementSet = $element[attributes][elementSet];
             $this->numberOfRecordsMatched = $element[attributes][numberOfRecordsMatched];
         }
         if ((mb_strtoupper($element[tag]) == "CSW:SUMMARYRECORD" or mb_strtoupper($element[tag]) == "SUMMARYRECORD") && $element[type] == "open") {
             //Create SummaryRecords Object
             $summaryObj = new cswSummaryRecord();
         }
         //SummaryRecord elements
         //ID
         if (mb_strtoupper($element[tag]) == "DC:IDENTIFIER" or mb_strtoupper($element[tag]) == "IDENTIFIER") {
             $summaryObj->identifier = $element[value];
         }
         //Title
         if (mb_strtoupper($element[tag]) == "DC:TITLE" or mb_strtoupper($element[tag]) == "TITLE") {
             $summaryObj->title = $element[value];
         }
         //@todo handle multiple subject elements
         //Subject
         if (mb_strtoupper($element[tag]) == "DC:SUBJECT" or mb_strtoupper($element[tag]) == "SUBJECT") {
             $summaryObj->subject = $element[value];
         }
         //Abstract
         if (mb_strtoupper($element[tag]) == "DC:ABSTRACT" or mb_strtoupper($element[tag]) == "ABSTRACT" or mb_strtoupper($element[tag]) == "DCT:ABSTRACT") {
             $summaryObj->abstract = $element[value];
         }
         //Modified
         if (mb_strtoupper($element[tag]) == "DC:MODIFIED" or mb_strtoupper($element[tag]) == "MODIFIED") {
             $summaryObj->modified = $element[value];
         }
         //Type
         if (mb_strtoupper($element[tag]) == "DC:TYPE" or mb_strtoupper($element[tag]) == "TYPE") {
             $summaryObj->type = $element[value];
         }
         //Format
         if (mb_strtoupper($element[tag]) == "DC:FORMAT" or mb_strtoupper($element[tag]) == "FORMAT") {
             $summaryObj->format = $element[value];
         }
         if ((mb_strtoupper($element[tag]) == "CSW:SUMMARYRECORD" or mb_strtoupper($element[tag]) == "SUMMARYRECORD") && $element[type] == "close") {
             //{ush SummaryRecords Object to Array
             array_push($this->SummaryRecordsArray, $summaryObj);
         }
     }
     //Success/Failure
     if ($this->numberOfRecordsMatched == 0) {
         $this->getrecords_status = false;
         $e = new mb_exception("There are no records that match your criteria");
         return false;
     } else {
         $this->getrecords_status = true;
         $e = new mb_notice("GetRecords Results Returned");
         return true;
     }
 }
function validateInspireMetadata($iso19139Doc, $recordId)
{
    $validatorUrl = 'http://www.inspire-geoportal.eu/INSPIREValidatorService/resources/validation/inspire';
    #$validatorUrl2 = 'http://localhost/mapbender/x_geoportal/log_requests.php';
    //send inspire xml to validator and push the result to requesting user
    $validatorInterfaceObject = new connector();
    $validatorInterfaceObject->set('httpType', 'POST');
    #$validatorInterfaceObject->set('httpContentType','application/xml');
    $validatorInterfaceObject->set('httpContentType', 'multipart/form-data');
    # maybe given automatically
    $xml = fillISO19139($iso19139Doc, $recordId);
    //first test with data from ram - doesn't function
    $fields = array('dataFile' => urlencode($xml));
    //generate file identifier:
    $fileId = guid();
    //generate temporary file under tmp
    if ($h = fopen(TMPDIR . "/" . $fileId . "iso19139_validate_tmp.xml", "w")) {
        if (!fwrite($h, $xml)) {
            $e = new mb_exception("mod_layerISOMetadata: cannot write to file: " . TMPDIR . "iso19139_validate_tmp.xml");
        }
        fclose($h);
    }
    //send file as post like described under http://www.tecbrat.com/?itemid=13&catid=1
    $fields['dataFile'] = '@' . TMPDIR . '/' . $fileId . 'iso19139_validate_tmp.xml';
    #if we give a string with parameters
    #foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    #rtrim($fields_string,'&');
    #$postData = $fields_string;
    $postData = $fields;
    #$e = new mb_exception("mod_layerISOMetadata: postData: ".$postData['dataFile']);
    //number of post fields:
    //curl_setopt($ch,CURLOPT_POST,count($fields));
    $validatorInterfaceObject->set('httpPostFieldsNumber', count($postData));
    $validatorInterfaceObject->set('curlSendCustomHeaders', false);
    //$validatorInterfaceObject->set('httpPostData', $postData);
    $validatorInterfaceObject->set('httpPostData', $postData);
    #give an array
    $validatorInterfaceObject->load($validatorUrl);
    header("Content-type: text/html; charset=UTF-8");
    echo $validatorInterfaceObject->file;
    //delete file in tmp
    //TODO - this normally done by a cronjob
    die;
}
 /**
  * Retrieves a document from a URL, presumably a 
  * describe feature type document, via POST
  * 
  * @return String
  * @param $url String
  */
 protected final function post($url, $postData)
 {
     $x = new connector();
     $x->set("httpType", "post");
     $x->set("httpPostData", $postData);
     $x->set("httpContentType", "XML");
     $xml = $x->load($url);
     if (!$xml) {
         throw new Exception("Unable to open document: " . $url);
         return null;
     }
     return $xml;
 }