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.

This commit is contained in:
smueller
2026-07-07 12:38:56 +02:00
parent 24f16e454e
commit dc44e38e32
2 changed files with 16 additions and 6 deletions

View File

@@ -76,14 +76,17 @@ function loadJsonFile(string $file, array $default): array
function saveJsonFile(string $file, array $data): void 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) { if ($json === false) {
throw new RuntimeException('Konfiguration konnte nicht kodiert werden.'); throw new RuntimeException('Konfiguration konnte nicht kodiert werden.');
} }
$written = @file_put_contents($file, $json, LOCK_EX); $written = @file_put_contents($file, $json, LOCK_EX);
if ($written === false) { 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).');
} }
} }

View File

@@ -72,11 +72,18 @@ function loadJsonFile(string $file, array $default): array
function saveJsonFile(string $file, array $data): void function saveJsonFile(string $file, array $data): void
{ {
file_put_contents( $json = json_encode(
$file, $data,
json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE
LOCK_EX
); );
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 function isAdminLoggedIn(): bool