diff --git a/server/app/app.php b/server/app/App.php similarity index 82% rename from server/app/app.php rename to server/app/App.php index 9b1d8f0..4561269 100644 --- a/server/app/app.php +++ b/server/app/App.php @@ -9,6 +9,7 @@ class App public static Database $db; private static string $dbPath; public static Request $req; + public static Auth $auth; public static array $s = []; // game settings public function __construct(string $dbPath) @@ -20,6 +21,9 @@ class App // load game settings $s = self::$db->q('SELECT * FROM settings WHERE id = 1;'); self::$s = $s ? $s->fetch() : []; + + // init authentication + self::$auth = new Auth(); } public static function performDatabaseReset(): void @@ -29,4 +33,9 @@ class App self::$db = new Database(self::$dbPath); } } + + public static function auth(): bool + { + return self::$auth->good(); + } } diff --git a/server/app/auth.php b/server/app/Auth.php similarity index 78% rename from server/app/auth.php rename to server/app/Auth.php index 2f7c2c4..2b9fb31 100644 --- a/server/app/auth.php +++ b/server/app/Auth.php @@ -13,10 +13,15 @@ class Auth // id of the player public static int $id = 0; - public static function login(string $identifier, string $password, bool $remember = false): bool + public function __construct() + { + $this->good(); + } + + public function login(string $identifier, string $password, bool $remember = false): bool { // delete the old session - if (isset($_SESSION['player_id'])) self::logout(); + if (isset($_SESSION['player_id'])) $this->logout(); // get the player by their username $id = Player::validateCredentials($identifier, $password); @@ -27,12 +32,12 @@ class Auth self::$id = $id; // set the remember me cookie - if ($remember) self::remember($id); + if ($remember) $this->remember($id); return true; } - private static function remember(int $id): array|false + private function remember(int $id): array|false { $data = ['player_id' => $id, 'token' => token()]; @@ -42,14 +47,13 @@ class Auth return $data; } - private static function logout(): void + private function logout(): void { if (isset($_SESSION['player_id'])) unset($_SESSION['player_id']); - if (isset($_SESSION['remember'])) unset($_SESSION['remember']); if (isset($_COOKIE[self::COOKIE_NAME])) setcookie(self::COOKIE_NAME, '', time() - 86400, '/', '', true, true); } - public static function good(): bool + public function good(): bool { // if our player_id session still exists, carry on if (isset($_SESSION['player_id'])) { @@ -65,7 +69,7 @@ class Auth if (!Session::validate($cookie[0], $cookie[1])) return false; // the token is invalid // token is valid, refresh cookie and assign session - self::remember($cookie[0]); + $this->remember($cookie[0]); $_SESSION['player_id'] = $cookie[0]; self::$id = $cookie[0]; return true; diff --git a/server/app/database.php b/server/app/Database.php similarity index 100% rename from server/app/database.php rename to server/app/Database.php diff --git a/server/app/request.php b/server/app/Request.php similarity index 100% rename from server/app/request.php rename to server/app/Request.php diff --git a/server/bootstrap.php b/server/bootstrap.php index 5470e2f..89160c0 100644 --- a/server/bootstrap.php +++ b/server/bootstrap.php @@ -39,18 +39,21 @@ const MAP = [ // 'Class' => 'path/to/class.php', // server-level classes - 'App' => SERVER.'/app/app.php', - 'Database' => SERVER.'/app/database.php', - 'Request' => SERVER.'/app/request.php', + 'App' => SERVER.'/app/App.php', + 'Database' => SERVER.'/app/Database.php', + 'Request' => SERVER.'/app/Request.php', + 'Auth' => SERVER.'/app/Auth.php', // modules 'HomeModule' => SERVER.'/modules/HomeModule.php', 'InstallModule' => SERVER.'/modules/InstallModule.php', + 'GateModule' => SERVER.'/modules/GateModule.php', // models 'Classes' => SERVER.'/models/Classes.php', 'Player' => SERVER.'/models/Player.php', 'Spell' => SERVER.'/models/Spell.php', + 'Session' => SERVER.'/models/Session.php', ]; // autoloader diff --git a/server/models/Player.php b/server/models/Player.php index d82a7dc..00bc693 100644 --- a/server/models/Player.php +++ b/server/models/Player.php @@ -40,7 +40,7 @@ class Player public static function validateCredentials(string $identifier, string $password, bool $fetch = false): int|false { // get the player from their username or email - $player = App::$db->do("SELECT " . $fetch ? '*' : 'id, password' . " FROM players WHERE username = :i OR email = :i LIMIT 1;", ['i' => $identifier]); + $player = App::$db->do("SELECT " . ($fetch ? '*' : 'id, password') . " FROM players WHERE username = :i OR email = :i LIMIT 1;", ['i' => $identifier]); if ($player == false) return false; $player = $player->fetch(); diff --git a/server/modules/GateModule.php b/server/modules/GateModule.php index e69de29..1f52518 100644 --- a/server/modules/GateModule.php +++ b/server/modules/GateModule.php @@ -0,0 +1,6 @@ +'; + } else { + echo 'You are not logged in!
'; + } + echo 'Your request is: ' . App::$req->uri(0); } } diff --git a/server/modules/InstallModule.php b/server/modules/InstallModule.php index b67c94c..cefc06e 100644 --- a/server/modules/InstallModule.php +++ b/server/modules/InstallModule.php @@ -294,11 +294,15 @@ class InstallModule 'level' => $_POST['level'] ?? 1 ]); + // Create the .installed file in the server folder + file_put_contents(SERVER.'/.installed', 'Installed on '.date('Y-m-d H:i:s')); + + // login the admin + App::$auth->login($_POST['username'], $_POST['password']); + // Render the finished page! echo render('install/layout', ['title' => 'Finished!', 'step' => 'done', 'name' => $_POST['username'], 'complete' => $_POST['complete'] ?? false]); - // Create the .installed file in the server folder - file_put_contents(SERVER.'/.installed', 'Installed on '.date('Y-m-d H:i:s')); } private static function fourOhFour() diff --git a/server/templates/install/done.php b/server/templates/install/done.php index 94bf1e1..fc258de 100644 --- a/server/templates/install/done.php +++ b/server/templates/install/done.php @@ -1,12 +1,12 @@

Congratulations, ! Your installation is complete. Dragon Knight is ready to go. - All that's left is to log in and start playing. Once you've logged in, + All that's left is to start playing. Once you've logged in, you can create some classes and assign your character one. By default you are a useless Adventurer. 😜

- Click here to log in. + Click here to begin your adventure.