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
|
|
@ -120,3 +120,18 @@ Häufige Ursachen:
|
|||
- **Keine sichtbare Meldung:** Upload-Fehler werden jetzt als Flash-Meldung
|
||||
über dem Upload-Formular angezeigt und zusätzlich im PHP-Error-Log
|
||||
protokolliert.
|
||||
|
||||
## Zugriff auf Tour-Galerien
|
||||
|
||||
- Die Galerie-Seite und der Upload sind nur für **Admins** und für
|
||||
**Teilnehmer der jeweiligen Tour** erreichbar (vorhandene Buchung in
|
||||
`bookings`). Wer die Tour storniert, verliert den Zugriff.
|
||||
- Die Bilder liegen außerhalb von `public/` und werden ausschließlich über
|
||||
`/img_storage/...` ausgeliefert. Dort wird die Berechtigung anhand der
|
||||
Kategorie erneut geprüft:
|
||||
- `tour-<id>`: Teilnehmer und Admins
|
||||
- `tour-<id>-DELETED`: nur Admins
|
||||
- `user`, `pdf`: Mitglieder und Admins
|
||||
- Unberechtigte erhalten bei Tour-Bildern **HTTP 403**. Die Auslieferung ist
|
||||
auf `Cache-Control: private, must-revalidate` gesetzt, damit entzogene
|
||||
Zugriffe nicht aus einem gemeinsamen Cache weiterverwendet werden.
|
||||
|
|
|
|||
|
|
@ -18,9 +18,7 @@ class GalleryController
|
|||
redirect('/login');
|
||||
}
|
||||
|
||||
$hasTour = Booking::forUser((int) $tour_id, (int) $user->id) !== null;
|
||||
|
||||
if (!$user->isAdmin() && !$hasTour) {
|
||||
if (!Booking::canAccessTourGallery((int) $tour_id, $user)) {
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
|
|
@ -58,14 +56,14 @@ class GalleryController
|
|||
|
||||
public function store(string $tour_id): void
|
||||
{
|
||||
// Gleiche Berechtigung wie bei der Galerie-Ansicht: Admin oder gebuchte Tour.
|
||||
// Gleiche Berechtigung wie bei der Galerie-Ansicht: Admin oder Teilnehmer.
|
||||
$user = Auth::user();
|
||||
|
||||
if ($user === null) {
|
||||
redirect('/login');
|
||||
}
|
||||
|
||||
if (!$user->isAdmin() && Booking::forUser((int) $tour_id, (int) $user->id) === null) {
|
||||
if (!Booking::canAccessTourGallery((int) $tour_id, $user)) {
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,23 @@ class Booking extends Model
|
|||
return (int) db()->fetchValue('SELECT COUNT(*) FROM `bookings` WHERE tour_id = ?', [$tourId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* true, wenn der Benutzer die Bildergalerie einer Tour sehen darf:
|
||||
* Admins immer, sonst nur Teilnehmer (vorhandene Buchung).
|
||||
*/
|
||||
public static function canAccessTourGallery(int $tourId, ?User $user): bool
|
||||
{
|
||||
if ($user === null || $tourId <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($user->isAdmin()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return static::forUser($tourId, (int) $user->id) !== null;
|
||||
}
|
||||
|
||||
public static function forUser(int $tourId, int $userId): ?static
|
||||
{
|
||||
$row = db()->fetchOne('SELECT * FROM `bookings` WHERE tour_id = ? AND user_id = ?', [$tourId, $userId]);
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@
|
|||
<div class="col-md-8">
|
||||
<div class="card mt-4">
|
||||
|
||||
<a href="/img_storage/tour-1/wer-montags-lachelt_gross.jpg" data-title="Die Tour war schön" data-lightbox="/img_storage/tour-1/wer-montags-lachelt.jpg" class="img">IMAGE</a>
|
||||
|
||||
<div class="card-body">
|
||||
<?php if (session_flash('status')): ?>
|
||||
<div class="alert alert-success" role="alert">
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue