<?php
// Check for files that might be serving as entry points
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Files that directly interact with the database
$databaseFiles = [];

// Files that accept HTTP requests
$requestFiles = [];

// Files that might generate logs with 'cybervax'
$cybervaxSuspects = [];

// Walk through all PHP files in the directory
function scanDirectory($dir) {
    global $databaseFiles, $requestFiles, $cybervaxSuspects;
    
    $files = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS)
    );
    
    foreach ($files as $file) {
        if ($file->getExtension() != 'php') {
            continue;
        }
        
        $path = $file->getPathname();
        $content = file_get_contents($path);
        
        // Check if file interacts with database
        if (strpos($content, 'mysqli_') !== false || 
            strpos($content, '$condb') !== false) {
            $databaseFiles[] = $path;
            
            // Check if this file might be generating cybervax entries
            if (strpos($content, 'cybervax') !== false) {
                $cybervaxSuspects[] = $path;
            }
        }
        
        // Check if file accepts HTTP requests directly
        if (strpos($content, '$_GET') !== false || 
            strpos($content, '$_POST') !== false || 
            strpos($content, '$_REQUEST') !== false ||
            strpos($content, 'php://input') !== false) {
            $requestFiles[] = $path;
        }
    }
}

// Start scanning from current directory
scanDirectory('.');

// Display results
echo "<h1>Potential System Entry Points</h1>";

echo "<h2>Files That Could Be Setting 'cybervax' Default</h2>";
echo "<ul>";
foreach ($cybervaxSuspects as $file) {
    // Exclude files we've already fixed
    $highlight = (strpos($file, 'update_stats.php') === false && 
                  strpos($file, 'gate.php') === false &&
                  strpos($file, 'inspect_logs.php') === false &&
                  strpos($file, 'remove_cybervax.php') === false &&
                  strpos($file, 'check_database.php') === false) ? 
                  'style="color: red; font-weight: bold;"' : '';
    
    echo "<li {$highlight}>{$file} ";
    
    // If this file is a potential culprit, show more details
    if ($highlight) {
        echo "- <a href='javascript:;' onclick='document.getElementById(\"file_{$file}\").style.display=\"block\";'>View Code</a>";
        echo "<div id='file_{$file}' style='display:none;'>";
        echo "<pre>" . htmlspecialchars(file_get_contents($file)) . "</pre>";
        echo "</div>";
    }
    
    echo "</li>";
}
echo "</ul>";

echo "<h2>Files Accepting HTTP Requests</h2>";
echo "<ul>";
foreach ($requestFiles as $file) {
    echo "<li>{$file}</li>";
}
echo "</ul>";

echo "<h2>Files Interacting with Database</h2>";
echo "<ul>";
foreach ($databaseFiles as $file) {
    echo "<li>{$file}</li>";
}
echo "</ul>";

// Look for cron jobs or scheduled tasks
echo "<h2>Potential Scheduled Tasks</h2>";

// Check for cron-related code
$cronSuspects = [];
$files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator('.', RecursiveDirectoryIterator::SKIP_DOTS)
);

foreach ($files as $file) {
    if ($file->getExtension() != 'php') {
        continue;
    }
    
    $path = $file->getPathname();
    $content = file_get_contents($path);
    
    // Look for common scheduled task indicators
    if (strpos($content, 'cron') !== false || 
        strpos($content, 'schedule') !== false ||
        strpos($content, 'task') !== false ||
        strpos($content, 'job') !== false) {
        $cronSuspects[] = $path;
    }
}

echo "<ul>";
if (empty($cronSuspects)) {
    echo "<li>No potential scheduled tasks found.</li>";
} else {
    foreach ($cronSuspects as $file) {
        echo "<li>{$file}</li>";
    }
}
echo "</ul>";

// Check for accidental API endpoint
echo "<h2>Testing Unexpected API Endpoints</h2>";

// Check for any file that might respond to empty POST requests
$apiTestResults = [];
$phpFiles = glob('*.php');

foreach ($phpFiles as $phpFile) {
    // Skip known files
    if (in_array($phpFile, ['identify_entry_points.php', 'check_database.php', 'inspect_logs.php', 'remove_cybervax.php'])) {
        continue;
    }
    
    // Test the file with an empty request
    $apiTestResults[$phpFile] = testEmptyRequest($phpFile);
}

echo "<table border='1' cellpadding='5'>";
echo "<tr><th>File</th><th>Status Code</th><th>Response</th></tr>";
foreach ($apiTestResults as $file => $result) {
    echo "<tr>";
    echo "<td>{$file}</td>";
    echo "<td>{$result['status']}</td>";
    echo "<td>" . substr(htmlspecialchars($result['response']), 0, 100) . 
         (strlen($result['response']) > 100 ? '...' : '') . "</td>";
    echo "</tr>";
}
echo "</table>";

// Function to test if a file responds to empty requests
function testEmptyRequest($file) {
    $url = "http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/{$file}";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    
    $response = curl_exec($ch);
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
    curl_close($ch);
    
    return [
        'status' => $status,
        'response' => $response
    ];
}
?> 