-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
107 lines (86 loc) · 3.28 KB
/
Copy pathindex.php
File metadata and controls
107 lines (86 loc) · 3.28 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
<?php
/**
* Portfolio Hub V2 - Main Router
* This file routes all requests to the appropriate handlers
*/
// Error logging for debugging
error_log("Request: " . $_SERVER['REQUEST_METHOD'] . " " . $_SERVER['REQUEST_URI']);
// Check if setup is needed
$configFile = __DIR__ . '/config/config.php';
$needsSetup = !file_exists($configFile) || filesize($configFile) < 500;
if ($needsSetup && basename($_SERVER['PHP_SELF']) !== 'setup.php') {
header('Location: /setup.php');
exit;
}
// Get the request URI and remove query string
$requestUri = $_SERVER['REQUEST_URI'];
$requestUri = strtok($requestUri, '?');
$requestUri = ltrim($requestUri, '/');
// Route setup page
if ($requestUri === 'setup.php' || strpos($requestUri, 'setup.php') === 0) {
require __DIR__ . '/setup.php';
exit;
}
// Route API requests (check BEFORE admin to avoid path conflicts)
if (strpos($requestUri, 'api/') === 0 || $requestUri === 'api') {
error_log("Routing to API handler");
require __DIR__ . '/api/index.php';
exit;
}
// Route admin requests
if (strpos($requestUri, 'admin/') === 0 || strpos($requestUri, 'admin') === 0) {
$adminFile = str_replace('admin/', '', $requestUri);
$adminFile = str_replace('admin', '', $adminFile);
// Handle admin assets
if (strpos($adminFile, 'assets/') === 0) {
$assetPath = __DIR__ . '/admin/' . $adminFile;
if (file_exists($assetPath)) {
$mimeType = mime_content_type($assetPath);
header('Content-Type: ' . $mimeType);
header('Cache-Control: public, max-age=31536000');
readfile($assetPath);
exit;
}
}
// Handle admin PHP files
if (empty($adminFile)) {
$adminFile = 'tiles.php';
}
// Add .php extension if not present
if (!preg_match('/\.php$/', $adminFile)) {
$adminFile .= '.php';
}
$adminPath = __DIR__ . '/admin/' . $adminFile;
if (file_exists($adminPath)) {
require $adminPath;
exit;
}
// 404 if admin file not found
http_response_code(404);
echo '<!DOCTYPE html><html><head><title>404</title></head><body><h1>404 - Admin Page Not Found</h1></body></html>';
exit;
}
// Serve static files from public directory if they exist
if (!empty($requestUri)) {
// Sanitize the request URI to prevent path traversal
$requestUri = basename($requestUri);
$publicPath = __DIR__ . '/public/' . $requestUri;
// Verify the real path is within public directory
$realPath = realpath($publicPath);
$publicDir = realpath(__DIR__ . '/public/');
if ($realPath && $publicDir && strpos($realPath, $publicDir) === 0 && is_file($realPath)) {
// Determine MIME type
$mimeType = mime_content_type($realPath);
// Set cache headers for static assets
$extension = pathinfo($realPath, PATHINFO_EXTENSION);
$cacheable = in_array($extension, ['jpg', 'jpeg', 'png', 'gif', 'webp', 'css', 'js', 'woff', 'woff2', 'ico']);
header('Content-Type: ' . $mimeType);
if ($cacheable) {
header('Cache-Control: public, max-age=31536000, immutable');
}
readfile($realPath);
exit;
}
}
// Default to public index.php (home page)
require __DIR__ . '/public/index.php';