<?php
/**
 * Autenticacion - auth.php
 * =========================
 * Protege el acceso al sistema de backup con clave.
 * La clave se lee de password.txt (primera linea).
 * Incluir al inicio de cada pagina protegida.
 */

session_start();

define('PASSWORD_FILE', __DIR__ . '/password.txt');

// Cargar clave desde archivo
function getPassword() {
    if (!file_exists(PASSWORD_FILE)) return null;
    return trim(file_get_contents(PASSWORD_FILE));
}

// Cerrar sesion
if (isset($_GET['logout'])) {
    session_destroy();
    header('Location: estado.php');
    exit;
}

// Verificar login
if (isset($_POST['clave'])) {
    $clave = trim($_POST['clave']);
    $correcta = getPassword();
    if ($correcta !== null && $clave === $correcta) {
        $_SESSION['backup_auth'] = true;
        header('Location: ' . $_SERVER['PHP_SELF'] . (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] ? '?' . $_SERVER['QUERY_STRING'] : ''));
        exit;
    } else {
        $authError = 'Clave incorrecta';
    }
}

// Si no esta autenticado, mostrar formulario de login
if (empty($_SESSION['backup_auth'])) {
    ?><!DOCTYPE html>
    <html lang="es">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Acceso - Backup SIE</title>
        <style>
            * { margin: 0; padding: 0; box-sizing: border-box; }
            body { font-family: 'Segoe UI', Tahoma, sans-serif; background: #1a1a2e; color: #e0e0e0; display: flex; align-items: center; justify-content: center; min-height: 100vh; }
            .login-box { background: #2a2a4a; padding: 30px; border-radius: 12px; border: 2px solid #fdcb6e; width: 350px; text-align: center; }
            .login-box h1 { color: #fdcb6e; font-size: 1.3em; margin-bottom: 5px; }
            .login-box p { color: #888; font-size: 0.85em; margin-bottom: 20px; }
            .login-box input[type="password"] { width: 100%; padding: 10px 14px; border: 2px solid #444; border-radius: 6px; background: #1a1a2e; color: #e0e0e0; font-size: 1em; margin-bottom: 12px; outline: none; }
            .login-box input[type="password"]:focus { border-color: #fdcb6e; }
            .login-box button { width: 100%; padding: 10px; border: none; border-radius: 6px; background: linear-gradient(90deg, #fdcb6e, #f39c12); color: #000; font-weight: bold; font-size: 1em; cursor: pointer; }
            .login-box button:hover { opacity: 0.9; }
            .error { color: #ff6b6b; font-size: 0.85em; margin-bottom: 10px; }
        </style>
    </head>
    <body>
        <div class="login-box">
            <h1>Backup SIE</h1>
            <p>Ingrese la clave de acceso</p>
            <?php if (isset($authError)): ?>
                <div class="error"><?php echo htmlspecialchars($authError); ?></div>
            <?php endif; ?>
            <form method="POST">
                <input type="password" name="clave" placeholder="Clave de acceso" autofocus required>
                <button type="submit">Ingresar</button>
            </form>
        </div>
    </body>
    </html>
    <?php
    exit;
}
