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\StatusFactory; |
14
|
|
|
use OCP\ILogger; |
15
|
|
|
|
16
|
|
|
class Local extends ScannerBase{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $avPath; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* STDIN and STDOUT descriptors |
25
|
|
|
* @var array of resources |
26
|
|
|
*/ |
27
|
|
|
private $pipes = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Process handle |
31
|
|
|
* @var resource |
32
|
|
|
*/ |
33
|
|
|
private $process; |
34
|
|
|
|
35
|
|
|
public function __construct(AppConfig $config, ILogger $logger, StatusFactory $statusFactory) { |
36
|
|
|
parent::__construct($config, $logger, $statusFactory); |
37
|
|
|
|
38
|
|
|
// get the path to the executable |
39
|
|
|
$this->avPath = escapeshellcmd($this->appConfig->getAvPath()); |
40
|
|
|
|
41
|
|
|
// check that the executable is available |
42
|
|
|
if (!file_exists($this->avPath)) { |
43
|
|
|
throw new \RuntimeException('The antivirus executable could not be found at ' . $this->avPath); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function initScanner(){ |
48
|
|
|
parent::initScanner(); |
49
|
|
|
|
50
|
|
|
// using 2>&1 to grab the full command-line output. |
51
|
|
|
$cmd = $this->avPath . ' ' . $this->appConfig->getCmdline() . ' - 2>&1'; |
52
|
|
|
$descriptorSpec = array( |
53
|
|
|
0 => ['pipe', 'r'], // STDIN |
54
|
|
|
1 => ['pipe', 'w'] // STDOUT |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
$this->process = proc_open($cmd, $descriptorSpec, $this->pipes); |
58
|
|
|
if (!is_resource($this->process)) { |
59
|
|
|
throw new \RuntimeException('Error starting process'); |
60
|
|
|
} |
61
|
|
|
$this->writeHandle = $this->pipes[0]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function shutdownScanner(){ |
65
|
|
|
@fclose($this->pipes[0]); |
|
|
|
|
66
|
|
|
$output = stream_get_contents($this->pipes[1]); |
67
|
|
|
@fclose($this->pipes[1]); |
|
|
|
|
68
|
|
|
|
69
|
|
|
$result = proc_close($this->process); |
70
|
|
|
$this->logger->debug( |
71
|
|
|
'Exit code :: ' . $result . ' Response :: ' . $output, |
72
|
|
|
['app' => 'files_antivirus'] |
73
|
|
|
); |
74
|
|
|
$this->status->parseResponse($output, $result); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: