-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrouter.php
executable file
·160 lines (144 loc) · 3.94 KB
/
router.php
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright (c) 2018 Zane Zakraisek
*
* Routes all requests to the appropriate controller or view.
* Returns the requested page from the view directory.
*
*/
session_start();
$REQUEST_URI = $_SERVER['REQUEST_URI'];
$path = urldecode(parse_url($REQUEST_URI, PHP_URL_PATH));
require_once './model/config.php';
require_once './model/auth.php';
//////// REQUESTS FOR API ////////
if( substr($path, 0, 5) === '/api/' ){
header('Content-Type: application/json');
require_once './model/courses.php';
require_once './model/queue.php';
require_once './controllers/errors.php';
if(is_login_endpoint($path)){
require_once './controllers/auth.php';
die();
}
//Authentication required beyond this point
if(!is_authenticated() && !attempt_silent_cas_auth()){
invalid_auth_reply();
die();
}
$username = $_SESSION['username']; //NOTE: This is always lowercase
$controller = explode("/", $path)[2];
switch($controller){
case "queue":
require_once './controllers/queue.php';
break;
case "user":
require_once './controllers/user.php';
break;
case "courses":
require_once './controllers/courses.php';
break;
case "stats":
require_once './model/stats.php';
require_once './controllers/stats.php';
break;
case "admins":
require_once './controllers/admins.php';
break;
default:
header('Location: /swagger');
}
}
//////// REQUESTS FOR PAGES ////////
else{
$source = './view'.$path.'.php';
//Open access pages
if(is_open_page($path)){
require_once $source;
die();
}
if(!is_authenticated()){
if(AUTH == 'CAS'){
require_once $phpcas_path . '/CAS.php';
phpCAS::client(CAS_VERSION_3_0, $cas_host, $cas_port, $cas_context);
phpCAS::setCasServerCACert($cas_server_ca_cert_path);
phpCAS::forceAuthentication();
$username = strtolower(phpCAS::getUser());
if(is_null(get_info($username))){
echo "User authenticated but information could not be obtained";
die();
}
$_SESSION["username"] = $username;
}elseif(AUTH == 'LDAP'){
require_once './view/index.php';
die();
}else{
echo "Invalid server auth config: Must be CAS or LDAP";
die();
}
}
//Authenticated beyond this point
//Admin Pages
if(is_admin_page($path)){
$username = $_SESSION['username'];
if(is_admin($username)){
require_once $source;
}else{
header('Location: /courses');
}
}
//Regular pages
elseif(file_exists($source)){
require_once $source;
}
//Nonexistant page
else{
header('Location: /courses');
}
}
//Check to see if the user is authenticated with CAS,
//and authenticate locally on the queue if so.
//
//This function DOES NOT force a CAS auth.
function attempt_silent_cas_auth(){
global $phpcas_path, $cas_host, $cas_context, $cas_port, $cas_server_ca_cert_path;
require_once $phpcas_path . '/CAS.php';
phpCAS::client(CAS_VERSION_3_0, $cas_host, $cas_port, $cas_context);
phpCAS::setCasServerCACert($cas_server_ca_cert_path);
$auth = phpCAS::checkAuthentication();
if($auth){
$username = strtolower(phpCAS::getUser());
if(is_null(get_info($username))){
echo "User authenticated but information could not be obtained";
die();
}
$_SESSION["username"] = $username;
return true;
}
return false;
}
function is_authenticated(){
return isset($_SESSION['username']);
}
function is_open_page($path){
return $path == '/about' ||
$path == '/help';
}
function is_login_endpoint($path){
return $path == '/api/login' ||
$path == '/api/logout';
}
function is_admin_page($path){
return $path == '/new_course' ||
$path == '/edit_course';
}
function invalid_auth_reply(){
http_response_code(401);
$return = array(
"authenticated" => False,
"error" => "Not Authenticated"
);
echo json_encode($return);
}
?>