This commit is contained in:
torsten 2026-10-23 13:14:31 +02:00
parent 1db373a03d
commit 9bcbd3e194
118 changed files with 1116 additions and 155 deletions

View file

@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace App\Controllers;
use App\Core\Auth;
class ImageController
{
public function getImage(string $category, string $imageName): void
{
$user = Auth::user();
if ($user !== null && ($user->isAdmin() || $user->isMember())) {
$file = $this->resolve(storage_path('img_storage'), $category, $imageName);
if ($file !== null) {
$this->serve($file);
return;
}
}
$placeholder = public_path('img/placeholder/icon_unknown.png');
$this->serve($placeholder);
}
/** Löst Kategorie + Dateiname zu einem sicheren Pfad auf. */
private function resolve(string $base, string $category, string $imageName): ?string
{
$category = basename($category);
$imageName = basename($imageName);
$file = realpath($base . '/' . $category . '/' . $imageName);
if ($file === false || !str_starts_with($file, realpath($base))) {
return null;
}
return $file;
}
private function serve(string $file): void
{
$mime = $this->mimeType($file);
header('Content-Type: ' . $mime);
header('Content-Length: ' . (string) filesize($file));
readfile($file);
exit;
}
private function mimeType(string $file): string
{
$map = [
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'webp' => 'image/webp',
'pdf' => 'application/pdf',
];
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
return $map[$ext] ?? 'application/octet-stream';
}
}