-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.php
45 lines (37 loc) · 1.25 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
<?php
// Set timezone
date_default_timezone_set("UTC");
// Directory that contains error pages
define("ERRORS", dirname(__FILE__) . "/errors");
// Default index file
define("DIRECTORY_INDEX", "frontend/index.html");
// Optional array of authorized client IPs for a bit of security
$config["hostsAllowed"] = array();
function logAccess($status = 200) {
file_put_contents("php://stdout", sprintf("[%s] %s:%s [%s]: %sn",
date("D M j H:i:s Y"), $_SERVER["REMOTE_ADDR"],
$_SERVER["REMOTE_PORT"], $status, $_SERVER["REQUEST_URI"]));
}
// Parse allowed host list
if (!empty($config['hostsAllowed'])) {
if (!in_array($_SERVER['REMOTE_ADDR'], $config['hostsAllowed'])) {
logAccess(403);
http_response_code(403);
include ERRORS . '/403.php';
exit;
}
}
// 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)) {
$path = rtrim($path, "/") . "/" . DIRECTORY_INDEX;
}
// If the file exists then return false and let the server handle it
if (file_exists($_SERVER["DOCUMENT_ROOT"] . $path)) {
return false;
}
// default behavior
logAccess(404);
http_response_code(404);
include ERRORS . "/404.php";