-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.php
More file actions
131 lines (118 loc) · 3.91 KB
/
index.php
File metadata and controls
131 lines (118 loc) · 3.91 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
// Enable CORS for local development
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: *');
header('Access-Control-Allow-Methods: *');
$localhosts = ['localhost', '127.0.0.1', 'dev.webmanajemen.com', 'php.webmanajemen.com'];
if (in_array($_SERVER['HTTP_HOST'], $localhosts)) {
// Enable error reporting for local development
error_reporting(E_ALL);
ini_set('display_errors', 1);
} else {
// Disable error reporting for production
error_reporting(0);
ini_set('display_errors', 0);
}
// Route /assets and /data to dist/assets and dist/data with auto MIME type, allow only specific file types
if (strpos($_SERVER['REQUEST_URI'], '/assets/') === 0 || strpos($_SERVER['REQUEST_URI'], '/data/') === 0) {
$allowedExtensions = [
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'txt' => 'text/plain',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'webp' => 'image/webp',
'svg' => 'image/svg+xml',
'ico' => 'image/x-icon',
'xml' => 'application/xml',
'xsl' => 'application/xslt+xml',
'jsonc' => 'application/json',
'woff' => 'font/woff',
'woff2' => 'font/woff2',
'ttf' => 'font/ttf',
'otf' => 'font/otf',
'eot' => 'application/vnd.ms-fontobject',
'sfnt' => 'font/sfnt',
'font' => 'font/ttf',
'fnt' => 'font/ttf',
];
$requestPath = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
$ext = strtolower(pathinfo($requestPath, PATHINFO_EXTENSION));
if (!array_key_exists($ext, $allowedExtensions)) {
http_response_code(403);
echo '403 Forbidden: The requested file type is not allowed.';
exit;
}
// Try to serve from local root first, then from /dist/react
$locations = [
__DIR__ . $requestPath,
__DIR__ . '/dist/react' . $requestPath,
];
foreach ($locations as $file) {
if (file_exists($file)) {
$mimeType = $allowedExtensions[$ext];
if ($mimeType === null) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($finfo, $file);
finfo_close($finfo);
}
header('Content-Type: ' . $mimeType);
readfile($file);
exit;
}
}
http_response_code(404);
echo '404 Not Found: The requested file ' . htmlspecialchars($_SERVER['REQUEST_URI']) . ' was not found.';
exit;
}
// Handle php extension file not found
if (strpos($_SERVER['REQUEST_URI'], '.php') !== false) {
$filePath = __DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if (!file_exists($filePath)) {
http_response_code(404);
if (!headers_sent()) {
header('Content-Type: text/plain');
}
echo '404 Not Found: The requested PHP file ' . htmlspecialchars($_SERVER['REQUEST_URI']) . ' was not found.';
exit;
}
}
// Maintenance mode check
$maintenanceFile = __DIR__ . '/tmp/locks/.build-lock';
if (file_exists($maintenanceFile)) {
readfile(__DIR__ . '/index.maintenance.html');
exit;
}
if (php_sapi_name() === 'cli') {
$options = getopt('', ['set-maintenance', 'clear-maintenance']);
if (isset($options['set-maintenance'])) {
file_put_contents($maintenanceFile, 'Maintenance mode enabled at ' . date('Y-m-d H:i:s'));
echo "Maintenance mode enabled.\n";
} elseif (isset($options['clear-maintenance'])) {
if (file_exists($maintenanceFile)) {
unlink($maintenanceFile);
echo "Maintenance mode disabled.\n";
} else {
echo "Maintenance mode is not enabled.\n";
}
}
exit;
}
$distIndexFile = __DIR__ . '/dist/react/index.html';
if (file_exists($distIndexFile)) {
readfile($distIndexFile);
exit;
}
$indexFile = __DIR__ . '/index.html';
if (file_exists($indexFile)) {
readfile($indexFile);
exit;
} else {
http_response_code(404);
echo '404 Not Found: The main index.html file was not found in the application root directory.';
exit;
}