Files
whatsapp/auth_protect.php
2026-03-10 18:42:51 -05:00

69 lines
2.1 KiB
PHP

<?php
/**
* Archivo de protección de autenticación
* Incluir en páginas que requieren login
* Desarrollado por U-Site.app
*/
// Asegurar que config.php esté incluido
if (!function_exists('isUserLoggedIn')) {
require_once __DIR__ . '/config/config.php';
}
// Verificar si la instalación está completada
if (!isInstallationCompleted()) {
header('Location: install.php');
exit('Sistema no instalado. <a href="install.php">Instalar ahora</a>');
}
// Verificar autenticación - redirigir al login si no está logueado
if (!isUserLoggedIn()) {
// Si es una petición AJAX, devolver JSON
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
http_response_code(401);
header('Content-Type: application/json; charset=utf-8');
echo json_encode([
'success' => false,
'error' => 'Sesión expirada. Por favor, inicie sesión.',
'redirect' => 'login.php'
]);
exit;
}
// Para páginas normales, redirigir al login
header('Location: login.php');
exit('Acceso denegado. <a href="login.php">Iniciar sesión</a>');
}
// Enfermeros solo pueden ver su portal, no el panel admin
if (isEnfermero()) {
header('Location: enfermero_portal.php');
exit;
}
// Verificar timeout de sesión (ya se hace en config.php, pero por seguridad)
if (isset($_SESSION['last_activity']) && (time() - $_SESSION['last_activity'] > SESSION_TIMEOUT)) {
session_destroy();
session_start();
// Si es AJAX
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
http_response_code(401);
header('Content-Type: application/json; charset=utf-8');
echo json_encode([
'success' => false,
'error' => 'Sesión expirada por inactividad.',
'redirect' => 'login.php'
]);
exit;
}
header('Location: login.php?session_expired=1');
exit;
}
// Actualizar última actividad
$_SESSION['last_activity'] = time();
?>