diff options
author | Freya Murphy <freya@freyacat.org> | 2024-11-26 11:45:12 -0500 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2024-11-26 11:45:12 -0500 |
commit | 82f911cf18c615d23d7a6934c36879e75f2cf46e (patch) | |
tree | 5171b3dbdedf96aee4740eeae03fec23b8d878aa /src/web/router.php | |
parent | add logout, better router (diff) | |
download | ldap_forwardauth-82f911cf18c615d23d7a6934c36879e75f2cf46e.tar.gz ldap_forwardauth-82f911cf18c615d23d7a6934c36879e75f2cf46e.tar.bz2 ldap_forwardauth-82f911cf18c615d23d7a6934c36879e75f2cf46e.zip |
new style, add redirect support
Diffstat (limited to 'src/web/router.php')
-rw-r--r-- | src/web/router.php | 52 |
1 files changed, 36 insertions, 16 deletions
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<string,mixed> $data - */ - private function send_page( + * @param array<string,mixed> $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<string> $fields - */ - private function get_post_info( + /** + * @param array<string> $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.<br><br><a href="logout">Log Out</a>'); + '<a class="btn" href="logout">Log Out</a>'); } } private function page_login(): void { http_response_code(200); $this->send_page('login', array( - 'title' => 'Login' + 'title' => 'Login', + 'redirect' => $_GET['redirect'] ?? '' )); } |