|
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
|
|
|
class Daemon extends AbstractScanner { |
|
13
|
|
|
|
|
14
|
2 |
|
public function initScanner(){ |
|
15
|
2 |
|
parent::initScanner(); |
|
16
|
2 |
|
|
|
17
|
|
|
$avHost = $this->appConfig->getAvHost(); |
|
18
|
2 |
|
$avPort = $this->appConfig->getAvPort(); |
|
19
|
2 |
|
$checks = [ |
|
20
|
|
|
'hostname' => $avHost, |
|
21
|
2 |
|
'port' => $avPort |
|
22
|
2 |
|
]; |
|
23
|
|
|
$errors = []; |
|
24
|
2 |
|
foreach ($checks as $key => $check) { |
|
25
|
2 |
|
if ($check === '') { |
|
26
|
|
|
$errors[] = sprintf( |
|
27
|
2 |
|
'Daemon mode requires a %s but it is empty.', |
|
28
|
2 |
|
$key |
|
29
|
2 |
|
); |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
2 |
|
|
|
33
|
|
|
if (count($errors)>0) { |
|
34
|
|
|
throw new InitException( |
|
35
|
|
|
'The app is not configured properly. ' . implode(' ', $errors) |
|
36
|
|
|
); |
|
37
|
2 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
$this->writeHandle = @fsockopen($avHost, $avPort); |
|
40
|
|
|
if (!$this->getWriteHandle()) { |
|
41
|
|
|
throw new InitException( |
|
42
|
|
|
sprintf( |
|
43
|
2 |
|
'Could not connect to host "%s" on port %d', $avHost, $avPort |
|
44
|
2 |
|
) |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// request scan from the daemon |
|
49
|
|
|
@fwrite($this->getWriteHandle(), "nINSTREAM\n"); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function shutdownScanner(){ |
|
53
|
2 |
|
@fwrite($this->getWriteHandle(), pack('N', 0)); |
|
|
|
|
|
|
54
|
2 |
|
$response = fgets($this->getWriteHandle()); |
|
55
|
|
|
$this->logger->debug( |
|
56
|
2 |
|
'Response :: ' . $response, |
|
57
|
2 |
|
['app' => 'files_antivirus'] |
|
58
|
2 |
|
); |
|
59
|
2 |
|
@fclose($this->getWriteHandle()); |
|
|
|
|
|
|
60
|
2 |
|
|
|
61
|
2 |
|
$this->status->parseResponse($response); |
|
62
|
|
|
} |
|
63
|
2 |
|
|
|
64
|
|
|
protected function prepareChunk($data){ |
|
65
|
2 |
|
$chunkLength = pack('N', strlen($data)); |
|
66
|
2 |
|
return $chunkLength . $data; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: