90 lines
2.8 KiB
PHP
90 lines
2.8 KiB
PHP
<?php
|
|
// Controlador raíz: decide qué página mostrar según sesión
|
|
|
|
// Endurecer cookie de sesión similar al login API
|
|
@ini_set('session.use_strict_mode', 1);
|
|
@ini_set('session.cookie_httponly', 1);
|
|
@ini_set('session.cookie_samesite', 'Lax');
|
|
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') {
|
|
@ini_set('session.cookie_secure', 1);
|
|
}
|
|
|
|
if (session_status() !== PHP_SESSION_ACTIVE) {
|
|
session_start();
|
|
}
|
|
|
|
// Render en sitio: cargar parcial para construir $title y $content y luego incluir el layout
|
|
define('RENDER_PARTIAL', true);
|
|
|
|
$isAuth = !empty($_SESSION['user']);
|
|
|
|
// Ruteo simple: primero por querystring (?view=...), si no, por REQUEST_URI (URL amigable)
|
|
if ($isAuth) {
|
|
$view = isset($_GET['view']) ? $_GET['view'] : null;
|
|
if ($view === null) {
|
|
$uriPath = '/';
|
|
if (isset($_SERVER['REQUEST_URI'])) {
|
|
$uriPath = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
|
}
|
|
// Normalizar base (por si la app corre en subdirectorio)
|
|
$scriptDir = rtrim(str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'] ?? '')), '/');
|
|
if ($scriptDir && $scriptDir !== '/' && strpos($uriPath, $scriptDir) === 0) {
|
|
$uriPath = substr($uriPath, strlen($scriptDir));
|
|
}
|
|
$path = trim($uriPath, '/');
|
|
if ($path === '' || $path === 'index.php') {
|
|
$view = 'home';
|
|
} else {
|
|
$first = explode('/', $path)[0];
|
|
$allowed = ['home', 'dashboard', 'puertos', 'flash', 'programacion'];
|
|
if (in_array($first, $allowed, true)) {
|
|
$view = $first;
|
|
}
|
|
}
|
|
}
|
|
if ($view === null) {
|
|
// fallback
|
|
$view = 'home';
|
|
}
|
|
error_log("DEBUG: View resuelta: " . $view);
|
|
switch ($view) {
|
|
case 'puertos':
|
|
$partial = __DIR__ . '/paginas/puertos.php';
|
|
break;
|
|
case 'flash':
|
|
$partial = __DIR__ . '/paginas/flash.php';
|
|
break;
|
|
case 'dashboard':
|
|
$partial = __DIR__ . '/paginas/dashboard.php';
|
|
break;
|
|
case 'nocache':
|
|
$partial = __DIR__ . '/paginas/nocache.php';
|
|
break;
|
|
case 'programacion':
|
|
$partial = __DIR__ . '/paginas/programacion.php';
|
|
break;
|
|
case 'home':
|
|
default:
|
|
$partial = __DIR__ . '/paginas/index.php';
|
|
break;
|
|
}
|
|
$layout = __DIR__ . '/layouts/layout.php';
|
|
} else {
|
|
$partial = __DIR__ . '/paginas/login.php';
|
|
$layout = __DIR__ . '/layouts/layout_login.php';
|
|
}
|
|
|
|
if (!file_exists($partial)) {
|
|
http_response_code(500);
|
|
echo 'Parcial no encontrado';
|
|
exit;
|
|
}
|
|
if (!file_exists($layout)) {
|
|
http_response_code(500);
|
|
echo 'Layout no encontrado';
|
|
exit;
|
|
}
|
|
|
|
require $partial; // define $title y $content
|
|
require $layout; // usa $title y $content
|
|
?>
|