1 | <?php namespace Sunspikes\ClamavValidator; |
||
10 | class ClamavValidator extends Validator |
||
11 | { |
||
12 | /** |
||
13 | * @const string CLAMAV_STATUS_OK |
||
14 | */ |
||
15 | const CLAMAV_STATUS_OK = 'OK'; |
||
16 | |||
17 | /** |
||
18 | * @const string CLAMAV_STATUS_ERROR |
||
19 | */ |
||
20 | const CLAMAV_STATUS_ERROR = 'ERROR'; |
||
21 | |||
22 | /** |
||
23 | * Creates a new instance of ClamavValidator] |
||
24 | * |
||
25 | * ClamavValidator constructor. |
||
26 | * @param Translator $translator |
||
27 | * @param array $data |
||
28 | * @param array $rules |
||
29 | * @param array $messages |
||
30 | * @param array $customAttributes |
||
31 | */ |
||
32 | 4 | public function __construct( |
|
41 | |||
42 | /** |
||
43 | * Validate the uploaded file for virus/malware with ClamAV |
||
44 | * |
||
45 | * @param $attribute string |
||
46 | * @param $value mixed |
||
47 | * @param $parameters array |
||
48 | * |
||
49 | * @return boolean |
||
50 | * @throws ClamavValidatorException |
||
51 | */ |
||
52 | 3 | public function validateClamav($attribute, $value, $parameters) |
|
53 | { |
||
54 | 3 | $file = $this->getFilePath($value); |
|
55 | 3 | $clamavSocket = $this->getClamavSocket(); |
|
56 | |||
57 | // Create a new socket instance |
||
58 | 3 | $socket = (new Factory())->createClient($clamavSocket); |
|
59 | |||
60 | // Create a new instance of the Client |
||
61 | 3 | $quahog = new Client($socket, Config::get('clamav.socket_read_timeout'), PHP_NORMAL_READ); |
|
62 | |||
63 | // Check if the file is readable |
||
64 | 3 | if (! is_readable($file)) { |
|
65 | 1 | throw new ClamavValidatorException(sprintf('The file "%s" is not readable', $file)); |
|
66 | } |
||
67 | |||
68 | // Scan the file |
||
69 | 2 | $result = $quahog->scanResourceStream(fopen($file, 'rb')); |
|
70 | |||
71 | 2 | if (Client::RESULT_ERROR === $result['status']) { |
|
72 | throw new ClamavValidatorException($result['reason']); |
||
73 | } |
||
74 | |||
75 | // Check if scan result is not clean |
||
76 | 2 | return Client::RESULT_OK === $result['status']; |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * Guess the ClamAV socket |
||
81 | * |
||
82 | * @return string |
||
83 | */ |
||
84 | 3 | protected function getClamavSocket() |
|
85 | { |
||
86 | 3 | $preferredSocket = Config::get('clamav.preferred_socket'); |
|
87 | |||
88 | 3 | if ($preferredSocket === 'unix_socket') { |
|
89 | 3 | $unixSocket = Config::get('clamav.unix_socket'); |
|
90 | 3 | if (file_exists($unixSocket)) { |
|
91 | 3 | return 'unix://' . $unixSocket; |
|
92 | } |
||
93 | } |
||
94 | // We use the tcp_socket as fallback as well |
||
95 | return Config::get('clamav.tcp_socket'); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Return the file path from the passed object |
||
100 | * |
||
101 | * @param $file mixed |
||
102 | * @return string |
||
103 | */ |
||
104 | 3 | protected function getFilePath($file) |
|
119 | } |
||
120 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.