New ignore argument to exclude files and folders with glob style matching

This commit is contained in:
Gabor Gyorvari
2017-01-11 19:10:59 +01:00
parent b522a23a74
commit acb58f1c2e
2 changed files with 65 additions and 15 deletions

View File

@@ -28,6 +28,7 @@ class MalwareScanner
private $flagHideWhitelist = false;
private $extraCheck = false;
private $whitelist = array();
private $ignore = array();
private $stat = array(
'directories' => 0,
'files_scanned' => 0,
@@ -37,7 +38,7 @@ class MalwareScanner
public function __construct()
{
$options = getopt('hd:e::', array('hide-ok', 'hide-whitelist', 'extra-check', 'follow-symlink'));
$options = getopt('hd:e::i::', array('hide-ok', 'hide-whitelist', 'extra-check', 'follow-symlink'));
if (isset($options['h'])) {
$this->showHelp();
} else {
@@ -48,6 +49,9 @@ class MalwareScanner
}
$this->extension = strtolower($ext);
}
if (isset($options['i'])) {
$this->ignore = is_array($options['i']) ? $options['i'] : array($options['i']);
}
if (isset($options['hide-ok'])) {
$this->flagHideOk = true;
}
@@ -110,6 +114,9 @@ class MalwareScanner
if ($file == '.' || $file == '..') {
continue;
}
if ($this->isIgnored($dir . $file)) {
continue;
}
if (!$this->followSymlink && is_link($dir . $file)) {
continue;
}
@@ -384,6 +391,45 @@ class MalwareScanner
return true;
}
private function isIgnored($pathname)
{
foreach ($this->ignore as $pattern) {
$match = $this->pathMatches($pathname, $pattern);
if ($match) {
return true;
}
}
return false;
}
/**
* @see http://stackoverflow.com/a/13914119
*/
private function pathMatches($path, $pattern, $ignoreCase = false)
{
$expr = preg_replace_callback(
'/[\\\\^$.[\\]|()?*+{}\\-\\/]/',
function ($matches) {
switch ($matches[0]) {
case '*':
return '.*';
case '?':
return '.';
default:
return '\\' . $matches[0];
}
},
$pattern
);
$expr = '/' . $expr . '/';
if ($ignoreCase) {
$expr .= 'i';
}
return (bool)preg_match($expr, $path);
}
private function out($color, $serv, $text)
{
echo $color . ' ' . $serv . ' ' . self::ANSI_OFF . $text . PHP_EOL;
@@ -391,13 +437,14 @@ class MalwareScanner
private function showHelp()
{
echo 'Usage scan.php -d <directory> [-e=.php] [--hide-ok] [--hide-whitelist]' . PHP_EOL;
echo ' -d Directory for searching' . PHP_EOL;
echo ' -e=.php Extension' . PHP_EOL;
echo ' --hide-ok Hide OK aka not infected messages' . PHP_EOL;
echo ' --hide-whitelist Hide whitelisted messages' . PHP_EOL;
echo ' --extra-check Adds GoogleBot and htaccess to Scan List' . PHP_EOL;
echo ' --follow-symlink Follow symlinked directories' . PHP_EOL;
echo 'Usage scan.php -d <directory> [-i=<directory|file>] [-e=.php] [--hide-ok] [--hide-whitelist]' . PHP_EOL;
echo ' -d Directory for searching' . PHP_EOL;
echo ' -e=.php Extension' . PHP_EOL;
echo ' -i=<directory|file> Directory of file to igonre' . PHP_EOL;
echo ' --hide-ok Hide OK aka not infected messages' . PHP_EOL;
echo ' --hide-whitelist Hide whitelisted messages' . PHP_EOL;
echo ' --extra-check Adds GoogleBot and htaccess to Scan List' . PHP_EOL;
echo ' --follow-symlink Follow symlinked directories' . PHP_EOL;
}
}