<?php 
if (isset($_REQUEST["filename"])) { 
	$dbname = $_REQUEST["filename"];
	if (end(explode('.',$dbname)) != "xml") die ("Invalid file extension");
	$dbname = stripslashes($dbname); 
}
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)) {
	$findp = array("</guestbook>", "</entry>", "><myText>", "myName=", "</myText>" , "rating=", "date=", '"');
	$replp =  array("", "", "\n$text.message:\n", "\n$text.name: ", "\n", "\n$text.rating: ", "\n$text.date: ", "");
	$newpost = substr($xmlString, strlen($xmlString) - $pos);
	$gbpath = str_replace(".xml", "", $dbname);
	$gbpath = str_replace("_", "/", $gbpath);
	$newpost = str_replace($findp, $replp, $newpost);
	$mailtxt = "$text.page: http://" . $_SERVER['SERVER_NAME'] . "/" . $gbpath . ".html" . $newpost;

	$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: guestbook@" . $_SERVER['SERVER_NAME'] . $eol .
		"X-Mailer: PHP" .  phpversion();
		mail("${guestbookNotificationEmail}", "$text.newMessage (" . $_SERVER['SERVER_NAME'] . ")", $mailtxt, $headers);
}
</ja:if>
?>
