From 9d28a9b38080f5889d02026afc9532ace034cd9f Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Thu, 19 Dec 2024 08:46:47 -0600 Subject: [PATCH] Update online time, auth logic --- src/bootstrap.php | 4 +++- src/lib.php | 3 ++- src/models/user.php | 8 ++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/bootstrap.php b/src/bootstrap.php index b9ae927..eace2ba 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -41,7 +41,7 @@ if (!file_exists('../.installed') && $uri[0] !== 'install') { if (!in_array($uri[0], ['login', 'register', 'verify', 'lostpassword', 'help'])) { redirect('/login'); } - } else { + } elseif($auth->good()) { // Block user if he/she has been banned. if (user()->authlevel === 2) { exit("Your account has been banned."); @@ -56,5 +56,7 @@ if (!file_exists('../.installed') && $uri[0] !== 'install') { if (user()->authlevel !== 1 && $uri[0] === 'admin') { redirect('/'); } + + user()->update_online_time(); } } diff --git a/src/lib.php b/src/lib.php index a7e1b13..0e52ed6 100644 --- a/src/lib.php +++ b/src/lib.php @@ -20,7 +20,8 @@ function db(): Database function redirect(string $location): void { if (is_htmx()) { - $json = json_encode(['path' => $location, 'target' => '#'.$_SERVER['HTTP_HX_TARGET'] ?? '#middle']); + $target = isset($_SERVER['HTTP_HX_TARGET']) ? '#'.$_SERVER['HTTP_HX_TARGET'] : '#middle'; + $json = json_encode(['path' => $location, 'target' => $target]); header("HX-Location: $json"); } else { header("Location: $location"); diff --git a/src/models/user.php b/src/models/user.php index 1887588..241d169 100644 --- a/src/models/user.php +++ b/src/models/user.php @@ -38,6 +38,14 @@ class User extends Model return $this; } + /** + * Sends a manual update to online time for this user. + */ + public function update_online_time(): void + { + db()->query('UPDATE users SET onlinetime=CURRENT_TIMESTAMP WHERE id=?;', [$this->id]); + } + /** * Save works just as it does on the Model class. In our case, though, user state changing may necessitate * OOB swaps for parts of the UI that have user data displayed. Left and right nav, for example. In these cases,