-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.php
127 lines (108 loc) · 2.53 KB
/
util.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
121
122
123
124
125
126
127
<?php
require_once __DIR__ . '/vendor/autoload.php';
session_start();
// Generate a pin
/**
* @return string
*/
function pin(int $length = 4): string
{
if ($length == 1) {
return strval(random_int(0, 9));
}
return random_int(0, 9) . pin($length - 1);
}
// returns the path of a vote based on the pin
function vote_path(string $pin): string
{
return sys_get_temp_dir() . DIRECTORY_SEPARATOR . $pin . ".json";
}
// retrieve a vote
function get_vote(string $pin)
{
$file = vote_path($pin);
if (file_exists($file)) {
$votedata = json_decode(file_get_contents($file), true);
return $votedata;
} else {
return NULL;
}
}
// save a vote structure to disk
function save_vote(array $vote): void
{
$pin = $vote["pin"];
$file = vote_path($pin);
$json = json_encode($vote);
file_put_contents($file, $json, LOCK_EX);
chmod($file, 0600);
}
// Initializes a blank vote
function initialize_vote(string $pin, string $description, string $type): void
{
$vote_data = array(
"pin" => $pin,
"description" => $description,
"type" => $type,
"votes" => array(),
);
save_vote($vote_data);
}
// append a vote to the vote file
function append_vote(string $pin, mixed $vote_contents): void
{
$vote = get_vote($pin);
$parsed = null;
// parse input depending on type of vote
switch ($vote["type"]) {
case "grade":
$parsed = intval($vote_contents);
break;
case "binary":
// workaround for abstentions
if ($vote_contents == "NULL") {
break;
}
$parsed = boolval($vote_contents);
break;
case "text":
$parsed = strval($vote_contents);
break;
}
// create vote entry
$vote_entry = [
"time" => time(),
"ip" => sha1($_SERVER['REMOTE_ADDR']),
"session-id" => session_id(),
"contents" => $parsed,
];
// add to existing votes
array_push($vote["votes"], $vote_entry);
// write to the file
save_vote($vote);
}
// Helper function to find out if someone is resubmitting their vote
function has_voted(string $pin): bool {
$target = session_id();
$votedata = get_vote($pin);
foreach ($votedata['votes'] as $vote) {
if ($vote['session-id'] == $target) {
return true;
}
}
return false;
}
// Returns a string describing the majority of counted votes to all votes
function getMaj(int $votes, int $total): string
{
if ($votes == $total) {
return "Einstimmige Mehrheit";
}
for ($i = $total; $i > 1; $i--) {
if (($votes % $i) == 0 && ($total % $i) == 0) {
$votes = $votes / $i;
$total = $total / $i;
}
}
return $votes . "/" . $total . " Mehrheit";
}