Completed
Push — master ( e6bd0e...360bd6 )
by Krishnaprasad
07:19 queued 05:18
created

ClamavValidator::getClamavSocket()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4286
cc 2
eloc 4
nc 2
nop 0
crap 2.0625
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_UNIX_SOCKET
17
     */
18
    const CLAMAV_UNIX_SOCKET = '/var/run/clamav/clamd.ctl';
19
20
    /**
21
     * @const string CLAMAV_LOCAL_TCP_SOCKET
22
     */
23
    const CLAMAV_LOCAL_TCP_SOCKET = 'tcp://127.0.0.1:3310';
24
25
    /**
26
     * Creates a new instance of ClamavValidator
27
     */
28 3
    public function __construct($translator, $data, $rules, $messages)
29
    {
30 3
        parent::__construct($translator, $data, $rules, $messages);
31 3
    }
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
     * @return boolean
40
     */
41 2
    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...
42
    {
43 2
        $file = $this->getFilePath($value);
44 2
        $clamavSocket = $this->getClamavSocket();
45
46
        // Create a new socket instance
47 2
        $socket = (new Factory())->createClient($clamavSocket);
48
49
        // Create a new instance of the Client
50
        $quahog = new Client($socket);
51
52
        // Scan the file
53
        $result = $quahog->scanFile($file);
54
55
        // Check if scan result is not clean
56
        if (self::CLAMAV_STATUS_OK != $result['status']) {
57
            return false;
58
        }
59
60
        return true;
61
    }
62
63
    /**
64
     * Guess the ClamAV socket
65
     *
66
     * @return string
67
     */
68 2
    protected function getClamavSocket()
69
    {
70 2
        if (file_exists(self::CLAMAV_UNIX_SOCKET)) {
71
            return 'unix://' . self::CLAMAV_UNIX_SOCKET;
72
        }
73
74 2
        return self::CLAMAV_LOCAL_TCP_SOCKET;
75
    }
76
77
    /**
78
     * Return the file path from the passed object
79
     *
80
     * @param $file mixed
81
     * @return string
82
     */
83 2
    protected function getFilePath($file)
84
    {
85
        // if were passed an instance of UploadedFile, return the path
86 2
        if ($file instanceof UploadedFile) {
87
            return $file->getPathname();
88
        }
89
90
        // if we're passed a PHP file upload array, return the "tmp_name"
91 2
        if (is_array($file) && null !== array_get($file, 'tmp_name')) {
92
            return $file['tmp_name'];
93
        }
94
95
        // fallback: we were likely passed a path already
96 2
        return $file;
97
    }
98
}
99