Completed
Pull Request — master (#195)
by Victor
09:23 queued 08:01
created

Daemon::initScanner()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.8054

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 37
ccs 15
cts 22
cp 0.6818
rs 8.439
nc 9
cc 5
eloc 22
nop 0
crap 5.8054
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 Daemon extends AbstractScanner {
13
	
14 2
	public function initScanner(){
15 2
		parent::initScanner();
16
17 2
		$avHost = $this->appConfig->getAvHost();
18 2
		$avPort = $this->appConfig->getAvPort();
19
		$checks = [
20 2
			'hostname' => $avHost,
21 2
			'port' => $avPort
22
		];
23 2
		$errors = [];
24 2
		foreach ($checks as $key => $check) {
25 2
			if ($check === '') {
26
				$errors[] = sprintf(
27
					'Daemon mode requires a %s but it is empty.',
28 2
					$key
29
				);
30
			}
31
		}
32
33 2
		if (count($errors)>0) {
34
			throw new InitException(
35
				'The app is not configured properly. ' . implode(' ', $errors)
36
			);
37
		}
38
39 2
		$this->writeHandle = @fsockopen($avHost, $avPort);
40 2
		if (!$this->getWriteHandle()) {
41
			throw new InitException(
42
				sprintf(
43
					'Could not connect to host "%s" on port %d', $avHost, $avPort
44
				)
45
			);
46
		}
47
48
		// request scan from the daemon
49 2
		@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...
50 2
	}
51
	
52 2
	protected function shutdownScanner(){
53 2
		@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...
54 2
		$response = fgets($this->getWriteHandle());
55 2
		$this->logger->debug(
56 2
			'Response :: ' . $response,
57 2
			['app' => 'files_antivirus']
58
		);
59 2
		@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...
60
		
61 2
		$this->status->parseResponse($response);
62 2
	}
63
	
64 2
	protected function prepareChunk($data){
65 2
		$chunkLength = pack('N', strlen($data));
66 2
		return $chunkLength . $data;
67
	}
68
}
69