<?php
include_once('xml.php');

function logToFile($msg)
{ 


$url = "http://localhost/geonetwork/srv/en/portal.sru?operation=explain&version=1.1";


$schema = "schemas/srw-types.xsd";
libxml_use_internal_errors(true);

$errorxml = true;
$errorschema = true;


$xml = file_get_contents($url);


if (!$xml) {
	header("HTTP/1.0 500 Server Error");
	logToFile("could not retrieve file". $url);
	return;	
}

$doc = new DOMDocument('1.0', 'utf-8');
$doc->loadXML($xml);

$errors = libxml_get_errors();
if (empty($errors))
{
	$errorxml = false; 
  
  // do the validating now
  
  if (! $doc->schemaValidate($schema) ) {
		
		$errors = libxml_get_errors();
		$message = "";
		foreach ($errors as $error) {
			$message .= libxml_display_error($error);
		}
		libxml_clear_errors();
		
  } else {
	$errorschema=false;
  }
}
else {
	
	foreach ($errors as $error ) {
	
		$lines = explode("r", $xml);
		$line = $lines[($error->line)-1];
		$message .= $error->message.' at line '.$error->line.':<br />'.htmlentities($line);
	}
}

if ($errorxml) {
	header("HTTP/1.0 415 Unsupported Media Type");
	print $message;
}
elseif ($errorschema) {
	header("HTTP/1.0 409 Conflict");
	print $message;
}
else {
	header("HTTP/1.0 200 OK");
	print "Validation ok";
}


?>
