This repository has been archived by the owner on Nov 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.php
643 lines (505 loc) · 12.4 KB
/
user.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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
<?php
if (!defined("APP_LOADED")) {
die();
}
/**
* User and account related things
*/
class Token {
public $name; // Name of the token
public $user; // Name of the user
public $created; // Time the user logged in
public $expire; // Expiration date of the token
public $key; // IP the token was created under
function __construct(string $id = null) {
$db = new Database("token");
// Generate new token name
// We just reroll until we get an unused one
if (!$id) {
do {
$id = random_hex();
} while ($db->has($id));
}
// Load an existing token
if ($db->has($id)) {
$token = $db->load($id);
$this->id = $token->id;
$this->user = $token->user;
$this->created = $token->created;
$this->expire = $token->expire;
$this->key = property_exists($token, "key") ? $token->key : "";
}
// Create a new token
else {
$this->id = $id;
$this->user = null;
$this->created = time();
$this->expire = time() + 60 * 60 * 24 * 7 * 2; // Expire in 2 weeks
$this->key = "";
}
}
function save() {
$db = new Database("token");
$db->save($this->id, $this);
}
function delete() {
/**
* Delete the token so it can't be used anymore.
*/
$db = new Database("token");
if ($db->has($this->id)) {
$db->delete($this->id);
}
}
function set_user(string $user) {
/**
* Set who the token is for if not already set. We don't allow changing
* the name once it is set for safety reasons.
*
* This returns the name of the issued token if it works.
*/
if ($this->user == null) {
$this->user = $user;
$db = new Database("token");
$db->save($this->id, $this);
return $this->id;
}
return null;
}
function get_user(?string $key = null, bool $require_key = false) {
/**
* Get the username with a token, or null if the token can't be used.
* This will also verify the key if given.
*
* Lockboxes are not nessicarially enforced here; if you pass in NULL
* then the LB isn't checked unless $require_key == true. This is
* kind of legacy code.
*
* TODO Make it not work this way
*/
// Not initialised
if ($this->user == null) {
return null;
}
// Expired
if (time() >= $this->expire) {
return null;
}
// Too early
if (time() < $this->created) {
return null;
}
// Check the key
$lbok = $this->verify_key($key);
if (($key || $require_key) && !$lbok) {
return null;
}
// probably okay to use
return $this->user;
}
function get_id() : string {
return $this->id;
}
function make_key() : string {
/**
* Create a key value and store its hash
*/
$key = random_hex();
$this->key = hash("sha256", $key);
$this->save();
return $key;
}
function verify_key(?string $key) : bool {
/**
* Verify that a key matches
*/
return ($key) && (hash("sha256", $key) === $this->key);
}
}
function get_yt_image(string $handle) : string {
/**
* Get the URL of the user's YouTube profile picture.
*/
try {
$ytpage = @file_get_contents("https://youtube.com/@$handle/featured");
if (!$ytpage) {
return "";
}
$before = "<meta property=\"og:image\" content=\"";
if ($before < 0) {
return "";
}
// Carve out anything before this url
$i = strpos($ytpage, $before);
$ytpage = substr($ytpage, $i + strlen($before));
// Carve out anything after this url
$i = strpos($ytpage, "\"");
$ytpage = substr($ytpage, 0, $i);
// We have the string!!!
return $ytpage;
}
catch (Exception $e) {
return "";
}
}
function get_gravatar_image(string $email, string $default = "identicon") : string {
/**
* Get a gravatar image URL.
*/
return "https://www.gravatar.com/avatar/" . md5(strtolower(trim($email))) . "?s=300&d=$default";
}
function has_gravatar_image(string $email) {
/**
* Check if an email has a gravatar image
*/
return !!(@file_get_contents(get_gravatar_image($email, "404")));
}
function find_pfp($user) : string | null {
/**
* One time find a user's pfp url
*/
switch ($user->image_type) {
case "gravatar": {
return get_gravatar_image($user->email);
}
case "youtube": {
return get_yt_image($user->youtube);
}
default: {
return "./avatar_default.png";
}
}
}
function generate_new_user_id() {
/**
* Generate a new, original user ID.
*/
$id = null;
$db = new Database("user");
while ($id == null) {
$id = random_base32(20);
if ($db->has($id)) {
$id = null;
}
}
return $id;
}
#[AllowDynamicProperties]
class User {
/**
* Represents a user and most of the state that comes with that. Unforunately,
* the decision to combine everything into one big table was made early on, and
* is not a mistake I would repeat, though switching to something better would
* require some effort.
*/
function __construct(string $id) {
$db = new Database("user");
if ($db->has($id)) {
$info = $db->load($id);
$this->id = $info->id;
$this->handle = $info->handle;
$this->display = $info->display;
$this->pronouns = $info->pronouns;
$this->password = $info->password;
$this->tokens = $info->tokens;
$this->email = $info->email;
$this->created = $info->created;
$this->login_wait = $info->login_wait;
$this->verified = $info->verified;
$this->image_type = $info->image_type;
$this->image = $info->image;
$this->roles = $info->roles;
$this->sak = $info->sak;
}
else {
$this->id = $id;
$this->handle = $id;
$this->display = "New User";
$this->pronouns = "";
$this->password = null;
$this->tokens = [];
$this->email = "";
$this->created = time();
$this->login_wait = 0;
$this->verified = null;
$this->image_type = "gravatar";
$this->image = "";
$this->roles = [];
$this->sak = random_hex();
}
}
function save() : void {
$db = new Database("user");
$db->save($this->id, $this);
}
function wipe_tokens() : array {
/**
* Delete any active tokens this user has. Also returns ip's in the
* tokens.
*/
$tdb = new Database("token");
$ips = [];
for ($i = 0; $i < sizeof($this->tokens); $i++) {
if ($tdb->has($this->tokens[$i])) {
$token = new Token($this->tokens[$i]);
$ips[] = $token->ip;
$tdb->delete($this->tokens[$i]);
}
}
$this->tokens = [];
return $ips;
}
function delete() : void {
/**
* Delete the user
*/
// Wipe tokens
$this->wipe_tokens();
// Delete the user
$db = new Database("user");
$db->delete($this->id);
}
function is_verified() : bool {
return ($this->verified != null);
}
function verify_sak(string $key) : bool {
/**
* Verify that the SAK is okay, and generate the next one.
*/
if ($this->sak == $key) {
$this->sak = random_hex();
$this->save();
return true;
}
else {
return false;
}
}
function get_sak() : string {
/**
* Get the current SAK.
*/
return $this->sak;
}
function clean_foreign_tokens() : void {
/**
* Clean the any tokens this user claims to have but does not
* actually have.
*/
$db = new Database("token");
$valid = array();
// TODO Yes, I really shouldn't work with database primitives here, but
// I can't find what I called the standard functions to do this stuff.
for ($i = 0; $i < sizeof($this->tokens); $i++) {
if ($db->has($this->tokens[$i])) {
$token = new Token($this->tokens[$i]);
if ($token->get_user() === $this->id) {
// It should be a good token.
$valid[] = $this->tokens[$i];
}
else {
// It's a dirty one!
$token->delete();
}
}
}
$this->tokens = $valid;
}
function set_password(string $password) : bool {
/**
* Set the user's password.
*
* @return False on failure, true on success
*/
$this->password = password_hash($password, PASSWORD_ARGON2I);
return true;
}
function new_password() : string {
/**
* Generate a new password for this user.
*
* @return The plaintext password is returned and a hashed value is
* stored.
*/
$password = @random_password();
$this->set_password($password);
return $password;
}
function set_email(string $email) : void {
/**
* Set the email for this user.
*/
$this->email = $email;
}
function authinticate(string $password) : bool {
/**
* Check the stored password against the given password.
*/
return password_verify($password, $this->password);
}
function make_token() {
/**
* Make a token assigned to this user
*/
$token = new Token();
$name = $token->set_user($this->id);
$this->tokens[] = $name;
$this->save();
return $token;
}
function login_rate_limited() {
/**
* Check if the user's login should be denied because they are trying
* to log in too soon after trying a first time.
*/
// The login isn't allowed if they have logged in too recently.
if ($this->login_wait >= time()) {
return true;
}
// It has been long enough to allow, also reset the counter.
$this->login_wait = time() + 15;
$this->save();
return false;
}
function issue_token(string $password, string $mfa = null) {
/**
* Given the password and MFA string, add a new token for this user
* and return its name.
*/
// Deny requests coming too soon
if ($this->login_rate_limited()) {
return null;
}
// First, run maintanance
$this->clean_foreign_tokens();
// Check the password
if (!$this->authinticate($password)) {
return null;
}
// Create a new token
$token = $this->make_token();
return $token;
}
function verify(?string $verifier) : void {
$this->verified = $verifier;
$this->save();
}
function is_admin() : bool {
/**
* Check if the user can preform administrative tasks.
*/
return $this->has_role("admin");
}
function is_mod() : bool {
/**
* Check if the user can preform moderation tasks.
*/
return $this->has_role("mod") || $this->is_admin();
}
function get_display() : string {
return $this->display ? $this->display : $this->id;
}
function set_roles(array $roles) : void {
/**
* Set the user's roles
*/
$this->roles = $roles;
$this->save();
}
function add_role(string $role) : void {
if (array_search($role, $this->roles) === false) {
$this->roles[] = $role;
}
$this->save();
}
function remove_role(string $role) : void {
$index = array_search($role, $this->roles);
if ($index !== false) {
array_splice($this->roles, $index, 1);
}
$this->save();
}
function has_role(string $role) : bool {
/**
* Check if the user has a certian role
*/
return (array_search($role, $this->roles) !== false);
}
function count_roles() : int {
/**
* Get the number of roles this user has
*/
return sizeof($this->roles);
}
function get_role_score() : int {
/**
* Get a number assocaited with a user's highest role.
*/
$n = 0;
for ($i = 0; $i < sizeof($this->roles); $i++) {
switch ($this->roles[$i]) {
case "mod": max($n, 1); break;
case "admin": max($n, 2); break;
case "headmaster": max($n, 3); break;
default: break;
}
}
return $n;
}
}
function user_exists(string $id) : bool {
/**
* Check if a user exists in the database given their user ID.
*/
$db = new Database("user");
return $db->has($id);
}
function check_token__(string $name, string $key) {
/**
* Given the name of the token, get the user's assocaited name and the token
* key, or null if the token is not valid.
*/
$token = new Token($name);
return $token->get_user($key, true);
}
function user_get_current() {
/**
* Get the current user
*/
if (!array_key_exists("token", $_COOKIE) || !array_key_exists("key", $_COOKIE)) {
return null;
}
$name = check_token__($_COOKIE["token"], $_COOKIE["key"]);
return ($name) ? (new User($name)) : null;
}
function user_lookup_handle(?string $user_id) : ?string {
/**
* Find a user's handle given their user ID.
*/
if ($user_id === null) {
return null;
}
if (!user_exists($user_id)) {
return null;
}
$user = new User($user_id);
return $user->handle;
}
function user_lookup_id(?string $handle) : ?string {
/**
* Give an user's handle, find their user ID
*/
$db = new Database("user");
return $db->where_one(["handle" => $handle]);
}
function user_get_from_handle(?string $handle) : ?User {
if (!$handle) {
return null;
}
$user_id = user_lookup_id($handle);
if (!$user_id) {
return null;
}
$user = new User($user_id);
return $user;
}