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