<?php
/**
 * Hardened Custom Login Template - Multi OPAC Style
 * Path: template/default/login_template.inc.php
 */

if (!defined('INDEX_AUTH')) {
    die("can not access this file directly");
}

// -------------------------------------------------------------
// 1. SECURITY HEADERS
// -------------------------------------------------------------
header('X-Frame-Options: SAMEORIGIN'); // Cegah Clickjacking
header('X-Content-Type-Options: nosniff'); // Cegah MIME Sniffing
header('X-XSS-Protection: 1; mode=block'); // Proteksi XSS Browser

// -------------------------------------------------------------
// 2. RATE LIMITER (PROTEKSI BRUTE FORCE Sederhana per IP)
// -------------------------------------------------------------
$client_ip = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
$lockout_key = 'login_attempts_' . md5($client_ip);
$attempts = $_SESSION[$lockout_key] ?? ['count' => 0, 'time' => time()];

// Jika salah login lebih dari 5x, kunci selama 15 menit
if ($attempts['count'] >= 5) {
    if (time() - $attempts['time'] < 900) { // 900 detik = 15 menit
        $remaining = ceil((900 - (time() - $attempts['time'])) / 60);
        http_response_code(429);
        die("<div style='font-family:sans-serif; text-align:center; padding:50px;'>
            <h2 style='color:#dc2626;'>Akses Ditolak Sementara</h2>
            <p>Terlalu banyak percobaan login yang gagal dari IP Anda.</p>
            <p>Silakan coba lagi dalam <b>{$remaining} menit</b>.</p>
        </div>");
    } else {
        // Reset counter jika waktu kunci sudah lewat
        $_SESSION[$lockout_key] = ['count' => 0, 'time' => time()];
    }
}

// Catat percobaan jika ada penolakan login dari SLiMS
if (isset($_GET['wrongpass'])) {
    $_SESSION[$lockout_key]['count'] = ($attempts['count'] ?? 0) + 1;
    $_SESSION[$lockout_key]['time'] = time();
}

// -------------------------------------------------------------
// 3. VISITOR ROUTE
// -------------------------------------------------------------
if (isset($_GET['p']) && $_GET['p'] === 'visitor') {
    $imagesDisk = \SLiMS\Filesystems\Storage::images();
    include "classic.php";
    include "parts/header.php";
    echo $main_content;
    include __DIR__ . "/assets/js/vegas.js.php";
    echo '</body></html>';
    exit;
}

$sysName = $sysconf['library_name'] ?? 'Perpustakaan Unsoed';
$multiOpacUrl = 'index.php?p=multi-opac';
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- ANTI INDEXING: Sembunyikan Halaman Login dari Google & Bot -->
    <meta name="robots" content="noindex, nofollow, noarchive, nosnippet">
    <title>Login Staff / Pustakawan - <?= htmlspecialchars($sysName) ?></title>
    
    <!-- ASET LOKAL TEMPLATE DEFAULT SLIMS -->
    <link rel="stylesheet" href="template/default/assets/css/bootstrap.min.css">
    <link rel="stylesheet" href="template/default/assets/css/font-awesome.min.css">
    
    <!-- FALLBACK CSS NATIVE JIKA BOOTSTRAP TERTENTU TIDAK TERSEDIA -->
    <link rel="stylesheet" href="admin/admin_template/default/css/bootstrap.min.css">
    <link rel="stylesheet" href="admin/admin_template/default/css/font-awesome.min.css">

    <style>
        :root {
            --primary-color: #0f172a;
            --accent-color: #2563eb;
            --accent-hover: #1d4ed8;
            --bg-gradient: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
        }
        body {
            background: var(--bg-gradient);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            margin: 0;
            padding: 20px;
            user-select: none;
        }
        .login-card {
            background: #ffffff;
            border-radius: 16px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.3);
            width: 100%;
            max-width: 420px;
            padding: 36px 32px;
            border: 1px solid rgba(255,255,255,0.1);
        }
        .brand-icon {
            width: 56px;
            height: 56px;
            background: rgba(37, 99, 235, 0.1);
            color: var(--accent-color);
            border-radius: 12px;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            margin-bottom: 16px;
        }
        #loginForm .heading1 {
            font-weight: 600;
            font-size: 0.875rem;
            color: #334155;
            margin-top: 14px;
            margin-bottom: 6px;
        }
        #loginForm input[type="text"],
        #loginForm input[type="password"] {
            width: 100%;
            padding: 11px 16px;
            border: 1.5px solid #e2e8f0;
            border-radius: 10px;
            box-sizing: border-box;
            outline: none;
            font-size: 0.95rem;
            transition: all 0.2s ease;
        }
        #loginForm input[type="text"]:focus,
        #loginForm input[type="password"]:focus {
            border-color: var(--accent-color);
            box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.1);
        }
        #loginForm .marginTop {
            margin-top: 22px;
        }
        #loginForm input[type="submit"].loginButton {
            width: 100%;
            background-color: var(--accent-color);
            color: #ffffff;
            border: none;
            padding: 12px;
            border-radius: 10px;
            font-weight: 600;
            font-size: 1rem;
            cursor: pointer;
            transition: background-color 0.2s ease;
        }
        #loginForm input[type="submit"].loginButton:hover {
            background-color: var(--accent-hover);
        }
        #loginForm input[type="button"].homeButton {
            display: none;
        }
        .remember_forgot {
            margin-bottom: 12px;
            font-size: 0.875rem;
            color: #64748b;
        }
    </style>
</head>
<body>

<div class="login-card">
    <div class="text-center mb-4">
        <div class="brand-icon">
            <i class="fas fa-user-shield"></i>
        </div>
        <h4 class="fw-bold text-dark mb-1">Masuk Pustakawan</h4>
        <p class="text-muted small mb-0"><?= htmlspecialchars($sysName) ?></p>
    </div>

    <!-- CETAK FORM NATIVE SLIMS -->
    <div class="slims-native-form">
        <?= $main_content ?>
    </div>

    <div class="text-center mt-4 pt-3 border-top">
        <a href="<?= $multiOpacUrl ?>" class="text-decoration-none small text-secondary fw-semibold">
            <i class="fas fa-arrow-left me-1"></i> Kembali ke Multi OPAC
        </a>
    </div>
</div>

</body>
</html>