Tour-Anlage robuster machen und App-Logging erweitern
This commit is contained in:
parent
4c9a48e79f
commit
c64fa427b3
8 changed files with 232 additions and 17 deletions
12
DEPLOY.md
12
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:
|
||||
|
|
|
|||
1
motorradig/.gitignore
vendored
1
motorradig/.gitignore
vendored
|
|
@ -4,6 +4,7 @@
|
|||
/*.sql
|
||||
/storage/img_storage/*
|
||||
!/storage/img_storage/.gitkeep
|
||||
/storage/logs/
|
||||
/storage/*.log
|
||||
/public/mix-manifest.json
|
||||
.DS_Store
|
||||
|
|
|
|||
|
|
@ -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.');
|
||||
|
|
|
|||
|
|
@ -77,6 +77,7 @@ class Auth
|
|||
public static function requireLogin(): void
|
||||
{
|
||||
if (!self::check()) {
|
||||
app_log('warning', 'Login erforderlich bzw. Session abgelaufen');
|
||||
redirect('/login');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 '<pre>' . e((string) $e) . '</pre>';
|
||||
} 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
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -1,3 +1,13 @@
|
|||
<?php
|
||||
$flashError = flash_pull('error');
|
||||
$validationErrors = errors();
|
||||
unset($_SESSION['_errors']);
|
||||
?>
|
||||
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vanillajs-datepicker@1.1.2/dist/css/datepicker.min.css">
|
||||
<script src="https://cdn.jsdelivr.net/npm/vanillajs-datepicker@1.1.2/dist/js/datepicker.min.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/vanillajs-datepicker@1.1.2/dist/js/locales/de.js"></script>
|
||||
|
||||
<div class="container">
|
||||
<div class="row justify-content-center">
|
||||
<div class="col-md-8">
|
||||
|
|
@ -5,6 +15,23 @@
|
|||
<div class="card-header"><h4>Tour anlegen</h4></div>
|
||||
|
||||
<div class="card-body">
|
||||
<?php if ($flashError !== null): ?>
|
||||
<div class="alert alert-danger" role="alert"><?= e($flashError) ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if ($validationErrors !== []): ?>
|
||||
<div class="alert alert-danger" role="alert">
|
||||
<strong>Bitte die Eingaben prüfen:</strong>
|
||||
<ul class="mb-0 mt-1">
|
||||
<?php foreach ($validationErrors as $messages): ?>
|
||||
<?php foreach ($messages as $message): ?>
|
||||
<li><?= e($message) ?></li>
|
||||
<?php endforeach; ?>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<form style="background-color: #fff;" class="text-dark" method="post" action="/tour/store" enctype="multipart/form-data">
|
||||
<?= csrf_field() ?>
|
||||
|
||||
|
|
@ -17,12 +44,12 @@
|
|||
|
||||
<div class="form-group" style="display:flex;">
|
||||
<label class="ml-2 mt-3" style="width:180px; text-align:left;font-size:18px;" for="start">Start:</label>
|
||||
<input class="mt-3 mr-2 form-control input-sm" type="text" id="start" name="start" value="<?= e(old('start') ?? $tour->start) ?>" required>
|
||||
<input class="mt-3 mr-2 form-control input-sm" type="text" id="start" name="start" value="<?= e(old('start') ?? $tour->start) ?>" autocomplete="off" required>
|
||||
</div>
|
||||
|
||||
<div class="form-group" style="display:flex;">
|
||||
<label class="ml-2 mt-4" style="width:180px; text-align:left;font-size:18px;" for="deadline">Anmeldeschluss:</label>
|
||||
<input class="mt-3 mr-2 form-control" type="text" id="deadline" style="font-size:16px;" name="deadline" value="<?= e(old('deadline') ?? $tour->deadline) ?>" required>
|
||||
<input class="mt-3 mr-2 form-control" type="text" id="deadline" style="font-size:16px;" name="deadline" value="<?= e(old('deadline') ?? $tour->deadline) ?>" autocomplete="off" required>
|
||||
</div>
|
||||
|
||||
<div class="form-group" style="display:flex;">
|
||||
|
|
@ -35,16 +62,20 @@
|
|||
|
||||
<img src="/img/placeholder/tour128x100.jpg" class="mt-3 ml-3">
|
||||
|
||||
<input type="file" class="mt-3 mr-2 form-control" id="bild" style="font-size:20px;" name="bild" value="">
|
||||
<input type="file" class="mt-3 mr-2 form-control" id="bild" style="font-size:20px;" name="bild"
|
||||
accept="image/jpeg,image/png,image/gif,image/webp" value="">
|
||||
<small class="form-text text-muted ml-3 mt-2">
|
||||
Optional. Weitere Bilder können nach dem Anlegen in der Tour hinzugefügt werden.
|
||||
</small>
|
||||
</div>
|
||||
|
||||
<div class="form-group" style="display:flex;">
|
||||
<label class="ml-2 mt-3" style="width:140px; text-align:left;font-size:18px;" for="is_cancelled">Storniert:</label>
|
||||
<input class="mt-3 mr-2 form-control" type="checkbox" id="is_cancelled" style="font-size:10px;width:20px;" name="is_cancelled" <?= input('is_cancelled') ? 'checked' : '' ?>>
|
||||
<input class="mt-3 mr-2 form-control" type="checkbox" id="is_cancelled" style="font-size:10px;width:20px;" name="is_cancelled" <?= (old('is_cancelled') ?? input('is_cancelled')) ? 'checked' : '' ?>>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<textarea class="col-12" name="beschreibung" rows="10" cols="50"><?= e($tour->beschreibung) ?></textarea>
|
||||
<textarea class="col-12" name="beschreibung" rows="10" cols="50"><?= e(old('beschreibung') ?? $tour->beschreibung) ?></textarea>
|
||||
</div>
|
||||
|
||||
<a href="/tour"><div type="submit" class="btn btn-success col-2 mb-2 float-right mr-2">Abbrechen</div></a>
|
||||
|
|
@ -55,3 +86,23 @@
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const startInput = document.querySelector('input[name="start"]');
|
||||
if (startInput && typeof Datepicker !== 'undefined') {
|
||||
new Datepicker(startInput, {
|
||||
'autohide': true,
|
||||
'format': 'dd.mm.yyyy',
|
||||
'language': 'de'
|
||||
});
|
||||
}
|
||||
|
||||
const deadlineInput = document.querySelector('input[name="deadline"]');
|
||||
if (deadlineInput && typeof Datepicker !== 'undefined') {
|
||||
new Datepicker(deadlineInput, {
|
||||
'autohide': true,
|
||||
'format': 'dd.mm.yyyy',
|
||||
'language': 'de'
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue