Update routing and error handlers

This commit is contained in:
Valithor Obsidion 2025-08-15 09:34:27 -04:00
parent c615a978e4
commit c06a9bbed6
6 changed files with 34 additions and 23 deletions

View File

@ -31,6 +31,8 @@ if (! $autoloader = require file_exists(__DIR__.'/../vendor/autoload.php') ? __D
throw new \Exception('Composer autoloader not found. Run `composer install` and try again.'); throw new \Exception('Composer autoloader not found. Run `composer install` and try again.');
} }
env_load(__DIR__.'/../.env');
// Check required extensions // Check required extensions
$requiredExtensions = [ $requiredExtensions = [
'sqlite3', 'sqlite3',
@ -53,7 +55,8 @@ if ($uri[0] === 'babblebox' && (isset($uri[1]) && $uri[1] === 'messages')) {
$r = new Router; $r = new Router;
$r->get('/', fn() => \DragonKnight\index()); $r->get(['/', '/index.php'], fn() => \DragonKnight\index());
$r->get('/register', fn() => \DragonKnight\Actions\Users::register());
$r->post('/move', fn() => \DragonKnight\Actions\Explore::move()); $r->post('/move', fn() => \DragonKnight\Actions\Explore::move());
$r->get('/spell/:id', fn($id) => \DragonKnight\Actions\Heal::healspells($id)); $r->get('/spell/:id', fn($id) => \DragonKnight\Actions\Heal::healspells($id));
$r->get('/character', fn() => \DragonKnight\show_character_info()); $r->get('/character', fn() => \DragonKnight\show_character_info());
@ -62,13 +65,13 @@ $r->get('/showmap', fn() => \DragonKnight\show_map());
$r->form('/babblebox', fn() => \DragonKnight\babblebox()); $r->form('/babblebox', fn() => \DragonKnight\babblebox());
$r->get('/babblebox/messages', fn() => \DragonKnight\babblebox_messages()); $r->get('/babblebox/messages', fn() => \DragonKnight\babblebox_messages());
Towns::register_routes($r);
Fight::register_routes($r);
Users::register_routes($r);
Help::register_routes($r);
Forum::register_routes($r);
Install::register_routes($r);
Admin::register_routes($r); Admin::register_routes($r);
Fight::register_routes($r);
Forum::register_routes($r);
Help::register_routes($r);
Install::register_routes($r);
Towns::register_routes($r);
Users::register_routes($r);
/* /*
NINJA! 🥷 NINJA! 🥷
@ -81,6 +84,11 @@ $r->get('/ninja', function () {
$l = $r->lookup($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']); $l = $r->lookup($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);
if (is_int($l)) { if (is_int($l)) {
exit("Error: $l"); error_log($path = $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI']);
exit("Error: $l at `$path`");
}
if (! isset($l['handler'])) {
error_log($path = $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI']);
exit("Error: Missing handler: $path");
} }
echo render_response($uri, $l['handler'](...$l['params'] ?? [])); echo render_response($uri, $l['handler'](...$l['params'] ?? []));

View File

@ -40,7 +40,7 @@ function loadEnv(string $filePath = __DIR__.'/../.env'): void
} }
}); });
} }
loadEnv(getcwd().'/../.env'); loadEnv(__DIR__.'/../../.env');
if (! $autoloader = require file_exists(__DIR__.'/../vendor/autoload.php') ? __DIR__.'/../vendor/autoload.php' : __DIR__.'/../../autoload.php') { if (! $autoloader = require file_exists(__DIR__.'/../vendor/autoload.php') ? __DIR__.'/../vendor/autoload.php' : __DIR__.'/../../autoload.php') {
throw new \Exception('Composer autoloader not found. Run `composer install` and try again.'); throw new \Exception('Composer autoloader not found. Run `composer install` and try again.');
@ -57,6 +57,6 @@ Install::fourth([
'confirm_email' => 'admin@example.com', 'confirm_email' => 'admin@example.com',
'password' => 'password', 'password' => 'password',
'confirm_password' => 'password', 'confirm_password' => 'password',
'charclass' => 'Warrior', // Mage, Warrior, Paladin 'charclass' => '1', // [1 => 'Mage', 2 => 'Warrior', 3 => 'Paladin']
]); ]);
Install::fifth(); Install::fifth();

View File

@ -87,6 +87,7 @@ class Users
*/ */
public static function register(): string|false public static function register(): string|false
{ {
$page = false;
if (isset($_POST['submit'])) { if (isset($_POST['submit'])) {
$form = validate($_POST, [ $form = validate($_POST, [
'username' => ['length:3-18', 'alpha-spaces', 'unique:users,username'], 'username' => ['length:3-18', 'alpha-spaces', 'unique:users,username'],
@ -103,12 +104,12 @@ class Users
} else { } else {
$form = $form['data']; $form = $form['data'];
$password = password_hash($form['password'], PASSWORD_ARGON2ID); $password = password_hash($form['password'], PASSWORD_ARGON2ID);
$token = env('verify_email') ? token(8) : 'g2g'; $token = (env('verify_email') ?? false) ? token(8) : 'g2g';
db()->query('INSERT INTO users (verify, username, password, email, charclass) VALUES (?, ?, ?, ?, ?)', [ db()->query('INSERT INTO users (verify, username, password, email, charclass) VALUES (?, ?, ?, ?, ?)', [
$token, $form['username'], $password, $form['email'], $form['charclass'], $token, $form['username'], $password, $form['email'], $form['charclass'],
]); ]);
if (env('verify_email')) { if ((env('verify_email') ?? false)) {
if (self::sendregmail($form['email'], $token)) { 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.'; $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 { } else {
@ -119,7 +120,7 @@ class Users
} }
} }
} else { } else {
if (env('verify_email')) { if ((env('verify_email') ?? false)) {
$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>'; $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 { } else {
$verify_text = ''; $verify_text = '';

View File

@ -13,7 +13,7 @@ declare(strict_types=1);
namespace DragonKnight; namespace DragonKnight;
env_load(getcwd().'/.env'); env_load(__DIR__.'/../../.env');
$uri = explode('/', trim($_SERVER['REQUEST_URI'], '/')); $uri = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
$GLOBALS['cache'] = []; $GLOBALS['cache'] = [];
@ -26,15 +26,15 @@ define('ADMIN_BULK_DATA_STRUCTS', [
'users', 'items', 'drops', 'towns', 'monsters', 'levels', 'spells', 'users', 'items', 'drops', 'towns', 'monsters', 'levels', 'spells',
]); ]);
if (! file_exists((getcwd().'/.installed')) && $uri[0] !== 'install') { // need to install the game if (! file_exists((__DIR__.'/../../.installed')) && $uri[0] !== 'install') { // need to install the game
redirect('/install'); redirect('/install');
return; return;
} }
if (file_exists((getcwd().'/.installed')) && $uri[0] === 'install') { // game is installed, go play! if (file_exists((__DIR__.'/../../.installed')) && $uri[0] === 'install') { // game is installed, go play!
redirect('/'); redirect('/');
return; return;
} }
if (! file_exists((getcwd().'/.installed')) || $uri[0] === 'install') { if (! file_exists((__DIR__.'/../../.installed')) || $uri[0] === 'install') {
return; return;
} }
@ -60,7 +60,7 @@ if ($auth->good()) {
} }
// Force verify if the user isn't verified yet. // Force verify if the user isn't verified yet.
if (env('verify_email') && user()->verify !== 'g2g' && ! in_array($uri[0], ['verify', 'logout'])) { if ((env('verify_email') ?? false) && user()->verify !== 'g2g' && ! in_array($uri[0], ['verify', 'logout'])) {
redirect('/verify'); redirect('/verify');
} }

View File

@ -116,9 +116,11 @@ class Router
/** /**
* Shorthand to register a GET route. * Shorthand to register a GET route.
*/ */
public function get(string $route, callable $handler): self public function get(array|string $route, callable $handler): self
{ {
return $this->add('GET', $route, $handler); return is_array($route)
? array_reduce($route, fn($r, $route) => $r->add('GET', $route, $handler), $this)
: $this->add('GET', $route, $handler);
} }
/** /**

View File

@ -17,13 +17,13 @@ use DragonKnight\Actions\Explore;
use DragonKnight\Actions\Towns; use DragonKnight\Actions\Towns;
use DragonKnight\Models\User; use DragonKnight\Models\User;
function index(): string|false function index(): string
{ {
$page = false; $page = false;
if (! $user = user()) { if (! $user = user()) {
error_log('User not found in index() function.'); error_log('User not found in index() function.');
return $page; return $page ?: '';
} }
if ($user->currentaction === 'In Town') { if ($user->currentaction === 'In Town') {
@ -34,7 +34,7 @@ function index(): string|false
redirect('/fight'); redirect('/fight');
} }
return $page; return $page ?: '';
} }
/** /**