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