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 | 3 | if (true === Config::get('clamav.skip_validation')) { |
|
46 | return true; |
||
47 | } |
||
48 | |||
49 | 3 | $file = $this->getFilePath($value); |
|
50 | 3 | if (! is_readable($file)) { |
|
51 | 1 | throw ClamavValidatorException::forNonReadableFile($file); |
|
52 | } |
||
53 | |||
54 | try { |
||
55 | 2 | $socket = $this->getClamavSocket(); |
|
56 | 2 | $scanner = $this->createQuahogScannerClient($socket); |
|
57 | 2 | $result = $scanner->scanResourceStream(fopen($file, 'rb')); |
|
58 | 2 | } catch (\Exception $exception) { |
|
59 | throw ClamavValidatorException::forClientException($exception); |
||
60 | } |
||
61 | |||
62 | 2 | if (QuahogClient::RESULT_ERROR === $result['status']) { |
|
63 | throw ClamavValidatorException::forScanResult($result); |
||
64 | } |
||
65 | |||
66 | // Check if scan result is clean |
||
67 | 2 | return QuahogClient::RESULT_OK === $result['status']; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * Guess the ClamAV socket. |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | 2 | protected function getClamavSocket() |
|
88 | |||
89 | /** |
||
90 | * Return the file path from the passed object. |
||
91 | * |
||
92 | * @param string|array|UploadedFile $file |
||
93 | * @return string |
||
94 | */ |
||
95 | 3 | protected function getFilePath($file) |
|
110 | |||
111 | /** |
||
112 | * Create a new quahog ClamAV scanner client. |
||
113 | * |
||
114 | * @param string $socket |
||
115 | * @return QuahogClient |
||
116 | */ |
||
117 | 2 | protected function createQuahogScannerClient($socket) |
|
124 | } |
||
125 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.