From d8c9ec8ef85b4cf09d02a4daae292db024cce83f Mon Sep 17 00:00:00 2001 From: torsten Date: Sun, 8 Nov 2026 14:32:36 +0100 Subject: [PATCH] =?UTF-8?q?Fix:=20Bild-Uploads=20wieder=20funktionsf=C3=A4?= =?UTF-8?q?hig=20machen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Upload-Limits auf 25 MB pro Bild bzw. 64 MB pro Request erhöht (public/.user.ini für FPM/CGI, docker/php/uploads.ini für Docker) - Docker: storage/-Verzeichnis beim Start auf www-data umstellen - Image::store() prüft Upload-Fehlercode, Bildformat, GD/JPEG und Schreibrechte und wirft verständliche Fehler statt still zu scheitern - Galerie, Benutzer- und Tour-Bild zeigen Fehler als Flash-Meldung - post_max_size-Überschreitung wird vor der CSRF-Prüfung abgefangen - Slug wandelt Großbuchstaben-Umlaute korrekt um - Diagnose-Script bin/check-upload.php ergänzt --- motorradig/Dockerfile | 3 + motorradig/README.md | 32 ++++++ motorradig/bin/check-upload.php | 106 +++++++++++++++++ motorradig/docker-compose.yml | 4 + motorradig/docker/nginx/default.conf | 5 + motorradig/docker/php/uploads.ini | 8 ++ motorradig/public/.user.ini | 7 ++ .../src/Controllers/GalleryController.php | 89 ++++++++++++--- motorradig/src/Controllers/TourController.php | 22 +++- motorradig/src/Controllers/UserController.php | 22 +++- motorradig/src/Core/Image.php | 108 +++++++++++++++--- motorradig/src/Core/Router.php | 7 ++ motorradig/src/helpers.php | 64 +++++++++++ motorradig/views/tour/edit.php | 6 + motorradig/views/tour/gallery/show.php | 29 ++++- motorradig/views/users/edit.php | 9 +- 16 files changed, 486 insertions(+), 35 deletions(-) create mode 100644 motorradig/bin/check-upload.php create mode 100644 motorradig/docker/php/uploads.ini create mode 100644 motorradig/public/.user.ini diff --git a/motorradig/Dockerfile b/motorradig/Dockerfile index 27286ee..0f0f6a2 100644 --- a/motorradig/Dockerfile +++ b/motorradig/Dockerfile @@ -9,4 +9,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ && docker-php-ext-install gd pdo_mysql \ && rm -rf /var/lib/apt/lists/* +# Höhere Upload-Limits für Handyfotos (Default wäre 2 MB). +COPY docker/php/uploads.ini /usr/local/etc/php/conf.d/zz-uploads.ini + WORKDIR /var/www diff --git a/motorradig/README.md b/motorradig/README.md index 2b953b5..60c5586 100644 --- a/motorradig/README.md +++ b/motorradig/README.md @@ -88,3 +88,35 @@ Die Datenbank wurde um ungenutzte bzw. redundante Bestandteile bereinigt Die Galerie ist rein dateisystembasiert; das Umbenennen von Bildern (`displayname`) entfällt. + +## Bild-Upload funktioniert nicht + +Zuerst die Diagnose ausführen: + +```bash +# Docker +docker compose exec app php bin/check-upload.php + +# nativ +php bin/check-upload.php +``` + +Häufige Ursachen: + +- **PHP-Limits zu klein:** Standard in `php:8.3-fpm` sind 2 MB pro Datei + (`upload_max_filesize`) und 8 MB pro Request (`post_max_size`). Handyfotos + sind oft größer. Die Werte werden über `public/.user.ini` (FPM/CGI) bzw. + `docker/php/uploads.ini` (Docker) auf 25 MB pro Bild / 64 MB pro Request angehoben. Nach dem + Ändern von `.user.ini` bis zu 5 Minuten warten (`user_ini.cache_ttl`) bzw. + PHP-FPM neu starten. +- **`storage/img_storage` nicht beschreibbar:** PHP-FPM läuft als `www-data`. + Bei einem Bind-Mount gehören die Verzeichnisse aber dem Host-Benutzer. + `docker-compose.yml` setzt die Rechte beim Container-Start automatisch. + Auf einem eigenen Server: + `chown -R www-data:www-data storage/img_storage`. +- **HEIC/HEIF (iPhone):** GD kann dieses Format nicht lesen. Die Bilder + müssen vor dem Upload als JPG exportiert werden. Die Galerie zeigt dafür + inzwischen eine konkrete Fehlermeldung an. +- **Keine sichtbare Meldung:** Upload-Fehler werden jetzt als Flash-Meldung + über dem Upload-Formular angezeigt und zusätzlich im PHP-Error-Log + protokolliert. diff --git a/motorradig/bin/check-upload.php b/motorradig/bin/check-upload.php new file mode 100644 index 0000000..1a6f07a --- /dev/null +++ b/motorradig/bin/check-upload.php @@ -0,0 +1,106 @@ += 80300, PHP_VERSION); + +check('GD-Erweiterung', extension_loaded('gd'), extension_loaded('gd') ? 'geladen' : 'fehlt'); + +check('GD: Bilder lesen', function_exists('imagecreatefromstring'), function_exists('imagecreatefromstring') ? 'imagecreatefromstring() vorhanden' : 'fehlt'); + +$jpegOk = function_exists('imagejpeg') + && function_exists('imagetypes') + && (imagetypes() & IMG_JPG) === IMG_JPG; + +check( + 'GD: JPEG schreiben', + $jpegOk, + $jpegOk ? 'imagejpeg() mit JPEG-Unterstützung vorhanden' : 'fehlt – GD wurde ohne libjpeg gebaut' +); + +if (function_exists('imagetypes')) { + $types = imagetypes(); + $formats = []; + + foreach (['JPG' => IMG_JPG, 'PNG' => IMG_PNG, 'GIF' => IMG_GIF, 'WebP' => IMG_WEBP] as $name => $flag) { + if (($types & $flag) === $flag) { + $formats[] = $name; + } + } + + check('Formate lesen', $formats !== [], $formats !== [] ? implode(', ', $formats) : 'keine'); +} + +echo "\n"; + +foreach (['upload_max_filesize', 'post_max_size', 'memory_limit', 'max_file_uploads', 'max_execution_time'] as $key) { + check($key, true, (string) ini_get($key)); +} + +$tmpDir = ini_get('upload_tmp_dir') ?: sys_get_temp_dir(); + +check('Upload-Temp-Verzeichnis', is_dir($tmpDir) && is_writable($tmpDir), $tmpDir); + +echo "\n"; + +$imgStorage = storage_path('img_storage'); + +check('storage/ existiert', is_dir(storage_path()), storage_path()); +check('img_storage/ existiert', is_dir($imgStorage), $imgStorage); + +$writeTarget = is_dir($imgStorage) ? $imgStorage : storage_path(); + +check('img_storage/ beschreibbar', is_dir($writeTarget) && is_writable($writeTarget), $writeTarget); + +if (function_exists('posix_geteuid')) { + $user = posix_getpwuid(posix_geteuid()); + check('PHP-Benutzer', true, $user['name'] ?? (string) posix_geteuid()); +} + +if (is_dir($imgStorage)) { + $owner = fileowner($imgStorage); + $group = filegroup($imgStorage); + + $ownerName = function_exists('posix_getpwuid') ? (posix_getpwuid($owner)['name'] ?? (string) $owner) : (string) $owner; + $groupName = function_exists('posix_getgrgid') ? (posix_getgrgid($group)['name'] ?? (string) $group) : (string) $group; + + echo str_pad('Eigentümer img_storage', 28) . ': ' . $ownerName . ':' . $groupName . "\n"; +} + +echo "\n"; + +if ($problems > 0) { + echo $problems . " Problem(e) gefunden. Uploads können dadurch fehlschlagen.\n"; + echo "Tipp: Im Docker-Setup korrigiert docker-compose.yml die Rechte beim Start\n"; + echo " (chown -R www-data:www-data /var/www/storage).\n"; + exit(1); +} + +echo "Alles in Ordnung.\n"; diff --git a/motorradig/docker-compose.yml b/motorradig/docker-compose.yml index 0c3c455..ee9f108 100644 --- a/motorradig/docker-compose.yml +++ b/motorradig/docker-compose.yml @@ -18,6 +18,10 @@ services: - db volumes: - ./:/var/www + # PHP-FPM läuft als www-data. Beim Bind-Mount gehören storage/ und + # img_storage/ aber dem Host-Benutzer, wodurch Uploads stumm fehlschlagen. + # Daher beim Start die Rechte im Container korrigieren. + command: sh -c "mkdir -p /var/www/storage/img_storage && chown -R www-data:www-data /var/www/storage && exec php-fpm" networks: - lahmi diff --git a/motorradig/docker/nginx/default.conf b/motorradig/docker/nginx/default.conf index dd9f70d..eb33f2e 100644 --- a/motorradig/docker/nginx/default.conf +++ b/motorradig/docker/nginx/default.conf @@ -16,6 +16,11 @@ server { fastcgi_param PATH_INFO $fastcgi_path_info; } + # Konfigurations- und versteckte Dateien (z. B. .user.ini) nicht ausliefern. + location ~ /\.(?!well-known) { + deny all; + } + location / { try_files $uri $uri/ /index.php?$query_string; gzip_static on; diff --git a/motorradig/docker/php/uploads.ini b/motorradig/docker/php/uploads.ini new file mode 100644 index 0000000..1cedcf9 --- /dev/null +++ b/motorradig/docker/php/uploads.ini @@ -0,0 +1,8 @@ +; Upload-Limits für große Handyfotos. +; Wird im Dockerfile nach /usr/local/etc/php/conf.d/ kopiert. +upload_max_filesize = 25M +post_max_size = 64M +memory_limit = 256M +max_file_uploads = 40 +max_execution_time = 120 +max_input_time = 120 diff --git a/motorradig/public/.user.ini b/motorradig/public/.user.ini new file mode 100644 index 0000000..814a4d4 --- /dev/null +++ b/motorradig/public/.user.ini @@ -0,0 +1,7 @@ +; Upload-Limits für große Handyfotos (greift bei PHP-FPM/CGI). +; Wird von PHP automatisch für alle Skripte unter public/ geladen. +upload_max_filesize = 25M +post_max_size = 64M +memory_limit = 256M +max_execution_time = 120 +max_input_time = 120 diff --git a/motorradig/src/Controllers/GalleryController.php b/motorradig/src/Controllers/GalleryController.php index 2129ae1..bcbb12d 100644 --- a/motorradig/src/Controllers/GalleryController.php +++ b/motorradig/src/Controllers/GalleryController.php @@ -29,9 +29,11 @@ class GalleryController $tourName = db()->fetchValue('SELECT name FROM tours WHERE id = ?', [(int) $tour_id]) ?: ''; view('tour.gallery.show', [ - 'tour_id' => $tour_id, - 'tour_name' => $tourName, - 'imagelist' => $imagelist, + 'tour_id' => $tour_id, + 'tour_name' => $tourName, + 'imagelist' => $imagelist, + 'upload_ready' => $this->isUploadReady('tour_' . $tour_id), + 'upload_limit' => ini_get('upload_max_filesize'), ]); } @@ -56,23 +58,64 @@ class GalleryController public function store(string $tour_id): void { - validate(['files' => 'required']); + // Gleiche Berechtigung wie bei der Galerie-Ansicht: Admin oder gebuchte Tour. + $user = Auth::user(); - $files = $_FILES['files'] ?? []; - $tmpNames = $files['tmp_name'] ?? []; + if ($user === null) { + redirect('/login'); + } - foreach ($tmpNames as $i => $tmpName) { - if (empty($tmpName) || !is_uploaded_file($tmpName)) { + if (!$user->isAdmin() && Booking::forUser((int) $tour_id, (int) $user->id) === null) { + redirect('/tour'); + } + + if (!isset($_FILES['files']) || !is_array($_FILES['files']['name'] ?? null)) { + flash_set('error', 'Bitte mindestens eine Bilddatei auswählen.'); + redirect('/tour/gallery/' . $tour_id); + } + + $names = $_FILES['files']['name']; + $tmpNames = $_FILES['files']['tmp_name'] ?? []; + $errors = $_FILES['files']['error'] ?? []; + $sizes = $_FILES['files']['size'] ?? []; + + $stored = 0; + $problems = []; + + foreach (array_keys($names) as $i) { + $upload = [ + 'name' => (string) ($names[$i] ?? ''), + 'tmp_name' => (string) ($tmpNames[$i] ?? ''), + 'error' => (int) ($errors[$i] ?? UPLOAD_ERR_NO_FILE), + 'size' => (int) ($sizes[$i] ?? 0), + ]; + + // Leere Slots überspringen, echte Fehler aber melden. + if ($upload['error'] === UPLOAD_ERR_NO_FILE) { continue; } - $name = $files['name'][$i] ?? ''; - $upload = [ - 'tmp_name' => $tmpName, - 'name' => $name, - ]; + try { + Image::store($upload, 'tour_' . $tour_id, $upload['name']); + $stored++; + } catch (\Throwable $e) { + if (!$e instanceof \RuntimeException) { + error_log('Galerie-Upload: ' . $e); + $problems[] = 'Ein Bild konnte nicht verarbeitet werden.'; + } else { + $problems[] = $e->getMessage(); + } + } + } - Image::store($upload, 'tour_' . $tour_id, $name); + if ($stored > 0) { + flash_set('status', $stored === 1 ? 'Bild wurde hochgeladen.' : $stored . ' Bilder wurden hochgeladen.'); + } + + if ($problems !== []) { + flash_set('error', implode(' ', array_unique($problems))); + } elseif ($stored === 0) { + flash_set('error', 'Bitte mindestens eine Bilddatei auswählen.'); } redirect('/tour/gallery/' . $tour_id); @@ -155,4 +198,22 @@ class GalleryController mkdir($dir, 0777, true); } } + + /** true, wenn das Zielverzeichnis (oder sein Elternverzeichnis) beschreibbar ist. */ + private function isUploadReady(string $category): bool + { + $dir = storage_path('img_storage/' . Image::slug($category)); + + if (is_dir($dir)) { + return is_writable($dir); + } + + $parent = storage_path('img_storage'); + + if (is_dir($parent)) { + return is_writable($parent); + } + + return is_writable(storage_path()); + } } diff --git a/motorradig/src/Controllers/TourController.php b/motorradig/src/Controllers/TourController.php index 559cc9a..a44653e 100644 --- a/motorradig/src/Controllers/TourController.php +++ b/motorradig/src/Controllers/TourController.php @@ -178,15 +178,28 @@ class TourController redirect('/tour'); } + // Bild wird in Image::store() inhaltlich geprüft (Format/Größe); + // die dortige Fehlermeldung wird als Flash-Meldung angezeigt. validate([ 'tour_id' => 'required', 'name' => 'required|min:3', 'user_id' => 'required', - 'bild' => 'mimes:jpg,jpeg,bmp,png,gif', ]); + $imageError = null; + if (!empty($_FILES['bild']['name'])) { - Image::store($_FILES['bild'], 'tour_' . $tourId, (string) input('name')); + try { + Image::store($_FILES['bild'], 'tour_' . $tourId, (string) input('name')); + } catch (\Throwable $e) { + if (!$e instanceof \RuntimeException) { + error_log('Tourbild-Upload: ' . $e); + } + + $imageError = $e instanceof \RuntimeException + ? $e->getMessage() + : 'Das Bild konnte nicht verarbeitet werden.'; + } } $tour->name = (string) input('name'); @@ -214,6 +227,11 @@ class TourController $tour->updated_at = now(); $tour->save(); + if ($imageError !== null) { + flash_set('error', $imageError); + redirect('/tour/' . $tourId . '/edit'); + } + redirect('/tour'); } diff --git a/motorradig/src/Controllers/UserController.php b/motorradig/src/Controllers/UserController.php index 4ca6ea0..a047447 100644 --- a/motorradig/src/Controllers/UserController.php +++ b/motorradig/src/Controllers/UserController.php @@ -99,15 +99,28 @@ class UserController redirect('/member'); } + // Bild wird in Image::store() inhaltlich geprüft (Format/Größe); + // die dortige Fehlermeldung wird als Flash-Meldung angezeigt. validate([ 'name' => 'required|min:3', 'email' => 'required|email', 'mobile' => 'required', - 'bild' => 'mimes:jpg,jpeg,bmp,png,gif', ]); + $imageError = null; + if (!empty($_FILES['bild']['name'])) { - Image::store($_FILES['bild'], 'user', (string) $userId); + try { + Image::store($_FILES['bild'], 'user', (string) $userId); + } catch (\Throwable $e) { + if (!$e instanceof \RuntimeException) { + error_log('Benutzerbild-Upload: ' . $e); + } + + $imageError = $e instanceof \RuntimeException + ? $e->getMessage() + : 'Das Bild konnte nicht verarbeitet werden.'; + } } $user->name = (string) input('name'); @@ -122,6 +135,11 @@ class UserController $user->updated_at = now(); $user->save(); + if ($imageError !== null) { + flash_set('error', $imageError); + redirect('/member/' . $userId . '/edit'); + } + redirect('/member'); } diff --git a/motorradig/src/Core/Image.php b/motorradig/src/Core/Image.php index 3a4ec5a..b5a76a3 100644 --- a/motorradig/src/Core/Image.php +++ b/motorradig/src/Core/Image.php @@ -14,33 +14,82 @@ namespace App\Core; * Hinweis: kein imagedestroy() aufrufen. Seit PHP 8.0 werden GdImage-Objekte * automatisch freigegeben; imagedestroy() ist seit PHP 8.5 deprecated und * erzeugt Warnungen, die Redirects (header()) kaputt machen können. + * + * Fehler werden bewusst als RuntimeException geworfen, damit die aufrufenden + * Controller sie dem Benutzer anzeigen können, statt still zu scheitern. */ class Image { - /** Speichert ein hochgeladenes Bild unter storage/img_storage//. */ - public static function store(array $upload, string $category, string $name): void + /** + * Speichert ein hochgeladenes Bild unter storage/img_storage//. + * + * @throws \RuntimeException wenn der Upload unvollständig, das Format nicht + * lesbar oder das Zielverzeichnis nicht beschreibbar ist + */ + public static function store(array $upload, string $category, string $name): bool { - if (empty($upload['tmp_name']) || !is_uploaded_file($upload['tmp_name'])) { - return; + // Ohne JPEG-Unterstützung in GD schlägt imagejpeg() sonst mit einem + // Fatal Error fehl, der (ohne display_errors) unsichtbar bleibt. + if (!function_exists('imagecreatefromstring') || !function_exists('imagejpeg') || (imagetypes() & IMG_JPG) !== IMG_JPG) { + error_log('Image::store: GD-Erweiterung fehlt oder wurde ohne JPEG-Unterstützung gebaut.'); + + throw new \RuntimeException('Die Bildverarbeitung auf dem Server unterstützt kein JPEG. Bitte den Administrator informieren (GD mit libjpeg installieren).'); + } + + $error = (int) ($upload['error'] ?? UPLOAD_ERR_OK); + + if ($error !== UPLOAD_ERR_OK) { + throw new \RuntimeException(upload_error_message($error) ?? 'Der Upload ist fehlgeschlagen.'); + } + + $tmp = (string) ($upload['tmp_name'] ?? ''); + + if ($tmp === '' || !is_uploaded_file($tmp)) { + throw new \RuntimeException('Die hochgeladene Datei konnte nicht gelesen werden.'); + } + + // Format anhand des Inhalts prüfen (nicht anhand der Dateiendung). + $info = @getimagesize($tmp); + + if ($info === false) { + throw new \RuntimeException('Die Datei ist kein lesbares Bild. Unterstützt werden JPG, PNG, GIF, WebP und BMP. HEIC/HEIF von iPhones bitte vorher in JPG umwandeln.'); + } + + $type = (int) ($info[2] ?? 0); + + if (!in_array($type, [IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_WEBP, IMAGETYPE_BMP], true)) { + throw new \RuntimeException('Dieses Bildformat wird nicht unterstützt. Bitte JPG, PNG, GIF, WebP oder BMP verwenden.'); + } + + [$w, $h] = [(int) $info[0], (int) $info[1]]; + + if ($w < 1 || $h < 1) { + throw new \RuntimeException('Das Bild hat keine gültige Größe.'); } $dir = config('paths.img_storage') . '/' . self::slug($category); if (!is_dir($dir)) { - mkdir($dir, 0777, true); + // 0775 genügt, wenn PHP-FPM als Eigentümer/Gruppe läuft; 0777 hilft + // bei Bind-Mounts mit abweichender UID. + @mkdir($dir, 0777, true); + } + + if (!is_dir($dir) || !is_writable($dir)) { + throw new \RuntimeException('Der Bildspeicher auf dem Server ist nicht beschreibbar. Bitte den Administrator informieren.'); } $base = $dir . '/' . self::slug(pathinfo($name, PATHINFO_FILENAME)); - $data = @file_get_contents($upload['tmp_name']); - $src = $data === false ? null : @imagecreatefromstring($data); + $data = @file_get_contents($tmp); + $src = $data === false ? false : @imagecreatefromstring($data); - if ($src === false || $src === null) { - return; + if (!$src instanceof \GdImage) { + error_log('Image::store: imagecreatefromstring() fehlgeschlagen für ' . $tmp); + + throw new \RuntimeException('Das Bild konnte nicht verarbeitet werden. Bitte ein anderes Format verwenden.'); } - [$w, $h] = [imagesx($src), imagesy($src)]; - // Große Ansicht if ($w > $h) { self::resize($src, $base . '_gross.jpg', 1200, (int) round($h * 1200 / $w)); @@ -54,20 +103,51 @@ class Image } else { self::resize($src, $base . '.jpg', (int) round($w * 256 / $h), 256); } + + return true; } private static function resize(\GdImage $src, string $target, int $newW, int $newH): void { + $newW = max(1, $newW); + $newH = max(1, $newH); + $dst = imagecreatetruecolor($newW, $newH); + + if ($dst === false) { + error_log('Image::resize: imagecreatetruecolor() fehlgeschlagen für ' . $target); + + throw new \RuntimeException('Das Bild konnte nicht verarbeitet werden.'); + } + + // Transparente PNG/GIF/WebP bekommen einen weißen Hintergrund, + // da das Ergebnis als JPEG gespeichert wird. + $white = imagecolorallocate($dst, 255, 255, 255); + + if ($white !== false) { + imagefilledrectangle($dst, 0, 0, $newW, $newH, $white); + } + imagecopyresampled($dst, $src, 0, 0, 0, 0, $newW, $newH, imagesx($src), imagesy($src)); - imagejpeg($dst, $target, 85); + + if (!@imagejpeg($dst, $target, 85)) { + error_log('Image::resize: imagejpeg() fehlgeschlagen für ' . $target); + + throw new \RuntimeException('Das Bild konnte nicht gespeichert werden (Schreibrechte prüfen).'); + } } /** Erzeugt einen sicheren, URL-tauglichen Slug. */ public static function slug(string $value): string { - $value = strtolower(trim($value)); - $value = str_replace(['ä', 'ö', 'ü', 'ß'], ['ae', 'oe', 'ue', 'ss'], $value); + // Umlaute vor dem Kleinschreiben ersetzen: strtolower() wandelt nur + // ASCII, "Ä" würde sonst zu "rger" statt "aer..." werden. + $value = str_replace( + ['Ä', 'Ö', 'Ü', 'ẞ', 'ä', 'ö', 'ü', 'ß'], + ['ae', 'oe', 'ue', 'ss', 'ae', 'oe', 'ue', 'ss'], + trim($value) + ); + $value = strtolower($value); $value = preg_replace('/[^a-z0-9]+/', '-', $value) ?? ''; $value = trim($value, '-'); diff --git a/motorradig/src/Core/Router.php b/motorradig/src/Core/Router.php index e96b0d6..2adeeed 100644 --- a/motorradig/src/Core/Router.php +++ b/motorradig/src/Core/Router.php @@ -65,6 +65,13 @@ class Router // CSRF-Schutz für verändernde Methoden if (in_array($method, ['POST', 'PUT', 'DELETE', 'PATCH'], true)) { + // Bei überschrittenem post_max_size sind $_POST und $_FILES leer. + // Ohne diese Prüfung würde der Request als CSRF-Fehler enden. + if (post_too_large()) { + flash_set('error', 'Der Upload ist zu groß (Server-Limit: ' . ini_get('post_max_size') . '). Bitte kleinere Dateien verwenden.'); + back(); + } + verify_csrf(); } diff --git a/motorradig/src/helpers.php b/motorradig/src/helpers.php index 949ade4..7467e37 100644 --- a/motorradig/src/helpers.php +++ b/motorradig/src/helpers.php @@ -98,6 +98,70 @@ function has_upload(string $field): bool return false; } +/** PHP-Upload-Fehlercode in eine lesbare Meldung übersetzen. */ +function upload_error_message(int $code): ?string +{ + return match ($code) { + UPLOAD_ERR_OK => null, + UPLOAD_ERR_INI_SIZE => 'Die Datei ist zu groß. Erlaubt sind maximal ' . ini_get('upload_max_filesize') . ' pro Bild.', + UPLOAD_ERR_FORM_SIZE => 'Die Datei ist zu groß für dieses Formular.', + UPLOAD_ERR_PARTIAL => 'Die Datei wurde nur unvollständig übertragen. Bitte erneut versuchen.', + UPLOAD_ERR_NO_FILE => 'Es wurde keine Datei ausgewählt.', + UPLOAD_ERR_NO_TMP_DIR => 'Der Server hat kein temporäres Upload-Verzeichnis.', + UPLOAD_ERR_CANT_WRITE => 'Die Datei konnte nicht auf dem Server gespeichert werden.', + UPLOAD_ERR_EXTENSION => 'Eine PHP-Erweiterung hat den Upload abgebrochen.', + default => 'Unbekannter Upload-Fehler (Code ' . $code . ').', + }; +} + +/** Größenangabe aus der php.ini (z. B. "25M") in Bytes umrechnen. */ +function ini_size_to_bytes(string $value): int +{ + $value = trim($value); + + if ($value === '') { + return 0; + } + + $number = (int) $value; + + return match (strtolower(substr($value, -1))) { + 'g' => $number * 1024 * 1024 * 1024, + 'm' => $number * 1024 * 1024, + 'k' => $number * 1024, + default => $number, + }; +} + +/** true, wenn der Request das post_max_size-Limit überschritten hat. */ +function post_too_large(): bool +{ + if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') { + return false; + } + + $max = ini_size_to_bytes((string) ini_get('post_max_size')); + $length = (int) ($_SERVER['CONTENT_LENGTH'] ?? 0); + + return $max > 0 && $length > $max; +} + +/** Flash-Meldung setzen (wird nach dem Redirect einmalig angezeigt). */ +function flash_set(string $key, string $message): void +{ + $_SESSION['_flash'][$key] = $message; +} + +/** Flash-Meldung lesen und anschließend entfernen. */ +function flash_pull(string $key): ?string +{ + $message = $_SESSION['_flash'][$key] ?? null; + + unset($_SESSION['_flash'][$key]); + + return is_string($message) ? $message : null; +} + /** Alter Eingabewert nach fehlgeschlagener Validierung (für Formulare). */ function old(string $key, $default = null) { diff --git a/motorradig/views/tour/edit.php b/motorradig/views/tour/edit.php index 542e111..09aee5c 100644 --- a/motorradig/views/tour/edit.php +++ b/motorradig/views/tour/edit.php @@ -1,4 +1,6 @@ name) ?>
+ + + +
diff --git a/motorradig/views/tour/gallery/show.php b/motorradig/views/tour/gallery/show.php index 1a60715..95573b0 100644 --- a/motorradig/views/tour/gallery/show.php +++ b/motorradig/views/tour/gallery/show.php @@ -1,8 +1,29 @@ - +

+ + + + + + + + + + + +
    @@ -27,8 +48,12 @@

    Hochladen

    - + +

    + JPG, PNG, GIF oder WebP – maximal pro Bild. + HEIC/HEIF (iPhone) bitte vorher als JPG exportieren. +

    isAdmin()): ?> diff --git a/motorradig/views/users/edit.php b/motorradig/views/users/edit.php index 19b09ab..91bb089 100644 --- a/motorradig/views/users/edit.php +++ b/motorradig/views/users/edit.php @@ -1,6 +1,13 @@ - +
    + + + +

    name) ?>