diff --git a/includes/ajaxHandler/account.class.php b/includes/ajaxHandler/account.class.php index 3ebb615a..6a430208 100644 --- a/includes/ajaxHandler/account.class.php +++ b/includes/ajaxHandler/account.class.php @@ -27,7 +27,7 @@ class AjaxAccount extends AjaxHandler { parent::__construct($params); - if (!$this->params || !User::$id) + if (!$this->params || !User::isLoggedIn()) return; // select handler diff --git a/includes/ajaxHandler/comment.class.php b/includes/ajaxHandler/comment.class.php index 2f77b0f1..0e943f01 100644 --- a/includes/ajaxHandler/comment.class.php +++ b/includes/ajaxHandler/comment.class.php @@ -172,7 +172,7 @@ class AjaxComment extends AjaxHandler protected function handleCommentDelete() : void { - if (!$this->_post['id'] || !User::$id) + if (!$this->_post['id'] || !User::isLoggedIn()) { trigger_error('AjaxComment::handleCommentDelete - commentId empty or user not logged in', E_USER_ERROR); return; @@ -204,7 +204,7 @@ class AjaxComment extends AjaxHandler protected function handleCommentUndelete() : void { - if (!$this->_post['id'] || !User::$id) + if (!$this->_post['id'] || !User::isLoggedIn()) { trigger_error('AjaxComment::handleCommentUndelete - commentId empty or user not logged in', E_USER_ERROR); return; @@ -242,7 +242,7 @@ class AjaxComment extends AjaxHandler protected function handleCommentVote() : string { - if (!User::$id || !$this->_get['id'] || !$this->_get['rating']) + if (!User::isLoggedIn() || !$this->_get['id'] || !$this->_get['rating']) return Util::toJSON(['error' => 1, 'message' => Lang::main('genericError')]); $target = DB::Aowow()->selectRow('SELECT c.`userId` AS owner, ur.`value` FROM ?_comments c LEFT JOIN ?_user_ratings ur ON ur.`type` = ?d AND ur.`entry` = c.id AND ur.`userId` = ?d WHERE c.id = ?d', RATING_COMMENT, User::$id, $this->_get['id']); @@ -393,7 +393,7 @@ class AjaxComment extends AjaxHandler protected function handleReplyDelete() : void { - if (!User::$id || !$this->_post['id']) + if (!User::isLoggedIn() || !$this->_post['id']) { trigger_error('AjaxComment::handleReplyDelete - commentId empty or user not logged in', E_USER_ERROR); return; @@ -407,7 +407,7 @@ class AjaxComment extends AjaxHandler protected function handleReplyFlag() : void { - if (!User::$id || !$this->_post['id']) + if (!User::isLoggedIn() || !$this->_post['id']) { trigger_error('AjaxComment::handleReplyFlag - commentId empty or user not logged in', E_USER_ERROR); return; diff --git a/includes/ajaxHandler/cookie.class.php b/includes/ajaxHandler/cookie.class.php index 5e018e2b..6fe4f170 100644 --- a/includes/ajaxHandler/cookie.class.php +++ b/includes/ajaxHandler/cookie.class.php @@ -10,7 +10,7 @@ class AjaxCookie extends AjaxHandler public function __construct(array $params) { // note that parent::__construct has to come after this - if (!$params || !User::$id) + if (!$params || !User::isLoggedIn()) return; $this->_get = array( @@ -30,7 +30,7 @@ class AjaxCookie extends AjaxHandler */ protected function handleCookie() : string { - if (User::$id && $this->params && $this->_get[$this->params[0]]) + if (User::isLoggedIn() && $this->params && $this->_get[$this->params[0]]) { if (DB::Aowow()->query('REPLACE INTO ?_account_cookies VALUES (?d, ?, ?)', User::$id, $this->params[0], $this->_get[$this->params[0]])) return '0'; diff --git a/includes/ajaxHandler/edit.class.php b/includes/ajaxHandler/edit.class.php index 4bf2b34f..fc5c753d 100644 --- a/includes/ajaxHandler/edit.class.php +++ b/includes/ajaxHandler/edit.class.php @@ -34,7 +34,7 @@ class AjaxEdit extends AjaxHandler */ protected function handleUpload() : string { - if (!User::$id || $this->_get['guide'] != 1) + if (!User::canWriteGuide() || $this->_get['guide'] != 1) return Util::toJSON(['success' => false, 'error' => '']); require_once('includes/libs/qqFileUploader.class.php'); diff --git a/includes/ajaxHandler/getdescription.class.php b/includes/ajaxHandler/getdescription.class.php index 53b464a1..41802c13 100644 --- a/includes/ajaxHandler/getdescription.class.php +++ b/includes/ajaxHandler/getdescription.class.php @@ -25,7 +25,7 @@ class AjaxGetdescription extends AjaxHandler { $this->contentType = MIME_TYPE_TEXT; - if (!User::$id) + if (!User::canWriteGuide()) return ''; $desc = (new Markup($this->_post['description']))->stripTags(); diff --git a/includes/ajaxHandler/profile.class.php b/includes/ajaxHandler/profile.class.php index 3b61851c..382b37e2 100644 --- a/includes/ajaxHandler/profile.class.php +++ b/includes/ajaxHandler/profile.class.php @@ -103,7 +103,7 @@ class AjaxProfile extends AjaxHandler */ protected function handleLink() : void // links char with account { - if (!User::$id || empty($this->_get['id'])) + if (!User::isLoggedIn() || empty($this->_get['id'])) { trigger_error('AjaxProfile::handleLink - profileId empty or user not logged in', E_USER_ERROR); return; @@ -143,7 +143,7 @@ class AjaxProfile extends AjaxHandler */ protected function handlePin() : void // (un)favorite { - if (!User::$id || empty($this->_get['id'][0])) + if (!User::isLoggedIn() || empty($this->_get['id'][0])) { trigger_error('AjaxProfile::handlePin - profileId empty or user not logged in', E_USER_ERROR); return; @@ -173,7 +173,7 @@ class AjaxProfile extends AjaxHandler */ protected function handlePrivacy() : void // public visibility { - if (!User::$id || empty($this->_get['id'][0])) + if (!User::isLoggedIn() || empty($this->_get['id'][0])) { trigger_error('AjaxProfile::handlePrivacy - profileId empty or user not logged in', E_USER_ERROR); return; @@ -451,7 +451,7 @@ class AjaxProfile extends AjaxHandler */ protected function handleDelete() : void // kill a profile { - if (!User::$id || !$this->_get['id']) + if (!User::isLoggedIn() || !$this->_get['id']) { trigger_error('AjaxProfile::handleDelete - profileId empty or user not logged in', E_USER_ERROR); return; diff --git a/includes/components/report.class.php b/includes/components/report.class.php index 4cd9b794..c3906e1d 100644 --- a/includes/components/report.class.php +++ b/includes/components/report.class.php @@ -134,7 +134,7 @@ class Report return; } - if (!User::$id && !User::$ip) + if (!User::isLoggedIn() && !User::$ip) { trigger_error('Report - could not determine IP for anonymous user', E_USER_ERROR); $this->errorCode = self::ERR_MISCELLANEOUS; @@ -147,7 +147,7 @@ class Report private function checkTargetContext() : int { // check already reported - $field = User::$id ? 'userId' : 'ip'; + $field = User::isLoggedIn() ? 'userId' : 'ip'; if (DB::Aowow()->selectCell('SELECT 1 FROM ?_reports WHERE `mode` = ?d AND `reason`= ?d AND `subject` = ?d AND ?# = ?', $this->mode, $this->reason, $this->subject, $field, User::$id ?: User::$ip)) return self::ERR_ALREADY_REPORTED; diff --git a/includes/user.class.php b/includes/user.class.php index 1b471337..abb56582 100644 --- a/includes/user.class.php +++ b/includes/user.class.php @@ -92,8 +92,8 @@ class User self::$expires = (bool)$uData['allowExpire']; self::$reputation = $uData['reputation']; self::$banStatus = $uData['bans']; - self::$groups = $uData['bans'] & (ACC_BAN_TEMP | ACC_BAN_PERM) ? 0 : intval($uData['userGroups']); - self::$perms = $uData['bans'] & (ACC_BAN_TEMP | ACC_BAN_PERM) ? 0 : intval($uData['userPerms']); + self::$groups = self::isBanned() ? 0 : intval($uData['userGroups']); + self::$perms = self::isBanned() ? 0 : intval($uData['userPerms']); self::$dailyVotes = $uData['dailyVotes']; self::$excludeGroups = $uData['excludeGroups']; @@ -114,7 +114,7 @@ class User // - conscutive visits // - votes per day // - reputation for daily visit - if (self::$id) + if (self::isLoggedIn()) { $lastLogin = DB::Aowow()->selectCell('SELECT curLogin FROM ?_account WHERE id = ?d', self::$id); // either the day changed or the last visit was >24h ago @@ -133,7 +133,7 @@ class User ); // gain rep for daily visit - if (!(self::$banStatus & (ACC_BAN_TEMP | ACC_BAN_PERM)) && !self::isInGroup(U_GROUP_PENDING)) + if (!(self::isBanned()) && !self::isInGroup(U_GROUP_PENDING)) Util::gainSiteReputation(self::$id, SITEREP_ACTION_DAILYVISIT); // increment consecutive visits (next day or first of new month and not more than 48h) @@ -181,7 +181,7 @@ class User $_SESSION['timeout'] = self::$expires ? time() + Cfg::get('SESSION_TIMEOUT_DELAY') : 0; // $_SESSION['dataKey'] does not depend on user login status and is set in User::init() - if (self::$id && $toDB) + if (self::isLoggedIn() && $toDB) DB::Aowow()->query('UPDATE ?_account SET `locale` = ? WHERE `id` = ?', self::$preferedLoc->value, self::$id); } @@ -427,7 +427,7 @@ class User public static function canComment() : bool { - if (!self::$id || self::$banStatus & (ACC_BAN_COMMENT | ACC_BAN_PERM | ACC_BAN_TEMP)) + if (!self::isLoggedIn() || self::isBanned(ACC_BAN_COMMENT)) return false; return self::$perms || self::$reputation >= Cfg::get('REP_REQ_COMMENT'); @@ -435,7 +435,7 @@ class User public static function canReply() : bool { - if (!self::$id || self::$banStatus & (ACC_BAN_COMMENT | ACC_BAN_PERM | ACC_BAN_TEMP)) + if (!self::isLoggedIn() || self::isBanned(ACC_BAN_COMMENT)) return false; return self::$perms || self::$reputation >= Cfg::get('REP_REQ_REPLY'); @@ -443,7 +443,7 @@ class User public static function canUpvote() : bool { - if (!self::$id || self::$banStatus & (ACC_BAN_COMMENT | ACC_BAN_PERM | ACC_BAN_TEMP)) + if (!self::isLoggedIn() || self::isBanned(ACC_BAN_COMMENT)) return false; return self::$perms || (self::$reputation >= Cfg::get('REP_REQ_UPVOTE') && self::$dailyVotes > 0); @@ -451,7 +451,7 @@ class User public static function canDownvote() : bool { - if (!self::$id || self::$banStatus & (ACC_BAN_RATE | ACC_BAN_PERM | ACC_BAN_TEMP)) + if (!self::isLoggedIn() || self::isBanned(ACC_BAN_RATE)) return false; return self::$perms || (self::$reputation >= Cfg::get('REP_REQ_DOWNVOTE') && self::$dailyVotes > 0); @@ -459,7 +459,7 @@ class User public static function canSupervote() : bool { - if (!self::$id || self::$banStatus & (ACC_BAN_RATE | ACC_BAN_PERM | ACC_BAN_TEMP)) + if (!self::isLoggedIn() || self::isBanned(ACC_BAN_RATE) || self::isInGroup(U_GROUP_PENDING)) return false; return self::$reputation >= Cfg::get('REP_REQ_SUPERVOTE'); @@ -467,7 +467,7 @@ class User public static function canUploadScreenshot() : bool { - if (!self::$id || self::$banStatus & (ACC_BAN_SCREENSHOT | ACC_BAN_PERM | ACC_BAN_TEMP)) + if (!self::isLoggedIn() || self::isBanned(ACC_BAN_SCREENSHOT) || self::isInGroup(U_GROUP_PENDING)) return false; return true; @@ -475,7 +475,7 @@ class User public static function canWriteGuide() : bool { - if (!self::$id || self::$banStatus & (ACC_BAN_GUIDE | ACC_BAN_PERM | ACC_BAN_TEMP)) + if (!self::isLoggedIn() || self::isBanned(ACC_BAN_GUIDE) || self::isInGroup(U_GROUP_PENDING)) return false; return true; @@ -483,7 +483,7 @@ class User public static function canSuggestVideo() : bool { - if (!self::$id || self::$banStatus & (ACC_BAN_VIDEO | ACC_BAN_PERM | ACC_BAN_TEMP)) + if (!self::isLoggedIn() || self::isBanned(ACC_BAN_VIDEO) || self::isInGroup(U_GROUP_PENDING)) return false; return true; @@ -494,6 +494,16 @@ class User return self::isInGroup(U_GROUP_PREMIUM) || self::$reputation >= Cfg::get('REP_REQ_PREMIUM'); } + public static function isLoggedIn() : bool + { + return self::$id > 0; // more checks? maybe check pending email verification here? (self::isInGroup(U_GROUP_PENDING)) + } + + public static function isBanned(int $addBanMask = 0x0) : bool + { + return self::$banStatus & (ACC_BAN_TEMP | ACC_BAN_PERM | $addBanMask); + } + /**************/ /* js-related */ @@ -512,7 +522,7 @@ class User public static function getMaxDailyVotes() : int { - if (!self::$id || self::$banStatus & (ACC_BAN_PERM | ACC_BAN_TEMP)) + if (!self::isLoggedIn() || self::isBanned()) return 0; return Cfg::get('USER_MAX_VOTES') + (self::$reputation >= Cfg::get('REP_REQ_VOTEMORE_BASE') ? 1 + intVal((self::$reputation - Cfg::get('REP_REQ_VOTEMORE_BASE')) / Cfg::get('REP_REQ_VOTEMORE_ADD')) : 0); @@ -533,7 +543,7 @@ class User 'cookies' => [] ); - if (!self::$id || self::$banStatus & (ACC_BAN_TEMP | ACC_BAN_PERM)) + if (!self::isLoggedIn() || self::isBanned()) return $gUser; $gUser['commentban'] = !self::canComment(); @@ -646,17 +656,15 @@ class User public static function getCookies() : array { - $data = []; + if (!self::isLoggedIn()) + return []; - if (self::$id) - $data = DB::Aowow()->selectCol('SELECT `name` AS ARRAY_KEY, `data` FROM ?_account_cookies WHERE `userId` = ?d', self::$id); - - return $data; + return DB::Aowow()->selectCol('SELECT `name` AS ARRAY_KEY, `data` FROM ?_account_cookies WHERE `userId` = ?d', self::$id); } public static function getFavorites() : array { - if (!self::$id) + if (!self::isLoggedIn()) return []; $res = DB::Aowow()->selectCol('SELECT `type` AS ARRAY_KEY, `typeId` AS ARRAY_KEY2, `typeId` FROM ?_account_favorites WHERE `userId` = ?d', self::$id); diff --git a/pages/account.php b/pages/account.php index 179b7c7a..ff814d32 100644 --- a/pages/account.php +++ b/pages/account.php @@ -63,10 +63,10 @@ class AccountPage extends GenericPage if ($pageParam) { // requires auth && not authed - if ($this->validCats[$pageParam][0] && !User::$id) + if ($this->validCats[$pageParam][0] && !User::isLoggedIn()) $this->forwardToSignIn('account='.$pageParam); // doesn't require auth && authed - else if (!$this->validCats[$pageParam][0] && User::$id) + else if (!$this->validCats[$pageParam][0] && User::isLoggedIn()) header('Location: ?account', true, 302); // goto dashboard } } @@ -200,7 +200,7 @@ class AccountPage extends GenericPage private function createDashboard() { - if (!User::$id) + if (!User::isLoggedIn()) $this->forwardToSignIn('account'); $user = DB::Aowow()->selectRow('SELECT * FROM ?_account WHERE id = ?d', User::$id); diff --git a/pages/genericPage.class.php b/pages/genericPage.class.php index 9781eba6..74566ec9 100644 --- a/pages/genericPage.class.php +++ b/pages/genericPage.class.php @@ -329,13 +329,13 @@ class GenericPage } // requires authed user - if ($this->reqAuth && !User::$id) + if ($this->reqAuth && !User::isLoggedIn()) $this->forwardToSignIn($_SERVER['QUERY_STRING'] ?? ''); // restricted access if ($this->reqUGroup && !User::isInGroup($this->reqUGroup)) { - if (User::$id) + if (User::isLoggedIn()) $this->error(); else $this->forwardToSignIn($_SERVER['QUERY_STRING'] ?? ''); diff --git a/pages/guide.php b/pages/guide.php index 9b0c62f4..e002809c 100644 --- a/pages/guide.php +++ b/pages/guide.php @@ -489,7 +489,7 @@ class GuidePage extends GenericPage if ($id = DB::Aowow()->selectCell('SELECT `id` FROM ?_guides WHERE `id` = ?d AND `status` <> ?d {AND `userId` = ?d}', $this->typeId, GUIDE_STATUS_ARCHIVED, User::isInGroup(U_GROUP_STAFF) ? DBSIMPLE_SKIP : User::$id)) $this->typeId = intVal($id); } - else if ($this->_get['id'] === 0) // create new guide and load in editor + else if ($this->_get['id'] === 0) // create new guide and load in editor $this->typeId = DB::Aowow()->query('INSERT INTO ?_guides (`userId`, `date`, `status`) VALUES (?d, ?d, ?d)', User::$id, time(), GUIDE_STATUS_DRAFT); return $this->typeId > 0; diff --git a/pages/guides.php b/pages/guides.php index 13201813..a2194a6c 100644 --- a/pages/guides.php +++ b/pages/guides.php @@ -29,7 +29,7 @@ class GuidesPage extends GenericPage if ($pageCall == 'my-guides') { - if (!User::$id) + if (!User::isLoggedIn()) $this->error(); $this->name = Util::ucFirst(Lang::guide('myGuides')); @@ -81,7 +81,7 @@ class GuidesPage extends GenericPage $this->lvTabs[] = [GuideList::$brickFile, $tabData]; - $this->redButtons = [BUTTON_GUIDE_NEW => User::$id && User::canComment()]; + $this->redButtons = [BUTTON_GUIDE_NEW => User::canWriteGuide()]; } protected function generateTitle() diff --git a/pages/more.php b/pages/more.php index af896d8a..a20f39e6 100644 --- a/pages/more.php +++ b/pages/more.php @@ -139,13 +139,12 @@ class MorePage extends GenericPage private function handleReputationPage() { - if (!User::$id) + if (!User::isLoggedIn()) return; - if ($repData = DB::Aowow()->select('SELECT action, amount, date AS \'when\', IF(action IN (3, 4, 5), sourceA, 0) AS param FROM ?_account_reputation WHERE userId = ?d', User::$id)) + if ($repData = DB::Aowow()->select('SELECT `action`, `amount`, `date` AS "when", IF(`action` IN (3, 4, 5), `sourceA`, 0) AS "param" FROM ?_account_reputation WHERE `userId` = ?d', User::$id)) { - foreach ($repData as &$r) - $r['when'] = date(Util::$dateFormatInternal, $r['when']); + array_walk($repData, fn(&$x) => $x['when'] = date(Util::$dateFormatInternal, $x['when'])); $this->tabsTitle = Lang::main('yourRepHistory'); $this->lvTabs[] = ['reputationhistory', array( diff --git a/pages/user.php b/pages/user.php index 800fcabc..44219600 100644 --- a/pages/user.php +++ b/pages/user.php @@ -31,12 +31,12 @@ class UserPage extends GenericPage if ($pageParam) { // todo: check if account is disabled or something - if ($user = DB::Aowow()->selectRow('SELECT a.id, a.user, a.displayName, a.consecutiveVisits, a.userGroups, a.avatar, a.title, a.description, a.joinDate, a.prevLogin, IFNULL(SUM(ar.amount), 0) AS sumRep FROM ?_account a LEFT JOIN ?_account_reputation ar ON a.id = ar.userId WHERE a.user = ? GROUP BY a.id', $pageParam)) + if ($user = DB::Aowow()->selectRow('SELECT a.`id`, a.`user`, a.`displayName`, a.`consecutiveVisits`, a.`userGroups`, a.`avatar`, a.`title`, a.`description`, a.`joinDate`, a.`prevLogin`, IFNULL(SUM(ar.`amount`), 0) AS "sumRep" FROM ?_account a LEFT JOIN ?_account_reputation ar ON a.`id` = ar.`userId` WHERE LOWER(a.`displayName`) = LOWER(?) GROUP BY a.`id`', $pageParam)) $this->user = $user; else $this->notFound(sprintf(Lang::user('notFound'), $pageParam)); } - else if (User::$id) + else if (User::isLoggedIn()) { header('Location: ?user='.User::$displayName, true, 302); die(); diff --git a/template/bricks/headerMenu.tpl.php b/template/bricks/headerMenu.tpl.php index 83705279..89883b26 100644 --- a/template/bricks/headerMenu.tpl.php +++ b/template/bricks/headerMenu.tpl.php @@ -1,7 +1,7 @@ |'; echo ''.User::$displayName.''; echo '('.User::getReputation().')'; diff --git a/template/bricks/pageTemplate.tpl.php b/template/bricks/pageTemplate.tpl.php index bc068a8d..1a9e5d77 100644 --- a/template/bricks/pageTemplate.tpl.php +++ b/template/bricks/pageTemplate.tpl.php @@ -17,7 +17,7 @@ if (!empty($this->gPageInfo)): echo " var g_pageInfo = ".Util::toJSON($this->gPageInfo).";\n"; // only used by item.php - if (User::$id > 0 && isset($this->redButtons[BUTTON_EQUIP]) && $this->redButtons[BUTTON_EQUIP]): + if (User::isLoggedIn() && isset($this->redButtons[BUTTON_EQUIP])): echo " DomContentLoaded.addEvent(function() { pr_addEquipButton('equip-pinned-button', ".$this->typeId."); });\n"; endif; endif; diff --git a/template/localized/contrib_0.tpl.php b/template/localized/contrib_0.tpl.php index 55902f66..780389e6 100644 --- a/template/localized/contrib_0.tpl.php +++ b/template/localized/contrib_0.tpl.php @@ -25,7 +25,7 @@
You are not logged in. Please log in or register an account to add your comment.