up
This commit is contained in:
+25
-7
@@ -4,7 +4,9 @@
|
|||||||
* Fecha: 13 de noviembre de 2025
|
* Fecha: 13 de noviembre de 2025
|
||||||
*/
|
*/
|
||||||
|
|
||||||
require_once __DIR__ . '/../config/config_enhanced.php';
|
require_once __DIR__ . '/../config/config.php';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Headers para API
|
// Headers para API
|
||||||
header('Content-Type: application/json; charset=utf-8');
|
header('Content-Type: application/json; charset=utf-8');
|
||||||
@@ -18,12 +20,23 @@ class WhatsAppWebhook {
|
|||||||
private $botService;
|
private $botService;
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->db = Database::getInstance();
|
error_log('[webhook] __construct start');
|
||||||
$this->whatsappService = new WhatsAppService();
|
try {
|
||||||
$this->botService = new BotService();
|
$this->db = Database::getInstance();
|
||||||
|
$this->whatsappService = new WhatsAppService();
|
||||||
|
$this->botService = new BotService();
|
||||||
|
} catch (Throwable $t) {
|
||||||
|
error_log('[webhook] constructor failed: ' . $t->getMessage());
|
||||||
|
if (function_exists('writeLog')) {
|
||||||
|
writeLog('ERROR', 'Webhook constructor failed: ' . $t->getMessage(), ['trace' => $t->getTraceAsString()]);
|
||||||
|
}
|
||||||
|
throw $t;
|
||||||
|
}
|
||||||
|
error_log('[webhook] __construct end');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handleRequest() {
|
public function handleRequest() {
|
||||||
|
error_log('[webhook] handleRequest start, method=' . ($_SERVER['REQUEST_METHOD'] ?? 'unknown'));
|
||||||
$method = $_SERVER['REQUEST_METHOD'];
|
$method = $_SERVER['REQUEST_METHOD'];
|
||||||
|
|
||||||
if ($method === 'GET') {
|
if ($method === 'GET') {
|
||||||
@@ -251,10 +264,15 @@ if (php_sapi_name() !== 'cli') {
|
|||||||
try {
|
try {
|
||||||
$webhook = new WhatsAppWebhook();
|
$webhook = new WhatsAppWebhook();
|
||||||
$webhook->handleRequest();
|
$webhook->handleRequest();
|
||||||
} catch (Exception $e) {
|
} catch (Throwable $t) {
|
||||||
error_log("Fatal error in webhook: " . $e->getMessage());
|
// Manejar excepciones fatales y errores (Throwable)
|
||||||
|
error_log("Fatal error in webhook: " . $t->getMessage());
|
||||||
|
error_log($t->getTraceAsString());
|
||||||
|
if (function_exists('writeLog')) {
|
||||||
|
writeLog('ERROR', 'Fatal error in webhook: ' . $t->getMessage(), ['trace' => $t->getTraceAsString()]);
|
||||||
|
}
|
||||||
http_response_code(500);
|
http_response_code(500);
|
||||||
echo json_encode(['error' => 'Error fatal del servidor']);
|
echo json_encode(['error' => 'Error fatal del servidor', 'detail' => $t->getMessage()]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// Función para cargar archivo .env
|
// Función para cargar archivo .env
|
||||||
|
if (!function_exists('loadEnvFile')) {
|
||||||
function loadEnvFile($path) {
|
function loadEnvFile($path) {
|
||||||
if (!file_exists($path)) {
|
if (!file_exists($path)) {
|
||||||
return false;
|
return false;
|
||||||
@@ -30,14 +31,17 @@ function loadEnvFile($path) {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Cargar .env si existe
|
// Cargar .env si existe
|
||||||
$envLoaded = loadEnvFile(__DIR__ . '/../.env');
|
$envLoaded = function_exists('loadEnvFile') ? loadEnvFile(__DIR__ . '/../.env') : false;
|
||||||
|
|
||||||
// Función helper para obtener variables de entorno con fallback
|
// Función helper para obtener variables de entorno con fallback
|
||||||
|
if (!function_exists('env')) {
|
||||||
function env($key, $default = null) {
|
function env($key, $default = null) {
|
||||||
return $_ENV[$key] ?? getenv($key) ?: $default;
|
return $_ENV[$key] ?? getenv($key) ?: $default;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Configuración de la base de datos
|
// Configuración de la base de datos
|
||||||
define('DB_HOST', env('DB_HOST', 'localhost'));
|
define('DB_HOST', env('DB_HOST', 'localhost'));
|
||||||
@@ -122,6 +126,7 @@ if (!defined('AUTO_DETECTED_URL')) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Función para logging centralizado
|
// Función para logging centralizado
|
||||||
|
if (!function_exists('writeLog')) {
|
||||||
function writeLog($level, $message, $context = []) {
|
function writeLog($level, $message, $context = []) {
|
||||||
if (!ENABLE_LOGGING) return;
|
if (!ENABLE_LOGGING) return;
|
||||||
|
|
||||||
@@ -143,18 +148,24 @@ function writeLog($level, $message, $context = []) {
|
|||||||
|
|
||||||
file_put_contents($logDir . '/system.log', $logMessage, FILE_APPEND | LOCK_EX);
|
file_put_contents($logDir . '/system.log', $logMessage, FILE_APPEND | LOCK_EX);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Función para verificar si la instalación está completada
|
// Función para verificar si la instalación está completada
|
||||||
|
if (!function_exists('isInstallationCompleted')) {
|
||||||
function isInstallationCompleted() {
|
function isInstallationCompleted() {
|
||||||
return file_exists(dirname(__DIR__) . '/.installation_completed');
|
return file_exists(dirname(__DIR__) . '/.installation_completed');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Función para verificar login
|
// Función para verificar login
|
||||||
|
if (!function_exists('isUserLoggedIn')) {
|
||||||
function isUserLoggedIn() {
|
function isUserLoggedIn() {
|
||||||
return isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true;
|
return isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Función para verificar intentos de login
|
// Función para verificar intentos de login
|
||||||
|
if (!function_exists('checkLoginAttempts')) {
|
||||||
function checkLoginAttempts($ip) {
|
function checkLoginAttempts($ip) {
|
||||||
$attemptsFile = dirname(__DIR__) . '/.login_attempts.json';
|
$attemptsFile = dirname(__DIR__) . '/.login_attempts.json';
|
||||||
|
|
||||||
@@ -178,8 +189,10 @@ function checkLoginAttempts($ip) {
|
|||||||
|
|
||||||
return $ipData['attempts'] < MAX_LOGIN_ATTEMPTS;
|
return $ipData['attempts'] < MAX_LOGIN_ATTEMPTS;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Función para registrar intento de login fallido
|
// Función para registrar intento de login fallido
|
||||||
|
if (!function_exists('recordFailedLogin')) {
|
||||||
function recordFailedLogin($ip) {
|
function recordFailedLogin($ip) {
|
||||||
$attemptsFile = dirname(__DIR__) . '/.login_attempts.json';
|
$attemptsFile = dirname(__DIR__) . '/.login_attempts.json';
|
||||||
|
|
||||||
@@ -201,8 +214,10 @@ function recordFailedLogin($ip) {
|
|||||||
file_put_contents($attemptsFile, json_encode($attempts));
|
file_put_contents($attemptsFile, json_encode($attempts));
|
||||||
writeLog('WARNING', 'Failed login attempt', ['ip' => $ip, 'attempts' => $attempts[$ip]['attempts']]);
|
writeLog('WARNING', 'Failed login attempt', ['ip' => $ip, 'attempts' => $attempts[$ip]['attempts']]);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Función para limpiar intentos de login exitoso
|
// Función para limpiar intentos de login exitoso
|
||||||
|
if (!function_exists('clearLoginAttempts')) {
|
||||||
function clearLoginAttempts($ip) {
|
function clearLoginAttempts($ip) {
|
||||||
$attemptsFile = dirname(__DIR__) . '/.login_attempts.json';
|
$attemptsFile = dirname(__DIR__) . '/.login_attempts.json';
|
||||||
|
|
||||||
@@ -214,6 +229,7 @@ function clearLoginAttempts($ip) {
|
|||||||
|
|
||||||
writeLog('INFO', 'Successful login', ['ip' => $ip]);
|
writeLog('INFO', 'Successful login', ['ip' => $ip]);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Autoloader mejorado
|
// Autoloader mejorado
|
||||||
spl_autoload_register(function ($class) {
|
spl_autoload_register(function ($class) {
|
||||||
@@ -236,6 +252,7 @@ spl_autoload_register(function ($class) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Función para obtener configuración desde base de datos
|
// Función para obtener configuración desde base de datos
|
||||||
|
if (!function_exists('getConfigFromDB')) {
|
||||||
function getConfigFromDB($key, $default = null) {
|
function getConfigFromDB($key, $default = null) {
|
||||||
static $configs = null;
|
static $configs = null;
|
||||||
|
|
||||||
@@ -267,8 +284,10 @@ function getConfigFromDB($key, $default = null) {
|
|||||||
|
|
||||||
return $configs[$key] ?? $default;
|
return $configs[$key] ?? $default;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Función para validar configuración de WhatsApp
|
// Función para validar configuración de WhatsApp
|
||||||
|
if (!function_exists('validateWhatsAppConfig')) {
|
||||||
function validateWhatsAppConfig() {
|
function validateWhatsAppConfig() {
|
||||||
$errors = [];
|
$errors = [];
|
||||||
|
|
||||||
@@ -286,14 +305,17 @@ function validateWhatsAppConfig() {
|
|||||||
|
|
||||||
return $errors;
|
return $errors;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Función para crear directorio si no existe
|
// Función para crear directorio si no existe
|
||||||
|
if (!function_exists('ensureDirectoryExists')) {
|
||||||
function ensureDirectoryExists($path) {
|
function ensureDirectoryExists($path) {
|
||||||
if (!is_dir($path)) {
|
if (!is_dir($path)) {
|
||||||
mkdir($path, 0755, true);
|
mkdir($path, 0755, true);
|
||||||
writeLog('INFO', 'Directory created', ['path' => $path]);
|
writeLog('INFO', 'Directory created', ['path' => $path]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Crear directorios necesarios
|
// Crear directorios necesarios
|
||||||
ensureDirectoryExists(dirname(__DIR__) . '/logs');
|
ensureDirectoryExists(dirname(__DIR__) . '/logs');
|
||||||
|
|||||||
@@ -5,6 +5,15 @@
|
|||||||
* Fecha: 13 de noviembre de 2025
|
* Fecha: 13 de noviembre de 2025
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// Asegurar que las funciones de configuración estén cargadas si este archivo
|
||||||
|
// se incluye directamente sin pasar por `config.php`.
|
||||||
|
if (!function_exists('getWhatsAppConfigFromDB')) {
|
||||||
|
$possible = __DIR__ . '/../config/config.php';
|
||||||
|
if (file_exists($possible)) {
|
||||||
|
require_once $possible;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class WhatsAppService
|
class WhatsAppService
|
||||||
{
|
{
|
||||||
private $token;
|
private $token;
|
||||||
|
|||||||
Reference in New Issue
Block a user