From 76d9502d57b73595653650746cb770e4ccad9936 Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 8 Sep 2016 23:32:33 +0200 Subject: [PATCH] sakura isn't dead? wow --- .../Settings/AccountController.php | 293 +++++++----------- app/Controllers/Settings/Controller.php | 41 +-- .../views/yuuno/settings/account/details.twig | 40 +++ .../views/yuuno/settings/account/email.twig | 21 -- .../yuuno/settings/account/password.twig | 24 -- .../views/yuuno/settings/account/title.twig | 21 -- .../yuuno/settings/account/username.twig | 27 -- routes.php | 10 +- 8 files changed, 178 insertions(+), 299 deletions(-) create mode 100644 resources/views/yuuno/settings/account/details.twig delete mode 100644 resources/views/yuuno/settings/account/email.twig delete mode 100644 resources/views/yuuno/settings/account/password.twig delete mode 100644 resources/views/yuuno/settings/account/title.twig delete mode 100644 resources/views/yuuno/settings/account/username.twig diff --git a/app/Controllers/Settings/AccountController.php b/app/Controllers/Settings/AccountController.php index 3d94e02..70911cb 100644 --- a/app/Controllers/Settings/AccountController.php +++ b/app/Controllers/Settings/AccountController.php @@ -90,197 +90,142 @@ class AccountController extends Controller } /** - * Renders the e-mail changing page. + * Details such as email, username and password. * @return string */ - public function email() + public function details() { - // Check permission - if (!CurrentSession::$user->permission(Site::CHANGE_EMAIL)) { - $message = "You aren't allowed to change your e-mail address."; - $redirect = route('settings.index'); - return view('global/information', compact('message', 'redirect')); + $user = CurrentSession::$user; + + // Check permissions + $edit_email = $user->permission(Site::CHANGE_EMAIL); + $edit_usern = $user->permission(Site::CHANGE_USERNAME); + $edit_title = $user->permission(Site::CHANGE_USERTITLE); + $edit_passw = $user->permission(Site::CHANGE_PASSWORD); + $last_name_change = 0; + + if ($edit_usern) { + $last_name_change = $user->getUsernameHistory()[0]->change_time ?? 0; } - $email = $_POST['email'] ?? null; + // Check eligibility for username changes + $username_allow = $edit_usern && (time() - $last_name_change) > 2592000; - if (session_check() && $email) { - $redirect = route('settings.account.email'); + if (isset($_POST['session']) && session_check()) { + $redirect = route('settings.account.details'); + $email = $_POST['email'] ?? null; - // Validate e-mail address - if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { - $message = "The e-mail address you supplied is invalid!"; - return view('global/information', compact('redirect', 'message')); + if ($email) { + // Validate e-mail address + if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { + $message = "The e-mail address you supplied is invalid!"; + return view('global/information', compact('redirect', 'message')); + } + + // 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.'; + return view('global/information', compact('redirect', 'message')); + } + + // 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!'; + return view('global/information', compact('redirect', 'message')); + } + + $user->setMail($email); } - // 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.'; - return view('global/information', compact('redirect', 'message')); + $username = $_POST['username'] ?? null; + + if ($username) { + $username_clean = clean_string($username, true); + + // Check if the username is too short + if (strlen($username_clean) < config('user.name_min')) { + $message = "This username is too short!"; + return view('global/information', compact('redirect', 'message')); + } + + // Check if the username is too long + if (strlen($username_clean) > config('user.name_max')) { + $message = "This username is too long!"; + return view('global/information', compact('redirect', 'message')); + } + + // 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) + ->where('change_time', '>', (config('user.name_reserve') * 24 * 60 * 60)) + ->orderBy('change_id', 'desc') + ->first(); + + // Check if anything was returned + if ($getOld && $getOld->user_id != $user->id) { + $message = "The username you tried to use is reserved, try again later!"; + return view('global/information', compact('redirect', 'message')); + } + + // Check if the username is already in use + $getInUse = DB::table('users') + ->where('username_clean', $username_clean) + ->count(); + + // Check if anything was returned + if ($getInUse) { + $message = "Someone is already using this name!"; + return view('global/information', compact('redirect', 'message')); + } + + $user->setUsername($username); } - // 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!'; - return view('global/information', compact('redirect', 'message')); + $title = $_POST['title'] ?? null; + + if ($title) { + if (strlen($title) > 64) { + $message = "This title is too long!"; + return view('global/information', compact('redirect', 'message')); + } + + if ($title !== $user->title) { + // Update database + DB::table('users') + ->where('user_id', $user->id) + ->update([ + 'user_title' => $title, + ]); + } } - CurrentSession::$user->setMail($email); + $password = $_POST['password'] ?? null; - $message = 'Changed your e-mail address!'; + if ($password) { + // Check password entropy + if (password_entropy($password) < config('user.pass_min_entropy')) { + $message = "Your password isn't strong enough!"; + return view('global/information', compact('redirect', 'message')); + } + + $user->setPassword($password); + } + + $message = "Saved!"; return view('global/information', compact('redirect', 'message')); } - return view('settings/account/email'); - } - - /** - * Renders the username changing page. - * @return string - */ - public function username() - { - // Check permission - if (!CurrentSession::$user->permission(Site::CHANGE_USERNAME)) { - $message = "You aren't allowed to change your username."; - $redirect = route('settings.index'); - return view('global/information', compact('redirect', 'message')); - } - - $username = $_POST['username'] ?? null; - - if (session_check() && $username) { - $redirect = route('settings.account.username'); - $username_clean = clean_string($username, true); - - // Check if the username is too short - if (strlen($username_clean) < config('user.name_min')) { - $message = "This username is too short!"; - return view('global/information', compact('redirect', 'message')); - } - - // Check if the username is too long - if (strlen($username_clean) > config('user.name_max')) { - $message = "This username is too long!"; - return view('global/information', compact('redirect', 'message')); - } - - // 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) - ->where('change_time', '>', (config('user.name_reserve') * 24 * 60 * 60)) - ->orderBy('change_id', 'desc') - ->get(); - - // Check if anything was returned - if ($getOld && $getOld[0]->user_id != CurrentSession::$user->id) { - $message = "The username you tried to use is reserved, try again later!"; - return view('global/information', compact('redirect', 'message')); - } - - // 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!"; - return view('global/information', compact('redirect', 'message')); - } - - CurrentSession::$user->setUsername($username); - - $message = "Changed your username!"; - return view('global/information', compact('redirect', 'message')); - } - - return view('settings/account/username'); - } - - /** - * Renders the user title changing page. - * @return string - */ - public function title() - { - // Check permission - if (!CurrentSession::$user->permission(Site::CHANGE_USERTITLE)) { - $message = "You aren't allowed to change your title."; - $redirect = route('settings.index'); - return view('global/information', compact('redirect', 'message')); - } - - $title = $_POST['title'] ?? null; - - if (session_check() && $title !== null) { - $redirect = route('settings.account.title'); - - if (strlen($title) > 64) { - $message = "This title is too long!"; - return view('global/information', compact('redirect', 'message')); - } - - if ($title === CurrentSession::$user->title) { - $message = "This is already your title!"; - return view('global/information', compact('redirect', 'message')); - } - - // Update database - DB::table('users') - ->where('user_id', CurrentSession::$user->id) - ->update([ - 'user_title' => $title, - ]); - - $message = "Changed your title!"; - return view('global/information', compact('redirect', 'message')); - } - - return view('settings/account/title'); - } - - /** - * Renders the password changing page. - * @return string - */ - public function password() - { - // Check permission - if (!CurrentSession::$user->permission(Site::CHANGE_PASSWORD)) { - $message = "You aren't allowed to change your password."; - $redirect = route('settings.index'); - return view('global/information', compact('redirect', 'message')); - } - - $current = $_POST['current'] ?? null; - $password = $_POST['password'] ?? null; - - if (session_check() && $current && $password) { - $redirect = route('settings.account.password'); - - // Check current password - if (!password_verify($current, CurrentSession::$user->password)) { - $message = "Your password was invalid!"; - return view('global/information', compact('redirect', 'message')); - } - - // Check password entropy - if (password_entropy($password) < config('user.pass_min_entropy')) { - $message = "Your password isn't strong enough!"; - return view('global/information', compact('redirect', 'message')); - } - - CurrentSession::$user->setPassword($password); - - $message = "Changed your password!"; - return view('global/information', compact('redirect', 'message')); - } - - return view('settings/account/password'); + return view('settings/account/details', compact( + 'edit_email', + 'edit_usern', + 'edit_title', + 'edit_passw', + 'last_name_change', + 'username_allow' + )); } /** diff --git a/app/Controllers/Settings/Controller.php b/app/Controllers/Settings/Controller.php index c0ba864..5bfd711 100644 --- a/app/Controllers/Settings/Controller.php +++ b/app/Controllers/Settings/Controller.php @@ -9,7 +9,6 @@ namespace Sakura\Controllers\Settings; use Sakura\Controllers\Controller as BaseController; use Sakura\CurrentSession; use Sakura\Perms\Site; -use Sakura\Router; use Sakura\Template; /** @@ -37,59 +36,53 @@ class Controller extends BaseController // Account if (CurrentSession::$user->permission(Site::ALTER_PROFILE)) { - $nav["Account"]["Profile"] = Router::route('settings.account.profile'); + $nav["Account"]["Profile"] = route('settings.account.profile'); } - if (CurrentSession::$user->permission(Site::CHANGE_EMAIL)) { - $nav["Account"]["E-mail address"] = Router::route('settings.account.email'); - } - if (CurrentSession::$user->permission(Site::CHANGE_USERNAME)) { - $nav["Account"]["Username"] = Router::route('settings.account.username'); - } - if (CurrentSession::$user->permission(Site::CHANGE_USERTITLE)) { - $nav["Account"]["Title"] = Router::route('settings.account.title'); - } - if (CurrentSession::$user->permission(Site::CHANGE_PASSWORD)) { - $nav["Account"]["Password"] = Router::route('settings.account.password'); + if (CurrentSession::$user->permission(Site::CHANGE_EMAIL) + || CurrentSession::$user->permission(Site::CHANGE_USERNAME) + || CurrentSession::$user->permission(Site::CHANGE_USERTITLE) + || CurrentSession::$user->permission(Site::CHANGE_PASSWORD)) { + $nav["Account"]["Details"] = route('settings.account.details'); } if (CurrentSession::$user->permission(Site::ALTER_RANKS)) { - $nav["Account"]["Ranks"] = Router::route('settings.account.ranks'); + $nav["Account"]["Ranks"] = route('settings.account.ranks'); } // Friends if (CurrentSession::$user->permission(Site::MANAGE_FRIENDS)) { - $nav["Friends"]["Listing"] = Router::route('settings.friends.listing'); - $nav["Friends"]["Requests"] = Router::route('settings.friends.requests'); + $nav["Friends"]["Listing"] = route('settings.friends.listing'); + $nav["Friends"]["Requests"] = route('settings.friends.requests'); } // Notifications - $nav["Notifications"]["History"] = Router::route('settings.notifications.history'); + $nav["Notifications"]["History"] = route('settings.notifications.history'); // Appearance if (CurrentSession::$user->permission(Site::CHANGE_AVATAR)) { - $nav["Appearance"]["Avatar"] = Router::route('settings.appearance.avatar'); + $nav["Appearance"]["Avatar"] = route('settings.appearance.avatar'); } if (CurrentSession::$user->permission(Site::CHANGE_BACKGROUND)) { - $nav["Appearance"]["Background"] = Router::route('settings.appearance.background'); + $nav["Appearance"]["Background"] = route('settings.appearance.background'); } if (CurrentSession::$user->permission(Site::CHANGE_HEADER)) { - $nav["Appearance"]["Header"] = Router::route('settings.appearance.header'); + $nav["Appearance"]["Header"] = route('settings.appearance.header'); } if (( CurrentSession::$user->page && CurrentSession::$user->permission(Site::CHANGE_USERPAGE) ) || CurrentSession::$user->permission(Site::CREATE_USERPAGE)) { - $nav["Appearance"]["Userpage"] = Router::route('settings.appearance.userpage'); + $nav["Appearance"]["Userpage"] = route('settings.appearance.userpage'); } if (CurrentSession::$user->permission(Site::CHANGE_SIGNATURE)) { - $nav["Appearance"]["Signature"] = Router::route('settings.appearance.signature'); + $nav["Appearance"]["Signature"] = route('settings.appearance.signature'); } // Advanced if (CurrentSession::$user->permission(Site::MANAGE_SESSIONS)) { - $nav["Advanced"]["Sessions"] = Router::route('settings.advanced.sessions'); + $nav["Advanced"]["Sessions"] = route('settings.advanced.sessions'); } if (CurrentSession::$user->permission(Site::DEACTIVATE_ACCOUNT)) { - $nav["Advanced"]["Deactivate"] = Router::route('settings.advanced.deactivate'); + $nav["Advanced"]["Deactivate"] = route('settings.advanced.deactivate'); } return $nav; diff --git a/resources/views/yuuno/settings/account/details.twig b/resources/views/yuuno/settings/account/details.twig new file mode 100644 index 0000000..33b44c3 --- /dev/null +++ b/resources/views/yuuno/settings/account/details.twig @@ -0,0 +1,40 @@ +{% extends 'settings/account/master.twig' %} + +{% set mode = 'Details' %} + +{% block description %} +

Alter the details of your account, leave fields blank to leave them unchanged.

+{% endblock %} + +{% block settingsContent %} +
+ {% if edit_email %} +
+

E-mail address

+
+
+ {% endif %} + {% if edit_usern %} +
+

Username {% if last_name_change %}(last change was ){% endif %}

+
+
+ {% endif %} + {% if edit_title %} +
+

Title

+
+
+ {% endif %} + {% if edit_passw %} +
+

Password

+
+
+ {% endif %} +
+ + +
+
+{% endblock %} diff --git a/resources/views/yuuno/settings/account/email.twig b/resources/views/yuuno/settings/account/email.twig deleted file mode 100644 index bc55dcc..0000000 --- a/resources/views/yuuno/settings/account/email.twig +++ /dev/null @@ -1,21 +0,0 @@ -{% extends 'settings/account/master.twig' %} - -{% set mode = 'E-mail address' %} - -{% block description %} -

You e-mail address is used for password recovery and stuff like that!

-{% endblock %} - -{% block settingsContent %} -
-

Your e-mail address is currently set to {{ user.email }}.

-
-

E-mail address

-
-
-
- - -
-
-{% endblock %} diff --git a/resources/views/yuuno/settings/account/password.twig b/resources/views/yuuno/settings/account/password.twig deleted file mode 100644 index df9fd3c..0000000 --- a/resources/views/yuuno/settings/account/password.twig +++ /dev/null @@ -1,24 +0,0 @@ -{% extends 'settings/account/master.twig' %} - -{% set mode = 'Password' %} - -{% block description %} -

Used to authenticate with the site and certain related services.

-{% endblock %} - -{% block settingsContent %} -
-
-

Current Password

-
-
-
-

New Password

-
-
-
- - -
-
-{% endblock %} diff --git a/resources/views/yuuno/settings/account/title.twig b/resources/views/yuuno/settings/account/title.twig deleted file mode 100644 index 0fde2bc..0000000 --- a/resources/views/yuuno/settings/account/title.twig +++ /dev/null @@ -1,21 +0,0 @@ -{% extends 'settings/account/master.twig' %} - -{% set mode = 'Title' %} - -{% block description %} -

That little piece of text displayed besides your username in most places.

-{% endblock %} - -{% block settingsContent %} -
-

Your current user title is:
{{ user.title }}

-
-

New title

-
-
-
- - -
-
-{% endblock %} diff --git a/resources/views/yuuno/settings/account/username.twig b/resources/views/yuuno/settings/account/username.twig deleted file mode 100644 index 8000883..0000000 --- a/resources/views/yuuno/settings/account/username.twig +++ /dev/null @@ -1,27 +0,0 @@ -{% extends 'settings/account/master.twig' %} - -{% set mode = 'Username' %} - -{% block description %} -

Probably the biggest part of your identity on a site.

-

You can only change this once every 30 days so choose wisely.

-{% endblock %} - -{% set eligible = user.getUsernameHistory ? (date().timestamp - user.getUsernameHistory()[0].change_time) > 2592000 : true %} - -{% block settingsContent %} -
-

You are {% if not eligible %}not {% endif %}eligible for a name change.

-

{% if user.getUsernameHistory %}Your last name change was .{% else %}This is your first username change.{% endif %}

- {% if eligible %} -
-

Username

-
-
-
- - -
- {% endif %} -
-{% endblock %} diff --git a/routes.php b/routes.php index 0d42f69..10731dd 100644 --- a/routes.php +++ b/routes.php @@ -226,14 +226,8 @@ Router::group(['before' => 'maintenance'], function () { Router::get('/profile', 'Settings.AccountController@profile', 'settings.account.profile'); Router::post('/profile', 'Settings.AccountController@profile', 'settings.account.profile'); - Router::get('/email', 'Settings.AccountController@email', 'settings.account.email'); - Router::post('/email', 'Settings.AccountController@email', 'settings.account.email'); - Router::get('/username', 'Settings.AccountController@username', 'settings.account.username'); - Router::post('/username', 'Settings.AccountController@username', 'settings.account.username'); - Router::get('/title', 'Settings.AccountController@title', 'settings.account.title'); - Router::post('/title', 'Settings.AccountController@title', 'settings.account.title'); - Router::get('/password', 'Settings.AccountController@password', 'settings.account.password'); - Router::post('/password', 'Settings.AccountController@password', 'settings.account.password'); + Router::get('/details', 'Settings.AccountController@details', 'settings.account.details'); + Router::post('/details', 'Settings.AccountController@details', 'settings.account.details'); Router::get('/ranks', 'Settings.AccountController@ranks', 'settings.account.ranks'); Router::post('/ranks', 'Settings.AccountController@ranks', 'settings.account.ranks'); });