-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathUserProfile.class.php
More file actions
1246 lines (1078 loc) · 35.9 KB
/
UserProfile.class.php
File metadata and controls
1246 lines (1078 loc) · 35.9 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
namespace wcf\data\user;
use wcf\data\DatabaseObjectDecorator;
use wcf\data\file\File;
use wcf\data\ITitledLinkObject;
use wcf\data\trophy\Trophy;
use wcf\data\trophy\TrophyCache;
use wcf\data\user\avatar\AvatarDecorator;
use wcf\data\user\avatar\DefaultAvatar;
use wcf\data\user\avatar\IUserAvatar;
use wcf\data\user\cover\photo\DefaultUserCoverPhoto;
use wcf\data\user\cover\photo\IUserCoverPhoto;
use wcf\data\user\cover\photo\UserCoverPhoto;
use wcf\data\user\group\UserGroup;
use wcf\data\user\ignore\UserIgnore;
use wcf\data\user\online\UserOnline;
use wcf\data\user\option\ViewableUserOption;
use wcf\data\user\rank\UserRank;
use wcf\system\cache\builder\UserGroupPermissionCacheBuilder;
use wcf\system\cache\eager\UserRankCache;
use wcf\system\cache\runtime\FileRuntimeCache;
use wcf\system\cache\runtime\UserProfileRuntimeCache;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\email\Mailbox;
use wcf\system\event\EventHandler;
use wcf\system\exception\ImplementationException;
use wcf\system\user\signature\SignatureCache;
use wcf\system\user\storage\UserStorageHandler;
use wcf\system\WCF;
use wcf\util\DateUtil;
use wcf\util\StringUtil;
/**
* Decorates the user object and provides functions to retrieve data for user profiles.
*
* @author Marcel Werk
* @copyright 2001-2019 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*
* @mixin User
* @property-read int $sessionLastActivityTime
* @property-read bool $birthdayShowYear
* @property-read string $birthday
* @extends DatabaseObjectDecorator<User>
*/
class UserProfile extends DatabaseObjectDecorator implements ITitledLinkObject
{
/**
* @inheritDoc
*/
protected static $baseClass = User::class;
/**
* list of ignored user ids
* @var int[]
*/
protected $ignoredUserIDs;
/**
* list of user ids that are ignoring this user
* @var int[]
*/
protected $ignoredByUserIDs;
/**
* list of follower user ids
* @var int[]
*/
protected $followerUserIDs;
/**
* list of following user ids
* @var int[]
*/
protected $followingUserIDs;
/**
* @var AvatarDecorator
*/
protected $avatar;
/**
* user rank object
* @var UserRank
* @deprecated 6.1 use `->getRank()` instead
*/
protected $rank;
/**
* age of this user
* @var int
*/
protected $__age;
/**
* group data and permissions
* @var mixed[][]
*/
protected $groupData;
/**
* current location of this user.
* @var string
*/
protected $currentLocation;
/**
* user cover photo
* @var ?IUserCoverPhoto
*/
protected $coverPhoto;
const GENDER_MALE = 1;
const GENDER_FEMALE = 2;
const GENDER_OTHER = 3;
const ACCESS_EVERYONE = 0;
const ACCESS_REGISTERED = 1;
const ACCESS_FOLLOWING = 2;
const ACCESS_NOBODY = 3;
/**
* @inheritDoc
*/
public function __toString(): string
{
return $this->getDecoratedObject()->__toString();
}
/**
* Returns a list of all user ids being followed by current user.
*
* @return int[]
*/
public function getFollowingUsers()
{
if ($this->followingUserIDs === null) {
$this->followingUserIDs = [];
if ($this->userID) {
// get ids
$data = UserStorageHandler::getInstance()->getField('followingUserIDs', $this->userID);
// cache does not exist or is outdated
if ($data === null) {
$sql = "SELECT followUserID
FROM wcf1_user_follow
WHERE userID = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([$this->userID]);
$this->followingUserIDs = $statement->fetchAll(\PDO::FETCH_COLUMN);
// update storage data
UserStorageHandler::getInstance()->update(
$this->userID,
'followingUserIDs',
\serialize($this->followingUserIDs)
);
} else {
$this->followingUserIDs = \unserialize($data);
}
}
}
return $this->followingUserIDs;
}
/**
* Returns a list of user ids following current user.
*
* @return int[]
*/
public function getFollowers()
{
if ($this->followerUserIDs === null) {
$this->followerUserIDs = [];
if ($this->userID) {
// get ids
$data = UserStorageHandler::getInstance()->getField('followerUserIDs', $this->userID);
// cache does not exist or is outdated
if ($data === null) {
$sql = "SELECT userID
FROM wcf1_user_follow
WHERE followUserID = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([$this->userID]);
$this->followerUserIDs = $statement->fetchAll(\PDO::FETCH_COLUMN);
// update storage data
UserStorageHandler::getInstance()->update(
$this->userID,
'followerUserIDs',
\serialize($this->followerUserIDs)
);
} else {
$this->followerUserIDs = \unserialize($data);
}
}
}
return $this->followerUserIDs;
}
/**
* Returns a list of ignored user ids.
*
* @param ?int $type One of the UserIgnore::TYPE_* constants.
* @return int[]
*/
public function getIgnoredUsers(?int $type = null)
{
if ($this->ignoredUserIDs === null) {
$this->ignoredUserIDs = [];
if ($this->userID) {
// get ids
$data = UserStorageHandler::getInstance()->getField('ignoredUserIDs', $this->userID);
// cache does not exist or is outdated
if ($data === null) {
$sql = "SELECT ignoreUserID, type
FROM wcf1_user_ignore
WHERE userID = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([$this->userID]);
$this->ignoredUserIDs = $statement->fetchMap('ignoreUserID', 'type');
// update storage data
UserStorageHandler::getInstance()->update(
$this->userID,
'ignoredUserIDs',
\serialize($this->ignoredUserIDs)
);
} else {
$this->ignoredUserIDs = \unserialize($data);
}
}
}
return \array_keys(\array_filter($this->ignoredUserIDs, static function ($userType) use ($type) {
if ($type === null) {
return true;
} elseif ($type === UserIgnore::TYPE_BLOCK_DIRECT_CONTACT) {
return \in_array($userType, [UserIgnore::TYPE_BLOCK_DIRECT_CONTACT, UserIgnore::TYPE_HIDE_MESSAGES]);
} elseif ($type === UserIgnore::TYPE_HIDE_MESSAGES) {
return $userType == UserIgnore::TYPE_HIDE_MESSAGES;
} else {
return false;
}
}));
}
/**
* Returns a list of user ids that are ignoring this user.
*
* @return int[]
*/
public function getIgnoredByUsers()
{
if ($this->ignoredByUserIDs === null) {
$this->ignoredByUserIDs = [];
if ($this->userID) {
// get ids
$data = UserStorageHandler::getInstance()->getField('ignoredByUserIDs', $this->userID);
// cache does not exist or is outdated
if ($data === null) {
$sql = "SELECT userID, type
FROM wcf1_user_ignore
WHERE ignoreUserID = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([$this->userID]);
$this->ignoredByUserIDs = $statement->fetchMap('userID', 'type');
// update storage data
UserStorageHandler::getInstance()->update(
$this->userID,
'ignoredByUserIDs',
\serialize($this->ignoredByUserIDs)
);
} else {
$this->ignoredByUserIDs = \unserialize($data);
}
}
}
return \array_keys($this->ignoredByUserIDs);
}
/**
* Returns true if current user is following given user id.
*
* @param int $userID
* @return bool
*/
public function isFollowing($userID)
{
return \in_array($userID, $this->getFollowingUsers());
}
/**
* Returns true if given user ids follows current user.
*
* @param int $userID
* @return bool
*/
public function isFollower($userID)
{
return \in_array($userID, $this->getFollowers());
}
/**
* Returns true if given user is ignored.
*
* @param int $userID
* @param ?int $type One of the UserIgnore::TYPE_* constants.
* @return bool
*/
public function isIgnoredUser($userID, ?int $type = null)
{
return \in_array($userID, $this->getIgnoredUsers($type));
}
/**
* Returns true if the given user ignores the current user.
*
* @param int $userID
* @return bool
*/
public function isIgnoredByUser($userID)
{
return \in_array($userID, $this->getIgnoredByUsers());
}
/**
* Returns the user's avatar.
*
* @return AvatarDecorator
*/
public function getAvatar()
{
if ($this->avatar === null) {
$avatar = null;
if (!$this->disableAvatar) {
if ($this->canSeeAvatar()) {
if ($this->avatarFileID !== null) {
$data = UserStorageHandler::getInstance()->getField('avatar', $this->userID);
if ($data === null) {
$avatar = FileRuntimeCache::getInstance()->getObject($this->avatarFileID);
UserStorageHandler::getInstance()->update(
$this->userID,
'avatar',
\serialize($avatar)
);
} else {
$avatar = \unserialize($data);
}
} else {
$parameters = ['avatar' => null];
EventHandler::getInstance()->fireAction($this, 'getAvatar', $parameters);
$avatar = $parameters['avatar'];
if ($avatar !== null && !($avatar instanceof IUserAvatar)) {
throw new ImplementationException(
\get_class($avatar),
IUserAvatar::class
);
}
}
}
}
// use default avatar
if ($avatar === null) {
$avatar = new DefaultAvatar($this->username ?: '');
}
$this->avatar = new AvatarDecorator($avatar);
}
return $this->avatar;
}
/**
* Sets the user's avatar.
*
* @since 6.2
*/
public function setFileAvatar(File $file): void
{
$this->avatar = new AvatarDecorator($file);
}
/**
* Returns true if the active user can view the avatar of this user.
*
* @return bool
*/
public function canSeeAvatar()
{
return
WCF::getUser()->userID == $this->userID
|| WCF::getSession()->getPermission('user.profile.avatar.canSeeAvatars')
|| (($pending = WCF::getSession()->getPendingUserChange()) && $pending->userID == $this->userID);
}
/**
* Returns the user's cover photo.
*
* @param bool $isACP override ban on cover photo
* @return IUserCoverPhoto
*/
public function getCoverPhoto($isACP = false)
{
if ($this->coverPhoto === null) {
if ($this->coverPhotoFileID) {
if ($isACP || !$this->disableCoverPhoto) {
if ($this->canSeeCoverPhoto()) {
$this->coverPhoto = new UserCoverPhoto(
$this->userID,
FileRuntimeCache::getInstance()->getObject($this->coverPhotoFileID)
);
}
}
}
// use default cover photo
if ($this->coverPhoto === null) {
$this->coverPhoto = new DefaultUserCoverPhoto();
}
}
return $this->coverPhoto;
}
/**
* Returns true if the active user can view the cover photo of this user.
*
* @return bool
*/
public function canSeeCoverPhoto()
{
return WCF::getUser()->userID == $this->userID || WCF::getSession()->getPermission('user.profile.coverPhoto.canSeeCoverPhotos');
}
/**
* Returns true if this user is currently online.
*
* @return bool
*/
public function isOnline()
{
if ($this->getLastActivityTime() > (TIME_NOW - USER_ONLINE_TIMEOUT) && $this->canViewOnlineStatus()) {
return true;
}
return false;
}
/**
* Returns true if the active user can view the online status of this user.
*
* @return bool
*/
public function canViewOnlineStatus()
{
return WCF::getUser()->userID == $this->userID
|| WCF::getSession()->getPermission('admin.user.canViewInvisible')
|| $this->isAccessible('canViewOnlineStatus');
}
/**
* Returns the current location of this user.
*
* @return string
*/
public function getCurrentLocation()
{
if ($this->currentLocation === null) {
$userOnline = new UserOnline($this->getDecoratedObject());
$userOnline->setLocation();
$this->currentLocation = $userOnline->getLocation();
}
return $this->currentLocation;
}
/**
* Returns the special trophies for the user.
*
* @return Trophy[]
*/
public function getSpecialTrophies()
{
$specialTrophies = UserStorageHandler::getInstance()->getField('specialTrophies', $this->userID);
if ($specialTrophies === null) {
// load special trophies for the user
$sql = "SELECT trophyID
FROM wcf1_user_special_trophy
WHERE userID = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([$this->userID]);
$specialTrophies = $statement->fetchAll(\PDO::FETCH_COLUMN);
UserStorageHandler::getInstance()->update($this->userID, 'specialTrophies', \serialize($specialTrophies));
} else {
$specialTrophies = \unserialize($specialTrophies);
}
// check if the user has the permission to store these number of trophies,
// otherwise, delete the last trophies
if (\count($specialTrophies) > $this->getPermission('user.profile.trophy.maxUserSpecialTrophies')) {
$trophyDeleteIDs = [];
while (\count($specialTrophies) > $this->getPermission('user.profile.trophy.maxUserSpecialTrophies')) {
$trophyDeleteIDs[] = \array_pop($specialTrophies);
}
$conditionBuilder = new PreparedStatementConditionBuilder();
$conditionBuilder->add('userID = ?', [$this->userID]);
$conditionBuilder->add('trophyID IN (?)', [$trophyDeleteIDs]);
// reset the user special trophies
$sql = "DELETE FROM wcf1_user_special_trophy
" . $conditionBuilder;
$statement = WCF::getDB()->prepare($sql);
$statement->execute($conditionBuilder->getParameters());
UserStorageHandler::getInstance()->update($this->userID, 'specialTrophies', \serialize($specialTrophies));
}
$trophies = TrophyCache::getInstance()->getTrophiesByID($specialTrophies);
$filteredTrophies = \array_filter($trophies);
if ($filteredTrophies !== $trophies) {
// One or more trophies no longer exists, remove them from the return
// value and force a cache reset.
$trophies = $filteredTrophies;
UserStorageHandler::getInstance()->reset([$this->userID], 'specialTrophies');
}
Trophy::sort($trophies, 'showOrder');
return $trophies;
}
/**
* Prepares the special trophies for the given user ids.
*
* @param int[] $userIDs
* @return void
* @since 5.2
*/
public static function prepareSpecialTrophies(array $userIDs)
{
UserProfileRuntimeCache::getInstance()->cacheObjectIDs($userIDs);
UserStorageHandler::getInstance()->loadStorage($userIDs);
$storageData = UserStorageHandler::getInstance()->getStorage($userIDs, 'specialTrophies');
$rebuildUserIDs = $deleteSpecialTrophyIDs = [];
foreach ($storageData as $userID => $datum) {
if ($datum === null) {
$rebuildUserIDs[] = $userID;
} else {
$specialTrophies = \unserialize($datum);
// check if the user has the permission to store these number of trophies,
// otherwise, delete the last trophies
if (\count($specialTrophies) > UserProfileRuntimeCache::getInstance()->getObject($userID)->getPermission('user.profile.trophy.maxUserSpecialTrophies')) {
$deleteSpecialTrophyIDs[$userID] = [];
while (\count($specialTrophies) > UserProfileRuntimeCache::getInstance()->getObject($userID)->getPermission('user.profile.trophy.maxUserSpecialTrophies')) {
$deleteSpecialTrophyIDs[$userID] = \array_pop($specialTrophies);
}
UserStorageHandler::getInstance()->update($userID, 'specialTrophies', \serialize($specialTrophies));
}
}
}
if (!empty($rebuildUserIDs)) {
$conditionBuilder = new PreparedStatementConditionBuilder();
$conditionBuilder->add('userID IN (?)', [$rebuildUserIDs]);
$sql = "SELECT userID, trophyID
FROM wcf1_user_special_trophy
" . $conditionBuilder;
$statement = WCF::getDB()->prepare($sql);
$statement->execute($conditionBuilder->getParameters());
$data = \array_combine($rebuildUserIDs, \array_fill(0, \count($rebuildUserIDs), []));
while ($row = $statement->fetchArray()) {
$data[$row['userID']][] = $row['trophyID'];
}
foreach ($data as $userID => $trophyIDs) {
UserStorageHandler::getInstance()->update($userID, 'specialTrophies', \serialize($trophyIDs));
}
}
if (!empty($deleteSpecialTrophyIDs)) {
$conditionBuilder = new PreparedStatementConditionBuilder(true, 'OR');
foreach ($deleteSpecialTrophyIDs as $userID => $trophyIDs) {
$conditionBuilder->add('(userID = ? AND trophyID IN (?))', [$userID, $trophyIDs]);
}
$sql = "DELETE FROM wcf1_user_special_trophy
" . $conditionBuilder;
$statement = WCF::getDB()->prepare($sql);
$statement->execute($conditionBuilder->getParameters());
}
}
/**
* Returns the last activity time.
*
* @return int
*/
public function getLastActivityTime()
{
return \max($this->lastActivityTime, $this->sessionLastActivityTime);
}
/**
* Returns a new user profile object.
*
* @param int $userID
* @return ?UserProfile
* @deprecated 3.0, use UserProfileRuntimeCache::getObject()
*/
public static function getUserProfile($userID)
{
return UserProfileRuntimeCache::getInstance()->getObject($userID);
}
/**
* Returns a list of user profiles.
*
* @param int[] $userIDs
* @return (UserProfile|null)[]
* @deprecated 3.0, use UserProfileRuntimeCache::getObjects()
*/
public static function getUserProfiles(array $userIDs)
{
$users = UserProfileRuntimeCache::getInstance()->getObjects($userIDs);
// this method does not return null for non-existing user profiles
foreach ($users as $userID => $user) {
if ($user === null) {
unset($users[$userID]);
}
}
return $users;
}
/**
* Returns the user profile of the user with the given name.
*
* @param string $username
* @return ?UserProfile
*/
public static function getUserProfileByUsername($username)
{
$users = self::getUserProfilesByUsername([$username]);
return $users[$username];
}
/**
* Returns the user profiles of the users with the given names.
*
* @param string[] $usernames
* @return array<string, UserProfile|null>
*/
public static function getUserProfilesByUsername(array $usernames)
{
$users = [];
// save case sensitive usernames
$caseSensitiveUsernames = [];
foreach ($usernames as &$username) {
$tmp = \mb_strtolower($username);
$caseSensitiveUsernames[$tmp] = $username;
$username = $tmp;
}
unset($username);
// check cache
$userProfiles = UserProfileRuntimeCache::getInstance()->getCachedObjects();
foreach ($usernames as $index => $username) {
foreach ($userProfiles as $user) {
if ($user === null) {
continue;
}
if (\mb_strtolower($user->username) === $username) {
$users[$username] = $user;
unset($usernames[$index]);
}
}
}
if (!empty($usernames)) {
$userList = new UserProfileList();
$userList->getConditionBuilder()->add("user_table.username IN (?)", [$usernames]);
$userList->readObjects();
foreach ($userList as $user) {
$users[\mb_strtolower($user->username)] = $user;
UserProfileRuntimeCache::getInstance()->addUserProfile($user);
}
foreach ($usernames as $username) {
if (!isset($users[$username])) {
$users[$username] = null;
}
}
}
// revert usernames to original case
foreach ($users as $username => $user) {
unset($users[$username]);
if (isset($caseSensitiveUsernames[$username])) {
$users[$caseSensitiveUsernames[$username]] = $user;
}
}
return $users;
}
/**
* Returns true if current user fulfills the required permissions.
*/
public function isAccessible(string $name, ?int $userID = null): bool
{
if ($userID === null) {
$userID = WCF::getUser()->userID;
}
$data = ['result' => true, 'name' => $name, 'userID' => $userID];
switch ($this->{$name}) {
case self::ACCESS_EVERYONE:
$data['result'] = true;
break;
case self::ACCESS_REGISTERED:
$data['result'] = ($userID ? true : false);
break;
case self::ACCESS_FOLLOWING:
$result = false;
if ($userID) {
if ($userID == $this->userID) {
$result = true;
} elseif ($this->isFollowing($userID)) {
$result = true;
}
}
$data['result'] = $result;
break;
case self::ACCESS_NOBODY:
$data['result'] = false;
break;
}
EventHandler::getInstance()->fireAction($this, 'isAccessible', $data);
return $data['result'];
}
/**
* Returns true if current user profile is protected.
*
* @return bool
*/
public function isProtected()
{
return !WCF::getSession()->getPermission('admin.general.canViewPrivateUserOptions') && !$this->isAccessible('canViewProfile') && $this->userID != WCF::getUser()->userID;
}
/**
* Returns the age of this user.
*
* @param int $year
* @return int
*/
public function getAge($year = null)
{
$showYear = $this->birthdayShowYear || WCF::getSession()->getPermission('admin.general.canViewPrivateUserOptions');
if ($year !== null) {
if ($showYear) {
$birthdayYear = 0;
$value = \explode('-', $this->birthday);
$birthdayYear = \intval($value[0]);
if ($birthdayYear) {
return $year - $birthdayYear;
}
}
return 0;
} else {
if ($this->__age === null) {
if ($this->birthday && $showYear) {
$this->__age = DateUtil::getAge($this->birthday);
} else {
$this->__age = 0;
}
}
return $this->__age;
}
}
/**
* Returns the formatted birthday of this user.
*
* @param int $year
* @return string
*/
public function getBirthday($year = null)
{
// split date
$birthdayYear = $month = $day = 0;
$value = \explode('-', $this->birthday);
$birthdayYear = \intval($value[0]);
if (isset($value[1])) {
$month = \intval($value[1]);
}
if (isset($value[2])) {
$day = \intval($value[2]);
}
if (!$month || !$day) {
return '';
}
$showYear = $this->birthdayShowYear || WCF::getSession()->getPermission('admin.general.canViewPrivateUserOptions');
$d = new \DateTimeImmutable($this->birthday, WCF::getUser()->getTimeZone());
$dateFormat = (($showYear && $birthdayYear) ? WCF::getLanguage()->get(DateUtil::DATE_FORMAT) : \str_replace(
'Y',
'',
WCF::getLanguage()->get(DateUtil::DATE_FORMAT)
));
$birthday = DateUtil::localizeDate($d->format($dateFormat), $dateFormat, WCF::getLanguage());
if ($showYear) {
$age = $this->getAge($year);
if ($age > 0) {
$birthday .= ' (' . $age . ')';
}
}
return $birthday;
}
/**
* Returns the age of user account in days.
*
* @return int
*/
public function getProfileAge()
{
return (TIME_NOW - $this->registrationDate) / 86400;
}
/**
* Returns the value of the permission with the given name.
*
* @param string $permission
* @return mixed permission value
*/
public function getPermission($permission)
{
if ($this->groupData === null) {
$this->loadGroupData();
}
if (!isset($this->groupData[$permission])) {
return false;
}
return $this->groupData[$permission];
}
/**
* Returns true if a permission was set to 'Never'. This is required to preserve
* compatibility, while preventing ACLs from overruling a 'Never' setting.
*
* @param string $permission
* @return bool
*/
public function getNeverPermission($permission)
{
$this->loadGroupData();
return isset($this->groupData['__never'][$permission]);
}
/**
* Returns the user title of this user.
*
* @return string
*/
public function getUserTitle()
{
if ($this->userTitle) {
return $this->userTitle;
}
if ($this->getRank() && $this->getRank()->showTitle()) {
return $this->getRank()->getTitle();
}
return '';
}
public function getRank(): ?UserRank
{
if (!\MODULE_USER_RANK) {
return null;
}
if (!$this->rankID) {
return null;
}
return (new UserRankCache())->getCache()[$this->rankID] ?? null;
}
/**
* Loads group data from cache.
*
* @return void
*/
protected function loadGroupData()
{
$this->groupData = UserGroupPermissionCacheBuilder::getInstance()->getData($this->getGroupIDs());
}
/**
* Returns the old username of this user.
*
* @return string
*/
public function getOldUsername()
{
if ($this->oldUsername) {
if ($this->lastUsernameChange + PROFILE_SHOW_OLD_USERNAME * 86400 > TIME_NOW) {
return $this->oldUsername;
}
}
return '';
}
/**
* Returns true if this user can edit his profile.
*
* @return bool
*/
public function canEditOwnProfile()
{
if ($this->pendingActivation() || !$this->getPermission('user.profile.canEditUserProfile')) {
return false;
}
return true;
}
/**
* Returns the encoded email address.
*/
public function getEncodedEmail(): string
{
if ($this->email === '') {
return '';
}
try {
$mailbox = new Mailbox($this->email);
} catch (\Throwable) {
// Skip invalid email addresses.
return '';
}