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
|
|
|
use OCA\Files_Antivirus\AppConfig; |
13
|
|
|
use OCP\ILogger; |
14
|
|
|
|
15
|
|
|
class Local extends AbstractScanner{ |
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
|
4 |
|
public function __construct(AppConfig $config, ILogger $logger){ |
35
|
4 |
|
parent::__construct($config, $logger); |
36
|
|
|
|
37
|
|
|
// get the path to the executable |
38
|
4 |
|
$this->avPath = escapeshellcmd($this->appConfig->getAvPath()); |
39
|
|
|
|
40
|
4 |
|
if (!file_exists($this->avPath)) { |
41
|
1 |
|
throw new InitException( |
42
|
1 |
|
sprintf( |
43
|
1 |
|
'The antivirus executable could not be found at path "%s"', |
44
|
1 |
|
$this->avPath |
45
|
1 |
|
) |
46
|
1 |
|
); |
47
|
|
|
} |
48
|
3 |
|
} |
49
|
|
|
|
50
|
3 |
|
public function initScanner(){ |
51
|
3 |
|
parent::initScanner(); |
52
|
|
|
|
53
|
|
|
// using 2>&1 to grab the full command-line output. |
54
|
3 |
|
$cmd = $this->avPath . " " . $this->appConfig->getCmdline() ." - 2>&1"; |
55
|
|
|
$descriptorSpec = array( |
56
|
3 |
|
0 => ["pipe","r"], // STDIN |
57
|
3 |
|
1 => ["pipe","w"] // STDOUT |
58
|
3 |
|
); |
59
|
|
|
|
60
|
3 |
|
$this->process = proc_open($cmd, $descriptorSpec, $this->pipes); |
61
|
3 |
|
if (!is_resource($this->process)) { |
62
|
|
|
throw new InitException( |
63
|
|
|
sprintf('Error starting process "%s"', $cmd) |
64
|
|
|
); |
65
|
|
|
} |
66
|
3 |
|
$this->writeHandle = $this->pipes[0]; |
67
|
3 |
|
} |
68
|
|
|
|
69
|
3 |
|
protected function shutdownScanner(){ |
70
|
3 |
|
@fclose($this->pipes[0]); |
|
|
|
|
71
|
3 |
|
$output = stream_get_contents($this->pipes[1]); |
72
|
3 |
|
@fclose($this->pipes[1]); |
|
|
|
|
73
|
|
|
|
74
|
3 |
|
$result = proc_close($this->process); |
75
|
3 |
|
$this->logger->debug( |
76
|
3 |
|
'Exit code :: ' . $result . ' Response :: ' . $output, |
77
|
3 |
|
['app' => 'files_antivirus'] |
78
|
3 |
|
); |
79
|
3 |
|
$this->status->parseResponse($output, $result); |
80
|
3 |
|
} |
81
|
|
|
} |
82
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: