<?php
// Include database connection
include("config.php");

// Are we running from command line?
$isCLI = (php_sapi_name() === 'cli');

if ($isCLI) {
    // Skip HTML output for CLI
    // Always confirm for CLI execution
    $confirmed = true;
    echo "Running in CLI mode\n";
} else {
    // Set a proper content type for web output
    header('Content-Type: text/html; charset=utf-8');
    
    echo "<h1>Cybervax Replacement Utility</h1>";
    echo "<p>This script will replace all username 'cybervax' entries in the logs table with a better system identifier.</p>";
    
    // Check if we're ready to proceed
    $confirmed = isset($_GET['confirm']) && $_GET['confirm'] === '1';
}

// Count existing 'cybervax' entries
$countQuery = "SELECT COUNT(*) as count FROM logs WHERE username = 'cybervax'";
$result = mysqli_query($condb, $countQuery);
$row = mysqli_fetch_assoc($result);
$cybervaxCount = $row['count'];

if ($isCLI) {
    echo "Current 'cybervax' entries found: {$cybervaxCount}\n";
} else {
    echo "<p>Current 'cybervax' entries found: <strong>{$cybervaxCount}</strong></p>";
}

if ($cybervaxCount === 0) {
    if ($isCLI) {
        echo "No 'cybervax' entries found. No action needed.\n";
    } else {
        echo "<p>No 'cybervax' entries found. No action needed.</p>";
    }
    exit;
}

if (!$confirmed && !$isCLI) {
    // Show confirmation page
    echo "<p>This will replace {$cybervaxCount} entries with username 'cybervax' to 'system_unidentified_user'.</p>";
    echo "<p style='color: red;'><strong>Warning:</strong> This action cannot be undone!</p>";
    echo "<p><a href='?confirm=1' style='background-color: #4CAF50; color: white; padding: 10px 15px; text-decoration: none; border-radius: 4px;'>Proceed with Replacement</a></p>";
} else {
    // Perform the replacement
    if ($isCLI) {
        echo "Performing Replacement...\n";
    } else {
        echo "<h2>Performing Replacement...</h2>";
    }
    
    // Start transaction
    mysqli_begin_transaction($condb);
    
    try {
        // Create a better system identifier
        $newUsername = "system_unidentified_user";
        
        // Update all 'cybervax' entries
        $updateQuery = "UPDATE logs SET username = ? WHERE username = 'cybervax'";
        $stmt = mysqli_prepare($condb, $updateQuery);
        mysqli_stmt_bind_param($stmt, "s", $newUsername);
        $result = mysqli_stmt_execute($stmt);
        
        if ($result) {
            $affectedRows = mysqli_stmt_affected_rows($stmt);
            mysqli_commit($condb);
            
            if ($isCLI) {
                echo "SUCCESS! Replaced {$affectedRows} 'cybervax' entries with '{$newUsername}'.\n";
            } else {
                echo "<p style='color: green;'><strong>Success!</strong> Replaced {$affectedRows} 'cybervax' entries with '{$newUsername}'.</p>";
            }
            
            // Verify the change
            $verifyQuery = "SELECT COUNT(*) as count FROM logs WHERE username = 'cybervax'";
            $result = mysqli_query($condb, $verifyQuery);
            $row = mysqli_fetch_assoc($result);
            $remainingCount = $row['count'];
            
            if ($remainingCount === 0) {
                if ($isCLI) {
                    echo "Verification successful. No 'cybervax' entries remain in the database.\n";
                } else {
                    echo "<p>Verification successful. No 'cybervax' entries remain in the database.</p>";
                }
            } else {
                if ($isCLI) {
                    echo "Warning: {$remainingCount} 'cybervax' entries still exist in the database. This could indicate an issue with the update query.\n";
                } else {
                    echo "<p style='color: orange;'><strong>Warning:</strong> {$remainingCount} 'cybervax' entries still exist in the database. This could indicate an issue with the update query.</p>";
                }
            }
        } else {
            mysqli_rollback($condb);
            if ($isCLI) {
                echo "Error: Failed to update entries. " . mysqli_error($condb) . "\n";
            } else {
                echo "<p style='color: red;'><strong>Error:</strong> Failed to update entries. " . mysqli_error($condb) . "</p>";
            }
        }
    } catch (Exception $e) {
        mysqli_rollback($condb);
        if ($isCLI) {
            echo "Error: " . $e->getMessage() . "\n";
        } else {
            echo "<p style='color: red;'><strong>Error:</strong> " . $e->getMessage() . "</p>";
        }
    }
    
    if (!$isCLI) {
        echo "<p><a href='inspect_logs.php' style='background-color: #2196F3; color: white; padding: 10px 15px; text-decoration: none; border-radius: 4px;'>View Logs Table</a></p>";
    }
}
?> 