<?php
if (isset($_REQUEST["delay"]) && is_numeric($_REQUEST["delay"]))
sleep($_REQUEST["delay"]);
$theFile = "ratings.txt";
$totalsFile = "totals.txt";
$rating = htmlentities(substr(urldecode(gpc("rating")),0,1024));
$comment = htmlentities(substr(urldecode(gpc("comment")),0,1024));
$response = strtolower(htmlentities(substr(urldecode(gpc("response")),0,1024)));
$error = htmlentities(substr(urldecode(gpc("error")),0,1024));
$callback = htmlentities(substr(urldecode(gpc("callback")),0,1024));
$validdtd = htmlentities(substr(urldecode(gpc("validdtd")),0,1024));
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")
{
$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/ch3/ratings.txt' target='_blank'>ratings file</a>";
}
else if ($response == "text")
{
$message = "Thank you for voting. You rated this a $rating. There are $votes total votes. The average is $average.";
}
else if ($response == "csv")
{
$message = "$rating,$average,$votes";
}
else if ($response == "encoded")
{
$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/ch3/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 SYSTEM \"ratings.dtd\">
<pollresults>
<rating>$rating</rating>
<average>$average</average>
<votes id=\"votes\"";
if ($validdtd == "false")
$message .= " name=\"votes\"";
$message .= ">$votes</votes>
</pollresults>
" ;
}
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 == "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 == "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/ch3/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 "";
}
class ResponseData
{
public $average = 0;
public $rating = 0;
public $votes = 0;
public $total = 0;
}
?>