forked from nihavi/CRElection
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config-sample.php
120 lines (99 loc) · 2.96 KB
/
config-sample.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
ini_set('display_errors','On');
error_reporting(E_ALL);
define('DB_HOST', 'localhost');
define('DB_USER', 'CRE');
define('DB_PASS', 'cre12321');
define('DB_NAME', 'CRE');
$base_url = 'http://localhost/CRElection/';
// Update the name of the election
$electionName = 'CR Election';
$multiple_votes = false;
$max_votes = 1;
$negative_votes = false;
$max_n_votes = 1;
$is_stv = false;
$delegation_size = 1;
//---------------------------------------------------------------------//
// Set default values for config option
if ( !(isset($multiple_votes) && $multiple_votes === true) )
$multiple_votes = false;
if ( !isset($max_votes) )
$max_votes = 1;
if ( !(isset($negative_votes) && $negative_votes === true) ) {
$negative_votes = false;
$max_n_votes = 0;
}
if ( !isset($max_n_votes) )
$max_n_votes = 1;
if ( !isset($is_stv) )
$is_stv = false;
//---------------------------------------------------------------------//
// Database connection
$DB = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (mysqli_connect_errno()){
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
function auth(){
header('WWW-Authenticate: Basic realm="Authentication Required"');
header('HTTP/1.0 401 Unauthorized');
echo '<h1>Access denied.</h1>';
exit;
}
if (!empty($auth_required)){
if (!isset($_SERVER['PHP_AUTH_USER'])) {
auth();
} else {
$query = mysqli_prepare($DB, "SELECT meta_value FROM `meta` WHERE `meta_name`='admin_pass'");
mysqli_stmt_execute($query);
mysqli_stmt_bind_result($query, $pass);
mysqli_stmt_store_result($query);
if(mysqli_stmt_fetch($query)){
if ($_SERVER['PHP_AUTH_USER'] != 'admin' || sha1($_SERVER['PHP_AUTH_PW']) != $pass){
auth();
}
}
}
}
//---------------------------------------------------------------------//
//Common functions
if (php_sapi_name() !== 'cli') {
$IP = $_SERVER['REMOTE_ADDR'];
}
function allowed() {
global $DB, $IP;
$query = mysqli_prepare($DB, "SELECT allow_vote FROM `clients` WHERE ip = ?");
mysqli_stmt_bind_param($query, 's', $IP);
mysqli_stmt_execute($query);
mysqli_stmt_bind_result($query, $allowed);
mysqli_stmt_store_result($query);
mysqli_stmt_fetch($query);
return $allowed == 1 ? true : false ;
}
function get_clients() {
global $DB;
$query = mysqli_prepare($DB, "SELECT id, name, ip FROM `clients`");
mysqli_stmt_execute($query);
mysqli_stmt_bind_result($query, $id, $name, $ip);
mysqli_stmt_store_result($query);
$results = array();
while(mysqli_stmt_fetch($query)){
$results[$id] = array(
"name" => $name,
"ip" => $ip
);
}
return $results;
}
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;
}