This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/_sakura/components/User.php

319 lines
9.2 KiB
PHP
Raw Normal View History

2015-08-18 23:29:45 +00:00
<?php
/*
* Everything you'd ever need from a specific user
*/
namespace Sakura;
class User
{
2015-08-18 23:29:45 +00:00
// User data
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
public function __construct($uid)
{
2015-08-18 23:29:45 +00:00
// Get the user database row
$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
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
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
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
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
public function checkIfUserHasRanks($ranks)
{
2015-08-21 22:07:45 +00:00
// Check if the main rank is the specified rank
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
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
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
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
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
public function country()
{
2015-08-19 02:37:45 +00:00
return [
'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
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
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
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
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
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
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
public function elapsed($append = ' ago', $none = 'Just now')
{
2015-09-08 21:57:33 +00:00
return [
'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
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
if (!count($profileFields)) {
2015-08-19 02:37:45 +00:00
return;
}
// Once again if nothing was returned just return null
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
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
if (!array_key_exists($fieldName, $this->data['userData']['profileFields'])) {
2015-08-19 02:37:45 +00:00
continue;
}
// Assign field to output with value
$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
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
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
foreach ($additional as $subName => $subField) {
2015-08-19 02:37:45 +00:00
// Check if the user has the current field set otherwise continue
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
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
if (!count($optionFields)) {
2015-08-21 22:07:45 +00:00
return;
}
// Once again if nothing was returned just return null
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
foreach ($optionFields as $field) {
2015-08-21 22:07:45 +00:00
// Check if the user has the current field set otherwise continue
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
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
public function checkPremium()
{
2015-08-19 12:13:38 +00:00
// Check if the user has static premium
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, [
'uid' => [$this->data['id'], '='],
2015-08-19 12:13:38 +00:00
]);
// If nothing was returned just return false
if (empty($getRecord)) {
2015-08-19 12:13:38 +00:00
return [0];
}
// Check if the Tenshi hasn't expired
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
public function getWarnings()
{
2015-08-19 12:13:38 +00:00
// Do the database query
$warnings = Database::fetch('warnings', true, [
'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
}