Completed
Push — master ( b32497...b91e8b )
by Roeland
16s queued 12s
created

ExternalKaspersky::shutdownScanner()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 0
cts 26
cp 0
rs 9.44
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 12
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
35
	public function __construct(AppConfig $config, ILogger $logger, StatusFactory $statusFactory, IClientService $clientService) {
36
		parent::__construct($config, $logger, $statusFactory);
37
		$this->clientService = $clientService;
38
	}
39
40
	public function initScanner() {
41
		parent::initScanner();
42
43
		$avHost = $this->appConfig->getAvHost();
44
		$avPort = $this->appConfig->getAvPort();
45
46
		if (!($avHost && $avPort)) {
47
			throw new \RuntimeException('The Kaspersky port and host are not set up.');
48
		}
49
		$this->writeHandle = fopen("php://temp", 'w+');
50
	}
51
52
	protected function shutdownScanner() {
53
		rewind($this->writeHandle);
54
55
		$avHost = $this->appConfig->getAvHost();
56
		$avPort = $this->appConfig->getAvPort();
57
58
		$response = $this->clientService->newClient()->post("$avHost:$avPort/scanmemory", [
59
			'body' => $this->writeHandle,
60
			'headers' => [
61
				'X-KAV-Timeout' => '60000',
62
				'X-KAV-ProtocolVersion' => '1',
63
			],
64
			'connect_timeout' => 5,
65
		])->getBody();
66
67
		$this->logger->debug(
68
			'Response :: ' . $response,
69
			['app' => 'files_antivirus']
70
		);
71
72
		if (trim($response) === 'CLEAN') {
73
			$this->status->setNumericStatus(Status::SCANRESULT_CLEAN);
74
		} else {
75
			$this->status->setNumericStatus(Status::SCANRESULT_INFECTED);
76
			if (strpos($response, "DETECT ") === 0) {
77
				$response = substr($response, 7);
78
			}
79
			$this->status->setDetails($response);
80
		}
81
	}
82
}
83