diff --git a/DEPLOY.md b/DEPLOY.md index 7409aa0..eddb5bc 100644 --- a/DEPLOY.md +++ b/DEPLOY.md @@ -88,6 +88,18 @@ git push origin main Als z. B. `public/__syntax_check.php` hochladen, im Browser aufrufen und die Datei danach **sofort wieder löschen**. +## Logs + +Die Anwendung schreibt strukturierte Logs (eine JSON-Zeile pro Ereignis) nach: + +``` +motorradig/storage/logs/app-YYYY-MM-DD.log +``` + +Erfasst werden u. a. fehlgeschlagene Validierungen, CSRF-/Session-Probleme, +zu große Uploads, Tour-Anlage-Versuche und unbehandelte Fehler. Die Logs +liegen außerhalb des Web-Roots und werden nicht mit deployed. + ## Rollback Die letzte Version einer Datei aus Git holen und erneut hochladen: diff --git a/motorradig/.gitignore b/motorradig/.gitignore index 744bc3f..6b6a14b 100644 --- a/motorradig/.gitignore +++ b/motorradig/.gitignore @@ -4,6 +4,7 @@ /*.sql /storage/img_storage/* !/storage/img_storage/.gitkeep +/storage/logs/ /storage/*.log /public/mix-manifest.json .DS_Store diff --git a/motorradig/src/Controllers/TourController.php b/motorradig/src/Controllers/TourController.php index 0b23911..c3fbb69 100644 --- a/motorradig/src/Controllers/TourController.php +++ b/motorradig/src/Controllers/TourController.php @@ -122,26 +122,58 @@ class TourController public function store(): void { + app_log('info', 'Tour create attempt', [ + 'name' => input('name'), + 'start' => input('start'), + 'deadline' => input('deadline'), + 'max_participants' => input('max_participants'), + 'is_cancelled' => input('is_cancelled'), + 'tour_guide_id' => input('tour_guide_id'), + 'has_image' => !empty($_FILES['bild']['name']), + 'image_name' => $_FILES['bild']['name'] ?? null, + 'image_size' => $_FILES['bild']['size'] ?? null, + ]); + validate([ 'name' => 'required|min:3', 'beschreibung' => 'required', 'start' => 'required', 'deadline' => 'required', 'tour_guide_id' => 'required', - 'bild' => 'mimes:jpg,jpeg,bmp,png,gif', ]); - $tour = Tour::create([ - 'name' => (string) input('name'), - 'tour_guide_id' => (int) input('tour_guide_id'), - 'beschreibung' => (string) input('beschreibung'), - 'start' => db_date(input('start')), - 'deadline' => db_date(input('deadline')), - 'max_participants' => (int) (input('max_participants') ?: 5), - 'is_cancelled' => (bool) input('is_cancelled', false), - 'bericht' => null, - 'created_at' => now(), - 'updated_at' => now(), + try { + $tour = Tour::create([ + 'name' => (string) input('name'), + 'tour_guide_id' => (int) input('tour_guide_id'), + 'beschreibung' => (string) input('beschreibung'), + 'start' => db_date(input('start')), + 'deadline' => db_date(input('deadline')), + 'max_participants' => (int) (input('max_participants') ?: 5), + 'is_cancelled' => (bool) input('is_cancelled', false), + 'bericht' => null, + 'created_at' => now(), + 'updated_at' => now(), + ]); + } catch (\Throwable $e) { + app_log('error', 'Tour create failed', [ + 'exception' => $e::class, + 'error' => $e->getMessage(), + 'name' => input('name'), + 'start' => input('start'), + 'deadline' => input('deadline'), + ]); + + $_SESSION['_old'] = $_POST; + flash_set('error', 'Die Tour konnte nicht gespeichert werden. Bitte den Administrator informieren.'); + redirect('/tour/create'); + } + + app_log('info', 'Tour created', [ + 'tour_id' => $tour->id, + 'name' => $tour->name, + 'start' => $tour->start, + 'tour_guide_id' => $tour->tour_guide_id, ]); if (!empty($_FILES['bild']['name'])) { @@ -152,6 +184,14 @@ class TourController error_log('Tourbild-Upload: ' . $e); } + app_log('error', 'Tour image upload failed', [ + 'tour_id' => $tour->id, + 'exception' => $e::class, + 'error' => $e->getMessage(), + 'image_name' => $_FILES['bild']['name'] ?? null, + 'image_size' => $_FILES['bild']['size'] ?? null, + ]); + flash_set('error', $e instanceof \RuntimeException ? $e->getMessage() : 'Das Bild konnte nicht verarbeitet werden.'); diff --git a/motorradig/src/Core/Auth.php b/motorradig/src/Core/Auth.php index ec9eee5..85b9894 100644 --- a/motorradig/src/Core/Auth.php +++ b/motorradig/src/Core/Auth.php @@ -77,6 +77,7 @@ class Auth public static function requireLogin(): void { if (!self::check()) { + app_log('warning', 'Login erforderlich bzw. Session abgelaufen'); redirect('/login'); } } diff --git a/motorradig/src/Core/Router.php b/motorradig/src/Core/Router.php index 2adeeed..58f6bc0 100644 --- a/motorradig/src/Core/Router.php +++ b/motorradig/src/Core/Router.php @@ -68,6 +68,10 @@ class Router // 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()) { + app_log('warning', 'Upload zu groß (post_max_size überschritten)', [ + 'content_length' => (int) ($_SERVER['CONTENT_LENGTH'] ?? 0), + 'post_max_size' => ini_get('post_max_size'), + ]); flash_set('error', 'Der Upload ist zu groß (Server-Limit: ' . ini_get('post_max_size') . '). Bitte kleinere Dateien verwenden.'); back(); } diff --git a/motorradig/src/bootstrap.php b/motorradig/src/bootstrap.php index 8901f4e..9211b9d 100644 --- a/motorradig/src/bootstrap.php +++ b/motorradig/src/bootstrap.php @@ -35,6 +35,43 @@ error_reporting(E_ALL); ini_set('log_errors', '1'); ini_set('display_errors', (getenv('APP_DEBUG') ?: 'false') === 'true' ? '1' : '0'); +// Unbehandelte Fehler landen zusätzlich im App-Log, damit spätere Vorfälle +// nachvollziehbar bleiben. +set_exception_handler(function (\Throwable $e): void { + try { + app_log('error', 'Unbehandelte Exception', [ + 'exception' => $e::class, + 'error' => $e->getMessage(), + 'file' => $e->getFile(), + 'line' => $e->getLine(), + ]); + } catch (\Throwable $logError) { + error_log('Unbehandelte Exception: ' . $e->getMessage()); + } + + http_response_code(500); + + if ((getenv('APP_DEBUG') ?: 'false') === 'true') { + echo '
' . e((string) $e) . '
'; + } else { + echo 'Interner Fehler. Bitte den Administrator informieren.'; + } +}); + +register_shutdown_function(function (): void { + $error = error_get_last(); + + if ($error === null || !in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR], true)) { + return; + } + + try { + app_log('error', 'Fataler Fehler', $error); + } catch (\Throwable $logError) { + error_log('Fataler Fehler: ' . ($error['message'] ?? 'unbekannt')); + } +}); + /** Startet eine Session (für Web-Requests). */ function start_session(): void { diff --git a/motorradig/src/helpers.php b/motorradig/src/helpers.php index 779be87..c31f979 100644 --- a/motorradig/src/helpers.php +++ b/motorradig/src/helpers.php @@ -192,6 +192,46 @@ function redirect(string $url): void exit; } +/** + * Schreibt einen strukturierten Eintrag in storage/logs/app-YYYY-MM-DD.log. + * Dadurch bleiben auch Fehler nachvollziehbar, die nur zu einem Redirect führen. + */ +function app_log(string $level, string $message, array $context = []): void +{ + $dir = storage_path('logs'); + + if (!is_dir($dir)) { + @mkdir($dir, 0775, true); + } + + $user = null; + + try { + $user = \App\Core\Auth::user(); + } catch (\Throwable $e) { + // Logging darf die eigentliche Anfrage nicht zusätzlich abbrechen. + } + + $entry = array_merge([ + 'time' => date('c'), + 'level' => strtoupper($level), + 'message' => $message, + 'user_id' => $user?->id, + 'user_name' => $user?->name, + 'ip' => $_SERVER['REMOTE_ADDR'] ?? null, + 'method' => $_SERVER['REQUEST_METHOD'] ?? null, + 'uri' => $_SERVER['REQUEST_URI'] ?? null, + 'referer' => $_SERVER['HTTP_REFERER'] ?? null, + 'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? null, + ], $context); + + $line = json_encode($entry, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE); + + if ($line === false || @file_put_contents($dir . '/app-' . date('Y-m-d') . '.log', $line . PHP_EOL, FILE_APPEND | LOCK_EX) === false) { + error_log('app_log: ' . ($line !== false ? $line : $message)); + } +} + /** Zurück zur vorherigen Seite (fallback auf '/'). */ function back(): void { @@ -218,6 +258,9 @@ function csrf_token(): string function verify_csrf(): void { if (empty($_SESSION['_csrf'])) { + app_log('warning', 'CSRF-Token fehlt', [ + 'post_fields' => array_keys($_POST), + ]); http_response_code(419); exit('CSRF-Token fehlt (Seite erneut laden).'); } @@ -225,6 +268,10 @@ function verify_csrf(): void $token = $_POST['_token'] ?? ($_SERVER['HTTP_X_CSRF_TOKEN'] ?? ''); if (!hash_equals($_SESSION['_csrf'], $token)) { + app_log('warning', 'CSRF-Token ungültig', [ + 'post_fields' => array_keys($_POST), + 'has_token' => $token !== '', + ]); http_response_code(419); exit('Ungültiges CSRF-Token.'); } @@ -413,6 +460,28 @@ function validate(array $rules): array } if ($errors !== []) { + $files = []; + + foreach ($_FILES as $field => $file) { + if (!is_array($file) || empty($file['name'])) { + continue; + } + + $name = basename((string) $file['name']); + $files[$field] = [ + 'name' => $name, + 'ext' => strtolower(pathinfo($name, PATHINFO_EXTENSION)), + 'size' => (int) ($file['size'] ?? 0), + 'error' => (int) ($file['error'] ?? 0), + ]; + } + + app_log('warning', 'Validierung fehlgeschlagen', [ + 'errors' => $errors, + 'post_fields' => array_keys($_POST), + 'files' => $files, + ]); + $_SESSION['_errors'] = $errors; $_SESSION['_old'] = $_POST; back(); diff --git a/motorradig/views/tour/create.php b/motorradig/views/tour/create.php index 7d42707..38035d3 100644 --- a/motorradig/views/tour/create.php +++ b/motorradig/views/tour/create.php @@ -1,3 +1,13 @@ + + + + + +
@@ -5,6 +15,23 @@

Tour anlegen

+ + + + + + + +
@@ -17,12 +44,12 @@
- +
- +
@@ -35,16 +62,20 @@ - + + + Optional. Weitere Bilder können nach dem Anlegen in der Tour hinzugefügt werden. +
- > + >
- +
Abbrechen
@@ -55,3 +86,23 @@
+ +