-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.php
40 lines (35 loc) · 1.08 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
<?php
$directoryIndexBasepath = 'index';
$directoryIndexExtensions = ['php', 'html', 'htm'];
function throw404() {
$statusCode = 404;
$outputColorRed = ["\033[31m", "\033[37m"];
file_put_contents(
"php://stdout",
sprintf("[%s] %s%s:%s [%s]: %s%s\n",
date("D M j H:i:s Y"),
$outputColorRed[0],
$_SERVER["REMOTE_ADDR"],
$_SERVER["REMOTE_PORT"],
$statusCode,
$_SERVER["REQUEST_URI"],
$outputColorRed[1]
));
http_response_code($statusCode);
require $statusCode . ".html";
}
// if requesting a directory then serve the default index
$path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
$ext = pathinfo($path, PATHINFO_EXTENSION);
if (empty($ext)) {
$basePath = rtrim($path, "/") . "/" . $directoryIndexBasepath;
}
if (file_exists($_SERVER["DOCUMENT_ROOT"] . $path)) {
return false;
}
foreach ($directoryIndexExtensions as $indexExt) {
if (file_exists($_SERVER["DOCUMENT_ROOT"] . $basePath . "." . $indexExt)) {
return false;
}
}
throw404();