Completed
Push — master ( b32497...b91e8b )
by Roeland
16s queued 12s
created

ExternalClam   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 12.5%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 62
ccs 4
cts 32
cp 0.125
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B initScanner() 0 24 8
A shutdownScanner() 0 11 1
A prepareChunk() 0 4 1
1
<?php
2
/**
3
 * Copyright (c) 2014 Victor Dubiniuk <[email protected]>
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later.
6
 * See the COPYING-README file.
7
 */
8
9
10
namespace OCA\Files_Antivirus\Scanner;
11
12
use OCA\Files_Antivirus\AppConfig;
13
use OCA\Files_Antivirus\StatusFactory;
14
use OCP\ILogger;
15
16
class ExternalClam extends ScannerBase {
17
	
18
	/**
19
	 * Daemon/socket mode
20
	 * @var bool
21
	 */
22
	private $useSocket;
23
24
	/**
25
	 * External constructor.
26
	 *
27
	 * @param AppConfig $config
28
	 * @param ILogger $logger
29
	 * @param StatusFactory $statusFactory
30
	 */
31 2
	public function __construct(AppConfig $config, ILogger $logger, StatusFactory $statusFactory) {
32 2
		parent::__construct($config, $logger, $statusFactory);
33 2
		$this->useSocket = $this->appConfig->getAvMode() === 'socket';
34 2
	}
35
	
36
	public function initScanner(){
37
		parent::initScanner();
38
		
39
		if ($this->useSocket){
40
			$avSocket = $this->appConfig->getAvSocket();
41
			$this->writeHandle = stream_socket_client('unix://' . $avSocket, $errno, $errstr, 5);
42
			if (!$this->getWriteHandle()) {
43
				throw new \RuntimeException('Cannot connect to "' . $avSocket . '": ' . $errstr . ' (code ' . $errno . ')');
44
			}
45
		} else {
46
			$avHost = $this->appConfig->getAvHost();
47
			$avPort = $this->appConfig->getAvPort();
48
			if (!($avHost && $avPort)) {
49
				throw new \RuntimeException('The ClamAV port and host are not set up.');
50
			}
51
			$this->writeHandle = ($avHost && $avPort) ? @fsockopen($avHost, $avPort) : false;
52
			if (!$this->getWriteHandle()) {
53
				throw new \RuntimeException('The ClamAV module is not in daemon mode.');
54
			}
55
		}
56
57
		// request scan from the daemon
58
		@fwrite($this->getWriteHandle(), "nINSTREAM\n");
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
59
	}
60
	
61
	protected function shutdownScanner(){
62
		@fwrite($this->getWriteHandle(), pack('N', 0));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
63
		$response = fgets($this->getWriteHandle());
64
		$this->logger->debug(
65
			'Response :: ' . $response,
66
			['app' => 'files_antivirus']
67
		);
68
		@fclose($this->getWriteHandle());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
69
		
70
		$this->status->parseResponse($response);
71
	}
72
	
73
	protected function prepareChunk($data){
74
		$chunkLength = pack('N', strlen($data));
75
		return $chunkLength . $data;
76
	}
77
}
78