<?php 
$files = glob('xml/*.xml');

function getRequestParam($name, $default) {
	if (isset($_REQUEST[$name])) {
		if (get_magic_quotes_gpc()) {
    		return stripslashes($_REQUEST[$name]);
		} else {
			return $_REQUEST[$name];
		}
	} else {
		return $default;
	}
}


function transform($data) {
//	$data = str_replace("\n\n", "\n", $data);
//	$data = preg_replace("@(</?[^>]*>|\? >)@", "$1\n", $data);
	$data = preg_replace('/<(\/?[^ >]*)([^>]*)>/', '<span style="font-weight: bold;">&lt;</span><span style="color: blue;">$1</span><span style="color: red;">$2</span><span style="font-weight: bold;">&gt;</span>', $data);
	
	return $data;
}

function SOAP_request($data) {

	$params = array(
		'http' => array(
			'method' => 'post',
			'content' => $data
		)
	);
	
	$ctx = stream_context_create($params);
	$fp = @fopen("http://demo.ait.co.at/thesaurus/thesaurus", 'rb', false, $ctx);
	if (!$fp) {
		return("Problem with SOAP Request\n $php_errormsg");
	}
	$response = @stream_get_contents($fp);
	if ($response === false) {
		return("Problem reading data\n $php_errormsg");
	}
	return $response;
} 

$file = getRequestParam('file', '');
$content = "";
if (in_array($file, $files)) {
	$content = file_get_contents($file);
} else {
	$content = getRequestParam('content', ''); 	
}
error_reporting(0);
ini_set('track_errors', 1);
ini_set('html_errors', 0);
	 

if (!empty($content)) {
	
	$loginData =  SOAP_request('<?xml version="1.0" encoding="UTF-8" ?>
		<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:thes="http://www.aitbiz.com/ns/Thesauri">
		   <soapenv:Header/>
		   <soapenv:Body>
		      <thes:login>
		         <username>test05</username>
		         <password>test05</password>
			 <timeout>300</timeout>
		      </thes:login>
		   </soapenv:Body>
		</soapenv:Envelope>
		');
	preg_match('/<session_ticket>(.*)<\/session_ticket>/', $loginData, $matches);
	$session = $matches[1]; 
	
	$content = str_replace('$$SESSION$$', $session, $content);
	$result = SOAP_request($content);
	$dom = new DomDocument();
	$dom->formatOutput = true;
	
	$dom->loadXML($result);
	if ($dom->documentElement == null) {
		// error
	} else {
		$result = $dom->saveXML();
	}
} else {
	$result = "";	
}

$asxml = getRequestParam('asxml', '');

if ($asxml) {
	header("Content-type: text/xml; charset=utf-8");
	echo $result;
	return;
}

header("Content-type: text/html; charset=utf-8");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"  dir="ltr" >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Thesaurus WSDL Tests</title>
<style type="text/css" >
	h1 {
		font-size: 1.5em;
	}
	h2 {
		font-size: 1.3em;
	}
	html {
		color: #000;
		background: #FFF;
	}
	p {
		padding-left: 1em;
	}
	* {
		font-family: arial, helvetica, clean, sans-serif;
	}
</style>
</head>
<body>
	<h1>ThesaurusClient</h1>
		<form method="post" action="index.php" style="position: relative; width: 1005px; height: 530px;">
		
		<textarea name="content" style="overflow: auto; white-space: nowrap; position: absolute; top: 0px; left: 0px; width:500px; height: 500px; font-family: Courier New, monospace; border: 1px solid #000000;"><?php echo htmlentities($content, null, "utf-8"); ?></textarea>
		<pre style="padding: 0px; margin: 0px; position: absolute; top: 0px; right: 0px; height: 500px; width:500px; overflow: auto; font-family: Courier New, monospace; border: 1px solid #000000;"><?php echo transform($result)?></pre>
		
		<div style="position: absolute; bottom: 0px; left: 0px; height: 25px;">
			<input onclick="this.form.target='_self';" type="submit" name="submit" value="Submit" />
			<button onclick="window.open('Thesauri.wsdl'); return false;">Download the WSDL</button>
			<button onclick="window.open('AIT%20Vocabulary%20Service%20Description%20V1.pdf'); return false;">Download the Service Description</button>
			<button onclick="window.open('Thesauri_v10_schema.pdf'); return false;">Download the XSD</button>
			<button onclick="window.open('Thesauri_v10.dtd'); return false;">Download the DTD</button>
			<input onclick="this.form.target='_blank';" type="submit" name="asxml" value="show as xml" />
		</div>
		</form>
	<h1>Samples</h1>	
		This is a list of sample queries
<?php 
	sort($files);
	
	foreach($files as $file) {
		echo "<h2><a href=\"index.php?file=$file\">$file</a></h2>";
		echo "<p>";
		$infoPath = str_replace('.xml', '.txt', $file);
		if (file_exists($infoPath)) {
			echo file_get_contents($infoPath);
		} else {
			echo "No description available";
		}
		echo "</p>";
	}
	?>
</body>
</html>