-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathrouter.php
42 lines (39 loc) · 1.39 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
<?php
// this is just for the easy bootstrapping of the demo
// adapted from http://stackoverflow.com/a/38926070
// DON'T USE IN PRODUCTION, please code your own router or use a framework!
chdir(__DIR__);
$filePath = realpath('./src/'.ltrim($_SERVER['REQUEST_URI'], '/'));
if ($filePath && is_dir($filePath)){
// attempt to find an index file
foreach (['index.php', 'index.html'] as $indexFile) {
if ($filePath = realpath($filePath . DIRECTORY_SEPARATOR . $indexFile)){
break;
}
}
}
if ($filePath && is_file($filePath)) {
// 1. check that file is not outside of this directory for security
// 2. check for circular reference to router.php
// 3. don't serve dot files
if (strpos($filePath, __DIR__ . DIRECTORY_SEPARATOR) === 0 &&
$filePath !== __DIR__ . DIRECTORY_SEPARATOR . 'router.php' &&
substr(basename($filePath), 0, 1) !== '.'
) {
if (strtolower(substr($filePath, -4)) === '.php') {
include $filePath;
} else {
if (strtolower(substr($filePath, -3)) === '.js') {
header('Content-Type: text/javascript');
}
readfile ($filePath);
}
} else {
// disallowed file
header('HTTP/1.1 404 Not Found');
echo '404 Not Found';
}
} else {
// rewrite to our index file
include 'src' . DIRECTORY_SEPARATOR . 'index.html';
}