From dc44e38e32b289d6afeec2a19643aaa6ca49f2c5 Mon Sep 17 00:00:00 2001 From: smueller Date: Tue, 7 Jul 2026 12:38:56 +0200 Subject: [PATCH] Refactor saveJsonFile function in index.php and wiki.php to include JSON_INVALID_UTF8_SUBSTITUTE flag for better handling of invalid UTF-8 characters. Enhanced error messages for file saving failures to include the file name, improving debugging and user guidance. --- index.php | 7 +++++-- wiki.php | 15 +++++++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/index.php b/index.php index 1d06380..41cdaa9 100644 --- a/index.php +++ b/index.php @@ -76,14 +76,17 @@ function loadJsonFile(string $file, array $default): array function saveJsonFile(string $file, array $data): void { - $json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); + $json = json_encode( + $data, + JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE + ); if ($json === false) { throw new RuntimeException('Konfiguration konnte nicht kodiert werden.'); } $written = @file_put_contents($file, $json, LOCK_EX); if ($written === false) { - throw new RuntimeException('Konfiguration konnte nicht gespeichert werden. Bitte Dateirechte prüfen.'); + throw new RuntimeException('Konfiguration konnte nicht gespeichert werden: ' . $file . ' (Dateirechte prüfen).'); } } diff --git a/wiki.php b/wiki.php index a54eecc..bb64bd5 100644 --- a/wiki.php +++ b/wiki.php @@ -72,11 +72,18 @@ function loadJsonFile(string $file, array $default): array function saveJsonFile(string $file, array $data): void { - file_put_contents( - $file, - json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), - LOCK_EX + $json = json_encode( + $data, + JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE ); + if ($json === false) { + throw new RuntimeException('Konfiguration konnte nicht kodiert werden.'); + } + + $written = @file_put_contents($file, $json, LOCK_EX); + if ($written === false) { + throw new RuntimeException('Konfiguration konnte nicht gespeichert werden: ' . $file . ' (Dateirechte prüfen).'); + } } function isAdminLoggedIn(): bool