2015-08-18 23:29:45 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Everything you'd ever need from a specific user
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Sakura;
|
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
class User
|
|
|
|
{
|
2015-08-18 23:29:45 +00:00
|
|
|
// User data
|
2015-09-14 20:51:23 +00:00
|
|
|
public $data = [];
|
|
|
|
public $ranks = [];
|
|
|
|
public $mainRank = [];
|
2015-08-18 23:29:45 +00:00
|
|
|
|
2015-08-19 02:37:45 +00:00
|
|
|
// Initialise the user object
|
2015-09-14 20:51:23 +00:00
|
|
|
public function __construct($uid)
|
|
|
|
{
|
2015-08-18 23:29:45 +00:00
|
|
|
|
|
|
|
// Get the user database row
|
2015-09-14 20:51:23 +00:00
|
|
|
$this->data = Database::fetch('users', false, ['id' => [$uid, '=', true], 'username_clean' => [Main::cleanString($uid, true), '=', true]]);
|
2015-08-21 22:07:45 +00:00
|
|
|
|
|
|
|
// Check if anything like the username exists
|
2015-09-14 20:51:23 +00:00
|
|
|
if (empty($this->data)) {
|
|
|
|
$this->data = Database::fetch('users', false, ['username_clean' => ['%' . Main::cleanString($uid, true) . '%', 'LIKE']]);
|
2015-08-21 22:07:45 +00:00
|
|
|
}
|
2015-08-19 02:37:45 +00:00
|
|
|
|
|
|
|
// Check if the user actually exists
|
2015-09-14 20:51:23 +00:00
|
|
|
if (empty($this->data)) {
|
2015-08-19 02:37:45 +00:00
|
|
|
// If not assign as the fallback user
|
|
|
|
$this->data = Users::$emptyUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode the json in the userData column
|
|
|
|
$this->data['userData'] = json_decode(!empty($this->data['userData']) ? $this->data['userData'] : '[]', true);
|
2015-08-18 23:29:45 +00:00
|
|
|
|
|
|
|
// Decode the ranks json array
|
2015-08-19 02:37:45 +00:00
|
|
|
$ranks = json_decode($this->data['ranks'], true);
|
2015-08-18 23:29:45 +00:00
|
|
|
|
|
|
|
// Get the rows for all the ranks
|
2015-09-14 20:51:23 +00:00
|
|
|
foreach ($ranks as $rank) {
|
2015-08-19 02:37:45 +00:00
|
|
|
// Store the database row in the array
|
|
|
|
$this->ranks[$rank] = Database::fetch('ranks', false, ['id' => [$rank, '=']]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if ranks were set
|
2015-09-14 20:51:23 +00:00
|
|
|
if (empty($this->ranks)) {
|
2015-08-19 02:37:45 +00:00
|
|
|
// If not assign the fallback rank
|
|
|
|
$this->ranks[0] = Users::$emptyRank;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign the user's main rank to a special variable since we'll use it a lot
|
|
|
|
$this->mainRank = $this->ranks[array_key_exists($this->data['rank_main'], $this->ranks) ? $this->data['rank_main'] : array_keys($this->ranks)[0]];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-21 22:07:45 +00:00
|
|
|
// Check if the user has the specified ranks
|
2015-09-14 20:51:23 +00:00
|
|
|
public function checkIfUserHasRanks($ranks)
|
|
|
|
{
|
2015-08-21 22:07:45 +00:00
|
|
|
|
|
|
|
// Check if the main rank is the specified rank
|
2015-09-14 20:51:23 +00:00
|
|
|
if (in_array($this->mainRank['id'], $ranks)) {
|
2015-08-21 22:07:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not go over all ranks and check if the user has them
|
2015-09-14 20:51:23 +00:00
|
|
|
foreach ($ranks as $rank) {
|
2015-08-21 22:07:45 +00:00
|
|
|
// We check if $rank is in $this->ranks and if yes return true
|
2015-09-14 20:51:23 +00:00
|
|
|
if (array_key_exists($rank, $this->ranks)) {
|
2015-08-21 22:07:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If all fails return false
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-19 02:37:45 +00:00
|
|
|
// Get the user's colour
|
2015-09-14 20:51:23 +00:00
|
|
|
public function colour()
|
|
|
|
{
|
2015-08-19 02:37:45 +00:00
|
|
|
|
|
|
|
return empty($this->data['name_colour']) ? $this->mainRank['colour'] : $this->data['name_colour'];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the user's title
|
2015-09-14 20:51:23 +00:00
|
|
|
public function userTitle()
|
|
|
|
{
|
2015-08-19 02:37:45 +00:00
|
|
|
|
|
|
|
return empty($this->data['usertitle']) ? $this->mainRank['title'] : $this->data['usertitle'];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the user's long and short country names
|
2015-09-14 20:51:23 +00:00
|
|
|
public function country()
|
|
|
|
{
|
2015-08-19 02:37:45 +00:00
|
|
|
|
|
|
|
return [
|
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'long' => Main::getCountryName($this->data['country']),
|
|
|
|
'short' => $this->data['country'],
|
2015-08-19 02:37:45 +00:00
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-19 12:13:38 +00:00
|
|
|
// Check if a user is online
|
2015-09-14 20:51:23 +00:00
|
|
|
public function checkOnline()
|
|
|
|
{
|
2015-08-19 12:13:38 +00:00
|
|
|
|
|
|
|
return $this->data['lastdate'] > (time() - Configuration::getConfig('max_online_time'));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get user's forum statistics
|
2015-09-14 20:51:23 +00:00
|
|
|
public function forumStats()
|
|
|
|
{
|
2015-08-19 12:13:38 +00:00
|
|
|
|
|
|
|
return Forum::getUserStats($this->data['id']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the user is friends with the currently authenticated
|
2015-09-14 20:51:23 +00:00
|
|
|
public function checkFriends($with)
|
|
|
|
{
|
2015-08-19 12:13:38 +00:00
|
|
|
|
|
|
|
return Users::checkFriend($this->data['id'], $with);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-23 22:08:36 +00:00
|
|
|
// Get all the friend of this user
|
2015-09-14 20:51:23 +00:00
|
|
|
public function getFriends($timestamps = false, $getData = false, $checkOnline = false)
|
|
|
|
{
|
2015-08-23 22:08:36 +00:00
|
|
|
|
2015-09-08 21:57:33 +00:00
|
|
|
return Users::getFriends($this->data['id'], $timestamps, $getData, $checkOnline);
|
2015-08-23 22:08:36 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-19 12:13:38 +00:00
|
|
|
// Check if the user is banned
|
2015-09-14 20:51:23 +00:00
|
|
|
public function checkBan()
|
|
|
|
{
|
2015-08-19 12:13:38 +00:00
|
|
|
|
|
|
|
return Bans::checkBan($this->data['id']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-21 22:07:45 +00:00
|
|
|
// Check if the user has the proper permissions
|
2015-09-14 20:51:23 +00:00
|
|
|
public function checkPermission($layer, $action)
|
|
|
|
{
|
2015-08-21 22:07:45 +00:00
|
|
|
|
|
|
|
return Permissions::check($layer, $action, $this->data['id'], 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-08 21:57:33 +00:00
|
|
|
// Get amount of time since user events
|
2015-09-14 20:51:23 +00:00
|
|
|
public function elapsed($append = ' ago', $none = 'Just now')
|
|
|
|
{
|
2015-09-08 21:57:33 +00:00
|
|
|
|
|
|
|
return [
|
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'joined' => Main::timeElapsed($this->data['regdate'], $append, $none),
|
|
|
|
'lastOnline' => Main::timeElapsed($this->data['lastdate'], $append, $none),
|
|
|
|
'birth' => Main::timeElapsed(strtotime($this->data['birthday']), $append, $none),
|
2015-09-08 21:57:33 +00:00
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-19 02:37:45 +00:00
|
|
|
// Get the user's profile fields
|
2015-09-14 20:51:23 +00:00
|
|
|
public function profileFields()
|
|
|
|
{
|
2015-08-19 02:37:45 +00:00
|
|
|
|
|
|
|
// Get profile fields
|
|
|
|
$profileFields = Database::fetch('profilefields');
|
|
|
|
|
|
|
|
// If there's nothing just return null
|
2015-09-14 20:51:23 +00:00
|
|
|
if (!count($profileFields)) {
|
2015-08-19 02:37:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once again if nothing was returned just return null
|
2015-09-14 20:51:23 +00:00
|
|
|
if (empty($this->data['userData']['profileFields'])) {
|
2015-08-19 02:37:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create output array
|
|
|
|
$profile = [];
|
|
|
|
|
|
|
|
// Check if profile fields aren't fake
|
2015-09-14 20:51:23 +00:00
|
|
|
foreach ($profileFields as $field) {
|
2015-08-19 02:37:45 +00:00
|
|
|
// 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
|
2015-09-14 20:51:23 +00:00
|
|
|
if (!array_key_exists($fieldName, $this->data['userData']['profileFields'])) {
|
2015-08-19 02:37:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign field to output with value
|
2015-09-14 20:51:23 +00:00
|
|
|
$profile[$fieldName] = array();
|
|
|
|
$profile[$fieldName]['name'] = $field['name'];
|
|
|
|
$profile[$fieldName]['value'] = $this->data['userData']['profileFields'][$fieldName];
|
|
|
|
$profile[$fieldName]['islink'] = $field['islink'];
|
2015-08-19 02:37:45 +00:00
|
|
|
|
|
|
|
// If the field is set to be a link add a value for that as well
|
2015-09-14 20:51:23 +00:00
|
|
|
if ($field['islink']) {
|
2015-08-19 02:37:45 +00:00
|
|
|
$profile[$fieldName]['link'] = str_replace('{{ VAL }}', $this->data['userData']['profileFields'][$fieldName], $field['linkformat']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we have additional options as well
|
2015-09-14 20:51:23 +00:00
|
|
|
if ($field['additional'] != null) {
|
2015-08-19 02:37:45 +00:00
|
|
|
// Decode the json of the additional stuff
|
|
|
|
$additional = json_decode($field['additional'], true);
|
|
|
|
|
|
|
|
// Go over all additional forms
|
2015-09-14 20:51:23 +00:00
|
|
|
foreach ($additional as $subName => $subField) {
|
2015-08-19 02:37:45 +00:00
|
|
|
// Check if the user has the current field set otherwise continue
|
2015-09-14 20:51:23 +00:00
|
|
|
if (!array_key_exists($subName, $this->data['userData']['profileFields'])) {
|
2015-08-19 02:37:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign field to output with value
|
|
|
|
$profile[$fieldName][$subName] = $this->data['userData']['profileFields'][$subName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return appropiate profile data
|
|
|
|
return $profile;
|
2015-08-18 23:29:45 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-21 22:07:45 +00:00
|
|
|
// Get the user's option fields
|
2015-09-14 20:51:23 +00:00
|
|
|
public function optionFields()
|
|
|
|
{
|
2015-08-21 22:07:45 +00:00
|
|
|
|
|
|
|
// Get option fields
|
|
|
|
$optionFields = Database::fetch('optionfields');
|
|
|
|
|
|
|
|
// If there's nothing just return null
|
2015-09-14 20:51:23 +00:00
|
|
|
if (!count($optionFields)) {
|
2015-08-21 22:07:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once again if nothing was returned just return null
|
2015-09-14 20:51:23 +00:00
|
|
|
if (empty($this->data['userData']['userOptions'])) {
|
2015-08-21 22:07:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create output array
|
|
|
|
$options = [];
|
|
|
|
|
|
|
|
// Check if profile fields aren't fake
|
2015-09-14 20:51:23 +00:00
|
|
|
foreach ($optionFields as $field) {
|
2015-08-21 22:07:45 +00:00
|
|
|
// Check if the user has the current field set otherwise continue
|
2015-09-14 20:51:23 +00:00
|
|
|
if (!array_key_exists($field['id'], $this->data['userData']['userOptions'])) {
|
2015-08-21 22:07:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the user has the proper permissions to use this option
|
2015-09-14 20:51:23 +00:00
|
|
|
if (!$this->checkPermission('SITE', $field['require_perm'])) {
|
2015-08-21 22:07:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign field to output with value
|
|
|
|
$options[$field['id']] = $this->data['userData']['userOptions'][$field['id']];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return appropiate profile data
|
|
|
|
return $options;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-19 12:13:38 +00:00
|
|
|
// Check if user has Premium
|
2015-09-14 20:51:23 +00:00
|
|
|
public function checkPremium()
|
|
|
|
{
|
2015-08-19 12:13:38 +00:00
|
|
|
|
|
|
|
// Check if the user has static premium
|
2015-09-14 20:51:23 +00:00
|
|
|
if (Permissions::check('SITE', 'STATIC_PREMIUM', $this->data['id'], 1)) {
|
2015-08-19 12:13:38 +00:00
|
|
|
return [2, 0, time() + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to retrieve the premium record from the database
|
|
|
|
$getRecord = Database::fetch('premium', false, [
|
2015-09-14 20:51:23 +00:00
|
|
|
'uid' => [$this->data['id'], '='],
|
2015-08-19 12:13:38 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
// If nothing was returned just return false
|
2015-09-14 20:51:23 +00:00
|
|
|
if (empty($getRecord)) {
|
2015-08-19 12:13:38 +00:00
|
|
|
return [0];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the Tenshi hasn't expired
|
2015-09-14 20:51:23 +00:00
|
|
|
if ($getRecord['expiredate'] < time()) {
|
2015-08-19 12:13:38 +00:00
|
|
|
Users::removeUserPremium($this->data['id']);
|
|
|
|
Users::updatePremiumMeta($this->data['id']);
|
|
|
|
return [0, $getRecord['startdate'], $getRecord['expiredate']];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Else return the start and expiration date
|
|
|
|
return [1, $getRecord['startdate'], $getRecord['expiredate']];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all warnings issued to the user
|
2015-09-14 20:51:23 +00:00
|
|
|
public function getWarnings()
|
|
|
|
{
|
2015-08-19 12:13:38 +00:00
|
|
|
|
|
|
|
// Do the database query
|
|
|
|
$warnings = Database::fetch('warnings', true, [
|
2015-09-14 20:51:23 +00:00
|
|
|
'uid' => [$this->data['id'], '='],
|
2015-08-19 12:13:38 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Return all the warnings
|
|
|
|
return $warnings;
|
|
|
|
|
|
|
|
}
|
2015-08-18 23:29:45 +00:00
|
|
|
}
|