Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,34 @@ function allow(ip) {
var params = encodeURI('allowed=' + ip);
xmlhttp.send(params);
}

function camelize(str) {
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(letter, index) {
return index == 0 ? letter.toLowerCase() : letter.toUpperCase();
}).replace(/\s+/g, '');
}

var admin_verified = false;

function verify() {
if (admin_verified) {
admin_verified = false;
document.getElementById('result_btn').setAttribute('disabled','');
document.getElementById('result_btn').innerHTML = 'View Results';
return true;
}
var str = Math.random().toString(36).substring(8) + ' ' + Math.random().toString(36).substring(5);
var verify = camelize(str);
var person = prompt('Please confirm that you are human, by typing the following string in camel case', str);
if (person != null) {
if (person === verify) {
admin_verified=true;
document.getElementById('result_btn').removeAttribute('disabled','');
document.getElementById('result_btn').innerHTML = 'Click to see final result !!';
}
}
return false;
};
</script>";

$htmlOutput .= "<form action='allow.php' method='post' onsubmit='return false'><input type='hidden' name='allowed' value='true'>";
Expand Down Expand Up @@ -88,6 +116,6 @@ function allow(ip) {

$htmlOutput .= "<h1>OR</h1>";

$htmlOutput .= "<a class='btn' href='results.php'>View Results</a>";
$htmlOutput .= "<a id='result_btn' class='btn' onclick='return verify()' href='results.php' target='_blank' disabled >View Results</a>";

include("../template.php");
12 changes: 12 additions & 0 deletions check.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
require_once("config.php");
session_start();

$htmlOutput = '';
if ( allowed() ) {
print("true");
}
else{
print("false");
}
?>
15 changes: 11 additions & 4 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
define('DB_PASS', 'cre12321');
define('DB_NAME', 'CRE');

$base_url = 'http://localhost/CRElection/';
$host = $_SERVER['HTTP_HOST'];
$host_upper = strtoupper($host);
$path = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');

if(strpos($path, "admin")){
$path = substr($path, 0, strpos($path, "/admin"));
}
$base_url = "http://".$host.$path."/";

// Update the name of the election
$electionName = 'CR Election';
$electionName = 'FC Elections 2016';

$multiple_votes = false;
$max_votes = 1;
$multiple_votes = true;
$max_votes = 3;

$negative_votes = false;
$max_n_votes = 1;
Expand Down
149 changes: 77 additions & 72 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
<?php
//CR Election portal
require_once("config.php");
session_start();
function get_candidates() {
global $DB;
$query = mysqli_prepare($DB, "SELECT id, name FROM `candidates` ORDER BY name");
mysqli_stmt_execute($query);
mysqli_stmt_bind_result($query, $id, $name);
mysqli_stmt_store_result($query);
$results = array();
while(mysqli_stmt_fetch($query)){
$results[$id] = $name;
}
return $results;
}
//CR Election portal
require_once("config.php");
session_start();
function get_candidates()
{
global $DB;
$query = mysqli_prepare($DB, "SELECT id, name FROM `candidates` ORDER BY name");
mysqli_stmt_execute($query);
mysqli_stmt_bind_result($query, $id, $name);
mysqli_stmt_store_result($query);
$results = array();
while (mysqli_stmt_fetch($query)) {
$results[$id] = $name;
}
return $results;
}

$htmlOutput = '';
$htmlOutput = '';

if ( allowed() ) {
if ( (empty($multiple_votes) || $max_votes <= 1) ) {
// Check and account for multiple votes
$input_type = 'radio';
$input_req = 'required';
}
else {
$htmlOutput .= "
if (allowed()) {
if ((empty($multiple_votes) || $max_votes <= 1)) {
// Check and account for multiple votes
$input_type = 'radio';
$input_req = 'required';
} else {
$htmlOutput .= "
<script>
function checkVotes(form) {
var allowed = ".$max_votes.";
var n_allowed = ".$max_n_votes.";
var allowed = " . $max_votes . ";
var n_allowed = " . $max_n_votes . ";
var inputs = form.getElementsByTagName('input');
var voted = 0, n_voted = 0;
for ( i=0; i<inputs.length; i++ ) {
Expand All @@ -50,51 +50,56 @@ function checkVotes(form) {
}
</script>
";
$input_type = 'checkbox';
$input_req = '';
}
if ( $negative_votes ) {
// Check and account for negative votes
if ( $max_n_votes <= 1) {
$n_input_type = 'radio';
}
else {
$n_input_type = 'checkbox';
}
}
$htmlOutput .= "<form action='vote.php' method='post' class='vote' onsubmit='return checkVotes(this)'>";
$candidates = get_candidates();
if ( count($candidates) ) {
if ( $negative_votes ) {
$htmlOutput .= '<table><!--tr><th>positive</th><th>negative</th><th>candidate</th></tr-->';
}
foreach ( $candidates as $id => $name ){
if ( $negative_votes ) {
$htmlOutput .= "<tr><td class='pos'><input type='$input_type' name='candidate_id[]' value='$id' $input_req></td>
$input_type = 'checkbox';
$input_req = '';
}
if ($negative_votes) {
// Check and account for negative votes
if ($max_n_votes <= 1) {
$n_input_type = 'radio';
} else {
$n_input_type = 'checkbox';
}
}
$htmlOutput .= "<form action='vote.php' method='post' class='vote' onsubmit='return checkVotes(this)'>";
$candidates = get_candidates();
if (count($candidates)) {
if ($negative_votes) {
$htmlOutput .= '<table><!--tr><th>positive</th><th>negative</th><th>candidate</th></tr-->';
}
foreach ($candidates as $id => $name) {
if ($negative_votes) {
$htmlOutput .= "<tr><td class='pos'><input type='$input_type' name='candidate_id[]' value='$id' $input_req></td>
<td class='neg'><input type='$n_input_type' name='n_candidate_id[]' value='$id'></td>
<td class='name'>$name</td></tr>";
}
else {
$htmlOutput .= "<label><input type='$input_type' name='candidate_id[]' value='$id' $input_req>$name</label><br>";
}
}
$htmlOutput .= '</table>';
$htmlOutput .= "<input class='btn' type='submit' value='Vote'></form>";
}
else {
$htmlOutput .= "No Candidates in the list.";
}
}
else {
if ( isset( $_SESSION["done_voting"] ) && $_SESSION["done_voting"] ) {
$htmlOutput .= "Your response has been recorded.<br><a class='btn' href=''>Refresh</a>";
$htmlOutput .= "<script>document.body.onload=function(){setTimeout(function(){window.location=''}, 3000)}</script>";
unset($_SESSION["done_voting"]);
}
else {
$htmlOutput .= "<strong>Access denied.</strong> Please ask the administrator to allow you to vote. <br><a class='btn' href=''>Reload</a>";
$htmlOutput .= "<script>document.body.onload=function(){setTimeout(function(){window.location=''}, 1000)}</script>";
}
}

include('template.php');
} else {
$htmlOutput .= "<label><input type='$input_type' name='candidate_id[]' value='$id' $input_req>$name</label><br>";
}
}
$htmlOutput .= '</table>';
$htmlOutput .= "<input class='btn' type='submit' value='Vote'></form>";
} else {
$htmlOutput .= "No Candidates in the list.";
}
} else {
$htmlOutput .= "
<script>
document.body.onload=function(){
setInterval(function(){
$.get(\"check.php\", function(data, status){
console.log(data);
if(data==\"true\"){
console.log(\"reload\");
window.location.reload();
}
});
}, 1000);
}</script>";
if (isset($_SESSION["done_voting"]) && $_SESSION["done_voting"]) {
$htmlOutput .= "Your response has been recorded.<br><a class='btn' href=''>Refresh</a>";
unset($_SESSION["done_voting"]);
} else {
$htmlOutput .= "<strong>Access denied.</strong> Please ask the administrator to allow you to vote. <br><a class='btn' href=''>Reload</a>";
}
}
include('template.php');
Loading