AvirWrapper::isWritingMode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Copyright (c) 2014 Victor 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
namespace OCA\Files_Antivirus;
10
11
use OC\Files\Storage\Wrapper\Wrapper;
12
use OCA\Files_Antivirus\Activity\Provider;
13
use OCA\Files_Antivirus\AppInfo\Application;
14
use OCA\Files_Antivirus\Event\ScanStateEvent;
15
use OCA\Files_Antivirus\Scanner\ScannerFactory;
16
use OCP\Activity\IManager as ActivityManager;
17
use OCP\App;
18
use OCP\Files\InvalidContentException;
19
use OCP\IL10N;
20
use OCP\ILogger;
21
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
22
use OCA\Files_Trashbin\Trash\ITrashManager;
23
24
class AvirWrapper extends Wrapper {
25
	
26
	/**
27
	 * Modes that are used for writing
28
	 * @var array
29
	 */
30
	private $writingModes = ['r+', 'w', 'w+', 'a', 'a+', 'x', 'x+', 'c', 'c+'];
31
	
32
	/** @var ScannerFactory */
33
	protected $scannerFactory;
34
	
35
	/** @var IL10N */
36
	protected $l10n;
37
	
38
	/** @var ILogger */
39
	protected $logger;
40
41
	/** @var ActivityManager */
42
	protected $activityManager;
43
44
	/** @var bool */
45
	protected $isHomeStorage;
46
47
	/** @var bool */
48
	private $shouldScan = true;
49
50
	/**
51
	 * @param array $parameters
52
	 */
53 2
	public function __construct($parameters) {
54 2
		parent::__construct($parameters);
55 2
		$this->scannerFactory = $parameters['scannerFactory'];
56 2
		$this->l10n = $parameters['l10n'];
57 2
		$this->logger = $parameters['logger'];
58 2
		$this->activityManager = $parameters['activityManager'];
59 2
		$this->isHomeStorage = $parameters['isHomeStorage'];
60
61
		/** @var EventDispatcherInterface $eventDispatcher */
62 2
		$eventDispatcher = $parameters['eventDispatcher'];
63
		$eventDispatcher->addListener(ScanStateEvent::class, function (ScanStateEvent $event) {
64
			$this->shouldScan = $event->getState();
65 2
		});
66 2
	}
67
	
68
	/**
69
	 * Asynchronously scan data that are written to the file
70
	 * @param string $path
71
	 * @param string $mode
72
	 * @return resource | bool
73
	 */
74 1
	public function fopen($path, $mode) {
75 1
		$stream = $this->storage->fopen($path, $mode);
76
77
		/*
78
		 * Only check when
79
		 *  - it is a resource
80
		 *  - it is a writing mode
81
		 *  - if it is a homestorage it starts with files/
82
		 *  - if it is not a homestorage we always wrap (external storages)
83
		 */
84 1
		if ($this->shouldWrap($path) && is_resource($stream) && $this->isWritingMode($mode)) {
85 1
			$stream = $this->wrapSteam($path, $stream);
86
		}
87 1
		return $stream;
88
	}
89
90
	public function writeStream(string $path, $stream, int $size = null): int {
91
		if ($this->shouldWrap($path)) {
92
			$stream = $this->wrapSteam($path, $stream);
93
		}
94
		return parent::writeStream($path, $stream, $size);
95
	}
96
97 1
	private function shouldWrap(string $path): bool {
98 1
		return $this->shouldScan
99 1
			&& (!$this->isHomeStorage
100 1
				|| (strpos($path, 'files/') === 0
101 1
					|| strpos($path, '/files/') == 0)
102
			);
103
	}
104
105 1
	private function wrapSteam(string $path, $stream) {
106
		try {
107 1
			$scanner = $this->scannerFactory->getScanner();
108
			$scanner->initScanner();
109
			return CallbackReadDataWrapper::wrap(
110
				$stream,
111
				function ($count, $data) use ($scanner) {
112
					$scanner->onAsyncData($data);
113
				},
114
				function ($data) use ($scanner) {
115
					$scanner->onAsyncData($data);
116
				},
117
				function () use ($scanner, $path) {
118
					$status = $scanner->completeAsyncScan();
119
					if ((int)$status->getNumericStatus() === Status::SCANRESULT_INFECTED) {
120
						//prevent from going to trashbin
121
						if (App::isEnabled('files_trashbin')) {
0 ignored issues
show
Deprecated Code introduced by
The method OCP\App::isEnabled() has been deprecated with message: 13.0.0 use \OC::$server->getAppManager()->isEnabledForUser($appId)

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...
122
							/** @var ITrashManager $trashManager */
123
							$trashManager = \OC::$server->query(ITrashManager::class);
124
							$trashManager->pauseTrash();
125
						}
126
127
						$owner = $this->getOwner($path);
128
						$this->unlink($path);
129
130
						if (App::isEnabled('files_trashbin')) {
0 ignored issues
show
Deprecated Code introduced by
The method OCP\App::isEnabled() has been deprecated with message: 13.0.0 use \OC::$server->getAppManager()->isEnabledForUser($appId)

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...
131
							/** @var ITrashManager $trashManager */
132
							$trashManager = \OC::$server->query(ITrashManager::class);
133
							$trashManager->resumeTrash();
134
						}
135
							
136
						$this->logger->warning(
0 ignored issues
show
Deprecated Code introduced by
The method OCP\ILogger::warning() has been deprecated with message: 20.0.0 use \Psr\Log\LoggerInterface::warning

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...
137
							'Infected file deleted. ' . $status->getDetails()
138
							. ' Account: ' . $owner . ' Path: ' . $path,
139
							['app' => 'files_antivirus']
140
						);
141
142
						$activity = $this->activityManager->generateEvent();
143
						$activity->setApp(Application::APP_NAME)
144
							->setSubject(Provider::SUBJECT_VIRUS_DETECTED_UPLOAD, [$status->getDetails()])
145
							->setMessage(Provider::MESSAGE_FILE_DELETED)
146
							->setObject('', 0, $path)
147
							->setAffectedUser($owner)
148
							->setType(Provider::TYPE_VIRUS_DETECTED);
149
						$this->activityManager->publish($activity);
150
151
						$this->logger->error('Infected file deleted. ' . $status->getDetails() .
0 ignored issues
show
Deprecated Code introduced by
The method OCP\ILogger::error() has been deprecated with message: 20.0.0 use \Psr\Log\LoggerInterface::error

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...
152
							' File: ' . $path . ' Account: ' . $owner, ['app' => 'files_antivirus']);
153
154
						throw new InvalidContentException(
155
							$this->l10n->t(
156
								'Virus %s is detected in the file. Upload cannot be completed.',
157
								$status->getDetails()
158
							)
159
						);
160
					}
161
				}
162
			);
163 1
		} catch (\Exception $e) {
164 1
			$this->logger->logException($e);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\ILogger::logException() has been deprecated with message: 20.0.0 use the `exception` entry in the context of any method in \Psr\Log\LoggerInterface

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...
Documentation introduced by
$e is of type object<Exception>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
165
		}
166 1
		return $stream;
167
	}
168
	
169
	/**
170
	 * Checks whether passed mode is suitable for writing
171
	 * @param string $mode
172
	 * @return bool
173
	 */
174 1
	private function isWritingMode($mode) {
175
		// Strip unessential binary/text flags
176 1
		$cleanMode = str_replace(
177 1
			['t', 'b'],
178 1
			['', ''],
179
			$mode
180
		);
181 1
		return in_array($cleanMode, $this->writingModes);
182
	}
183
}
184