File: /home/httpd/html/stoptube.com/scan_files.php
<?php
set_time_limit(0);
$exclude_files = array(
$_SERVER['DOCUMENT_ROOT'] . '/' . 'scan_files.php',
);
$exclude_dirs = array(
$_SERVER['DOCUMENT_ROOT'] . '/' . 'stats',
);
$update = $_REQUEST["u"] == "1";
$verbose = $_REQUEST["v"] == "1";
function scan_files($dir) {
global $exclude_files, $exclude_dirs, $update, $verbose;
// regular expressions to search
$exp1 = "#\<\?php if\(!function_exists\('tmp_lkojfghx'\)\).*?tmp_lkojfghx2\(\); \?\>#";
$exp2 = "#<script language=javascript><!-- ?\n\(function\(.*?\){var .*?unescape\(.*?\);\n --></script>#";
$exp3 = "#<script language=javascript><!-- ?\n\(function\(.*?\){eval\(unescape\(.*?\);\n --></script>#";
$exp4 = "#<!-- ?\n\(function\(.*?\){var .*?unescape\(.*?\);\n -->#";
$exp5 = "#<!-- ?\n\(function\(.*?\){eval\(unescape\(.*?\);\n -->#";
$search = array(
$exp1,
$exp2,
$exp3,
$exp4,
$exp5,
);
$dirs_array = array();
if ($handle = opendir($dir)) {
echo "Open dir: " . $dir . "\n";
echo "Files:";
// this is the correct way to loop over the directory.
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..') {
$path = $dir . $file;
if (is_file($path)) {
// skip large files
if (filesize($path) > 1000000) {
continue;
}
// exclude files
if (in_array($path, $exclude_files)) {
continue;
}
// exclude files
if (endsWith($file, '.bak')) {
continue;
}
if (endsWith($path, '/images/image.php') ||
endsWith($path, '/images/gifimg.php')) {
echo "\n===>" . $path . "\n";
if ($update) {
unlink($path);
}
continue;
}
// get content
$contents = file_get_contents($path);
$origContents = $contents;
// loop for search string
foreach ($search as $pattern) {
$contents = preg_replace($pattern, "", $contents);
}
if ($contents != $origContents) {
echo "\n===>" . $path;
echo "\n";
if ($update) {
if (!$file_handle = fopen($path . '.bak', 'w')) {
echo "Cannot open file ({$path}.bak)<br/>\n";
exit;
}
if (fwrite($file_handle, $origContents) === FALSE) {
echo "Cannot write to file ({$path}.bak)<br/>\n";
exit;
}
fclose($file_handle);
if (!$file_handle = fopen($path, 'w')) {
echo "Cannot open file ({$path})<br/>\n";
exit;
}
if (fwrite($file_handle, $contents) === FALSE) {
echo "Cannot write to file ({$path})<br/>\n";
exit;
}
fclose($file_handle);
}
elseif ($verbose) {
echo "**********\norigContents=$origContents\n";
echo "**********\ncontents=$contents\n";
}
}
} elseif (is_dir($path)) {
if (in_array($path, $exclude_dirs)) {
continue;
}
$dirs_array[] = $path;
}
}
}
closedir($handle);
}
foreach ($dirs_array as $dir) {
scan_files($dir . '/');
}
unset($dirs_array);
}
function endsWith($string, $ending) {
$len = strlen($ending);
$string_end = substr($string, strlen($string) - $len);
return $string_end == $ending;
}
$start_dir = $_SERVER['DOCUMENT_ROOT'] . '/';
echo 'Starting from: ' . $start_dir . "\n";
scan_files($start_dir);
?>