Sintaxis
This commit is contained in:
@@ -4,8 +4,8 @@ require_once __DIR__ . '/../../config/database.php';
|
||||
require_once __DIR__ . '/../helpers/crypto.php';
|
||||
require_once __DIR__ . '/../helpers/bitacoras.php';
|
||||
require_once __DIR__ . '/../helpers/env.php';
|
||||
|
||||
require_once __DIR__ . '/../../vendor/autoload.php';
|
||||
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
use PHPMailer\PHPMailer\Exception;
|
||||
|
||||
@@ -33,9 +33,7 @@ function validar()
|
||||
$emailEncrypted = encrypt($email);
|
||||
|
||||
// Obtenemos el usuario incluyendo el campo dos_factores
|
||||
$sql = "SELECT id_usuario, nombre, email, password_hash, tipo_usuario, activo, dos_factores
|
||||
FROM usuarios_sistema WHERE email = ?";
|
||||
|
||||
$sql = "SELECT id_usuario, nombre, email, password_hash, tipo_usuario, activo, dos_factores FROM usuarios_sistema WHERE email = ?";
|
||||
$stmt = sqlsrv_query($conn, $sql, [$emailEncrypted]);
|
||||
|
||||
if ($stmt && $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
|
||||
@@ -225,9 +223,7 @@ function confirmarAcceso()
|
||||
exit;
|
||||
}
|
||||
|
||||
$sql = "SELECT codigo, expiracion FROM verificaciones
|
||||
WHERE id_usuario = ? AND codigo = ?
|
||||
ORDER BY id DESC";
|
||||
$sql = "SELECT codigo, expiracion FROM verificaciones WHERE id_usuario = ? AND codigo = ? ORDER BY id DESC";
|
||||
$stmt = sqlsrv_query($conn, $sql, [$usuarioId, $codigoIngresado]);
|
||||
|
||||
if (!$stmt || !($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))) {
|
||||
@@ -282,7 +278,9 @@ function recuperar()
|
||||
function enviarCodigo()
|
||||
{
|
||||
header('Content-Type: application/json');
|
||||
|
||||
$conn = getConnection();
|
||||
|
||||
if (!$conn) {
|
||||
echo json_encode(['success' => false, 'message' => '❌ Error de conexión con la base de datos.']);
|
||||
exit;
|
||||
@@ -314,8 +312,7 @@ function enviarCodigo()
|
||||
}
|
||||
|
||||
$now = (new DateTime())->format('Y-m-d H:i:s');
|
||||
$sqlCheck = "SELECT COUNT(*) AS total FROM recuperacion_password
|
||||
WHERE email = ? AND estatus = 0 AND expiracion > ?";
|
||||
$sqlCheck = "SELECT COUNT(*) AS total FROM recuperacion_password WHERE email = ? AND estatus = 0 AND expiracion > ?";
|
||||
$checkStmt = sqlsrv_query($conn, $sqlCheck, [$emailEncrypted, $now]);
|
||||
$checkRow = sqlsrv_fetch_array($checkStmt, SQLSRV_FETCH_ASSOC);
|
||||
|
||||
@@ -339,7 +336,7 @@ function enviarCodigo()
|
||||
|
||||
// ✅ AGREGAR ESTAS LÍNEAS PARA ESTABLECER LA SESIÓN
|
||||
$_SESSION['email_recuperacion'] = $email;
|
||||
$_SESSION['id_usuario'] = $id_usuario;
|
||||
$_SESSION['id_usuario'] = $id_usuario;
|
||||
|
||||
try {
|
||||
$mail = new PHPMailer(true);
|
||||
@@ -458,6 +455,7 @@ function reenviarCodigo()
|
||||
}
|
||||
|
||||
$conn = getConnection();
|
||||
|
||||
if (!$conn) {
|
||||
echo json_encode(['success' => false, 'message' => '❌ Error de conexión con la base de datos.']);
|
||||
exit;
|
||||
@@ -467,10 +465,8 @@ function reenviarCodigo()
|
||||
$emailEncrypted = encrypt($_SESSION['email_recuperacion']);
|
||||
|
||||
// Obtener el último código activo de recuperación
|
||||
$now = (new DateTime())->format('Y-m-d H:i:s');
|
||||
$sql = "SELECT TOP 1 codigo FROM recuperacion_password
|
||||
WHERE email = ? AND estatus = 0 AND expiracion > ?
|
||||
ORDER BY expiracion DESC";
|
||||
$now = (new DateTime())->format('Y-m-d H:i:s');
|
||||
$sql = "SELECT TOP 1 codigo FROM recuperacion_password WHERE email = ? AND estatus = 0 AND expiracion > ? ORDER BY expiracion DESC";
|
||||
$stmt = sqlsrv_query($conn, $sql, [$emailEncrypted, $now]);
|
||||
|
||||
if (!$stmt || !($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))) {
|
||||
@@ -530,8 +526,7 @@ function verificarCodigo()
|
||||
$emailEncrypted = encrypt($email);
|
||||
|
||||
// Consulta el último código válido para ese email
|
||||
$sql = "SELECT TOP 1 id, codigo, expiracion FROM recuperacion_password
|
||||
WHERE email = ? AND estatus = 0 ORDER BY id DESC";
|
||||
$sql = "SELECT TOP 1 id, codigo, expiracion FROM recuperacion_password WHERE email = ? AND estatus = 0 ORDER BY id DESC";
|
||||
$stmt = sqlsrv_query($conn, $sql, [$emailEncrypted]);
|
||||
|
||||
if ($stmt === false) {
|
||||
@@ -567,13 +562,13 @@ function verificarCodigo()
|
||||
// Verificar si se han excedido los intentos
|
||||
if ($_SESSION['intentos_codigo'] >= 5) {
|
||||
// Bloquear el código
|
||||
$sqlUpdate = "UPDATE recuperacion_password SET estatus = 1 WHERE id = ?";
|
||||
$sqlUpdate = "UPDATE recuperacion_password SET estatus = 1 WHERE id = ?";
|
||||
$stmtUpdate = sqlsrv_query($conn, $sqlUpdate, [$row['id']]);
|
||||
if ($stmtUpdate) sqlsrv_free_stmt($stmtUpdate);
|
||||
|
||||
// Bloquear la cuenta del usuario
|
||||
$sqlBloquearUsuario = "UPDATE usuarios_sistema SET activo = 0 WHERE email = ?";
|
||||
$stmtBloqueo = sqlsrv_query($conn, $sqlBloquearUsuario, [$emailEncrypted]);
|
||||
$stmtBloqueo = sqlsrv_query($conn, $sqlBloquearUsuario, [$emailEncrypted]);
|
||||
if ($stmtBloqueo) sqlsrv_free_stmt($stmtBloqueo);
|
||||
|
||||
// Enviar notificación
|
||||
@@ -639,25 +634,25 @@ function verificarCodigo()
|
||||
function enviarNotificacionIntentosExcedidos($email)
|
||||
{
|
||||
$conn = getConnection();
|
||||
|
||||
if (!$conn) {
|
||||
throw new Exception("No se pudo conectar a la base de datos");
|
||||
}
|
||||
|
||||
// Buscar información del usuario por email
|
||||
$emailEncrypted = encrypt($email);
|
||||
$sqlNotif = "
|
||||
SELECT
|
||||
u.nombre,
|
||||
u.email,
|
||||
u.notificaciones,
|
||||
u.notificaciones_extra,
|
||||
COALESCE(p.intentos_fallidos, 0) as intentos_fallidos,
|
||||
ce.correo as correo_extra
|
||||
FROM usuarios_sistema u
|
||||
LEFT JOIN preferencias_notificaciones_usuario p ON u.id_usuario = p.id_usuario
|
||||
LEFT JOIN correo_extra ce ON u.id_usuario = ce.id_usuario
|
||||
WHERE u.email = ?";
|
||||
|
||||
$sqlNotif = "SELECT
|
||||
u.nombre, u.email, u.notificaciones, u.notificaciones_extra,
|
||||
COALESCE(p.intentos_fallidos, 0) as intentos_fallidos,
|
||||
ce.correo as correo_extra
|
||||
FROM usuarios_sistema u
|
||||
LEFT JOIN preferencias_notificaciones_usuario p
|
||||
ON u.id_usuario = p.id_usuario
|
||||
LEFT JOIN correo_extra ce
|
||||
ON u.id_usuario = ce.id_usuario
|
||||
WHERE u.email = ?
|
||||
";
|
||||
$stmtUsuario = sqlsrv_query($conn, $sqlNotif, [$emailEncrypted]);
|
||||
|
||||
if ($stmtUsuario === false) {
|
||||
@@ -687,7 +682,7 @@ function enviarNotificacionIntentosExcedidos($email)
|
||||
'email' => $usuario['email'],
|
||||
'nombre' => $usuario['nombre'],
|
||||
'fecha_hora' => date('Y-m-d H:i:s'),
|
||||
'ip' => $_SERVER['REMOTE_ADDR'] ?? 'Desconocida',
|
||||
'ip' => $_SERVER['REMOTE_ADDR'] ?? 'Desconocida',
|
||||
'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? 'Desconocido'
|
||||
];
|
||||
|
||||
@@ -822,23 +817,21 @@ function enviarNotificacionSeguridadIntentos($emailDestino, $nombreUsuario, $dat
|
||||
function enviarNotificacionCuentaBloqueada($email)
|
||||
{
|
||||
$conn = getConnection();
|
||||
|
||||
if (!$conn) {
|
||||
throw new Exception("No se pudo conectar a la base de datos");
|
||||
}
|
||||
|
||||
// Buscar información del usuario por email (incluyendo correo extra)
|
||||
$emailEncrypted = encrypt($email);
|
||||
$sqlNotif = "
|
||||
SELECT
|
||||
u.nombre,
|
||||
u.email,
|
||||
u.notificaciones,
|
||||
u.notificaciones_extra,
|
||||
ce.correo as correo_extra
|
||||
FROM usuarios_sistema u
|
||||
LEFT JOIN correo_extra ce ON u.id_usuario = ce.id_usuario
|
||||
WHERE u.email = ?";
|
||||
|
||||
$sqlNotif = "SELECT
|
||||
u.nombre, u.email, u.notificaciones, u.notificaciones_extra,
|
||||
ce.correo as correo_extra
|
||||
FROM usuarios_sistema u
|
||||
LEFT JOIN correo_extra ce
|
||||
ON u.id_usuario = ce.id_usuario
|
||||
WHERE u.email = ?
|
||||
";
|
||||
$stmtUsuario = sqlsrv_query($conn, $sqlNotif, [$emailEncrypted]);
|
||||
|
||||
if ($stmtUsuario === false) {
|
||||
@@ -854,7 +847,7 @@ function enviarNotificacionCuentaBloqueada($email)
|
||||
}
|
||||
|
||||
// 🔐 Desencriptar datos sensibles
|
||||
$usuario['email'] = decrypt($usuario['email']);
|
||||
$usuario['email'] = decrypt($usuario['email']);
|
||||
$usuario['nombre'] = decrypt($usuario['nombre']);
|
||||
|
||||
// Preparar datos para la notificación
|
||||
@@ -862,7 +855,7 @@ function enviarNotificacionCuentaBloqueada($email)
|
||||
'email' => $usuario['email'],
|
||||
'nombre' => $usuario['nombre'],
|
||||
'fecha_hora' => date('Y-m-d H:i:s'),
|
||||
'ip' => $_SERVER['REMOTE_ADDR'] ?? 'Desconocida',
|
||||
'ip' => $_SERVER['REMOTE_ADDR'] ?? 'Desconocida',
|
||||
'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? 'Desconocido'
|
||||
];
|
||||
|
||||
@@ -1019,8 +1012,9 @@ function cambiarPassword()
|
||||
return;
|
||||
}
|
||||
|
||||
$conn = getConnection();
|
||||
$password = $_POST['password'] ?? '';
|
||||
$conn = getConnection();
|
||||
|
||||
$password = $_POST['password'] ?? '';
|
||||
$confirmar = $_POST['confirmar'] ?? '';
|
||||
|
||||
// Validaciones
|
||||
|
||||
Reference in New Issue
Block a user