From d40b826dfada96179502f46405ac1521a30370db Mon Sep 17 00:00:00 2001 From: Valithor Obsidion Date: Thu, 14 Aug 2025 16:49:20 -0400 Subject: [PATCH] Fix imports, throw exceptions --- src/DragonKnight/Actions/Help.php | 9 +++- src/DragonKnight/Actions/Users.php | 86 +++++++++++++++++------------- src/DragonKnight/DragonKnight.php | 7 +++ src/DragonKnight/Mail.php | 14 ++--- src/DragonKnight/functions.php | 36 +++++++------ templates/layouts/admin.php | 2 +- templates/layouts/primary.php | 2 +- 7 files changed, 91 insertions(+), 65 deletions(-) diff --git a/src/DragonKnight/Actions/Help.php b/src/DragonKnight/Actions/Help.php index b8cbcf8..b17d1f0 100644 --- a/src/DragonKnight/Actions/Help.php +++ b/src/DragonKnight/Actions/Help.php @@ -13,8 +13,13 @@ declare(strict_types=1); namespace DragonKnight\Actions; +use DragonKnight\DragonKnight; use DragonKnight\Router; +use function DragonKnight\db; +use function DragonKnight\render; +use function DragonKnight\special_to_string; + class Help { public static function register_routes(Router $r): Router @@ -509,8 +514,8 @@ class Help { return render('layouts/help', [ 'content' => $content, - 'version' => VERSION, - 'build' => BUILD, + 'version' => DragonKnight::VERSION, + 'build' => DragonKnight::BUILD, ]); } } diff --git a/src/DragonKnight/Actions/Users.php b/src/DragonKnight/Actions/Users.php index 52b5aa1..deb35e4 100644 --- a/src/DragonKnight/Actions/Users.php +++ b/src/DragonKnight/Actions/Users.php @@ -13,10 +13,20 @@ declare(strict_types=1); namespace DragonKnight\Actions; -use DragonKnight\Lib; use DragonKnight\Mail; use DragonKnight\Router; +use function DragonKnight\db; +use function DragonKnight\env; +use function DragonKnight\is_post; +use function DragonKnight\page_title; +use function DragonKnight\redirect; +use function DragonKnight\render; +use function DragonKnight\token; +use function DragonKnight\ul_from_validate_errors; +use function DragonKnight\user; +use function DragonKnight\validate; + class Users { public static function register_routes(Router $r): Router @@ -39,14 +49,14 @@ class Users { global $auth; - if (Lib::is_post()) { - $form = Lib::validate($_POST, [ + if (is_post()) { + $form = validate($_POST, [ 'username' => ['length:3-18', 'alpha-spaces'], 'password' => ['length:6-255'], ]); if (! $form['valid']) { - exit(Lib::ul_from_validate_errors($form['errors'])); + exit(ul_from_validate_errors($form['errors'])); } $good = $auth->login($form['data']['username'], $form['data']['password']); @@ -54,12 +64,12 @@ class Users exit('Invalid username or password. Please go back and try again.'); } - Lib::redirect('/'); + redirect('/'); } - Lib::page_title('Login'); + page_title('Login'); - return Lib::render('login'); + return render('login'); } /** @@ -69,7 +79,7 @@ class Users { global $auth; $auth->logout(); - Lib::redirect('/login'); + redirect('/login'); } /** @@ -78,7 +88,7 @@ class Users public static function register() { if (isset($_POST['submit'])) { - $form = Lib::validate($_POST, [ + $form = validate($_POST, [ 'username' => ['length:3-18', 'alpha-spaces', 'unique:users,username'], 'email' => ['email', 'unique:users,email'], 'confirm_email' => ['confirm'], @@ -88,37 +98,37 @@ class Users ]); if (! $form['valid']) { - $err = Lib::ul_from_validate_errors($form['errors']); + $err = ul_from_validate_errors($form['errors']); $page = "The following error(s) occurred when your account was being made:
$err
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 = env('verify_email') ? token(8) : 'g2g'; + 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 (env('verify_email')) { if (self::sendregmail($form['email'], $token)) { $page = 'Your account was created successfully.

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 Verification Page to enter your code and start playing.'; } else { $page = 'Your account was created successfully.

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.

You may now continue to the Login Page and continue playing '.Lib::env('game_name').'!'; + $page = 'Your account was created succesfully.

You may now continue to the Login Page and continue playing '.env('game_name').'!'; } } } else { - if (Lib::env('verify_email')) { + if (env('verify_email')) { $verify_text = '
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.'; } else { $verify_text = ''; } - $page = Lib::render('register', ['verify_text' => $verify_text]); + $page = render('register', ['verify_text' => $verify_text]); } - Lib::page_title('Register'); + page_title('Register'); return $page; } @@ -130,17 +140,17 @@ class Users $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]); + $query = 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]); + db()->query("UPDATE users SET verify='g2g' WHERE username=?;", [$u]); return 'Your account was verified successfully.

You may now continue to the Login Page and start playing the game.

Thanks for playing!'; } - return Lib::render('verify'); + return render('verify'); } public static function lostpassword() @@ -148,14 +158,14 @@ class Users if (isset($_POST['submit'])) { $e = trim($_POST['email'] ?? ''); - if (! Lib::db()->exists('users', 'email', $e)) { + if (! db()->exists('users', 'email', $e)) { exit('No account with that email address.'); } - $newpass = Lib::token(16); + $newpass = token(16); $hashed = password_hash($newpass, PASSWORD_ARGON2ID); - Lib::db()->query('UPDATE users SET password=? WHERE email=?;', [$hashed, $e]); + 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.

Once you receive it, you may Log In and continue playing.

Thank you.'; @@ -164,7 +174,7 @@ class Users return 'There was an error sending your new password.

Please check with the game administrator for more information.

We apologize for the inconvience.'; } - return Lib::render('lostpassword'); + return render('lostpassword'); } public static function changepassword() @@ -177,7 +187,7 @@ class Users $np = $_POST['new_password'] ?? ''; $np2 = $_POST['new_password2'] ?? ''; - $user = Lib::db()->query('SELECT password FROM users WHERE username=? LIMIT 1;', [$u]); + $user = 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.'); @@ -196,7 +206,7 @@ class Users } $realnewpass = password_hash($np, PASSWORD_ARGON2ID); - Lib::db()->query('UPDATE users SET password=? WHERE username=?;', [$realnewpass, $u]); + db()->query('UPDATE users SET password=? WHERE username=?;', [$realnewpass, $u]); $auth->logout(); @@ -206,30 +216,30 @@ class Users public static function settings() { - if (Lib::is_post()) { - $form = Lib::validate($_POST, [ + if (is_post()) { + $form = validate($_POST, [ 'game_skin' => ['in:0,1'], ]); if (! $form['valid']) { - exit(Lib::ul_from_validate_errors($form['errors'])); + exit(ul_from_validate_errors($form['errors'])); } $form = $form['data']; - Lib::user()->game_skin = $form['game_skin']; - Lib::user()->save(); + user()->game_skin = $form['game_skin']; + user()->save(); $alert = '
Settings updated
'; - return $alert.Lib::render('settings'); + return $alert.render('settings'); } - return Lib::render('settings'); + return render('settings'); } public static function sendpassemail($emailaddress, $password) { $email = << Lib::env('admin_email', 'noreply@'.$_SERVER['SERVER_NAME']), + 'from' => env('admin_email', 'noreply@'.$_SERVER['SERVER_NAME']), 'log_path' => getcwd().'/logs/email.log', 'method' => 'smtp', // 'smtp' or 'log' - 'smtp_host' => Lib::env('smtp_host', 'localhost'), - 'smtp_port' => Lib::env('smtp_port', 25), - 'smtp_username' => Lib::env('smtp_username', null), - 'smtp_password' => Lib::env('smtp_password', null), - 'smtp_encryption' => Lib::env('smtp_encryption', null), + 'smtp_host' => env('smtp_host', 'localhost'), + 'smtp_port' => env('smtp_port', 25), + 'smtp_username' => env('smtp_username', null), + 'smtp_password' => env('smtp_password', null), + 'smtp_encryption' => env('smtp_encryption', null), ], $options); // Always send to log during debug - if (Lib::env('debug', false)) { + if (env('debug', false)) { $config['method'] = 'log'; } diff --git a/src/DragonKnight/functions.php b/src/DragonKnight/functions.php index 52242f0..375a64c 100644 --- a/src/DragonKnight/functions.php +++ b/src/DragonKnight/functions.php @@ -35,7 +35,7 @@ function index(): string|false /** * Show the user their position on the current world map. Only works with a game size of 250 and the default towns 😅. */ -function show_map() +function show_map(): void { $pos = sprintf( '
', @@ -56,7 +56,7 @@ function show_character_info(int $id = 0): string { $user = $id !== 0 ? User::find($id) : user(); if ($user === false) { - exit('Failed to show info for user ID '.$id); + throw new \RuntimeException('Failed to show info for user ID '.$id); } $level = db()->query("SELECT `{$user->charclass}_exp` FROM levels WHERE id=? LIMIT 1;", [$user->level + 1])->fetchArray(SQLITE3_ASSOC); @@ -82,7 +82,7 @@ function show_character_info(int $id = 0): string /** * Handle a POST request to send a new babblebox message. */ -function babblebox() +function babblebox(): ?string { if (is_post()) { $content = trim($_POST['babble']); @@ -95,6 +95,8 @@ function babblebox() return babblebox_messages(); } + + return null; } /** @@ -127,7 +129,7 @@ function db(): Database { if (! is_dir($path = getcwd().'/db')) { error_log('Database folder not found at '.$path.'. Please run the installer first.'); - exit(); + throw new \RuntimeException('Database folder not found. Please run the installer first.'); } return $GLOBALS['database'] ??= new Database(getcwd().'/db/database.db'); @@ -213,7 +215,9 @@ function display_admin($content, $title) */ function game_skin(): int { - return user() !== false ? user()->game_skin : 0; + return user() !== false + ? user()->game_skin + : 0; } /** @@ -573,7 +577,7 @@ function env_load(string $filePath): void /** * Retrieve an environment variable. */ -function env(string $key, mixed $default = null): mixed +function env(string $key, mixed $default = null) { $v = $_ENV[$key] ?? $_SERVER[$key] ?? (getenv($key) ?: $default); @@ -604,7 +608,9 @@ function get_spells_from_list(array|string $spell_ids): array|false $rows[] = $row; } - return ! empty($rows) ? $rows : false; + return ! empty($rows) + ? $rows + : false; } function generate_stat_bar(int $current, int $max): string @@ -627,15 +633,13 @@ function generate_stat_bar(int $current, int $max): string function create_stat_table(): string { - $stat_table = '
'. - '
'. - '
'.generate_stat_bar((int) user()->currenthp, (int) user()->maxhp).'
HP
'. - '
'.generate_stat_bar((int) user()->currentmp, (int) user()->maxmp).'
MP
'. - '
'.generate_stat_bar((int) user()->currenttp, (int) user()->maxtp).'
TP
'. - '
'. - '
'; - - return $stat_table; + return '
'. + '
'. + '
'.generate_stat_bar((int) user()->currenthp, (int) user()->maxhp).'
HP
'. + '
'.generate_stat_bar((int) user()->currentmp, (int) user()->maxmp).'
MP
'. + '
'.generate_stat_bar((int) user()->currenttp, (int) user()->maxtp).'
TP
'. + '
'. + '
'; } /** diff --git a/templates/layouts/admin.php b/templates/layouts/admin.php index f0480b7..8549c05 100644 --- a/templates/layouts/admin.php +++ b/templates/layouts/admin.php @@ -41,7 +41,7 @@
Powered by Dragon Knight
© 2024 Sharkk
-
Version
+
Version
Powered by Dragon Knight
© 2024 Sharkk
-
Version
+
Version