1
|
|
|
<?php namespace Sunspikes\ClamavValidator; |
2
|
|
|
|
3
|
|
|
use Illuminate\Validation\Validator; |
4
|
|
|
use Quahog\Client; |
5
|
|
|
use Socket\Raw\Factory; |
6
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
7
|
|
|
|
8
|
|
|
class ClamavValidator extends Validator |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @const string CLAMAV_STATUS_OK |
12
|
|
|
*/ |
13
|
|
|
const CLAMAV_STATUS_OK = 'OK'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @const string CLAMAV_UNIX_SOCKET |
17
|
|
|
*/ |
18
|
|
|
const CLAMAV_UNIX_SOCKET = '/var/run/clamav/clamd.ctl'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
3 |
|
* @const string CLAMAV_LOCAL_TCP_SOCKET |
22
|
|
|
*/ |
23
|
3 |
|
const CLAMAV_LOCAL_TCP_SOCKET = 'tcp://127.0.0.1:3310'; |
24
|
3 |
|
|
25
|
|
|
/** |
26
|
|
|
* Creates a new instance of ClamavValidator |
27
|
|
|
*/ |
28
|
|
|
public function __construct($translator, $data, $rules, $messages) |
29
|
|
|
{ |
30
|
|
|
parent::__construct($translator, $data, $rules, $messages); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
2 |
|
* Validate the uploaded file for virus/malware with ClamAV |
35
|
|
|
* |
36
|
2 |
|
* @param $attribute string |
37
|
|
|
* @param $value mixed |
38
|
|
|
* @param $parameters array |
39
|
2 |
|
* @return boolean |
40
|
|
|
*/ |
41
|
|
|
public function validateClamav($attribute, $value, $parameters) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$file = $this->getFilePath($value); |
44
|
|
|
$clamavSocket = $this->getClamavSocket(); |
45
|
|
|
|
46
|
|
|
// Create a new socket instance |
47
|
|
|
$socket = (new Factory())->createClient($clamavSocket); |
48
|
|
|
|
49
|
|
|
// Create a new instance of the Client |
50
|
|
|
$quahog = new Client($socket); |
51
|
|
|
|
52
|
|
|
// Scan the file |
53
|
|
|
$result = $quahog->scanFile($file); |
54
|
|
|
|
55
|
|
|
// Check if scan result is not clean |
56
|
|
|
if (self::CLAMAV_STATUS_OK != $result['status']) { |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return true; |
61
|
2 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
2 |
|
* Guess the ClamAV socket |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
protected function getClamavSocket() |
69
|
2 |
|
{ |
70
|
|
|
if (file_exists(self::CLAMAV_UNIX_SOCKET)) { |
71
|
|
|
return 'unix://' . self::CLAMAV_UNIX_SOCKET; |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
return self::CLAMAV_LOCAL_TCP_SOCKET; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Return the file path from the passed object |
79
|
|
|
* |
80
|
|
|
* @param $file mixed |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
protected function getFilePath($file) |
84
|
|
|
{ |
85
|
|
|
// if were passed an instance of UploadedFile, return the path |
86
|
|
|
if ($file instanceof UploadedFile) { |
87
|
|
|
return $file->getPathname(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// if we're passed a PHP file upload array, return the "tmp_name" |
91
|
|
|
if (is_array($file) && null !== array_get($file, 'tmp_name')) { |
92
|
|
|
return $file['tmp_name']; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// fallback: we were likely passed a path already |
96
|
|
|
return $file; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.