Allowing multiple use of -d option and braces in path syntax, closes #56

This commit is contained in:
Gabor Gyorvari
2020-08-18 15:36:52 +02:00
parent 21185202f3
commit e9a45d4bdc

View File

@@ -69,16 +69,31 @@ class MalwareScanner
if ($cli === true) {
//Read Run Options
$this->parseArgs();
$this->dir = realpath($this->dir);
$dirs = array();
if (is_array($this->dir)) {
// allow multiple directory aka. array
foreach ($this->dir as $path) {
$dirs[] = realpath($path);
}
} elseif ($bpos = strpos($this->dir, '{')) {
// Check path has a "brace", expand it to subdirectories
foreach (glob($this->dir, GLOB_BRACE) as $path) {
$dirs[] = realpath($path);
}
} else {
// only one directory specified
$dirs = array (realpath($this->dir));
}
//Make sure a directory was specified.
if ($this->dir === '') {
if (empty($dirs)) {
$this->error('No directory specified or directory doesn\'t exist');
exit(-1);
}
//Initiate Scan
if (!$this->run($this->dir)) {
if (!$this->run($dirs)) {
exit(-1);
}
}
@@ -592,18 +607,11 @@ class MalwareScanner
* - Fetch and load combined whitelist
* - Calls the process and report functions.
*
* @param $dir
* @param string|array $dir A directory path or a list of paths in array
* @return bool
*/
public function run($dir)
{
// Make sure the input is a valid directory path.
$dir = rtrim($dir, '/');
if (!is_dir($dir)) {
$this->error('Specified path is not a directory: ' . $dir);
return false;
}
$this->initializePatterns();
$this->loadWhitelist();
@@ -613,9 +621,23 @@ class MalwareScanner
}
$start = time();
$this->process($dir . '/');
if (!is_array($dir)) {
$dir = array ($dir);
}
foreach ($dir as $path) {
// Make sure the input is a valid directory path.
$path = rtrim($path, '/');
if (!is_dir($path)) {
$this->error('Specified path is not a directory: ' . $path);
return false;
}
$this->process($path . '/');
}
if (!$this->flagDisableStats) {
$this->report($start, $dir . '/');
$this->report($start, implode(', ', $dir));
}
return true;
}