2005-10-24 17:59:15 +00:00
|
|
|
<?php
|
|
|
|
|
2005-10-24 18:25:43 +00:00
|
|
|
$has_config = FALSE;
|
|
|
|
|
|
|
|
$mysql_host = "";
|
|
|
|
$mysql_user = "";
|
|
|
|
$mysql_password = "";
|
|
|
|
$mysql_db = "";
|
2005-10-24 17:59:15 +00:00
|
|
|
|
|
|
|
$elections_table = "elections";
|
2005-10-24 18:25:43 +00:00
|
|
|
$choices_table = "election_choices";
|
2005-10-24 17:59:15 +00:00
|
|
|
$anon_tokens_table = "election_anon_tokens";
|
2005-10-24 18:25:43 +00:00
|
|
|
$tmp_tokens_table = "election_tmp_tokens";
|
2005-10-24 17:59:15 +00:00
|
|
|
$votes_table = "election_votes";
|
2005-10-24 18:25:43 +00:00
|
|
|
$members_table = "foundationmembers";
|
|
|
|
|
|
|
|
if (is_readable ("include/localconfig.php")) {
|
|
|
|
/* You can use such a file to have a local config for testing purpose. */
|
|
|
|
include ("include/localconfig.php");
|
|
|
|
$has_config = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_readable ("/home/admin/secret/anonvoting")) {
|
|
|
|
include ("/home/admin/secret/anonvoting");
|
|
|
|
$has_config = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$has_config) {
|
|
|
|
trigger_error ("No configuration found.");
|
|
|
|
}
|
2005-10-24 17:59:15 +00:00
|
|
|
|
|
|
|
function elec_sql_open () {
|
|
|
|
global $mysql_host;
|
|
|
|
global $mysql_user;
|
|
|
|
global $mysql_password;
|
|
|
|
global $mysql_db;
|
|
|
|
|
2005-10-24 18:25:43 +00:00
|
|
|
$handle = mysql_connect ("$mysql_host", "$mysql_user", "$mysql_password");
|
|
|
|
if (!$handle) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-10-24 17:59:15 +00:00
|
|
|
|
2005-10-25 11:18:49 +00:00
|
|
|
$select_base = mysql_select_db ($mysql_db, $handle);
|
2005-10-24 18:25:43 +00:00
|
|
|
if (!$select_base) {
|
|
|
|
elec_sql_close ($handle);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-10-24 17:59:15 +00:00
|
|
|
|
2005-10-24 18:25:43 +00:00
|
|
|
return $handle;
|
2005-10-24 17:59:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function elec_sql_close ($handle) {
|
|
|
|
if ($handle === FALSE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mysql_close ($handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
function elec_sql_start_transaction ($handle) {
|
|
|
|
if ($handle === FALSE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
$result = mysql_query ("START TRANSACTION", $handle);
|
|
|
|
|
|
|
|
return ($result !== FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
function elec_sql_commit ($handle) {
|
|
|
|
if ($handle === FALSE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
$result = mysql_query ("COMMIT", $handle);
|
|
|
|
|
|
|
|
return ($result !== FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
function elec_sql_rollback ($handle) {
|
|
|
|
if ($handle === FALSE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
$result = mysql_query ("ROLLBACK", $handle);
|
|
|
|
|
|
|
|
return ($result !== FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
function elec_get_by_date_desc_with_where ($handle, $where = "") {
|
|
|
|
global $elections_table;
|
|
|
|
|
|
|
|
if ($handle === FALSE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
$query = "SELECT * FROM " . $elections_table;
|
|
|
|
if (isset ($where) && $where != "") {
|
|
|
|
$query .= " " . $where;
|
|
|
|
}
|
|
|
|
$query .= " ORDER BY voting_start desc";
|
|
|
|
|
|
|
|
$result = mysql_query ($query, $handle);
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
$retval = FALSE;
|
|
|
|
} else {
|
|
|
|
$result_array = array ();
|
|
|
|
while ($buffer = mysql_fetch_assoc ($result)) {
|
|
|
|
$result_array[] = $buffer;
|
|
|
|
}
|
|
|
|
$retval = $result_array;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
function elec_get_current_by_date_desc ($handle) {
|
|
|
|
$gmdate = gmdate ("Y-m-d H:i:s");
|
|
|
|
$where = "WHERE '".$gmdate."' >= voting_start AND '".$gmdate."' <= voting_end";
|
|
|
|
return elec_get_by_date_desc_with_where ($handle, $where);
|
|
|
|
}
|
|
|
|
|
|
|
|
function elec_get_previous_by_date_desc ($handle) {
|
|
|
|
$gmdate = gmdate ("Y-m-d H:i:s");
|
|
|
|
$where = "WHERE '".$gmdate."' > voting_start AND '".$gmdate."' > voting_end";
|
|
|
|
return elec_get_by_date_desc_with_where ($handle, $where);
|
|
|
|
}
|
|
|
|
|
2005-10-24 18:25:43 +00:00
|
|
|
function elec_verify_email_tmp_token ($handle, $election_id, $email, $tmp_token) {
|
|
|
|
global $tmp_tokens_table;
|
2005-10-24 17:59:15 +00:00
|
|
|
global $members_table;
|
|
|
|
|
|
|
|
if ($handle === FALSE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
$escaped_election_id = mysql_real_escape_string ($election_id, $handle);
|
|
|
|
$escaped_email = mysql_real_escape_string ($email, $handle);
|
2005-10-24 18:25:43 +00:00
|
|
|
$escaped_tmp_token = mysql_real_escape_string ($tmp_token, $handle);
|
2005-10-24 17:59:15 +00:00
|
|
|
|
2005-10-24 18:25:43 +00:00
|
|
|
$query = "SELECT COUNT(*) FROM " . $tmp_tokens_table . " AS tt, " . $members_table . " AS mt";
|
2005-10-24 17:59:15 +00:00
|
|
|
$query .= " WHERE tt.election_id = '".$escaped_election_id."'";
|
2005-10-24 18:25:43 +00:00
|
|
|
$query .= " AND tt.tmp_token = '".$escaped_tmp_token."'";
|
2005-10-24 17:59:15 +00:00
|
|
|
$query .= " AND tt.member_id = mt.id";
|
|
|
|
$query .= " AND mt.email = '".$escaped_email."'";
|
|
|
|
|
|
|
|
$result = mysql_query ($query, $handle);
|
|
|
|
if (!$result)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return (mysql_result ($result, 0, 0) == 1);
|
|
|
|
}
|
|
|
|
|
2005-10-24 18:25:43 +00:00
|
|
|
function elec_choices_get ($handle, $election_id) {
|
|
|
|
global $choices_table;
|
2005-10-24 17:59:15 +00:00
|
|
|
|
|
|
|
if ($handle === FALSE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
$escaped_election_id = mysql_real_escape_string ($election_id, $handle);
|
|
|
|
|
2005-10-24 18:25:43 +00:00
|
|
|
$query = "SELECT * FROM " . $choices_table;
|
2005-10-24 17:59:15 +00:00
|
|
|
$query .= " WHERE election_id = '".$escaped_election_id."'";
|
|
|
|
|
|
|
|
$result = mysql_query ($query, $handle);
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
$retval = FALSE;
|
|
|
|
} else {
|
|
|
|
$result_array = array ();
|
|
|
|
while ($buffer = mysql_fetch_assoc ($result)) {
|
|
|
|
$result_array[] = $buffer;
|
|
|
|
}
|
|
|
|
$retval = $result_array;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $retval;
|
|
|
|
}
|
|
|
|
|
2005-10-24 18:25:43 +00:00
|
|
|
function elec_verify_elections ($choices_nb, $choices) {
|
|
|
|
if ($choices_nb === FALSE || $choices === FALSE)
|
2005-10-24 17:59:15 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2005-10-24 18:25:43 +00:00
|
|
|
if ($choices_nb < 1)
|
2005-10-24 17:59:15 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2005-10-24 18:25:43 +00:00
|
|
|
if (count ($choices) < $choices_nb || count ($choices) <= 1)
|
2005-10-24 17:59:15 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
function elec_get_election ($handle, $election_id) {
|
|
|
|
global $elections_table;
|
|
|
|
|
|
|
|
if ($handle === FALSE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
$escaped_election_id = mysql_real_escape_string ($election_id, $handle);
|
|
|
|
|
|
|
|
$query = "SELECT * FROM " . $elections_table;
|
|
|
|
$query .= " WHERE id = '".$escaped_election_id."'";
|
|
|
|
|
|
|
|
$result = mysql_query ($query, $handle);
|
|
|
|
|
|
|
|
if (!$result)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return mysql_fetch_assoc ($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
function elec_election_is_current ($election) {
|
|
|
|
$gmdate = gmdate ("Y-m-d H:i:s");
|
|
|
|
return ($gmdate >= $election["voting_start"] && $gmdate <= $election["voting_end"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function elec_election_has_ended ($election) {
|
|
|
|
$gmdate = gmdate ("Y-m-d H:i:s");
|
|
|
|
return ($gmdate > $election["voting_start"] && $gmdate > $election["voting_end"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function elec_election_get_type ($election) {
|
2005-10-25 12:43:11 +00:00
|
|
|
if ($election["type"] == "referendum")
|
2005-10-24 17:59:15 +00:00
|
|
|
return "referendum";
|
|
|
|
else
|
|
|
|
return "election";
|
|
|
|
}
|
|
|
|
|
2005-10-24 18:25:43 +00:00
|
|
|
function elec_vote_get_votes_from_post ($choices) {
|
2005-10-24 17:59:15 +00:00
|
|
|
$votes_array = array ();
|
|
|
|
|
2005-10-24 18:25:43 +00:00
|
|
|
foreach ($choices as $choice) {
|
|
|
|
$name = "vote".$choice["id"];
|
2005-10-24 17:59:15 +00:00
|
|
|
if (isset ($_POST[$name]) && $_POST[$name] != "") {
|
2005-10-24 18:25:43 +00:00
|
|
|
array_push ($votes_array, $choice["id"]);
|
2005-10-24 17:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $votes_array;
|
|
|
|
}
|
|
|
|
|
2005-10-24 18:25:43 +00:00
|
|
|
function elec_verify_vote_is_valid ($choices_nb, $choices, $vote, $votes_array) {
|
|
|
|
if ($choices_nb == 1)
|
2005-10-24 17:59:15 +00:00
|
|
|
return "";
|
|
|
|
|
2005-10-24 18:25:43 +00:00
|
|
|
if (count ($votes_array) > $choices_nb) {
|
|
|
|
return "you chose ".count ($votes_array)." answers, while you can't choose more than ".$choices_nb." answers.";
|
2005-10-24 17:59:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
function elec_insert_new_anon_token ($handle, $election_id, $anon_token) {
|
|
|
|
global $anon_tokens_table;
|
|
|
|
|
|
|
|
if ($handle === FALSE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
$escaped_election_id = mysql_real_escape_string ($election_id, $handle);
|
|
|
|
$escaped_anon_token = mysql_real_escape_string ($anon_token, $handle);
|
|
|
|
|
|
|
|
$query = "SELECT COUNT(*) FROM " . $anon_tokens_table;
|
|
|
|
$query .= " WHERE anon_token = '".$escaped_anon_token."'";
|
|
|
|
|
|
|
|
$result = mysql_query ($query, $handle);
|
|
|
|
if (!$result)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (mysql_result ($result, 0, 0) != 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
$query = "INSERT INTO " . $anon_tokens_table . " (anon_token, election_id)";
|
|
|
|
$query .= " VALUES ('".$escaped_anon_token."', '".$escaped_election_id."')";
|
|
|
|
|
|
|
|
$result = mysql_query ($query, $handle);
|
|
|
|
if (!$result)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return mysql_insert_id ($handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
function elec_insert_new_vote ($handle, $anon_token_id, $vote) {
|
|
|
|
global $votes_table;
|
|
|
|
|
|
|
|
if ($handle === FALSE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
$escaped_vote = mysql_real_escape_string ($vote, $handle);
|
|
|
|
$escaped_anon_token_id = mysql_real_escape_string ($anon_token_id, $handle);
|
|
|
|
|
2005-10-24 18:25:43 +00:00
|
|
|
$query = "INSERT INTO " . $votes_table . " (choice_id, anon_id)";
|
2005-10-24 17:59:15 +00:00
|
|
|
$query .= " VALUES ('".$escaped_vote."', '".$escaped_anon_token_id."')";
|
|
|
|
|
|
|
|
$result = mysql_query ($query, $handle);
|
|
|
|
if (!$result)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2005-10-24 18:25:43 +00:00
|
|
|
function elec_sql_remove_tmp_token ($handle, $election_id, $email, $tmp_token) {
|
2005-10-24 17:59:15 +00:00
|
|
|
global $members_table;
|
2005-10-24 18:25:43 +00:00
|
|
|
global $tmp_tokens_table;
|
2005-10-24 17:59:15 +00:00
|
|
|
|
|
|
|
if ($handle === FALSE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
$escaped_election_id = mysql_real_escape_string ($election_id, $handle);
|
|
|
|
$escaped_email = mysql_real_escape_string ($email, $handle);
|
2005-10-24 18:25:43 +00:00
|
|
|
$escaped_tmp_token = mysql_real_escape_string ($tmp_token, $handle);
|
2005-10-24 17:59:15 +00:00
|
|
|
|
2005-10-24 18:25:43 +00:00
|
|
|
$query = "DELETE FROM " . $tmp_tokens_table;
|
|
|
|
$query .= " USING ". $tmp_tokens_table . " AS tt, " . $members_table . " AS mt";
|
2005-10-24 17:59:15 +00:00
|
|
|
$query .= " WHERE tt.election_id = '".$escaped_election_id."'";
|
2005-10-24 18:25:43 +00:00
|
|
|
$query .= " AND tt.tmp_token = '".$escaped_tmp_token."'";
|
2005-10-24 17:59:15 +00:00
|
|
|
$query .= " AND tt.member_id = mt.id";
|
|
|
|
$query .= " AND mt.email = '".$escaped_email."'";
|
|
|
|
|
|
|
|
$result = mysql_query ($query, $handle);
|
|
|
|
if (!$result)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
function elec_get_anon_tokens_for_election ($handle, $election_id) {
|
|
|
|
global $anon_tokens_table;
|
|
|
|
|
|
|
|
if ($handle === FALSE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
$escaped_election_id = mysql_real_escape_string ($election_id, $handle);
|
|
|
|
|
|
|
|
$query = "SELECT * FROM " . $anon_tokens_table;
|
|
|
|
$query .= " WHERE election_id = '".$escaped_election_id."'";
|
|
|
|
$query .= " ORDER BY anon_token";
|
|
|
|
|
|
|
|
$result = mysql_query ($query, $handle);
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
$retval = FALSE;
|
|
|
|
} else {
|
|
|
|
$result_array = array ();
|
|
|
|
while ($buffer = mysql_fetch_assoc ($result)) {
|
|
|
|
$result_array[] = $buffer;
|
|
|
|
}
|
|
|
|
$retval = $result_array;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
function elec_get_results_election ($handle, $election_id) {
|
|
|
|
global $anon_tokens_table;
|
|
|
|
global $votes_table;
|
|
|
|
|
|
|
|
if ($handle === FALSE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
$escaped_election_id = mysql_real_escape_string ($election_id, $handle);
|
|
|
|
|
2005-10-24 18:25:43 +00:00
|
|
|
$query = "SELECT choice_id, COUNT(choice_id) AS total_choice FROM " . $anon_tokens_table . " AS att, " . $votes_table . " AS vt";
|
2005-10-24 17:59:15 +00:00
|
|
|
$query .= " WHERE att.election_id = '".$escaped_election_id."'";
|
|
|
|
$query .= " AND att.id = vt.anon_id";
|
2005-10-24 18:25:43 +00:00
|
|
|
$query .= " GROUP BY choice_id";
|
|
|
|
$query .= " ORDER BY total_choice DESC";
|
2005-10-24 17:59:15 +00:00
|
|
|
|
|
|
|
$result = mysql_query ($query, $handle);
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
$retval = FALSE;
|
|
|
|
} else {
|
|
|
|
$result_array = array ();
|
|
|
|
while ($buffer = mysql_fetch_assoc ($result)) {
|
|
|
|
$result_array[] = $buffer;
|
|
|
|
}
|
|
|
|
$retval = $result_array;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
function elec_get_votes_for_anon_token ($handle, $anon_token_id) {
|
|
|
|
global $votes_table;
|
|
|
|
|
|
|
|
if ($handle === FALSE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
$escaped_anon_token_id = mysql_real_escape_string ($anon_token_id, $handle);
|
|
|
|
|
2005-10-24 18:25:43 +00:00
|
|
|
$query = "SELECT choice_id FROM " . $votes_table;
|
2005-10-24 17:59:15 +00:00
|
|
|
$query .= " WHERE anon_id = '".$escaped_anon_token_id."'";
|
2005-10-24 18:25:43 +00:00
|
|
|
$query .= " ORDER BY choice_id";
|
2005-10-24 17:59:15 +00:00
|
|
|
|
|
|
|
$result = mysql_query ($query, $handle);
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
$retval = FALSE;
|
|
|
|
} else {
|
|
|
|
$result_array = array ();
|
|
|
|
while ($buffer = mysql_fetch_assoc ($result)) {
|
2005-10-24 18:25:43 +00:00
|
|
|
$result_array[] = $buffer["choice_id"];
|
2005-10-24 17:59:15 +00:00
|
|
|
}
|
|
|
|
$retval = $result_array;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|