-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.php
More file actions
418 lines (379 loc) · 14.4 KB
/
index.php
File metadata and controls
418 lines (379 loc) · 14.4 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<?php
/*
* CS-CLUB Elections Website
*
* Copyright (C) 2013 Jonathan Gillett, Joseph Heron, Computer Science Club at DC and UOIT
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
require_once 'inc/db_interface.php';
require 'inc/election_auth.php';
require 'inc/election_date.php';
require_once 'inc/election.php';
require_once 'inc/utility.php';
require_once 'inc/validate.php';
require_once 'inc/verify.php';
session_start();
/* Connect to the databases */
$mysqli_accounts = new mysqli("localhost", $db_user, $db_pass, $db_acc_name);
$mysqli_elections = new mysqli("localhost", $db_user, $db_pass, $db_elec_name);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/**
* Handle which page to display based on the current events and whether or not the
* user has a login cookie set, there are the following conditions.
*
* TODO I DO NOT LIKE THIS IMPLEMENTATION, PERHAPS CLEANUP THE MULTILINE IF STATEMENTS INTO A CLEANER SET OF METHODS
*
* 1. User is not logged in, display one of the templates if the nomination/election
* period is open/closed with info about the Computer Science Club
*
* 2. User is not logged in and has submitted their login information
*
* a) If the login information is valid
*
* i) If they are already a member and have logged in for the election website
* before then sign them in
*
* ii) If it is the first time they have logged in display the first time login
* page with election information and terms and conditions
*
* b) If the login information is invalid dislpay the invalid login page
*
* 3. User is logged in and has clicked the Sign Out button
*
* 4. User has logged in for the first time and submitted the terms and conditions form
*
* a) If the user accepted the terms and conditions they are allowed to login and vote
*
* b) If the user did not accept the terms and conditions they are redirected to default site
*
* 5. User has a valid login cookie set / has logged into the site with valid account
* and the post data for NOMINATION voting is SET
*
* a) If the nomination vote post data is valid and it is the nomination period
*
* -> Record their nomination vote
*
* b) Elseif (check if they are nominating themselves)
*
* -> Record their nominate_self vote (does not use up their vote!)
*
* 6. User has a valid login cookie set / has logged into the site with valid account
* and the post data for ELECTION voting is SET
*
* a) If the election vote post data is valid and it is the election period
*
* -> Record their election vote
*
* 7. User has a valid login cookie set / has logged into the site with valid account
* and there is NO nomination/election post data
*
* a) If the user has not already voted display the template for nomination/election
* period voting
*
* b) If the user has already voted then dislplay the thank you for voting page
*
* c) If the user has already voted and the election period is over, display the
* election results page.
*
* 8. Display the footer template at the bottom of the page regardless
*/
/*
* 1. User is not logged in, display one of the templates if the nomination/election
* period is open/closed with info about the Computer Science Club
*/
if (verify_login_cookie($mysqli_accounts, $SESSION_KEY) === false
&& (!isset($_SESSION['login'])
|| verify_login_session($mysqli_accounts, $_SESSION['login'], $SESSION_KEY) === false)
&& !isset($_POST['login_username'])
&& !isset($_POST['login_password'])
&& !isset($_POST['accept_rules']))
{
include 'templates/header.php';
/* "Nomination open" template if its between Sept. 1, 12am to Sept 14th 11:59pm */
if (is_nomination($mysqli_elections))
{
include 'templates/nomination-open.php';
}
/* "Nomination closed" template if its after the nomination period and before election opens */
elseif (is_nomination_closed($mysqli_elections))
{
include 'templates/nomination-closed.php';
}
/* "Election open" template, it is the next weekday after Sept. 14th */
elseif (is_election($mysqli_elections))
{
include 'templates/election-open.php';
}
else
{
include 'templates/election-closed.php';
}
}
/* 2. User is not logged in and has submitted their login information */
elseif (verify_login_cookie($mysqli_accounts, $SESSION_KEY) === false
&& (!isset($_SESSION['login'])
|| verify_login_session($mysqli_accounts, $_SESSION['login'], $SESSION_KEY) === false)
&& isset($_POST['login_username'])
&& isset($_POST['login_password']))
{
/* a) If the login information is valid and they entered the correct username/password */
if (validate_username($_POST['login_username']) && validate_password($_POST['login_password'])
&& verify_login($mysqli_accounts, $_POST['login_username'] , $_POST['login_password'], $AES_KEY))
{
/* i) If they are already a member and have logged in to the election website
* before then sign them in
*/
if (is_member($mysqli_accounts, $mysqli_elections, $_POST['login_username']))
{
set_session_data($mysqli_accounts, $_POST['login_username'], $SESSION_KEY);
if ($_POST['login_remember'] == 1)
{
set_login_cookie();
}
/* Refresh the page */
header('Location: '.$_SERVER['REQUEST_URI']);
}
/* ii) If it is the first time they have logged in display the first time login
* page with election information and terms and conditions
*/
else
{
/* Store the login username and password they posted in the session, so they can
* be allowed to vote in the election if they accept the terms and conditions
*/
$_SESSION['login_username'] = $_POST['login_username'];
$_SESSION['login_password'] = $_POST['login_password'];
$_SESSION['login_remember'] = $_POST['login_remember'];
include 'templates/header.php';
include 'templates/first-login.php';
}
}
/* b) The login information is invalid dislpay the invalid login page */
else
{
include 'templates/header.php';
include 'templates/invalid-login.php';
}
}
/* 3. User is logged in and has clicked the Sign Out button */
elseif ((verify_login_cookie($mysqli_accounts, $SESSION_KEY)
|| verify_login_session($mysqli_accounts, $_SESSION['login'], $SESSION_KEY))
&& isset($_POST['signout']))
{
session_unset();
/* Overwrite the login cookie as NULL if it exists */
if (isset($_COOKIE['login']))
{
setcookie('login', NULL, time()+1);
}
/* Refresh the page */
header('Location: '.$_SERVER['REQUEST_URI']);
}
/* 4. User has logged in for the first time and submitted the terms and conditions form */
elseif (isset($_SESSION['login_username']) && isset($_SESSION['login_password'])
&& isset($_POST['accept_rules']))
{
$login_remember = $_SESSION['login_remember'];
$login_username = $_SESSION['login_username'];
session_unset();
/* a) If the user accepted the terms and conditions they are allowed to login and vote */
if ($_POST['accept_rules'] == 1)
{
add_member($mysqli_accounts, $mysqli_elections, $login_username);
set_session_data($mysqli_accounts, $login_username, $SESSION_KEY);
if ($login_remember == 1)
{
set_login_cookie();
}
}
/* Refresh the page */
header('Location: '.$_SERVER['REQUEST_URI']);
}
/* 5. User has a valid login cookie set / has logged into the site with valid account
* and the post data for NOMINATION voting is SET
*/
elseif ((verify_login_cookie($mysqli_accounts, $SESSION_KEY)
|| verify_login_session($mysqli_accounts, $_SESSION['login'], $SESSION_KEY))
&& isset($_POST['nomination_vote']))
{
if (!isset($_POST['president_nom']))
{
$_POST['president_nom'] = 'None';
}
if (!isset($_POST['vicepresident_nom']))
{
$_POST['vicepresident_nom'] = 'None';
}
if (!isset($_POST['coordinator_nom']))
{
$_POST['coordinator_nom'] = 'None';
}
if (!isset($_POST['treasurer_nom']))
{
$_POST['treasurer_nom'] = 'None';
}
/* An array mapping the positions to the nominee */
$positions = array( 'President' => $_POST['president_nom'],
'Vice President' => $_POST['vicepresident_nom'],
'Coordinator' => $_POST['coordinator_nom'],
'Treasurer' => $_POST['treasurer_nom'],
);
/* An array containing the positions a person has nominated themselves for */
$positions_self = array();
/* If nomination vote post data valid and its nomination period, record nomination vote */
if (validate_nomination_vote($mysqli_elections, $positions)
&& is_nomination($mysqli_elections) && !is_incumbent($mysqli_elections, $positions))
{
/* Get each position the user has nominated themselves for */
foreach ($positions as $position => $nominee)
{
if (validate_nominate_self($nominee))
{
$positions_self[] = $position;
}
}
/* Nominate user for each position the user nominated themselves for, now others can vote for them */
if (count($positions_self) > 0)
{
nominate_self($mysqli_elections, $_SESSION['access_account'], $positions_self);
}
/* Record the nominees and the position the nominees are in that the user voted for */
nomination_vote($mysqli_elections, $_SESSION['access_account'], $positions);
/* Refresh the page */
header('Location: '.$_SERVER['REQUEST_URI']);
}
/* Voting error, user either posted invalid data, or tried to vote when election is closed */
else
{
include 'templates/header-member.php';
include 'templates/voting-error.php';
}
}
/* 6. User has a valid login cookie set / has logged into the site with valid account
* and the post data for ELECTION voting is SET
*/
elseif ((verify_login_cookie($mysqli_accounts, $SESSION_KEY)
|| verify_login_session($mysqli_accounts, $_SESSION['login'], $SESSION_KEY))
&& isset($_POST['election_vote']))
{
/* An array mapping the positions to the election nominee */
$positions = array( 'President' => '',
'Vice President' => '',
'Coordinator' => '',
'Treasurer' => ''
);
/* An array mapping the position to the POST data name */
$positions_post = array('President' => 'president_elect',
'Vice President' => 'vicepresident_elect',
'Coordinator' => 'coordinator_elect',
'Treasurer' => 'treasurer_elect'
);
/* TODO CLEAN THIS UP -- I DO NOT LIKE THIS!!!
* For each position's post data select just the name of the nominee from the position, ignoring the
* parentheses indicating "(candidate)" or "(incumbent)"
*/
foreach ($positions_post as $position => $post_name)
{
if (preg_match('/(^(([A-Za-z]+)|\s{1}[A-Za-z]+)+)\s*?(\(Candidate\)|\(Incumbent\))$/', $_POST[$post_name], $matches))
{
// The first regex group, $matches[1] is the nominee's name
$positions[$position] = $matches[1];
}
}
/* If election vote post data valid and its election period, record election vote */
if (validate_election_vote($mysqli_elections, $positions)
&& is_election($mysqli_elections))
{
/* Record the final election nominees and the position they are in that the user voted for */
election_vote($mysqli_elections, $_SESSION['access_account'], $positions);
/* Refresh the page */
header('Location: '.$_SERVER['REQUEST_URI']);
}
/* Voting error, user either posted invalid data, or tried to vote when election is closed */
else
{
include 'templates/header-member.php';
include 'templates/voting-error.php';
}
}
/* 7. User has a valid login cookie set / has logged into the site with valid account */
elseif (verify_login_cookie($mysqli_accounts, $SESSION_KEY)
|| verify_login_session($mysqli_accounts, $_SESSION['login'], $SESSION_KEY))
{
/* FIX, forgot to account for when user has login cookie set but there is no session
* data, have to retrieve username from cookie and then set the session data
*/
if (verify_login_cookie($mysqli_accounts, $SESSION_KEY))
{
/* Get the login cookie data */
$login_cookie = htmlspecialchars($_COOKIE['login']);
/* Get the username from login cookie data and set session info */
$username = username_from_session($mysqli_accounts, $login_cookie, $SESSION_KEY);
set_session_data($mysqli_accounts, $username, $SESSION_KEY);
}
include 'templates/header-member.php';
/* a) If the user has not already voted display the template for nomination/election period voting */
if (!has_voted_all_positions($mysqli_elections, $_SESSION['access_account'], "nomination")
//(!has_voted($mysqli_elections, $_SESSION['access_account'], "nomination")
&& is_nomination($mysqli_elections))
{
/* Get the nominees needed to populate the nomination voting form */
$nominees = get_nominees($mysqli_elections);
/* Get the incumbents to ensure an incumbent cannot nominate themself */
$incumbents = get_incumbents($mysqli_elections);
include 'templates/nomination-form.php';
}
elseif (!has_voted($mysqli_elections, $_SESSION['access_account'], "election")
&& is_election($mysqli_elections))
{
/* Get the candidates and incumbents needed to populate the election voting form*/
$candidates = get_candidates($mysqli_elections);
$incumbents = get_incumbents($mysqli_elections);
include 'templates/election-form.php';
}
/* b) If the user has already voted then display the thank you for voting page with
* election results/information if applicable
*/
elseif ((has_voted($mysqli_elections, $_SESSION['access_account'], "nomination")
|| has_voted($mysqli_elections, $_SESSION['access_account'], "election"))
&& (is_nomination($mysqli_elections) || is_election($mysqli_elections)))
{
include 'templates/already-voted.php';
}
else
{
/* "Nomination closed" template if its after the nomination period and before election opens */
if (is_nomination_closed($mysqli_elections))
{
include 'templates/nomination-closed.php';
}
/* "Election closed" template, it is AFTER the next weekday after Sept. 14th */
else
{
include 'templates/election-closed.php';
}
}
}
/* close connection */
$mysqli_accounts->close();
$mysqli_elections->close();
include 'templates/footer.php';
exit();
?>