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