Completed
Pull Request — master (#133)
by Victor
03:28
created

Local::getWriteHandle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
class Local extends \OCA\Files_Antivirus\Scanner{
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 5
	public function __construct($config){
32 5
		$this->appConfig = $config;
33
		// get the path to the executable
34 5
		$this->avPath = escapeshellcmd($this->appConfig->getAvPath());
35
36
		// check that the executable is available
37 5
		if (!file_exists($this->avPath)) {
38 2
			throw new \RuntimeException('The antivirus executable could not be found at ' . $this->avPath);
39
		}
40 3
	}
41
	
42 3
	public function initScanner(){
43 3
		parent::initScanner();
44
		
45
		// using 2>&1 to grab the full command-line output.
46 3
		$cmd = $this->avPath . " " . $this->appConfig->getCmdline() ." - 2>&1";
47
		$descriptorSpec = array(
48 3
			0 => ["pipe","r"], // STDIN
49
			1 => ["pipe","w"]  // STDOUT
50
		);
51
		
52 3
		$this->process = proc_open($cmd, $descriptorSpec, $this->pipes);
53 3
		if (!is_resource($this->process)) {
54
			throw new \RuntimeException('Error starting process');
55
		}
56 3
		$this->writeHandle = $this->pipes[0];
57 3
	}
58
	
59 3
	protected function shutdownScanner(){
60 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...
61 3
		$output = stream_get_contents($this->pipes[1]);
62 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...
63
		
64 3
		$result = proc_close($this->process);
65 3
		\OC::$server->getLogger()->debug(
66 3
			'Exit code :: ' . $result . ' Response :: ' . $output,
67 3
			['app' => 'files_antivirus']
68
		);
69 3
		$this->status->parseResponse($output, $result);
70 3
	}
71
}
72