diff --git a/_sakura/components/Main.php b/_sakura/components/Main.php index 484edfe..64bf8c7 100755 --- a/_sakura/components/Main.php +++ b/_sakura/components/Main.php @@ -142,14 +142,13 @@ class Main ] )) { // If so assign the errid - $errid = $past['id']; + $errid = $past['error_id']; } else { // Create an error ID $errid = substr(md5(microtime()), rand(0, 22), 10); // Log the error Database::insert('error_log', [ - 'error_id' => $errid, 'error_timestamp' => date("r"), 'error_revision' => SAKURA_VERSION, @@ -158,7 +157,6 @@ class Main 'error_string' => $errstr, 'error_file' => $errfile, 'error_backtrace' => $backtrace, - ]); } } @@ -188,6 +186,11 @@ class Main ob_clean(); ob_end_clean(); + // Check if this request was made through the ajax thing + if (isset($_REQUEST['ajax'])) { + die('An error occurred while executing the script.|1|javascript:alert("' . (isset($errid) ? 'Error Log ID: '. $errid : 'Failed to log.') . '");'); + } + // Build page $errorPage = ' diff --git a/_sakura/components/User.php b/_sakura/components/User.php index e0a8b7b..88ef8ce 100755 --- a/_sakura/components/User.php +++ b/_sakura/components/User.php @@ -170,6 +170,15 @@ class User // Check if a user is online public function isOnline() { + // Get all sessions + $sessions = Database::fetch('sessions', true, ['user_id' => [$this->id(), '=']]); + + // If there's no entries just straight up return false + if(!$sessions) { + return false; + } + + // Otherwise use the standard method return $this->data['user_last_online'] > (time() - Config::getConfig('max_online_time')); } @@ -197,6 +206,52 @@ class User return $times; } + // Add ranks to a user + public function addRanks($ranks) { + // Update the ranks array + $ranks = array_map('intval', array_unique(array_merge($this->ranks(), $ranks))); + + // Save to the database + Database::update('users', [ + [ + 'user_ranks' => json_encode($ranks), + ], + [ + 'user_id' => [$this->id(), '='], + ], + ]); + } + + // Remove ranks from a user + public function removeRanks($ranks) + { + // Current ranks + $currRanks = $this->ranks(); + + // Iterate over the ranks + foreach ($ranks as $rank) { + // Try to find the value + if($key = array_search($rank, $currRanks)) { + unset($currRanks[$key]); + + // Change the main rank if it's set to the rank that's currently being remove + if ($this->mainRank() == $rank) { + $this->setMainRank($this->ranks()[0]); + } + } + } + + // Save to the database + Database::update('users', [ + [ + 'user_ranks' => json_encode($currRanks), + ], + [ + 'user_id' => [$this->id(), '='], + ], + ]); + } + // Set the main rank of this user public function setMainRank($rank) { @@ -206,7 +261,7 @@ class User } // If it does exist update their row - Database::update('user', [ + Database::update('users', [ [ 'rank_main' => $rank, ], @@ -239,12 +294,6 @@ class User return false; } - // For compatibility, too lazy to update the references right now! - public function checkIfUserHasRanks($ranks) - { - return $this->hasRanks($ranks); - } - // Add a new friend public function addFriend($uid) { @@ -256,11 +305,8 @@ class User return [0, 'USER_NOT_EXIST']; } - // Get check - $check = $this->checkFriends($uid); - // Check if the user already has this user a friend - if ($check) { + if ($this->isFriends($uid)) { return [0, 'ALREADY_FRIENDS']; } @@ -272,7 +318,7 @@ class User ]); // Return true because yay - return [1, $check == 2 ? 'FRIENDS' : 'NOT_MUTUAL']; + return [1, $user->isFriends($this->id()) ? 'FRIENDS' : 'NOT_MUTUAL']; } // Remove a friend @@ -287,7 +333,7 @@ class User } // Check if the user has this user a friend - if (!$this->checkFriends($uid)) { + if (!$this->isFriends($uid)) { return [0, 'ALREADY_REMOVED']; } @@ -334,12 +380,6 @@ class User return 0; } - // Compat. - public function checkFriends($with) - { - return $this->isFriends($with); - } - // Get all the friend of this user public function friends($level = 0, $noObj = false) { @@ -400,12 +440,6 @@ class User return $objects; } - // Compatibility - public function getFriends() - { - return $this->friends(); - } - // Check if the user is banned public function checkBan() { @@ -530,11 +564,11 @@ class User } // Check if user has Premium - public function checkPremium() + public function isPremium() { // Check if the user has static premium - if (Permissions::check('SITE', 'STATIC_PREMIUM', $this->data['user_id'], 1)) { + if ($this->checkPermission('SITE', 'STATIC_PREMIUM')) { return [2, 0, time() + 1]; } @@ -550,8 +584,6 @@ class User // Check if the Tenshi hasn't expired if ($getRecord['premium_expire'] < time()) { - Users::removeUserPremium($this->data['user_id']); - Users::updatePremiumMeta($this->data['user_id']); return [0, $getRecord['premium_start'], $getRecord['premium_expire']]; } diff --git a/_sakura/components/Users.php b/_sakura/components/Users.php index 6fd950c..b9c043d 100755 --- a/_sakura/components/Users.php +++ b/_sakura/components/Users.php @@ -636,82 +636,6 @@ class Users return $code; } - // Set the default rank of a user - public static function setDefaultRank($uid, $rid, $userIdIsUserData = false) - { - return (new User($uid))->setMainRank($rid); - } - - // Add a rank to a user - public static function addRanksToUser($ranks, $uid, $userIdIsUserData = false) - { - // Define $current - $current = []; - - // Go over all the new ranks - foreach ($ranks as $rank) { - // Check if the user already has this rank and set it if not - if (!in_array($rank, $current)) { - $current[] = (int) $rank; - } - } - - // Encode the array - $current = json_encode($current); - - // Update the row - Database::update('users', [ - [ - 'user_ranks' => $current, - ], - [ - 'user_id' => [$uid, '='], - ], - ]); - - // Return true because - return true; - } - - // Removing ranks from a user - public static function removeRanksFromUser($ranks, $uid, $userIdIsUserData = false) - { - // Get the specified user - $user = new User($uid); - - $current = $user->ranks(); - - // Check the current ranks for ranks in the set array - foreach ($current as $key => $rank) { - // Unset the rank - if (in_array($rank, $user->ranks())) { - unset($current[$key]); - } - } - - // Encode the array - $current = json_encode($current); - - // Update the row - Database::update('users', [ - [ - 'user_ranks' => $current, - ], - [ - 'user_id' => [$uid, '='], - ], - ]); - - // Return true - return true; - } - - // Check if a user has these ranks - public static function checkIfUserHasRanks($ranks, $userid, $userIdIsUserData = false) - { - return (new User($userid))->checkIfUserHasRanks($ranks); - } - // Check if a user exists public static function userExists($user, $id = true) { @@ -830,64 +754,35 @@ class Users return $expire; } - // Remove the premium status of a user - public static function removeUserPremium($id) - { - Database::delete('premium', [ - 'user_id' => [$id, '='], - ]); - } - - // 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]; - } - - // Attempt to retrieve the premium record from the database - $getRecord = Database::fetch('premium', false, [ - 'user_id' => [$id, '='], - ]); - - // If nothing was returned just return false - if (empty($getRecord)) { - return [0]; - } - - // Check if the Tenshi hasn't expired - if ($getRecord['premium_expire'] < time()) { - self::removeUserPremium($id); - self::updatePremiumMeta($id); - return [0, $getRecord['premium_start'], $getRecord['premium_expire']]; - } - - // Else return the start and expiration date - return [1, $getRecord['premium_start'], $getRecord['premium_expire']]; - } - // 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'); + // Create user object + $user = new User($id); + // Run the check - $check = self::checkUserPremium($id); + $check = $user->isPremium(); // Check if the user has premium - if ($check[0] == 1) { + if ($check[0]) { // If so add the rank to them - self::addRanksToUser([$premiumRank], $id); + $user->addRanks([$premiumRank]); // Check if the user's default rank is standard user and update it to premium - if (((new User($id))->mainRank()) == 2) { - self::setDefaultRank($id, $premiumRank); + if ($user->mainRank() == 2) { + $user->setMainRank($premiumRank); } - } elseif ($check[0] == 0 && count($check) > 1) { + } elseif (!$check[0] && count($check) > 1) { + // Remove the expired entry + Database::delete('premium', [ + 'user_id' => [$user->id(), '='], + ]); + // Else remove the rank from them - self::removeRanksFromUser([$premiumRank], $id); + $user->removeRanks([$premiumRank]); } } @@ -921,7 +816,7 @@ class Users // Go over all users and check if they have the rank id foreach ($users as $user) { // If so store the user's row in the array - if (self::checkIfUserHasRanks([$rankId], $user->id()) + if ($user->hasRanks([$rankId], $user->id()) && ($excludeAbyss ? $user->password()['password_algo'] != 'nologin' : true)) { $rank[] = $user; } @@ -1047,83 +942,6 @@ class Users ]); } - // Get friends - public static function getFriends($uid = null, $timestamps = false, $getData = false, $checkOnline = false) - { - // Assign $uid - if (!$uid) { - $uid = Users::checkLogin()[0]; - } - - // Get all friends - $getFriends = Database::fetch('friends', true, [ - 'user_id' => [$uid, '='], - ]); - - // Create the friends array - $friends = []; - - // Iterate over the raw database return - foreach ($getFriends as $key => $friend) { - // Add friend to array - $friends[($timestamps ? $friend['friend_id'] : $key)] = $getData ? ([ - - 'user' => ($_UDATA = new User($friend['friend_id'])), - 'rank' => new Rank($_UDATA->mainRank()), - - ]) : $friend[($timestamps ? 'friend_timestamp' : 'friend_id')]; - } - - // Check who is online and who isn't - if ($checkOnline) { - // Check each user - foreach ($friends as $key => $friend) { - $friends[ - (new User($getData ? $friend['user']->id() : $friend))->checkOnline() ? 'online' : 'offline' - ][] = $friend; - } - } - - // Return formatted array - return $friends; - } - - // 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]; - } - - // Get all friend entries from other people involved the current user - $friends = Database::fetch('friends', true, [ - 'friend_id' => [$uid, '='], - ]); - - // Create pending array - $pending = []; - - // Check if the friends are mutual - foreach ($friends as $friend) { - // Create user object - $user = new User($uid); - - // Check if the friend is mutual - if (!$user->checkFriends($friend['user_id'])) { - $pending[] = $getData ? ([ - - 'user' => ($_UDATA = new User($friend['user_id'])), - 'rank' => new Rank($_UDATA->mainRank()), - - ]) : $friend; - } - } - - // Return the pending friends - return $pending; - } - // Get the ID of the newest user public static function getNewestUserId() { diff --git a/_sakura/cron.php b/_sakura/cron.php index 44a7762..ffa9a1c 100755 --- a/_sakura/cron.php +++ b/_sakura/cron.php @@ -26,25 +26,19 @@ set_time_limit(0); // Clean expired sessions Database::delete('sessions', [ - 'session_expire' => [time(), '<'], 'session_remember' => ['1', '!='], - ]); // Delete notifications that are older than a month but not unread Database::delete('notifications', [ - 'alert_timestamp' => [(time() - 109500), '<'], 'alert_read' => ['1', '='], - ]); // Get expired premium accounts $expiredPremium = Database::fetch('premium', true, [ - 'premium_expire' => [time(), '<'], - ]); // Process expired premium accounts diff --git a/_sakura/sakura.php b/_sakura/sakura.php index c052d86..e5cd8e9 100755 --- a/_sakura/sakura.php +++ b/_sakura/sakura.php @@ -139,6 +139,7 @@ if (!defined('SAKURA_NO_TPL')) { 'dateFormat' => Config::getConfig('date_format'), 'currentPage' => '//' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 'referrer' => (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null), + 'onlineTimeout' => Config::getConfig('max_online_time'), 'recaptchaPublic' => Config::getConfig('recaptcha_public'), 'recaptchaEnabled' => Config::getConfig('recaptcha'), diff --git a/_sakura/templates/yuuno/elements/indexPanel.tpl b/_sakura/templates/yuuno/elements/indexPanel.tpl index 1fa4539..0897252 100755 --- a/_sakura/templates/yuuno/elements/indexPanel.tpl +++ b/_sakura/templates/yuuno/elements/indexPanel.tpl @@ -3,7 +3,7 @@