r20151108

Signed-off-by: Flashwave <me@flash.moe>
This commit is contained in:
flash 2015-11-08 23:27:42 +01:00
parent 845193f871
commit 93d66e51fe
10 changed files with 102 additions and 182 deletions

2
.gitignore vendored
View file

@ -1,4 +1,5 @@
.idea/ .idea/
.vs/
errors.log errors.log
_sakura/config/config.ini _sakura/config/config.ini
BingSiteAuth.xml BingSiteAuth.xml
@ -12,3 +13,4 @@ ehthumbs.db
Desktop.ini Desktop.ini
$RECYCLE.BIN/ $RECYCLE.BIN/
.DS_Store .DS_Store
*.phpproj

View file

@ -11,5 +11,4 @@ namespace Sakura;
*/ */
class Forum class Forum
{ {
} }

View file

@ -162,7 +162,7 @@ class Permissions
$userPerms = Database::fetch('permissions', false, ['rank_id' => [0, '='], 'user_id' => [$user->id(), '=']]); $userPerms = Database::fetch('permissions', false, ['rank_id' => [0, '='], 'user_id' => [$user->id(), '=']]);
// Get their rank permissions // 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 // Just return the rank permissions if no special ones are set
if (empty($userPerms)) { if (empty($userPerms)) {

View file

@ -12,7 +12,29 @@ namespace Sakura;
class User class User
{ {
// User data // 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 $ranks = [];
private $mainRank = []; private $mainRank = [];
@ -20,7 +42,7 @@ class User
public function __construct($uid) public function __construct($uid)
{ {
// Get the user database row // Get the user database row
$this->data = Database::fetch( $getUser = Database::fetch(
'users', 'users',
false, false,
[ [
@ -30,17 +52,17 @@ class User
); );
// Check if the user actually exists // Check if the user actually exists
if (empty($this->data)) { if (!empty($getUser)) {
// If not assign as the fallback user // If not assign as the fallback user
$this->data = Users::$emptyUser; $this->data = $getUser;
} }
// Decode the json in the user_data column // 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['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 // 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 // Store the database row in the array
$this->ranks[$rank] = new Rank($rank); $this->ranks[$rank] = new Rank($rank);
} }
@ -169,11 +191,33 @@ class User
return $times; return $times;
} }
// Check if the user has the specified ranks // Set the main rank of this user
public function checkIfUserHasRanks($ranks) 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 // Check if the main rank is the specified rank
if ($this->mainRank->id() === $ranks) { if (in_array($this->mainRank->id(), $ranks)) {
return true; return true;
} }
@ -189,6 +233,12 @@ class User
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 // Add a new friend
public function addFriend($uid) public function addFriend($uid)
{ {
@ -308,12 +358,12 @@ class User
// If there's nothing just return null // If there's nothing just return null
if (!count($profileFields)) { if (!count($profileFields)) {
return; return [];
} }
// Once again if nothing was returned just return null // Once again if nothing was returned just return null
if (empty($this->data['user_data']['profileFields'])) { if (empty($this->data['user_data']['profileFields'])) {
return; return [];
} }
// Create output array // Create output array
@ -374,12 +424,12 @@ class User
// If there's nothing just return null // If there's nothing just return null
if (!count($optionFields)) { if (!count($optionFields)) {
return; return [];
} }
// Once again if nothing was returned just return null // Once again if nothing was returned just return null
if (empty($this->data['user_data']['userOptions'])) { if (empty($this->data['user_data']['userOptions'])) {
return; return [];
} }
// Create output array // Create output array
@ -670,4 +720,23 @@ class User
// Return success // Return success
return [1, '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(), '='],
],
]);
}
} }

View file

@ -11,31 +11,6 @@ namespace Sakura;
*/ */
class Users 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 // Empty rank template
public static $emptyRank = [ public static $emptyRank = [
'rank_id' => 0, 'rank_id' => 0,
@ -695,34 +670,12 @@ class Users
// Set the default rank of a user // Set the default rank of a user
public static function setDefaultRank($uid, $rid, $userIdIsUserData = false) public static function setDefaultRank($uid, $rid, $userIdIsUserData = false)
{ {
// Get the specified user return (new User($uid))->setMainRank($rid);
$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;
} }
// Add a rank to a user // Add a rank to a user
public static function addRanksToUser($ranks, $uid, $userIdIsUserData = false) public static function addRanksToUser($ranks, $uid, $userIdIsUserData = false)
{ {
// Get the specified user
$user = new User($uid);
// Define $current // Define $current
$current = []; $current = [];
@ -757,6 +710,8 @@ class Users
// Get the specified user // Get the specified user
$user = new User($uid); $user = new User($uid);
$current = $user->ranks();
// Check the current ranks for ranks in the set array // Check the current ranks for ranks in the set array
foreach ($current as $key => $rank) { foreach ($current as $key => $rank) {
// Unset the rank // Unset the rank
@ -853,103 +808,6 @@ class Users
return $fields; 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 // Get all online users
public static function checkAllOnline() public static function checkAllOnline()
{ {
@ -1094,7 +952,7 @@ class Users
// Go over all users and check if they have the rank id // Go over all users and check if they have the rank id
foreach ($users as $user) { foreach ($users as $user) {
// If so store the user's row in the array // 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)) { && ($excludeAbyss ? $user->password()['password_algo'] != 'nologin' : true)) {
$rank[] = $user; $rank[] = $user;
} }
@ -1270,7 +1128,7 @@ class Users
// Check each user // Check each user
foreach ($friends as $key => $friend) { foreach ($friends as $key => $friend) {
$friends[ $friends[
self::checkUserOnline($getData ? $friend['user']->id() : $friend) ? 'online' : 'offline' (new User($getData ? $friend['user']->id() : $friend))->checkOnline() ? 'online' : 'offline'
][] = $friend; ][] = $friend;
} }
} }

View file

@ -8,7 +8,7 @@
namespace Sakura; namespace Sakura;
// Define Sakura version // Define Sakura version
define('SAKURA_VERSION', '20151107'); define('SAKURA_VERSION', '20151108');
define('SAKURA_VLABEL', 'Eminence'); define('SAKURA_VLABEL', 'Eminence');
define('SAKURA_COLOUR', '#6C3082'); define('SAKURA_COLOUR', '#6C3082');
define('SAKURA_STABLE', false); define('SAKURA_STABLE', false);

View file

@ -25,7 +25,7 @@
{% endif %} {% endif %}
<div class="head">Stats</div> <div class="head">Stats</div>
We have <b>{{ stats.userCount }} user{% if stats.userCount != 1 %}s{% endif %}</b>, We have <b>{{ stats.userCount }} user{% if stats.userCount != 1 %}s{% endif %}</b>,
<b><a href="{{ urls.format('USER_PROFILE', [stats.newestUser.id]) }}" class="default">{{ stats.newestUser.username }}</a></b> is the newest user, <b><a href="{{ urls.format('USER_PROFILE', [stats.newestUser.id]) }}" style="color: {{ stats.newestUser.colour }};" class="default">{{ stats.newestUser.username }}</a></b> is the newest user,
it has been <b>{{ stats.lastRegDate }}</b> since the last user registered and the forum has <b>{{ stats.topicCount }} thread{% if stats.topicCount != 1 %}s{% endif %}</b> and <b>{{ stats.postCount }} post{% if stats.postCount != 1 %}s{% endif %}</b>. it has been <b>{{ stats.lastRegDate }}</b> since the last user registered and the forum has <b>{{ stats.topicCount }} thread{% if stats.topicCount != 1 %}s{% endif %}</b> and <b>{{ stats.postCount }} post{% if stats.postCount != 1 %}s{% endif %}</b>.
<div class="head">Online Users</div> <div class="head">Online Users</div>
{% if stats.onlineUsers %} {% if stats.onlineUsers %}
@ -36,7 +36,7 @@
{% else %} {% else %}
There were no online users in the past 5 minutes. There were no online users in the past 5 minutes.
{% endif %} {% endif %}
{#<div class="ad-container ad-sidebar" id="footerAd"> {#<div class="ad-container ad-sidebar" id="sideAd">
<div class="head">Advertisment</div> <div class="head">Advertisment</div>
<div class="ad-box"> <div class="ad-box">
<img src="http://i.flash.moe/1445793369-523-9238.png" /> <img src="http://i.flash.moe/1445793369-523-9238.png" />

View file

@ -42,7 +42,7 @@
<a class="fa fa-file-text-o" title="View {{ profile.username }}'s user page" href="{{ urls.format('USER_PROFILE', [profile.id]) }}"></a> <a class="fa fa-file-text-o" title="View {{ profile.username }}'s user page" href="{{ urls.format('USER_PROFILE', [profile.id]) }}"></a>
<a class="fa fa-list" title="View {{ profile.username }}'s threads" href="{{ urls.format('USER_THREADS', [profile.id]) }}"></a> <a class="fa fa-list" title="View {{ profile.username }}'s threads" href="{{ urls.format('USER_THREADS', [profile.id]) }}"></a>
<a class="fa fa-reply" title="View {{ profile.username }}'s posts" href="{{ urls.format('USER_POSTS', [profile.id]) }}"></a> <a class="fa fa-reply" title="View {{ profile.username }}'s posts" href="{{ urls.format('USER_POSTS', [profile.id]) }}"></a>
<a class="fa fa-user-plus" title="View {{ profile.username }}'s friends" href="{{ urls.format('USER_FRIENDS', [profile.id]) }}"></a> <a class="fa fa-star" title="View {{ profile.username }}'s friends" href="{{ urls.format('USER_FRIENDS', [profile.id]) }}"></a>
{#<a class="fa fa-users" title="View {{ profile.username }}'s groups" href="{{ urls.format('USER_GROUPS', [profile.id]) }}"></a>#} {#<a class="fa fa-users" title="View {{ profile.username }}'s groups" href="{{ urls.format('USER_GROUPS', [profile.id]) }}"></a>#}
{% if not noUserpage %} {% if not noUserpage %}
<a class="fa fa-comments-o" title="View {{ profile.username }}'s profile comments" href="{{ urls.format('USER_COMMENTS', [profile.id]) }}"></a> <a class="fa fa-comments-o" title="View {{ profile.username }}'s profile comments" href="{{ urls.format('USER_COMMENTS', [profile.id]) }}"></a>

View file

@ -1,3 +1,3 @@
{% set comments = profile.profileComments.comments %} {% set comments = profile.profileComments.comments %}
{% set commentsCategory = 'profile-' ~ profile.data.user_id %} {% set commentsCategory = 'profile-' ~ profile.id %}
{% include 'elements/comments.tpl' %} {% include 'elements/comments.tpl' %}

View file

@ -448,7 +448,7 @@ if (isset($_REQUEST['request-notifications']) && $_REQUEST['request-notification
$filepath = ROOT . Config::getConfig('user_uploads') . '/'; $filepath = ROOT . Config::getConfig('user_uploads') . '/';
$filename = $filepath . $mode . '_' . $currentUser->id(); $filename = $filepath . $mode . '_' . $currentUser->id();
$currfile = isset($currentUser->userData()[$userDataKey]) $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 // Check if $_FILES is set
if (!isset($_FILES[$mode]) && empty($_FILES[$mode])) { if (!isset($_FILES[$mode]) && empty($_FILES[$mode])) {
@ -606,7 +606,7 @@ if (isset($_REQUEST['request-notifications']) && $_REQUEST['request-notification
} }
// Update database // Update database
Users::updateUserDataField($currentUser->id(), $updated); $currentUser->setUserData($updated);
// Set render data // Set render data
$renderData['page'] = [ $renderData['page'] = [
@ -645,7 +645,7 @@ if (isset($_REQUEST['request-notifications']) && $_REQUEST['request-notification
} }
// Update database // Update database
Users::updateUserDataField($currentUser->id(), ['profileFields' => $store]); $currentUser->setUserData(['profileFields' => $store]);
// Set render data // Set render data
$renderData['page'] = [ $renderData['page'] = [
@ -738,7 +738,7 @@ if (isset($_REQUEST['request-notifications']) && $_REQUEST['request-notification
} }
// Update database // Update database
Users::updateUserDataField($currentUser->id(), ['userOptions' => $store]); $currentUser->setUserData(['userOptions' => $store]);
// Set render data // Set render data
$renderData['page'] = [ $renderData['page'] = [
@ -939,7 +939,7 @@ if (isset($_REQUEST['request-notifications']) && $_REQUEST['request-notification
$userPage = base64_encode($_POST['userpage']); $userPage = base64_encode($_POST['userpage']);
// Update database // Update database
Users::updateUserDataField($currentUser->id(), ['userPage' => $userPage]); $currentUser->setUserData(['userPage' => $userPage]);
// Set render data // Set render data
$renderData['page'] = [ $renderData['page'] = [
@ -957,7 +957,7 @@ if (isset($_REQUEST['request-notifications']) && $_REQUEST['request-notification
$signature = base64_encode($_POST['signature']); $signature = base64_encode($_POST['signature']);
// Update database // Update database
Users::updateUserDataField($currentUser->id(), ['signature' => $signature]); $currentUser->setUserData(['signature' => $signature]);
// Set render data // Set render data
$renderData['page'] = [ $renderData['page'] = [
@ -1427,10 +1427,8 @@ if (Users::checkLogin()) {
// Profile // Profile
case 'general.profile': case 'general.profile':
$renderData['profile'] = [ $renderData['profile'] = [
'fields' => Users::getProfileFields(), 'fields' => Users::getProfileFields(),
'months' => [ 'months' => [
1 => 'January', 1 => 'January',
2 => 'February', 2 => 'February',
3 => 'March', 3 => 'March',
@ -1443,18 +1441,14 @@ if (Users::checkLogin()) {
10 => 'October', 10 => 'October',
11 => 'November', 11 => 'November',
12 => 'December', 12 => 'December',
], ],
]; ];
break; break;
// Options // Options
case 'general.options': case 'general.options':
$renderData['options'] = [ $renderData['options'] = [
'fields' => Users::getOptionFields(), 'fields' => Users::getOptionFields(),
]; ];
break; break;
@ -1482,14 +1476,12 @@ if (Users::checkLogin()) {
case 'appearance.avatar': case 'appearance.avatar':
case 'appearance.background': case 'appearance.background':
$renderData[$mode] = [ $renderData[$mode] = [
'max_width' => Config::getConfig($mode . '_max_width'), 'max_width' => Config::getConfig($mode . '_max_width'),
'max_height' => Config::getConfig($mode . '_max_height'), 'max_height' => Config::getConfig($mode . '_max_height'),
'min_width' => Config::getConfig($mode . '_min_width'), 'min_width' => Config::getConfig($mode . '_min_width'),
'min_height' => Config::getConfig($mode . '_min_height'), 'min_height' => Config::getConfig($mode . '_min_height'),
'max_size' => Config::getConfig($mode . '_max_fsize'), 'max_size' => Config::getConfig($mode . '_max_fsize'),
'max_size_view' => Main::getByteSymbol(Config::getConfig($mode . '_max_fsize')), 'max_size_view' => Main::getByteSymbol(Config::getConfig($mode . '_max_fsize')),
]; ];
break; break;