Completed
Push — master ( 74b39a...45a732 )
by Thomas
04:27
created

Local::initScanner()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.0491

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 10
cts 13
cp 0.7692
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0491
1
<?php
2
/**
3
 * ownCloud - Files_antivirus
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Viktar Dubiniuk <[email protected]>
9
 *
10
 * @copyright Viktar Dubiniuk 2014-2018
11
 * @license AGPL-3.0
12
 */
13
14
namespace OCA\Files_Antivirus\Scanner;
15
16
use OCA\Files_Antivirus\AppConfig;
17
use OCP\ILogger;
18
19
class Local extends AbstractScanner {
20
	
21
	/**
22
	 * @var string
23
	 */
24
	protected $avPath;
25
	
26
	/**
27
	 * STDIN and STDOUT descriptors
28
	 *
29
	 * @var array of resources
30
	 */
31
	private $pipes = [];
32
	
33
	/**
34
	 * Process handle
35
	 *
36
	 * @var resource
37
	 */
38
	private $process;
39
40
	/**
41
	 * Local constructor.
42
	 *
43
	 * @param AppConfig $config
44
	 * @param ILogger $logger
45
	 *
46
	 * @throws InitException
47
	 */
48 4
	public function __construct(AppConfig $config, ILogger $logger) {
49 4
		parent::__construct($config, $logger);
50
51
		// get the path to the executable
52 4
		$this->avPath = \escapeshellcmd($this->appConfig->getAvPath());
53
54 4
		if (!\file_exists($this->avPath)) {
55 1
			throw new InitException(
56 1
				\sprintf(
57 1
					'The antivirus executable could not be found at path "%s"',
58 1
					$this->avPath
59 1
				)
60 1
			);
61
		}
62 3
	}
63
	
64 3
	public function initScanner() {
65 3
		parent::initScanner();
66
		
67
		// using 2>&1 to grab the full command-line output.
68 3
		$cmd = $this->avPath . " " . $this->appConfig->getCmdline() . " - 2>&1";
69
		$descriptorSpec = [
70 3
			0 => ["pipe","r"], // STDIN
71 3
			1 => ["pipe","w"]  // STDOUT
72 3
		];
73
		
74 3
		$this->process = \proc_open($cmd, $descriptorSpec, $this->pipes);
75 3
		if (!\is_resource($this->process)) {
76
			throw new InitException(
77
				\sprintf('Error starting process "%s"', $cmd)
78
			);
79
		}
80 3
		$this->writeHandle = $this->pipes[0];
81 3
	}
82
	
83 3
	protected function shutdownScanner() {
84 3
		@\fclose($this->pipes[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...
85 3
		$output = \stream_get_contents($this->pipes[1]);
86 3
		@\fclose($this->pipes[1]);
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...
87
		
88 3
		$result = \proc_close($this->process);
89 3
		$this->logger->debug(
90 3
			'Exit code :: ' . $result . ' Response :: ' . $output,
91 3
			['app' => 'files_antivirus']
92 3
		);
93 3
		$this->status->parseResponse($output, $result);
94 3
	}
95
}
96