-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathUser.class.php
More file actions
771 lines (682 loc) · 24.2 KB
/
User.class.php
File metadata and controls
771 lines (682 loc) · 24.2 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
<?php
namespace wcf\data\user;
use wcf\data\DatabaseObject;
use wcf\data\IPopoverObject;
use wcf\data\IUserContent;
use wcf\data\language\Language;
use wcf\data\user\group\UserGroup;
use wcf\data\user\option\UserOption;
use wcf\system\cache\builder\UserOptionCacheBuilder;
use wcf\system\language\LanguageFactory;
use wcf\system\request\IRouteController;
use wcf\system\request\LinkHandler;
use wcf\system\user\authentication\password\algorithm\DoubleBcrypt;
use wcf\system\user\authentication\password\PasswordAlgorithmManager;
use wcf\system\user\storage\UserStorageHandler;
use wcf\system\WCF;
use wcf\util\JSON;
use wcf\util\UserUtil;
/**
* Represents a user.
*
* @author Alexander Ebert
* @copyright 2001-2020 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*
* @property-read int $userID unique id of the user
* @property-read string $username name of the user
* @property-read string $email email address of the user
* @property-read string $password double salted hash of the user's password
* @property-read string $accessToken token used for access authentication, for example used by feed pages
* @property-read int $languageID id of the interface language used by the user
* @property-read int $registrationDate timestamp at which the user has registered/has been created
* @property-read int $styleID id of the style used by the user
* @property-read int $banned is `1` if the user is banned, otherwise `0`
* @property-read string $banReason reason why the user is banned
* @property-read int $banExpires timestamp at which the banned user is automatically unbanned
* @property-read int $activationCode flag which determines, whether the user is activated (for legacy reasons an random integer, if the user is *not* activated)
* @property-read string $emailConfirmed code sent to the user's email address used for account activation or null if the email is confirmed
* @property-read int $lastLostPasswordRequestTime timestamp at which the user has reported that they lost their password or 0 if password has not been reported as lost
* @property-read string $lostPasswordKey code used for authenticating setting new password after password loss or empty if password has not been reported as lost
* @property-read int $lastUsernameChange timestamp at which the user changed their name the last time or 0 if username has not been changed
* @property-read string $newEmail new email address of the user that has to be manually confirmed or empty if no new email address has been set
* @property-read string $oldUsername previous name of the user or empty if they have had no previous name
* @property-read int $quitStarted timestamp at which the user terminated their account
* @property-read int $reactivationCode code used for authenticating setting new email address or empty if no new email address has been set
* @property-read string $registrationIpAddress ip address of the user at the time of registration or empty if user has been created manually or if no ip address are logged
* @property-read int|null $avatarID id of the user's avatar or null if they have no avatar
* @property-read int|null $avatarFileID id of the user's avatar core file or null if they have no avatar
* @property-read int $disableAvatar is `1` if the user's avatar has been disabled, otherwise `0`
* @property-read string $disableAvatarReason reason why the user's avatar is disabled
* @property-read int $disableAvatarExpires timestamp at which the user's avatar will automatically be enabled again
* @property-read string|null $signature text of the user's signature
* @property-read int $signatureEnableHtml is `1` if HTML will rendered in the user's signature, otherwise `0`
* @property-read int $disableSignature is `1` if the user's signature has been disabled, otherwise `0`
* @property-read string $disableSignatureReason reason why the user's signature is disabled
* @property-read int $disableSignatureExpires timestamp at which the user's signature will automatically be enabled again
* @property-read int $lastActivityTime timestamp of the user's last activity
* @property-read int $profileHits number of times the user's profile has been visited
* @property-read int|null $rankID id of the user's rank or null if they have no rank
* @property-read string $userTitle custom user title used instead of rank title or empty if user has no custom title
* @property-read int|null $userOnlineGroupID id of the user group whose online marking is used when printing the user's formatted name or null if no special marking is used
* @property-read int $activityPoints total number of the user's activity points
* @property-read string $notificationMailToken token used for authenticating requests by the user to disable notification emails
* @property-read string $authData data of the third party used for authentication
* @property-read int $likesReceived cumulative result of likes (counting +1) the user's contents have received
* @property-read int|null $coverPhotoFileID
* @property-read int $disableCoverPhoto is `1` if the user's cover photo has been disabled, otherwise `0`
* @property-read string $disableCoverPhotoReason reason why the user's cover photo is disabled
* @property-read int $disableCoverPhotoExpires timestamp at which the user's cover photo will automatically be enabled again
* @property-read int $articles number of articles written by the user
* @property-read string $blacklistMatches JSON string of an array with all matches in the blacklist, otherwise an empty string
* @property-read int $multifactorActive is `1` if the use has enabled a second factor, otherwise `0`
* @property-read int $trophyPoints total number of user's trophies in active categories
* @property-read string $timezone
*/
final class User extends DatabaseObject implements IPopoverObject, IRouteController, IUserContent
{
/**
* list of group ids
* @var int[]
*/
protected $groupIDs;
/**
* true, if user has access to the ACP
* @var bool
*/
protected $hasAdministrativePermissions;
/**
* list of language ids
* @var int[]
*/
protected $languageIDs;
/**
* date time zone object
* @var ?\DateTimeZone
*/
protected $timezoneObj;
/**
* list of user options
* @var UserOption[]
*/
protected static $userOptions;
const REGISTER_ACTIVATION_NONE = 0;
const REGISTER_ACTIVATION_USER = 1;
const REGISTER_ACTIVATION_ADMIN = 2;
const REGISTER_ACTIVATION_USER_AND_ADMIN = self::REGISTER_ACTIVATION_USER | self::REGISTER_ACTIVATION_ADMIN;
/**
* @inheritDoc
*/
public function __construct($id, $row = null, ?DatabaseObject $object = null)
{
if ($id !== null) {
$sql = "SELECT user_option_value.*, user_table.*
FROM wcf1_user user_table
LEFT JOIN wcf1_user_option_value user_option_value
ON user_option_value.userID = user_table.userID
WHERE user_table.userID = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([$id]);
$row = $statement->fetchArray();
// enforce data type 'array'
if ($row === false) {
$row = [];
}
} elseif ($object !== null) {
$row = $object->data;
}
$this->handleData($row);
}
/**
* Returns true if the given password is the correct password for this user.
*
* @param string $password
* @return bool password correct
*/
public function checkPassword(
#[\SensitiveParameter]
$password
) {
$isValid = false;
$manager = PasswordAlgorithmManager::getInstance();
// Compatibility for WoltLab Suite < 5.4.
if (DoubleBcrypt::isLegacyDoubleBcrypt($this->password)) {
$algorithmName = 'DoubleBcrypt';
$hash = $this->password;
} else {
[$algorithmName, $hash] = \explode(':', $this->password, 2);
}
$algorithm = $manager->getAlgorithmFromName($algorithmName);
$isValid = $algorithm->verify($password, $hash);
if (!$isValid) {
return false;
}
$defaultAlgorithm = $manager->getDefaultAlgorithm();
if (\get_class($algorithm) !== \get_class($defaultAlgorithm) || $algorithm->needsRehash($hash)) {
$userEditor = new UserEditor($this);
$userEditor->update([
'password' => $password,
]);
}
// $isValid is always true at this point. However we intentionally use a variable
// that defaults to false to prevent accidents during refactoring.
// @phpstan-ignore function.alreadyNarrowedType
\assert($isValid);
return $isValid;
}
/**
* @param string $passwordHash
* @return false
* @deprecated 5.4 - This method always returns false, as user sessions are long-lived now.
*/
public function checkCookiePassword($passwordHash)
{
return false;
}
/**
* Returns an array with all the groups in which the actual user is a member.
*
* @param bool $skipCache
* @return int[]
*/
public function getGroupIDs($skipCache = false)
{
if ($this->groupIDs === null || $skipCache) {
if (!$this->userID) {
// user is a guest, use default guest group
$this->groupIDs = UserGroup::getGroupIDsByType([UserGroup::GUESTS, UserGroup::EVERYONE]);
} else {
// get group ids
$data = UserStorageHandler::getInstance()->getField('groupIDs', $this->userID);
// cache does not exist or is outdated
if ($data === null || $skipCache) {
$sql = "SELECT groupID
FROM wcf1_user_to_group
WHERE userID = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([$this->userID]);
$this->groupIDs = $statement->fetchAll(\PDO::FETCH_COLUMN);
// update storage data
if (!$skipCache) {
UserStorageHandler::getInstance()->update(
$this->userID,
'groupIDs',
\serialize($this->groupIDs)
);
}
} else {
$this->groupIDs = \unserialize($data);
}
}
\sort($this->groupIDs, \SORT_NUMERIC);
}
return $this->groupIDs;
}
/**
* Returns a list of language ids for this user.
*
* @return int[]
*/
public function getLanguageIDs()
{
if ($this->languageIDs === null) {
$this->languageIDs = [];
if ($this->userID) {
// get language ids
$data = UserStorageHandler::getInstance()->getField('languageIDs', $this->userID);
// cache does not exist or is outdated
if ($data === null) {
$sql = "SELECT languageID
FROM wcf1_user_to_language
WHERE userID = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([$this->userID]);
$this->languageIDs = $statement->fetchAll(\PDO::FETCH_COLUMN);
// update storage data
UserStorageHandler::getInstance()->update(
$this->userID,
'languageIDs',
\serialize($this->languageIDs)
);
} else {
$this->languageIDs = \unserialize($data);
}
} else {
$this->languageIDs = LanguageFactory::getInstance()->getContentLanguageIDs();
}
}
return $this->languageIDs;
}
/**
* Returns the value of the user option with the given name.
*
* @param string $name user option name
* @param bool $filterDisabled suppress values for disabled options
* @return mixed user option value
*/
public function getUserOption($name, $filterDisabled = false)
{
$optionID = self::getUserOptionID($name);
if ($optionID === null) {
return;
} elseif ($filterDisabled && self::$userOptions[$name]->isDisabled) {
return;
}
return $this->data['userOption' . $optionID] ?? null;
}
/**
* Fetches all user options from cache.
*
* @return void
*/
protected static function getUserOptionCache()
{
self::$userOptions = UserOptionCacheBuilder::getInstance()->getData([], 'options');
}
/**
* Returns the id of a user option.
*
* @param string $name
* @return ?int
*/
public static function getUserOptionID($name)
{
// get user option cache if necessary
if (self::$userOptions === null) {
self::getUserOptionCache();
}
if (!isset(self::$userOptions[$name])) {
return null;
}
return self::$userOptions[$name]->optionID;
}
/**
* @inheritDoc
*/
public function __get($name)
{
return $this->data[$name] ?? $this->getUserOption($name);
}
/**
* Returns the user with the given username.
*
* @param string $username
* @return User
*/
public static function getUserByUsername($username)
{
$sql = "SELECT user_option_value.*, user_table.*
FROM wcf1_user user_table
LEFT JOIN wcf1_user_option_value user_option_value
ON user_option_value.userID = user_table.userID
WHERE user_table.username = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([$username]);
$row = $statement->fetchArray();
if (!$row) {
$row = [];
}
return new self(null, $row);
}
/**
* Returns the user with the given email.
*
* @param string $email
* @return User
*/
public static function getUserByEmail($email)
{
$sql = "SELECT user_option_value.*, user_table.*
FROM wcf1_user user_table
LEFT JOIN wcf1_user_option_value user_option_value
ON user_option_value.userID = user_table.userID
WHERE user_table.email = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([$email]);
$row = $statement->fetchArray();
if (!$row) {
$row = [];
}
return new self(null, $row);
}
/**
* Returns the user with the given authData.
*
* @param string $authData
* @return User
*/
public static function getUserByAuthData($authData)
{
$sql = "SELECT user_option_value.*, user_table.*
FROM wcf1_user user_table
LEFT JOIN wcf1_user_option_value user_option_value
ON user_option_value.userID = user_table.userID
WHERE user_table.authData = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([$authData]);
$row = $statement->fetchArray();
if (!$row) {
$row = [];
}
return new self(null, $row);
}
/**
* Returns 3rd party auth provider name.
*
* @return string
* @since 5.2
*/
public function getAuthProvider()
{
if (!$this->authData) {
return '';
}
return \mb_substr($this->authData, 0, \mb_strpos($this->authData, ':'));
}
/**
* Returns true if this user is marked.
*
* @return int
*/
public function isMarked()
{
$markedUsers = WCF::getSession()->getVar('markedUsers');
if ($markedUsers !== null) {
if (\in_array($this->userID, $markedUsers)) {
return 1;
}
}
return 0;
}
/**
* Returns true if the email is confirmed.
*
* @return bool
* @since 5.3
*/
public function isEmailConfirmed()
{
return $this->emailConfirmed === null;
}
/**
* Returns the time zone of this user.
*
* @return \DateTimeZone
*/
public function getTimeZone()
{
if ($this->timezoneObj === null) {
if ($this->timezone) {
$this->timezoneObj = new \DateTimeZone($this->timezone);
} else {
$this->timezoneObj = new \DateTimeZone(TIMEZONE);
}
}
return $this->timezoneObj;
}
/**
* Applies the user's timezone to the given timestamp.
*/
public function getLocalDate(int $timestamp): \DateTimeImmutable
{
$dateTime = (new \DateTimeImmutable('@' . $timestamp));
$dateTime = $dateTime->setTimezone($this->getTimeZone());
return $dateTime;
}
/**
* Returns a list of users.
*
* @param int[] $userIDs
* @return User[]
*/
public static function getUsers(array $userIDs)
{
$userList = new UserList();
$userList->setObjectIDs($userIDs);
$userList->readObjects();
return $userList->getObjects();
}
/**
* Returns username.
*/
public function __toString(): string
{
return $this->getTitle();
}
/**
* @inheritDoc
*/
public static function getDatabaseTableAlias()
{
return 'user_table';
}
/**
* @inheritDoc
*/
public function getTitle(): string
{
return $this->username ?: '';
}
/**
* Returns the language of this user.
*
* @return Language
*/
public function getLanguage()
{
$language = LanguageFactory::getInstance()->getLanguage($this->languageID);
if ($language === null) {
$language = LanguageFactory::getInstance()->getLanguage(LanguageFactory::getInstance()->getDefaultLanguageID());
}
return $language;
}
/**
* Returns true if the active user can edit this user.
*
* @return bool
*/
public function canEdit()
{
return WCF::getSession()->getPermission('admin.user.canEditUser') && UserGroup::isAccessibleGroup($this->getGroupIDs());
}
/**
* Returns true, if this user has access to the ACP.
*
* @return bool
*/
public function hasAdministrativeAccess()
{
if ($this->hasAdministrativePermissions === null) {
$this->hasAdministrativePermissions = false;
if ($this->userID) {
foreach (UserGroup::getGroupsByIDs($this->getGroupIDs()) as $group) {
if ($group->isAdminGroup()) {
$this->hasAdministrativePermissions = true;
break;
}
}
}
}
return $this->hasAdministrativePermissions;
}
/**
* Returns true, if this user is a member of the owner group.
*
* @return bool
* @since 5.2
*/
public function hasOwnerAccess()
{
foreach (UserGroup::getGroupsByIDs($this->getGroupIDs()) as $group) {
if ($group->isOwner()) {
return true;
}
}
return false;
}
/**
* @inheritDoc
*/
public function getUserID()
{
return $this->userID;
}
/**
* @inheritDoc
*/
public function getUsername()
{
return $this->username;
}
/**
* @inheritDoc
*/
public function getTime()
{
return $this->registrationDate;
}
/**
* @inheritDoc
*/
public function getLink(): string
{
return LinkHandler::getInstance()->getLink('User', [
'application' => 'wcf',
'object' => $this,
'forceFrontend' => true,
]);
}
/**
* Returns the registration ip address, attempts to convert to IPv4.
*
* @return string
*/
public function getRegistrationIpAddress()
{
if ($this->registrationIpAddress) {
return UserUtil::convertIPv6To4($this->registrationIpAddress);
}
return '';
}
/**
* Returns true, if this user can purchase paid subscriptions.
*
* @return bool
*/
public function canPurchasePaidSubscriptions()
{
return WCF::getUser()->userID && !$this->pendingActivation();
}
/**
* Returns the list of fields that had matches in the blacklist. An empty list is
* returned if the user has been approved, regardless of any known matches.
*
* @return string[]
* @since 5.2
*/
public function getBlacklistMatches()
{
if ($this->pendingActivation() && $this->blacklistMatches) {
return JSON::decode($this->blacklistMatches);
}
return [];
}
/**
* Returns a human readable list of fields that have positive matches against the
* blacklist. If you require the raw field names, please use `getBlacklistMatches()`
* instead.
*
* @return string[]
* @since 5.2
*/
public function getBlacklistMatchesTitle()
{
return \array_map(static function ($field) {
if ($field === 'ip') {
$field = 'ipAddress';
}
return WCF::getLanguage()->get('wcf.user.' . $field);
}, $this->getBlacklistMatches());
}
/**
* Returns true if this user is not activated.
*
* @return bool
* @since 5.3
*/
public function pendingActivation()
{
return $this->activationCode != 0;
}
/**
* Returns true if this user requires activation by the user.
*
* @return bool
* @since 5.3
*/
public function requiresEmailActivation()
{
return (int)REGISTER_ACTIVATION_METHOD & self::REGISTER_ACTIVATION_USER && $this->pendingActivation() && !$this->isEmailConfirmed();
}
/**
* Returns true if this user requires the activation by an admin.
*
* @return bool
* @since 5.3
*/
public function requiresAdminActivation()
{
return (int)REGISTER_ACTIVATION_METHOD & self::REGISTER_ACTIVATION_ADMIN && $this->pendingActivation();
}
/**
* Returns true if this user can confirm the email themself.
*
* @return bool
* @since 5.3
*/
public function canEmailConfirm()
{
return (int)REGISTER_ACTIVATION_METHOD & self::REGISTER_ACTIVATION_USER && !$this->isEmailConfirmed();
}
/**
* Returns true, if the user must confirm his email by themself.
*
* @return bool
* @since 5.3
*/
public function mustSelfEmailConfirm()
{
return !!((int)REGISTER_ACTIVATION_METHOD & self::REGISTER_ACTIVATION_USER);
}
/**
* Returns true if the user is a member of a user group that requires
* multi-factor authentication to be enabled.
*
* @since 5.4
*/
public function requiresMultifactor(): bool
{
foreach (UserGroup::getGroupsByIDs($this->getGroupIDs()) as $group) {
if ($group->requireMultifactor) {
return true;
}
}
return false;
}
/**
* @inheritDoc
*/
public function getPopoverLinkClass()
{
return 'userLink';
}
/**
* Returns the value for the access token link parameter.
*
* @since 6.2
*/
public function getAccessToken(): string
{
return \sprintf('%d-%s', $this->userID, $this->accessToken);
}
}