Completed
Push — master ( 26b677...48f920 )
by Krishnaprasad
8s
created

ClamavValidator::validateClamav()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 5.1971

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 25
ccs 3
cts 8
cp 0.375
rs 8.8571
cc 3
eloc 11
nc 3
nop 3
crap 5.1971
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_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 3
    const CLAMAV_LOCAL_TCP_SOCKET = 'tcp://127.0.0.1:3310';
29
30 3
    /**
31 3
     * Creates a new instance of ClamavValidator
32
     */
33
    public function __construct($translator, $data, $rules, $messages)
34
    {
35
        parent::__construct($translator, $data, $rules, $messages);
36
    }
37
38
    /**
39
     * Validate the uploaded file for virus/malware with ClamAV
40
     *
41 2
     * @param  $attribute  string
42
     * @param  $value       mixed
43 2
     * @param  $parameters array
44 2
     * @return boolean
45
     */
46
    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...
47 2
    {
48
        $file = $this->getFilePath($value);
49
        $clamavSocket = $this->getClamavSocket();
50
51
        // Create a new socket instance
52
        $socket = (new Factory())->createClient($clamavSocket);
53
54
        // Create a new instance of the Client
55
        $quahog = new Client($socket);
56
57
        // Scan the file
58
        $result = $quahog->scanFile($file);
59
60
        if (self::CLAMAV_STATUS_ERROR === $result['status']) {
61
            throw new ClamavValidatorException($result['reason']);
62
        }
63
64
        // Check if scan result is not clean
65
        if (self::CLAMAV_STATUS_OK != $result['status']) {
66
            return false;
67
        }
68 2
69
        return true;
70 2
    }
71
72
    /**
73
     * Guess the ClamAV socket
74 2
     *
75
     * @return string
76
     */
77
    protected function getClamavSocket()
78
    {
79
        if (file_exists(self::CLAMAV_UNIX_SOCKET)) {
80
            return 'unix://' . self::CLAMAV_UNIX_SOCKET;
81
        }
82
83 2
        return self::CLAMAV_LOCAL_TCP_SOCKET;
84
    }
85
86 2
    /**
87
     * Return the file path from the passed object
88
     *
89
     * @param $file mixed
90
     * @return string
91 2
     */
92
    protected function getFilePath($file)
93
    {
94
        // if were passed an instance of UploadedFile, return the path
95
        if ($file instanceof UploadedFile) {
96 2
            return $file->getPathname();
97
        }
98
99
        // if we're passed a PHP file upload array, return the "tmp_name"
100
        if (is_array($file) && null !== array_get($file, 'tmp_name')) {
101
            return $file['tmp_name'];
102
        }
103
104
        // fallback: we were likely passed a path already
105
        return $file;
106
    }
107
}
108