<?php 
if (isset($_REQUEST["filename"])) { 
	$dbname = $_REQUEST["filename"];
	if (end(explode('.',$dbname)) != "xml") die ("Invalid file extension");
}
else
	$dbname = "guestbook.xml";

$file = fopen($dbname, "w+") or die("Can't open XML file");

if (!isset($HTTP_RAW_POST_DATA))
	$HTTP_RAW_POST_DATA = file_get_contents('php://input');

$xmlString = $HTTP_RAW_POST_DATA;
if (!is_null($xmlString)) {
	if (fwrite($file, str_replace("</entry>", "</entry>\r\n", $xmlString)))
		print $xmlString;
	else
		print "Error writing to XML-file"; 
}
fclose($file);

<ja:if test=${guestbookNotification}>
$pos = strpos(strrev($xmlString), "yrtne<");
if (is_numeric($pos)) {
	$newpost = substr($xmlString, strlen($xmlString) - $pos);
	$newpost = str_replace(array("</guestbook>", "</entry>", "><myText>", "myName=", "</myText>" , "rating=", "date=", '"'), 
		array("", "", "\n$text.message:\n", "\n$text.name: ", "\n", "\n$text.rating: ", "\n$text.date: ", ""), $newpost);
	$gbpath = urlencode($_REQUEST["filename"]);
	$gbpath = str_replace(array("_%21_","_._",".xml","+"), array("/","/","<%=chameleon.pageExtension%>","%20"), $gbpath);
	$basedir = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
	$pos = strpos($basedir, "guestbook/");
	if (is_numeric($pos))
		$basedir = substr($basedir, 0, $pos);
	$fromaddr = $_SERVER['SERVER_NAME'];
	$pos = strpos($fromaddr, "www.");
	if ($pos === 0)
		$fromaddr = substr($fromaddr, 4);
	$fromaddr = "guestbook@" . $fromaddr;

	$eol = (strtoupper(substr(PHP_OS,0,3)) == 'WIN')? "\r\n" : "\n";
	$headers ="MIME-Version: 1.0" . $eol . 
		"Content-type: text/plain; charset=UTF-8" . $eol .
		"Content-Transfer-Encoding: 8bit" . $eol .
		"From:  " . $fromaddr . $eol .
		"X-Mailer: PHP" .  phpversion();
		mail("${guestbookNotificationEmail}", "$text.newMessage (" . $_SERVER['SERVER_NAME'] . ")", "$text.page: " . $basedir . $gbpath . $newpost, $headers);
}
</ja:if>
?>
