-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.php
48 lines (38 loc) · 1.27 KB
/
serve.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
<?php
// Set timezone
date_default_timezone_set('UTC');
// Directory that contains error pages
define('ERRORS', dirname(__FILE__).'/errors');
// Default index file
define('DIRECTORY_INDEX', 'index.php');
// Optional array of authorized client IPs for a bit of security
$config['hostsAllowed'] = [];
chdir(getcwd().DIRECTORY_SEPARATOR.'public');
function logAccess($status = 200)
{
file_put_contents('php://stdout', sprintf("[%s] %s:%s [%s]: %s\n",
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);
echo '403';
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(getcwd().DIRECTORY_SEPARATOR.$path) && strstr($path, '.php') === false
) {
return false;
}
logAccess();
require_once getcwd().DIRECTORY_SEPARATOR.'index.php';