ExternalKaspersky::shutdownScanner()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * @copyright Copyright (c) 2020 Robin Appelman <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Files_Antivirus\Scanner;
25
26
use OCA\Files_Antivirus\AppConfig;
27
use OCA\Files_Antivirus\Status;
28
use OCA\Files_Antivirus\StatusFactory;
29
use OCP\Http\Client\IClientService;
30
use OCP\ILogger;
31
32
class ExternalKaspersky extends ScannerBase {
33
	/** @var IClientService IClientService */
34
	private $clientService;
35
	private $chunkSize;
36
37
	public function __construct(AppConfig $config, ILogger $logger, StatusFactory $statusFactory, IClientService $clientService) {
38
		parent::__construct($config, $logger, $statusFactory);
39
		$this->clientService = $clientService;
40
		$this->chunkSize = 10 * 1024 * 1024;
41
	}
42
43
	public function initScanner() {
44
		parent::initScanner();
45
46
		$avHost = $this->appConfig->getAvHost();
47
		$avPort = $this->appConfig->getAvPort();
48
49
		if (!($avHost && $avPort)) {
50
			throw new \RuntimeException('The Kaspersky port and host are not set up.');
51
		}
52
		$this->writeHandle = fopen("php://temp", 'w+');
53
	}
54
55
	protected function writeChunk($chunk) {
56
		if (ftell($this->writeHandle) > $this->chunkSize) {
57
			$this->scanBuffer();
58
			$this->writeHandle = fopen("php://temp", 'w+');
59
		}
60
		parent::writeChunk($chunk);
61
	}
62
63
	protected function scanBuffer() {
64
		rewind($this->writeHandle);
65
66
		$avHost = $this->appConfig->getAvHost();
67
		$avPort = $this->appConfig->getAvPort();
68
69
		$response = $this->clientService->newClient()->post("$avHost:$avPort/scanmemory", [
70
			'body' => $this->writeHandle,
71
			'headers' => [
72
				'X-KAV-Timeout' => '60000',
73
				'X-KAV-ProtocolVersion' => '1',
74
			],
75
			'connect_timeout' => 5,
76
		])->getBody();
77
78
		$this->logger->debug(
0 ignored issues
show
Deprecated Code introduced by
The method OCP\ILogger::debug() has been deprecated with message: 20.0.0 use \Psr\Log\LoggerInterface::debug

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
79
			'Response :: ' . $response,
80
			['app' => 'files_antivirus']
81
		);
82
83
		$response = trim($response);
84
85
		if (substr($response, 0, 5) === 'CLEAN' && $this->status->getNumericStatus() != Status::SCANRESULT_INFECTED) {
86
			$this->status->setNumericStatus(Status::SCANRESULT_CLEAN);
87
		} elseif (substr($response, 0, 11) === 'NON_SCANNED' && $this->status->getNumericStatus() != Status::SCANRESULT_INFECTED) {
88
			$this->status->setNumericStatus(Status::SCANRESULT_UNCHECKED);
89
			$this->status->setDetails($response);
90
		} else {
91
			$this->status->setNumericStatus(Status::SCANRESULT_INFECTED);
92
			if (strpos($response, "DETECT ") === 0) {
93
				$response = substr($response, 7);
94
			}
95
			$this->status->setDetails($response);
96
		}
97
	}
98
99
	protected function shutdownScanner() {
100
		$this->scanBuffer();
101
	}
102
}
103