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 External extends \OCA\Files_Antivirus\Scanner { |
15
|
|
|
|
16
|
|
|
// Daemon/socket mode |
17
|
|
|
private $useSocket; |
18
|
|
|
|
19
|
2 |
|
public function __construct(AppConfig $config){ |
20
|
2 |
|
$this->appConfig = $config; |
21
|
2 |
|
$this->useSocket = $this->appConfig->getAvMode() === 'socket'; |
22
|
2 |
|
} |
23
|
|
|
|
24
|
2 |
|
public function initScanner(){ |
25
|
2 |
|
parent::initScanner(); |
26
|
|
|
|
27
|
2 |
|
if ($this->useSocket){ |
28
|
|
|
$avSocket = $this->appConfig->getAvSocket(); |
29
|
|
|
$this->writeHandle = stream_socket_client('unix://' . $avSocket, $errno, $errstr, 5); |
30
|
|
|
if (!$this->getWriteHandle()) { |
31
|
|
|
throw new \RuntimeException('Cannot connect to "' . $avSocket . '": ' . $errstr . ' (code ' . $errno . ')'); |
32
|
|
|
} |
33
|
|
|
} else { |
34
|
2 |
|
$avHost = $this->appConfig->getAvHost(); |
35
|
2 |
|
$avPort = $this->appConfig->getAvPort(); |
36
|
2 |
|
if (!($avHost && $avPort)) { |
37
|
|
|
throw new \RuntimeException('The clamav port and host are not configured.'); |
38
|
|
|
} |
39
|
2 |
|
$this->writeHandle = ($avHost && $avPort) ? @fsockopen($avHost, $avPort) : false; |
40
|
2 |
|
if (!$this->getWriteHandle()) { |
41
|
|
|
throw new \RuntimeException('The clamav module is not configured for daemon mode.'); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// request scan from the daemon |
46
|
2 |
|
@fwrite($this->getWriteHandle(), "nINSTREAM\n"); |
|
|
|
|
47
|
2 |
|
} |
48
|
|
|
|
49
|
2 |
|
protected function shutdownScanner(){ |
50
|
2 |
|
@fwrite($this->getWriteHandle(), pack('N', 0)); |
|
|
|
|
51
|
2 |
|
$response = fgets($this->getWriteHandle()); |
52
|
2 |
|
\OC::$server->getLogger()->debug( |
53
|
2 |
|
'Response :: ' . $response, |
54
|
2 |
|
['app' => 'files_antivirus'] |
55
|
2 |
|
); |
56
|
2 |
|
@fclose($this->getWriteHandle()); |
|
|
|
|
57
|
|
|
|
58
|
2 |
|
$this->status->parseResponse($response); |
59
|
2 |
|
} |
60
|
|
|
|
61
|
2 |
|
protected function prepareChunk($data){ |
62
|
2 |
|
$chunkLength = pack('N', strlen($data)); |
63
|
2 |
|
return $chunkLength . $data; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: