2026-10-09 09:07:18 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Core\Auth;
|
2026-11-08 14:46:19 +01:00
|
|
|
use App\Models\Booking;
|
|
|
|
|
use App\Models\User;
|
2026-10-09 09:07:18 +02:00
|
|
|
|
|
|
|
|
class ImageController
|
|
|
|
|
{
|
|
|
|
|
public function getImage(string $category, string $imageName): void
|
|
|
|
|
{
|
|
|
|
|
$user = Auth::user();
|
2026-11-08 14:46:19 +01:00
|
|
|
$category = basename($category);
|
2026-10-09 09:07:18 +02:00
|
|
|
|
2026-11-08 14:46:19 +01:00
|
|
|
$isTourGallery = preg_match('/^tour-\d+(-DELETED)?$/', $category) === 1;
|
2026-10-09 09:07:18 +02:00
|
|
|
|
2026-11-08 14:46:19 +01:00
|
|
|
if (!$this->canAccessCategory($category, $user)) {
|
|
|
|
|
// Tour-Galerien: klar verweigern, damit der Link nicht funktioniert.
|
|
|
|
|
if ($isTourGallery) {
|
|
|
|
|
$this->deny();
|
2026-10-09 09:07:18 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2026-11-08 14:46:19 +01:00
|
|
|
|
|
|
|
|
// Profilbilder/PDFs: wie bisher einen Platzhalter liefern.
|
|
|
|
|
$this->serve(public_path('img/placeholder/icon_unknown.png'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$file = $this->resolve(storage_path('img_storage'), $category, $imageName);
|
|
|
|
|
|
|
|
|
|
if ($file !== null) {
|
|
|
|
|
$this->serve($file);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->serve(public_path('img/placeholder/icon_unknown.png'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Zugriffsregeln pro Bildkategorie:
|
|
|
|
|
* tour-<id> nur Teilnehmer der Tour und Admins
|
|
|
|
|
* tour-<id>-DELETED nur Admins (Papierkorb)
|
|
|
|
|
* user, pdf Mitglieder und Admins (wie bisher)
|
|
|
|
|
*/
|
|
|
|
|
private function canAccessCategory(string $category, ?User $user): bool
|
|
|
|
|
{
|
|
|
|
|
if ($user === null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (preg_match('/^tour-(\d+)-DELETED$/', $category, $matches) === 1) {
|
|
|
|
|
return $user->isAdmin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (preg_match('/^tour-(\d+)$/', $category, $matches) === 1) {
|
|
|
|
|
return Booking::canAccessTourGallery((int) $matches[1], $user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (in_array($category, ['user', 'pdf'], true)) {
|
|
|
|
|
return $user->isAdmin() || $user->isMember();
|
2026-10-09 09:07:18 +02:00
|
|
|
}
|
|
|
|
|
|
2026-11-08 14:46:19 +01:00
|
|
|
// Unbekannte Kategorien werden nicht ausgeliefert.
|
|
|
|
|
return false;
|
2026-10-09 09:07:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-08 14:46:19 +01:00
|
|
|
/** Verweigert den Zugriff ohne verräterische Details. */
|
|
|
|
|
private function deny(): void
|
|
|
|
|
{
|
|
|
|
|
http_response_code(403);
|
|
|
|
|
header('Content-Type: text/plain; charset=UTF-8');
|
|
|
|
|
header('Cache-Control: no-store');
|
|
|
|
|
echo 'Zugriff verweigert.';
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-09 09:07:18 +02:00
|
|
|
private function serve(string $file): void
|
|
|
|
|
{
|
|
|
|
|
$mime = $this->mimeType($file);
|
2026-11-08 14:46:19 +01:00
|
|
|
$mtime = @filemtime($file) ?: time();
|
|
|
|
|
$size = @filesize($file) ?: 0;
|
|
|
|
|
$etag = '"' . dechex($mtime) . '-' . dechex($size) . '"';
|
2026-10-09 09:07:18 +02:00
|
|
|
|
|
|
|
|
header('Content-Type: ' . $mime);
|
2026-11-08 14:46:19 +01:00
|
|
|
header('X-Content-Type-Options: nosniff');
|
|
|
|
|
header('Cache-Control: private, must-revalidate');
|
|
|
|
|
header('ETag: ' . $etag);
|
|
|
|
|
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $mtime) . ' GMT');
|
|
|
|
|
|
|
|
|
|
// 304 nur nach erfolgreicher Rechteprüfung, damit entzogene Zugriffe
|
|
|
|
|
// nicht aus dem Browser-Cache weiterverwendet werden.
|
|
|
|
|
$ifNoneMatch = trim((string) ($_SERVER['HTTP_IF_NONE_MATCH'] ?? ''));
|
|
|
|
|
$ifModifiedSince = trim((string) ($_SERVER['HTTP_IF_MODIFIED_SINCE'] ?? ''));
|
|
|
|
|
|
|
|
|
|
if ($ifNoneMatch === $etag
|
|
|
|
|
|| ($ifNoneMatch === '' && $ifModifiedSince !== '' && (strtotime($ifModifiedSince) ?: 0) >= $mtime)) {
|
|
|
|
|
http_response_code(304);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
header('Content-Length: ' . $size);
|
2026-10-09 09:07:18 +02:00
|
|
|
readfile($file);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function mimeType(string $file): string
|
|
|
|
|
{
|
2026-11-08 14:46:19 +01:00
|
|
|
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
|
|
|
|
|
|
|
|
|
|
// Bei Bildern den tatsächlichen Inhalt auswerten: Dateiendung und
|
|
|
|
|
// Inhalt stimmen nicht immer überein (z. B. beim Platzhalterbild).
|
|
|
|
|
if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'webp'], true)) {
|
|
|
|
|
$info = @getimagesize($file);
|
|
|
|
|
|
|
|
|
|
if ($info !== false && !empty($info['mime'])) {
|
|
|
|
|
return (string) $info['mime'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-09 09:07:18 +02:00
|
|
|
$map = [
|
|
|
|
|
'jpg' => 'image/jpeg',
|
|
|
|
|
'jpeg' => 'image/jpeg',
|
|
|
|
|
'png' => 'image/png',
|
|
|
|
|
'gif' => 'image/gif',
|
|
|
|
|
'webp' => 'image/webp',
|
|
|
|
|
'pdf' => 'application/pdf',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return $map[$ext] ?? 'application/octet-stream';
|
|
|
|
|
}
|
|
|
|
|
}
|