2025-08-14 13:55:37 -04:00

261 lines
9.5 KiB
PHP

<?php
declare(strict_types=1);
/*
* This file is a part of the Dragon-Knight project.
*
* Copyright (c) 2024-present Sharkk
*
* This file is subject to the MIT license that is bundled
* with this source code in the LICENSE.md file.
*/
namespace DragonKnight\Actions;
use DragonKnight\Lib;
use DragonKnight\Mail;
use DragonKnight\Router;
class Users
{
public static function register_routes(Router $r): Router
{
$r->form('/login', 'Users\login');
$r->get('/logout', 'Users\logout');
$r->form('/register', 'Users\register');
$r->form('/lostpassword', 'Users\lostpassword');
$r->post('/changepassword', 'Users\changepassword');
$r->form('/verify', 'Users\verify');
$r->form('/settings', 'Users\settings');
return $r;
}
/**
* Displays the login page, and processes login requests.
*/
public static function login()
{
global $auth;
if (Lib::is_post()) {
$form = Lib::validate($_POST, [
'username' => ['length:3-18', 'alpha-spaces'],
'password' => ['length:6-255'],
]);
if (! $form['valid']) {
exit(Lib::ul_from_validate_errors($form['errors']));
}
$good = $auth->login($form['data']['username'], $form['data']['password']);
if (! $good) {
exit('Invalid username or password. Please go back and try again.');
}
Lib::redirect('/');
}
Lib::page_title('Login');
return Lib::render('login');
}
/**
* Delete the current cookie and redirect to home.
*/
public static function logout()
{
global $auth;
$auth->logout();
Lib::redirect('/login');
}
/**
* Register a new account.
*/
public static function register()
{
if (isset($_POST['submit'])) {
$form = Lib::validate($_POST, [
'username' => ['length:3-18', 'alpha-spaces', 'unique:users,username'],
'email' => ['email', 'unique:users,email'],
'confirm_email' => ['confirm'],
'password' => ['length:6-255'],
'confirm_password' => ['confirm'],
'charclass' => ['in:1,2,3'],
]);
if (! $form['valid']) {
$err = Lib::ul_from_validate_errors($form['errors']);
$page = "The following error(s) occurred when your account was being made:<br><span style=\"color:red;\">$err</span><br>Please go back and try again.";
} else {
$form = $form['data'];
$password = password_hash($form['password'], PASSWORD_ARGON2ID);
$token = Lib::env('verify_email') ? Lib::token(8) : 'g2g';
Lib::db()->query('INSERT INTO users (verify, username, password, email, charclass) VALUES (?, ?, ?, ?, ?)', [
$token, $form['username'], $password, $form['email'], $form['charclass'],
]);
if (Lib::env('verify_email')) {
if (self::sendregmail($form['email'], $token)) {
$page = 'Your account was created successfully.<br><br>You should receive an Account Verification email shortly. You will need the verification code contained in that email before you are allowed to log in. Once you have received the email, please visit the <a href="users.php?do=verify">Verification Page</a> to enter your code and start playing.';
} else {
$page = 'Your account was created successfully.<br><br>However, there was a problem sending your verification email. Please check with the game administrator to help resolve this problem.';
}
} else {
$page = 'Your account was created succesfully.<br><br>You may now continue to the <a href="/login">Login Page</a> and continue playing '.Lib::env('game_name').'!';
}
}
} else {
if (Lib::env('verify_email')) {
$verify_text = '<br><span class="small">A verification code will be sent to the address above, and you will not be able to log in without first entering the code. Please be sure to enter your correct email address.</span>';
} else {
$verify_text = '';
}
$page = Lib::render('register', ['verify_text' => $verify_text]);
}
Lib::page_title('Register');
return $page;
}
public static function verify()
{
if (isset($_POST['submit'])) {
$u = trim($_POST['username'] ?? '');
$e = trim($_POST['email'] ?? '');
$t = trim($_POST['token'] ?? '');
$query = Lib::db()->query('SELECT id FROM users WHERE username=? AND email=? AND verify=? LIMIT 1;', [$u, $e, $t]);
if ($query === false) {
exit('Verification failed. Go back, double-check your details, and try again.');
}
Lib::db()->query("UPDATE users SET verify='g2g' WHERE username=?;", [$u]);
return 'Your account was verified successfully.<br><br>You may now continue to the <a href="/login">Login Page</a> and start playing the game.<br><br>Thanks for playing!';
}
return Lib::render('verify');
}
public static function lostpassword()
{
if (isset($_POST['submit'])) {
$e = trim($_POST['email'] ?? '');
if (! Lib::db()->exists('users', 'email', $e)) {
exit('No account with that email address.');
}
$newpass = Lib::token(16);
$hashed = password_hash($newpass, PASSWORD_ARGON2ID);
Lib::db()->query('UPDATE users SET password=? WHERE email=?;', [$hashed, $e]);
if (self::sendpassemail($e, $newpass)) {
return 'Your new password was emailed to the address you provided.<br><br>Once you receive it, you may <a href="/login">Log In</a> and continue playing.<br><br>Thank you.';
}
return 'There was an error sending your new password.<br><br>Please check with the game administrator for more information.<br><br>We apologize for the inconvience.';
}
return Lib::render('lostpassword');
}
public static function changepassword()
{
global $auth;
if (isset($_POST['submit'])) {
$u = trim($_POST['username'] ?? '');
$p = $_POST['password'] ?? '';
$np = $_POST['new_password'] ?? '';
$np2 = $_POST['new_password2'] ?? '';
$user = Lib::db()->query('SELECT password FROM users WHERE username=? LIMIT 1;', [$u]);
$user = $user->fetchArray(SQLITE3_ASSOC);
if ($user === false) {
exit('No account with that username.');
}
if (! password_verify($p, $user['password'])) {
exit('The old password you provided was incorrect.');
}
if (empty($np) || strlen($np) < 6) {
$errors[] = 'New password is required and must be at least 6 characters long.';
}
if ($np2 !== $np) {
$errors[] = 'Verify New Password must match.';
}
$realnewpass = password_hash($np, PASSWORD_ARGON2ID);
Lib::db()->query('UPDATE users SET password=? WHERE username=?;', [$realnewpass, $u]);
$auth->logout();
return 'Your password was changed successfully.<br><br>You have been logged out of the game to avoid errors.<br><br>Please <a href="/login">log back in</a> to continue playing.';
}
}
public static function settings()
{
if (Lib::is_post()) {
$form = Lib::validate($_POST, [
'game_skin' => ['in:0,1'],
]);
if (! $form['valid']) {
exit(Lib::ul_from_validate_errors($form['errors']));
}
$form = $form['data'];
Lib::user()->game_skin = $form['game_skin'];
Lib::user()->save();
$alert = '<div class="alert">Settings updated</div>';
return $alert.Lib::render('settings');
}
return Lib::render('settings');
}
public static function sendpassemail($emailaddress, $password)
{
$email = <<<HTML
You or someone using your email address submitted a Lost Password application on the {Lib::env('game_name')} server, located at {Lib::env('game_url')}.
We have issued you a new password so you can log back into the game.
Your new password is: $password
Thanks for playing.
HTML;
return Mail::send_email($emailaddress, Lib::env('game_name').' Lost Password', $email);
}
public static function sendregmail($emailaddress, $vercode)
{
$verurl = Lib::env('game_url').'/verify';
$email = <<<HTML
You or someone using your email address recently signed up for an account on the {Lib::env('game_name')} server, located at {Lib::env('game_url')}.
This email is sent to verify your registration email. In order to begin using your account, you must verify your email address.
Please visit the Verification Page ($verurl) and enter the code below to activate your account.
Verification code: $vercode
If you were not the person who signed up for the game, please disregard this message. You will not be emailed again.
HTML;
return Mail::send_email($emailaddress, Lib::env('game_name').' Account Verification', $email);
}
}