Datenschutz: Tour-Galerien nur für Teilnehmer und Admins
- zentrale Regel Booking::canAccessTourGallery() (Buchung oder Admin) - Galerie-Ansicht und Upload nutzen diese Regel - Bild-Auslieferung prüft die Kategorie: Tour-Bilder nur für Teilnehmer, Papierkorb nur für Admins, unbekannte Kategorien gesperrt - unberechtigte Bild-Requests liefern HTTP 403 statt Inhalt - Bilder mit Cache-Control private/must-revalidate und ETag/304 - MIME-Typ wird am Bildinhalt ermittelt (Platzhalter war JPEG als .png) - öffentlichen Link auf ein geschütztes Tour-Bild von der Startseite entfernt
This commit is contained in:
parent
afdb77549a
commit
3570f6a8ed
5 changed files with 125 additions and 16 deletions
|
|
@ -5,24 +5,66 @@ declare(strict_types=1);
|
|||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Auth;
|
||||
use App\Models\Booking;
|
||||
use App\Models\User;
|
||||
|
||||
class ImageController
|
||||
{
|
||||
public function getImage(string $category, string $imageName): void
|
||||
{
|
||||
$user = Auth::user();
|
||||
$category = basename($category);
|
||||
|
||||
if ($user !== null && ($user->isAdmin() || $user->isMember())) {
|
||||
$file = $this->resolve(storage_path('img_storage'), $category, $imageName);
|
||||
$isTourGallery = preg_match('/^tour-\d+(-DELETED)?$/', $category) === 1;
|
||||
|
||||
if ($file !== null) {
|
||||
$this->serve($file);
|
||||
if (!$this->canAccessCategory($category, $user)) {
|
||||
// Tour-Galerien: klar verweigern, damit der Link nicht funktioniert.
|
||||
if ($isTourGallery) {
|
||||
$this->deny();
|
||||
return;
|
||||
}
|
||||
|
||||
// Profilbilder/PDFs: wie bisher einen Platzhalter liefern.
|
||||
$this->serve(public_path('img/placeholder/icon_unknown.png'));
|
||||
return;
|
||||
}
|
||||
|
||||
$placeholder = public_path('img/placeholder/icon_unknown.png');
|
||||
$this->serve($placeholder);
|
||||
$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();
|
||||
}
|
||||
|
||||
// Unbekannte Kategorien werden nicht ausgeliefert.
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Löst Kategorie + Dateiname zu einem sicheren Pfad auf. */
|
||||
|
|
@ -40,18 +82,59 @@ class ImageController
|
|||
return $file;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
private function serve(string $file): void
|
||||
{
|
||||
$mime = $this->mimeType($file);
|
||||
$mtime = @filemtime($file) ?: time();
|
||||
$size = @filesize($file) ?: 0;
|
||||
$etag = '"' . dechex($mtime) . '-' . dechex($size) . '"';
|
||||
|
||||
header('Content-Type: ' . $mime);
|
||||
header('Content-Length: ' . (string) filesize($file));
|
||||
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);
|
||||
readfile($file);
|
||||
exit;
|
||||
}
|
||||
|
||||
private function mimeType(string $file): string
|
||||
{
|
||||
$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'];
|
||||
}
|
||||
}
|
||||
|
||||
$map = [
|
||||
'jpg' => 'image/jpeg',
|
||||
'jpeg' => 'image/jpeg',
|
||||
|
|
@ -61,8 +144,6 @@ class ImageController
|
|||
'pdf' => 'application/pdf',
|
||||
];
|
||||
|
||||
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
|
||||
|
||||
return $map[$ext] ?? 'application/octet-stream';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue