<?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));
$transport = htmlentities(substr(urldecode(gpc("transport")),0,1024));
$response = htmlentities(substr(urldecode(gpc("response")),0,1024));
$error = htmlentities(substr(urldecode(gpc("error")),0,1024));
$callback = htmlentities(substr(urldecode(gpc("callback")),0,1024));
if ($rating == "")
$rating = 0;
if ($transport == "")
$transport = "downgrade";
if ($response == "")
$response = "none";
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");
if ($transport != "downgrade")
{
$message = "";
if ($response == "none")
{
header("HTTP/1.1 204 No Content\n\n");
exit;
}
else if ($response == "cookie")
{
$results = $rating . "a" . $average . "a" . $votes;
$filename = 'pixel.gif';
$fp = fopen($filename, 'rb');
header("Content-Type: image/gif");
header("Content-Length: " . filesize($filename));
setcookie("PollResults", $results, time()+3600, "/", "ajaxref.com");
fpassthru($fp);
exit;
}
else if ($response == "style")
{
$results = $rating . "a" . $average . "a" . $votes;
$results = '==' . urlencode($results) . '==';
header("Content-Type: text/css");
echo
"
#divforajax
{
background-image: url($results) ;
}
" ;
exit;
}
else if ($response == "xml")
{
header("Content-Type: text/xml");
echo
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n
<pollresults>\r\n
<rating>$rating</rating>\r\n
<average>$average</average>\r\n
<votes>$votes</votes>\r\n
</pollresults>\r\n
" ;
exit;
}
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 == "script" && $callback != "")
{
$message = "";
if ($transport != "script")
{
$message = "<script>";
if ($transport == "iframe")
$message .= "window.parent.";
}
$message .= "$callback('$rating','$votes','$average');";
if ($transport != "script")
$message .= "</script>";
else
{
print $message;
exit;
}
}
}
else
{
$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>.";
}
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;
}
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Voting Results</title>
</head>
<body>
<?php echo $message ?>
</body>
</html>