Completed
Pull Request — master (#195)
by Victor
14:02 queued 12:49
created

Local::initScanner()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.032

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 8
cts 10
cp 0.8
rs 9.4285
nc 2
cc 2
eloc 11
nop 0
crap 2.032
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 Local extends AbstractScanner{
13
	
14
	/**
15
	 * @var string
16
	 */
17
	protected $avPath;
18
	
19
	/**
20
	 * STDIN and STDOUT descriptors
21
	 * @var array of resources
22
	 */
23
	private $pipes = [];
24
	
25
	/**
26
	 * Process handle
27
	 * @var resource
28
	 */
29
	private $process;
30
	
31 4
	public function __construct($config){
32 4
		$this->appConfig = $config;
33
		// get the path to the executable
34 4
		$this->avPath = escapeshellcmd($this->appConfig->getAvPath());
35
36 4
		if (!file_exists($this->avPath)) {
37 2
			throw new InitException(
38 2
				sprintf(
39 2
					'The antivirus executable could not be found at path "%s"',
40 2
					$this->avPath
41
				)
42
			);
43
		}
44 2
	}
45
	
46 2
	public function initScanner(){
47 2
		parent::initScanner();
48
		
49
		// using 2>&1 to grab the full command-line output.
50 2
		$cmd = $this->avPath . " " . $this->appConfig->getCmdline() ." - 2>&1";
51
		$descriptorSpec = array(
52 2
			0 => ["pipe","r"], // STDIN
53
			1 => ["pipe","w"]  // STDOUT
54
		);
55
		
56 2
		$this->process = proc_open($cmd, $descriptorSpec, $this->pipes);
57 2
		if (!is_resource($this->process)) {
58
			throw new InitException(
59
				sprintf('Error starting process "%s"', $cmd)
60
			);
61
		}
62 2
		$this->writeHandle = $this->pipes[0];
63 2
	}
64
	
65 2
	protected function shutdownScanner(){
66 2
		@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...
67 2
		$output = stream_get_contents($this->pipes[1]);
68 2
		@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...
69
		
70 2
		$result = proc_close($this->process);
71 2
		\OC::$server->getLogger()->debug(
72 2
			'Exit code :: ' . $result . ' Response :: ' . $output,
73 2
			['app' => 'files_antivirus']
74
		);
75 2
		$this->status->parseResponse($output, $result);
76 2
	}
77
}
78