Completed
Pull Request — master (#50)
by Roeland
01:32
created

Local::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

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