File: /home/igniwute/ceylonaqua.com/wordpress_cleanup_cron.php
<?php
/**
* WordPress root cleanup cron script
* Clean root files that are not part of WordPress
*/
// ----------------- CONFIG -----------------
$root = __DIR__;
$quarantineDir = $root . DIRECTORY_SEPARATOR . 'quarantine_for_cleanup';
$logFile = $root . DIRECTORY_SEPARATOR . 'cleanup.log';
$dryRun = true;
// Whitelist files/folders
$whitelist = [
'wp-admin',
'wp-content',
'wp-includes',
'.htaccess',
'error_log',
'index.php',
'license.txt',
'readme.html',
'wp-activate.php',
'wp-blog-header.php',
'wp-comments-post.php',
'wp-config-sample.php',
'wp-config.php',
'wp-cron.php',
'wp-links-opml.php',
'wp-load.php',
'wp-login.php',
'wp-mail.php',
'wp-settings.php',
'wp-signup.php',
'wp-trackback.php',
'xmlrpc.php',
basename(__FILE__),
'cleanup.log'
];
$whitelistPrefixes = [
'wp-content/uploads',
'wp-content/plugins',
'wp-content/themes',
];
// ------------------------------------------
// CLI args
foreach ($argv ?? [] as $arg) {
if ($arg === '--run') $dryRun = false;
}
// Ensure quarantine exists
if (!file_exists($quarantineDir)) {
mkdir($quarantineDir, 0755, true);
}
function log_msg($msg)
{
global $logFile;
$t = date('[Y-m-d H:i:s] ');
file_put_contents($logFile, $t . $msg . PHP_EOL, FILE_APPEND);
}
function is_whitelisted($relative)
{
global $whitelist, $whitelistPrefixes;
if (in_array($relative, $whitelist)) return true;
foreach ($whitelistPrefixes as $p) {
if (strpos($relative, $p) === 0) return true;
}
return false;
}
function rrmdir_or_delete($path)
{
if (is_dir($path)) {
foreach (scandir($path) as $file) {
if ($file === '.' || $file === '..') continue;
rrmdir_or_delete($path . DIRECTORY_SEPARATOR . $file);
}
rmdir($path);
} else {
unlink($path);
}
}
// --------- MAIN LOGIC: CLEAN ROOT ONLY ---------
$items = scandir($root);
$problems = 0;
foreach ($items as $item) {
if ($item === '.' || $item === '..') continue;
$full = $root . DIRECTORY_SEPARATOR . $item;
// Skip quarantine + log
if (realpath($full) === realpath($quarantineDir)) continue;
if (realpath($full) === realpath($logFile)) continue;
// Skip whitelisted
if (is_whitelisted($item)) continue;
// Unwanted file/folder detected
$problems++;
if ($dryRun) {
echo "[DRY-RUN] Would DELETE: $item\n";
log_msg("DRY-RUN Would DELETE: $item");
} else {
rrmdir_or_delete($full);
echo "DELETED: $item\n";
log_msg("Deleted: $item");
}
}
echo "\nScan complete. Items flagged: $problems\n";
log_msg("Scan complete. Items flagged: $problems");
if ($dryRun) {
echo "Dry-run only. Use --run to perform deletion.\n";
}
?>