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