|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ownCloud - Files_antivirus |
|
4
|
|
|
* |
|
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
6
|
|
|
* later. See the COPYING file. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Viktar Dubiniuk <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* @copyright Viktar Dubiniuk 2014-2018 |
|
11
|
|
|
* @license AGPL-3.0 |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace OCA\Files_Antivirus\Scanner; |
|
15
|
|
|
|
|
16
|
|
|
use OCA\Files_Antivirus\AppConfig; |
|
17
|
|
|
use OCP\ILogger; |
|
18
|
|
|
|
|
19
|
|
|
class Local extends AbstractScanner { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $avPath; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* STDIN and STDOUT descriptors |
|
28
|
|
|
* |
|
29
|
|
|
* @var array of resources |
|
30
|
|
|
*/ |
|
31
|
|
|
private $pipes = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Process handle |
|
35
|
|
|
* |
|
36
|
|
|
* @var resource |
|
37
|
|
|
*/ |
|
38
|
|
|
private $process; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Local constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param AppConfig $config |
|
44
|
|
|
* @param ILogger $logger |
|
45
|
|
|
* |
|
46
|
|
|
* @throws InitException |
|
47
|
|
|
*/ |
|
48
|
4 |
|
public function __construct(AppConfig $config, ILogger $logger) { |
|
49
|
4 |
|
parent::__construct($config, $logger); |
|
50
|
|
|
|
|
51
|
|
|
// get the path to the executable |
|
52
|
4 |
|
$this->avPath = \escapeshellcmd($this->appConfig->getAvPath()); |
|
53
|
|
|
|
|
54
|
4 |
|
if (!\file_exists($this->avPath)) { |
|
55
|
1 |
|
throw new InitException( |
|
56
|
1 |
|
\sprintf( |
|
57
|
1 |
|
'The antivirus executable could not be found at path "%s"', |
|
58
|
1 |
|
$this->avPath |
|
59
|
1 |
|
) |
|
60
|
1 |
|
); |
|
61
|
|
|
} |
|
62
|
3 |
|
} |
|
63
|
|
|
|
|
64
|
3 |
|
public function initScanner() { |
|
65
|
3 |
|
parent::initScanner(); |
|
66
|
|
|
|
|
67
|
|
|
// using 2>&1 to grab the full command-line output. |
|
68
|
3 |
|
$cmd = $this->avPath . " " . $this->appConfig->getCmdline() . " - 2>&1"; |
|
69
|
|
|
$descriptorSpec = [ |
|
70
|
3 |
|
0 => ["pipe","r"], // STDIN |
|
71
|
3 |
|
1 => ["pipe","w"] // STDOUT |
|
72
|
3 |
|
]; |
|
73
|
|
|
|
|
74
|
3 |
|
$this->process = \proc_open($cmd, $descriptorSpec, $this->pipes); |
|
75
|
3 |
|
if (!\is_resource($this->process)) { |
|
76
|
|
|
throw new InitException( |
|
77
|
|
|
\sprintf('Error starting process "%s"', $cmd) |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
3 |
|
$this->writeHandle = $this->pipes[0]; |
|
81
|
3 |
|
} |
|
82
|
|
|
|
|
83
|
3 |
|
protected function shutdownScanner() { |
|
84
|
3 |
|
@\fclose($this->pipes[0]); |
|
|
|
|
|
|
85
|
3 |
|
$output = \stream_get_contents($this->pipes[1]); |
|
86
|
3 |
|
@\fclose($this->pipes[1]); |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
3 |
|
$result = \proc_close($this->process); |
|
89
|
3 |
|
$this->logger->debug( |
|
90
|
3 |
|
'Exit code :: ' . $result . ' Response :: ' . $output, |
|
91
|
3 |
|
['app' => 'files_antivirus'] |
|
92
|
3 |
|
); |
|
93
|
3 |
|
$this->status->parseResponse($output, $result); |
|
94
|
3 |
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: