Panel/Panel/modules/website/doc_asset.php

32 lines
837 B
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/bootstrap.php';
$doc = trim((string)($_GET['doc'] ?? ''));
$file = trim((string)($_GET['file'] ?? ''));
if (!website_is_valid_doc_slug($doc) || !preg_match('/^[a-z0-9._-]+\.(png|jpe?g|webp)$/i', $file)) {
http_response_code(404);
exit;
}
$assetPath = website_doc_path($doc, $file);
if ($assetPath === null || !is_readable($assetPath)) {
http_response_code(404);
exit;
}
$extension = strtolower(pathinfo($assetPath, PATHINFO_EXTENSION));
$mimeTypes = [
'png' => 'image/png',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'webp' => 'image/webp',
];
$mimeType = $mimeTypes[$extension] ?? 'application/octet-stream';
header('Content-Type: ' . $mimeType);
header('Content-Length: ' . (string)filesize($assetPath));
readfile($assetPath);