<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Bot Check</title>
  <meta name="robots" content="noindex">
  <style>
    body {
      font-family: sans-serif;
      display: flex;
      height: 100vh;
      align-items: center;
      justify-content: center;
      background: #f5f5f5;
    }

    #wrap {
      text-align: center;
    }

    button {
      padding: 0.75rem 1.25rem;
      font-size: 1rem;
      cursor: pointer;
    }

    #status {
      margin-top: 1rem;
      font-size: 0.9rem;
      color: #555;
    }

    .throbber {
      display: inline-block;
      width: 1em;
      height: 1em;
      border: 2px solid rgba(0, 0, 0, 0.1);
      border-left-color: #555;
      border-radius: 50%;
      animation: spin 1s linear infinite;
      vertical-align: middle;
      margin-right: 0.5rem;
    }

    @keyframes spin {
      to {
        transform: rotate(360deg);
      }
    }
  </style>
  <script>
    const translations = {
      en: {
        title: 'Bot Check',
        h1: 'Verification Required',
        p: 'Press the button to continue.',
        btn: 'I am human',
        verifying: 'Verifying...',
        redirecting: 'Redirecting...'
      },
      de: {
        title: 'Bot-Check',
        h1: 'Überprüfung erforderlich',
        p: 'Drücken Sie den Knopf, um fortzufahren.',
        btn: 'Ich bin ein Mensch',
        verifying: 'Überprüfung...',
        redirecting: 'Weiterleitung...'
      }
    };

    let currentLang = 'en';

    function startChallenge() {
      const btn = document.getElementById('challengeBtn');
      if (btn.disabled) {
        return;
      }
      const statusMsg = document.getElementById('status-message');
      btn.disabled = true;

      setTimeout(function () {
        const params = new URLSearchParams(window.location.search);
        const target = params.get('target');
        const token = params.get('token');

        // Set a cookie valid for 1 year with the dynamic token.
        if (token) {
          document.cookie = 'bot_verified=' + token + '; Max-Age=' + (60 * 60 * 24 * 365) + '; Path=/; SameSite=Lax';
        }

        statusMsg.textContent = translations[currentLang].redirecting;

        // Prevent Open Redirect: Ensure target starts with / and is not protocol-relative (//)
        if (target && target.startsWith('/') && !target.startsWith('//')) {
          window.location.replace(target);
        } else {
          window.location.replace('/');
        }
      }, 1000);
    }
    document.addEventListener('DOMContentLoaded', function () {
      const userLang = navigator.language || navigator.userLanguage;
      if (userLang && userLang.startsWith('de')) {
        currentLang = 'de';
        document.documentElement.lang = 'de';
        document.title = translations.de.title;
        document.querySelector('h1').textContent = translations.de.h1;
        document.querySelector('p').textContent = translations.de.p;
        document.getElementById('challengeBtn').textContent = translations.de.btn;
        document.getElementById('status-message').textContent = translations.de.verifying;
      }

      document.getElementById('challengeBtn').addEventListener('click', startChallenge);
      setTimeout(startChallenge, 1000);
    });
  </script>
</head>

<body>
  <div id="wrap">
    <h1>Verification Required</h1>
    <p>Press the button to continue.</p>
    <button id="challengeBtn" type="button">I am human</button>
    <div id="status" aria-live="polite">
      <span class="throbber"></span>
      <span id="status-message">Verifying...</span>
    </div>
  </div>
</body>

</html>
