Completed
Pull Request — master (#196)
by Victor
16:07 queued 14:51
created

External   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 49
ccs 0
cts 36
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B initScanner() 0 21 6
A shutdownScanner() 0 11 1
A prepareChunk() 0 4 1
1
<?php
2
/**
3
 * Copyright (c) 2014 Viktar 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
class External extends \OCA\Files_Antivirus\Scanner {
13
	
14
	// Daemon/socket mode
15
	private $useSocket;
16
	
17
	public function __construct($config){
18
		$this->appConfig = $config;
19
		$this->useSocket = $this->appConfig->getAvMode() === 'socket';
20
	}
21
	
22
	public function initScanner(){
23
		parent::initScanner();
24
		
25
		if ($this->useSocket){
26
			$avSocket = $this->appConfig->getAvSocket();
27
			$this->writeHandle = stream_socket_client('unix://' . $avSocket, $errno, $errstr, 5);
28
			if (!$this->getWriteHandle()) {
29
				throw new \RuntimeException('Cannot connect to "' . $avSocket . '": ' . $errstr . ' (code ' . $errno . ')');
30
			}
31
		} else {
32
			$avHost = $this->appConfig->getAvHost();
33
			$avPort = $this->appConfig->getAvPort();
34
			$this->writeHandle = ($avHost && $avPort) ? @fsockopen($avHost, $avPort) : false;
35
			if (!$this->getWriteHandle()) {
36
				throw new \RuntimeException('The clamav module is not configured for daemon mode.');
37
			}
38
		}
39
40
		// request scan from the daemon
41
		@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...
42
	}
43
	
44
	protected function shutdownScanner(){
45
		@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...
46
		$response = fgets($this->getWriteHandle());
47
		\OC::$server->getLogger()->debug(
48
			'Response :: ' . $response,
49
			['app' => 'files_antivirus']
50
		);
51
		@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...
52
		
53
		$this->status->parseResponse($response);
54
	}
55
	
56
	protected function prepareChunk($data){
57
		$chunkLength = pack('N', strlen($data));
58
		return $chunkLength . $data;
59
	}
60
}
61