<?php

// Function to scan directory and its subdirectories
function scanDirectory($dir) {
    $files = [];
    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));

    foreach ($iterator as $fileinfo) {
        if ($fileinfo->isFile()) {
            $extension = pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION);
            if (in_array($extension, ['html', 'php', 'js','css'])) {
                $files[] = $fileinfo->getPathname();
            }
        }
    }

    return $files;
}

// Function to remove malicious script from a file
function cleanFile($file) {
    $content = file_get_contents($file);
    $maliciousScript = '/<script>function _0x3023\(.*?<\/script>/s';

    if (preg_match($maliciousScript, $content)) {
        $cleanContent = preg_replace($maliciousScript, '', $content);
        file_put_contents($file, $cleanContent);
        echo "Cleaned: $file\n"."<br/>";
    } else {
        echo "No malicious script found in: $file\n"."<br/>";
    }
}

// Directory to scan
$directoryToScan = './';

// Get all relevant files in the directory and subdirectories
$files = scanDirectory($directoryToScan);

// Clean each file
foreach ($files as $file) {
    cleanFile($file);
}

echo "Cleaning completed.\n";
