forked from SimpleMachines/SMF
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.php
More file actions
5039 lines (4341 loc) · 152 KB
/
User.php
File metadata and controls
5039 lines (4341 loc) · 152 KB
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Simple Machines Forum (SMF)
*
* @package SMF
* @author Simple Machines https://www.simplemachines.org
* @copyright 2025 Simple Machines and individual contributors
* @license https://www.simplemachines.org/about/smf/license.php BSD
*
* @version 3.0 Alpha 4
*/
declare(strict_types=1);
namespace SMF;
use SMF\Actions\Admin\ACP;
use SMF\Actions\Admin\Bans;
use SMF\Actions\Login2;
use SMF\Actions\Logout;
use SMF\Actions\Moderation\ReportedContent;
use SMF\Cache\CacheApi;
use SMF\Db\DatabaseApi as Db;
use SMF\Permissions\Permission;
use SMF\Permissions\PermissionProfile;
use SMF\Permissions\UserPermissionSet;
use SMF\PersonalMessage\PM;
/**
* Represents a user, including both guests and registered members.
*
* All loaded users are available via User::$loaded[$id], where $id is the ID
* number of a user.
*
* The current user is available as User::$me. For example, if you need to know
* the current user's ID number, use User::$me->id.
*
* For the convenience of theme creators, User::$me is also available as
* Utils::$context['user'], and its properties can be accessed as if they were
* array elements. This means that Utils::$context['user']['id'] is
* interchangeable with User::$me->id.
*
* The data previously available in the deprecated global $user_profile array
* is now available as User::$profiles. For example, where old code might have
* used $user_profile[$id_member]['last_login'], the same information is now
* available as User::profiles[$id_member]['last_login'].
*
* The data previously available in the deprecated $memberContext array is now
* available via the $formatted property of a User object. For example, where
* old code might have used $memberContext[$id_member], the same information is
* now available via User::$loaded[$id_member]->formatted. Also note that, in
* the same way that loadMemberContext($id_member) had to be called in order to
* populate $memberContext[$id_member], User::$loaded[$id_member]->format() must
* be called in order to populate User::$loaded[$id_member]->formatted.
*
* To facilitate backward compatibility, the deprecated global $user_info array
* is still available, but it is simply a reference to User::$me.
*
* Similarly, the deprecated global $user_settings array is still available, but
* it is simply a reference to User::$profiles[User::$me->id].
*
* Similarly, the deprecated global $cur_profile array is still available, but
* it is simply a reference to User::$profiles[$id], where $id is the ID of the
* user whose profile is being viewed.
*
* NOTE: It is STRONGLY RECOMMENDED that new and updated code use User::$me,
* User::$loaded, and User::$profiles directly, rather than using any of the
* deprecated global variables. A future version of SMF will remove backward
* compatibility support for these deprecated globals.
*/
class User implements \ArrayAccess
{
use ArrayAccessHelper;
use BackwardCompatibility;
/*****************
* Class constants
*****************/
/**
* Constants to define loading methods.
*/
public const LOAD_BY_ID = 0;
public const LOAD_BY_NAME = 1;
public const LOAD_BY_EMAIL = 2;
/**
* Constants to define activation states.
*/
public const NOT_ACTIVATED = 0;
public const ACTIVATED = 1;
public const UNVALIDATED = 2;
public const UNAPPROVED = 3;
public const REQUESTED_DELETE = 4;
public const NEED_COPPA = 5;
public const REQUESTED_DELETE_ANONYMIZE = 6;
public const BANNED = 10;
public const ACTIVATED_BANNED = 11;
public const UNVALIDATED_BANNED = 12;
public const UNAPPROVED_BANNED = 13;
public const REQUESTED_DELETE_BANNED = 14;
public const NEED_COPPA_BANNED = 15;
public const REQUESTED_DELETE_ANONYMIZE_BANNED = 16;
/*******************
* Public properties
*******************/
/**
* @var int
*
* The user's ID number.
*/
public int $id;
/**
* @var string
*
* The user's member_name.
*/
public string $username;
/**
* @var string
*
* The user's real_name, a.k.a display name.
*/
public string $name;
/**
* @var string
*
* The user's email address.
*/
public string $email;
/**
* @var string
*
* The user's password.
*/
public string $passwd;
/**
* @var string
*
* The user's password salt.
*/
public string $password_salt;
/**
* @var string
*
* The user's two factor authentication secret.
*/
public string $tfa_secret;
/**
* @var string
*
* The user's two factor authentication backup code.
*/
public string $tfa_backup;
/**
* @var string
*
* The user's secret question (used for password resets).
*/
public string $secret_question;
/**
* @var string
*
* Answer to the user's secret question (used for password resets).
*/
public string $secret_answer;
/**
* @var string
*
* The user's validation code (used for password resets).
*/
public string $validation_code;
/**
* @var int
*
* ID of this user's primary group.
*/
public int $group_id;
/**
* @var int
*
* ID of this user's post-count based group.
*/
public int $post_group_id;
/**
* @var array
*
* IDs of any additional groups this user belongs to.
*/
public array $additional_groups = [];
/**
* @var array
*
* IDs of all the groups this user belongs to.
*/
public array $groups = [];
/**
* @var bool
*
* If true, probably a search engine spider.
*/
public bool $possibly_robot;
/**
* @var bool
*
* Whether this user is a guest.
*/
public bool $is_guest;
/**
* @var bool
*
* Whether this user is an admin.
*/
public bool $is_admin;
/**
* @var bool
*
* Whether this user is a moderator on the current board.
*/
public bool $is_mod;
/**
* @var int
*
* Activation status of this user's account.
*/
public int $is_activated;
/**
* @var bool
*
* Whether this user has been banned.
*/
public bool $is_banned;
/**
* @var bool
*
* Whether this user is currently browsing the forum.
*/
public bool $is_online;
/**
* @var bool
*
* Whether to show that this user is currently browsing the forum.
*/
public bool $show_online;
/**
* @var string
*
* JSON data about the URL this user is currently viewing.
*/
public string $url;
/**
* @var int
*
* Unix timestamp of the last time the user logged in.
*/
public int $last_login;
/**
* @var int
*
* ID of the latest message the last time they visited.
* All messages with higher IDs than this are new to this user.
*/
public int $id_msg_last_visit;
/**
* @var int
*
* Total amount of time the user has been logged in, measured in seconds.
*/
public int $total_time_logged_in = 0;
/**
* @var bool
*
* Whether the user wants their login cookie not to expire.
*/
public bool $stay_logged_in;
/**
* @var int
*
* Unix timestamp when this user registered.
*/
public int $date_registered;
/**
* @var string
*
* The user's current IP address.
*/
public string $ip;
/**
* @var string
*
* The user's previous known IP address, if any.
*/
public string $ip2;
/**
* @var string
*
* The user's preferred language.
*/
public string $language;
/**
* @var string
*
* The user's preferred time format.
*/
public string $time_format;
/**
* @var string
*
* The user's time zone.
*/
public string $timezone;
/**
* @var int
*
* The UTC offset of the user's time zone.
*/
public int $time_offset;
/**
* @var int
*
* Number of posts the user has made.
*/
public int $posts;
/**
* @var string
*
* The user's title.
*/
public string $title;
/**
* @var string
*
* The user's signature.
*/
public string $signature;
/**
* @var string
*
* The user's personal text blurb.
*/
public string $personal_text;
/**
* @var string
*
* The user's birthdate.
*/
public string $birthdate;
/**
* @var array
*
* Info about the user's website.
*/
public array $website = [
'url' => null,
'title' => null,
];
/**
* @var int
*
* The user's preferred theme.
*/
public int $theme;
/**
* @var array
*
* The user's theme options.
*/
public array $options = [];
/**
* @var string
*
* The user's preferred smiley set.
*/
public string $smiley_set;
/**
* @var array
*
* IDs of users on this user's buddy list.
*/
public array $buddies = [];
/**
* @var array
*
* IDs of users that this user is ignoring.
*/
public array $ignoreusers = [];
/**
* @var int
*
* This user's preference about who to receive personal messages from.
*/
public int $pm_receive_from;
/**
* @var int
*
* This user's display preferences for personal messages.
*/
public int $pm_prefs;
/**
* @var int
*
* Total number of personal messages the user has.
*/
public int $messages;
/**
* @var int
*
* Number of unread personal messages the user has.
*/
public int $unread_messages;
/**
* @var int
*
* Whether the user has new personal messages.
*/
public int $new_pm;
/**
* @var int
*
* Number of unread alerts the user has.
*/
public int $alerts;
/**
* @var array
*
* IDs of boards that this user is ignoring.
*/
public array $ignoreboards = [];
/**
* @var string
*
* Name of the user's group.
*
* Usually the same as $primary_group_name, but might change if the user
* is a moderator on the current board.
*/
public string $group_name;
/**
* @var string
*
* Name of the user's primary group.
*
* Does not change even if the user is a moderator on the current board.
*/
public string $primary_group_name;
/**
* @var string
*
* Name of the user's post-count based group.
*/
public string $post_group_name;
/**
* @var string
*
* The color associated with this user's group.
*/
public string $group_color;
/**
* @var string
*
* The color associated with this user's post group.
*/
public string $post_group_color;
/**
* @var array
*
* Info about the icons associated with this user's group.
* (Exactly which group will depend on the situation.)
*/
public array $icons;
/**
* @var array
*
* Info about the user's avatar.
*/
public array $avatar = [
'original_url' => null,
'url' => null,
'href' => null,
'name' => null,
'filename' => null,
'custom_dir' => null,
'id_attach' => null,
'width' => null,
'height' => null,
'image' => null,
];
/**
* @var array
*
* A collection of UserPermissionSet instances, organized by board.
*
* The UserPermissionSet for this user's global (a.k.a. general) permissions
* is stored in $this->permission_sets[0].
*
* Otherwise, keys are board IDs and values are UserPermissionSet instances
* for those boards.
*/
public array $permission_sets;
/**
* @var int
*
* This user's warning level.
*/
public int $warning;
/**
* @var array
*
* Moderator access info.
*/
public array $mod_cache = [];
/**
* @var string
*
* Moderator preferences.
* @todo This doesn't appear to be used anywhere.
*/
public string $mod_prefs = '';
/**
* @var bool
*
* Whether this user can access the moderation center.
*/
public bool $can_mod = false;
/**
* @var bool
*
* Whether this user can manage boards.
*/
public bool $can_manage_boards = false;
/**
* @var string
*
* SQL query string to get only boards this user can see.
*/
public string $query_see_board;
/**
* @var string
*
* Variant of $query_see_board that checks against topics' id_board field.
*/
public string $query_see_topic_board;
/**
* @var string
*
* Variant of $query_see_board that checks against posts' id_board field.
*/
public string $query_see_message_board;
/**
* @var string
*
* SQL query string to get only boards this user can see and is not ignoring.
*/
public string $query_wanna_see_board;
/**
* @var string
*
* Variant of $query_wanna_see_board that checks against topics' id_board field.
*/
public string $query_wanna_see_topic_board;
/**
* @var string
*
* Variant of $query_wanna_see_board that checks against posts' id_board field.
*/
public string $query_wanna_see_message_board;
/**
* @var array
*
* Formatted versions of this user's properties, suitable for display.
*/
public array $formatted = [];
/**************************
* Public static properties
**************************/
/**
* @var array
*
* All loaded instances of this class.
*/
public static array $loaded = [];
/**
* @var self
*
* Instance of this class for the current user.
*/
public static self $me;
/**
* @var int
*
* ID number of the current user.
*
* As a general rule, code outside this class should use User::$me->id
* rather than User::$my_id. The only exception to this rule is in code
* executed during the login and logout processes, because User::$me->id
* is not set at all points during those processes.
*/
public static int $my_id;
/**
* @var string
*
* "Session check" value for the current user.
* Set by Session::load(). Used by checkSession().
*/
public static $sc;
/**
* @var array
*
* Basic data from the database about all loaded users.
*/
public static array $profiles = [];
/**
* @var array
*
* Basic data from the database about the current user.
* A reference to User::$profiles[User::$my_id].
* Only exists for backward compatibility reasons.
*/
public static $settings;
/**
* @var object
*
* Processed data about the current user.
* This is set to a reference to User::$me once the latter exists.
* Only exists for backward compatibility reasons.
*/
public static $info;
/**
* @var array
*
* Alternative way to get formatted data about users.
* A reference to User::$loaded[$id]->formatted (where $id is a user ID).
* Only exists for backward compatibility reasons.
*/
public static $memberContext;
/**
* @var array
*
* Fields in the member table that take integers.
*/
public static array $knownInts = [
'alerts',
'date_registered',
'gender',
'id_group',
'id_msg_last_visit',
'id_post_group',
'id_theme',
'instant_messages',
'is_activated',
'last_login',
'new_pm',
'pm_prefs',
'pm_receive_from',
'posts',
'show_online',
'total_time_logged_in',
'unread_messages',
'warning',
];
/**
* @var array
*
* Fields in the member table that take floats.
*/
public static array $knownFloats = [
'time_offset',
];
/**
* @var array
*
* Names of variables to pass to the integrate_change_member_data hook.
*/
public static array $integration_vars = [
'avatar',
'birthdate',
'email_address',
'gender',
'id_group',
'lngfile',
'location',
'member_name',
'real_name',
'time_format',
'time_offset',
'timezone',
'website_title',
'website_url',
];
/*********************
* Internal properties
*********************/
/**
* @var array
*
* Alternate names for some object properties.
*/
protected array $prop_aliases = [
'id_member' => 'id',
'member_name' => 'username',
'real_name' => 'name',
'display_name' => 'name',
'email_address' => 'email',
'lngfile' => 'language',
'member_group' => 'group_name',
'primary_group' => 'primary_group_name',
'member_group_color' => 'group_color',
'member_ip' => 'ip',
'member_ip2' => 'ip2',
'usertitle' => 'title',
'blurb' => 'title',
'id_theme' => 'theme',
'ignore_boards' => 'ignoreboards',
'pm_ignore_list' => 'ignoreusers',
'buddy_list' => 'buddies',
'instant_messages' => 'messages',
'birth_date' => 'birthdate',
'last_login_timestamp' => 'last_login',
// Square brackets are parsed to find array elements.
'website_url' => 'website[url]',
'website_title' => 'website[title]',
// Initial exclamation mark means inverse of the property.
'is_logged' => '!is_guest',
];
/**
* @var bool
*
* Whether the integrate_verify_user hook verified this user for us.
*/
private bool $already_verified = false;
/**
* @var string
*
* The dataset that was loaded for this user.
*/
private string $dataset;
/**
* @var bool
*
* Whether custom profile fields are in the formatted data for this user.
*/
private bool $custom_fields_displayed = false;
/**
* @var array
*
* Cache for the allowedTo() method.
*/
private array $perm_cache = [];
/**
* @var array
*
* Cache for the boardsCanAccess() method.
*/
private array $accessible_boards;
/**
* @var array
*
* Cache for the groupsCanModerate() method.
*/
private array $groups_can_moderate;
/****************************
* Internal static properties
****************************/
/**
* @var array
*
* Maps names of dataset levels to numeric values.
*/
protected static array $dataset_levels = [
'minimal' => 0,
'basic' => 1,
'normal' => 2,
'profile' => 3,
];
/**
* @var array
*
* BackwardCompatibility settings for this class.
*/
private static $backcompat = [
'prop_names' => [
'profiles' => 'user_profile',
'settings' => 'user_settings',
'info' => 'user_info',
'sc' => 'sc',
'memberContext' => 'memberContext',
],
];
/****************
* Public methods
****************/
/**
* Sets custom properties.
*
* @param string $prop The property name.
* @param mixed $value The value to set.
*/
public function __set(string $prop, mixed $value): void
{
if (\in_array($this->prop_aliases[$prop] ?? $prop, ['additional_groups', 'buddies', 'ignoreusers', 'ignoreboards']) && \is_string($value)) {
$prop = (string) ($this->prop_aliases[$prop] ?? $prop);
$value = array_map('intval', array_filter(explode(',', $value), 'strlen'));
}
$this->customPropertySet($prop, $value);
}
/**
* Load this user's permissions.
*
* @param int|array|null $boards Boards to load permissions for.
* Default: null
*/
public function loadPermissions(int|array|null $boards = null): void
{
if ($boards === []) {
$boards = null;
}
foreach (
UserPermissionSet::load(
$this,
array_filter(
array_unique(
array_merge(
[0],
(array) ($boards ?? Board::$info->id ?? 0),
),
),
fn($board) => !isset($this->permission_sets[$board]),
),
) as $set
) {
if ($set->profile->id === PermissionProfile::DEFAULT) {
$this->permission_sets[0] = $set;
}
foreach ($set->profile->boards() as $id_board) {
$this->permission_sets[$id_board] = $set;
}
}
if (!isset($boards)) {
$this->loadModCache();
// Can this user approve group requests?
if (($this->mod_cache['gq'] ?? '0=1') != '0=1') {
$this->permission_sets[0]->grant('approve_group_requests');
}
// A user can mod if they have permission to see the mod center, or they are a board/group/approval moderator.
$this->can_mod = (
$this->is_admin
|| $this->permission_sets[0]->allowedTo('access_mod_center')
|| ($this->mod_cache['gq'] ?? '0=1') != '0=1'
|| ($this->mod_cache['bq'] ?? '0=1') != '0=1'
|| (
Config::$modSettings['postmod_active']
&& !empty($this->mod_cache['ap'])
)
);
// This is a useful phantom permission added to the current user,
// and only the current user while they are logged in. For example
// this drastically simplifies certain changes to the profile area.
if (!$this->is_guest && $this === self::$me) {
$this->permission_sets[0]->grant('is_not_guest');
} else {
$this->permission_sets[0]->deny('is_not_guest');
}
}
}
/**
* Sets the formatted versions of user data for use in themes and templates.
*
* @param bool $display_custom_fields Whether to get custom profile fields
* ready for display.
* @return array A copy of $this->formatted.
*/
public function format(bool $display_custom_fields = false): array
{
static $loadedLanguages = [];
if (empty(Config::$modSettings['displayFields'])) {
$display_custom_fields = false;
}
// If this user's data is already loaded, skip it.
if (!empty($this->formatted) && $this->custom_fields_displayed >= $display_custom_fields) {
return $this->formatted;
}
// The minimal values.
$this->formatted = [
'id' => $this->id,
'username' => $this->is_guest ? Lang::getTxt('guest_title', file: 'General') : $this->username,
'name' => $this->is_guest ? Lang::getTxt('guest_title', file: 'General') : $this->name,