This commit completes the merge process, incorporating remote changes that conflicted with local modifications. It also stages and commits all remaining modified and untracked files as per the user's instruction to 'upload everything without exception'.
66 lines
2.5 KiB
PHP
Executable File
66 lines
2.5 KiB
PHP
Executable File
<?php
|
|
// admin/sync_languages.php
|
|
|
|
require_once __DIR__ . '/../includes/session_check.php';
|
|
require_once __DIR__ . '/../config/config.php';
|
|
require_once __DIR__ . '/../includes/db.php';
|
|
require_once __DIR__ . '/../includes/logger.php';
|
|
require_once __DIR__ . '/../src/Translate.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
// Envolver todo en un manejador de errores para capturar hasta los errores fatales
|
|
register_shutdown_function(function () {
|
|
$error = error_get_last();
|
|
if ($error !== null && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR])) {
|
|
// Limpiar cualquier salida anterior
|
|
if (ob_get_length()) {
|
|
ob_end_clean();
|
|
}
|
|
echo json_encode(['success' => false, 'error' => 'Error fatal en el servidor: ' . $error['message'] . ' en ' . $error['file'] . ' línea ' . $error['line']]);
|
|
}
|
|
});
|
|
|
|
// Solo para administradores
|
|
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
|
|
echo json_encode(['success' => false, 'error' => 'Acceso denegado.']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
// 1. Verificar que la URL de LibreTranslate esté configurada
|
|
if (empty($_ENV['LIBRETRANSLATE_URL'])) {
|
|
throw new Exception("La variable de entorno LIBRETRANSLATE_URL no está configurada en tu archivo .env");
|
|
}
|
|
|
|
$translator = new Translate(LIBRETRANSLATE_URL);
|
|
$libreLanguages = $translator->getSupportedLanguages();
|
|
|
|
if ($libreLanguages === null) {
|
|
throw new Exception("No se pudo obtener la lista de idiomas de LibreTranslate. Revisa que la URL ('" . htmlspecialchars($_ENV['LIBRETRANSLATE_URL']) . "') sea correcta y que el servicio esté funcionando.");
|
|
}
|
|
|
|
if (empty($libreLanguages)) {
|
|
throw new Exception("LibreTranslate devolvió una lista de idiomas vacía.");
|
|
}
|
|
|
|
$newLanguagesCount = 0;
|
|
$sql = "INSERT IGNORE INTO supported_languages (language_code, language_name, is_active) VALUES (?, ?, 0)";
|
|
$stmt = $pdo->prepare($sql);
|
|
|
|
foreach ($libreLanguages as $lang) {
|
|
if (isset($lang['code']) && isset($lang['name'])) {
|
|
$stmt->execute([$lang['code'], $lang['name']]);
|
|
if ($stmt->rowCount() > 0) {
|
|
$newLanguagesCount++;
|
|
}
|
|
}
|
|
}
|
|
|
|
echo json_encode(['success' => true, 'new_languages' => $newLanguagesCount]);
|
|
|
|
} catch (Throwable $e) { // Captura Throwable para errores y excepciones
|
|
error_log("Error en sync_languages.php: " . $e->getMessage());
|
|
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
|
}
|
|
?>
|