Completed
Push — master ( 6a0a5c...a7323b )
by Roeland
03:23 queued 02:02
created

ExternalKaspersky::writeChunk()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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