This repository was archived by the owner on May 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendVote.php
More file actions
59 lines (51 loc) · 1.78 KB
/
sendVote.php
File metadata and controls
59 lines (51 loc) · 1.78 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
<?php
print_r($_POST);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('max_execution_time', 120);
error_reporting(E_ALL);
require './login.php';
require_once './assets/config.php';
$psql = pg_connect("$db->host $db->port $db->name $db->credentials");
if (empty($_SESSION['access_token'])) {
header('Location: error.php?eCode=auth_err&eDesc=Not Authenticated');
die();
} else if ($_SESSION['api_user']->hasMember === false) {
header('Location: error.php?eCode=no_role');
die();
}
$already_voted = voteStatus($psql, $_SESSION['api_user']->id);
if (empty($already_voted)) {
echo "<script>console.log('User has not voted yet!');</script>";
} else {
header('Location: error.php?eCode=already_voted');
die();
}
if ($duedate < date('Y-m-d')) {
header('Location: error.php?eCode=concluded');
}
$edit_code = generateCode(16);
$votedList = [];
foreach ($_POST as $nomineeId) {
if (!in_array($nomineeId, $votedList)) {
$nomineeId = pg_escape_string($nomineeId);
$insert = "INSERT INTO votes (nominee, edit_code) VALUES ('{$nomineeId}', '{$edit_code}')";
$res = pg_query($psql, $insert);
array_push($votedList, $nomineeId);
} else {
echo "<script>console.log('Duplicate vote detected, omitting.')</script>";
}
}
$voted = "INSERT INTO voter_list (user_snowflake) VALUES ('{$_SESSION['api_user']->id}')";
$res = pg_query($psql, $voted);
header("Location: voteComplete.php?editCode=".$edit_code);
function generateCode($length) {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$charLength = strlen($chars);
$code = '';
for ($i = 0; $i < $length; $i++) {
$code .= $chars[rand(0, $charLength - 1)];
}
return $code;
}
?>