-
Notifications
You must be signed in to change notification settings - Fork 27
/
functions.php
599 lines (482 loc) · 17.4 KB
/
functions.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
<?php
// Configuration
function get_configuration($data)
{
$sql = ("SELECT * FROM " . global_mysql_configuration_table);
$configuration = DB::query_single_row($sql);
return($configuration[$data]);
}
// Password
function random_password()
{
$password = rand('1001', '9999');
return $password;
}
function encrypt_password($password)
{
$password = crypt($password, '$1$' . global_salt);
return($password);
}
function add_salt($password)
{
$password = '$1$' . substr(global_salt, 0, -1) . '$' . $password;
return($password);
}
function strip_salt($password)
{
$password = str_replace('$1$' . substr(global_salt, 0, -1) . '$', '', $password);
return($password);
}
// String manipulation
function modify_email($email)
{
$email = str_replace('@', '(at)', $email);
$email = str_replace('.', '(dot)', $email);
return($email);
}
// String validation
function validate_user_name($user_name)
{
if(preg_match('/^[a-z æøåÆØÅ]{2,12}$/i', $user_name))
{
return(true);
}
}
function validate_user_email($user_email)
{
if(filter_var($user_email, FILTER_VALIDATE_EMAIL) && strlen($user_email) < 51)
{
return(true);
}
}
function validate_user_password($user_password)
{
if(strlen($user_password) > 3 && trim($user_password) != '')
{
return(true);
}
}
function validate_price($price)
{
if(is_numeric($price))
{
return(true);
}
}
// User validation
function user_name_exists($user_name)
{
$sql = ("SELECT * FROM " . global_mysql_users_table . " WHERE user_name='$user_name'") ;
$res = DB::query_all($sql);
if(count($res) > 0)
{
return(true);
}
}
function user_email_exists($user_email)
{
$query = ("SELECT * FROM " . global_mysql_users_table . " WHERE user_email='$user_email'") ;
$res = DB::query_all($query);
if(count($res) > 0)
{
return(true);
}
}
// Login
function get_login_data($data)
{
if($data == 'user_email' && isset($_COOKIE[global_cookie_prefix . '_user_email']))
{
return($_COOKIE[global_cookie_prefix . '_user_email']);
}
elseif($data == 'user_password' && isset($_COOKIE[global_cookie_prefix . '_user_password']))
{
return($_COOKIE[global_cookie_prefix . '_user_password']);
}
}
function login($user_email, $user_password, $user_remember)
{
$user_password_encrypted = encrypt_password($user_password);
$user_password = add_salt($user_password);
$query = ("SELECT * FROM " . global_mysql_users_table . " WHERE user_email='$user_email' AND user_password='$user_password_encrypted' OR user_email='$user_email' AND user_password='$user_password'");
$all = DB::query_all($query);
if(count($all) == 1)
{
$user = $all[0];
$_SESSION['user_id'] = $user['user_id'];
$_SESSION['user_is_admin'] = $user['user_is_admin'];
$_SESSION['user_email'] = $user['user_email'];
$_SESSION['user_name'] = $user['user_name'];
$_SESSION['user_reservation_reminder'] = $user['user_reservation_reminder'];
$_SESSION['logged_in'] = '1';
if($user_remember == '1')
{
$user_password = strip_salt($user['user_password']);
setcookie(global_cookie_prefix . '_user_email', $user['user_email'], time() + 3600 * 24 * intval(global_remember_login_days));
setcookie(global_cookie_prefix . '_user_password', $user_password, time() + 3600 * 24 * intval(global_remember_login_days));
}
return(1);
}
}
function check_login()
{
if(isset($_SESSION['logged_in']))
{
$user_id = $_SESSION['user_id'];
$query = ("SELECT * FROM " . global_mysql_users_table . " WHERE user_id='$user_id'");
$all = DB::query_all($query);
if(count($all) == 1)
{
return(true);
}
else
{
logout();
echo '<script type="text/javascript">window.location.replace(\'.\');</script>';
}
}
else
{
logout();
echo '<script type="text/javascript">window.location.replace(\'.\');</script>';
}
}
function logout()
{
session_unset();
setcookie(global_cookie_prefix . '_user_email', '', time() - 3600);
setcookie(global_cookie_prefix . '_user_password', '', time() - 3600);
}
function create_user($user_name, $user_email, $user_password, $user_secret_code)
{
if(validate_user_name($user_name) != true)
{
return('<span class="error_span">Name must be <u>letters only</u> and be <u>2 to 12 letters long</u>. If your name is longer, use a short version of your name</span>');
}
elseif(validate_user_email($user_email) != true)
{
return('<span class="error_span">Email must be a valid email address and be no more than 50 characters long</span>');
}
elseif(validate_user_password($user_password) != true)
{
return('<span class="error_span">Password must be at least 4 characters</span>');
}
elseif(global_secret_code != '0' && $user_secret_code != global_secret_code)
{
return('<span class="error_span">Wrong secret code</span>');
}
elseif(user_name_exists($user_name) == true)
{
return('<span class="error_span">Name is already in use. If you have the same name as someone else, use another spelling that identifies you</span>');
}
elseif(user_email_exists($user_email) == true)
{
return('<span class="error_span">Email is already registered. <a href="#forgot_password">Forgot your password?</a></span>');
}
else
{
$query = ("SELECT * FROM " . global_mysql_users_table . "");
$all = DB::query_all($query);
if(count($all) == 1)
{
$user_is_admin = '1';
}
else
{
$user_is_admin = '0';
}
$user_password = encrypt_password($user_password);
db::query("INSERT INTO " . global_mysql_users_table . " (user_is_admin,user_email,user_password,user_name,user_reservation_reminder) VALUES ($user_is_admin,'$user_email','$user_password','$user_name','0')");
$user_password = strip_salt($user_password);
setcookie(global_cookie_prefix . '_user_email', $user_email, time() + 3600 * 24 * intval(global_remember_login_days));
setcookie(global_cookie_prefix . '_user_password', $user_password, time() + 3600 * 24 * intval(global_remember_login_days));
return(1);
}
}
function list_admin_users()
{
$query = mysql_query("SELECT * FROM " . global_mysql_users_table . " WHERE user_is_admin='1' ORDER BY user_name");
if(count($query) > 0)
{
return('<span class="error_span">There are no admins</span>');
}
else
{
$return = '<table id="forgot_password_table"><tr><th>Name</th><th>Email</th></tr>';
$i = 0;
foreach($query as $user)
{
$i++;
$return .= '<tr><td>' . $user['user_name'] . '</td><td><span id="email_span_' . $i . '"></span></td></tr><script type="text/javascript">$(\'#email_span_' . $i . '\').html(\'<a href="mailto:\'+$.base64.decode(\'' . base64_encode($user['user_email']) . '\')+\'">\'+$.base64.decode(\'' . base64_encode($user['user_email']) . '\')+\'</a>\');</script>';
}
$return .= '</table>';
return($return);
}
}
// Reservations
function highlight_day($day)
{
$day = str_ireplace(global_day_name, '<span id="today_span">' . global_day_name . '</span>', $day);
return $day;
}
function read_reservation($week, $day, $time)
{
$query = ("SELECT * FROM " . global_mysql_reservations_table . " WHERE reservation_week='$week' AND reservation_day='$day' AND reservation_time='$time'");
$reservation = DB::query_single_row($query);
if (!$reservation ) return "";
return($reservation['reservation_user_name']);
}
function read_reservation_details($week, $day, $time)
{
$reservation = DB::query_single_row("SELECT * FROM " . global_mysql_reservations_table . " WHERE reservation_week='$week' AND reservation_day='$day' AND reservation_time='$time'");
if(empty($reservation))
{
return(0);
}
else
{
return('<b>Reservation faite le:</b> ' . $reservation['reservation_made_time'] . '<br><b>Email de l\'utilisateur:</b> ' . $reservation['reservation_user_email']);
}
}
function make_reservation($week, $day, $time)
{
$user_id = $_SESSION['user_id'];
$user_email = $_SESSION['user_email'];
$user_name = $_SESSION['user_name'];
$price = global_price;
if($week < global_week_number && $_SESSION['user_is_admin'] != '1' || $week == global_week_number && $day < global_day_number && $_SESSION['user_is_admin'] != '1')
{
return('You can\'t reserve back in time');
}
elseif($week > global_week_number + global_weeks_forward && $_SESSION['user_is_admin'] != '1')
{
return('You can only reserve ' . global_weeks_forward . ' weeks forward in time');
}
else
{
$query = DB::query_all("SELECT * FROM " . global_mysql_reservations_table . " WHERE reservation_week='$week' AND reservation_day='$day' AND reservation_time='$time'");
if(count($query) < 1)
{
$year = global_year;
$week_start = new DateTimeFrench();
$week_start->setISODate($year,$week);
$week_start->add(new DateInterval("P".($day-1)."D"));
// $datefull=$week_start->format('l j F Y');
$datefull=$week_start->format('Y-m-d');
// echo $datefull;
DB::query("INSERT INTO " . global_mysql_reservations_table . " (reservation_made_time,reservation_year,reservation_week,reservation_day,reservation_date,reservation_time,reservation_price,reservation_user_id,reservation_user_email,reservation_user_name) VALUES (datetime('now','localtime'),'$year','$week','$day','$datefull','$time','$price','$user_id','$user_email','$user_name')");
return(1);
}
else
{
return('Someone else just reserved this time');
}
}
}
function delete_reservation($week, $day, $time)
{
if($week < global_week_number && $_SESSION['user_is_admin'] != '1' || $week == global_week_number && $day < global_day_number && $_SESSION['user_is_admin'] != '1')
{
return('You can\'t reserve back in time');
}
elseif($week > global_week_number + global_weeks_forward && $_SESSION['user_is_admin'] != '1')
{
return('You can only reserve ' . global_weeks_forward . ' weeks forward in time');
}
else
{
$user = DB::query_single_row("SELECT * FROM " . global_mysql_reservations_table . " WHERE reservation_week='$week' AND reservation_day='$day' AND reservation_time='$time'");
if($user['reservation_user_id'] == $_SESSION['user_id'] || $_SESSION['user_is_admin'] == '1')
{
DB::query("DELETE FROM " . global_mysql_reservations_table . " WHERE reservation_week='$week' AND reservation_day='$day' AND reservation_time='$time'");
return(1);
}
else
{
return('You can\'t remove other users\' reservations');
}
}
}
// Admin control panel
function list_users()
{
$query = DB::query("SELECT * FROM " . global_mysql_users_table . " ORDER BY user_is_admin DESC, user_name");
$users = '<table id="users_table"><tr><th>ID</th><th>Admin</th><th>Name</th><th>Email</th><th>Reminders</th><th>Usage</th><th></th></tr>';
foreach($query as $user)
{
$users .= '<tr id="user_tr_' . $user['user_id'] . '"><td><label for="user_radio_' . $user['user_id'] . '">' . $user['user_id'] . '</label></td><td>' . $user['user_is_admin'] . '</td><td><label for="user_radio_' . $user['user_id'] . '">' . $user['user_name'] . '</label></td><td><label for="user_radio_' . $user['user_id'] . '">' . $user['user_email'] . '</label></td><td>' . $user['user_reservation_reminder'] . '</td><td>' . count_reservations($user['user_id']) . '</td><td>' . '</td><td><input type="radio" name="user_radio" class="user_radio" id="user_radio_' . $user['user_id'] . '" value="' . $user['user_id'] . '"></td></tr>';
}
$users .= '</table>';
return($users);
}
function reset_user_password($user_id)
{
$password = random_password();
$password_encrypted = encrypt_password($password);
DB::query("UPDATE " . global_mysql_users_table . " SET user_password='$password_encrypted' WHERE user_id='$user_id'");
if($user_id == $_SESSION['user_id'])
{
return(0);
}
else
{
return('The password to the user with ID ' . $user_id . ' is now "' . $password . '". The user can now log in and change the password');
}
}
function change_user_permissions($user_id)
{
if($user_id == $_SESSION['user_id'])
{
return('<span class="error_span">Sorry, you can\'t use your superuser powers to remove them</span>');
}
else
{
DB::query("UPDATE " . global_mysql_users_table . " SET user_is_admin = 1 - user_is_admin WHERE user_id='$user_id'");
return(1);
}
}
function delete_user_data($user_id, $data)
{
if($user_id == $_SESSION['user_id'] && $data != 'reservations')
{
return('<span class="error_span">Sorry, self-destructive behaviour is not accepted</span>');
}
else
{
if($data == 'reservations')
{
DB::query("DELETE FROM " . global_mysql_reservations_table . " WHERE reservation_user_id='$user_id'");
}
elseif($data == 'user')
{
DB::query("DELETE FROM " . global_mysql_users_table . " WHERE user_id='$user_id'");
DB::query("DELETE FROM " . global_mysql_reservations_table . " WHERE reservation_user_id='$user_id'");
}
return(1);
}
}
function delete_all($data)
{
$user_id = $_SESSION['user_id'];
if($data == 'reservations')
{
DB::query("DELETE FROM " . global_mysql_reservations_table . " WHERE reservation_user_id!='$user_id'");
}
elseif($data == 'users')
{
DB::query("DELETE FROM " . global_mysql_users_table . " WHERE user_id!='$user_id'");
DB::query("DELETE FROM " . global_mysql_reservations_table . " WHERE reservation_user_id!='$user_id'");
}
elseif($data == 'everything')
{
DB::query("DELETE FROM " . global_mysql_users_table . "");
DB::query("DELETE FROM " . global_mysql_reservations_table . "");
}
return(1);
}
function save_system_configuration($price)
{
if(validate_price($price) != true)
{
return('<span class="error_span">Price must be a number (use . and not , if you want to use decimals)</span>');
}
else
{
DB::query("UPDATE " . global_mysql_configuration_table . " SET price='$price'");
}
return(1);
}
// User control panel
function get_usage()
{
$user_id = $_SESSION['user_id'];
$query = DB::query("SELECT * FROM " . global_mysql_reservations_table . " WHERE reservation_user_id='$user_id'");
//
$users = '<table id="usage_table"><tr><th>Reservations</th><th>When</th></tr>';
foreach($query as $user)
{
$users .= '<tr><td>' . $user['reservation_date'] . '</label></td><td>' . $user['reservation_time'] . '</td>'.
'</tr>';
}
$users .= '</table>';
return($users);
}
function count_reservations($user_id)
{
$count = DB::query_single_field("SELECT count(*) FROM " . global_mysql_reservations_table . " WHERE reservation_user_id='$user_id'");
// $count = mysql_num_rows($query);
return($count);
}
function cost_reservations($user_id)
{
$query = DB::query("SELECT * FROM " . global_mysql_reservations_table . " WHERE reservation_user_id='$user_id'");
$cost = 0;
foreach($query as $reservation)
{
$cost =+ $cost + $reservation['reservation_price'];
}
return($cost);
}
function get_reservation_reminders()
{
$user_id = $_SESSION['user_id'];
$user = DB::query_single_row("SELECT * FROM " . global_mysql_users_table . " WHERE user_id='$user_id'");
if($user['user_reservation_reminder'] == 1)
{
$return = '<input type="checkbox" id="reservation_reminders_checkbox" checked="checked">';
}
else
{
$return = '<input type="checkbox" id="reservation_reminders_checkbox">';
}
return($return);
}
function toggle_reservation_reminder()
{
$user_id = $_SESSION['user_id'];
DB::query("UPDATE " . global_mysql_users_table . " SET user_reservation_reminder = 1 - user_reservation_reminder WHERE user_id='$user_id'");
return(1);
}
function change_user_details($user_name, $user_email, $user_password)
{
$user_id = $_SESSION['user_id'];
if(validate_user_name($user_name) != true)
{
return('<span class="error_span">Name must be <u>letters only</u> and be <u>2 to 12 letters long</u>. If your name is longer, use a short version of your name</span>');
}
if(validate_user_email($user_email) != true)
{
return('<span class="error_span">Email must be a valid email address and be no more than 50 characters long</span>');
}
elseif(validate_user_password($user_password) != true && !empty($user_password))
{
return('<span class="error_span">Password must be at least 4 characters</span>');
}
elseif(user_name_exists($user_name) == true && $user_name != $_SESSION['user_name'])
{
return('<span class="error_span">Name is already in use. If you have the same name as someone else, use another spelling that identifies you</span>');
}
elseif(user_email_exists($user_email) == true && $user_email != $_SESSION['user_email'])
{
return('<span class="error_span">Email is already registered</span>');
}
else
{
if(empty($user_password))
{
DB::query("UPDATE " . global_mysql_users_table . " SET user_name='$user_name', user_email='$user_email' WHERE user_id='$user_id'");
}
else
{
$user_password = encrypt_password($user_password);
DB::query("UPDATE " . global_mysql_users_table . " SET user_name='$user_name', user_email='$user_email', user_password='$user_password' WHERE user_id='$user_id'");
}
DB::query("UPDATE " . global_mysql_reservations_table . " SET reservation_user_name='$user_name', reservation_user_email='$user_email' WHERE reservation_user_id='$user_id'");
$_SESSION['user_name'] = $user_name;
$_SESSION['user_email'] = $user_email;
$user_password = strip_salt($user_password);
setcookie(global_cookie_prefix . '_user_email', $user_email, time() + 3600 * 24 * intval(global_remember_login_days));
setcookie(global_cookie_prefix . '_user_password', $user_password, time() + 3600 * 24 * intval(global_remember_login_days));
return(1);
}
}