diff --git a/README.md b/README.md index a8643c9..ab1f720 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,8 @@ $rules = [ ]; ``` +`ClamavValidator` will automatically run multiple files one-by-one through ClamAV in case `file` represent multiple uploaded files. + ## Author diff --git a/src/ClamavValidator/ClamavValidator.php b/src/ClamavValidator/ClamavValidator.php index 75cecc3..6b86cf0 100755 --- a/src/ClamavValidator/ClamavValidator.php +++ b/src/ClamavValidator/ClamavValidator.php @@ -48,6 +48,28 @@ public function validateClamav($attribute, $value, $parameters) return true; } + if(is_array($value)) { + $result = true; + foreach($value as $file) { + $result &= $this->validateFileWithClamAv($file); + } + + return $result; + } + + return $this->validateFileWithClamAv($value); + } + + /** + * Validate the single uploaded file for virus/malware with ClamAV. + * + * @param $value mixed + * + * @return bool + * @throws ClamavValidatorException + */ + protected function validateFileWithClamAv($value) + { $file = $this->getFilePath($value); if (! is_readable($file)) { throw ClamavValidatorException::forNonReadableFile($file); diff --git a/tests/ClamavValidatorTest.php b/tests/ClamavValidatorTest.php index f618498..1f8b77c 100755 --- a/tests/ClamavValidatorTest.php +++ b/tests/ClamavValidatorTest.php @@ -18,6 +18,8 @@ class ClamavValidatorTest extends \PHPUnit_Framework_TestCase protected $error_data; protected $rules; protected $messages; + protected $multiple_files_all_clean; + protected $multiple_files_some_with_virus; public function setUp() { @@ -35,6 +37,19 @@ public function setUp() $this->error_data = [ 'file' => $this->getTempPath(__DIR__ . '/files/test3.txt') ]; + $this->multiple_files_all_clean = [ + 'files' => [ + $this->getTempPath(__DIR__ . '/files/test1.txt'), + $this->getTempPath(__DIR__ . '/files/test4.txt'), + ] + ]; + $this->multiple_files_some_with_virus = [ + 'files' => [ + $this->getTempPath(__DIR__ . '/files/test1.txt'), + $this->getTempPath(__DIR__ . '/files/test2.txt'), + $this->getTempPath(__DIR__ . '/files/test4.txt'), + ] + ]; $this->messages = []; $config = new Config(); @@ -66,6 +81,18 @@ public function testValidatesClean() $this->assertTrue($validator->passes()); } + public function testValidatesCleanMultiFile() + { + $validator = new ClamavValidator( + $this->translator, + $this->multiple_files_all_clean, + ['files' => 'clamav'], + $this->messages + ); + + $this->assertTrue($validator->passes()); + } + public function testValidatesVirus() { $validator = new ClamavValidator( @@ -78,6 +105,18 @@ public function testValidatesVirus() $this->assertFalse($validator->passes()); } + public function testValidatesVirusMultiFile() + { + $validator = new ClamavValidator( + $this->translator, + $this->multiple_files_some_with_virus, + ['files' => 'clamav'], + $this->messages + ); + + $this->assertFalse($validator->passes()); + } + public function testValidatesError() { $this->setExpectedException(ClamavValidatorException::class, 'is not readable'); diff --git a/tests/files/test4.txt b/tests/files/test4.txt new file mode 100644 index 0000000..727fe42 --- /dev/null +++ b/tests/files/test4.txt @@ -0,0 +1,5 @@ +dfdsfdsfdsf +ds +fds +fdsfds +fdsfdsfds \ No newline at end of file