Completed
Pull Request — master (#195)
by Victor
34:31 queued 27:05
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) 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
use OCA\Files_Antivirus\AppConfig;
13
use OCP\ILogger;
14
15
class Daemon extends AbstractScanner {
16
17
	public function __construct(AppConfig $config, ILogger $logger) {
18
		parent::__construct($config, $logger);
19
20
		$avHost = $this->appConfig->getAvHost();
21
		$avPort = $this->appConfig->getAvPort();
22
		$checks = [
23
			'hostname' => $avHost,
24
			'port' => $avPort
25
		];
26
		$errors = [];
27
		foreach ($checks as $key => $check) {
28
			if ($check === '') {
29
				$errors[] = sprintf(
30
					'Daemon mode requires a %s but it is empty.',
31
					$key
32
				);
33
			}
34
		}
35
36
		if (count($errors) > 0) {
37
			throw new InitException(
38
				'The app is not configured properly. ' . implode(' ', $errors)
39
			);
40
		}
41
	}
42
43
	public function initScanner(){
44
		parent::initScanner();
45
		$this->writeHandle = @fsockopen($avHost, $avPort);
0 ignored issues
show
Bug introduced by
The variable $avHost does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $avPort does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
46
		if (!$this->getWriteHandle()) {
47
			throw new InitException(
48
				sprintf(
49
					'Could not connect to host "%s" on port %d', $avHost, $avPort
50
				)
51
			);
52
		}
53
		// request scan from the daemon
54
		@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...
55
	}
56
	
57
	protected function shutdownScanner(){
58
		@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...
59
		$response = fgets($this->getWriteHandle());
60
		$this->logger->debug(
61
			'Response :: ' . $response,
62
			['app' => 'files_antivirus']
63
		);
64
		@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...
65
		
66
		$this->status->parseResponse($response);
67
	}
68
	
69
	protected function prepareChunk($data){
70
		$chunkLength = pack('N', strlen($data));
71
		return $chunkLength . $data;
72
	}
73
}
74