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
|
|
|
use OCA\Files_Antivirus\AppConfig; |
13
|
|
|
use OCP\ILogger; |
14
|
|
|
|
15
|
|
|
class Daemon extends AbstractScanner { |
16
|
|
|
|
17
|
|
|
public function __construct(AppConfig $config, ILogger $logger) { |
18
|
|
|
parent::__construct($config, $logger); |
19
|
|
|
|
20
|
|
|
$avHost = $this->appConfig->getAvHost(); |
21
|
|
|
$avPort = $this->appConfig->getAvPort(); |
22
|
|
|
$checks = [ |
23
|
|
|
'hostname' => $avHost, |
24
|
|
|
'port' => $avPort |
25
|
|
|
]; |
26
|
|
|
$errors = []; |
27
|
|
|
foreach ($checks as $key => $check) { |
28
|
|
|
if ($check === '') { |
29
|
|
|
$errors[] = sprintf( |
30
|
|
|
'Daemon mode requires a %s but it is empty.', |
31
|
|
|
$key |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if (count($errors) > 0) { |
37
|
|
|
throw new InitException( |
38
|
|
|
'The app is not configured properly. ' . implode(' ', $errors) |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function initScanner(){ |
44
|
|
|
parent::initScanner(); |
45
|
|
|
$this->writeHandle = @fsockopen($avHost, $avPort); |
|
|
|
|
46
|
|
|
if (!$this->getWriteHandle()) { |
47
|
|
|
throw new InitException( |
48
|
|
|
sprintf( |
49
|
|
|
'Could not connect to host "%s" on port %d', $avHost, $avPort |
50
|
|
|
) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
// request scan from the daemon |
54
|
|
|
@fwrite($this->getWriteHandle(), "nINSTREAM\n"); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function shutdownScanner(){ |
58
|
|
|
@fwrite($this->getWriteHandle(), pack('N', 0)); |
|
|
|
|
59
|
|
|
$response = fgets($this->getWriteHandle()); |
60
|
|
|
$this->logger->debug( |
61
|
|
|
'Response :: ' . $response, |
62
|
|
|
['app' => 'files_antivirus'] |
63
|
|
|
); |
64
|
|
|
@fclose($this->getWriteHandle()); |
|
|
|
|
65
|
|
|
|
66
|
|
|
$this->status->parseResponse($response); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function prepareChunk($data){ |
70
|
|
|
$chunkLength = pack('N', strlen($data)); |
71
|
|
|
return $chunkLength . $data; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.