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

624 lines
18 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
* @package 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
2015-09-14 21:41:43 +00:00
$this->data = Database::fetch(
'users',
false,
[
'user_id' => [$uid, '=', true],
2015-09-14 21:41:43 +00:00
'username_clean' => [Main::cleanString($uid, true), '=', true],
]
);
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 user_data column
$this->data['user_data'] = json_decode(!empty($this->data['user_data']) ? $this->data['user_data'] : '[]', true);
2015-08-18 23:29:45 +00:00
// Decode the ranks json array
$ranks = json_decode($this->data['user_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] = new Rank($rank);
2015-08-19 02:37:45 +00:00
}
// 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] = new Rank(0);
2015-08-19 02:37:45 +00:00
}
// Assign the user's main rank to a special variable since we'll use it a lot
2015-09-14 21:41:43 +00:00
$this->mainRank = $this->ranks[
array_key_exists($this->data['rank_main'], $this->ranks) ?
$this->data['rank_main'] :
array_keys($this->ranks)[0]
];
2015-08-19 02:37:45 +00:00
}
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 ($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()
{
return empty($this->data['user_colour']) ? $this->mainRank->colour() : $this->data['user_colour'];
2015-08-19 02:37:45 +00:00
}
// Get the user's title
public function userTitle()
{
return empty($this->data['user_title']) ? $this->mainRank->title() : $this->data['user_title'];
2015-08-19 02:37:45 +00:00
}
// 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['user_country']),
'short' => $this->data['user_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()
{
return $this->data['user_last_online'] > (time() - Configuration::getConfig('max_online_time'));
2015-08-19 12:13:38 +00:00
}
// Get user's forum statistics
public function forumStats()
{
return Forum::getUserStats($this->data['user_id']);
2015-08-19 12:13:38 +00:00
}
2015-10-12 18:25:37 +00:00
// Add a new friend
public function addFriend($uid)
{
// Create the foreign object
$user = new User($uid);
// Validate that the user exists
if ($user->checkPermission('SITE', 'DEACTIVATED')) {
return [0, 'USER_NOT_EXIST'];
}
// Get check
$check = $this->checkFriends($uid);
// Check if the user already has this user a friend
if ($check) {
return [0, 'ALREADY_FRIENDS'];
}
// Add friend
Database::insert('friends', [
'user_id' => $this->data['user_id'],
2015-10-12 18:25:37 +00:00
'friend_id' => $uid,
'friend_timestamp' => time(),
]);
// Return true because yay
return [1, $check == 2 ? 'FRIENDS' : 'NOT_MUTUAL'];
}
// Remove a friend
public function removeFriend($uid, $deleteRequest = false)
{
// Create the foreign object
$user = new User($uid);
// Validate that the user exists
if ($user->checkPermission('SITE', 'DEACTIVATED')) {
return [0, 'USER_NOT_EXIST'];
}
// Check if the user has this user a friend
if (!$this->checkFriends($uid)) {
return [0, 'ALREADY_REMOVED'];
}
// Remove friend
Database::delete('friends', [
'user_id' => [$this->data['user_id'], '='],
2015-10-12 18:25:37 +00:00
'friend_id' => [$uid, '='],
]);
// Attempt to remove the request
if ($deleteRequest) {
Database::delete('friends', [
'friend_id' => [$this->data['user_id'], '='],
2015-10-12 18:25:37 +00:00
'user_id' => [$uid, '='],
]);
}
// Return true because yay
return [1, 'REMOVED'];
}
2015-08-19 12:13:38 +00:00
// Check if the user is friends with the currently authenticated
public function checkFriends($with)
{
2015-08-19 12:13:38 +00:00
2015-10-12 18:25:37 +00:00
// Get the friend's friends
$friend = in_array($this->data['user_id'], (new User($with))->getFriends());
// Get the user's friends
$self = in_array($with, $this->getFriends());
// Check if the friend is actually in the user's array
if ($friend && $self) {
return 2;
}
// Check if the friend is actually in the user's array
if ($self) {
return 1;
}
// Return true if all went through
return 0;
2015-08-19 12:13:38 +00:00
}
2015-08-23 22:08:36 +00:00
// Get all the friend of this user
public function getFriends($timestamps = false, $getData = false, $checkOnline = false)
{
return Users::getFriends($this->data['user_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()
{
return Bans::checkBan($this->data['user_id']);
2015-08-19 12:13:38 +00:00
}
2015-08-21 22:07:45 +00:00
// Check if the user has the proper permissions
public function checkPermission($layer, $action)
{
return Permissions::check($layer, $action, $this->data['user_id'], 1);
2015-08-21 22:07:45 +00:00
}
// Get a user's profile comments
public function profileComments()
{
return new Comments('profile-' . $this->data['user_id']);
}
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['user_registered'], $append, $none),
'lastOnline' => Main::timeElapsed($this->data['user_last_online'], $append, $none),
'birth' => Main::timeElapsed(strtotime($this->data['user_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['user_data']['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['field_name'], true, true);
2015-08-19 02:37:45 +00:00
// Check if the user has the current field set otherwise continue
if (!array_key_exists($fieldName, $this->data['user_data']['profileFields'])) {
2015-08-19 02:37:45 +00:00
continue;
}
// Assign field to output with value
$profile[$fieldName] = [];
$profile[$fieldName]['name'] = $field['field_name'];
$profile[$fieldName]['value'] = $this->data['user_data']['profileFields'][$fieldName];
$profile[$fieldName]['islink'] = $field['field_link'];
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['field_link']) {
2015-09-14 21:41:43 +00:00
$profile[$fieldName]['link'] = str_replace(
'{{ VAL }}',
$this->data['user_data']['profileFields'][$fieldName],
$field['field_linkformat']
2015-09-14 21:41:43 +00:00
);
2015-08-19 02:37:45 +00:00
}
// Check if we have additional options as well
if ($field['field_additional'] != null) {
2015-08-19 02:37:45 +00:00
// Decode the json of the additional stuff
$additional = json_decode($field['field_additional'], true);
2015-08-19 02:37:45 +00:00
// 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['user_data']['profileFields'])) {
2015-08-19 02:37:45 +00:00
continue;
}
// Assign field to output with value
$profile[$fieldName][$subName] = $this->data['user_data']['profileFields'][$subName];
2015-08-19 02:37:45 +00:00
}
}
}
// 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['user_data']['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['option_id'], $this->data['user_data']['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['option_permission'])) {
2015-08-21 22:07:45 +00:00
continue;
}
// Assign field to output with value
$options[$field['option_id']] = $this->data['user_data']['userOptions'][$field['option_id']];
2015-08-21 22:07:45 +00:00
}
// 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['user_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, [
'user_id' => [$this->data['user_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['premium_expire'] < time()) {
Users::removeUserPremium($this->data['user_id']);
Users::updatePremiumMeta($this->data['user_id']);
return [0, $getRecord['premium_start'], $getRecord['premium_expire']];
2015-08-19 12:13:38 +00:00
}
// Else return the start and expiration date
return [1, $getRecord['premium_start'], $getRecord['premium_expire']];
2015-08-19 12:13:38 +00:00
}
// Get all warnings issued to the user
public function getWarnings()
{
2015-08-19 12:13:38 +00:00
// Do the database query
$getWarnings = Database::fetch('warnings', true, [
'user_id' => [$this->data['user_id'], '='],
2015-08-19 12:13:38 +00:00
]);
// Storage array
$warnings = [];
// Add special stuff
foreach ($getWarnings as $warning) {
// Check if it hasn't expired
if ($warning['warning_expires'] < time()) {
Database::delete('warnings', ['warning_id' => [$warning['warning_id'], '=']]);
continue;
}
// Text action
switch ($warning['warning_action']) {
default:
case '0':
$warning['warning_action_text'] = 'Warning';
break;
case '1':
$warning['warning_action_text'] = 'Silence';
break;
case '2':
$warning['warning_action_text'] = 'Restriction';
break;
case '3':
$warning['warning_action_text'] = 'Ban';
break;
case '4':
$warning['warning_action_text'] = 'Abyss';
break;
}
// Text expiration
$warning['warning_length'] = round(($warning['warning_expires'] - $warning['warning_issued']) / 60);
// Add to array
$warnings[$warning['warning_id']] = $warning;
}
2015-08-19 12:13:38 +00:00
// Return all the warnings
return $warnings;
}
// Get a user's userpage
public function userPage()
{
return isset($this->data['user_data']['userPage']) ?
Main::mdParse(
base64_decode(
$this->data['user_data']['userPage']
),
true
) :
null;
}
// Get a user's signature
public function signature()
{
return isset($this->data['user_data']['signature']) ?
Main::bbParse(
Main::parseEmotes(
base64_decode(
$this->data['user_data']['signature']
)
)
) :
null;
}
// Get username change history
public function getUsernameHistory()
{
// Do the database query
$changes = Database::fetch('username_history', true, [
'user_id' => [$this->data['user_id'], '='],
], ['change_id', true]);
// Return all the warnings
return $changes;
}
// Set a new username
public function setUsername($username)
{
// Create a cleaned version
$username_clean = Main::cleanString($username, true);
// Check if the username is too short
if (strlen($username_clean) < Configuration::getConfig('username_min_length')) {
return [0, 'TOO_SHORT'];
}
// Check if the username is too long
if (strlen($username_clean) > Configuration::getConfig('username_max_length')) {
return [0, 'TOO_LONG'];
}
// Check if this username hasn't been used in the last amount of days set in the config
$getOld = Database::fetch('username_history', false, [
'username_old_clean' => [$username_clean, '='],
'change_time' => [(Configuration::getConfig('old_username_reserve') * 24 * 60 * 60), '>'],
], ['change_id', true]);
// Check if anything was returned
if ($getOld) {
return [0, 'TOO_RECENT', $getOld['change_time']];
}
// Check if the username is already in use
$getInUse = Database::fetch('users', false, [
'username_clean' => [$username_clean, '='],
]);
// Check if anything was returned
if ($getInUse) {
return [0, 'IN_USE', $getInUse['user_id']];
}
// Insert into username_history table
Database::insert('username_history', [
'change_time' => time(),
'user_id' => $this->data['user_id'],
'username_new' => $username,
'username_new_clean' => $username_clean,
'username_old' => $this->data['username'],
'username_old_clean' => $this->data['username_clean'],
]);
// Update userrow
Database::update('users', [
[
'username' => $username,
'username_clean' => $username_clean,
],
[
'user_id' => [$this->data['user_id'], '='],
],
]);
// Return success
return [1, 'SUCCESS', $username];
}
// Set a new e-mail address
public function setEMailAddress($email)
{
// Validate e-mail address
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
return [0, 'INVALID'];
}
// Check if the username is already in use
$getInUse = Database::fetch('users', false, [
'email' => [$email, '='],
]);
// Check if anything was returned
if ($getInUse) {
return [0, 'IN_USE', $getInUse['user_id']];
}
// Update userrow
Database::update('users', [
[
'email' => $email,
],
[
'user_id' => [$this->data['user_id'], '='],
],
]);
// Return success
return [1, 'SUCCESS', $email];
}
// Set a new password
public function setPassword($old, $new, $confirm)
{
// Validate password
switch ($this->data['password_algo']) {
// Abyssing
case 'nologin':
return [0, 'NO_LOGIN'];
// Default hashing method
default:
if (!Hashing::validatePassword($old, [
$this->data['password_algo'],
$this->data['password_iter'],
$this->data['password_salt'],
$this->data['password_hash'],
])) {
return [0, 'INCORRECT_PASSWORD', $this->data['password_chan']];
}
}
// Check password entropy
if (Main::pwdEntropy($new) < Configuration::getConfig('min_entropy')) {
return [0, 'PASS_TOO_SHIT'];
}
// Passwords do not match
if ($new != $confirm) {
return [0, 'PASS_NOT_MATCH'];
}
// Create hash
$password = Hashing::createHash($new);
// Update userrow
Database::update('users', [
[
'password_hash' => $password[3],
'password_salt' => $password[2],
'password_algo' => $password[0],
'password_iter' => $password[1],
'password_chan' => time(),
],
[
'user_id' => [$this->data['user_id'], '='],
],
]);
// Return success
return [1, 'SUCCESS'];
}
2015-08-18 23:29:45 +00:00
}