105 lines
3.1 KiB
PHP
Executable File
105 lines
3.1 KiB
PHP
Executable File
<?php
|
|
header('Content-Type: application/json');
|
|
|
|
// Habilitar logging para depuración
|
|
ini_set('display_errors', 0);
|
|
error_reporting(E_ALL);
|
|
|
|
define('DEBUG_MODE', true); // Cambiar a false en producción
|
|
|
|
require_once __DIR__ . '/../../shared/utils/helpers.php';
|
|
require_once __DIR__ . '/../../shared/auth/jwt.php';
|
|
|
|
// Verificar autenticación
|
|
try {
|
|
$userData = JWTAuth::authenticate();
|
|
if (!$userData) {
|
|
jsonResponse(['success' => false, 'error' => 'No autenticado'], 401);
|
|
}
|
|
} catch (Exception $e) {
|
|
jsonResponse(['success' => false, 'error' => 'Error de autenticación: ' . $e->getMessage()], 401);
|
|
}
|
|
|
|
// Verificar método
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
jsonResponse(['success' => false, 'error' => 'Método no permitido'], 405);
|
|
}
|
|
|
|
// Obtener datos de la petición
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$text = $input['text'] ?? '';
|
|
$targetLang = $input['target'] ?? '';
|
|
$sourceLang = $input['source'] ?? 'es';
|
|
|
|
// Validar parámetros
|
|
if (empty($text) || empty($targetLang)) {
|
|
jsonResponse(['success' => false, 'error' => 'Texto o idioma de destino no especificado'], 400);
|
|
}
|
|
|
|
try {
|
|
// URL de LibreTranslate (ajusta según tu configuración)
|
|
$ltUrl = getenv('LIBRETRANSLATE_URL') ?: 'http://10.10.4.17:5000';
|
|
|
|
// Configurar la petición a LibreTranslate
|
|
$ch = curl_init("$ltUrl/translate");
|
|
|
|
$postData = [
|
|
'q' => $text,
|
|
'source' => $sourceLang,
|
|
'target' => $targetLang,
|
|
'format' => 'html', // Para mantener formato HTML si existe
|
|
'api_key' => getenv('LIBRETRANSLATE_API_KEY') ?: ''
|
|
];
|
|
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_POST => true,
|
|
CURLOPT_POSTFIELDS => json_encode($postData),
|
|
CURLOPT_HTTPHEADER => [
|
|
'Content-Type: application/json',
|
|
'Accept: application/json'
|
|
]
|
|
]);
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$error = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
if ($error) {
|
|
throw new Exception("Error en la petición: $error");
|
|
}
|
|
|
|
$result = json_decode($response, true);
|
|
|
|
if ($httpCode !== 200 || !isset($result['translatedText'])) {
|
|
$errorMsg = $result['error'] ?? 'Error desconocido al traducir';
|
|
throw new Exception("Error en la traducción: $errorMsg");
|
|
}
|
|
|
|
// Devolver la traducción
|
|
jsonResponse([
|
|
'success' => true,
|
|
'translatedText' => $result['translatedText']
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
error_log("Error en translate.php: " . $e->getMessage());
|
|
|
|
jsonResponse([
|
|
'success' => false,
|
|
'error' => 'Error al procesar la traducción',
|
|
'debug' => DEBUG_MODE ? $e->getMessage() : null
|
|
], 500);
|
|
}
|
|
|
|
/**
|
|
* Envía una respuesta JSON y termina la ejecución
|
|
*/
|
|
function jsonResponse($data, $statusCode = 200) {
|
|
http_response_code($statusCode);
|
|
header('Content-Type: application/json');
|
|
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
exit;
|
|
}
|