Completed
Push — master ( 0f1d39...28c19e )
by Joas
13s
created

Local::initScanner()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0054

Importance

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