-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
485 lines (404 loc) · 13 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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
//Start session
$lifetime = 0;
session_set_cookie_params($lifetime, '/', 'recipe.nathantschultz.com');
session_start();
require_once('../../Database_Connections/recipe_db.php');
require_once('model.php');
require_once('helpers.php');
// Get the action to perform
if (isset($_POST['action'])) {
$action = cleanString($_POST['action']);
} else if (isset($_GET['action'])) {
$action = cleanString($_GET['action']);
} else {
$action = 'content';
}
// Get page id
if (isset($_POST['page_id'])) {
$page_id = $_POST['page_id'];
} else if (isset($_GET['page_id'])) {
$page_id = $_GET['page_id'];
} else {
$page_id = 1;
}
// Determine action to take
switch($action) {
case 'search':
$_SESSION['searchTerm'] = cleanString($_POST['searchTerm']);
header('Location: /view/search_results.php');
break;
case 'create_account':
header('Location: /view/createAccount.php');
break;
case 'add_account':
$name = cleanString($_POST['name']);
$email = cleanEmail($_POST['email']);
$password = $_POST['password'];
$problems = array();
if (empty($name)){
$problems['name'] = ' Your name is required.<br>';
}
if (empty($email)){
$problems['email'] = ' Please enter a correctly formatted email.<br>';
}
if (empty($password)){
$problems['password'] = ' Your password is required.<br>';
}
if (strlen($password)<8 or strlen($password) > 20) {
$problems['pLength'] = " Your password must have between 8 and 20 characters.";
}
if (searchForPerson($email)){
$problems['error'] = " There is already an existing account with that email address.";
}
// If problems are found send them back to be fixed
if (!empty($problems))
{
foreach ($problems as $problem){
$_SESSION['$alerts'] .= $problem;
}
header('Location: /view/createAccount.php');
}
$password = hashPassword($password);
// If no problems, proceed with the database interaction
if(empty($problems))
{
$insertresult = insertPerson($name, $email, $password);
$userId = getUserId($email);
// Test what was returned from the model
if ($insertresult and $userId)
{
$_SESSION['userId'] = $userId;
$_SESSION['login'] = true;
$_SESSION['$alerts'] .= "Your account has been created.";
header('Location: /view/profile.php');
} else {
// it failed
$_SESSION['$alerts'] .= "Sorry, an error occurred while attempting to create your account.";
header('Location: /view/createAccount.php');
}
}
break;
case 'login':
if ($_SESSION['login']){
header('Location: /view/profile.php');
} else {
header('Location: /view/login.php');
}
break;
case 'logout':
$_SESSION = array();
session_destroy();
include $_SERVER['DOCUMENT_ROOT']. '/view/header.php';
echo "<section class='content'>You have been logged out.</section>";
break;
case 'check_credentials':
//$_SESSION['alert'] = $email;
$email = $_POST['email'];
$password = hashPassword($_POST['password']);
$key = checkCredentials($email, $password);
$userId11 = getUserId($email);
$isAdmin = getIsAdmin($userId11);
if ($key and $userId11){
//Open sesame
$_SESSION['$alerts'] .= "Access Granted.";
$_SESSION['login'] = true;
$_SESSION['userId'] = $userId11;
if($isAdmin){
$_SESSION['admin'] = true;
} else {
$_SESSION['admin'] = false;
}
header('Location: /view/profile.php');
} else {
//You shall not pass!
$_SESSION['$alerts'] .= "Access Denied.";
header('Location: /view/login.php');
}
break;
case 'profile':
if($_SESSION['login']){
header('Location: /view/profile.php');
} else {
header('Location: /view/login.php');
}
break;
case 'edit_user':
if ($_SESSION['login']){
$_SESSION['userId1'] = $_SESSION['userId'];
if ($_SESSION['admin']){
$_SESSION['userId1'] = $page_id;
}
header('Location: /view/edit_user.php');
} else {
$_SESSION['$alerts'] .= "ACCESS DENIED.";
header('Location: /view/blank.php');
}
break;
case 'update_user':
if ($_SESSION['login']){
$admin1 = cleanString($_POST['adminSelected']);
if ($_SESSION['admin'] and $admin1){
$admin1 = true;
} else {
$admin1 = 0;
}
if ($_SESSION['admin']){
$userId2 = $page_id;
} else {
$userId2 = $_SESSION['userId'];
}
$name = cleanString($_POST['name']);
$email = cleanEmail($_POST['email']);
$password = $_POST['password'];
$problems = array();
if (empty($name)){
$problems['name'] = ' Your name is required.<br>';
}
if (empty($email)){
$problems['email'] = ' Please enter a correctly formatted email.<br>';
}
if (empty($password)){
$problems['password'] = ' Your password is required.<br>';
}
if (strlen($password)<8 or strlen($password) > 20) {
$problems['pLength'] = " Your password must have between 8 and 20 characters.";
}
// If problems are found send them back to be fixed
if (!empty($problems))
{
foreach ($problems as $problem){
$_SESSION['$alerts'] .= $problem;
}
header('Location: /view/edit_user.php');
}
$password = hashPassword($password);
// If no problems, proceed with the database interaction
if(empty($problems))
{
$insertresult = editUser($userId2, $name, $email, $password, $admin1);
// Test what was returned from the model
if ($insertresult){
$_SESSION['$alerts'] .= "The account has been updated.";
} else {
$_SESSION['$alerts'] .= "There was an error updating the account.";
}
header('Location: /view/profile.php');
}
} else {
$_SESSION['$alerts'] .= "ACCESS DENIED.";
header('Location: /view/blank.php');
}
break;
case 'confirm_delete_user':
if ($_SESSION['login']){
$uId = $_SESSION['userId'];
if ($_SESSION['admin']){
$uId = $page_id;
}
$_SESSION['confirm_id'] = $uId;
header('Location: /view/confirm_user.php');
} else {
$_SESSION['$alerts'] .= "ACCESS DENIED.";
header('Location: /view/blank.php');
}
break;
case 'delete_user':
if ($_SESSION['login']){
$userI = $_SESSION['userId'];
if ($_SESSION['admin']){
$userI = $page_id;
}
$isDeleted = deleteUser($userI);
if($isDeleted){
$_SESSION['$alerts'] .= "User Deleted.<br>";
if ($_SESSION['userId'] == $userI){
$_SESSION = array();
session_destroy();
include $_SERVER['DOCUMENT_ROOT']. '/view/header.php';
echo "User Deleted.";
include $_SERVER['DOCUMENT_ROOT']. '/view/footer.php';
} else {
header('Location: /view/profile.php');
}
} else {
$_SESSION['$alerts'] .= "An error occurred while attempting to delete the user.";
header('Location: /view/profile.php');
}
} else {
$_SESSION['$alerts'] .= "ACCESS DENIED.";
header('Location: /view/blank.php');
}
break;
case 'list_content':
if ($_SESSION['admin']){
header('Location: /view/list.php');
} else {
$_SESSION['$alerts'] .= "ACCESS DENIED.";
header('Location: /view/blank.php');
}
break;
case 'create_content':
if ($_SESSION['admin']){
header('Location: /view/create.php');
} else {
$_SESSION['$alerts'] .= "ACCESS DENIED.";
header('Location: /view/blank.php');
}
break;
case 'save_content';
if ($_SESSION['admin']){
$title = cleanString($_POST['title']);
$cooking_time = cleanString($_POST['cooking_time']);
$difficulty = cleanString($_POST['difficulty']);
$ingredients = encodeTags($_POST['ingredients']);
$directions = encodeTags($_POST['directions']);
//$image_name = cleanString($_POST['image_name']);
$poster = $_SESSION['userId'];
/*
$_SESSION['$alerts'] .= $title;
$_SESSION['$alerts'] .= $cooking_time;
$_SESSION['$alerts'] .= $difficulty;
$_SESSION['$alerts'] .= $ingredients;
$_SESSION['$alerts'] .= $directions;
$_SESSION['$alerts'] .= $image_name;
$_SESSION['$alerts'] .= $poster;
*/
$image_dir = 'images';
$image_dir_path = getcwd() . DIRECTORY_SEPARATOR . $image_dir;
$image_name = upload_file('file1');
$problems = array();
if (empty($title)){
$problems['title'] = '*The title must be supplied.<br>';
}
if (empty($ingredients) or empty($directions)){
$problems['empty'] = '*You must enter the ingredients and directions.<br>';
}
// If problems are found send them back to be fixed
if (!empty($problems))
{
foreach ($problems as $problem){
$_SESSION['$alerts'] .= $problem;
}
header('Location: /view/create.php');
}
// If no problems, proceed with the database interaction
if
(empty($problems)){
$insertresult = insertContent($title, $cooking_time, $difficulty, $ingredients, $directions, $image_name, $poster);
// Test what was returned from the model
if ($insertresult)
{
$_SESSION['$alerts'] .= "The page has been created.";
} else {
// it failed
$_SESSION['$alerts'] .= "Sorry, an error occurred while attempting to create your page.";
}
header('Location: /view/list.php');
}
} else {
$_SESSION['$alerts'] .= "ACCESS DENIED.";
header('Location: /view/blank.php');
}
break;
case 'edit_content':
if ($_SESSION['admin']){
$_SESSION['linkId'] = $page_id;
header('Location: /view/edit.php');
} else {
$_SESSION['$alerts'] .= "ACCESS DENIED.";
header('Location: /view/blank.php');
}
break;
case 'update_content':
if ($_SESSION['admin']){
$title = cleanString($_POST['title']);
$cooking_time = cleanString($_POST['cooking_time']);
$difficulty = cleanString($_POST['difficulty']);
$ingredients = encodeTags($_POST['ingredients']);
$directions = encodeTags($_POST['directions']);
$image_name = cleanString($_POST['image_name']);
$poster = $_SESSION['userId'];
$oldTitle = cleanString($_POST['oldTitle']);
/*
$_SESSION['$alerts'] .= $title;
$_SESSION['$alerts'] .= $cooking_time;
$_SESSION['$alerts'] .= $difficulty;
$_SESSION['$alerts'] .= $ingredients;
$_SESSION['$alerts'] .= $directions;
$_SESSION['$alerts'] .= $image_name;
$_SESSION['$alerts'] .= $poster;
*/
$image_dir = 'images';
$image_dir_path = getcwd() . DIRECTORY_SEPARATOR . $image_dir;
$uploaded_image_name = upload_file('file1');
if (!$uploaded_image_name == null){
$image_name = $uploaded_image_name;
}
$problems = array();
if (empty($title)){
$problems['title'] = '*The title must be supplied.<br>';
}
if (empty($ingredients) or empty($directions)){
$problems['empty'] = '*You must enter the ingredients and directions.<br>';
}
// If problems are found send them back to be fixed
if (!empty($problems))
{
foreach ($problems as $problem){
$_SESSION['$alerts'] .= $problem;
}
header('Location: /view/create.php');
}
// If no problems, proceed with the database interaction
if
(empty($problems))
{
$insertWorked = updateContent($oldTitle, $title, $cooking_time, $difficulty, $ingredients, $directions, $image_name, $poster);
// Test what was returned from the model
if ($insertWorked)
{
$_SESSION['$alerts'] .= "The page has been created.";
} else {
// it failed
$_SESSION['$alerts'] .= "Sorry, an error occurred while attempting to update your page.";
}
header('Location: /view/list.php');
}
} else {
$_SESSION['$alerts'] .= "ACCESS DENIED.";
header('Location: /view/blank.php');
}
break;
case 'confirm_delete_content':
if ($_SESSION['admin']){
$_SESSION['confirm_id'] = $page_id;
header('Location: /view/confirm.php');
} else {
$_SESSION['$alerts'] .= "ACCESS DENIED.";
header('Location: /view/blank.php');
}
break;
case 'delete_content':
if ($_SESSION['admin']){
$isDeleted = deleteContent($page_id);
if($isDeleted){
$_SESSION['$alerts'] .= "Page Deleted";
} else {
$_SESSION['$alerts'] .= "An error occurred while attempting to delete the page.";
}
header('Location: /view/list.php');
} else {
$_SESSION['$alerts'] .= "ACCESS DENIED.";
header('Location: /view/blank.php');
}
break;
case 'assignments':
header('Location: /view/assignments.php');
break;
case 'content':
default:
buildContent($page_id);
break;
}
?>