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/libraries/Users.php

370 lines
11 KiB
PHP
Raw Normal View History

2015-04-02 13:24:05 +00:00
<?php
2016-02-03 22:22:56 +00:00
/**
* Holds various functions to interface with users.
2016-02-05 11:20:33 +00:00
*
2016-02-03 22:22:56 +00:00
* @package Sakura
*/
2015-04-02 13:24:05 +00:00
namespace Sakura;
2015-12-29 21:52:19 +00:00
use Sakura\Perms\Site;
2016-03-19 15:29:47 +00:00
use Sakura\Router;
2015-12-29 21:52:19 +00:00
/**
2016-02-02 21:04:15 +00:00
* User management
2016-02-05 11:20:33 +00:00
*
* @package Sakura
2016-02-02 21:04:15 +00:00
* @author Julian van de Groep <me@flash.moe>
*/
class Users
{
2016-02-02 21:04:15 +00:00
/**
* Check if a user is logged in
2016-02-05 11:20:33 +00:00
*
2016-02-02 21:04:15 +00:00
* @param int $uid The user ID.
* @param string $sid The session ID.
2016-02-05 11:20:33 +00:00
*
2016-02-02 21:04:15 +00:00
* @return array|bool Either false or the ID and session in an array.
*/
public static function checkLogin($uid = null, $sid = null)
{
// Assign $uid and $sid
2015-12-04 14:19:10 +00:00
$uid = $uid ? $uid : (isset($_COOKIE[Config::get('cookie_prefix') . 'id'])
? $_COOKIE[Config::get('cookie_prefix') . 'id']
: 0);
2015-12-04 14:19:10 +00:00
$sid = $sid ? $sid : (isset($_COOKIE[Config::get('cookie_prefix') . 'session'])
? $_COOKIE[Config::get('cookie_prefix') . 'session']
: 0);
2015-08-23 22:08:36 +00:00
// Get session
$session = new Session($uid, $sid);
// Validate the session
$sessionValid = $session->validate();
2015-04-17 22:14:31 +00:00
2015-12-29 21:52:19 +00:00
// Get user object
$user = User::construct($uid);
// Check if the session exists and check if the user is activated
2015-12-29 21:52:19 +00:00
if ($sessionValid == 0 || $user->permission(Site::DEACTIVATED)) {
// Unset User ID
setcookie(
2015-12-04 14:19:10 +00:00
Config::get('cookie_prefix') . 'id',
0,
time() - 60,
2016-02-02 21:04:15 +00:00
Config::get('cookie_path')
);
// Unset Session ID
setcookie(
2015-12-04 14:19:10 +00:00
Config::get('cookie_prefix') . 'session',
'',
time() - 60,
2016-02-02 21:04:15 +00:00
Config::get('cookie_path')
);
2015-06-04 12:41:55 +00:00
return false;
}
2015-04-17 22:14:31 +00:00
// Extend the cookie times if the remember flag is set
if ($sessionValid == 2) {
2015-09-14 21:41:43 +00:00
// User ID cookie
setcookie(
2015-12-04 14:19:10 +00:00
Config::get('cookie_prefix') . 'id',
2015-09-14 21:41:43 +00:00
$uid,
time() + 604800,
2016-02-02 21:04:15 +00:00
Config::get('cookie_path')
2015-09-14 21:41:43 +00:00
);
// Session ID cookie
setcookie(
2015-12-04 14:19:10 +00:00
Config::get('cookie_prefix') . 'session',
2015-09-14 21:41:43 +00:00
$sid,
time() + 604800,
2016-02-02 21:04:15 +00:00
Config::get('cookie_path')
2015-09-14 21:41:43 +00:00
);
2015-04-17 22:14:31 +00:00
}
2015-04-27 00:41:59 +00:00
// Update last online
DB::table('users')
->where('user_id', $uid)
->update([
'user_last_online' => time(),
]);
2015-04-27 00:41:59 +00:00
2015-07-01 14:29:12 +00:00
// Update the premium meta
self::updatePremiumMeta($uid);
2015-07-01 14:29:12 +00:00
2015-04-17 22:14:31 +00:00
// If everything went through return true
return [$uid, $sid];
2015-04-02 13:24:05 +00:00
}
2016-02-02 21:04:15 +00:00
/**
* Send password forgot e-mail
2016-02-05 11:20:33 +00:00
*
2016-03-19 15:29:47 +00:00
* @param string $userId The user id.
2016-02-02 21:04:15 +00:00
* @param string $email The e-mail.
*/
2016-03-19 15:29:47 +00:00
public static function sendPasswordForgot($userId, $email)
{
2016-03-19 15:29:47 +00:00
$user = User::construct($userId);
2015-12-29 21:52:19 +00:00
2016-03-19 15:29:47 +00:00
if (!$user->id || $user->permission(Site::DEACTIVATED)) {
return;
}
2015-04-25 20:08:44 +00:00
// Generate the verification key
2016-03-19 15:29:47 +00:00
$verk = ActionCode::generate('LOST_PASS', $user->id);
2015-04-25 20:08:44 +00:00
2016-03-19 15:29:47 +00:00
$siteName = Config::get('sitename');
$baseUrl = "http://" . Config::get('url_main');
$reactivateLink = Router::route('auth.resetpassword') . "?u={$user->id}&k={$verk}";
$signature = Config::get('mail_signature');
2015-04-25 20:08:44 +00:00
// Build the e-mail
2016-03-19 15:29:47 +00:00
$message = "Hello {$user->username},\r\n\r\n"
. "You are receiving this notification because you have (or someone pretending to be you has)"
. " requested a password reset link to be sent for your account on \"{$siteName}\"."
. " If you did not request this notification then please ignore it,"
. " if you keep receiving it please contact the site administrator.\r\n\r\n"
. "To use this password reset key you need to go to a special page."
. " To do this click the link provided below.\r\n\r\n"
. "{$baseUrl}{$reactivateLink}\r\n\r\n"
. "If successful you should be able to change your password here.\r\n\r\n"
. "You can of course change this password yourself via the settings page."
. " If you have any difficulties please contact the site administrator.\r\n\r\n"
. "--\r\n\r\nThanks\r\n\r\n{$signature}";
2015-04-25 20:08:44 +00:00
// Send the message
2016-03-19 15:29:47 +00:00
Utils::sendMail([$user->email => $user->username], "{$siteName} password restoration", $message);
2015-04-21 14:23:28 +00:00
}
2016-02-02 21:04:15 +00:00
/**
* Send activation e-mail.
2016-02-05 11:20:33 +00:00
*
2016-03-19 15:29:47 +00:00
* @param mixed $userId User ID.
2016-02-02 21:04:15 +00:00
* @param mixed $customKey Key.
*/
2016-03-19 15:29:47 +00:00
public static function sendActivationMail($userId, $customKey = null)
{
2015-04-18 18:26:52 +00:00
// Get the user data
2016-03-19 15:29:47 +00:00
$user = User::construct($userId);
2015-04-18 18:26:52 +00:00
// User is already activated or doesn't even exist
2016-01-17 01:58:31 +00:00
if (!$user->id || !$user->permission(Site::DEACTIVATED)) {
2016-03-19 15:29:47 +00:00
return;
}
2015-04-18 18:26:52 +00:00
// Generate activation key
2016-01-17 01:58:31 +00:00
$activate = ActionCode::generate('ACTIVATE', $user->id);
2015-04-18 18:26:52 +00:00
2016-03-19 15:29:47 +00:00
$siteName = Config::get('sitename');
$baseUrl = "http://" . Config::get('url_main');
$activateLink = Router::route('auth.activate') . "?u={$user->id}&k={$activate}";
$profileLink = Router::route('user.profile', $user->id);
$signature = Config::get('mail_signature');
2015-04-18 18:26:52 +00:00
// Build the e-mail
2016-03-19 15:29:47 +00:00
$message = "Welcome to {$siteName}!\r\n\r\n"
. "Please keep this e-mail for your records. Your account intormation is as follows:\r\n\r\n"
. "----------------------------\r\n\r\n"
. "Username: {$user->username}\r\n\r\n"
. "Your profile: {$baseUrl}{$profileLink}\r\n\r\n"
. "----------------------------\r\n\r\n"
. "Please visit the following link in order to activate your account:\r\n\r\n"
. "{$baseUrl}{$activateLink}\r\n\r\n"
. "Your password has been securely stored in our database and cannot be retrieved. "
. "In the event that it is forgotten,"
. " you will be able to reset it using the email address associated with your account.\r\n\r\n"
. "Thank you for registering.\r\n\r\n"
. "--\r\n\r\nThanks\r\n\r\n{$signature}";
2015-04-18 18:26:52 +00:00
// Send the message
2016-03-19 15:29:47 +00:00
Utils::sendMail([$user->email => $user->username], "{$siteName} activation mail", $message);
2015-04-18 18:26:52 +00:00
}
2016-02-02 21:04:15 +00:00
/**
* Get all available profile fields.
2016-02-05 11:20:33 +00:00
*
2016-02-02 21:04:15 +00:00
* @return array|null The fields.
*/
public static function getProfileFields()
{
2015-06-27 11:03:11 +00:00
// Get profile fields
$profileFields = DB::table('profilefields')
->get();
2015-06-27 11:03:11 +00:00
// If there's nothing just return null
if (!count($profileFields)) {
2015-06-27 11:03:11 +00:00
return null;
}
2015-06-27 11:03:11 +00:00
// Create output array
$fields = [];
// Iterate over the fields and clean them up
foreach ($profileFields as $field) {
$field = get_object_vars($field);
$fields[$field['field_id']] = $field;
2016-01-17 01:58:31 +00:00
$fields[$field['field_id']]['field_identity'] = Utils::cleanString($field['field_name'], true, true);
$fields[$field['field_id']]['field_additional'] = json_decode($field['field_additional'], true);
2015-06-27 11:03:11 +00:00
}
// Return the yeahs
return $fields;
}
2016-02-02 21:04:15 +00:00
/**
* Get all available option fields.
2016-02-05 11:20:33 +00:00
*
2016-02-02 21:04:15 +00:00
* @return array|null The fields.
*/
public static function getOptionFields()
{
2015-08-21 22:07:45 +00:00
// Get option fields
$optionFields = DB::table('optionfields')
->get();
2015-08-21 22:07:45 +00:00
// If there's nothing just return null
if (!count($optionFields)) {
2015-08-21 22:07:45 +00:00
return null;
}
// Create output array
$fields = [];
2015-12-29 21:52:19 +00:00
$user = User::construct(self::checkLogin()[0]);
2015-08-21 22:07:45 +00:00
// Iterate over the fields and clean them up
foreach ($optionFields as $field) {
$field = get_object_vars($field);
2015-12-29 21:52:19 +00:00
if (!$user->permission(constant('Sakura\Perms\Site::' . $field['option_permission']))) {
2015-08-21 22:07:45 +00:00
continue;
}
$fields[$field['option_id']] = $field;
2015-08-21 22:07:45 +00:00
}
// Return the yeahs
return $fields;
}
2016-02-02 21:04:15 +00:00
/**
* Get all online users.
2016-02-05 11:20:33 +00:00
*
2016-02-02 21:04:15 +00:00
* @return array Array containing User instances.
*/
public static function checkAllOnline()
{
2015-04-27 00:41:59 +00:00
// Assign time - 500 to a variable
2015-12-04 14:19:10 +00:00
$time = time() - Config::get('max_online_time');
$return = [];
2015-04-27 00:41:59 +00:00
// Get all online users in the past 5 minutes
$getAll = DB::table('users')
->where('user_last_online', '>', $time)
->get();
2015-04-27 00:41:59 +00:00
foreach ($getAll as $user) {
2016-02-18 23:28:44 +00:00
$return[] = User::construct($user->user_id);
}
2015-04-27 00:41:59 +00:00
// Return all the online users
return $return;
2015-04-27 00:41:59 +00:00
}
2016-02-02 21:04:15 +00:00
/**
* Add premium time to a user.
2016-02-05 11:20:33 +00:00
*
2016-02-02 21:04:15 +00:00
* @param int $id The user ID.
* @param int $seconds The amount of extra seconds.
2016-02-05 11:20:33 +00:00
*
2016-02-02 21:04:15 +00:00
* @return array|double|int The new expiry date.
*/
public static function addUserPremium($id, $seconds)
{
2015-07-01 14:29:12 +00:00
// Check if there's already a record of premium for this user in the database
$getUser = DB::table('premium')
->where('user_id', $id)
->count();
2015-07-01 14:29:12 +00:00
// Calculate the (new) start and expiration timestamp
$start = isset($getUser['premium_start']) ? $getUser['premium_start'] : time();
$expire = isset($getUser['premium_expire']) ? $getUser['premium_expire'] + $seconds : time() + $seconds;
2015-07-01 14:29:12 +00:00
// If the user already exists do an update call, otherwise an insert call
if (empty($getUser)) {
DB::table('premium')
->insert([
'user_id' => $id,
'premium_start' => $start,
'premium_expire' => $expire,
]);
2015-07-01 14:29:12 +00:00
} else {
DB::table('premium')
->where('user_id', $id)
->update('premium_expire', $expire);
2015-07-01 14:29:12 +00:00
}
// Return the expiration timestamp
return $expire;
}
2016-02-02 21:04:15 +00:00
/**
* Process premium meta data.
2016-02-05 11:20:33 +00:00
*
2016-02-02 21:04:15 +00:00
* @param int $id The user ID.
*/
public static function updatePremiumMeta($id)
{
2015-07-01 14:29:12 +00:00
// Get the ID for the premium user rank from the database
2015-12-04 14:19:10 +00:00
$premiumRank = Config::get('premium_rank_id');
$excepted = Config::get('restricted_rank_id');
2015-07-01 14:29:12 +00:00
// Create user object
2015-12-29 01:27:49 +00:00
$user = User::construct($id);
2015-07-01 14:29:12 +00:00
// Run the check
$check = $user->isPremium();
2015-07-01 14:29:12 +00:00
// Check if the user has premium
2016-01-17 01:58:31 +00:00
if ($check[0] && !array_key_exists($excepted, $user->ranks)) {
2015-07-01 14:29:12 +00:00
// If so add the rank to them
$user->addRanks([$premiumRank]);
2015-07-01 14:29:12 +00:00
// Check if the user's default rank is standard user and update it to premium
2016-01-17 01:58:31 +00:00
if ($user->mainRankId == 2) {
$user->setMainRank($premiumRank);
}
} elseif (!$check[0]) {
// Remove the expired entry
DB::table('premium')
->where('user_id', $user->id)
->delete();
2015-07-01 14:29:12 +00:00
// Else remove the rank from them
$user->removeRanks([$premiumRank]);
2015-07-01 14:29:12 +00:00
}
2015-04-27 00:41:59 +00:00
}
2016-02-02 21:04:15 +00:00
/**
* Get the newest member's ID.
2016-02-05 11:20:33 +00:00
*
2016-02-02 21:04:15 +00:00
* @return int The user ID.
*/
public static function getNewestUserId()
{
$get = DB::table('users')
->where('rank_main', '!=', Config::get('restricted_rank_id'))
2016-03-17 19:09:00 +00:00
->where('rank_main', '!=', Config::get('deactive_rank_id'))
->orderBy('user_id', 'desc')
->limit(1)
->get(['user_id']);
2016-02-18 23:28:44 +00:00
return $get ? $get[0]->user_id : 0;
2015-08-28 20:32:31 +00:00
}
2015-04-02 13:24:05 +00:00
}