';
//$AddlJS = '';
include("header.php");
$error = "";
function uuid() {
return sprintf('%04x%04x-%04x-%03x4-%04x-%04x%04x%04x',
mt_rand(0, 65535), mt_rand(0, 65535), // 32 bits for "time_low"
mt_rand(0, 65535), // 16 bits for "time_mid"
mt_rand(0, 4095), // 12 bits before the 0100 of (version) 4 for "time_hi_and_version"
bindec(substr_replace(sprintf('%016b', mt_rand(0, 65535)), '01', 6, 2)),
mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535) // 48 bits for "node"
);
}
function get_can_id($s) {
$sa = split("_", $s);
return $sa[0];
}
function get_can_name($s) {
$sa = split("_", $s);
return $sa[1];
}
// captcha code
require_once('recaptchalib.php');
$privatekey = "6Lex1gAAAAAAANGUbIkt0iNQgALDey30obbidyqS";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
$captcha = ($resp->is_valid) ? true : false ;
// validation
if (! array_key_exists("c1", $_POST) ) $error = $error . "You must cast a vote for your first-choice candidate.
";
if (! array_key_exists("eligible", $_POST) ) $error = $error . "You must select one answer about your eligibility to vote.
";
if (! $captcha) $error = $error . "You didn't enter the correct words into the validation box.
";
if (strlen($error) > 0) {
// redisplay form to correct invalid input
include("vote2a.php");
}
else {
// create UUID
$voter_id = uuid();
// receive form input
$c1 = $_POST["c1"];
$c2 = ( array_key_exists("c2", $_POST) ) ? $_POST["c2"] : 0;
$c3 = ( array_key_exists("c3", $_POST) ) ? $_POST["c3"] : 0;
$c4 = ( array_key_exists("c4", $_POST) ) ? $_POST["c4"] : 0;
$c5 = ( array_key_exists("c5", $_POST) ) ? $_POST["c5"] : 0;
$ab = ( array_key_exists("ab", $_POST) ) ? $_POST["ab"] : 0;
$eligible = $_POST["eligible"];
$ballot = ( array_key_exists("ballot", $_POST) ) ? $_POST["ballot"] : "beats me";
// calculate credibility
$ip_addr = $_SERVER["REMOTE_ADDR"];
$link = mysql_connect($server,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT count(ip_addr) as long_total "
. "FROM ip_addrs "
. "WHERE ip_addr = '" . $ip_addr . "'";
if ($result = mysql_query($query) )
$long_total = mysql_result($result,0,"long_total");
else $long_total = mysql_error();
$query = "SELECT count(ip_addr) as short_total "
. "FROM ip_addrs "
. "WHERE ip_addr = '" . $ip_addr . "' "
. "and access_time > timestamp(now(), '-01:00:00') ";
$result = mysql_query($query);
$short_total = mysql_result($result,0,"short_total");
$credibility = min(100, max(0, (110 - ($short_total * 2) - ($long_total / 4)) ) );
// save to db
// ip address
$query = sprintf("INSERT INTO ip_addrs (ip_addr) VALUES ('%s')",
mysql_real_escape_string($ip_addr, $link) );
$result = mysql_query($query);
if ($result != 1) $error = $error . "Problem saving IP address. " .mysql_error() . "
";
// voter
$query = sprintf("INSERT INTO voters (id, eligible, credibility) VALUES ('%s', %s, %d)",
mysql_real_escape_string($voter_id, $link),
mysql_real_escape_string($eligible, $link),
mysql_real_escape_string($credibility, $link) );
$result = mysql_query($query);
if ($result != 1) $error = $error . "Problem saving voter. " . mysql_error() . "
";
// votes
$query = sprintf("INSERT INTO votes (voter_id, candidate_id, rank, credibility) VALUES ('%s', %d, 1, '%s')",
mysql_real_escape_string($voter_id, $link),
mysql_real_escape_string(get_can_id($c1), $link),
mysql_real_escape_string($credibility, $link) );
$result = mysql_query($query);
if ($result != 1) $error = $error . "Problem saving 1st choice vote. " . mysql_error() . "
";
if ($c2 != 0) {
$query = sprintf("INSERT INTO votes (voter_id, candidate_id, rank, credibility) VALUES ('%s', %d, 2, '%s')",
mysql_real_escape_string($voter_id, $link),
mysql_real_escape_string(get_can_id($c2), $link),
mysql_real_escape_string($credibility, $link) );
$result = mysql_query($query);
if ($result != 1) $error = $error . "Problem saving 1st choice vote. " . mysql_error() . "
";
}
if ($c3 != 0) {
$query = sprintf("INSERT INTO votes (voter_id, candidate_id, rank, credibility) VALUES ('%s', %d, 3, '%s')",
mysql_real_escape_string($voter_id, $link),
mysql_real_escape_string(get_can_id($c3), $link),
mysql_real_escape_string($credibility, $link) );
$result = mysql_query($query);
if ($result != 1) $error = $error . "Problem saving 1st choice vote. " . mysql_error() . "
";
}
if ($c4 != 0) {
$query = sprintf("INSERT INTO votes (voter_id, candidate_id, rank, credibility) VALUES ('%s', %d, 4, '%s')",
mysql_real_escape_string($voter_id, $link),
mysql_real_escape_string(get_can_id($c4), $link),
mysql_real_escape_string($credibility, $link) );
$result = mysql_query($query);
if ($result != 1) $error = $error . "Problem saving 1st choice vote. " . mysql_error() . "
";
}
if ($c5 != 0) {
$query = sprintf("INSERT INTO votes (voter_id, candidate_id, rank, credibility) VALUES ('%s', %d, 5, '%s')",
mysql_real_escape_string($voter_id, $link),
mysql_real_escape_string(get_can_id($c5), $link),
mysql_real_escape_string($credibility, $link) );
$result = mysql_query($query);
if ($result != 1) $error = $error . "Problem saving 1st choice vote. " . mysql_error() . "
";
}
if ($ab != 0) {
$query = sprintf("INSERT INTO votes (voter_id, candidate_id, rank, credibility) VALUES ('%s', %d, -1, '%s')",
mysql_real_escape_string($voter_id, $link),
mysql_real_escape_string(get_can_id($ab), $link),
mysql_real_escape_string($credibility, $link) );
$result = mysql_query($query);
if ($result != 1) $error = $error . "Problem saving 1st choice vote. " . mysql_error() . "
";
}
// votecount (get, calc new values, then set)
@mysql_query("BEGIN;");
$query = "SELECT total_votes + 1 as newtotal"
. " FROM votecount"
. " WHERE rowid = 1;";
$result=mysql_query($query);
$newtotal = mysql_result($result,0,"newtotal");
$query = "UPDATE votecount SET total_votes = " . $newtotal
. ", last_vote_cast_at = NOW()"
. " WHERE rowid = 1;";
$result=mysql_query($query);
if ($result != 1) $error = $error . "Problem updating vote count. " . mysql_error() . "
";
@mysql_query("COMMIT");
// if write-in, insert into candidates table
mysql_close();
if ($eligible) $eligible = "will";
else $eligible = "will NOT";
include("vote2b.php");
}
?>