Completed
Pull Request — master (#12)
by
unknown
11:38
created

ClamavValidator::getFilePath()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 6
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 3
nop 1
crap 20
1
<?php namespace Sunspikes\ClamavValidator;
2
3
use Illuminate\Validation\Validator;
4
use Xenolope\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_STATUS_ERROR
17
     */
18
    const CLAMAV_STATUS_ERROR = 'ERROR';
19
20
    /**
21
     * @const string CLAMAV_UNIX_SOCKET
22
     */
23
    const CLAMAV_UNIX_SOCKET = '/var/run/clamav/clamd.ctl';
24
25
    /**
26
     * @const string CLAMAV_LOCAL_TCP_SOCKET
27
     */
28
    const CLAMAV_LOCAL_TCP_SOCKET = 'tcp://127.0.0.1:3310';
29
30
    /**
31
     * @const string CLAMAV_SOCKET_READ_TIMEOUT
32
     */
33
    const CLAMAV_SOCKET_READ_TIMEOUT = 30;
34
35
    /**
36
     * Creates a new instance of ClamavValidator
37
     */
38
    public function __construct(
39
        Translator $translator,
40
        array $data,
41
        array $rules,
42
        array $messages = [],
43
        array $customAttributes = []
44
    ) {
45
        parent::__construct($translator, $data, $rules, $messages, $customAttributes);
46
    }
47
48
    /**
49
     * Validate the uploaded file for virus/malware with ClamAV
50
     *
51
     * @param  $attribute   string
52
     * @param  $value       mixed
53
     * @param  $parameters  array
54
     *
55
     * @return boolean
56
     * @throws ClamavValidatorException
57
     */
58
    public function validateClamav($attribute, $value, $parameters)
0 ignored issues
show
Unused Code introduced by
The parameter $attribute is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $parameters is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
    {
60
        $file = $this->getFilePath($value);
61
        $clamavSocket = $this->getClamavSocket();
62
63
        // Create a new socket instance
64
        $socket = (new Factory())->createClient($clamavSocket);
65
66
        // Create a new instance of the Client
67
        $quahog = new Client($socket, self::CLAMAV_SOCKET_READ_TIMEOUT, PHP_NORMAL_READ);
68
69
        // Scan the file
70
        $result = $quahog->scanFile($file);
71
72
        if (self::CLAMAV_STATUS_ERROR === $result['status']) {
73
            throw new ClamavValidatorException($result['reason']);
74
        }
75
76
        // Check if scan result is not clean
77
        return !(self::CLAMAV_STATUS_OK !== $result['status']);
78
    }
79
80
    /**
81
     * Guess the ClamAV socket
82
     *
83
     * @return string
84
     */
85
    protected function getClamavSocket()
86
    {
87
        if (file_exists(env('CLAMAV_UNIX_SOCKET', self::CLAMAV_UNIX_SOCKET))) {
88
            return 'unix://' . env('CLAMAV_UNIX_SOCKET', self::CLAMAV_UNIX_SOCKET);
89
        }
90
91
        return env('CLAMAV_TCP_SOCKET', self::CLAMAV_LOCAL_TCP_SOCKET);
92
    }
93
94
    /**
95
     * Return the file path from the passed object
96
     *
97
     * @param $file mixed
98
     * @return string
99
     */
100
    protected function getFilePath($file)
101
    {
102
        // if were passed an instance of UploadedFile, return the path
103
        if ($file instanceof UploadedFile) {
104
            return $file->getPathname();
105
        }
106
107
        // if we're passed a PHP file upload array, return the "tmp_name"
108
        if (is_array($file) && null !== array_get($file, 'tmp_name')) {
109
            return $file['tmp_name'];
110
        }
111
112
        // fallback: we were likely passed a path already
113
        return $file;
114
    }
115
}
116