2015-08-18 23:29:45 +00:00
|
|
|
<?php
|
2016-02-03 22:22:56 +00:00
|
|
|
/**
|
|
|
|
* Holds the user object class.
|
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
|
2015-08-18 23:29:45 +00:00
|
|
|
namespace Sakura;
|
|
|
|
|
2016-08-04 21:24:08 +00:00
|
|
|
use Carbon\Carbon;
|
2016-09-19 15:33:52 +00:00
|
|
|
use LastFmApi\Api\AuthApi;
|
|
|
|
use LastFmApi\Api\UserApi;
|
|
|
|
use LastFmApi\Exception\LastFmApiExeption;
|
2016-09-21 11:26:08 +00:00
|
|
|
use Sakura\Exceptions\NetAddressTypeException;
|
2015-12-29 21:52:19 +00:00
|
|
|
use Sakura\Perms;
|
|
|
|
use Sakura\Perms\Site;
|
2016-03-27 21:18:57 +00:00
|
|
|
use stdClass;
|
2015-12-29 21:52:19 +00:00
|
|
|
|
2015-10-18 19:06:30 +00:00
|
|
|
/**
|
2016-02-02 21:04:15 +00:00
|
|
|
* Everything you'd ever need from a specific user.
|
2015-10-18 19:06:30 +00:00
|
|
|
* @package Sakura
|
2016-02-02 21:04:15 +00:00
|
|
|
* @author Julian van de Groep <me@flash.moe>
|
2015-10-18 19:06:30 +00:00
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
class User
|
|
|
|
{
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* The User's ID.
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $id = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The user's username.
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $username = 'User';
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A cleaned version of the username.
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $usernameClean = 'user';
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The user's password hash.
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-09-21 11:51:32 +00:00
|
|
|
private $password = '';
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* UNIX timestamp of last time the password was changed.
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $passwordChan = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The user's e-mail address.
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $email = 'user@sakura';
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The rank object of the user's main rank.
|
|
|
|
* @var Rank
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $mainRank = null;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The ID of the main rank.
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $mainRankId = 1;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The index of rank objects.
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $ranks = [];
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The user's username colour.
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $colour = '';
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The IP the user registered from.
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $registerIp = '0.0.0.0';
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The IP the user was last active from.
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $lastIp = '0.0.0.0';
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A user's title.
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $title = '';
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The UNIX timestamp of when the user registered.
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $registered = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The UNIX timestamp of when the user was last online.
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $lastOnline = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The 2 character country code of a user.
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $country = 'XX';
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The File id of the user's avatar.
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $avatar = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The File id of the user's background.
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $background = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
2016-02-28 17:45:25 +00:00
|
|
|
* The File id of the user's header.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @var int
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $header = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The raw userpage of the user.
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $page = '';
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The raw signature of the user.
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $signature = '';
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
2016-08-02 20:35:12 +00:00
|
|
|
* Whether the user's background should be displayed sitewide.
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $backgroundSitewide = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The user's website url.
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2016-08-02 20:35:12 +00:00
|
|
|
public $website = '';
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
2016-08-02 20:35:12 +00:00
|
|
|
* The user's twitter handle.
|
|
|
|
* @var string
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2016-08-02 20:35:12 +00:00
|
|
|
public $twitter = '';
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
2016-08-02 20:35:12 +00:00
|
|
|
* The user's github username.
|
|
|
|
* @var string
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2016-08-02 20:35:12 +00:00
|
|
|
public $github = '';
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
2016-08-02 20:35:12 +00:00
|
|
|
* The user's skype username.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $skype = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The user's discord tag.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $discord = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The user's youtube channel id/name.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $youtube = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The user's steam community username.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $steam = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The user's osu! username.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $osu = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The user's lastfm username.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $lastfm = '';
|
|
|
|
|
2016-08-07 14:23:02 +00:00
|
|
|
/**
|
|
|
|
* The user's selected design.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $design = '';
|
|
|
|
|
2016-09-19 15:33:52 +00:00
|
|
|
/**
|
|
|
|
* Title of the track this user last listened to.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $musicTrack;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Artist of the track this user last listened to.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $musicArtist;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Last time this was updated.
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $musicCheck;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the user is actively listening.
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $musicListening;
|
|
|
|
|
2016-08-02 20:35:12 +00:00
|
|
|
/**
|
|
|
|
* The user's birthday.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $birthday = '0000-00-00';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The user's permission container.
|
|
|
|
* @var Perms
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2016-08-02 20:35:12 +00:00
|
|
|
private $permissions;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The User instance cache array.
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-07-29 19:31:36 +00:00
|
|
|
protected static $userCache = [];
|
2015-12-29 01:27:49 +00:00
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Cached constructor.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @param int|string $uid
|
|
|
|
* @param bool $forceRefresh
|
|
|
|
* @return User
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2016-01-02 17:55:31 +00:00
|
|
|
public static function construct($uid, $forceRefresh = false)
|
|
|
|
{
|
2015-12-29 01:27:49 +00:00
|
|
|
// Check if a user object isn't present in cache
|
2016-07-29 19:31:36 +00:00
|
|
|
if ($forceRefresh || !array_key_exists($uid, self::$userCache)) {
|
2015-12-29 01:27:49 +00:00
|
|
|
// If not create a new object and cache it
|
2016-07-29 19:31:36 +00:00
|
|
|
self::$userCache[$uid] = new User($uid);
|
2015-12-29 01:27:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the cached object
|
2016-07-29 19:31:36 +00:00
|
|
|
return self::$userCache[$uid];
|
2015-12-29 01:27:49 +00:00
|
|
|
}
|
2015-08-18 23:29:45 +00:00
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Create a new user.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @param string $username
|
|
|
|
* @param string $password
|
|
|
|
* @param string $email
|
|
|
|
* @param array $ranks
|
|
|
|
* @return User
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2016-01-04 20:14:09 +00:00
|
|
|
public static function create($username, $password, $email, $ranks = [2])
|
|
|
|
{
|
|
|
|
// Set a few variables
|
2016-04-01 21:44:31 +00:00
|
|
|
$usernameClean = clean_string($username, true);
|
|
|
|
$emailClean = clean_string($email, true);
|
2016-07-29 19:31:36 +00:00
|
|
|
$password = password_hash($password, PASSWORD_BCRYPT);
|
2016-01-17 02:08:08 +00:00
|
|
|
|
2016-02-28 17:45:25 +00:00
|
|
|
// Insert the user into the database and get the id
|
|
|
|
$userId = DB::table('users')
|
|
|
|
->insertGetId([
|
|
|
|
'username' => $username,
|
|
|
|
'username_clean' => $usernameClean,
|
2016-07-29 19:31:36 +00:00
|
|
|
'password' => $password,
|
2016-02-28 17:45:25 +00:00
|
|
|
'email' => $emailClean,
|
|
|
|
'rank_main' => 0,
|
2016-03-27 21:18:57 +00:00
|
|
|
'register_ip' => Net::pton(Net::ip()),
|
|
|
|
'last_ip' => Net::pton(Net::ip()),
|
2016-02-28 17:45:25 +00:00
|
|
|
'user_registered' => time(),
|
|
|
|
'user_last_online' => 0,
|
2016-04-01 21:44:31 +00:00
|
|
|
'user_country' => get_country_code(),
|
2016-02-28 17:45:25 +00:00
|
|
|
]);
|
2016-01-04 20:14:09 +00:00
|
|
|
|
|
|
|
// Create a user object
|
|
|
|
$user = self::construct($userId);
|
|
|
|
|
|
|
|
// Assign the default rank
|
|
|
|
$user->addRanks($ranks);
|
|
|
|
|
|
|
|
// Set the default rank
|
|
|
|
$user->setMainRank($ranks[0]);
|
|
|
|
|
|
|
|
// Return the user object
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
2016-08-05 02:35:37 +00:00
|
|
|
* The actual constructor.
|
|
|
|
* @param int|string $userId
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2016-02-28 17:45:25 +00:00
|
|
|
private function __construct($userId)
|
2015-09-14 20:51:23 +00:00
|
|
|
{
|
2015-08-18 23:29:45 +00:00
|
|
|
// Get the user database row
|
2016-02-28 17:45:25 +00:00
|
|
|
$userRow = DB::table('users')
|
|
|
|
->where('user_id', $userId)
|
2016-08-06 14:09:01 +00:00
|
|
|
->orWhere('username_clean', clean_string($userId, true))
|
|
|
|
->first();
|
2015-08-21 22:07:45 +00:00
|
|
|
|
2016-01-17 01:58:31 +00:00
|
|
|
// Populate the variables
|
|
|
|
if ($userRow) {
|
2016-08-02 20:35:12 +00:00
|
|
|
$this->id = intval($userRow->user_id);
|
2016-02-18 23:28:44 +00:00
|
|
|
$this->username = $userRow->username;
|
|
|
|
$this->usernameClean = $userRow->username_clean;
|
2016-07-29 19:31:36 +00:00
|
|
|
$this->password = $userRow->password;
|
2016-08-02 20:35:12 +00:00
|
|
|
$this->passwordChan = intval($userRow->password_chan);
|
2016-02-18 23:28:44 +00:00
|
|
|
$this->email = $userRow->email;
|
2016-08-02 20:35:12 +00:00
|
|
|
$this->mainRankId = intval($userRow->rank_main);
|
2016-02-18 23:28:44 +00:00
|
|
|
$this->colour = $userRow->user_colour;
|
|
|
|
$this->title = $userRow->user_title;
|
2016-08-02 20:35:12 +00:00
|
|
|
$this->registered = intval($userRow->user_registered);
|
|
|
|
$this->lastOnline = intval($userRow->user_last_online);
|
2016-02-18 23:28:44 +00:00
|
|
|
$this->birthday = $userRow->user_birthday;
|
|
|
|
$this->country = $userRow->user_country;
|
2016-08-02 20:35:12 +00:00
|
|
|
$this->avatar = intval($userRow->user_avatar);
|
|
|
|
$this->background = intval($userRow->user_background);
|
|
|
|
$this->header = intval($userRow->user_header);
|
2016-02-18 23:28:44 +00:00
|
|
|
$this->page = $userRow->user_page;
|
|
|
|
$this->signature = $userRow->user_signature;
|
2016-08-02 20:35:12 +00:00
|
|
|
$this->backgroundSitewide = boolval($userRow->user_background_sitewide);
|
|
|
|
$this->website = $userRow->user_website;
|
|
|
|
$this->twitter = $userRow->user_twitter;
|
|
|
|
$this->github = $userRow->user_github;
|
|
|
|
$this->skype = $userRow->user_skype;
|
|
|
|
$this->discord = $userRow->user_discord;
|
|
|
|
$this->youtube = $userRow->user_youtube;
|
|
|
|
$this->steam = $userRow->user_steam;
|
|
|
|
$this->osu = $userRow->user_osu;
|
|
|
|
$this->lastfm = $userRow->user_lastfm;
|
2016-08-07 14:23:02 +00:00
|
|
|
$this->design = $userRow->user_design;
|
2016-09-19 15:33:52 +00:00
|
|
|
$this->musicTrack = $userRow->user_music_track;
|
|
|
|
$this->musicArtist = $userRow->user_music_artist;
|
|
|
|
$this->musicListening = boolval($userRow->user_music_listening);
|
|
|
|
$this->musicCheck = intval($userRow->user_music_check);
|
2016-04-25 02:01:14 +00:00
|
|
|
|
|
|
|
// Temporary backwards compatible IP storage system
|
|
|
|
try {
|
|
|
|
$this->registerIp = Net::ntop($userRow->register_ip);
|
2016-09-19 15:33:52 +00:00
|
|
|
} catch (NetAddressTypeException $e) {
|
2016-04-25 02:01:14 +00:00
|
|
|
$this->registerIp = $userRow->register_ip;
|
|
|
|
|
|
|
|
DB::table('users')
|
|
|
|
->where('user_id', $this->id)
|
|
|
|
->update([
|
|
|
|
'register_ip' => Net::pton($this->registerIp),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->lastIp = Net::ntop($userRow->last_ip);
|
2016-09-19 15:33:52 +00:00
|
|
|
} catch (NetAddressTypeException $e) {
|
2016-04-25 02:01:14 +00:00
|
|
|
$this->lastIp = $userRow->last_ip;
|
|
|
|
|
|
|
|
DB::table('users')
|
|
|
|
->where('user_id', $this->id)
|
|
|
|
->update([
|
|
|
|
'last_ip' => Net::pton($this->lastIp),
|
|
|
|
]);
|
|
|
|
}
|
2015-08-19 02:37:45 +00:00
|
|
|
}
|
|
|
|
|
2016-01-03 21:19:37 +00:00
|
|
|
// Get all ranks
|
2016-02-28 17:45:25 +00:00
|
|
|
$ranks = DB::table('user_ranks')
|
|
|
|
->where('user_id', $this->id)
|
|
|
|
->get(['rank_id']);
|
2015-08-18 23:29:45 +00:00
|
|
|
|
|
|
|
// Get the rows for all the ranks
|
2016-01-03 21:19:37 +00:00
|
|
|
foreach ($ranks as $rank) {
|
2015-08-19 02:37:45 +00:00
|
|
|
// Store the database row in the array
|
2016-02-18 23:28:44 +00:00
|
|
|
$this->ranks[$rank->rank_id] = Rank::construct($rank->rank_id);
|
2015-08-19 02:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2016-01-04 20:14:09 +00:00
|
|
|
$this->ranks[1] = Rank::construct(1);
|
2015-08-19 02:37:45 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 01:58:31 +00:00
|
|
|
// Check if the rank is actually assigned to this user
|
|
|
|
if (!array_key_exists($this->mainRankId, $this->ranks)) {
|
|
|
|
$this->mainRankId = array_keys($this->ranks)[0];
|
|
|
|
$this->setMainRank($this->mainRankId);
|
|
|
|
}
|
2015-11-07 22:58:02 +00:00
|
|
|
|
2016-01-17 01:58:31 +00:00
|
|
|
// Assign the main rank to its own var
|
|
|
|
$this->mainRank = $this->ranks[$this->mainRankId];
|
2015-11-07 22:58:02 +00:00
|
|
|
|
2016-01-17 01:58:31 +00:00
|
|
|
// Set user colour
|
|
|
|
$this->colour = $this->colour ? $this->colour : $this->mainRank->colour;
|
2015-11-07 22:58:02 +00:00
|
|
|
|
2016-01-17 01:58:31 +00:00
|
|
|
// Set user title
|
|
|
|
$this->title = $this->title ? $this->title : $this->mainRank->title;
|
2015-08-21 22:07:45 +00:00
|
|
|
|
2016-01-17 01:58:31 +00:00
|
|
|
// Init the permissions
|
|
|
|
$this->permissions = new Perms(Perms::SITE);
|
2015-08-19 02:37:45 +00:00
|
|
|
}
|
|
|
|
|
2016-08-04 21:24:08 +00:00
|
|
|
/**
|
2016-08-05 02:35:37 +00:00
|
|
|
* Get a Carbon object of the registration date.
|
2016-08-04 21:24:08 +00:00
|
|
|
* @return Carbon
|
|
|
|
*/
|
|
|
|
public function registerDate()
|
|
|
|
{
|
|
|
|
return Carbon::createFromTimestamp($this->registered);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-05 02:35:37 +00:00
|
|
|
* Get a Carbon object of the last online date.
|
2016-08-04 21:24:08 +00:00
|
|
|
* @return Carbon
|
|
|
|
*/
|
|
|
|
public function lastDate()
|
|
|
|
{
|
|
|
|
return Carbon::createFromTimestamp($this->lastOnline);
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get the user's birthday.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @param bool $age
|
|
|
|
* @return int|string
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public function birthday($age = false)
|
2015-11-07 22:58:02 +00:00
|
|
|
{
|
2016-01-17 01:58:31 +00:00
|
|
|
// If age is requested calculate it
|
|
|
|
if ($age) {
|
|
|
|
// Create dates
|
|
|
|
$birthday = date_create($this->birthday);
|
|
|
|
$now = date_create(date('Y-m-d'));
|
2015-11-07 22:58:02 +00:00
|
|
|
|
2016-01-17 01:58:31 +00:00
|
|
|
// Get the difference
|
|
|
|
$diff = date_diff($birthday, $now);
|
2015-08-19 02:37:45 +00:00
|
|
|
|
2016-01-17 01:58:31 +00:00
|
|
|
// Return the difference in years
|
2016-01-22 12:46:52 +00:00
|
|
|
return (int) $diff->format('%Y');
|
2016-01-17 01:58:31 +00:00
|
|
|
}
|
2015-11-07 22:58:02 +00:00
|
|
|
|
2016-01-17 01:58:31 +00:00
|
|
|
// Otherwise just return the birthday value
|
|
|
|
return $this->birthday;
|
2015-08-19 02:37:45 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get the user's country.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @param bool $long
|
|
|
|
* @return string
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public function country($long = false)
|
2015-11-07 22:58:02 +00:00
|
|
|
{
|
2016-04-01 21:44:31 +00:00
|
|
|
return $long ? get_country_name($this->country) : $this->country;
|
2015-11-07 22:58:02 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Check if a user is online.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @return bool
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2015-11-10 23:34:48 +00:00
|
|
|
public function isOnline()
|
2015-09-14 20:51:23 +00:00
|
|
|
{
|
2016-02-28 17:45:25 +00:00
|
|
|
// Count sessions
|
|
|
|
$sessions = DB::table('sessions')
|
|
|
|
->where('user_id', $this->id)
|
|
|
|
->count();
|
2015-11-11 21:54:56 +00:00
|
|
|
|
|
|
|
// If there's no entries just straight up return false
|
2016-02-28 17:45:25 +00:00
|
|
|
if (!$sessions) {
|
2015-11-11 21:54:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise use the standard method
|
2016-07-26 17:29:53 +00:00
|
|
|
return $this->lastOnline > (time() - 120);
|
2015-11-10 23:34:48 +00:00
|
|
|
}
|
|
|
|
|
2016-07-30 13:48:09 +00:00
|
|
|
/**
|
2016-08-05 02:35:37 +00:00
|
|
|
* Updates the last IP and online time of the user.
|
2016-07-30 13:48:09 +00:00
|
|
|
*/
|
|
|
|
public function updateOnline()
|
|
|
|
{
|
|
|
|
$this->lastOnline = time();
|
|
|
|
$this->lastIp = Net::ip();
|
|
|
|
|
|
|
|
DB::table('users')
|
|
|
|
->where('user_id', $this->id)
|
|
|
|
->update([
|
|
|
|
'user_last_online' => $this->lastOnline,
|
|
|
|
'last_ip' => Net::pton($this->lastIp),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-04-02 15:59:45 +00:00
|
|
|
/**
|
|
|
|
* Runs some checks to see if this user is activated.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @return bool
|
2016-04-02 15:59:45 +00:00
|
|
|
*/
|
|
|
|
public function isActive()
|
|
|
|
{
|
|
|
|
return $this->id !== 0 && !$this->permission(Site::DEACTIVATED);
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get a few forum statistics.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @return array
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
public function forumStats()
|
|
|
|
{
|
2016-02-28 17:45:25 +00:00
|
|
|
$posts = DB::table('posts')
|
|
|
|
->where('poster_id', $this->id)
|
|
|
|
->count();
|
2016-02-18 23:28:44 +00:00
|
|
|
|
2016-07-30 13:48:09 +00:00
|
|
|
$topics = DB::table('posts')
|
2016-02-28 17:45:25 +00:00
|
|
|
->where('poster_id', $this->id)
|
|
|
|
->distinct()
|
|
|
|
->groupBy('topic_id')
|
|
|
|
->count();
|
2016-02-18 23:28:44 +00:00
|
|
|
|
2015-12-11 20:49:40 +00:00
|
|
|
return [
|
2016-02-28 17:45:25 +00:00
|
|
|
'posts' => $posts,
|
2016-07-30 13:48:09 +00:00
|
|
|
'topics' => $topics,
|
2015-12-11 20:49:40 +00:00
|
|
|
];
|
2015-08-19 12:13:38 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Add ranks to a user.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @param array $ranks
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2015-11-11 21:56:03 +00:00
|
|
|
public function addRanks($ranks)
|
|
|
|
{
|
2015-11-11 21:54:56 +00:00
|
|
|
// Update the ranks array
|
2016-01-17 01:58:31 +00:00
|
|
|
$ranks = array_diff(
|
|
|
|
array_unique(
|
|
|
|
array_merge(
|
|
|
|
array_keys($this->ranks),
|
2016-03-17 19:09:00 +00:00
|
|
|
$ranks
|
|
|
|
)
|
2016-03-13 20:35:51 +00:00
|
|
|
),
|
|
|
|
array_keys($this->ranks)
|
|
|
|
);
|
2015-11-11 21:54:56 +00:00
|
|
|
|
|
|
|
// Save to the database
|
2016-01-03 21:19:37 +00:00
|
|
|
foreach ($ranks as $rank) {
|
2016-08-07 14:32:14 +00:00
|
|
|
$this->ranks[$rank] = Rank::construct($rank);
|
|
|
|
|
2016-02-28 17:45:25 +00:00
|
|
|
DB::table('user_ranks')
|
|
|
|
->insert([
|
|
|
|
'rank_id' => $rank,
|
|
|
|
'user_id' => $this->id,
|
|
|
|
]);
|
2016-01-03 21:19:37 +00:00
|
|
|
}
|
2015-11-11 21:54:56 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Remove a set of ranks from a user.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @param array $ranks
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2015-11-11 21:54:56 +00:00
|
|
|
public function removeRanks($ranks)
|
|
|
|
{
|
|
|
|
// Current ranks
|
2016-01-17 01:58:31 +00:00
|
|
|
$remove = array_intersect(array_keys($this->ranks), $ranks);
|
2015-11-11 21:54:56 +00:00
|
|
|
|
|
|
|
// Iterate over the ranks
|
2016-01-03 21:19:37 +00:00
|
|
|
foreach ($remove as $rank) {
|
2016-08-07 14:32:14 +00:00
|
|
|
unset($this->ranks[$rank]);
|
|
|
|
|
2016-03-17 19:09:00 +00:00
|
|
|
DB::table('user_ranks')
|
2016-02-28 17:45:25 +00:00
|
|
|
->where('user_id', $this->id)
|
|
|
|
->where('rank_id', $rank)
|
|
|
|
->delete();
|
2015-11-11 21:54:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Change the main rank of a user.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @param int $rank
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2015-11-08 22:31:52 +00:00
|
|
|
public function setMainRank($rank)
|
|
|
|
{
|
2016-08-07 14:32:14 +00:00
|
|
|
$this->mainRankId = $rank;
|
|
|
|
$this->mainRank = $this->ranks[$rank];
|
|
|
|
|
2016-02-28 17:45:25 +00:00
|
|
|
DB::table('users')
|
|
|
|
->where('user_id', $this->id)
|
|
|
|
->update([
|
2016-08-07 14:32:14 +00:00
|
|
|
'rank_main' => $this->mainRankId,
|
2016-02-28 17:45:25 +00:00
|
|
|
]);
|
2015-11-08 22:31:52 +00:00
|
|
|
}
|
2015-11-08 22:27:42 +00:00
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Check if a user has a certain set of rank.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @param array $ranks
|
|
|
|
* @return bool
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2015-11-08 22:31:52 +00:00
|
|
|
public function hasRanks($ranks)
|
|
|
|
{
|
2015-11-07 22:58:02 +00:00
|
|
|
// Check if the main rank is the specified rank
|
2016-01-17 01:58:31 +00:00
|
|
|
if (in_array($this->mainRankId, $ranks)) {
|
2015-11-07 22:58:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not go over all ranks and check if the user has them
|
|
|
|
foreach ($ranks as $rank) {
|
|
|
|
// We check if $rank is in $this->ranks and if yes return true
|
2016-01-17 01:58:31 +00:00
|
|
|
if (in_array($rank, array_keys($this->ranks))) {
|
2015-11-07 22:58:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If all fails return false
|
|
|
|
return false;
|
2015-11-08 22:31:52 +00:00
|
|
|
}
|
2015-11-08 22:27:42 +00:00
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Add a new friend.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @param int $uid
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2015-10-12 18:25:37 +00:00
|
|
|
public function addFriend($uid)
|
|
|
|
{
|
|
|
|
// Add friend
|
2016-02-28 17:45:25 +00:00
|
|
|
DB::table('friends')
|
|
|
|
->insert([
|
|
|
|
'user_id' => $this->id,
|
|
|
|
'friend_id' => $uid,
|
|
|
|
'friend_timestamp' => time(),
|
|
|
|
]);
|
2015-10-12 18:25:37 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Remove a friend.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @param int $uid
|
|
|
|
* @param bool $deleteRequest
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2015-10-12 18:25:37 +00:00
|
|
|
public function removeFriend($uid, $deleteRequest = false)
|
|
|
|
{
|
|
|
|
// Remove friend
|
2016-03-13 20:35:51 +00:00
|
|
|
DB::table('friends')
|
|
|
|
->where('user_id', $this->id)
|
|
|
|
->where('friend_id', $uid)
|
|
|
|
->delete();
|
2015-10-12 18:25:37 +00:00
|
|
|
|
|
|
|
// Attempt to remove the request
|
|
|
|
if ($deleteRequest) {
|
2016-03-13 20:35:51 +00:00
|
|
|
DB::table('friends')
|
|
|
|
->where('user_id', $uid)
|
|
|
|
->where('friend_id', $this->id)
|
|
|
|
->delete();
|
2015-10-12 18:25:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Check if this user is friends with another user.
|
2016-08-05 02:35:37 +00:00
|
|
|
* 0 = no, 1 = pending request, 2 = mutual.
|
|
|
|
* @param int $with
|
|
|
|
* @return int
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2015-11-10 23:34:48 +00:00
|
|
|
public function isFriends($with)
|
2015-09-14 20:51:23 +00:00
|
|
|
{
|
2015-11-10 23:34:48 +00:00
|
|
|
// Accepted from this user
|
2016-03-13 20:35:51 +00:00
|
|
|
$user = DB::table('friends')
|
|
|
|
->where('user_id', $this->id)
|
|
|
|
->where('friend_id', $with)
|
|
|
|
->count();
|
2015-11-10 23:34:48 +00:00
|
|
|
|
|
|
|
// And the other user
|
2016-03-19 15:29:47 +00:00
|
|
|
$friend = DB::table('friends')
|
2016-03-13 20:35:51 +00:00
|
|
|
->where('user_id', $with)
|
|
|
|
->where('friend_id', $this->id)
|
|
|
|
->count();
|
2015-11-10 23:34:48 +00:00
|
|
|
|
|
|
|
if ($user && $friend) {
|
|
|
|
return 2; // Mutual friends
|
|
|
|
} elseif ($user) {
|
|
|
|
return 1; // Pending request
|
|
|
|
}
|
|
|
|
|
|
|
|
// Else return 0
|
|
|
|
return 0;
|
|
|
|
}
|
2015-10-12 18:25:37 +00:00
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get all the friends from this user.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @param int $level
|
|
|
|
* @param bool $noObj
|
|
|
|
* @return array
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2015-11-11 00:30:22 +00:00
|
|
|
public function friends($level = 0, $noObj = false)
|
2015-11-10 23:34:48 +00:00
|
|
|
{
|
|
|
|
// User ID container
|
|
|
|
$users = [];
|
|
|
|
|
|
|
|
// Select the correct level
|
|
|
|
switch ($level) {
|
2016-02-18 23:28:44 +00:00
|
|
|
// Mutual
|
2015-11-10 23:34:48 +00:00
|
|
|
case 2:
|
|
|
|
// Get all the current user's friends
|
2016-03-13 20:35:51 +00:00
|
|
|
$self = DB::table('friends')
|
|
|
|
->where('user_id', $this->id)
|
|
|
|
->get(['friend_id']);
|
|
|
|
$self = array_column($self, 'friend_id');
|
2016-02-18 23:28:44 +00:00
|
|
|
|
2015-11-10 23:34:48 +00:00
|
|
|
// Get all the people that added this user as a friend
|
2016-03-13 20:35:51 +00:00
|
|
|
$others = DB::table('friends')
|
|
|
|
->where('friend_id', $this->id)
|
|
|
|
->get(['user_id']);
|
|
|
|
$others = array_column($others, 'user_id');
|
2016-02-18 23:28:44 +00:00
|
|
|
|
2015-11-10 23:34:48 +00:00
|
|
|
// Create a difference map
|
|
|
|
$users = array_intersect($self, $others);
|
|
|
|
break;
|
|
|
|
|
2016-02-18 23:28:44 +00:00
|
|
|
// Non-mutual (from user perspective)
|
2015-11-10 23:34:48 +00:00
|
|
|
case 1:
|
2016-03-13 20:35:51 +00:00
|
|
|
$users = DB::table('friends')
|
|
|
|
->where('user_id', $this->id)
|
|
|
|
->get(['friend_id']);
|
|
|
|
$users = array_column($users, 'friend_id');
|
2015-11-10 23:34:48 +00:00
|
|
|
break;
|
|
|
|
|
2016-02-18 23:28:44 +00:00
|
|
|
// All friend cases
|
2015-11-10 23:34:48 +00:00
|
|
|
case 0:
|
|
|
|
default:
|
|
|
|
// Get all the current user's friends
|
2016-03-13 20:35:51 +00:00
|
|
|
$self = DB::table('friends')
|
|
|
|
->where('user_id', $this->id)
|
|
|
|
->get(['friend_id']);
|
|
|
|
$self = array_column($self, 'friend_id');
|
2016-02-18 23:28:44 +00:00
|
|
|
|
2015-11-10 23:34:48 +00:00
|
|
|
// Get all the people that added this user as a friend
|
2016-03-13 20:35:51 +00:00
|
|
|
$others = DB::table('friends')
|
|
|
|
->where('friend_id', $this->id)
|
|
|
|
->get(['user_id']);
|
|
|
|
$others = array_column($others, 'user_id');
|
2016-02-18 23:28:44 +00:00
|
|
|
|
2015-11-10 23:34:48 +00:00
|
|
|
// Create a difference map
|
|
|
|
$users = array_merge($others, $self);
|
|
|
|
break;
|
|
|
|
|
2016-02-18 23:28:44 +00:00
|
|
|
// Open requests
|
2015-11-10 23:34:48 +00:00
|
|
|
case -1:
|
|
|
|
// Get all the current user's friends
|
2016-03-13 20:35:51 +00:00
|
|
|
$self = DB::table('friends')
|
|
|
|
->where('user_id', $this->id)
|
|
|
|
->get(['friend_id']);
|
|
|
|
$self = array_column($self, 'friend_id');
|
2016-02-18 23:28:44 +00:00
|
|
|
|
2015-11-10 23:34:48 +00:00
|
|
|
// Get all the people that added this user as a friend
|
2016-03-13 20:35:51 +00:00
|
|
|
$others = DB::table('friends')
|
|
|
|
->where('friend_id', $this->id)
|
|
|
|
->get(['user_id']);
|
|
|
|
$others = array_column($others, 'user_id');
|
2016-02-18 23:28:44 +00:00
|
|
|
|
2015-11-10 23:34:48 +00:00
|
|
|
// Create a difference map
|
|
|
|
$users = array_diff($others, $self);
|
|
|
|
break;
|
2015-10-12 18:25:37 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 00:30:22 +00:00
|
|
|
// Check if we only requested the IDs
|
|
|
|
if ($noObj) {
|
|
|
|
// If so just return $users
|
|
|
|
return $users;
|
|
|
|
}
|
|
|
|
|
2015-11-10 23:34:48 +00:00
|
|
|
// Create the storage array
|
|
|
|
$objects = [];
|
|
|
|
|
2015-11-11 00:30:22 +00:00
|
|
|
// Create the user objects
|
2015-11-10 23:34:48 +00:00
|
|
|
foreach ($users as $user) {
|
|
|
|
// Create new object
|
2015-12-29 01:27:49 +00:00
|
|
|
$objects[$user] = User::construct($user);
|
2015-10-12 18:25:37 +00:00
|
|
|
}
|
|
|
|
|
2015-11-10 23:34:48 +00:00
|
|
|
// Return the objects
|
|
|
|
return $objects;
|
2015-08-19 12:13:38 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Check if the user has a certaing permission flag.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @param int $flag
|
|
|
|
* @param string $mode
|
|
|
|
* @return bool
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2015-12-29 21:52:19 +00:00
|
|
|
public function permission($flag, $mode = null)
|
2015-09-14 20:51:23 +00:00
|
|
|
{
|
2015-12-29 21:52:19 +00:00
|
|
|
// Set mode
|
|
|
|
$this->permissions->mode($mode ? $mode : Perms::SITE);
|
|
|
|
|
|
|
|
// Set default permission value
|
|
|
|
$perm = 0;
|
|
|
|
|
|
|
|
// Bitwise OR it with the permissions for this forum
|
2016-01-17 01:58:31 +00:00
|
|
|
$perm = $this->permissions->user($this->id);
|
2015-12-29 21:52:19 +00:00
|
|
|
|
|
|
|
return $this->permissions->check($flag, $perm);
|
2015-08-21 22:07:45 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
2016-03-28 14:47:43 +00:00
|
|
|
* Get the comments from the user's profile.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @return array
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2015-10-06 21:05:39 +00:00
|
|
|
public function profileComments()
|
|
|
|
{
|
2016-03-28 14:47:43 +00:00
|
|
|
$commentIds = DB::table('comments')
|
|
|
|
->where('comment_category', "profile-{$this->id}")
|
|
|
|
->orderBy('comment_id', 'desc')
|
|
|
|
->where('comment_reply_to', 0)
|
|
|
|
->get(['comment_id']);
|
|
|
|
$commentIds = array_column($commentIds, 'comment_id');
|
|
|
|
|
|
|
|
$comments = [];
|
|
|
|
|
|
|
|
foreach ($commentIds as $comment) {
|
|
|
|
$comments[$comment] = new Comment($comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $comments;
|
2015-10-06 21:05:39 +00:00
|
|
|
}
|
|
|
|
|
2016-03-27 21:18:57 +00:00
|
|
|
/**
|
|
|
|
* Add premium in seconds.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @param int $seconds
|
|
|
|
* @return int
|
2016-03-27 21:18:57 +00:00
|
|
|
*/
|
|
|
|
public function addPremium($seconds)
|
|
|
|
{
|
|
|
|
// Check if there's already a record of premium for this user in the database
|
|
|
|
$getUser = DB::table('premium')
|
|
|
|
->where('user_id', $this->id)
|
2016-08-07 14:32:14 +00:00
|
|
|
->first();
|
2016-03-27 21:18:57 +00:00
|
|
|
|
|
|
|
// Calculate the (new) start and expiration timestamp
|
2016-08-07 14:32:14 +00:00
|
|
|
$start = $getUser ? $getUser->premium_start : time();
|
|
|
|
$expire = $getUser ? $getUser->premium_expire + $seconds : time() + $seconds;
|
2016-03-27 21:18:57 +00:00
|
|
|
|
|
|
|
// If the user already exists do an update call, otherwise an insert call
|
|
|
|
if ($getUser) {
|
|
|
|
DB::table('premium')
|
|
|
|
->where('user_id', $this->id)
|
|
|
|
->update([
|
|
|
|
'premium_expire' => $expire,
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
DB::table('premium')
|
|
|
|
->insert([
|
|
|
|
'user_id' => $this->id,
|
|
|
|
'premium_start' => $start,
|
|
|
|
'premium_expire' => $expire,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the expiration timestamp
|
|
|
|
return $expire;
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Does this user have premium?
|
2016-08-05 02:35:37 +00:00
|
|
|
* @return int
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2015-11-11 21:54:56 +00:00
|
|
|
public function isPremium()
|
2015-09-14 20:51:23 +00:00
|
|
|
{
|
2016-03-27 21:18:57 +00:00
|
|
|
// Get rank IDs from the db
|
2016-07-26 17:29:53 +00:00
|
|
|
$premiumRank = (int) config('rank.premium');
|
|
|
|
$defaultRank = (int) config('rank.regular');
|
2016-03-27 21:18:57 +00:00
|
|
|
|
|
|
|
// Fetch expiration date
|
|
|
|
$expire = $this->premiumInfo()->expire;
|
2015-08-19 12:13:38 +00:00
|
|
|
|
|
|
|
// Check if the user has static premium
|
2016-08-07 14:32:14 +00:00
|
|
|
if (!$expire && $this->permission(Site::STATIC_PREMIUM)) {
|
2016-03-27 21:18:57 +00:00
|
|
|
$expire = time() + 1;
|
2015-08-19 12:13:38 +00:00
|
|
|
}
|
|
|
|
|
2016-03-27 21:18:57 +00:00
|
|
|
// Check if the user has premium and isn't in the premium rank
|
2016-08-07 14:32:14 +00:00
|
|
|
if ($expire && !$this->hasRanks([$premiumRank])) {
|
2016-03-27 21:18:57 +00:00
|
|
|
// Add the premium rank
|
|
|
|
$this->addRanks([$premiumRank]);
|
|
|
|
|
|
|
|
// Set it as default
|
|
|
|
if ($this->mainRankId == $defaultRank) {
|
|
|
|
$this->setMainRank($premiumRank);
|
|
|
|
}
|
2016-08-07 14:32:14 +00:00
|
|
|
} elseif (!$expire && $this->hasRanks([$premiumRank])) {
|
2016-03-27 21:18:57 +00:00
|
|
|
$this->removeRanks([$premiumRank]);
|
|
|
|
|
|
|
|
if ($this->mainRankId == $premiumRank) {
|
|
|
|
$this->setMainRank($defaultRank);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $expire;
|
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Gets the start and end date of this user's premium tag.
|
|
|
|
* @return stdClass
|
|
|
|
*/
|
2016-03-27 21:18:57 +00:00
|
|
|
public function premiumInfo()
|
|
|
|
{
|
2015-08-19 12:13:38 +00:00
|
|
|
// Attempt to retrieve the premium record from the database
|
2016-03-27 21:18:57 +00:00
|
|
|
$check = DB::table('premium')
|
2016-03-13 20:35:51 +00:00
|
|
|
->where('user_id', $this->id)
|
2016-03-27 21:18:57 +00:00
|
|
|
->where('premium_expire', '>', time())
|
2016-08-07 14:32:14 +00:00
|
|
|
->first();
|
2015-08-19 12:13:38 +00:00
|
|
|
|
2016-03-27 21:18:57 +00:00
|
|
|
$return = new stdClass;
|
2016-03-13 20:35:51 +00:00
|
|
|
|
2016-08-07 14:32:14 +00:00
|
|
|
$return->start = $check->premium_start ?? 0;
|
|
|
|
$return->expire = $check->premium_expire ?? 0;
|
2015-08-19 12:13:38 +00:00
|
|
|
|
2016-03-27 21:18:57 +00:00
|
|
|
return $return;
|
2015-08-19 12:13:38 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Parse the user's userpage.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @return string
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2015-09-26 16:12:42 +00:00
|
|
|
public function userPage()
|
|
|
|
{
|
2016-08-06 17:31:32 +00:00
|
|
|
return BBCode\Parser::toHTML(htmlentities($this->page));
|
2015-09-26 16:12:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
2016-08-05 02:35:37 +00:00
|
|
|
* Parse a user's signature.
|
|
|
|
* @return string
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2015-10-24 08:55:45 +00:00
|
|
|
public function signature()
|
|
|
|
{
|
2016-08-06 17:31:32 +00:00
|
|
|
return BBCode\Parser::toHTML(htmlentities($this->signature));
|
2015-10-24 08:55:45 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get a user's username history.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @return array
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2015-09-23 20:45:42 +00:00
|
|
|
public function getUsernameHistory()
|
|
|
|
{
|
2016-03-13 20:35:51 +00:00
|
|
|
return DB::table('username_history')
|
|
|
|
->where('user_id', $this->id)
|
|
|
|
->orderBy('change_id', 'desc')
|
|
|
|
->get();
|
2015-09-23 20:45:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
2016-08-05 02:35:37 +00:00
|
|
|
* Alter the user's username.
|
|
|
|
* @param string $username
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2016-08-07 14:32:14 +00:00
|
|
|
public function setUsername($username)
|
2015-09-23 20:45:42 +00:00
|
|
|
{
|
2016-08-07 14:32:14 +00:00
|
|
|
$username_clean = clean_string($username, true);
|
|
|
|
|
2016-03-13 20:35:51 +00:00
|
|
|
DB::table('username_history')
|
|
|
|
->insert([
|
|
|
|
'change_time' => time(),
|
|
|
|
'user_id' => $this->id,
|
2016-03-26 18:03:35 +00:00
|
|
|
'username_new' => $username,
|
|
|
|
'username_new_clean' => $username_clean,
|
2016-03-13 20:35:51 +00:00
|
|
|
'username_old' => $this->username,
|
|
|
|
'username_old_clean' => $this->usernameClean,
|
|
|
|
]);
|
2015-09-23 20:45:42 +00:00
|
|
|
|
2016-08-07 14:32:14 +00:00
|
|
|
$this->username = $username;
|
|
|
|
$this->usernameClean = $username_clean;
|
|
|
|
|
2016-03-13 20:35:51 +00:00
|
|
|
DB::table('users')
|
|
|
|
->where('user_id', $this->id)
|
|
|
|
->update([
|
2016-08-07 14:32:14 +00:00
|
|
|
'username' => $this->username,
|
|
|
|
'username_clean' => $this->usernameClean,
|
2016-03-13 20:35:51 +00:00
|
|
|
]);
|
2015-09-23 20:45:42 +00:00
|
|
|
}
|
2015-09-26 16:12:42 +00:00
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
2016-08-05 02:35:37 +00:00
|
|
|
* Alter a user's e-mail address.
|
|
|
|
* @param string $email
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2016-04-03 21:29:46 +00:00
|
|
|
public function setMail($email)
|
2015-09-26 16:12:42 +00:00
|
|
|
{
|
2016-08-07 14:32:14 +00:00
|
|
|
$this->email = $email;
|
|
|
|
|
2016-03-13 20:35:51 +00:00
|
|
|
DB::table('users')
|
|
|
|
->where('user_id', $this->id)
|
|
|
|
->update([
|
2016-08-07 14:32:14 +00:00
|
|
|
'email' => $this->email,
|
2016-03-13 20:35:51 +00:00
|
|
|
]);
|
2015-09-26 16:12:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
2016-08-05 02:35:37 +00:00
|
|
|
* Change the user's password.
|
|
|
|
* @param string $password
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2016-04-03 21:29:46 +00:00
|
|
|
public function setPassword($password)
|
2015-09-26 16:12:42 +00:00
|
|
|
{
|
|
|
|
// Create hash
|
2016-08-07 14:32:14 +00:00
|
|
|
$this->password = password_hash($password, PASSWORD_BCRYPT);
|
|
|
|
$this->passwordChan = time();
|
2015-09-26 16:12:42 +00:00
|
|
|
|
|
|
|
// Update userrow
|
2016-03-13 20:35:51 +00:00
|
|
|
DB::table('users')
|
|
|
|
->where('user_id', $this->id)
|
|
|
|
->update([
|
2016-08-07 14:32:14 +00:00
|
|
|
'password' => $this->password,
|
|
|
|
'password_chan' => $this->passwordChan,
|
2016-03-13 20:35:51 +00:00
|
|
|
]);
|
2015-09-26 16:12:42 +00:00
|
|
|
}
|
2016-03-26 16:36:58 +00:00
|
|
|
|
2016-08-01 21:09:27 +00:00
|
|
|
/**
|
2016-08-05 02:35:37 +00:00
|
|
|
* Check if password expired.
|
2016-08-01 21:09:27 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function passwordExpired()
|
|
|
|
{
|
|
|
|
return strlen($this->password) < 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-05 02:35:37 +00:00
|
|
|
* Verify the user's password.
|
2016-08-01 21:09:27 +00:00
|
|
|
* @param string $password
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function verifyPassword($password)
|
|
|
|
{
|
|
|
|
return password_verify($password, $this->password);
|
|
|
|
}
|
|
|
|
|
2016-03-26 16:36:58 +00:00
|
|
|
/**
|
|
|
|
* Get all the notifications for this user.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @param int $timeDifference
|
|
|
|
* @param bool $excludeRead
|
|
|
|
* @return array
|
2016-03-26 16:36:58 +00:00
|
|
|
*/
|
|
|
|
public function notifications($timeDifference = 0, $excludeRead = true)
|
|
|
|
{
|
|
|
|
$alertIds = DB::table('notifications')
|
|
|
|
->where('user_id', $this->id);
|
|
|
|
|
|
|
|
if ($timeDifference) {
|
|
|
|
$alertIds->where('alert_timestamp', '>', time() - $timeDifference);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($excludeRead) {
|
|
|
|
$alertIds->where('alert_read', 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
$alertIds = array_column($alertIds->get(['alert_id']), 'alert_id');
|
|
|
|
$alerts = [];
|
|
|
|
|
|
|
|
foreach ($alertIds as $alertId) {
|
2016-08-03 17:47:46 +00:00
|
|
|
$alerts[] = new Notification($alertId);
|
2016-03-26 16:36:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $alerts;
|
|
|
|
}
|
2016-08-07 14:10:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Invalidate all sessions related to this user.
|
|
|
|
*/
|
|
|
|
public function purgeSessions()
|
|
|
|
{
|
|
|
|
DB::table('sessions')
|
|
|
|
->where('user_id', $this->id)
|
|
|
|
->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all a user's sessions
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function sessions()
|
|
|
|
{
|
|
|
|
$sessions = [];
|
|
|
|
$ids = array_column(DB::table('sessions')
|
|
|
|
->where('user_id', $this->id)
|
|
|
|
->get(['session_id']), 'session_id');
|
|
|
|
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
$sessions[$id] = new Session($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sessions;
|
|
|
|
}
|
2016-08-07 14:23:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the user's selected design.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function design()
|
|
|
|
{
|
|
|
|
return Template::exists($this->design) ? $this->design : config('general.design');
|
|
|
|
}
|
2016-08-10 16:10:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the user's proper (highest) hierarchy.
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function hierarchy()
|
|
|
|
{
|
|
|
|
return DB::table('ranks')
|
|
|
|
->join('user_ranks', 'ranks.rank_id', '=', 'user_ranks.rank_id')
|
|
|
|
->where('user_id', $this->id)
|
|
|
|
->max('ranks.rank_hierarchy');
|
|
|
|
}
|
2016-09-19 15:33:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update last listened data.
|
|
|
|
*/
|
|
|
|
public function updateLastTrack()
|
|
|
|
{
|
|
|
|
if (strlen($this->lastfm) < 1
|
|
|
|
|| $this->musicCheck + config('user.music_update') > time()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$lfm = new UserApi(
|
|
|
|
new AuthApi('setsession', ['apiKey' => config('lastfm.api_key')])
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$last = $lfm->getRecentTracks(['user' => $this->lastfm, 'limit' => '1']);
|
|
|
|
} catch (LastFmApiExeption $e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($last) < 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->musicCheck = time();
|
|
|
|
$this->musicListening = isset($last[0]['nowplaying']);
|
|
|
|
$this->musicTrack = $last[0]['name'] ?? null;
|
|
|
|
$this->musicArtist = $last[0]['artist']['name'] ?? null;
|
|
|
|
|
|
|
|
DB::table('users')
|
|
|
|
->where('user_id', $this->id)
|
|
|
|
->update([
|
|
|
|
'user_music_check' => $this->musicCheck,
|
|
|
|
'user_music_listening' => $this->musicListening,
|
|
|
|
'user_music_track' => $this->musicTrack,
|
|
|
|
'user_music_artist' => $this->musicArtist,
|
|
|
|
]);
|
|
|
|
}
|
2015-08-18 23:29:45 +00:00
|
|
|
}
|