-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
executable file
·583 lines (531 loc) · 15 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
<?php
require_once('PasswordHash.php');
session_start();
function create_event($name, $start, $end, $description, $type, $account = 0, $public = true, $hidden = false)
{
global $db;
$db->Prepare("SELECT UUID()");
$db->Execute();
$uuid = $db->Fetch();
$uuid = str_replace("-", "", implode($uuid, ""));
$uuid = substr($uuid, 0, 16);
$db->Prepare("INSERT INTO events (name,range_start,range_end,hash_code,description,type,account,public,hidden) VALUES ('$0','" . $start . " 00:00:00'" . ",'" . $end . " 23:59:00', '$1', '$2', '$3', '$4', '$5', '$6')");
$db->Execute($name, $uuid, $description, $type, $account, ($public ? 1 : 0), ($hidden ? 1 : 0));
return $uuid;
}
function get_event($hash)
{
global $db;
$db->Prepare("SELECT * FROM events WHERE hash_code='$0'");
$db->Execute($hash);
return $db->Fetch();
}
function edit_event($id, $name, $description)
{
global $db;
$db->Prepare("UPDATE events SET name='$0', description='$1' WHERE id='$2'");
$db->Execute($name, $description, $id);
}
function put_schedule($name, $ranges, $event, $account = 0, $text = "", $hash = "")
{
global $db;
if ($hash == "")
{
$db->Prepare("SELECT * FROM users WHERE event_id='$0' AND account='$1'");
$db->Execute($event, $account);
}
else
{
$db->Prepare("SELECT * FROM users WHERE id IN (SELECT completed FROM invites WHERE event_id='$0' AND hash='$1')");
$db->Execute($event, $hash);
}
$id = "";
if ($db->RowCount() == 0 || ($hash == "" && $account == 0))
{
$db->Prepare("INSERT INTO users (event_id, name, account, entry) VALUES ('$0', '$1', '$2', '$3')");
$db->Execute($event, $name, $account, $text);
$db->Prepare("SELECT LAST_INSERT_ID()");
$db->Execute();
$id = $db->Fetch();
$id = implode($id, "");
}
else
{
$id = $db->Fetch()["id"];
$db->Prepare("UPDATE users SET entry='$0' WHERE id='$1'");
$db->Execute($text, $id);
$db->Prepare("DELETE FROM schedules WHERE user_id='$0'");
$db->Execute($id);
}
foreach($ranges as $r)
{
$start = date("Y-m-d H:i:s", $r[0]);
$end = date("Y-m-d H:i:s", $r[1]);
$db->Prepare("INSERT INTO schedules (user_id, event_id, range_start, range_end) VALUES ('$0', '$1', '$2', '$3')");
$db->Execute($id, $event, $start, $end);
}
generate_event_intervals($event);
return $id;
}
function get_users($event)
{
global $db;
$db->Prepare("SELECT * FROM users WHERE event_id='$0' ORDER BY name");
$db->Execute($event);
$ret = array();
while($result = $db->Fetch())
{
array_push($ret, $result);
}
return $ret;
}
function get_schedule($user, $event)
{
global $db;
$db->Prepare("SELECT * FROM schedules WHERE event_id='$0' AND user_id='$1' ORDER BY range_start");
$db->Execute($event, $user);
$ret = array();
while($result = $db->Fetch())
{
array_push($ret, $result);
}
return $ret;
}
function generate_event_intervals($event)
{
global $db;
$db->Prepare("DELETE FROM event_intervals WHERE event_id='$0'");
$db->Execute($event);
$db->Prepare("SELECT * FROM schedules WHERE event_id='$0'");
$db->Execute($event);
$intervals = array();
while($result = $db->Fetch())
{
$start = strtotime($result["range_start"]);
$end = strtotime($result["range_end"]);
if (!isset($intervals[$start]))
$intervals[$start] = array(array(), array());
if (!isset($intervals[$end]))
$intervals[$end] = array(array(), array());
array_push($intervals[$start][0], $result["user_id"]);
array_push($intervals[$end][1], $result["user_id"]);
}
$current = array();
$first = true;
$lastkey = "";
ksort($intervals);
$query = "INSERT INTO event_intervals (event_id, user_id, range_start, range_end) VALUES ";
foreach($intervals as $key => $value)
{
//echo date("m/d/Y H:i:s", $key) . ": ";
//print_r(array_values($value[0]));
//print_r(array_values($value[1]));
//echo "<br>";
if (!$first)
{
foreach($current as $k => $v)
{
if ($v == 1)
{
$query .= "('$event', '$k', '" . date("Y-m-d H:i:s", $lastkey) . "', '" . date("Y-m-d H:i:s", $key) . "'),";
}
}
}
foreach($value[0] as $user)
{
$current[$user] = 1;
}
foreach($value[1] as $user)
{
$current[$user] = 0;
}
$first = false;
$lastkey = $key;
}
$query = substr($query, 0, strlen($query) - 1);
$db->Prepare($query);
$db->Execute();
}
function get_event_intervals($event)
{
global $db;
$ret = array();
$db->Prepare("SELECT * FROM event_intervals WHERE event_id=$0 ORDER BY range_start");
$db->Execute($event);
while($result = $db->Fetch())
{
$key = strtotime($result["range_start"]);
if (!isset($ret[$key]))
{
$ret[$key] = array($result["range_start"], $result["range_end"], array());
}
$ret[$key][2][$result["user_id"]] = 1;
}
return array_values($ret);
}
function event_intervals_cmp($a, $b)
{
if (count($a[2]) == count($b[2]))
return event_intervals_sub_cmp($a, $b);
return (count($a[2]) > count($b[2])) ? -1 : 1;
}
function event_intervals_sub_cmp($a, $b)
{
if (strtotime($a[0]) == strtotime($b[0]))
return 0;
return (strtotime($a[0]) < strtotime($b[0])) ? -1 : 1;
}
function add_comment($name, $content, $event_id)
{
global $db;
$db->Prepare("INSERT INTO comments (name, content, event_id) VALUES ('$0', '$1', '$2')");
$db->Execute($name, $content, $event_id);
}
function get_comments($event_id)
{
global $db;
$db->Prepare("SELECT * FROM comments WHERE event_id='$0' ORDER BY timestamp DESC");
$db->Execute($event_id);
$ret = array();
while($result = $db->Fetch())
{
array_push($ret, $result);
}
return $ret;
}
function get_event_creator($event_id) // For centered events
{
global $db;
$db->Prepare("SELECT * FROM schedules WHERE event_id='$0'");
$db->Execute($event_id);
$id = $db->Fetch()["user_id"];
$db->Prepare("SELECT * FROM users WHERE id='$0'");
$db->Execute($id);
return $db->Fetch();
}
function get_schedules_for_event($event_id)
{
global $db;
$db->Prepare("SELECT * FROM schedules WHERE event_id='$0'");
$db->Execute($event_id);
$ret = array();
while($result = $db->Fetch())
{
array_push($ret, $result);
}
return $ret;
}
function add_user($name, $event, $account = 0)
{
global $db;
$db->Prepare("INSERT INTO users (event_id, name, account) VALUES ('$0', '$1', '$2')");
$db->Execute($event, $name, $account);
$db->Prepare("SELECT LAST_INSERT_ID()");
$db->Execute();
$id = $db->Fetch();
$id = implode($id, "");
return $id;
}
function add_vote($name, $intervals, $event_id, $account = 0, $hash = "")
{
global $db;
$user = false;
if ($hash == "")
$user = get_user_from_account($event_id, $account);
else
{
$db->Prepare("SELECT * FROM users WHERE id IN (SELECT completed FROM invites WHERE event_id='$0' AND hash='$1')");
$db->Execute($event_id, $hash);
if ($db->RowCount() > 0)
$user = $db->Fetch();
}
if ($user !== false && ($hash != "" || $account != 0))
{
$query = "UPDATE votes SET value = CASE interval_id ";
foreach($intervals as $i)
{
$query .= "WHEN '" . $i[0] . "' THEN '" . $i[1] . "' ";
}
$query .= "END WHERE user_id='$0'";
$db->Prepare($query);
$db->Execute($user["id"]);
return $user["id"];
}
else
{
$id = add_user($name, $event_id, $account);
$query = "INSERT INTO votes (interval_id,user_id,value) VALUES ";
foreach($intervals as $i)
{
$query .= "(" . $i[0] . ",$id," . $i[1] . "),";
}
$query = substr($query, 0, strlen($query) - 1);
$db->Prepare($query);
$db->Execute();
return $id;
}
}
function get_votes($user_id)
{
global $db;
$db->Prepare("SELECT * FROM votes WHERE user_id='$0'");
$db->Execute($user_id);
$ret = array();
while($result = $db->Fetch())
{
$ret[$result["interval_id"]] = $result["value"];
}
return $ret;
}
function login($email, $password)
{
global $db;
if(isset($_COOKIE['session']))
return 3;
$db->Prepare('SELECT * FROM `accounts` WHERE email=\'$0\'');
$db->Execute($email);
if($db->RowCount() <= 0)
return 1;
$row = $db->Fetch();
$hasher = new PasswordHash(8, false);
if(!$hasher->CheckPassword($password, $row['password']))
return 2;
if($row['activated'] == 0)
return 4;
$secure = $hasher->HashPassword($email . ':' . uniqid() . ':' . $password);
$db->Prepare('SELECT id FROM `sessions` WHERE user_id=\'$0\'');
$db->Execute($row['id']);
if($db->RowCount() <= 0)
{
$db->Prepare('INSERT INTO `sessions` (ip, session, user_id) VALUES (\'$0\', \'$1\', \'$2\')');
$db->Execute($_SERVER['REMOTE_ADDR'], $secure, $row['id']);
}
else
{
$sess_row = $db->Fetch();
$db->Prepare('UPDATE `sessions` SET ip=\'$0\', session=\'$1\', user_id=\'$2\' WHERE id=$3');
$db->Execute($_SERVER['REMOTE_ADDR'], $secure, $row['id'], $sess_row['id']);
}
setcookie('session', $secure);
return 0;
}
function load_session()
{
global $db;
global $account;
$account = false;
if(isset($_COOKIE['session']))
{
$db->Prepare('SELECT * FROM `sessions` WHERE session=\'$0\' LIMIT 1');
$db->Execute($_COOKIE['session']);
if($db->RowCount() <= 0)
{
setcookie('session', "", time() - 1);
}
else
{
$row = $db->Fetch();
$account = $row['user_id'];
}
}
}
function logout()
{
global $db;
if(isset($_COOKIE['session']))
{
$db->Prepare('DELETE FROM `sessions` WHERE session=\'$0\' LIMIT 1');
$db->Execute($_COOKIE['session']);
} else
return -1;
setcookie('session', "", time() - 1);
return 0;
}
function register($email, $password, $first, $last)
{
global $db;
$db->Prepare('SELECT id FROM `accounts` WHERE email=\'$0\'');
$db->Execute($email);
if($db->RowCount() > 0)
return -1;
$hasher = new PasswordHash(8, false);
$password = $hasher->HashPassword($password);
$firstname = ucfirst($first);
$lastname = ucfirst($last);
$db->Prepare("INSERT INTO accounts (email, password, first_name, last_name, activated) VALUES ('$0', '$1', '$2', '$3', '$4')");
$db->Execute(trim($email), $password, trim($firstname), trim($lastname), 0);
$db->Prepare("SELECT LAST_INSERT_ID()");
$db->Execute();
$id = $db->Fetch();
$id = implode($id, "");
$db->Prepare("SELECT UUID()");
$db->Execute();
$uuid = $db->Fetch();
$uuid = str_replace("-", "", implode($uuid, ""));
$uuid = substr($uuid, 0, 16);
$db->Prepare("INSERT INTO activation_keys (`key`, user_id) VALUES ('$0', '$1')");
$db->Execute($uuid, $id);
return $uuid;
}
function activate_account($key)
{
global $db;
$db->Prepare("SELECT user_id FROM activation_keys WHERE `key`='$0'");
$db->Execute($key);
if($db->RowCount() == 1)
{
$id = $db->Fetch();
$db->Prepare("UPDATE accounts SET activated='1' WHERE id='$0'");
$db->Execute($id["user_id"]);
$db->Prepare("DELETE FROM activation_keys WHERE `key`='$0'");
$db->Execute($key);
return $id;
}
return false;
}
function get_account_info($id)
{
global $db;
$db->Prepare("SELECT * FROM accounts WHERE id='$0'");
$db->Execute($id);
return $db->Fetch();
}
function get_entry($event_id, $account_id)
{
global $db;
$db->Prepare("SELECT entry FROM users WHERE event_id='$0' AND account='$1'");
$db->Execute($event_id, $account_id);
return $db->Fetch()["entry"];
}
function get_user_from_account($event_id, $account_id)
{
global $db;
$db->Prepare("SELECT * FROM users WHERE event_id='$0' AND account='$1'");
$db->Execute($event_id, $account_id);
if ($db->RowCount() > 0)
return $db->Fetch();
return false;
}
function get_events_by_account($account)
{
global $db;
$db->Prepare("SELECT * FROM events WHERE account='$0' ORDER BY id DESC");
$db->Execute($account);
$ret = array();
while($result = $db->Fetch())
{
array_push($ret, $result);
}
return $ret;
}
function get_number_responses($event)
{
global $db;
$db->Prepare("SELECT * FROM users WHERE event_id='$0'");
$db->Execute($event);
return $db->RowCount();
}
function get_involved_events($account)
{
global $db;
$db->Prepare("SELECT * FROM events WHERE id IN (SELECT event_id FROM users WHERE account='$0' ORDER BY id DESC) AND account != '$1'");
$db->Execute($account, $account);
$ret = array();
while($result = $db->Fetch())
{
array_push($ret, $result);
}
return $ret;
}
function add_invite($event, $email)
{
global $db;
$db->Prepare("SELECT * FROM invites WHERE email='$0' AND event_id='$1'");
$db->Execute($email, $event);
if ($db->RowCount() > 0)
return;
$db->Prepare("SELECT id FROM accounts WHERE email='$0'");
$db->Execute($email);
if ($db->RowCount() == 1)
{
$id = $db->Fetch()["id"];
$db->Prepare("INSERT INTO invites (event_id, account_id, email) VALUES ('$0', '$1', '$2')");
$db->Execute($event, $id, $email);
}
else
{
$db->Prepare("SELECT UUID()");
$db->Execute();
$uuid = $db->Fetch();
$uuid = str_replace("-", "", implode($uuid, ""));
$uuid = substr($uuid, 0, 16);
$db->Prepare("INSERT INTO invites (event_id, hash, email) VALUES ('$0', '$1', '$2')");
$db->Execute($event, $uuid, $email);
}
}
function validate_invite($event, $hash)
{
global $db;
$db->Prepare("SELECT * FROM invites WHERE event_id='$0' AND hash='$1'");
$db->Execute($event, $hash);
return $db->RowCount() > 0;
}
function validate_invite_account($event, $account)
{
global $db;
$db->Prepare("SELECT * FROM invites WHERE event_id='$0' AND account_id='$1'");
$db->Execute($event, $account);
return $db->RowCount() > 0;
}
function get_invite_user($event, $hash)
{
global $db;
$db->Prepare("SELECT * FROM users WHERE id IN (SELECT completed FROM invites WHERE event_id='$0' AND hash='$1')");
$db->Execute($event, $hash);
if ($db->RowCount() > 0)
return $db->Fetch();
return false;
}
function update_invite($event, $hash, $user)
{
global $db;
$db->Prepare("UPDATE invites SET completed='$0' WHERE event_id='$1' AND hash='$2'");
$db->Execute($user, $event, $hash);
}
function update_invite_account($event, $account, $user)
{
global $db;
$db->Prepare("UPDATE invites SET completed='$0' WHERE event_id='$1' AND account_id='$2'");
$db->Execute($user, $event, $account);
}
function get_invites($event, $pending = false)
{
global $db;
if ($pending)
$db->Prepare("SELECT * FROM invites WHERE event_id='$0' AND completed=0");
else
$db->Prepare("SELECT * FROM invites WHERE event_id='$0'");
$db->Execute($event);
$ret = array();
while($result = $db->Fetch())
{
array_push($ret, $result);
}
return $ret;
}
function get_invites_on_account($account, $pending = false)
{
global $db;
if ($pending)
$db->Prepare("SELECT * FROM events WHERE id IN (SELECT event_id FROM invites WHERE account_id='$0' AND completed=0)");
else
$db->Prepare("SELECT * FROM events WHERE id IN (SELECT event_id FROM invites WHERE account_id='$0')");
$db->Execute($account);
$ret = array();
while($result = $db->Fetch())
{
array_push($ret, $result);
}
return $ret;
}
?>