These functions, invoked at the start of main, set up a database environment and establish a database connection.
<?
function setOracleEnvironment() {
// Ammend these values as necessary
putenv("ORACLE_HOME=/packages/oracle8/u01/app/oracle/product/8.1.6");
putenv("ORACLE_SID=csci8");
putenv("TWO_TASK=csci8");
}
function connect() {
$user = "HSimpson";
$password = "Duh";
$db = Ora_Logon( $user, $password );
if (!$db) die( "The Oracle refuses to admit you" );
// switch on auto-commit mode
Ora_CommitOn($db);
return $db;
}
This code performs a rather weak throttle check on scripted voting. If the vote comes from the same IP address as that last recorded, then after a suitable delay a response is generated indicating that the user should wait before again voting.
If it appears to be a new voter, the script continues after updating the record of the IP address last used.
function checkvoter($db, $addr) {
$cursor = Ora_Open($db);
if(!$cursor)
die( "The Oracle refuses to speak to you" );
$sql_string = "select * from USERADDR where ID=1";
$try = Ora_Parse($cursor, $sql_string);
if(!$try)
die("Your programmer stuffed up the composition of the sql query + $sql_string");
$result = Ora_Exec($cursor);
$last = "";
if( Ora_Fetch( $cursor) ) {
$last = ora_getColumn($cursor, 0);
}
Ora_close($cursor);
if($last == $addr) {
print <<<NOVOTE
<html><head><title>Script voter?</title></head>
<body bgcolor=red>
<p>No more votes from you for a little while. Thank you.</p>
</body></html>
NOVOTE;
sleep(6);
exit;
}
$cursor = Ora_Open($db);
if(!$cursor)
die( "The Oracle refuses to speak to you" );
$sql_string = "update USERADDR set ADDR='$addr' where ID=1";
$try = Ora_Parse($cursor, $sql_string);
if(!$try)
die("Your programmer stuffed up the composition of the sql query + $sql_string");
$result = Ora_Exec($cursor);
if (!$result)
die( "Error when running query $sqlstring " );
}
function update($person, $db) {
$cursor = Ora_Open($db);
if(!$cursor)
die( "The Oracle refuses to speak to you" );
$sql_string = "update BigBrother set Votes=Votes+1 where Name='$person'";
$try = Ora_Parse($cursor, $sql_string);
if(!$try)
die("Your programmer stuffed up the composition of the sql query + $sql_string");
$result = Ora_Exec($cursor);
if (!$result)
die( "Error when running query $sqlstring " );
Ora_close($cursor);
}
This function returns an associative array, with person identifier as subscript and votes as value. It is a simple "select * from table" style query.
function getdata($db) {
$cursor = Ora_Open($db);
if(!$cursor)
die( "The Oracle refuses to speak to you" );
$sqlstring= "select * from BigBrother";
$try = Ora_Parse($cursor, $sqlstring);
if(!$try)
die("Your programmer stuffed up the composition of the sql query");
$result = Ora_Exec($cursor);
if (!$result)
die( "Error when running query $sqlstring " );
$data = array();
while( Ora_Fetch( $cursor) ) {
$Name = ora_getColumn($cursor, 0);
$Votes = ora_getColumn($cursor, 1);
$data[$Name] = $Votes;
}
Ora_close($cursor);
return $data;
}
This function generates an image showing a histogram of the votes cast so far. The image is written to a temporary file from where it is fetched by an <img> tag in the generated response HTML page.
// Data that supplies names for the final histogram
// Values must match form, many are ommitted here
$names = array (
"Person1" => "his/her name and title",
"Person2" => "his/her name and title",
"Person23" => "his/her name and title"
);
function makeImage($data)
{
global $names;
$num = count($data);
$total = 0;
$max = 0;
foreach($data as $key=>$value) {
$total += $value;
$max = ($value > $max) ? $value : $max;
}
print "<h1 align=center>Total votes cast $total</h1>";
$imagewidth = 800;
$imageheight = 20*$num;
$w = (integer) $imagewidth * 0.9;
$h = (integer) $imageheight * 0.9;
$tempfile = "/tmp/afile" . $total . ".png";
print "<p align=center><img src=\"$tempfile\" width=$w height=$h border=2>";
$image = ImageCreate ($imagewidth, $imageheight);
$white = ImageColorAllocate ($image, 255, 255, 255);
$text_color = ImageColorAllocate ($image, 233, 0 , 233);
$fill_color = ImageColorAllocate($image, 255, 50, 50);
$black = ImageColorAllocate($image,0,0,0);
ImageLine($image, 0, 0, 0, $imageheight, $black);
ImageLine($image, 0, $imageheight-2, $imagewdith-1, $imageheight-2, $black);
ImageLine($image, 0, 0, $imagewidth, 0, $black);
ImageLine($image, $imagewidth-1, 0, $imagewidth-1, $imageheight, $black);
$x1 = 5; $x2 = 200;
$y = 18;
if($max<50) $max = 50;
foreach($data as $key=>$value) {
$fullname = $names[$key];
ImageString ($image, 2, $x1, $y-10, $fullname, $text_color);
if($value==0) {
ImageString($image, 2, $x2, $y-10, "Who?", $text_color);
}
else {
$scaled = ($value*600)/$max;
$scaled = (integer) $scaled;
imagefilledrectangle($image, $x2, $y-12, $x2+$scaled, $y+2, $fill_color);
}
ImageLine($image, 0, $y+4, $imagewidth, $y+4, $black);
$y+=20;
}
imagepng($image,$tempfile);
imagedestroy($image);
}
This performs initialization, establishes a database connection and then does a simple check for scripted voting.
setOracleEnvironment(); $db = connect(); $addr = $HTTP_SERVER_VARS["REMOTE_ADDR"]; checkvoter($db, $addr);
print <<<WELCOME <html><head><title>Another vote</title></head> <body> <h1 align=center>Vote him/her out</h1> <p>Every vote counts! Vote early! Vote often! Get your friends to vote! WELCOME;
This code determines which input element was used and invokes the update function
to submit a vote and then the getdata function to obtain the records with
the total votes for each candidate.
foreach($HTTP_POST_VARS as $key=>$val) {
if(preg_match("/_[xy]$/", $key)) { $temp = explode("_", $key); $identifier = $temp[0]; }
else $identifier = $key;
}
if(isset($identifier)) update($identifier, $db);
$voterecord = getdata($db);
Ora_Logoff($db);
makeImage($voterecord); print "<br><br><h1 align=center>Your vote may make the difference</h1>"; print "<p align=center><em>Vote again soon!</em>" ?> </body></html>