forked from syseleven/shared-secrets
-
Notifications
You must be signed in to change notification settings - Fork 2
/
router.php
64 lines (58 loc) · 2.05 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
<?php
// Usage: `php -S localhost:8080 ./router.php`
// this script shall only be called from the CLI server
if ("cli-server" !== PHP_SAPI) { die(""); }
function preg_match_array($pattern, $subject) {
$result = false;
if (is_array($pattern) && is_string($subject)) {
foreach ($pattern as $pattern_item) {
$result = (1 === preg_match($pattern_item, $subject));
// it's enough to have one match
if ($result) {
break;
}
}
}
return $result;
}
// do some URL handling
$result = false;
$path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
if (preg_match_array(["@^\/\.dockerignore$@",
"@^\/\.env(\.default)?$@",
"@^\/\.git(\/.*)?$@",
"@^\/\.gitattributes$@",
"@^\/\.gitignore$@",
"@^\/\.htaccess$@",
"@^\/CHANGELOG(\.md)?$@",
"@^\/ENCRYPTION(\.md)?$@",
"@^\/LICENSE(\.md)?$@",
"@^\/README(\.md)?$@",
"@^\/SECURITY(\.md)?$@",
"@^\/Dockerfile$@",
"@^\/router\.php$@",
"@^\/defaults(\/.*)?$@",
"@^\/(html\/)?actions(\/.*)?$@",
"@^\/(html\/)?config(\/.*)?$@",
"@^\/(html\/)?db(\/.*)?$@",
"@^\/(html\/)?lib(\/.*)?$@",
"@^\/(html\/)?pages(\/.*)?$@",
"@^\/(html\/)?template(\/.*)?$@"],
$path)) {
// single entrypoint
require_once(__DIR__."/index.php");
$result = true;
} elseif (preg_match_array(["@^\/resources(\/.*)?$@",
"@^\/vendors(\/.*)?$@"],
$path) &&
is_file(__DIR__."/html{$path}")) {
// redirect direct accesses
http_response_code(302);
header("Location: /html{$path}");
$result = true;
} elseif (!is_file(__DIR__.$path)) {
// single entrypoint
require_once(__DIR__."/index.php");
$result = true;
}
return $result;