forked from operasoftware/ssh-key-authority
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequesthandler.php
More file actions
119 lines (106 loc) · 3.78 KB
/
requesthandler.php
File metadata and controls
119 lines (106 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
##
## Copyright 2013-2017 Opera Software AS
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
chdir(dirname(__FILE__));
require('core.php');
ob_start();
set_exception_handler('exception_handler');
// Helper function to check if a route is public
function isPublicRoute($request_path) {
global $public_routes;
foreach ($public_routes as $route => $is_public) {
if ($is_public) {
// Convert route pattern to regex for matching
$pattern = preg_replace('/\{[^}]+\}/', '[^/]+', $route);
if (preg_match('|^' . $pattern . '$|', $request_path)) {
return true;
}
}
}
return false;
}
// Work out where we are on the server
$base_url = dirname($_SERVER['SCRIPT_NAME']);
$request_url = $_SERVER['REQUEST_URI'];
$relative_request_url = preg_replace('/^'.preg_quote($base_url, '/').'/', '/', $request_url);
$absolute_request_url = 'http'.(isset($_SERVER['HTTPS']) ? 's' : '').'://'.$_SERVER['HTTP_HOST'].$request_url;
// Initialize authentication service
$auth_service = new AuthService($ldap, $user_dir, $config);
// Check if user is authenticated
$active_user = $auth_service->getCurrentUser();
// If no active user and not on a public route, redirect to login
if (!$active_user && !isPublicRoute($relative_request_url)) {
// Store the current URL to redirect back after login
$_SESSION['redirect_after_login'] = $_SERVER['REQUEST_URI'];
redirect('/login');
}
// Prevent authenticated users from accessing login page (they're already logged in)
if ($active_user && $relative_request_url === '/login') {
$redirect_url = $_SESSION['redirect_after_login'] ?? '/';
unset($_SESSION['redirect_after_login']);
redirect($redirect_url);
}
// Prevent logged out users from accessing logout page (they're already logged out)
if (!$active_user && $relative_request_url === '/logout') {
// They're already logged out, just redirect to login
redirect('/login');
}
if(empty($config['web']['enabled'])) {
require('views/error503.php');
die;
}
if($active_user && (!$active_user->active || $active_user->force_disable)) {
require('views/error403.php');
}
if(!empty($_POST) && $active_user) {
// Check CSRF token
if(isset($_SERVER['HTTP_X_BYPASS_CSRF_PROTECTION']) && $_SERVER['HTTP_X_BYPASS_CSRF_PROTECTION'] == 1) {
// This is being called from script, not a web browser
} elseif(!$active_user->check_csrf_token($_POST['csrf_token'])) {
require('views/csrf.php');
die;
}
}
// Route request to the correct view
$router = new Router;
foreach($routes as $path => $service) {
$public = array_key_exists($path, $public_routes);
$router->add_route($path, $service, $public);
}
$router->handle_request($relative_request_url);
if(isset($router->view)) {
$view = path_join($base_path, 'views', $router->view.'.php');
if(file_exists($view)) {
if($router->public || ($active_user && $active_user->auth_realm == 'LDAP')) {
require($view);
} else {
require('views/error403.php');
}
} else {
throw new Exception("View file $view missing.");
}
}
// Handler for uncaught exceptions
function exception_handler($e) {
global $active_user, $config;
$error_number = time();
error_log("$error_number: ".str_replace("\n", "\n$error_number: ", $e));
while(ob_get_length()) {
ob_end_clean();
}
require('views/error500.php');
die;
}