Completed
Pull Request — master (#50)
by Roeland
01:32
created

External::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 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\Scanner;
14
15
class External extends Scanner {
16
	
17
	// Daemon/socket mode
18
	private $useSocket;
19
	
20 2
	public function __construct(AppConfig $config){
21 2
		$this->appConfig = $config;
22 2
		$this->useSocket = $this->appConfig->getAvMode() === 'socket';
23 2
	}
24
	
25
	public function initScanner(){
26
		parent::initScanner();
27
		
28
		if ($this->useSocket){
29
			$avSocket = $this->appConfig->getAvSocket();
30
			$this->writeHandle = stream_socket_client('unix://' . $avSocket, $errno, $errstr, 5);
31
			if (!$this->getWriteHandle()) {
32
				throw new \RuntimeException('Cannot connect to "' . $avSocket . '": ' . $errstr . ' (code ' . $errno . ')');
33
			}
34
		} else {
35
			$avHost = $this->appConfig->getAvHost();
36
			$avPort = $this->appConfig->getAvPort();
37
			if (!($avHost && $avPort)) {
38
				throw new \RuntimeException('The clamav port and host are not configured.');
39
			}
40
			$this->writeHandle = ($avHost && $avPort) ? @fsockopen($avHost, $avPort) : false;
41
			if (!$this->getWriteHandle()) {
42
				throw new \RuntimeException('The clamav module is not configured for daemon mode.');
43
			}
44
		}
45
46
		// request scan from the daemon
47
		@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...
48
	}
49
	
50
	protected function shutdownScanner(){
51
		@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...
52
		$response = fgets($this->getWriteHandle());
53
		\OC::$server->getLogger()->debug(
54
			'Response :: ' . $response,
55
			['app' => 'files_antivirus']
56
		);
57
		@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...
58
		
59
		$this->status->parseResponse($response);
60
	}
61
	
62
	protected function prepareChunk($data){
63
		$chunkLength = pack('N', strlen($data));
64
		return $chunkLength . $data;
65
	}
66
}
67