From d47e1c5744923956bff267233757b672e11fe14a Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Thu, 19 Dec 2024 12:53:51 -0600 Subject: [PATCH] Replace all display() calls with new Render\content call, which accounts for HTMX --- public/index.php | 2 +- src/actions/explore.php | 2 +- src/actions/fight.php | 25 +++++--------- src/actions/forum.php | 6 ++-- src/actions/heal.php | 2 +- src/actions/towns.php | 21 +++++------- src/actions/users.php | 30 ++++++++-------- src/bootstrap.php | 3 +- src/lib.php | 64 +++-------------------------------- src/render.php | 22 ++++++------ templates/changepassword.php | 10 ------ templates/layouts/primary.php | 12 +++++-- templates/left_nav.php | 5 ++- templates/settings.php | 33 +++++++++++++----- 14 files changed, 96 insertions(+), 141 deletions(-) delete mode 100644 templates/changepassword.php diff --git a/public/index.php b/public/index.php index ed709ef..7d0f50e 100644 --- a/public/index.php +++ b/public/index.php @@ -68,7 +68,7 @@ function index(): string redirect('/fight'); } - return is_htmx() ? $page : display($page, ''); + return Render\content($page); } /** diff --git a/src/actions/explore.php b/src/actions/explore.php index 4594e65..e41dcbf 100644 --- a/src/actions/explore.php +++ b/src/actions/explore.php @@ -27,7 +27,7 @@ function move() { $form = validate($_POST, ['direction' => ['in:north,west,east,south']]); if (!$form['valid']) { $errors = ul_from_validate_errors($form['errors']); - return is_htmx() ? $errors : display($errors, 'Move Error'); + return \Render\content($errors); } // Current game state diff --git a/src/actions/fight.php b/src/actions/fight.php index c3c2d9f..b5352bf 100644 --- a/src/actions/fight.php +++ b/src/actions/fight.php @@ -120,22 +120,15 @@ function fight() // Spell action if (isset($_POST["spell"])) { $pickedspell = $_POST["userspell"]; - if ($pickedspell == 0) { - return display("You must select a spell first. Please go back and try again.", "Error"); - die(); - } + if ($pickedspell == 0) return \Render\content('You must select a spell first. Please go back and try again.'); $newspellrow = get_spell($pickedspell); $spell = in_array($pickedspell, explode(',', user()->spells)); - if (!$spell) { - return display("You have not yet learned this spell. Please go back and try again.", "Error"); - die(); - } + if (!$spell) return \Render\content('You have not yet learned this spell. Please go back and try again.'); if (user()->currentmp < $newspellrow["mp"]) { - return display("You do not have enough Magic Points to cast this spell. Please go back and try again.", "Error"); - die(); + return \Render\content('You do not have enough Magic Points to cast this spell. Please go back and try again.'); } // Spell type handling (similar to original function) @@ -184,7 +177,7 @@ function fight() // Finalize page and display it $page = render('fight', ['page' => $page]); - return is_htmx() ? $page : display($page, "Fighting"); + return \Render\content($page); } function victory() @@ -258,7 +251,7 @@ function victory() user()->save(); page_title($title); - return is_htmx() ? $page : display($page, $title); + return \Render\content($page); } function drop() @@ -272,7 +265,7 @@ function drop() if ($slot == 0) { $page = 'Please go back and select an inventory slot to continue.'; - return is_htmx() ? $page : display($page, ''); + return \Render\content($page); } $slotstr = 'slot'.$slot.'id'; @@ -329,7 +322,7 @@ function drop() user()->save(); $page = 'The item has been equipped. You can now continue exploring.'; - return is_htmx() ? $page : display($page, "Item Drop"); + return \Render\content($page); } $attributearray = array("maxhp"=>"Max HP", @@ -359,7 +352,7 @@ function drop() $page .= "
"; $page .= "You may also choose to just continue exploring and give up this item."; - return is_htmx() ? $page : display($page, "Item Drop"); + return \Render\content($page); } @@ -371,7 +364,7 @@ function dead() to continue your journey.

You may now continue back to town, and we hope you fair better next time. HTML; - return is_htmx() ? $page : display($page, 'You Died'); + return \Render\content($page); } function handleMonsterTurn(&$userrow, $monsterrow) diff --git a/src/actions/forum.php b/src/actions/forum.php index c175c86..90c106b 100644 --- a/src/actions/forum.php +++ b/src/actions/forum.php @@ -56,7 +56,7 @@ function donothing($start = 0) $page .= ''; page_title('Forum'); - return is_htmx() ? $page : display($page); + return \Render\content($page); } function showthread($id, $start) @@ -72,7 +72,7 @@ function showthread($id, $start) $page .= "
Reply To This Thread:

"; page_title('Forum: '.$title['title']); - return is_htmx() ? $page : display($page); + return \Render\content($page); } function reply() @@ -112,5 +112,5 @@ function newthread() $page = "
Make A New Post:

Title:


Message:


"; page_title('Form: New Thread'); - return is_htmx() ? $page : display($page); + return \Render\content($page); } diff --git a/src/actions/heal.php b/src/actions/heal.php index 90ecf43..d3c8c42 100644 --- a/src/actions/heal.php +++ b/src/actions/heal.php @@ -30,5 +30,5 @@ function healspells(int $id): string } page_title('Casting '.$spell['name']); - return is_htmx() ? $page : display($page, ''); + return \Render\content($page); } diff --git a/src/actions/towns.php b/src/actions/towns.php index ce89093..4c3668a 100644 --- a/src/actions/towns.php +++ b/src/actions/towns.php @@ -79,9 +79,6 @@ function inn() $town = get_town_by_xy(user()->longitude, user()->latitude); if ($town === false) { exit('Cheat attempt detected.

Get a life, loser.'); } - $htmx = is_htmx(); - page_title($town['name'] . ' Inn'); - if (user()->gold < $town['innprice']) { $page = <<
@@ -107,7 +104,8 @@ function inn() HTML; } - return $htmx ? $page : display($page, $town['name'] . ' Inn'); + page_title($town['name'] . ' Inn'); + return \Render\content($page); } /** @@ -120,8 +118,6 @@ function shop() $town = get_town_by_xy(user()->longitude, user()->latitude); if ($town === false) exit('Cheat attempt detected.

Get a life, loser.'); - $htmx = is_htmx(); - page_title($town['name'] . ' Shop'); $page = <<
@@ -161,7 +157,8 @@ function shop() If you've changed your mind, you may also return back to town. HTML; - return $htmx ? $page : display($page, $town['name'] . ' Shop'); + page_title($town['name'] . ' Shop'); + return \Render\content($page); } /** @@ -192,7 +189,7 @@ function buy(int $id) if (!isset($type_mapping[$item["type"]])) { // should never happen $page = 'Error! Invalid item type...
'.var_dump($item); - return is_htmx() ? $page : display($page, ''); + return \Render\content($page, ''); } // Retrieve current equipped item or create a default @@ -276,7 +273,7 @@ function buy(int $id) } page_title('Buying '.$item['name']); - return is_htmx() ? $page : display($page, 'Buying '.$item['name']); + return \Render\content($page); } /** @@ -322,7 +319,7 @@ function maps() HTML; page_title('Maps'); - return is_htmx() ? $page : display($page, ''); + return \Render\content($page); } function buy_map(int $id): string @@ -357,7 +354,7 @@ function buy_map(int $id): string } page_title('Buying '.$town['name'].' Map'); - return is_htmx() ? $page : display($page, ''); + return \Render\content($page); } /** @@ -403,5 +400,5 @@ function travelto(int $id, bool $use_points = true): string } page_title('Travelling to '.$town['name']); - return is_htmx() ? $page : display($page, ''); + return \Render\content($page); } diff --git a/src/actions/users.php b/src/actions/users.php index 552ff8d..c236526 100644 --- a/src/actions/users.php +++ b/src/actions/users.php @@ -10,7 +10,7 @@ function register_routes(Router $r): Router $r->get('/logout', 'Users\logout'); $r->form('/register', 'Users\register'); $r->form('/lostpassword', 'Users\lostpassword'); - $r->form('/changepassword', 'Users\changepassword'); + $r->post('/changepassword', 'Users\changepassword'); $r->form('/verify', 'Users\verify'); $r->form('/settings', 'Users\settings'); return $r; @@ -38,7 +38,8 @@ function login() redirect('/'); } - return display(render('login'), 'Log In', true, false, false); + page_title('Login'); + return \Render\content(render('login')); } /** @@ -99,7 +100,8 @@ function register() $page = render('register', ['controlrow' => $controlrow]); } - return display($page, 'Register', true, false, false); + page_title('Register'); + return \Render\content($page); } function verify() @@ -114,10 +116,10 @@ function verify() db()->query("UPDATE users SET verify='g2g' WHERE username=?;", [$u]); - return display("Your account was verified successfully.

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

Thanks for playing!","Verify Email",false,false,false); + return \Render\content("Your account was verified successfully.

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

Thanks for playing!"); } - return display(render('verify'), "Verify Email", true, false, false); + return \Render\content(render('verify')); } function lostpassword() @@ -133,17 +135,19 @@ function lostpassword() db()->query('UPDATE users SET password=? WHERE email=?;', [$hashed, $e]); if (sendpassemail($e, $newpass)) { - return display("Your new password was emailed to the address you provided.

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

Thank you.","Lost Password",false,false,false); + return \Render\content("Your new password was emailed to the address you provided.

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

Thank you."); } else { - return display("There was an error sending your new password.

Please check with the game administrator for more information.

We apologize for the inconvience.","Lost Password",false,false,false); + return \Render\content("There was an error sending your new password.

Please check with the game administrator for more information.

We apologize for the inconvience."); } } - return display(render('lostpassword'), "Lost Password", true, false, false); + return \Render\content(render('lostpassword')); } function changepassword() { + global $auth; + if (isset($_POST["submit"])) { $u = trim($_POST['username'] ?? ''); $p = $_POST['password'] ?? ''; @@ -167,12 +171,10 @@ function changepassword() $realnewpass = password_hash($np, PASSWORD_ARGON2ID); db()->query('UPDATE users SET password=? WHERE username=?;', [$realnewpass, $u]); - set_cookie('dkgame', '', -3600); + $auth->logout(); - return display("Your password was changed successfully.

You have been logged out of the game to avoid errors.

Please log back in to continue playing.","Change Password",false,false,false); + return \Render\content("Your password was changed successfully.

You have been logged out of the game to avoid errors.

Please log back in to continue playing."); } - - return display(render('changepassword'), "Change Password", true, false, false); } function settings() @@ -188,10 +190,10 @@ function settings() user()->save(); $alert = '
Settings updated
'; - return display($alert . render('settings'), "Account Settings"); + return \Render\content($alert . render('settings')); } - return display(render('settings'), "Account Settings"); + return \Render\content(render('settings')); } function sendpassemail($emailaddress, $password) diff --git a/src/bootstrap.php b/src/bootstrap.php index 90f7b99..a943e5f 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -31,7 +31,8 @@ if (!file_exists('../.installed') && $uri[0] !== 'install') { $controlrow = get_control_row(); if (!$controlrow["gameopen"]) { - display("The game is currently closed for maintanence. Please check back later.", "Game Closed"); + echo Render\content('The game is currently closed for maintanence. Please check back later.'); + exit; } $auth = new Auth; diff --git a/src/lib.php b/src/lib.php index ad5bca4..5eb9fa9 100644 --- a/src/lib.php +++ b/src/lib.php @@ -31,22 +31,13 @@ function redirect(string $location): void } /** - * Return the path to a view file. - */ -function template(string $name): string -{ - return "../templates/$name.php"; -} - -/** - * Render a view with the given data. Looks for `$path_to_base_view` through `template()`. Can be used redundantly - * within the template. + * Render a view with the given data. Can be used redundantly within the template. */ function render(string $path_to_base_view, array $data = []): string|false { ob_start(); extract($data); - require template($path_to_base_view); + require "../templates/$name.php"; return ob_get_clean(); } @@ -102,56 +93,11 @@ function display_admin($content, $title) } /** - * Finalize page and output to browser. + * Determine what game skin to use. If a user is logged in then it uses their setting, otherwise defaults to 0 (retro). */ -function display($content, $title = '', bool $topnav = true, bool $leftnav = true, bool $rightnav = true): string +function game_skin(): int { - global $controlrow; - - $game_skin = user() !== false ? user()->game_skin : 0; - - return render('layouts/primary', [ - "dkgamename" => $controlrow["gamename"], - "content" => $content, - "game_skin" => $game_skin, - "topnav" => $topnav ? Render\header_links() : '' - ]); -} - -function checkcookies() -{ - $row = false; - - if (isset($_COOKIE["dkgame"])) { - // COOKIE FORMAT: - // {ID} {USERNAME} {PASSWORDHASH} {REMEMBERME} - $theuser = explode(" ",$_COOKIE["dkgame"]); - $query = db()->query('SELECT * FROM users WHERE id = ? AND username = ? AND password = ? LIMIT 1;', [$theuser[0], $theuser[1], $theuser[2]]); - if ($query === false) { - set_cookie('dkgame', '', -3600); - die("Invalid cookie data. Please log in again."); - } - $row = $query->fetchArray(SQLITE3_ASSOC); - set_cookie('dkgame', implode(" ", $theuser), (int) $theuser[3] === 1 ? time() + 31536000 : 0); - db()->query('UPDATE users SET onlinetime = CURRENT_TIMESTAMP WHERE id = ?;', [$theuser[0]]); - } - - return $row; -} - -/** - * Set a cookie with secure and HTTP-only flags. - */ -function set_cookie($name, $value, $expires) -{ - setcookie($name, $value, [ - 'expires' => $expires, - 'path' => '/', - 'domain' => '', // Defaults to the current domain - 'secure' => true, // Ensure the cookie is only sent over HTTPS - 'httponly' => true, // Prevent access to cookie via JavaScript - 'samesite' => 'Strict' // Enforce SameSite=Strict - ]); + return user() !== false ? user()->game_skin : 0; } /** diff --git a/src/render.php b/src/render.php index 00cc755..d301af6 100644 --- a/src/render.php +++ b/src/render.php @@ -7,18 +7,20 @@ namespace Render; to HTMX/AJAX for more fluid gameplay. */ -function header_links(): string +/** + * Prepare content for final render. If the request is HTMX-based, will return just the content passed to it. Otherwise + * it will render() onto layouts/primary with some additional bits. + */ +function content(string $content): string { - if (user() !== false) { - $links = "Log Out"; - } else { - $links = <<Log In - Register - HTML; - } + if (is_htmx()) return $content; - return $links .= "Help"; + global $controlrow; + + return render('layouts/primary', [ + "dkgamename" => $controlrow["gamename"], + "content" => $content + ]); } function debug_db_info(): string { diff --git a/templates/changepassword.php b/templates/changepassword.php deleted file mode 100644 index 79aeb30..0000000 --- a/templates/changepassword.php +++ /dev/null @@ -1,10 +0,0 @@ -
- - - - - - - -
Use the form below to change your password. All fields are required. New passwords must be 10 alphanumeric characters or less.
Username:
Old Password:
New Password:
Verify New Password:


-
diff --git a/templates/layouts/primary.php b/templates/layouts/primary.php index adf990a..a6e2649 100644 --- a/templates/layouts/primary.php +++ b/templates/layouts/primary.php @@ -20,11 +20,19 @@ } - +
- +
diff --git a/templates/left_nav.php b/templates/left_nav.php index a6317c1..e3f559e 100644 --- a/templates/left_nav.php +++ b/templates/left_nav.php @@ -50,10 +50,9 @@ Home
Forum
Settings
- Change Password
- Log Out
authlevel === 1): ?> Admin
- Help + Help
+ Log Out diff --git a/templates/settings.php b/templates/settings.php index 0db0f66..6c45718 100644 --- a/templates/settings.php +++ b/templates/settings.php @@ -1,12 +1,29 @@

Account Settings

Here you can change some basic settings for your account.

-
- - +
+

Game Skin

+ + + + + +
+ + +
+

Change Password

+
+ + + + + + +
Use the form below to change your password. All fields are required. New passwords must be 10 alphanumeric characters or less.
Old Password:
New Password:
Verify New Password:


+
+
- -