Auto-discover locales and harden JSON config loading

- discover available locale files dynamically instead of relying on a hardcoded language list
- make config JSON decoding more defensive to avoid null-state failures on invalid or empty data
- improve startup resilience when configuration files are missing or malformed
This commit is contained in:
Johnnybegood90
2026-03-14 13:08:33 +01:00
parent bc5c9e296a
commit 90679e9221
2 changed files with 24 additions and 4 deletions
+11 -2
View File
@@ -3,7 +3,7 @@ $config_path = __DIR__ . '/config.json';
// Automatically migrate the legacy config format. // Automatically migrate the legacy config format.
if (file_exists($config_path)) { if (file_exists($config_path)) {
$existing = json_decode(file_get_contents($config_path), true); $existing = json_decode(file_get_contents($config_path), true) ?? [];
if (isset($existing['epg_url']) && !isset($existing['epg_sources'])) { if (isset($existing['epg_url']) && !isset($existing['epg_sources'])) {
$existing['epg_sources'] = [[ $existing['epg_sources'] = [[
'name' => 'Main', 'name' => 'Main',
@@ -22,7 +22,16 @@ if (file_exists($config_path)) {
} }
$is_first_setup = !file_exists($config_path); $is_first_setup = !file_exists($config_path);
$existing = $is_first_setup ? [] : json_decode(file_get_contents($config_path), true); $existing = [];
$corrupt_config = false;
if (!$is_first_setup) {
$existing = json_decode(file_get_contents($config_path), true) ?? [];
if (!is_array($existing)) {
$existing = [];
$corrupt_config = true;
$is_first_setup = true;
}
}
$admin_key = $existing['admin_key'] ?? null; $admin_key = $existing['admin_key'] ?? null;
$mode = 'setup'; $mode = 'setup';
+13 -2
View File
@@ -8,6 +8,10 @@ if (!file_exists($config_path)) {
} }
$config = json_decode(file_get_contents($config_path), true); $config = json_decode(file_get_contents($config_path), true);
if (!is_array($config)) {
http_response_code(500);
die('<h2>GridTV — Configuration error</h2><p><code>config.json</code> is invalid or corrupted.<br>Please delete it and re-run <a href="setup.php">setup</a>, or fix it manually via SSH.</p>');
}
// ── Automatically migrate the legacy config format to the current one ───────── // ── Automatically migrate the legacy config format to the current one ─────────
if (isset($config['epg_url']) && !isset($config['epg_sources'])) { if (isset($config['epg_url']) && !isset($config['epg_sources'])) {
@@ -32,15 +36,22 @@ $epg_url = htmlspecialchars($first_source['epg_url'] ?? '');
$m3u_url = htmlspecialchars($first_source['m3u_url'] ?? ''); $m3u_url = htmlspecialchars($first_source['m3u_url'] ?? '');
// ── Locale detection ───────────────────────────────────────────────────────── // ── Locale detection ─────────────────────────────────────────────────────────
$supported_locales = ['en', 'fr', 'es']; // Auto-discover available locales from the locales/ directory.
$locale_files = glob(__DIR__ . '/../locales/*.json') ?: [];
$supported_locales = array_map(fn($f) => basename($f, '.json'), $locale_files);
$locale = 'en'; $locale = 'en';
$accept = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? 'en'; $accept = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? 'en';
foreach (explode(',', $accept) as $lang) { foreach (explode(',', $accept) as $lang) {
$code = strtolower(substr(trim($lang), 0, 2)); $code = strtolower(substr(trim($lang), 0, 2));
if (in_array($code, $supported_locales)) { $locale = $code; break; } if (in_array($code, $supported_locales, true)) { $locale = $code; break; }
} }
$locale_path = __DIR__ . "/../locales/{$locale}.json"; $locale_path = __DIR__ . "/../locales/{$locale}.json";
$L = json_decode(file_get_contents($locale_path), true); $L = json_decode(file_get_contents($locale_path), true);
if (!is_array($L)) {
// Fallback to English if the locale file is missing or corrupted.
$fallback_path = __DIR__ . '/../locales/en.json';
$L = is_file($fallback_path) ? (json_decode(file_get_contents($fallback_path), true) ?? []) : [];
}
// ───────────────────────────────────────────────────────────────────────────── // ─────────────────────────────────────────────────────────────────────────────
?> ?>
<!DOCTYPE html> <!DOCTYPE html>