Completed
Pull Request — master (#195)
by Victor
19:38 queued 09:40
created

Daemon::initScanner()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 8
nc 2
nop 0
1
<?php
2
/**
3
 * Copyright (c) 2017 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
use OCA\Files_Antivirus\AppConfig;
13
use OCP\ILogger;
14
15
class Daemon extends External {
16
17
	/** @var string  */
18
	private $avHost;
19
20
	/** @var int  */
21
	private $avPort;
22
23
	/**
24
	 * Daemon constructor.
25
	 *
26
	 * @param AppConfig $config
27
	 * @param ILogger $logger
28
	 * @throws InitException
29
	 */
30
	public function __construct(AppConfig $config, ILogger $logger) {
31
		parent::__construct($config, $logger);
32
33
		$this->avHost = $this->appConfig->getAvHost();
34
		$this->avPort = $this->appConfig->getAvPort();
35
		$checks = [
36
			'hostname' => $this->avHost,
37
			'port' => $this->avPort
38
		];
39
		$errors = [];
40
		foreach ($checks as $key => $check) {
41
			if ($check === '') {
42
				$errors[] = sprintf(
43
					'Daemon mode requires a %s but it is empty.',
44
					$key
45
				);
46
			}
47
		}
48
49
		if (count($errors) > 0) {
50
			throw new InitException(
51
				'The app is not configured properly. ' . implode(' ', $errors)
52
			);
53
		}
54
	}
55
56
	/**
57
	 * @throws InitException
58
	 */
59
	public function initScanner(){
60
		parent::initScanner();
61
		$this->writeHandle = @fsockopen($this->avHost, $this->avPort);
62
		if (!$this->getWriteHandle()) {
63
			throw new InitException(
64
				sprintf(
65
					'Could not connect to host "%s" on port %d', $this->avHost, $this->avPort
66
				)
67
			);
68
		}
69
		// request scan from the daemon
70
		@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...
71
	}
72
}
73