diff --git a/public/index.php b/public/index.php index f3c0e5a..6c64471 100644 --- a/public/index.php +++ b/public/index.php @@ -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.'); } +env_load(__DIR__.'/../.env'); + // Check required extensions $requiredExtensions = [ 'sqlite3', @@ -53,7 +55,8 @@ if ($uri[0] === 'babblebox' && (isset($uri[1]) && $uri[1] === 'messages')) { $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->get('/spell/:id', fn($id) => \DragonKnight\Actions\Heal::healspells($id)); $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->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); +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! 🥷 @@ -81,6 +84,11 @@ $r->get('/ninja', function () { $l = $r->lookup($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']); 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'] ?? [])); diff --git a/public/install.php b/public/install.php index 938e0b1..04163ca 100644 --- a/public/install.php +++ b/public/install.php @@ -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') { throw new \Exception('Composer autoloader not found. Run `composer install` and try again.'); @@ -57,6 +57,6 @@ Install::fourth([ 'confirm_email' => 'admin@example.com', 'password' => 'password', 'confirm_password' => 'password', - 'charclass' => 'Warrior', // Mage, Warrior, Paladin + 'charclass' => '1', // [1 => 'Mage', 2 => 'Warrior', 3 => 'Paladin'] ]); Install::fifth(); diff --git a/src/DragonKnight/Actions/Users.php b/src/DragonKnight/Actions/Users.php index 0df8ca5..e296e08 100644 --- a/src/DragonKnight/Actions/Users.php +++ b/src/DragonKnight/Actions/Users.php @@ -87,6 +87,7 @@ class Users */ public static function register(): string|false { + $page = false; if (isset($_POST['submit'])) { $form = validate($_POST, [ 'username' => ['length:3-18', 'alpha-spaces', 'unique:users,username'], @@ -103,12 +104,12 @@ class Users } else { $form = $form['data']; $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 (?, ?, ?, ?, ?)', [ $token, $form['username'], $password, $form['email'], $form['charclass'], ]); - if (env('verify_email')) { + if ((env('verify_email') ?? false)) { 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 { @@ -119,7 +120,7 @@ class Users } } } else { - if (env('verify_email')) { + if ((env('verify_email') ?? false)) { $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 = ''; diff --git a/src/DragonKnight/Bootstrap.php b/src/DragonKnight/Bootstrap.php index 057f264..756e3aa 100644 --- a/src/DragonKnight/Bootstrap.php +++ b/src/DragonKnight/Bootstrap.php @@ -13,7 +13,7 @@ declare(strict_types=1); namespace DragonKnight; -env_load(getcwd().'/.env'); +env_load(__DIR__.'/../../.env'); $uri = explode('/', trim($_SERVER['REQUEST_URI'], '/')); $GLOBALS['cache'] = []; @@ -26,15 +26,15 @@ define('ADMIN_BULK_DATA_STRUCTS', [ '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'); 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('/'); return; } -if (! file_exists((getcwd().'/.installed')) || $uri[0] === 'install') { +if (! file_exists((__DIR__.'/../../.installed')) || $uri[0] === 'install') { return; } @@ -60,7 +60,7 @@ if ($auth->good()) { } // 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'); } diff --git a/src/DragonKnight/Router.php b/src/DragonKnight/Router.php index 561526b..2ba8d65 100644 --- a/src/DragonKnight/Router.php +++ b/src/DragonKnight/Router.php @@ -116,9 +116,11 @@ class Router /** * 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); } /** diff --git a/src/DragonKnight/functions.php b/src/DragonKnight/functions.php index 881fdfa..4218ff8 100644 --- a/src/DragonKnight/functions.php +++ b/src/DragonKnight/functions.php @@ -17,13 +17,13 @@ use DragonKnight\Actions\Explore; use DragonKnight\Actions\Towns; use DragonKnight\Models\User; -function index(): string|false +function index(): string { $page = false; if (! $user = user()) { error_log('User not found in index() function.'); - return $page; + return $page ?: ''; } if ($user->currentaction === 'In Town') { @@ -34,7 +34,7 @@ function index(): string|false redirect('/fight'); } - return $page; + return $page ?: ''; } /**