From 082bd62efc9dad71b1b674a3627ee89e7db66e56 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 11 Nov 2015 22:54:56 +0100 Subject: [PATCH] r20151111.1 Signed-off-by: Flashwave --- _sakura/components/Main.php | 9 +- _sakura/components/User.php | 90 +++++--- _sakura/components/Users.php | 214 ++---------------- _sakura/cron.php | 6 - _sakura/sakura.php | 1 + .../templates/yuuno/elements/indexPanel.tpl | 6 +- _sakura/templates/yuuno/main/profile.tpl | 4 +- .../yuuno/settings/friends.listing.tpl | 10 +- .../yuuno/settings/friends.requests.tpl | 12 +- public/imageserve.php | 6 +- public/index.php | 4 - public/settings.php | 57 +++-- public/support.php | 6 +- 13 files changed, 134 insertions(+), 291 deletions(-) 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 @@
Hi, {{ user.username }}!
@@ -29,12 +29,12 @@ it has been {{ stats.lastRegDate }} since the last user registered and the forum has {{ stats.topicCount }} thread{% if stats.topicCount != 1 %}s{% endif %} and {{ stats.postCount }} post{% if stats.postCount != 1 %}s{% endif %}.
Online Users
{% if stats.onlineUsers %} - All active users in the past 5 minutes:
+ All active users in the past {{ sakura.onlineTimeout / 60 }} minute{% if sakura.onlineTimeout != 60 %}s{% endif %}:
{% for amount,onlineUser in stats.onlineUsers %} {{ onlineUser.username }}{% if amount != (stats.onlineUsers|length - 1) %}, {% endif %} {% endfor %} {% else %} - There were no online users in the past 5 minutes. + There were no online users in the past {{ sakura.onlineTimeout / 60 }} minute{% if sakura.onlineTimeout != 60 %}s{% endif %}. {% endif %} {#
Advertisment
diff --git a/_sakura/templates/yuuno/main/profile.tpl b/_sakura/templates/yuuno/main/profile.tpl index 7599459..603daee 100755 --- a/_sakura/templates/yuuno/main/profile.tpl +++ b/_sakura/templates/yuuno/main/profile.tpl @@ -34,8 +34,8 @@ {% if user.id == profile.id %} {% else %} - {% if user.checkFriends(profile.id) != 0 %}{% endif %} - + {% if user.isFriends(profile.id) != 0 %}{% endif %} + {% endif %}
diff --git a/_sakura/templates/yuuno/settings/friends.listing.tpl b/_sakura/templates/yuuno/settings/friends.listing.tpl index 5e2a896..eb9fe34 100755 --- a/_sakura/templates/yuuno/settings/friends.listing.tpl +++ b/_sakura/templates/yuuno/settings/friends.listing.tpl @@ -13,13 +13,13 @@ window.addEventListener("load", function() { {% if friends|length %}
{% for friend in friends[page.currentPage] %} -
- - {{ friend.user.username }} -
{{ friend.user.username }}
+
diff --git a/_sakura/templates/yuuno/settings/friends.requests.tpl b/_sakura/templates/yuuno/settings/friends.requests.tpl index 45ab4eb..032ea84 100755 --- a/_sakura/templates/yuuno/settings/friends.requests.tpl +++ b/_sakura/templates/yuuno/settings/friends.requests.tpl @@ -13,14 +13,14 @@ window.addEventListener("load", function() { {% if friends|length %}
{% for friend in friends[page.currentPage] %} -
- - {{ friend.user.username }} -
{{ friend.user.username }}
+
diff --git a/public/imageserve.php b/public/imageserve.php index 1d6b409..9889f92 100755 --- a/public/imageserve.php +++ b/public/imageserve.php @@ -49,7 +49,7 @@ if (isset($_GET['m'])) { $user = new User($_GET['u']); // If user is deactivated use deactive avatar - if ($user->checkIfUserHasRanks([0, 1])) { + if ($user->hasRanks([0, 1])) { $serveImage = $deactiveAvatar; break; } @@ -84,7 +84,7 @@ if (isset($_GET['m'])) { $user = new User($_GET['u']); // If user is deactivated use deactive avatar - if ($user->checkIfUserHasRanks([0, 1])) { + if ($user->hasRanks([0, 1])) { $serveImage = $noBackground; break; } @@ -120,7 +120,7 @@ if (isset($_GET['m'])) { $user = new User($_GET['u']); // If user is deactivated use deactive avatar - if ($user->checkIfUserHasRanks([0, 1])) { + if ($user->hasRanks([0, 1])) { $serveImage = $noHeader; break; } diff --git a/public/index.php b/public/index.php index 334d4f2..2fbd949 100755 --- a/public/index.php +++ b/public/index.php @@ -50,10 +50,6 @@ $renderData['news'] = ($forumMode ? null : (new News(Config::getConfig('site_new $renderData['newsCount'] = Config::getConfig('front_page_news_posts'); -$renderData['page'] = [ - 'friend_req' => Users::getPendingFriends(), -]; - $renderData['board'] = [ 'forums' => ($forumMode ? Forums::getForumList() : null), 'viewforum' => false, diff --git a/public/settings.php b/public/settings.php index 11bcdb0..b32541e 100755 --- a/public/settings.php +++ b/public/settings.php @@ -51,7 +51,8 @@ if (isset($_REQUEST['request-notifications']) && $_REQUEST['request-notification } // Check if friendOnline is set (so it doesn't tell you all your friends all online on first visit) - $onlineNotify = isset($_SESSION['friendsOnline']) ? $_SESSION['friendsOnline'] : []; + $onlineFriends = isset($_SESSION['friendsOnline']) ? $_SESSION['friendsOnline'] : []; + $onlineNotify = isset($_SESSION['friendsOnline']); // Populate the array foreach ($currentUser->friends(1) as $friend) { @@ -59,34 +60,38 @@ if (isset($_REQUEST['request-notifications']) && $_REQUEST['request-notification $online = $friend->isOnline(); // If true check if they're already in the array - if($online && !in_array($friend->id(), $onlineNotify)) { + if ($online && !in_array($friend->id(), $onlineFriends)) { // Add user to the online array $_SESSION['friendsOnline'][$friend->id()] = $friend->id(); // Add the notification to the display array - $notifications[time()] = [ - 'read' => 0, - 'title' => $friend->username() . ' is online.', - 'text' => '', - 'link' => '', - 'img' => '/a/' . $friend->id(), - 'timeout' => 2000, - 'sound' => false, - ]; - } elseif(!$online && in_array($friend->id(), $onlineNotify)) { + if($onlineNotify) { + $notifications[] = [ + 'read' => 0, + 'title' => $friend->username() . ' is online.', + 'text' => '', + 'link' => '', + 'img' => '/a/' . $friend->id(), + 'timeout' => 2000, + 'sound' => false, + ]; + } + } elseif (!$online && in_array($friend->id(), $onlineFriends)) { // Remove the person from the array unset($_SESSION['friendsOnline'][$friend->id()]); // Add the notification to the display array - $notifications[time()] = [ - 'read' => 0, - 'title' => $friend->username() . ' is offline.', - 'text' => '', - 'link' => '', - 'img' => '/a/' . $friend->id(), - 'timeout' => 2000, - 'sound' => false, - ]; + if($onlineNotify) { + $notifications[] = [ + 'read' => 0, + 'title' => $friend->username() . ' is offline.', + 'text' => '', + 'link' => '', + 'img' => '/a/' . $friend->id(), + 'timeout' => 2000, + 'sound' => false, + ]; + } } } @@ -346,32 +351,26 @@ if (isset($_REQUEST['request-notifications']) && $_REQUEST['request-notification // Set the messages $messages = [ - 'USER_NOT_EXIST' => 'The user you tried to add doesn\'t exist.', 'ALREADY_FRIENDS' => 'You are already friends with this person!', 'FRIENDS' => 'You are now mutual friends!', 'NOT_MUTUAL' => 'A friend request has been sent to this person.', 'ALREADY_REMOVED' => 'You aren\'t friends with this person.', 'REMOVED' => 'Removed this person from your friends list.', - ]; // Notification strings $notifStrings = [ - 'FRIENDS' => ['%s accepted your friend request!', 'You can now do mutual friend things!'], 'NOT_MUTUAL' => ['%s added you as a friend!', 'Click here to add them as well.'], 'REMOVED' => ['%s removed you from their friends.', 'You can no longer do friend things now ;_;'], - ]; // Add page specific things $renderData['page'] = [ - 'redirect' => $redirect, 'message' => $messages[$action[1]], 'success' => $action[0], - ]; // Create a notification @@ -1492,12 +1491,12 @@ if (Users::checkLogin()) { // Friends case 'friends.listing': - $renderData['friends'] = array_chunk(array_reverse(Users::getFriends(null, true, true)), 12, true); + $renderData['friends'] = array_chunk(array_reverse($currentUser->friends(1)), 12, true); break; // Pending Friend Requests case 'friends.requests': - $renderData['friends'] = array_chunk(array_reverse(Users::getPendingFriends(null, true)), 12, true); + $renderData['friends'] = array_chunk(array_reverse($currentUser->friends(-1)), 12, true); break; // PM inbox diff --git a/public/support.php b/public/support.php index 2b9cbd9..095d5fd 100755 --- a/public/support.php +++ b/public/support.php @@ -63,7 +63,7 @@ if (isset($_REQUEST['mode']) $total, $itemName, Config::getConfig('sitename') . ' Premium Purchase', - 'https://' . Config::getConfig('url_main') . $urls->format('SITE_PREMIUM') + 'http://' . Config::getConfig('url_main') . $urls->format('SITE_PREMIUM') )) { // Store the amount of months in the global session array $_SESSION['premiumMonths'] = (int) $_POST['months']; @@ -119,7 +119,7 @@ if (isset($_REQUEST['mode']) case 'complete': $renderData = array_merge([ 'page' => [ - 'expiration' => ($prem = Users::checkUserPremium($currentUser->id())[2]) !== null ? $prem : 0, + 'expiration' => ($prem = $currentUser->isPremium()[2]) !== null ? $prem : 0, ], ], $renderData); @@ -163,7 +163,7 @@ $renderData['page'] = [ 'fail' => isset($_GET['fail']), 'price' => Config::getConfig('premium_price_per_month'), - 'current' => $currentUser->checkPremium(), + 'current' => $currentUser->isPremium(), 'amount_max' => Config::getConfig('premium_amount_max'), ];