<?php
/**
 * NYSTRIX SYSTEM - DECRYPTOR TOOL
 * VI Camporee 2026 Admin Dash
 */

require_once 'SecureEmailStorage.php';

// Same key used in savemail.php
$encryptionKey = 'uP7$zQ#3vF!AxLr9yTgK2Nw&EbM@Jh84DVm!ZcXsRq1^Yp6WLoBt*GjU5HdCqn';

// --- SECURITY LAYER: TOKEN + IP LOCK ---
$adminToken = 'NYSTRIX_SECURE_ACCESS_2026'; // <--- TU TOKEN SECRETO
$allowedIP  = '127.0.0.1'; // <--- CAMBIA ESTO POR TU IP PÚBLICA (Usa '::1' o '127.0.0.1' para local)

$clientIP = $_SERVER['REMOTE_ADDR'];

if (!isset($_GET['access']) || $_GET['access'] !== $adminToken || $clientIP !== $allowedIP) {
    http_response_code(403);
    // Log intent to a separate file for monitoring
    error_log("[NYSTRIX SECURITY] Intento fallido desde IP: $clientIP - Token: " . ($_GET['access'] ?? 'N/A'));
    die("<h1 style='color:#d4af37; background:#0a0605; height:100vh; display:flex; flex-direction:column; align-items:center; justify-content:center; font-family:sans-serif;'>
        <span>[ PROTOCOLO NYSTRIX ]</span>
        <span style='font-size:12px; margin-top:20px; color:#555;'>TU IP ($clientIP) NO TIENE PERMISOS DE DECODIFICACIÓN</span>
    </h1>");
}
// ----------------------------------------

$storage = new SecureEmailStorage(__DIR__ . '/securemail.db', $encryptionKey);

$registries = $storage->getAllRegistries();
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Nystrix Admin - Decodificador de Manuscritos</title>
    <link href="https://fonts.googleapis.com/css2?family=Cormorant+SC:wght@700&family=Outfit:wght@300;600&family=JetBrains+Mono:wght@400&display=swap" rel="stylesheet">
    <style>
        :root {
            --gold: #d4af37;
            --black: #0a0605;
            --white: #f0ebd8;
        }
        body {
            background: var(--black);
            color: var(--white);
            font-family: 'Outfit', sans-serif;
            padding: 40px;
        }
        h1 {
            font-family: 'Cormorant SC', serif;
            color: var(--gold);
            text-transform: uppercase;
            letter-spacing: 4px;
            border-bottom: 1px solid var(--gold);
            padding-bottom: 10px;
            margin-bottom: 30px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            background: rgba(255,255,255,0.03);
            font-size: 0.9rem;
        }
        th {
            background: var(--gold);
            color: var(--black);
            text-transform: uppercase;
            padding: 15px;
            text-align: left;
            font-weight: 800;
        }
        td {
            padding: 12px 15px;
            border-bottom: 1px solid rgba(212, 175, 55, 0.2);
            font-family: 'Outfit', sans-serif;
        }
        .mono {
            font-family: 'JetBrains Mono', monospace;
            font-size: 0.75rem;
            color: #aaa;
        }
        .count {
            color: var(--gold);
            font-weight: 800;
        }
        .empty {
            padding: 50px;
            text-align: center;
            opacity: 0.5;
            font-style: italic;
        }
    </style>
</head>
<body>

    <h1>DECODIFICADOR DE MANUSCRITOS NYSTRIX</h1>

    <?php if (empty($registries)): ?>
        <div class="empty">No hay registros encriptados en el sistema.</div>
    <?php else: ?>
        <table>
            <thead>
                <tr>
                    <th>Nombre</th>
                    <th>Email</th>
                    <th>IP</th>
                    <th>SO</th>
                    <th>Descargas</th>
                    <th>Fecha Registro</th>
                    <th>Último Acceso</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($registries as $r): ?>
                <tr>
                    <td><strong><?php echo htmlspecialchars($r['nombre']); ?></strong></td>
                    <td><?php echo htmlspecialchars($r['email']); ?></td>
                    <td class="mono"><?php echo htmlspecialchars($r['ip']); ?></td>
                    <td><?php echo htmlspecialchars($r['metadata']['os'] ?? 'Unknown'); ?></td>
                    <td class="count"><?php echo $r['countdownload']; ?></td>
                    <td class="mono"><?php echo $r['fecha']; ?></td>
                    <td class="mono"><?php echo $r['last_access_date']; ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>

</body>
</html>
