From 82f911cf18c615d23d7a6934c36879e75f2cf46e Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Tue, 26 Nov 2024 11:45:12 -0500 Subject: new style, add redirect support --- src/web/router.php | 52 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) (limited to 'src/web/router.php') diff --git a/src/web/router.php b/src/web/router.php index ce30f8b..536e228 100644 --- a/src/web/router.php +++ b/src/web/router.php @@ -17,12 +17,13 @@ class Router { /** * Displays a page to the user * @param string $file - * @param array $data - */ - private function send_page( + * @param array $data + */ + private function send_page( string $file, array $data = array() ): void { + $data['bg'] = random_int(1, 70); extract($data); $webroot = $GLOBALS['webroot']; require($webroot . '/views/header.php'); @@ -35,7 +36,7 @@ class Router { * @param string $title * @param string $msg * @param int $code - */ + */ private function send_message( string $title, string $msg @@ -50,15 +51,16 @@ class Router { * Gets the HTTP request information */ private function get_req(): array { - $path = $_SERVER['REQUEST_URI']; + $uri = $_SERVER['REQUEST_URI']; + $path = parse_url($uri)['path']; $method = $_SERVER['REQUEST_METHOD']; return [$method, $path]; } - /** - * @param array $fields - */ - private function get_post_info( + /** + * @param array $fields + */ + private function get_post_info( string ...$fields ): ?array { $values = array(); @@ -88,10 +90,18 @@ class Router { return; } + $redirect = $this->get_post_info('redirect') ?? ''; + if (is_array($redirect)) { + $redirect = $redirect['redirect']; + $redirect = base64_decode($redirect); + } + $user = $this->ldap->search($info['username']); if ($user == NULL || !count($user)) { http_response_code(400); - $this->send_message('Error', 'User does not exist'); + $this->send_page('error', array( + 'title' => 'Error', + 'redirect' => $redirect)); return; } @@ -108,9 +118,18 @@ class Router { $session = $this->auth->create_session($user); - http_response_code(200); - $session->write_headers(); - $this->send_message('Success', 'Authenticated. You can now go back to your content'); + if ($redirect == '') { + http_response_code(200); + $session->write_headers(); + $this->send_message('Success', 'Authenticated. You can now go back to your content'); + } else { + if (!str_starts_with($redirect, 'http')) { + $redirect = 'http://' . $redirect; + } + http_response_code(303); + $session->write_headers(); + header("Location: $redirect"); + } } private function handle_logout(): void { @@ -131,7 +150,7 @@ class Router { 'You have been logged out successfully.'); } - private function handle_auth(): void { + private function handle_auth(): void { $session = $this->auth->get_session(); if ($session == NULL) { // redirect them to login @@ -145,14 +164,15 @@ class Router { http_response_code(200); $session->write_headers(); $this->send_message('Authenticated', - 'You are already logged in.

Log Out'); + 'Log Out'); } } private function page_login(): void { http_response_code(200); $this->send_page('login', array( - 'title' => 'Login' + 'title' => 'Login', + 'redirect' => $_GET['redirect'] ?? '' )); } -- cgit v1.2.3-freya