Fix: Bild-Uploads wieder funktionsfähig machen
- 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
This commit is contained in:
parent
8bde8d143e
commit
d8c9ec8ef8
16 changed files with 486 additions and 35 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
106
motorradig/bin/check-upload.php
Normal file
106
motorradig/bin/check-upload.php
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Diagnose für Bild-Uploads.
|
||||
*
|
||||
* php bin/check-upload.php
|
||||
* docker compose exec app php bin/check-upload.php
|
||||
*
|
||||
* Prüft GD, PHP-Limits und die Schreibrechte des Bildspeichers.
|
||||
*/
|
||||
|
||||
require dirname(__DIR__) . '/src/bootstrap.php';
|
||||
|
||||
$problems = 0;
|
||||
|
||||
function check(string $label, bool $ok, string $value): void
|
||||
{
|
||||
global $problems;
|
||||
|
||||
if (!$ok) {
|
||||
$problems++;
|
||||
}
|
||||
|
||||
echo str_pad($label, 28) . ': ' . ($ok ? 'OK ' : 'FEHLER') . ' ' . $value . "\n";
|
||||
}
|
||||
|
||||
echo "Upload-Diagnose\n";
|
||||
echo str_repeat('=', 70) . "\n\n";
|
||||
|
||||
check('PHP-Version', PHP_VERSION_ID >= 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";
|
||||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
8
motorradig/docker/php/uploads.ini
Normal file
8
motorradig/docker/php/uploads.ini
Normal file
|
|
@ -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
|
||||
7
motorradig/public/.user.ini
Normal file
7
motorradig/public/.user.ini
Normal file
|
|
@ -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
|
||||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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/<category>/. */
|
||||
public static function store(array $upload, string $category, string $name): void
|
||||
/**
|
||||
* Speichert ein hochgeladenes Bild unter storage/img_storage/<category>/.
|
||||
*
|
||||
* @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, '-');
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
<?php
|
||||
$flashError = flash_pull('error');
|
||||
|
||||
// Räume gruppieren, Reihenfolge wie aus der Abfrage
|
||||
$groups = [];
|
||||
foreach ($rooms as $room) {
|
||||
|
|
@ -26,6 +28,10 @@
|
|||
<div class="card-header"><?= e($tour->name) ?></div>
|
||||
|
||||
<div class="card-body">
|
||||
<?php if ($flashError !== null): ?>
|
||||
<div class="alert alert-danger" role="alert"><?= e($flashError) ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<form style="background-color: #fff;" class="text-dark" method="post"
|
||||
action="/tour/<?= e($tour->id) ?>" enctype="multipart/form-data">
|
||||
<?= csrf_field() ?>
|
||||
|
|
|
|||
|
|
@ -1,8 +1,29 @@
|
|||
<?php $current = auth_check() ? user() : null; ?>
|
||||
<?php
|
||||
$current = auth_check() ? user() : null;
|
||||
$flashStatus = flash_pull('status');
|
||||
$flashError = flash_pull('error');
|
||||
$uploadReady = $upload_ready ?? true;
|
||||
$uploadLimit = $upload_limit ?? ini_get('upload_max_filesize');
|
||||
?>
|
||||
|
||||
<div class="container-fluid text-center text-light mt-5">
|
||||
<h1><?= e($tour_name) ?></h1>
|
||||
|
||||
<?php if ($flashStatus !== null): ?>
|
||||
<div class="alert alert-success" role="alert"><?= e($flashStatus) ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if ($flashError !== null): ?>
|
||||
<div class="alert alert-danger" role="alert"><?= e($flashError) ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (!$uploadReady): ?>
|
||||
<div class="alert alert-danger" role="alert">
|
||||
Der Bildspeicher auf dem Server ist nicht beschreibbar
|
||||
(<code>storage/img_storage</code>). Bitte den Administrator informieren.
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="col-12 text-primary d-flex">
|
||||
<ul class="list-group col-12" style="display:inline">
|
||||
<?php foreach ($imagelist as $img_tpl): ?>
|
||||
|
|
@ -27,8 +48,12 @@
|
|||
<h1 style="color:white;">Hochladen</h1>
|
||||
<form style="background-color: #fff;" class="text-dark" method="post" action="/tour/gallery/<?= e($tour_id) ?>" enctype="multipart/form-data">
|
||||
<?= csrf_field() ?>
|
||||
<input type="file" id="myFile" name="files[]" multiple>
|
||||
<input type="file" id="myFile" name="files[]" accept="image/jpeg,image/png,image/gif,image/webp" multiple>
|
||||
<input type="submit" value="Hochladen">
|
||||
<p class="mt-2 mb-2" style="font-size:14px;">
|
||||
JPG, PNG, GIF oder WebP – maximal <?= e($uploadLimit) ?> pro Bild.
|
||||
HEIC/HEIF (iPhone) bitte vorher als JPG exportieren.
|
||||
</p>
|
||||
</form>
|
||||
|
||||
<?php if ($current && $current->isAdmin()): ?>
|
||||
|
|
|
|||
|
|
@ -1,6 +1,13 @@
|
|||
<?php $current = auth_check() ? user() : null; ?>
|
||||
<?php
|
||||
$current = auth_check() ? user() : null;
|
||||
$flashError = flash_pull('error');
|
||||
?>
|
||||
|
||||
<div class="container text-center text-light mt-5">
|
||||
<?php if ($flashError !== null): ?>
|
||||
<div class="alert alert-danger" role="alert"><?= e($flashError) ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<h1 class="text-center"><?= e($user->name) ?></h1>
|
||||
|
||||
<form style="background-color: #fff;" class="text-dark" method="post" action="/member/<?= e($user->id) ?>"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue