-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
351 lines (285 loc) · 10.9 KB
/
index.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
//Name: Alicia Buehner, Evan Wheeler
//Date: 6/10/19
//Description: This file contains the index page for the HeroHub IT328 Final Project
//Turn on error reporting
ini_set('display_errors', 1);
error_reporting(E_ALL);
//Require vendor/autoload file
require_once('vendor/autoload.php');
// Start session
session_start();
//Create an instance of the Base class (instantiate Fat-Free)
$f3 = Base::instance();
define('HASH_ALGO', 'sha512');
//Turn on Fat-Free error reporting
$f3->set('DEBUG', 3);
// create the database connection, FIXME note: hive variable so that we could get data from views
$f3->set('db', new Database());
//expected values for form fields
$f3->set('platforms', array('pc' => 'PC', 'psn' => 'PS4', 'xbl' => "Xbox One"));
$f3->set('memberships', 'Sign up for Premium Access and gain preferred character matching!');
$f3->set('regions', array('America', 'Europe', 'Asia'));
$f3->set('heroes', array('ana' => 'Ana', 'ashe' => 'Ashe', 'baptiste' => 'Baptiste', 'bastion' => 'Bastion',
'brigitte' => 'Brigitte', 'dVa' => 'D.Va', 'doomfist' => 'Doomfist', 'genji' => 'Genji', 'hanzo' => 'Hanzo',
'junkrat' => 'Junkrat', 'lucio' => 'Lúcio', 'mccree' => 'McCree', 'mei' => 'Mei', 'mercy' => 'Mercy',
'moira' => 'Moira', 'orisa' => 'Orisa', 'pharah' => 'Pharah', 'reaper' => 'Reaper', 'reinhardt' => 'Reinhardt',
'roadhog' => 'Roadhog', 'soldier76' => 'Soldier: 76', 'sombra' => 'Sombra', 'symmetra' => 'Symmetra',
'torbjorn' => 'Torbjörn', 'tracer' => 'Tracer', 'widowmaker' => 'Widowmaker', 'winston' => 'Winston',
'wreckingBall' => 'Wrecking Ball', 'zarya' => 'Zarya', 'zenyatta' => 'Zenyatta'));
//Define a default route to homepage
$f3->route('GET /', function()
{
$view = new Template();
echo $view->render('views/homepage.html');
});
//Define route to the first form page user registration information
$f3->route('GET|POST /account', function($f3)
{
//variable that indicates whether a user is complete
$loggedIn = isset($_SESSION['user']) && $_SESSION['user']->getId() != 0;
if (empty($_POST)) {
if ($loggedIn) {
$user = $_SESSION['user'];
$f3->set('platform', $user->getPlatform());
$f3->set('email', $user->getEmail());
$f3->set('membership', $user instanceof PremiumUser ? $f3->get('memberships') : '');
}
}
//if post is not empty
else {
//get data from form - $variable = $_POST['']
$platform = $_POST['platform'];
$email = $_POST['email'];
$pw = $_POST['password'];
$pw2 = $_POST['password2'];
$membership = $_POST['membership'];
//add data to the hive - $f3->set('', $variable)
$f3->set('platform', $platform);
$f3->set('email', $email);
$f3->set('password', $pw);
$f3->set('password2', $pw2);
$f3->set('membership', $membership);
//if valid add to session (valid form) set session to variable
//redirect to preferences page
if (validForm1()) {
//hash password after valid check
$pw = hash(HASH_ALGO, $_POST['password']);
if ($loggedIn) {
if ($platform != $_SESSION['user']->getPlatform()) {
// we need to update the tag if the platform changed
$_SESSION['platform'] = $platform;
$_SESSION['email'] = $email;
$_SESSION['passhash'] = $pw;
$f3->reroute('/preferences');
}
// call the update function
$f3->get('db')->updateAccount($_SESSION['user']->getId(), $platform, $email, $pw);
// and redirect them back to the dashboard
$f3->reroute('/dashboard');
} else {
if (!empty($membership)) {
$newUser = new PremiumUser($platform, $email, $pw);
} else {
$newUser = new User($platform, $email, $pw);
}
$_SESSION['user'] = $newUser;
//Redirect to preferences
$f3->reroute('/preferences');
}
}
}
$view = new Template();
echo $view->render('views/account.html');
});
//Define route to second form page user play style settings
$f3->route('GET|POST /preferences', function($f3)
{
//variable that indicates whether a user is complete
$loggedIn = isset($_SESSION['user']) && $_SESSION['user']->getId() != 0;
if (empty($_POST)) {
if ($loggedIn) {
$user = $_SESSION['user'];
$f3->set('tag', $user->getTag());
$f3->set('mic', $user->getMicPref());
$f3->set('leadership', $user->getLeaderPref());
$f3->set('region', $user->getRegion());
}
// if post data was supplied
} else {
// get data from form - $variable = $_POST['']
$tag = $_POST['tag'];
$mic = $_POST['mic'];
$leadership = $_POST['leader'];
$region = null;
//if user is a pc player or is switching to pc
if ($_SESSION['user']->getPlatform() == 'pc' && !(isset($_SESSION['platform']) ||
$_SESSION['platform'] == 'pc')) {
//gather region field
$region = $_POST['region'];
//set to hive here
$f3->set('region', $region);
}
//add data to the hive - $f3->set('', $variable)
$f3->set('tag', $tag);
$f3->set('mic', $mic);
$f3->set('leadership', $leadership);
//if valid add to session (valid form) set session to variable
//redirect to heroes page if PremiumUser
if (validForm2()) {
$user = $_SESSION['user'];
$user->setTag($tag);
$user->setRegion($region);
$user->setMicPref($mic);
$user->setLeaderPref($leadership);
if ($loggedIn) {
if (isset($_SESSION['platform'])) {
// call the update function
$f3->get('db')->updateAccount(
$_SESSION['user']->getId(), $_SESSION['platform'],
$_SESSION['email'], $_SESSION['passhash']);
$user->setPlatform($_SESSION['platform']);
$user->setEmail($_SESSION['email']);
$user->setPasshash($_SESSION['passhash']);
unset($_SESSION['platform']);
unset($_SESSION['email']);
unset($_SESSION['passhash']);
}
// call the update function
$f3->get('db')->updatePreferences($_SESSION['user']->getId(), $tag, $region, $mic, $leadership);
// and redirect them back to the dashboard
$f3->reroute('/dashboard');
}
//redirect user based on User type
if ($_SESSION['user'] instanceof PremiumUser) {
//Redirect to heroes form
$f3->reroute('/heroes');
} else {
//insert the user in the db
$f3->get('db')->insertUser($user);
//Redirect to summary
$f3->reroute('/summary');
}
}
}
$view = new Template();
echo $view->render('views/preferences.html');
});
//Define route to the third form page user hero preferences
$f3->route('GET /heroes', function($f3)
{
if ($_SESSION['user'] instanceof PremiumUser) {
$view = new Template();
echo $view->render('views/heroes.html');
} else {
//Redirect to summary if not a PremiumUser
$f3->reroute('/summary');
}
});
//Define route to the third form page user hero preferences
$f3->route('POST /heroes', function($f3)
{
if (!($_SESSION['user'] instanceof PremiumUser)) {
//Redirect to summary if not a PremiumUser
$f3->reroute('/summary');
}
if (!empty($_POST)) {
//get data from form - $variable = $_POST['']
$role = $_POST['role'];
$hero1 = $_POST['preferredchar1'];
$hero2 = $_POST['preferredchar2'];
$hero3 = $_POST['preferredchar3'];
//add data to the hive - $f3->set('', $variable)
$f3->set('role', $role);
$f3->set('hero1', $hero1);
$f3->set('hero2', $hero2);
$f3->set('hero3', $hero3);
//if valid add to session (valid form) set session to variable
//redirect to summary page
if (validForm3()) {
$user = $_SESSION['user'];
$user->setRole($role);
$heroes = array();
if ($hero1 != '') {
$heroes[] = $hero1;
}
if ($hero2 != '') {
$heroes[] = $hero2;
}
if ($hero3 != '') {
$heroes[] = $hero3;
}
$user->setHeroes($heroes);
//Insert the PremiumUser into the db
$f3->get('db')->insertUser($user);
//Redirect to summary
$f3->reroute('/dashboard');
}
}
$view = new Template();
echo $view->render('views/heroes.html');
});
//Define route to the registration summary page
$f3->route('GET|POST /summary', function($f3)
{
if (!isset($_SESSION['user'])) {
// reroute to the login page if the page is accessed again
$f3->reroute('/login');
}
$view = new Template();
echo $view->render('views/summary.html');
// clear the session variable to indicate a logged out User
$_SESSION = array();
});
//Define route to the user login page
$f3->route('GET|POST /login', function($f3)
{
if (!empty($_POST)) {
// try to login and get the user
$user = $f3->get('db')->loginUser(
$_POST['email'], hash(HASH_ALGO, $_POST['password']));
if (isset($user)) {
// successful login, so set the user in the session and
// reroute to the dashboard
$_SESSION['user'] = $user;
$f3->reroute('/dashboard');
}
$f3->set('invalidLogin', true);
}
$view = new Template();
echo $view->render('views/login.html');
});
//Define route to the user dashboard page
$f3->route('GET|POST /dashboard', function($f3)
{
if (!isset($_SESSION['user']))
{
// reroute to the login page if the page is accessed without a user
$f3->reroute('/login');
}
if (!empty($_POST)) {
$f3->set('users', $f3->get('db')->selectUsersWithHero($_POST['hero']));
}
$view = new Template();
echo $view->render('views/dashboard.html');
});
$f3->route('GET /logout', function($f3)
{
if (!isset($_SESSION['user']))
{
// reroute to the login page if the page is accessed without a user
$f3->reroute('/login');
}
// clear session array and route to root
$_SESSION = array();
$f3->reroute('/');
});
$f3->route('GET|POST /delete', function($f3)
{
$f3->set('users', $f3->get('db')->deleteUser($_SESSION['user']));
// clear session array and route to root
$_SESSION = array();
$f3->reroute('/');
});
//Run Fat-free
$f3->run();