Completed
Push — closes-181 ( 0a3609 )
by Victor
08:40
created

Daemon::initScanner()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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