<?php
$enddate = mktime(22, 0, 0, 6, 23, 2016);
$poll_open = time() < $enddate;
$voted = false;
if(!$poll_open) {
$msg = 'Poll is already closed, sorry!';
}
$con = new mysqli('', '', '', '');
if ($con->connect_errno) {
$msg = 'Failed to connect to MySQL: (' . $con->connect_errno . ') ' . $con->connect_error;
}
$con->set_charset('utf8');
function insertVote($poll_open) {
global $con, $msg, $enddate;
$token = isset($_POST['accesstoken']) ? $_POST['accesstoken'] : '';
$vote = isset($_POST['vote']) ? $_POST['vote'] : '';
$agreement = isset($_POST['agreement']) ? $_POST['agreement'] : '';
$time = isset($_POST['time']) ? $_POST['time'] : '';
// check that poll is open
if($poll_open = false) {
$msg = 'Poll is already closed, sorry!';
return false;
}
// also check user's time
if(strtotime($time) >= $enddate) {
$msg = 'Poll is already closed, sorry!';
return false;
}
// check that it is not empty
if(empty($token) || empty($vote) || empty($time)) {
$msg = 'You have to fill out the complete poll and JavaScript needs to be activated!';
return false;
}
// check that vote is a valid outcome
if($vote != 'remain' && $vote != 'leave') {
$msg = 'Do not tamper with the form!';
return false;
}
// then check for agreement
if(empty($agreement) || $agreement != 'yes') {
$msg = 'You have to agree to your vote!';
return false;
}
// check if access token is fine
$stmt = $con->prepare("SELECT token FROM valid_tokens WHERE token = ?");
if(!$stmt->bind_param("s", $token)) {
$msg = 'This is not a valid access token!';
return false;
}
if (!$stmt->execute()) {
$msg = "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
return false;
}
$stmt->bind_result($roken);
// check if row exists
if($stmt->fetch()) {
$stmt->close();
// ok, this token may vote :)
// let's insert the vote into the database
// but first check that there is no such vote in the database
$stmt = $con->prepare("SELECT token FROM votes WHERE token LIKE ?");
if(!$stmt->bind_param("s", $token)) {
$msg = 'This is not a valid access token!';
return false;
}
if (!$stmt->execute()) {
$msg = "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
return false;
}
$stmt->bind_result($roken);
// check if row exists
if($stmt->fetch()) {
$msg = 'You probably already have voted... Sorry! You can only vote once!';
return false;
}
// row didn't exist, so we can insert your vote!
$stmt = $con->prepare("INSERT INTO votes(token, vote) VALUES (?, ?)");
if (!$stmt->bind_param("ss", $token, $vote)) {
$msg = "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
return false;
}
if (!$stmt->execute()) {
$msg = 'You probably already have voted... Sorry! You can only vote once!';
return false;
}
return true;
} else {
$stmt->close();
$msg = 'This is not a valid access token!';
return false;
}
}
// insert voting result
if(isset($_POST['time'])) {
$voted = insertVote($poll_open);
}
// get poll result
// we also have a filter functionality, since the first character of the access token depends on the local authority of the voter
// ?filter=a will show the result of voting district a
$filter = isset($_GET['filter']) ? $_GET['filter'] : '';
// only allow alphanumeric character as filter
if(!ctype_alnum($filter)) {
$filter = '';
}
$filterv = $filter . '%'; // then add our wildcard
// all
$res = $con->query("SELECT COUNT(*) AS 'all' FROM votes WHERE token LIKE '" . $filterv . "'");
$row = $res->fetch_assoc();
$all = floatval($row['all']);
// remain
$res = $con->query("SELECT COUNT(*) AS 'remain' FROM votes WHERE vote LIKE 'remain' AND token LIKE '" . $filterv . "'");
$row = $res->fetch_assoc();
$remain = floatval($row['remain']);
// leave
$res = $con->query("SELECT COUNT(*) AS 'leave' FROM votes WHERE vote LIKE 'leave' AND token LIKE '" . $filterv . "'");
$row = $res->fetch_assoc();
$leave = floatval($row['leave']);
$remainp = ($remain * 100.) / $all;
$leavep = ($leave * 100.) / $all;
$con->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<title>Vote For It - Deciding Our Future</title>
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/voteforit.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.0.0.min.js" integrity="sha256-JmvOoLtYsmqlsWxa7mDSLMwa6dZ9rrIdtrrVYRnDRH0=" crossorigin="anonymous"></script>
<script type="text/javascript">
<!--
function getDateTime() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth()+1;
var day = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
if(month.toString().length == 1) {
month = '0'+month;
}
if(day.toString().length == 1) {
day = '0'+day;
}
if(hour.toString().length == 1) {
hour = '0'+hour;
}
if(minute.toString().length == 1) {
minute = '0'+minute;
}
if(second.toString().length == 1) {
second = '0'+second;
}
var dateTime = year+'/'+month+'/'+day+' '+hour+':'+minute+':'+second;
return dateTime;
}
$(document).ready(function() {
// also send client's timestamp to prevent cheating
$('#poll').submit(function() {
$('#time').val(getDateTime());
return true;
});
});
-->
</script>
</head>
<body>
<div class="container">
<div class="page-header">
<h1>Vote For It - Deciding Our Future</h1>
</div>
<p>This is the secure, online poll system of (AU)stralia. We are <a href="source.html">open source</a>!</p>
<p>In order to vote, you need your anonymous voting token. The vote will close on June 23, 10PM.</p>
<hr>
<div class="result">
<h3>Current Result<?php if($filter != '') echo ' for District ' . $filter . ' (<a href=".">reset</a>)'; ?></h3>
<?php
if($all <= 0) {
echo '<p>No such results.</p>';
}
?>
<ul class="summary">
<li class="segment remain" style="width: <?php echo $remainp; ?>%;"><span><?php if($remainp > 0): ?>Remain: <?php echo round($remainp, 2); ?>%<?php endif; ?></span></li>
<li class="segment leave" style="width: <?php echo $leavep; ?>%;"><span><?php if($leavep > 0): ?>Leave: <?php echo round($leavep, 2); ?>%<?php endif; ?></span></li>
<div class="mid">
50%
</div>
</ul>
</div>
<div class="vote">
<?php
if(isset($msg) && !empty($msg)):
?>
<div class="alert alert-danger" role="alert"><?php echo $msg; ?></div>
<?php
endif;
?>
<?php
if($voted):
?>
<div class="alert alert-success" role="alert">Thank you for your vote! Every vote is counting!</div>
<?php
endif;
?>
<p class="lead">Today's vote is on whether to exit or remain in the AU.</p>
<p>If the majority votes for exit, our country's name will be changed to "stralia".</p>
<form id="poll" action="." method="post">
<div class="form-group">
<label for="accesstoken">Voting Token</label>
<input type="text" class="form-control" id="accesstoken" name="accesstoken" placeholder="Voting Token"<?php if(!$poll_open) { echo ' disabled'; } ?>>
</div>
<div class="radio<?php if(!$poll_open) { echo ' disabled'; } ?>">
<label>
<input type="radio" name="vote" id="voteRemain" value="remain" checked<?php if(!$poll_open) { echo ' disabled'; } ?>>
Remain in the AU and keep our country's name (AU)stralia.
</label>
</div>
<div class="radio<?php if(!$poll_open) { echo ' disabled'; } ?>">
<label>
<input type="radio" name="vote" id="voteLeave" value="leave"<?php if(!$poll_open) { echo ' disabled'; } ?>>
Leave the AU and change our name to stralia.
</label>
</div>
<div class="checkbox<?php if(!$poll_open) { echo ' disabled'; } ?>">
<label>
<input type="checkbox" name="agreement" value="yes"<?php if(!$poll_open) { echo ' disabled'; } ?>> I totally agree to my vote!
</label>
</div>
<input type="hidden" id="time" name="time" value="">
<button type="submit" class="btn btn-primary"<?php if(!$poll_open) { echo ' disabled'; } ?>>Submit Vote</button>
</form>
</div>
<hr class="separator">
<p>See individual results for districts:</p>
<p>
<?php
$districts = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
foreach ($districts as $key => $district) {
if($key > 0) {
echo ' | ';
}
echo '<a href="?filter=' . $district . '">' . $district . '</a>';
}
?>
</p>
<!-- Site footer -->
<footer class="footer">
<p>© 2016 (AU)stralian Online Poll Company</p>
</footer>
</div> <!-- /container -->
</body>
</html>