Completed
Push — master ( 835da2...f8eb08 )
by Krishnaprasad
05:09
created

ClamavValidator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 81.82%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 0
cbo 5
dl 0
loc 103
ccs 18
cts 22
cp 0.8182
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validateClamav() 0 21 2
A getClamavSocket() 0 8 2
A getFilePath() 0 15 4
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 4
    public function __construct($translator, $data, $rules, $messages)
39
    {
40 4
        parent::__construct($translator, $data, $rules, $messages);
41 4
    }
42
43
    /**
44
     * Validate the uploaded file for virus/malware with ClamAV
45
     *
46
     * @param  $attribute   string
47
     * @param  $value       mixed
48
     * @param  $parameters  array
49
     *
50
     * @return boolean
51
     * @throws ClamavValidatorException
52
     */
53 3
    public function validateClamav($attribute, $value, $parameters)
0 ignored issues
show
Unused Code introduced by Krishnaprasad MG
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 Krishnaprasad MG
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...
54
    {
55 3
        $file = $this->getFilePath($value);
56 3
        $clamavSocket = $this->getClamavSocket();
57
58
        // Create a new socket instance
59 3
        $socket = (new Factory())->createClient($clamavSocket);
60
61
        // Create a new instance of the Client
62 3
        $quahog = new Client($socket, self::CLAMAV_SOCKET_READ_TIMEOUT, PHP_NORMAL_READ);
63
64
        // Scan the file
65 3
        $result = $quahog->scanFile($file);
66
67 3
        if (self::CLAMAV_STATUS_ERROR === $result['status']) {
68
            throw new ClamavValidatorException($result['reason']);
69
        }
70
71
        // Check if scan result is not clean
72 3
        return !(self::CLAMAV_STATUS_OK !== $result['status']);
73
    }
74
75
    /**
76
     * Guess the ClamAV socket
77
     *
78
     * @return string
79
     */
80 3
    protected function getClamavSocket()
81
    {
82 3
        if (file_exists(env('CLAMAV_UNIX_SOCKET', self::CLAMAV_UNIX_SOCKET))) {
83 3
            return 'unix://' . env('CLAMAV_UNIX_SOCKET', self::CLAMAV_UNIX_SOCKET);
84
        }
85
86
        return env('CLAMAV_TCP_SOCKET', self::CLAMAV_LOCAL_TCP_SOCKET);
87
    }
88
89
    /**
90
     * Return the file path from the passed object
91
     *
92
     * @param $file mixed
93
     * @return string
94
     */
95 3
    protected function getFilePath($file)
96
    {
97
        // if were passed an instance of UploadedFile, return the path
98 3
        if ($file instanceof UploadedFile) {
99
            return $file->getPathname();
100
        }
101
102
        // if we're passed a PHP file upload array, return the "tmp_name"
103 3
        if (is_array($file) && null !== array_get($file, 'tmp_name')) {
104
            return $file['tmp_name'];
105
        }
106
107
        // fallback: we were likely passed a path already
108 3
        return $file;
109
    }
110
}
111