-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboardgame_survey_loader.php
332 lines (294 loc) · 11.2 KB
/
boardgame_survey_loader.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
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
<?php
$session_started = session_start();
if(!$session_started) {
die("Error: Session could not be started!");
}
require_once("./libs/Collection.php");
require_once("./libs/Helpers.php");
require_once("./libs/UniqueVotes.php");
visitor_check();
require_once("./libs/Boardgame.php");
require_once("./libs/Survey.php");
require_once("./inc/SetAdminPage.php");
require_once("./inc/UnauthenticatedPage.php");
require_once("./inc/LoginPage.php");
require_once("./inc/LoginValidationPage.php");
require_once("./inc/NewEntryPage.php");
require_once("./inc/AdminHomePage.php");
require_once("./inc/StartSurveyPage.php");
require_once("./inc/ShowSurveysPage.php");
require_once("./inc/ShowSurveyPage.php");
require_once("./inc/ShowBoardgamePage.php");
require_once("./inc/ShowSurveyResultsPage.php");
require_once("./inc/ShowBoardgamesPage.php");
function digest_request(string $received_payload) {
switch ($received_payload) {
case Payload::NewAdminPassword:
// check if request is valid
if((!isset($_SESSION["admin"]) || !$_SESSION["admin"] || !isset($_POST["new_password"])) && file_exists("./config.json"))
{
break;
}
// load/create config and insert new password
if(!file_exists("./config.json"))
{
$password_hash = password_hash(htmlspecialchars($_POST["new_password"]), PASSWORD_DEFAULT);
$config = array(
"admin_password" => $password_hash
);
} else {
$config = json_decode(file_get_contents("./config.json"), true);
$config["admin_password"] = password_hash(htmlspecialchars($_POST["new_password"]), PASSWORD_DEFAULT);
}
// save new config
$result = file_put_contents("./config.json", json_encode($config));
if(!$result) {
die("Error: New config could not be written. Maybe the index.php file settings are not sufficent.");
} else {
echo <<<SUCCESS
<p class="success">Password has been changed</p>
SUCCESS;
}
// log user out
$_SESSION["admin"] = false;
break;
case Payload::Login:
if((isset($_SESSION["admin"]) && $_SESSION["admin"]) || !file_exists("./config.json") || !isset($_POST["password"]))
{
break;
}
// read password hash
$config = json_decode(file_get_contents("./config.json"), true);
$password_hash = $config["admin_password"];
// validate
if (password_verify(htmlspecialchars($_POST["password"]), $password_hash))
{
$_SESSION["admin"] = true;
} else {
$_SESSION["admin"] = false;
sleep(1); // hold program to reduce brute force attack validation speed
}
break;
case Payload::NewEntry:
// check if request is valid
if(!isset($_SESSION["admin"]) || !$_SESSION["admin"])
{
break;
}
// read data
if(!isset($_POST["title"]))
{
die("Error: The title of the game could not be read!");
}
$title = htmlspecialchars($_POST["title"]);
$player_count = array();
if(isset($_POST["player-count"]))
{
// securing payload
foreach($_POST["player-count"] as $key => $players)
{
$player_count[$key] = htmlspecialchars($players);
}
}
$multisession = false;
if(isset($_POST["multisession"]))
{
$multisession = true;
}
$tags = array();
if(isset($_POST["tags"]))
{
// securing payload
foreach($_POST["tags"] as $key => $tag)
{
$tags[$key] = htmlspecialchars($tag);
}
}
$languages = array();
if(isset($_POST["languages"]))
{
// securing payload
foreach($_POST["languages"] as $key => $language)
{
$languages[$key] = htmlspecialchars($language);
}
}
$preview_url = "";
if(isset($_POST["preview_url"]))
{
$preview_url = htmlspecialchars($_POST["preview_url"]);
}
$tutorial_url = "";
if(isset($_POST["tutorial_url"]))
{
$tutorial_url = htmlspecialchars($_POST["tutorial_url"]);
}
$bgg_url = "";
if(isset($_POST["bgg_url"]))
{
$bgg_url = htmlspecialchars($_POST["bgg_url"]);
}
// extract BGG game ID from BGG URL
preg_match("#^https?://([^./]+.)com/[^/]+/(?<id>\d+)/#", $bgg_url, $matches);
if(!isset($matches["id"]))
{
die("Error: Invalid Boardgamegeek game URL supplied!");
}
$bgg_id = $matches["id"];
// save board game
$boardgame = new Boardgame($title, $player_count, $multisession, $tags, $languages, $preview_url, $tutorial_url, $bgg_id);
$boardgame->write_boardgame_to_json();
break;
case Payload::NewSurvey:
// check if request is valid
if(!isset($_SESSION["admin"]) || !$_SESSION["admin"])
{
break;
}
// read data
if(!isset($_POST["player_count"]))
{
die("Error: Missing player count!");
}
$player_count = (int)htmlspecialchars($_POST["player_count"]);
if(!isset($_POST["games_amount"]))
{
die("Error: Missing games amount!");
}
$games_amount = (int)htmlspecialchars($_POST["games_amount"]);
$until = time() + (24*5); // default 5 days
if(isset($_POST["run_until_date"]) && $_POST["run_until_date"] != "" && isset($_POST["user_timezone"]) && $_POST["user_timezone"] != "")
{
//validate timezone
$user_tz = htmlspecialchars($_POST["user_timezone"]);
if(preg_match("#[+-]\d\d:\d\d#", $user_tz)!=1)
{
die("Error: Invalid time zone format received!");
}
$until_date = htmlspecialchars($_POST["run_until_date"]);
if(isset($_POST["run_until_time"]) && $_POST["run_until_time"] != "")
{
$until_time = htmlspecialchars($_POST["run_until_time"]);
} else {
$until_time = "00:00";
}
$until = strtotime("{$until_date}T{$until_time}:00.00{$user_tz}");
if(!$until)
{
die("Error: Invalid Date or Time format received!");
}
}
$survey = new Survey();
$survey->start_survey($player_count, $games_amount, $until);
break;
case Payload::NewVote:
// check if request is valid
// todo insert some sort of check for multi-voting attempts
if(!isset($_POST["survey_id"]))
{
die("Error: No survey_id found. Can not process votes for unknown survey.");
}
// load survey
$survey_id = htmlspecialchars($_POST["survey_id"]);
$surveys = fetch_all_surveys();
if(!key_exists($survey_id, $surveys))
{
die("Error: No survey found for id '$survey_id'.");
}
$survey = $surveys[$survey_id];
// load votes
$voting = array();
if(isset($_POST["vote"]))
{
// securing payload
foreach($_POST["vote"] as $key => $players)
{
$voting[$key] = htmlspecialchars($players);
}
} else {
error_log("Warning: Blank vote received.");
break;
}
$survey->digest_voting($voting);
break;
default:
die("Error: Not yet implemented!");
}
}
function page_init(string $requested_page): WebPage {
switch ($requested_page) {
case Page::Login:
if(!file_exists("./config.json")) {
return new SetAdminPage();
}
if(isset($_SESSION["admin"]) && $_SESSION["admin"]) {
return new AdminHomePage();
}
return new LoginPage();
case Page::LoginVerification:
if(!isset($_SESSION["admin"]) || !$_SESSION["admin"])
{
return new LoginValidationPage(false);
}
return new LoginValidationPage(true);
case Page::SetAdminUser:
if((isset($_SESSION["admin"]) && $_SESSION["admin"]) || !file_exists("./config.json")) {
return new SetAdminPage();
}
return new UnauthenticatedPage();
case Page::NewEntry:
if(isset($_SESSION["admin"]) && $_SESSION["admin"])
{
return new NewEntryPage();
}
return new UnauthenticatedPage();
case Page::AdminHome:
if(isset($_SESSION["admin"]) && $_SESSION["admin"])
{
return new AdminHomePage();
}
return new UnauthenticatedPage();
case Page::ShowSurveys:
if(isset($_SESSION["admin"]) && $_SESSION["admin"])
{
return new ShowSurveysPage();
}
return new UnauthenticatedPage();
case Page::ShowSurvey:
if(isset($_GET["survey_id"]))
{
return new ShowSurveyPage(htmlspecialchars($_GET["survey_id"]));
}
return new LoginPage();
case Page::ShowBoardgameDetails:
if(isset($_GET["boardgame_id"])){
return new ShowBoardgamePage(htmlspecialchars($_GET["boardgame_id"]));
}
return new LoginPage();
case Page::ShowSurveyResults:
if(isset($_GET["survey_id"]))
{
return new ShowSurveyResultsPage(htmlspecialchars($_GET["survey_id"]));
}
return new LoginPage();
case Page::StartNewSurvey:
if(isset($_SESSION["admin"]) && $_SESSION["admin"])
{
return new StartSurveyPage();
}
return new UnauthenticatedPage();
case Page::ShowBoardgames:
if(isset($_SESSION["admin"]) && $_SESSION["admin"])
{
return new ShowBoardgamesPage();
}
return new UnauthenticatedPage();
default:
if(key_exists($requested_page, Lists::AllPages))
{
die("Error: $requested_page not yet implemented!");
}
die("Error: $requested_page not implemented!");
}
}
?>