diff --git a/_sakura/components/Permissions.php b/_sakura/components/Permissions.php index 59ee9d2..c221629 100755 --- a/_sakura/components/Permissions.php +++ b/_sakura/components/Permissions.php @@ -159,10 +159,10 @@ class Permissions $user = new User($uid); // Attempt to get the permission row of a user - $userPerms = Database::fetch('permissions', false, ['rank_id' => [0, '='], 'user_id' => [$user->data['user_id'], '=']]); + $userPerms = Database::fetch('permissions', false, ['rank_id' => [0, '='], 'user_id' => [$user->id(), '=']]); // Get their rank permissions - $rankPerms = self::getRankPermissions(json_decode($user->data['user_ranks'], true)); + $rankPerms = self::getRankPermissions(json_decode($user->ranks(), true)); // Just return the rank permissions if no special ones are set if (empty($userPerms)) { diff --git a/_sakura/components/User.php b/_sakura/components/User.php index 2100f3a..729259e 100755 --- a/_sakura/components/User.php +++ b/_sakura/components/User.php @@ -12,9 +12,9 @@ namespace Sakura; class User { // User data - public $data = []; - public $ranks = []; - public $mainRank = []; + private $data = []; + private $ranks = []; + private $mainRank = []; // Initialise the user object public function __construct($uid) @@ -37,12 +37,10 @@ class User // Decode the json in the user_data column $this->data['user_data'] = json_decode(!empty($this->data['user_data']) ? $this->data['user_data'] : '[]', true); - - // Decode the ranks json array - $ranks = json_decode($this->data['user_ranks'], true); + $this->data['ranks'] = json_decode($this->data['user_ranks'], true); // Get the rows for all the ranks - foreach ($ranks as $rank) { + foreach ($this->data['ranks'] as $rank) { // Store the database row in the array $this->ranks[$rank] = new Rank($rank); } @@ -61,6 +59,116 @@ class User ]; } + // Get user id + public function id() + { + return $this->data['user_id']; + } + + // Get username (or clean variant) + public function username($clean = false) + { + return $this->data['username' . ($clean ? '_clean' : '')]; + } + + // Get password data + public function password() + { + return [ + 'password_hash' => $this->data['password_hash'], + 'password_salt' => $this->data['password_salt'], + 'password_algo' => $this->data['password_algo'], + 'password_iter' => $this->data['password_iter'], + 'password_chan' => $this->data['password_chan'], + 'password_new' => $this->data['password_new'], + ]; + } + + // Get email + public function email() + { + return $this->data['email']; + } + + // Get main rank id + public function mainRank() + { + return $this->data['rank_main']; + } + + // Get all rank ids + public function ranks() + { + return $this->data['user_ranks']; + } + + // Get the user's colour + public function colour() + { + return empty($this->data['user_colour']) ? $this->mainRank->colour() : $this->data['user_colour']; + } + + // Get the user's ip + public function ip($last = false) + { + return $this->data[($last ? 'last' : 'register') . '_ip']; + } + + // Get the user's title + public function userTitle() + { + return empty($this->data['user_title']) ? $this->mainRank->title() : $this->data['user_title']; + } + + // Get user event times + public function dates() + { + return [ + 'joined' => $this->data['user_registered'], + 'lastOnline' => $this->data['user_last_online'], + 'birth' => $this->data['user_birthday'], + ]; + } + + // Get the user's long and short country names + public function country() + { + return [ + 'long' => Main::getCountryName($this->data['user_country']), + 'short' => $this->data['user_country'], + ]; + } + + // Get the user's raw additional settings + public function userData() + { + return $this->data['user_data']; + } + + // Check if a user is online + public function checkOnline() + { + return $this->data['user_last_online'] > (time() - Config::getConfig('max_online_time')); + } + + // Get user's forum statistics + public function forumStats() + { + return Forums::getUserStats($this->data['user_id']); + } + + // Get amount of time since user events using the same format as dates() + public function elapsed($append = ' ago', $none = 'Just now') + { + $times = []; + + foreach ($this->dates() as $key => $val) { + $times[$key] = Main::timeElapsed(is_string($val) ? strtotime($val) : $val, $append, $none); + } + + return $times; + } + // Check if the user has the specified ranks public function checkIfUserHasRanks($ranks) { @@ -81,39 +189,6 @@ class User return false; } - // Get the user's colour - public function colour() - { - return empty($this->data['user_colour']) ? $this->mainRank->colour() : $this->data['user_colour']; - } - - // Get the user's title - public function userTitle() - { - return empty($this->data['user_title']) ? $this->mainRank->title() : $this->data['user_title']; - } - - // Get the user's long and short country names - public function country() - { - return [ - 'long' => Main::getCountryName($this->data['user_country']), - 'short' => $this->data['user_country'], - ]; - } - - // Check if a user is online - public function checkOnline() - { - return $this->data['user_last_online'] > (time() - Config::getConfig('max_online_time')); - } - - // Get user's forum statistics - public function forumStats() - { - return Forums::getUserStats($this->data['user_id']); - } - // Add a new friend public function addFriend($uid) { @@ -225,16 +300,6 @@ class User return new Comments('profile-' . $this->data['user_id']); } - // Get amount of time since user events - public function elapsed($append = ' ago', $none = 'Just now') - { - return [ - 'joined' => Main::timeElapsed($this->data['user_registered'], $append, $none), - 'lastOnline' => Main::timeElapsed($this->data['user_last_online'], $append, $none), - 'birth' => Main::timeElapsed(strtotime($this->data['user_birthday']), $append, $none), - ]; - } - // Get the user's profile fields public function profileFields() { diff --git a/_sakura/components/Users.php b/_sakura/components/Users.php index 224108f..e55b7fc 100755 --- a/_sakura/components/Users.php +++ b/_sakura/components/Users.php @@ -51,7 +51,6 @@ class Users // Check if a user is logged in public static function checkLogin($uid = null, $sid = null) { - // Assign $uid and $sid $uid = $uid ? $uid : (isset($_COOKIE[Config::getConfig('cookie_prefix') . 'id']) ? $_COOKIE[Config::getConfig('cookie_prefix') . 'id'] @@ -130,7 +129,6 @@ class Users // Log a user in public static function login($username, $password, $remember = false, $cookies = true) { - // Check if authentication is disallowed if (Config::getConfig('lock_authentication')) { return [0, 'AUTH_LOCKED']; @@ -153,10 +151,10 @@ class Users } // Get account data - $user = self::getUser($uid); + $user = new User($uid); // Validate password - switch ($user['password_algo']) { + switch ($user->password()['password_algo']) { // Abyssing case 'nologin': return [0, 'NO_LOGIN']; @@ -164,12 +162,12 @@ class Users // Default hashing method default: if (!Hashing::validatePassword($password, [ - $user['password_algo'], - $user['password_iter'], - $user['password_salt'], - $user['password_hash'], + $user->password()['password_algo'], + $user->password()['password_iter'], + $user->password()['password_salt'], + $user->password()['password_hash'], ])) { - return [0, 'INCORRECT_PASSWORD', $user['user_id'], $user['password_chan']]; + return [0, 'INCORRECT_PASSWORD', $user->id(), $user->password()['password_chan']]; } } @@ -213,7 +211,6 @@ class Users // Logout and kill the session public static function logout() { - // Check if user is logged in if (!$check = self::checkLogin()) { return false; @@ -247,7 +244,6 @@ class Users // Register user public static function register($username, $password, $confirmpass, $email, $tos, $captcha = null, $regkey = null) { - // Check if authentication is disallowed if (Config::getConfig('lock_authentication')) { return [0, 'AUTH_LOCKED']; @@ -362,7 +358,6 @@ class Users // Check if a user exists and then send the password forgot email public static function sendPasswordForgot($username, $email) { - // Check if authentication is disallowed if (Config::getConfig('lock_authentication')) { return [0, 'AUTH_LOCKED']; @@ -419,7 +414,6 @@ class Users // Reset password with key public static function resetPassword($verk, $uid, $newpass, $verpass) { - // Check if authentication is disallowed if (Config::getConfig('lock_authentication')) { return [0, 'AUTH_LOCKED']; @@ -468,7 +462,6 @@ class Users // Check if a user exists and then resend the activation e-mail public static function resendActivationMail($username, $email) { - // Check if authentication is disallowed if (Config::getConfig('lock_authentication')) { return [0, 'AUTH_LOCKED']; @@ -554,7 +547,6 @@ class Users // Activating a user public static function activateUser($uid, $requireKey = false, $key = null) { - // Get the user data $user = Database::fetch('users', false, ['user_id' => [$uid, '=']]); @@ -607,7 +599,6 @@ class Users // Deactivating a user public static function deactivateUser($uid) { - // Get the user data $user = Database::fetch('users', false, ['user_id' => [$uid, '=']]); @@ -639,7 +630,6 @@ class Users // Check if registration code is valid public static function checkRegistrationCode($code) { - // Get registration key $keyRow = Database::fetch('regcodes', true, ['code' => [$code, '='], 'key_used' => [0, '=']]); @@ -650,7 +640,6 @@ class Users // Mark registration code as used public static function markRegistrationCodeUsed($code, $uid = 0) { - // Check if the code exists if (!$id = self::checkRegistrationCode($code)) { return false; @@ -674,7 +663,6 @@ class Users // Create new registration code public static function createRegistrationCode($userId) { - // Check if we're logged in if (!self::checkLogin()) { return false; @@ -707,15 +695,11 @@ class Users // Set the default rank of a user public static function setDefaultRank($uid, $rid, $userIdIsUserData = false) { - // Get the specified user - $user = $userIdIsUserData ? $uid : self::getUser($uid); - - // Decode the json - $ranks = json_decode($user['user_ranks'], true); + $user = new User($uid); // Check if the rank we're trying to set is actually there - if (!in_array($rid, $ranks)) { + if (!in_array($rid, $user->ranks())) { return false; } @@ -736,15 +720,11 @@ class Users // Add a rank to a user public static function addRanksToUser($ranks, $uid, $userIdIsUserData = false) { - // Get the specified user - $user = $userIdIsUserData ? $uid : self::getUser($uid); - - // Decode the array - $current = json_decode($user['user_ranks'], true); + $user = new User($uid); // Go over all the new ranks - foreach ($ranks as $rank) { + foreach ($user->ranks() as $rank) { // Check if the user already has this rank and set it if not if (!in_array($rank, $current)) { $current[] = (int) $rank; @@ -771,17 +751,13 @@ class Users // Removing ranks from a user public static function removeRanksFromUser($ranks, $uid, $userIdIsUserData = false) { - // Get the specified user - $user = $userIdIsUserData ? $uid : self::getUser($uid); - - // Get the ranks - $current = json_decode($user['user_ranks'], true); + $user = new User($uid); // Check the current ranks for ranks in the set array foreach ($current as $key => $rank) { // Unset the rank - if (in_array($rank, $ranks)) { + if (in_array($rank, $user->ranks())) { unset($current[$key]); } } @@ -806,13 +782,12 @@ class Users // Check if a user has these ranks public static function checkIfUserHasRanks($ranks, $userid, $userIdIsUserData = false) { - return $userIdIsUserData ? $userid->checkIfUserHasRanks($ranks) : (new User($userid))->checkIfUserHasRanks($ranks); + return (new User($userid))->checkIfUserHasRanks($ranks); } // Check if a user exists public static function userExists($user, $id = true) { - // Clean string $user = Main::cleanString($user, true); @@ -826,7 +801,6 @@ class Users // Get the available profile fields public static function getProfileFields() { - // Get profile fields $profileFields = Database::fetch('profilefields'); @@ -852,7 +826,6 @@ class Users // Get the available option fields public static function getOptionFields() { - // Get option fields $optionFields = Database::fetch('optionfields'); @@ -880,7 +853,6 @@ class Users // Get user's profile fields public static function getUserProfileFields($id, $inputIsData = false) { - // Get profile fields $profileFields = Database::fetch('profilefields'); @@ -890,7 +862,7 @@ class Users } // Assign the profileData variable - $profileData = ($inputIsData ? $id : self::getUser($id)['user_data']); + $profileData = ($inputIsData ? $id : (new User($id))->userData()); // Once again if nothing was returned just return null if (count($profileData) < 1 || $profileData == null || empty($profileData['profileFields'])) { @@ -949,9 +921,8 @@ class Users // Updating the profile data of a user public static function updateUserDataField($id, $data) { - // We retrieve the current content from the database - $current = self::getUser($id)['user_data']; + $current = (new User($id))->userData(); // Merge the arrays $data = array_merge($current, $data); @@ -973,37 +944,31 @@ class Users // Check if a user is online public static function checkUserOnline($id) { - - // Get user - $user = self::getUser($id); - - // Return false if the user doesn't exist because a user that doesn't exist can't be online - if (empty($user)) { - return false; - } - - // Return true if the user was online in the last 5 minutes - return ($user['user_last_online'] > (time() - 500)); + return (new User($id))->checkOnline(); } // Get all online users public static function checkAllOnline() { - // Assign time - 500 to a variable - $time = time() - 500; + $time = time() - Config::getConfig('max_online_time'); + + $return = []; // Get all online users in the past 5 minutes $getAll = Database::fetch('users', true, ['user_last_online' => [$time, '>']]); + foreach ($getAll as $user) { + $return[] = new User($user['user_id']); + } + // Return all the online users - return $getAll; + return $return; } // Add premium to a user public static function addUserPremium($id, $seconds) { - // Check if there's already a record of premium for this user in the database $getUser = Database::fetch('premium', false, [ 'user_id' => [$id, '='], @@ -1046,7 +1011,6 @@ class Users // Check if user has Premium public static function checkUserPremium($id) { - // Check if the user has static premium if (Permissions::check('SITE', 'STATIC_PREMIUM', $id, 1)) { return [2, 0, time() + 1]; @@ -1076,7 +1040,6 @@ class Users // Update the premium data public static function updatePremiumMeta($id) { - // Get the ID for the premium user rank from the database $premiumRank = Config::getConfig('premium_rank_id'); @@ -1089,7 +1052,7 @@ class Users self::addRanksToUser([$premiumRank], $id); // Check if the user's default rank is standard user and update it to premium - if (self::getUser($id)['rank_main'] == 2) { + if (((new User($id))->mainRank()) == 2) { self::setDefaultRank($id, $premiumRank); } } elseif ($check[0] == 0 && count($check) > 1) { @@ -1098,26 +1061,9 @@ class Users } } - // Get user data by id - public static function getUser($id) - { - - // If user was found return user data - return (new User($id))->data; - } - - // Get rank data by id - public static function getRank($id) - { - - // If rank was found return rank data - return (new Rank($id))->data; - } - // Get user(s) by IP public static function getUsersByIP($ip) { - // Get users by registration IP $registeredFrom = Database::fetch('users', true, ['register_ip' => [$ip, '=']]); @@ -1134,7 +1080,6 @@ class Users // Get users in rank public static function getUsersInRank($rankId, $users = null, $excludeAbyss = true) { - // Get all users (or use the supplied user list to keep server load down) if (!$users) { $users = self::getAllUsers(); @@ -1147,7 +1092,7 @@ class Users foreach ($users as $user) { // If so store the user's row in the array if (self::checkIfUserHasRanks([$rankId], $user, true) - && ($excludeAbyss ? $user->data['password_algo'] != 'nologin' : true)) { + && ($excludeAbyss ? $user->password()['password_algo'] != 'nologin' : true)) { $rank[] = $user; } } @@ -1159,7 +1104,6 @@ class Users // Get all users public static function getAllUsers($includeInactive = true, $includeAbyss = false) { - // Execute query $getUsers = Database::fetch('users', true); @@ -1188,7 +1132,6 @@ class Users // Get all ranks public static function getAllRanks() { - // Execute query $getRanks = Database::fetch('ranks', true); @@ -1207,7 +1150,6 @@ class Users // Get all warnings issued to a user (or all warnings a user issued) public static function getWarnings($uid = 0, $iid = false) { - // Do the database query $warnings = Database::fetch('warnings', true, ($uid ? [ ($iid ? 'moderator_id' : 'user_id') => [$uid, '='], @@ -1220,7 +1162,6 @@ class Users // Get a user's notifications public static function getNotifications($uid = null, $timediff = 0, $excludeRead = true, $markRead = false) { - // Prepare conditions $conditions = []; $conditions['user_id'] = [($uid ? $uid : self::checkLogin()[0]), '=']; @@ -1257,7 +1198,6 @@ class Users // Marking notifications as read public static function markNotificationRead($id, $mode = true) { - // Execute an update statement Database::update('notifications', [ [ @@ -1272,7 +1212,6 @@ class Users // Adding a new notification public static function createNotification($user, $title, $text, $timeout = 60000, $img = 'FONT:fa-info-circle', $link = '', $sound = 0) { - // Get current timestamp $time = time(); @@ -1293,35 +1232,12 @@ class Users // Getting a user's PMs public static function getPrivateMessages($from = false) { - - // Get all messages from the database - $messages = Database::fetch('messages', true, [ - ($from ? 'from_user' : 'to_user') => [self::checkLogin()[0], '='], - ]); - - // Prepare a storage array - $store = []; - - // Go over each message and check if they are for the current user - foreach ($messages as $message) { - // Store the message - $store[$message['id']] = $message; - - // Store user data as well - $store[$message['id']]['data']['from']['user'] = ($_MSG_USR = self::getUser($message['from_user'])); - $store[$message['id']]['data']['from']['rank'] = self::getRank($_MSG_USR['rank_main']); - $store[$message['id']]['data']['to']['user'] = ($_MSG_USR = self::getUser($message['to_user'])); - $store[$message['id']]['data']['to']['rank'] = self::getRank($_MSG_USR['rank_main']); - } - - // Return store array - return $store; + return []; } // Get friends public static function getFriends($uid = null, $timestamps = false, $getData = false, $checkOnline = false) { - // Assign $uid if (!$uid) { $uid = Users::checkLogin()[0]; @@ -1340,8 +1256,8 @@ class Users // Add friend to array $friends[($timestamps ? $friend['friend_id'] : $key)] = $getData ? ([ - 'user' => ($_UDATA = self::getUser($friend['friend_id'])), - 'rank' => self::getRank($_UDATA['rank_main']), + 'user' => ($_UDATA = new User($friend['friend_id'])), + 'rank' => new Rank($_UDATA->mainRank()), ]) : $friend[($timestamps ? 'friend_timestamp' : 'friend_id')]; } @@ -1351,7 +1267,7 @@ class Users // Check each user foreach ($friends as $key => $friend) { $friends[ - self::checkUserOnline($getData ? $friend['user']['user_id'] : $friend) ? 'online' : 'offline' + self::checkUserOnline($getData ? $friend['user']->id() : $friend) ? 'online' : 'offline' ][] = $friend; } } @@ -1363,7 +1279,6 @@ class Users // Get non-mutual friends public static function getPendingFriends($uid = null, $getData = false) { - // Assign $of automatically if it's not set if (!$uid) { $uid = self::checkLogin()[0]; @@ -1386,8 +1301,8 @@ class Users if (!$user->checkFriends($friend['user_id'])) { $pending[] = $getData ? ([ - 'user' => ($_UDATA = self::getUser($friend['user_id'])), - 'rank' => self::getRank($_UDATA['rank_main']), + 'user' => ($_UDATA = new User($friend['user_id'])), + 'rank' => new Rank($_UDATA->mainRank()), ]) : $friend; } diff --git a/_sakura/sakura.php b/_sakura/sakura.php index b34f0e4..a5f1c26 100755 --- a/_sakura/sakura.php +++ b/_sakura/sakura.php @@ -8,7 +8,7 @@ namespace Sakura; // Define Sakura version -define('SAKURA_VERSION', '20151106'); +define('SAKURA_VERSION', '20151107'); define('SAKURA_VLABEL', 'Eminence'); define('SAKURA_COLOUR', '#6C3082'); define('SAKURA_STABLE', false); @@ -103,11 +103,7 @@ $templateName = defined('SAKURA_MANAGE') ? Config::getConfig('manage_style') : ( - ( - isset($currentUser->data['user_data']['userOptions']['useMisaki']) && - $currentUser->data['user_data']['userOptions']['useMisaki'] && - $currentUser->checkPermission('SITE', 'ALTER_PROFILE') - ) ? + $currentUser->optionFields()['useMisaki'] ? 'misaki' : Config::getConfig('site_style') ); @@ -197,7 +193,7 @@ if (!defined('SAKURA_NO_TPL')) { } // Ban checking - if ($authCheck && $ban = Bans::checkBan($currentUser->data['user_id'])) { + if ($authCheck && $ban = Bans::checkBan($currentUser->id())) { // Additional render data $renderData = array_merge($renderData, [ diff --git a/_sakura/templates/broomcloset/global/master.tpl b/_sakura/templates/broomcloset/global/master.tpl index 57922ec..e9e785b 100755 --- a/_sakura/templates/broomcloset/global/master.tpl +++ b/_sakura/templates/broomcloset/global/master.tpl @@ -85,9 +85,9 @@