From 93d66e51fe4357e3ffde6308391b6bc7f060f9e5 Mon Sep 17 00:00:00 2001 From: flashwave Date: Sun, 8 Nov 2015 23:27:42 +0100 Subject: [PATCH] r20151108 Signed-off-by: Flashwave --- .gitignore | 2 + _sakura/components/Forum.php | 1 - _sakura/components/Permissions.php | 2 +- _sakura/components/User.php | 97 +++++++++-- _sakura/components/Users.php | 152 +----------------- _sakura/sakura.php | 2 +- .../templates/yuuno/elements/indexPanel.tpl | 4 +- _sakura/templates/yuuno/main/profile.tpl | 2 +- _sakura/templates/yuuno/profile/comments.tpl | 2 +- public/settings.php | 20 +-- 10 files changed, 102 insertions(+), 182 deletions(-) diff --git a/.gitignore b/.gitignore index 81bfaf3..04e92a0 100755 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea/ +.vs/ errors.log _sakura/config/config.ini BingSiteAuth.xml @@ -12,3 +13,4 @@ ehthumbs.db Desktop.ini $RECYCLE.BIN/ .DS_Store +*.phpproj diff --git a/_sakura/components/Forum.php b/_sakura/components/Forum.php index df4a7e7..5f2be81 100644 --- a/_sakura/components/Forum.php +++ b/_sakura/components/Forum.php @@ -11,5 +11,4 @@ namespace Sakura; */ class Forum { - } diff --git a/_sakura/components/Permissions.php b/_sakura/components/Permissions.php index c221629..db45871 100755 --- a/_sakura/components/Permissions.php +++ b/_sakura/components/Permissions.php @@ -162,7 +162,7 @@ class Permissions $userPerms = Database::fetch('permissions', false, ['rank_id' => [0, '='], 'user_id' => [$user->id(), '=']]); // Get their rank permissions - $rankPerms = self::getRankPermissions(json_decode($user->ranks(), true)); + $rankPerms = self::getRankPermissions($user->ranks()); // 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 729259e..0e73d7d 100755 --- a/_sakura/components/User.php +++ b/_sakura/components/User.php @@ -12,7 +12,29 @@ namespace Sakura; class User { // User data - private $data = []; + private $data = [ + 'user_id' => 0, + 'username' => 'User', + 'username_clean' => 'user', + 'password_hash' => '', + 'password_salt' => '', + 'password_algo' => 'nologin', + 'password_iter' => 0, + 'password_chan' => 0, + 'password_new' => '', + 'email' => 'sakura@localhost', + 'rank_main' => 0, + 'user_ranks' => '[0]', + 'user_colour' => '', + 'register_ip' => '127.0.0.1', + 'last_ip' => '127.0.0.1', + 'user_title' => '', + 'user_registered' => 0, + 'user_last_online' => 0, + 'user_birthday' => '', + 'user_country' => 'XX', + 'user_data' => '[]', + ]; private $ranks = []; private $mainRank = []; @@ -20,7 +42,7 @@ class User public function __construct($uid) { // Get the user database row - $this->data = Database::fetch( + $getUser = Database::fetch( 'users', false, [ @@ -30,17 +52,17 @@ class User ); // Check if the user actually exists - if (empty($this->data)) { + if (!empty($getUser)) { // If not assign as the fallback user - $this->data = Users::$emptyUser; + $this->data = $getUser; } // Decode the json in the user_data column $this->data['user_data'] = json_decode(!empty($this->data['user_data']) ? $this->data['user_data'] : '[]', true); - $this->data['ranks'] = json_decode($this->data['user_ranks'], true); + $this->data['user_ranks'] = json_decode($this->data['user_ranks'], true); // Get the rows for all the ranks - foreach ($this->data['ranks'] as $rank) { + foreach ($this->data['user_ranks'] as $rank) { // Store the database row in the array $this->ranks[$rank] = new Rank($rank); } @@ -169,11 +191,33 @@ class User return $times; } - // Check if the user has the specified ranks - public function checkIfUserHasRanks($ranks) - { + // Set the main rank of this user + public function setMainRank($rank) + { + // Only allow this if this rank is actually present in their set of ranks + if (!in_array($rank, $this->ranks())) { + return false; + } + + // If it does exist update their row + Database::update('user', [ + [ + 'rank_main' => $rank, + ], + [ + 'user_id' => [$this->id(), '='], + ], + ]); + + // Return true if everything was successful + return true; + } + + // Check if this user has the specified ranks + public function hasRanks($ranks) + { // Check if the main rank is the specified rank - if ($this->mainRank->id() === $ranks) { + if (in_array($this->mainRank->id(), $ranks)) { return true; } @@ -187,6 +231,12 @@ class User // If all fails return false return false; + } + + // For compatibility, too lazy to update the references right now! + public function checkIfUserHasRanks($ranks) + { + return $this->hasRanks($ranks); } // Add a new friend @@ -308,12 +358,12 @@ class User // If there's nothing just return null if (!count($profileFields)) { - return; + return []; } // Once again if nothing was returned just return null if (empty($this->data['user_data']['profileFields'])) { - return; + return []; } // Create output array @@ -374,12 +424,12 @@ class User // If there's nothing just return null if (!count($optionFields)) { - return; + return []; } // Once again if nothing was returned just return null if (empty($this->data['user_data']['userOptions'])) { - return; + return []; } // Create output array @@ -670,4 +720,23 @@ class User // Return success return [1, 'SUCCESS']; } + + // Update a user's userData + public function setUserData($data) { + // Merge the arrays + $data = array_merge($this->userData(), $data); + + // Encode it + $data = json_encode($data); + + // Save it in the database + Database::update('users', [ + [ + 'user_data' => $data, + ], + [ + 'user_id' => [$this->id(), '='], + ], + ]); + } } diff --git a/_sakura/components/Users.php b/_sakura/components/Users.php index 228ea3b..f71c68e 100755 --- a/_sakura/components/Users.php +++ b/_sakura/components/Users.php @@ -11,31 +11,6 @@ namespace Sakura; */ class Users { - // Empty user template - public static $emptyUser = [ - 'user_id' => 0, - 'username' => 'User', - 'username_clean' => 'user', - 'password_hash' => '', - 'password_salt' => '', - 'password_algo' => 'nologin', - 'password_iter' => 1000, - 'password_chan' => 0, - 'password_new' => '', - 'email' => 'sakura@localhost', - 'rank_main' => 0, - 'user_ranks' => '[0]', - 'user_colour' => '', - 'register_ip' => '127.0.0.1', - 'last_ip' => '127.0.0.1', - 'user_title' => '', - 'user_registered' => 0, - 'user_last_online' => 0, - 'user_birthday' => '', - 'user_country' => 'XX', - 'user_data' => '[]', - ]; - // Empty rank template public static $emptyRank = [ 'rank_id' => 0, @@ -695,34 +670,12 @@ class Users // Set the default rank of a user public static function setDefaultRank($uid, $rid, $userIdIsUserData = false) { - // Get the specified user - $user = new User($uid); - - // Check if the rank we're trying to set is actually there - if (!in_array($rid, $user->ranks())) { - return false; - } - - // Update the row - Database::update('users', [ - [ - 'rank_main' => $rid, - ], - [ - 'user_id' => [$uid, '='], - ], - ]); - - // Return true if everything was successful - return true; + return (new User($uid))->setMainRank($rid); } // Add a rank to a user public static function addRanksToUser($ranks, $uid, $userIdIsUserData = false) { - // Get the specified user - $user = new User($uid); - // Define $current $current = []; @@ -757,6 +710,8 @@ class Users // 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 @@ -853,103 +808,6 @@ class Users return $fields; } - // Get user's profile fields - public static function getUserProfileFields($id, $inputIsData = false) - { - // Get profile fields - $profileFields = Database::fetch('profilefields'); - - // If there's nothing just return null - if (!count($profileFields)) { - return null; - } - - // Assign the profileData variable - $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'])) { - return null; - } - - // Redeclare profileData - $profileData = $profileData['profileFields']; - - // Create output array - $profile = []; - - // Check if profile fields aren't fake - foreach ($profileFields as $field) { - // Completely strip all special characters from the field name - $fieldName = Main::cleanString($field['name'], true, true); - - // Check if the user has the current field set otherwise continue - if (!array_key_exists($fieldName, $profileData)) { - continue; - } - - // Assign field to output with value - $profile[$fieldName] = []; - $profile[$fieldName]['name'] = $field['name']; - $profile[$fieldName]['value'] = $profileData[$fieldName]; - $profile[$fieldName]['islink'] = $field['islink']; - - // If the field is set to be a link add a value for that as well - if ($field['islink']) { - $profile[$fieldName]['link'] = str_replace('{{ VAL }}', $profileData[$fieldName], $field['linkformat']); - } - - // Check if we have additional options as well - if ($field['additional'] != null) { - // Decode the json of the additional stuff - $additional = json_decode($field['additional'], true); - - // Go over all additional forms - foreach ($additional as $subName => $subField) { - // Check if the user has the current field set otherwise continue - if (!array_key_exists($subName, $profileData)) { - continue; - } - - // Assign field to output with value - $profile[$fieldName][$subName] = $profileData[$subName]; - } - } - } - - // Return appropiate profile data - return $profile; - } - - // Updating the profile data of a user - public static function updateUserDataField($id, $data) - { - // We retrieve the current content from the database - $current = (new User($id))->userData(); - - // Merge the arrays - $data = array_merge($current, $data); - - // Encode the json - $data = json_encode($data); - - // Store it in the database - Database::update('users', [ - [ - 'user_data' => $data, - ], - [ - 'user_id' => [$id, '='], - ], - ]); - } - - // Check if a user is online - public static function checkUserOnline($id) - { - return (new User($id))->checkOnline(); - } - // Get all online users public static function checkAllOnline() { @@ -1094,7 +952,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, true) + if (self::checkIfUserHasRanks([$rankId], $user->id()) && ($excludeAbyss ? $user->password()['password_algo'] != 'nologin' : true)) { $rank[] = $user; } @@ -1270,7 +1128,7 @@ class Users // Check each user foreach ($friends as $key => $friend) { $friends[ - self::checkUserOnline($getData ? $friend['user']->id() : $friend) ? 'online' : 'offline' + (new User($getData ? $friend['user']->id() : $friend))->checkOnline() ? 'online' : 'offline' ][] = $friend; } } diff --git a/_sakura/sakura.php b/_sakura/sakura.php index a5f1c26..294e83c 100755 --- a/_sakura/sakura.php +++ b/_sakura/sakura.php @@ -8,7 +8,7 @@ namespace Sakura; // Define Sakura version -define('SAKURA_VERSION', '20151107'); +define('SAKURA_VERSION', '20151108'); define('SAKURA_VLABEL', 'Eminence'); define('SAKURA_COLOUR', '#6C3082'); define('SAKURA_STABLE', false); diff --git a/_sakura/templates/yuuno/elements/indexPanel.tpl b/_sakura/templates/yuuno/elements/indexPanel.tpl index ee11ce8..1fa4539 100755 --- a/_sakura/templates/yuuno/elements/indexPanel.tpl +++ b/_sakura/templates/yuuno/elements/indexPanel.tpl @@ -25,7 +25,7 @@ {% endif %}
Stats
We have {{ stats.userCount }} user{% if stats.userCount != 1 %}s{% endif %}, - {{ stats.newestUser.username }} is the newest user, + {{ stats.newestUser.username }} is the newest user, 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 %} @@ -36,7 +36,7 @@ {% else %} There were no online users in the past 5 minutes. {% endif %} - {#
+ {#
Advertisment
diff --git a/_sakura/templates/yuuno/main/profile.tpl b/_sakura/templates/yuuno/main/profile.tpl index 701ba62..7599459 100755 --- a/_sakura/templates/yuuno/main/profile.tpl +++ b/_sakura/templates/yuuno/main/profile.tpl @@ -42,7 +42,7 @@ - + {##} {% if not noUserpage %} diff --git a/_sakura/templates/yuuno/profile/comments.tpl b/_sakura/templates/yuuno/profile/comments.tpl index f031bbe..ed06d7e 100755 --- a/_sakura/templates/yuuno/profile/comments.tpl +++ b/_sakura/templates/yuuno/profile/comments.tpl @@ -1,3 +1,3 @@ {% set comments = profile.profileComments.comments %} -{% set commentsCategory = 'profile-' ~ profile.data.user_id %} +{% set commentsCategory = 'profile-' ~ profile.id %} {% include 'elements/comments.tpl' %} diff --git a/public/settings.php b/public/settings.php index 2ec8afb..bb20498 100755 --- a/public/settings.php +++ b/public/settings.php @@ -448,7 +448,7 @@ if (isset($_REQUEST['request-notifications']) && $_REQUEST['request-notification $filepath = ROOT . Config::getConfig('user_uploads') . '/'; $filename = $filepath . $mode . '_' . $currentUser->id(); $currfile = isset($currentUser->userData()[$userDataKey]) - && !empty($_OLDFILE = $currentUser->userData()[$userDataKey]) ? $_OLDFILE : null; + && !empty($currentUser->userData()[$userDataKey]) ? $currentUser->userData()[$userDataKey] : null; // Check if $_FILES is set if (!isset($_FILES[$mode]) && empty($_FILES[$mode])) { @@ -606,7 +606,7 @@ if (isset($_REQUEST['request-notifications']) && $_REQUEST['request-notification } // Update database - Users::updateUserDataField($currentUser->id(), $updated); + $currentUser->setUserData($updated); // Set render data $renderData['page'] = [ @@ -645,7 +645,7 @@ if (isset($_REQUEST['request-notifications']) && $_REQUEST['request-notification } // Update database - Users::updateUserDataField($currentUser->id(), ['profileFields' => $store]); + $currentUser->setUserData(['profileFields' => $store]); // Set render data $renderData['page'] = [ @@ -738,7 +738,7 @@ if (isset($_REQUEST['request-notifications']) && $_REQUEST['request-notification } // Update database - Users::updateUserDataField($currentUser->id(), ['userOptions' => $store]); + $currentUser->setUserData(['userOptions' => $store]); // Set render data $renderData['page'] = [ @@ -939,7 +939,7 @@ if (isset($_REQUEST['request-notifications']) && $_REQUEST['request-notification $userPage = base64_encode($_POST['userpage']); // Update database - Users::updateUserDataField($currentUser->id(), ['userPage' => $userPage]); + $currentUser->setUserData(['userPage' => $userPage]); // Set render data $renderData['page'] = [ @@ -957,7 +957,7 @@ if (isset($_REQUEST['request-notifications']) && $_REQUEST['request-notification $signature = base64_encode($_POST['signature']); // Update database - Users::updateUserDataField($currentUser->id(), ['signature' => $signature]); + $currentUser->setUserData(['signature' => $signature]); // Set render data $renderData['page'] = [ @@ -1427,10 +1427,8 @@ if (Users::checkLogin()) { // Profile case 'general.profile': $renderData['profile'] = [ - 'fields' => Users::getProfileFields(), 'months' => [ - 1 => 'January', 2 => 'February', 3 => 'March', @@ -1443,18 +1441,14 @@ if (Users::checkLogin()) { 10 => 'October', 11 => 'November', 12 => 'December', - ], - ]; break; // Options case 'general.options': $renderData['options'] = [ - 'fields' => Users::getOptionFields(), - ]; break; @@ -1482,14 +1476,12 @@ if (Users::checkLogin()) { case 'appearance.avatar': case 'appearance.background': $renderData[$mode] = [ - 'max_width' => Config::getConfig($mode . '_max_width'), 'max_height' => Config::getConfig($mode . '_max_height'), 'min_width' => Config::getConfig($mode . '_min_width'), 'min_height' => Config::getConfig($mode . '_min_height'), 'max_size' => Config::getConfig($mode . '_max_fsize'), 'max_size_view' => Main::getByteSymbol(Config::getConfig($mode . '_max_fsize')), - ]; break;