<?php
if (isset($_REQUEST["delay"]) && is_numeric($_REQUEST["delay"]))
sleep($_REQUEST["delay"]);
$theFile = "ratings.txt";
$totalsFile = "totals.txt";
$headers = getallheaders();
if (isset($headers["Content-Type"]) && !strstr($headers["Content-Type"], "application/x-www-form-urlencoded"))
{
$payloadString = $GLOBALS['HTTP_RAW_POST_DATA'];
$payloadArray = array();
if (strstr($headers["Content-Type"], "application/json"))
{
require_once('JSON.php');
$json = new Services_JSON();
$jsonObject = $json->decode($payloadString);
$payloadArray = (array)$jsonObject;
}
else if (strstr($headers["Content-Type"], "text/x-yaml"))
{
require_once('spyc.php');
$payloadArray = Spyc::YAMLLoad($payloadString);
}
else if (strstr($headers["Content-Type"], "text/plain"))
{
if (isset($headers["Content-Transfer-Encoding"]) && $headers["Content-Transfer-Encoding"] == "base64")
{
$payloadString = base64_decode($payloadString);
parse_str($payloadString, $payloadArray);
}
else
{
$tmpPayloadArray = explode(",", $payloadString);
for($i=0;$i<count($tmpPayloadArray);$i++)
{
$index = strpos($tmpPayloadArray[$i], "=");
$name = substr($tmpPayloadArray[$i], 0, $index);
$value = substr($tmpPayloadArray[$i], $index+1);
$payloadArray[$name] = $value;
}
}
}
else if (strstr($headers["Content-Type"], "text/xml"))
{
$doc = new DOMDocument();
$doc->loadXML($payloadString);
$children = $doc->documentElement->childNodes;
for($i=0;$i<$children->length;$i++)
{
$child = $children->item($i);
$payloadArray[$child->nodeName] = $child->nodeValue;
}
}
$rating = cleanVariable(arrayIndex($payloadArray,"rating"));
$comment = cleanVariable(arrayIndex($payloadArray,"comment"));
$response = strtolower(cleanVariable(arrayIndex($payloadArray,"response")));
$error = cleanVariable(arrayIndex($payloadArray,"error"));
$callback = cleanVariable(arrayIndex($payloadArray,"callback"));
$validdtd = cleanVariable(arrayIndex($payloadArray,"validdtd"));
}
else
{
$rating = cleanVariable(gpc("rating"));
$comment = cleanVariable(gpc("comment"));
$response = strtolower(cleanVariable(gpc("response")));
$error = cleanVariable(gpc("error"));
$callback = cleanVariable(gpc("callback"));
$validdtd = cleanVariable(gpc("validdtd"));
}
if ($rating == "")
$rating = 0;
$transport = "XHR";
if ($response == "")
$response = "html";
if ($error != "")
{
if ($error == "404")
header("HTTP/1.1 404 Not Found\n\n");
else
header("HTTP/1.1 500 Internal Server Error\n\n");
exit;
}
$userIP = $_SERVER['REMOTE_ADDR'];;
$currentTime = date("M d y h:i:s A");
$filehandle = fopen($theFile, "r");
if ($filehandle)
{
$data = fread($filehandle, filesize($theFile));
fclose($filehandle);
}
else
die('Failed to read file');
$filehandle = fopen($theFile, "w+");
if ($filehandle)
{
fwrite($filehandle,"$rating\t $transport\t $userIP @ $currentTime\t $comment\n");
fwrite($filehandle, $data);
fclose($filehandle);
}
else
die('Failed to write file');
$votes = $total = $average = 0;
$filehandle = fopen($totalsFile, "r+");
if ($filehandle)
{
$line = fgets($filehandle, 4096);
$tokens = explode("\t", $line);
if (count($tokens) > 1)
{
$votes = $tokens[0] + 1;
$total = $tokens[1] + $rating;
}
fclose($filehandle);
}
else
die('Failed to read file');
$filehandle = fopen($totalsFile, "w+");
if ($filehandle)
{
fwrite($filehandle,"$votes\t$total\n");
fclose($filehandle);
}
else
die('Failed to write file');
if ($votes != 0) $average = round(($total/$votes), 2);
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header("Ajax-Response-Type: $response");
$message = "";
if ($response == "html")
{
header("Content-Type: text/html");
$message = "Thank you for voting. You rated this a <strong>$rating</strong>. There are <strong>$votes</strong> total votes. The average is <strong>$average</strong>. You can see the ratings in the <a href='https://ajaxref.com/ch4/ratings.txt' target='_blank'>ratings file</a>";
}
else if ($response == "text")
{
header("Content-Type: text/plain");
$message = "Thank you for voting. You rated this a $rating. There are $votes total votes. The average is $average.";
}
else if ($response == "csv")
{
header("Content-Type: text/plain");
$message = "$rating,$average,$votes";
}
else if ($response == "base64 encoded")
{
header("Content-Type: text/plain");
header("Content-Transfer-Encoding: base64");
$msg = "Thank you for voting. You rated this a <strong>$rating</strong>. There are <strong>$votes</strong> total votes. The average is <strong>$average</strong>. You can see the ratings in the <a href='https://ajaxref.com/ch4/ratings.txt' target='_blank'>ratings file</a>";
$message = base64_encode($msg);
}
else if ($response == "xml")
{
header("Content-Type: text/xml");
$message = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<!DOCTYPE pollresults [
<!ELEMENT pollresults (rating,average,votes)>
<!ELEMENT rating (#PCDATA)>
<!ELEMENT average (#PCDATA)>
<!ELEMENT votes (#PCDATA)>
<!ATTLIST rating id ID #IMPLIED>
<!ATTLIST average id ID #IMPLIED>
<!ATTLIST votes id ID #IMPLIED>
]>
<pollresults>
<rating id=\"rating\">$rating</rating>
<average id=\"average\">$average</average>
<votes id=\"votes\"";
if ($validdtd == "false")
$message .= " name=\"votes\"";
$message .= ">$votes</votes>
</pollresults>
" ;
}
else if ($response == "xmldom")
{
header("Content-Type: text/xml");
$xml = new DOMDocument('1.0', 'UTF-8');
$root = $xml->createElement("pollresults");
$ratingNode = $xml->createElement("rating");
$ratingNode->appendChild($xml->createTextNode($rating));
$ratingNode->setAttribute("id", "rating");
$root->appendChild($ratingNode);
$averageNode = $xml->createElement("average");
$averageNode->appendChild($xml->createTextNode($average));
$averageNode->setAttribute("id", "average");
$root->appendChild($averageNode);
$votesNode = $xml->createElement("votes");
$votesNode->appendChild($xml->createTextNode($votes));
$votesNode->setAttribute("id", "votes");
$root->appendChild($votesNode);
$xml->appendChild($root);
$message = $xml->saveXML();
}
else if ($response == "xmlbad")
{
header("Content-Type: text/xml");
$message =
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n
<pollresults>\r\n
<rating>$rating</rating>\r\n
<average>$average\r\n
<votes>$votes</votes>\r\n
</pollresults>\r\n
" ;
}
else if ($response == "xhtml hack")
{
header("Content-Type: application/xhtml+xml");
$message =
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">
<div xmlns=\"http://www.w3.org/1999/xhtml\">
<rating id=\"rating\">$rating</rating>
<average id=\"average\">$average</average>
<votes id=\"votes\">$votes</votes>
</div>" ;
}
else if ($response == "xhtml")
{
header("Content-Type: text/xml");
$message =
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head><title>Testing</title></head>
<body>
<div id=\"rating\">$rating</div>
<div id=\"average\">$average</div>
<div id=\"votes\">$votes</div>
</body>
</html>
" ;
}
else if ($response == "json")
{
header("Content-Type: application/json");
require_once('JSON.php');
$json = new Services_JSON();
$jsonResponse = new ResponseData();
$jsonResponse->rating = $rating;
$jsonResponse->votes = $votes;
$jsonResponse->average = $average;
$message = $json->encode($jsonResponse);
}
else if ($response == "yaml")
{
header("Content-Type: text/x-yaml");
require_once('spyc.php');
$yamlResponse = new ResponseData();
$yamlResponse->rating = $rating;
$yamlResponse->votes = $votes;
$yamlResponse->average = $average;
$message = Spyc::YAMLDump($yamlResponse);
}
else if ($response == "javascript")
{
header("Content-Type: application/x-javascript");
$message = "
var responseOutput = document.getElementById(\"responseOutput\");
responseOutput.innerHTML += 'Thank you for voting. You rated this a <strong>$rating</strong>. There are <strong>$votes</strong> total votes. The average is <strong>$average</strong>. You can see the ratings in the <a href=\"https://ajaxref.com/ch4/ratings.txt\" target=\"_blank\">ratings file</a>';
";
}
echo $message;
function gpc($name)
{
if (isset($_GET[$name]))
return $_GET[$name];
else if (isset($_POST[$name]))
return $_POST[$name];
else if (isset($_COOKIE[$name]))
return $_COOKIE[$name];
else
return "";
}
function arrayIndex($array, $index)
{
if (isset($array[$index]))
return $array[$index];
else
return "";
}
function cleanVariable($originalVariable)
{
return htmlentities(substr(urldecode($originalVariable),0,1024));
}
class ResponseData
{
public $average = 0;
public $rating = 0;
public $votes = 0;
public $total = 0;
}
?>