-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
executable file
·70 lines (53 loc) · 1.47 KB
/
Copy pathindex.php
File metadata and controls
executable file
·70 lines (53 loc) · 1.47 KB
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
65
66
67
68
69
70
<?php
$uri = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
$rootPath = __DIR__ . $uri;
$contentPath = __DIR__ . '/content' . $uri;
// serve static files
if ($uri !== '/' && file_exists($rootPath) && is_file($rootPath)) {
return false;
}
// dir exists but no index → 404
if ($uri === '/') {
require __DIR__ . '/main_page.php';
exit;
}
if (file_exists($contentPath) && is_file($contentPath)) {
$mimeType = mime_content_type($contentPath);
if ($mimeType === 'text/plain' && pathinfo($contentPath, PATHINFO_EXTENSION) === 'css') {
$mimeType = 'text/css';
}
// make sure .org files are readable and don't just render as html
if (pathinfo($contentPath, PATHINFO_EXTENSION) === 'org') {
$mimeType = 'text/plain';
}
if ($mimeType) {
header('Content-Type: ' . $mimeType);
}
// cache my shit
header('Cache-Control: public, max-age=10200');
readfile($contentPath);
exit;
}
// dir inside /content → serve its index.php or index.html
if (is_dir($contentPath)) {
foreach (['index.php', 'index.html'] as $indexFile) {
$index = $contentPath . '/' . $indexFile;
if (file_exists($index)) {
if (pathinfo($index, PATHINFO_EXTENSION) === 'php') {
require $index;
} else {
readfile($index);
}
exit;
}
}
http_response_code(404);
readfile(__DIR__ . '/404.html');
exit;
}
if (!file_exists($contentPath)) {
http_response_code(404);
readfile(__DIR__ . '/404.html');
exit;
}
require __DIR__ . '/main_page.php';