User/Misc

* floating changes
 * codify user checks into functions
This commit is contained in:
Sarjuuk
2025-04-02 22:27:05 +02:00
parent 3078763ec3
commit 682b315e17
22 changed files with 78 additions and 71 deletions

View File

@@ -27,7 +27,7 @@ class AjaxAccount extends AjaxHandler
{
parent::__construct($params);
if (!$this->params || !User::$id)
if (!$this->params || !User::isLoggedIn())
return;
// select handler

View File

@@ -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;

View File

@@ -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';

View File

@@ -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');

View File

@@ -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();

View File

@@ -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;

View File

@@ -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;

View File

@@ -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);