<?php
/**
 * Liefert App-/Theme-Icons: zuerst aus storage/icons/ (eigene Uploads), sonst aus html/icons/.
 * Aufruf: /icon.php?name=icon.svg | theme-sun.svg | theme-moon.svg
 */
require __DIR__ . '/../config.php';

$allowed = ['icon.svg', 'theme-sun.svg', 'theme-moon.svg', 'settings.svg'];
$name = isset($_GET['name']) ? basename($_GET['name']) : '';
if ($name === '' || !in_array($name, $allowed, true)) {
    http_response_code(400);
    exit;
}

$storageDir = rtrim(STORAGE_ROOT, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'icons';
$defaultDir = __DIR__ . DIRECTORY_SEPARATOR . 'icons';
$path = $storageDir . DIRECTORY_SEPARATOR . $name;
if (!is_file($path) || !is_readable($path)) {
    $path = $defaultDir . DIRECTORY_SEPARATOR . $name;
}
if (!is_file($path) || !is_readable($path)) {
    http_response_code(404);
    exit;
}

$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
$mimes = ['svg' => 'image/svg+xml', 'ico' => 'image/x-icon', 'png' => 'image/png'];
$mime = $mimes[$ext] ?? 'application/octet-stream';
header('Content-Type: ' . $mime);
header('Cache-Control: public, max-age=86400');
header('Content-Length: ' . filesize($path));
readfile($path);
