<?php
include("config.php");

// This script helps diagnose issues with usernames in the logs table

echo "<h1>Logs Table Inspection</h1>";

// Check for cybervax entries
$cybervax_query = "SELECT COUNT(*) as count FROM logs WHERE username = 'cybervax'";
$result = mysqli_query($condb, $cybervax_query);
$row = mysqli_fetch_assoc($result);
$cybervax_count = $row['count'];

echo "<h2>Current Status</h2>";
echo "<p>Entries with username 'cybervax': <strong>{$cybervax_count}</strong></p>";

// Get most recent entries
$recent_query = "SELECT * FROM logs ORDER BY last_updated DESC LIMIT 20";
$result = mysqli_query($condb, $recent_query);

echo "<h2>20 Most Recent Entries</h2>";
echo "<table border='1' cellpadding='5' style='border-collapse: collapse;'>";
echo "<tr>
        <th>ID</th>
        <th>HWID</th>
        <th>Username</th>
        <th>IP</th>
        <th>Country</th>
        <th>Last Updated</th>
      </tr>";

while ($row = mysqli_fetch_assoc($result)) {
    $bgColor = ($row['username'] == 'cybervax') ? 'style="background-color: #ffcccc;"' : '';
    
    echo "<tr {$bgColor}>
            <td>{$row['id_log']}</td>
            <td>{$row['hwid']}</td>
            <td><strong>{$row['username']}</strong></td>
            <td>{$row['IP']}</td>
            <td>{$row['Country']}</td>
            <td>{$row['last_updated']}</td>
          </tr>";
}

echo "</table>";

// Check for any other scripts that might modify the logs table
$filesWithLogsTable = [];
$dir = new RecursiveDirectoryIterator('.');
$ite = new RecursiveIteratorIterator($dir);
$phpFiles = new RegexIterator($ite, '/\.php$/', RegexIterator::GET_MATCH);

foreach ($phpFiles as $file) {
    $path = $file[0];
    $content = file_get_contents($path);
    
    // Check if file contains "INSERT INTO logs" or "UPDATE logs"
    if (strpos($content, "INSERT INTO logs") !== false || 
        strpos($content, "UPDATE logs") !== false) {
        $filesWithLogsTable[] = $path;
    }
}

echo "<h2>Files that Modify the Logs Table</h2>";
echo "<ul>";
foreach ($filesWithLogsTable as $file) {
    echo "<li>{$file}</li>";
}
echo "</ul>";

// Look for any hardcoded 'cybervax' in the codebase
$filesWithCybervax = [];
foreach ($phpFiles as $file) {
    $path = $file[0];
    $content = file_get_contents($path);
    
    if (strpos($content, "cybervax") !== false) {
        $filesWithCybervax[] = $path;
    }
}

echo "<h2>Files Containing 'cybervax'</h2>";
echo "<ul>";
foreach ($filesWithCybervax as $file) {
    echo "<li>{$file}</li>";
}
echo "</ul>";

// Test inserting an entry directly
if (isset($_GET['test_insert'])) {
    $test_hwid = "TEST_" . md5(time());
    $test_username = "test_user_" . time();
    
    echo "<h2>Test Insertion</h2>";
    echo "<p>Testing direct insertion with:</p>";
    echo "<ul>";
    echo "<li>HWID: {$test_hwid}</li>";
    echo "<li>Username: {$test_username}</li>";
    echo "</ul>";
    
    $insert_query = "INSERT INTO logs (hwid, username, last_updated) VALUES (?, ?, NOW())";
    $stmt = mysqli_prepare($condb, $insert_query);
    mysqli_stmt_bind_param($stmt, "ss", $test_hwid, $test_username);
    $result = mysqli_stmt_execute($stmt);
    
    if ($result) {
        $inserted_id = mysqli_insert_id($condb);
        echo "<p>Insertion successful! ID: {$inserted_id}</p>";
        
        // Verify what was inserted
        $verify_query = "SELECT username FROM logs WHERE id_log = ?";
        $stmt = mysqli_prepare($condb, $verify_query);
        mysqli_stmt_bind_param($stmt, "i", $inserted_id);
        mysqli_stmt_execute($stmt);
        $result = mysqli_stmt_get_result($stmt);
        
        if ($row = mysqli_fetch_assoc($result)) {
            echo "<p>Verified username in database: '{$row['username']}'</p>";
            
            if ($row['username'] !== $test_username) {
                echo "<p style='color: red;'><strong>WARNING:</strong> Username was changed! Expected '{$test_username}' but got '{$row['username']}'</p>";
                echo "<p>This suggests there might be a database trigger or event changing usernames automatically.</p>";
            }
        }
    } else {
        echo "<p>Insertion failed: " . mysqli_error($condb) . "</p>";
    }
}

echo "<p><a href='?test_insert=1'>Run Test Insertion</a> | <a href='remove_cybervax.php'>Run Cleanup Script</a></p>";
?> 