folderweb/app/static.php

45 lines
1.5 KiB
PHP

<?php
// Serve static files from /app directory
$requestUri = $_SERVER['REQUEST_URI'];
$file = preg_replace('#^/app/#', '', parse_url($requestUri, PHP_URL_PATH));
$file = str_replace(['../', '..\\'], '', $file); // Prevent directory traversal
// Map request paths to actual file paths
$basePath = __DIR__ . '/';
$customBasePath = dirname(__DIR__) . '/';
if (str_starts_with($file, 'styles/')) {
$filePath = $customBasePath . 'custom/' . $file;
} elseif (str_starts_with($file, 'fonts/')) {
$filePath = $customBasePath . 'custom/' . $file;
} elseif (str_starts_with($file, 'assets/')) {
$filePath = $customBasePath . 'custom/' . $file;
} elseif (str_starts_with($file, 'default-styles/')) {
$filePath = $basePath . 'default/' . substr($file, 15); // Remove 'default-styles/' prefix
} else {
http_response_code(404);
exit;
}
// Check if file exists and is readable
if (!file_exists($filePath) || !is_readable($filePath)) {
http_response_code(404);
exit;
}
// Determine MIME type based on extension
$ext = pathinfo($filePath, PATHINFO_EXTENSION);
$mimeTypes = [
'css' => 'text/css',
'js' => 'application/javascript',
'woff' => 'font/woff',
'woff2' => 'font/woff2',
'ttf' => 'font/ttf',
'otf' => 'font/otf',
'eot' => 'application/vnd.ms-fontobject',
'svg' => 'image/svg+xml',
];
$mimeType = $mimeTypes[$ext] ?? (mime_content_type($filePath) ?: 'application/octet-stream');
header('Content-Type: ' . $mimeType);
readfile($filePath);