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'; } }