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

echo "<h1>Database Investigation</h1>";

// Check for triggers
echo "<h2>Database Triggers</h2>";
$query = "SHOW TRIGGERS";
$result = mysqli_query($condb, $query);

if ($result) {
    if (mysqli_num_rows($result) > 0) {
        echo "<table border='1' cellpadding='5'>";
        echo "<tr><th>Trigger</th><th>Event</th><th>Table</th><th>Statement</th><th>Timing</th></tr>";
        
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<tr>";
            echo "<td>" . $row['Trigger'] . "</td>";
            echo "<td>" . $row['Event'] . "</td>";
            echo "<td>" . $row['Table'] . "</td>";
            echo "<td>" . $row['Statement'] . "</td>";
            echo "<td>" . $row['Timing'] . "</td>";
            echo "</tr>";
        }
        
        echo "</table>";
    } else {
        echo "<p>No triggers found in the database.</p>";
    }
} else {
    echo "<p>Error checking triggers: " . mysqli_error($condb) . "</p>";
}

// Check logs table structure
echo "<h2>Logs Table Structure</h2>";
$query = "DESCRIBE logs";
$result = mysqli_query($condb, $query);

if ($result) {
    echo "<table border='1' cellpadding='5'>";
    echo "<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th><th>Extra</th></tr>";
    
    while ($row = mysqli_fetch_assoc($result)) {
        echo "<tr>";
        echo "<td>" . $row['Field'] . "</td>";
        echo "<td>" . $row['Type'] . "</td>";
        echo "<td>" . $row['Null'] . "</td>";
        echo "<td>" . $row['Key'] . "</td>";
        echo "<td>" . ($row['Default'] === NULL ? "NULL" : $row['Default']) . "</td>";
        echo "<td>" . $row['Extra'] . "</td>";
        echo "</tr>";
    }
    
    echo "</table>";
} else {
    echo "<p>Error describing logs table: " . mysqli_error($condb) . "</p>";
}

// Check for events
echo "<h2>Database Events</h2>";
$query = "SHOW EVENTS";
$result = mysqli_query($condb, $query);

if ($result) {
    if (mysqli_num_rows($result) > 0) {
        echo "<table border='1' cellpadding='5'>";
        echo "<tr><th>Event</th><th>Type</th><th>Execute At</th><th>Interval Value</th><th>Status</th></tr>";
        
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<tr>";
            echo "<td>" . $row['Name'] . "</td>";
            echo "<td>" . $row['Type'] . "</td>";
            echo "<td>" . $row['Execute at'] . "</td>";
            echo "<td>" . $row['Interval value'] . "</td>";
            echo "<td>" . $row['Status'] . "</td>";
            echo "</tr>";
        }
        
        echo "</table>";
    } else {
        echo "<p>No events found in the database.</p>";
    }
} else {
    echo "<p>Error checking events: " . mysqli_error($condb) . "</p>";
}

// Test username handling in empty requests
echo "<h2>Testing Empty Request Handling</h2>";

// Simulate an empty request directly to update_stats.php
echo "<h3>Testing Direct Call to update_stats.php with Empty Values</h3>";
$test_hwid = "TEST_EMPTY_" . time();
$test_username = "";

echo "<p>Testing with empty username:</p>";
echo "<ul>";
echo "<li>HWID: {$test_hwid}</li>";
echo "<li>Username: [empty string]</li>";
echo "</ul>";

include_once("Panel/update_stats.php");
$stats = updateBotStatistics($test_hwid, $test_username, []);

// Check what was actually inserted
$query = "SELECT * FROM logs WHERE hwid = '{$test_hwid}' LIMIT 1";
$result = mysqli_query($condb, $query);

if ($result && mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
    echo "<p>Result in database:</p>";
    echo "<ul>";
    echo "<li>HWID: {$row['hwid']}</li>";
    echo "<li>Username: {$row['username']}</li>";
    echo "</ul>";
    
    if ($row['username'] !== $test_username && $row['username'] === 'cybervax') {
        echo "<p style='color: red; font-weight: bold;'>ISSUE DETECTED: Empty username was changed to 'cybervax'!</p>";
    }
} else {
    echo "<p>No record was inserted (this is expected if validation is working).</p>";
}

// Check for 'cybervax' in other tables
echo "<h2>Checking Other Tables for 'cybervax'</h2>";
$query = "SHOW TABLES";
$result = mysqli_query($condb, $query);

if ($result) {
    while ($row = mysqli_fetch_row($result)) {
        $table = $row[0];
        echo "<h3>Table: {$table}</h3>";
        
        // Skip logs table as we already know it has cybervax
        if ($table === 'logs') {
            echo "<p>Skipping logs table as it's already known to have 'cybervax' entries.</p>";
            continue;
        }
        
        // Check if table has any string columns that might contain cybervax
        $columns_query = "DESCRIBE {$table}";
        $columns_result = mysqli_query($condb, $columns_query);
        $string_columns = [];
        
        if ($columns_result) {
            while ($column = mysqli_fetch_assoc($columns_result)) {
                if (strpos($column['Type'], 'char') !== false || 
                    strpos($column['Type'], 'text') !== false || 
                    strpos($column['Type'], 'varchar') !== false) {
                    $string_columns[] = $column['Field'];
                }
            }
        }
        
        if (empty($string_columns)) {
            echo "<p>No string columns found in this table.</p>";
            continue;
        }
        
        // Search for cybervax in each column
        foreach ($string_columns as $column) {
            $search_query = "SELECT * FROM {$table} WHERE {$column} = 'cybervax' LIMIT 5";
            $search_result = mysqli_query($condb, $search_query);
            
            if ($search_result && mysqli_num_rows($search_result) > 0) {
                echo "<p>Found 'cybervax' in column '{$column}':</p>";
                $count = mysqli_num_rows($search_result);
                echo "<p>Number of records: {$count}</p>";
                
                // Display up to 5 records
                if ($count > 0) {
                    echo "<table border='1' cellpadding='5'>";
                    echo "<tr>";
                    $first_row = mysqli_fetch_assoc($search_result);
                    foreach (array_keys($first_row) as $key) {
                        echo "<th>{$key}</th>";
                    }
                    echo "</tr>";
                    
                    // Reset result pointer
                    mysqli_data_seek($search_result, 0);
                    
                    while ($record = mysqli_fetch_assoc($search_result)) {
                        echo "<tr>";
                        foreach ($record as $value) {
                            echo "<td>" . htmlspecialchars($value) . "</td>";
                        }
                        echo "</tr>";
                    }
                    echo "</table>";
                }
            } else {
                echo "<p>No 'cybervax' found in column '{$column}'.</p>";
            }
        }
    }
}
?> 