2016-03-28 01:18:59 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Holds the account section controller.
|
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Sakura\Controllers\Settings;
|
|
|
|
|
2016-08-07 14:10:27 +00:00
|
|
|
use Sakura\CurrentSession;
|
2016-04-03 21:29:46 +00:00
|
|
|
use Sakura\DB;
|
|
|
|
use Sakura\Perms\Site;
|
|
|
|
|
2016-03-28 01:18:59 +00:00
|
|
|
/**
|
|
|
|
* Account settings.
|
|
|
|
* @package Sakura
|
|
|
|
* @author Julian van de Groep <me@flash.moe>
|
|
|
|
*/
|
|
|
|
class AccountController extends Controller
|
|
|
|
{
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Renders the profile changing page.
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-08-02 20:35:12 +00:00
|
|
|
public function profile()
|
|
|
|
{
|
|
|
|
// Check permission
|
2016-08-07 14:10:27 +00:00
|
|
|
if (!CurrentSession::$user->permission(Site::ALTER_PROFILE)) {
|
2016-08-02 20:35:12 +00:00
|
|
|
$message = "You aren't allowed to edit your profile!";
|
|
|
|
$redirect = route('settings.index');
|
|
|
|
return view('global/information', compact('message', 'redirect'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (session_check()) {
|
|
|
|
$redirect = route('settings.account.profile');
|
|
|
|
$save = [];
|
|
|
|
$allowed = [
|
|
|
|
'website',
|
|
|
|
'twitter',
|
|
|
|
'github',
|
|
|
|
'skype',
|
|
|
|
'discord',
|
|
|
|
'youtube',
|
|
|
|
'steam',
|
|
|
|
'osu',
|
|
|
|
'lastfm',
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($allowed as $field) {
|
|
|
|
$save["user_{$field}"] = $_POST["profile_{$field}"] ?? null;
|
|
|
|
}
|
|
|
|
|
|
|
|
DB::table('users')
|
2016-08-07 14:10:27 +00:00
|
|
|
->where('user_id', CurrentSession::$user->id)
|
2016-08-02 20:35:12 +00:00
|
|
|
->update($save);
|
|
|
|
|
|
|
|
// Birthdays
|
|
|
|
if (isset($_POST['birthday_day'], $_POST['birthday_month'], $_POST['birthday_year'])) {
|
|
|
|
$day = intval($_POST['birthday_day']);
|
|
|
|
$month = intval($_POST['birthday_month']);
|
|
|
|
$year = intval($_POST['birthday_year']);
|
|
|
|
|
|
|
|
if (!$day && !$month && !$year) {
|
|
|
|
$birthdate = null;
|
|
|
|
} else {
|
|
|
|
if (!checkdate($month, $day, $year ? $year : 1)
|
|
|
|
|| $year > date("Y")
|
|
|
|
|| ($year != 0 && $year < (date("Y") - 100))) {
|
|
|
|
$message = "Your birthdate was invalid, everything else was saved though!";
|
|
|
|
|
|
|
|
return view('global/information', compact('message', 'redirect'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Combine it into a YYYY-MM-DD format
|
|
|
|
$birthdate = implode('-', compact('year', 'month', 'day'));
|
|
|
|
}
|
|
|
|
|
|
|
|
DB::table('users')
|
2016-08-07 14:10:27 +00:00
|
|
|
->where('user_id', CurrentSession::$user->id)
|
2016-08-02 20:35:12 +00:00
|
|
|
->update([
|
|
|
|
'user_birthday' => $birthdate,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$message = "Updated your profile!";
|
|
|
|
|
|
|
|
return view('global/information', compact('message', 'redirect'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('settings/account/profile');
|
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Renders the e-mail changing page.
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-03-28 01:18:59 +00:00
|
|
|
public function email()
|
|
|
|
{
|
2016-04-03 21:29:46 +00:00
|
|
|
// Check permission
|
2016-08-07 14:10:27 +00:00
|
|
|
if (!CurrentSession::$user->permission(Site::CHANGE_EMAIL)) {
|
2016-04-03 21:29:46 +00:00
|
|
|
$message = "You aren't allowed to change your e-mail address.";
|
2016-08-02 20:35:12 +00:00
|
|
|
$redirect = route('settings.index');
|
|
|
|
return view('global/information', compact('message', 'redirect'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$email = $_POST['email'] ?? null;
|
|
|
|
|
2016-08-02 20:35:12 +00:00
|
|
|
if (session_check() && $email) {
|
|
|
|
$redirect = route('settings.account.email');
|
2016-04-03 21:29:46 +00:00
|
|
|
|
|
|
|
// Validate e-mail address
|
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
|
|
$message = "The e-mail address you supplied is invalid!";
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the MX record of the email
|
|
|
|
if (!check_mx_record($email)) {
|
|
|
|
$message = 'No valid MX-Record found on the e-mail address you supplied.';
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the e-mail has already been used
|
|
|
|
$emailCheck = DB::table('users')
|
|
|
|
->where('email', $email)
|
|
|
|
->count();
|
|
|
|
if ($emailCheck) {
|
|
|
|
$message = 'Someone already used this e-mail!';
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 14:10:27 +00:00
|
|
|
CurrentSession::$user->setMail($email);
|
2016-04-03 21:29:46 +00:00
|
|
|
|
|
|
|
$message = 'Changed your e-mail address!';
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('settings/account/email');
|
2016-03-28 01:18:59 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Renders the username changing page.
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-03-28 01:18:59 +00:00
|
|
|
public function username()
|
|
|
|
{
|
2016-04-03 21:29:46 +00:00
|
|
|
// Check permission
|
2016-08-07 14:10:27 +00:00
|
|
|
if (!CurrentSession::$user->permission(Site::CHANGE_USERNAME)) {
|
2016-04-03 21:29:46 +00:00
|
|
|
$message = "You aren't allowed to change your username.";
|
2016-08-02 20:35:12 +00:00
|
|
|
$redirect = route('settings.index');
|
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$username = $_POST['username'] ?? null;
|
|
|
|
|
2016-08-02 20:35:12 +00:00
|
|
|
if (session_check() && $username) {
|
|
|
|
$redirect = route('settings.account.username');
|
2016-04-03 21:29:46 +00:00
|
|
|
$username_clean = clean_string($username, true);
|
|
|
|
|
|
|
|
// Check if the username is too short
|
2016-07-26 17:29:53 +00:00
|
|
|
if (strlen($username_clean) < config('user.name_min')) {
|
2016-04-03 21:29:46 +00:00
|
|
|
$message = "This username is too short!";
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the username is too long
|
2016-07-26 17:29:53 +00:00
|
|
|
if (strlen($username_clean) > config('user.name_max')) {
|
2016-04-03 21:29:46 +00:00
|
|
|
$message = "This username is too long!";
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if this username hasn't been used in the last amount of days set in the config
|
|
|
|
$getOld = DB::table('username_history')
|
|
|
|
->where('username_old_clean', $username_clean)
|
2016-07-26 17:29:53 +00:00
|
|
|
->where('change_time', '>', (config('user.name_reserve') * 24 * 60 * 60))
|
2016-04-03 21:29:46 +00:00
|
|
|
->orderBy('change_id', 'desc')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
// Check if anything was returned
|
2016-08-07 14:10:27 +00:00
|
|
|
if ($getOld && $getOld[0]->user_id != CurrentSession::$user->id) {
|
2016-04-03 21:29:46 +00:00
|
|
|
$message = "The username you tried to use is reserved, try again later!";
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the username is already in use
|
|
|
|
$getInUse = DB::table('users')
|
|
|
|
->where('username_clean', $username_clean)
|
|
|
|
->get();
|
|
|
|
|
|
|
|
// Check if anything was returned
|
|
|
|
if ($getInUse) {
|
|
|
|
$message = "Someone is already using this name!";
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 14:10:27 +00:00
|
|
|
CurrentSession::$user->setUsername($username, $username_clean);
|
2016-04-03 21:29:46 +00:00
|
|
|
|
|
|
|
$message = "Changed your username!";
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('settings/account/username');
|
2016-03-28 01:18:59 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Renders the user title changing page.
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-03-28 01:18:59 +00:00
|
|
|
public function title()
|
|
|
|
{
|
2016-04-03 21:29:46 +00:00
|
|
|
// Check permission
|
2016-08-07 14:10:27 +00:00
|
|
|
if (!CurrentSession::$user->permission(Site::CHANGE_USERTITLE)) {
|
2016-04-03 21:29:46 +00:00
|
|
|
$message = "You aren't allowed to change your title.";
|
2016-08-02 20:35:12 +00:00
|
|
|
$redirect = route('settings.index');
|
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$title = $_POST['title'] ?? null;
|
|
|
|
|
2016-08-02 20:35:12 +00:00
|
|
|
if (session_check() && $title !== null) {
|
|
|
|
$redirect = route('settings.account.title');
|
2016-04-03 21:29:46 +00:00
|
|
|
|
|
|
|
if (strlen($title) > 64) {
|
|
|
|
$message = "This title is too long!";
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 14:10:27 +00:00
|
|
|
if ($title === CurrentSession::$user->title) {
|
2016-04-03 21:29:46 +00:00
|
|
|
$message = "This is already your title!";
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update database
|
|
|
|
DB::table('users')
|
2016-08-07 14:10:27 +00:00
|
|
|
->where('user_id', CurrentSession::$user->id)
|
2016-04-03 21:29:46 +00:00
|
|
|
->update([
|
|
|
|
'user_title' => $title,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$message = "Changed your title!";
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('settings/account/title');
|
2016-03-28 01:18:59 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Renders the password changing page.
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-03-28 01:18:59 +00:00
|
|
|
public function password()
|
|
|
|
{
|
2016-04-03 21:29:46 +00:00
|
|
|
// Check permission
|
2016-08-07 14:10:27 +00:00
|
|
|
if (!CurrentSession::$user->permission(Site::CHANGE_PASSWORD)) {
|
2016-04-03 21:29:46 +00:00
|
|
|
$message = "You aren't allowed to change your password.";
|
2016-08-02 20:35:12 +00:00
|
|
|
$redirect = route('settings.index');
|
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$current = $_POST['current'] ?? null;
|
|
|
|
$password = $_POST['password'] ?? null;
|
|
|
|
|
2016-08-02 20:35:12 +00:00
|
|
|
if (session_check() && $current && $password) {
|
|
|
|
$redirect = route('settings.account.password');
|
2016-04-03 21:29:46 +00:00
|
|
|
|
|
|
|
// Check current password
|
2016-08-07 14:10:27 +00:00
|
|
|
if (!password_verify($current, CurrentSession::$user->password)) {
|
2016-04-03 21:29:46 +00:00
|
|
|
$message = "Your password was invalid!";
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check password entropy
|
2016-07-26 17:29:53 +00:00
|
|
|
if (password_entropy($password) < config('user.pass_min_entropy')) {
|
2016-04-03 21:29:46 +00:00
|
|
|
$message = "Your password isn't strong enough!";
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 14:10:27 +00:00
|
|
|
CurrentSession::$user->setPassword($password);
|
2016-04-03 21:29:46 +00:00
|
|
|
|
|
|
|
$message = "Changed your password!";
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('settings/account/password');
|
2016-03-28 01:18:59 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Renders the rank management page.
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-03-28 01:18:59 +00:00
|
|
|
public function ranks()
|
|
|
|
{
|
2016-04-03 21:29:46 +00:00
|
|
|
// Check permission
|
2016-08-07 14:10:27 +00:00
|
|
|
if (!CurrentSession::$user->permission(Site::ALTER_RANKS)) {
|
2016-04-03 21:29:46 +00:00
|
|
|
$message = "You aren't allowed to manage your ranks.";
|
2016-08-02 20:35:12 +00:00
|
|
|
$redirect = route('settings.index');
|
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$rank = $_POST['rank'] ?? null;
|
|
|
|
$mode = $_POST['mode'] ?? null;
|
|
|
|
|
|
|
|
$locked = [
|
2016-07-26 17:29:53 +00:00
|
|
|
config('rank.inactive'),
|
|
|
|
config('rank.regular'),
|
|
|
|
config('rank.premium'),
|
|
|
|
config('rank.alumni'),
|
|
|
|
config('rank.banned'),
|
2016-04-03 21:29:46 +00:00
|
|
|
];
|
|
|
|
|
2016-08-02 20:35:12 +00:00
|
|
|
if (session_check() && $rank && $mode) {
|
|
|
|
$redirect = route('settings.account.ranks');
|
2016-04-03 21:29:46 +00:00
|
|
|
|
|
|
|
// Check if user has this rank
|
2016-08-07 14:10:27 +00:00
|
|
|
if (!CurrentSession::$user->hasRanks([$rank])) {
|
2016-04-03 21:29:46 +00:00
|
|
|
$message = "You aren't a part of this rank!";
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($mode == 'remove') {
|
|
|
|
if (in_array($rank, $locked)) {
|
|
|
|
$message = "You aren't allowed to remove this rank from your account!";
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 14:10:27 +00:00
|
|
|
CurrentSession::$user->removeRanks([$rank]);
|
2016-04-03 21:29:46 +00:00
|
|
|
|
|
|
|
$message = "Removed the rank from your account!";
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 14:10:27 +00:00
|
|
|
CurrentSession::$user->setMainRank($rank);
|
2016-04-03 21:29:46 +00:00
|
|
|
|
|
|
|
$message = "Changed your main rank!";
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('global/information', compact('redirect', 'message'));
|
2016-04-03 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 20:35:12 +00:00
|
|
|
return view('settings/account/ranks', compact('locked'));
|
2016-03-28 01:18:59 +00:00
|
|
|
}
|
|
|
|
}
|