Completed
Push — master ( aa875b...df65e3 )
by Roeland
02:29
created

AvirWrapper::isWritingMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 9.6666
c 1
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
crap 2
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\Scanner\ScannerFactory;
13
use OCP\App;
14
use OCP\IL10N;
15
use OCP\ILogger;
16
use OCP\Files\InvalidContentException;
17
use Icewind\Streams\CallbackWrapper;
18
19
20
class AvirWrapper extends Wrapper{
21
	
22
	/**
23
	 * Modes that are used for writing 
24
	 * @var array 
25
	 */
26
	private $writingModes = ['r+', 'w', 'w+', 'a', 'a+', 'x', 'x+', 'c', 'c+'];
27
	
28
	/**
29
	 * @var ScannerFactory
30
	 */
31
	protected $scannerFactory;
32
	
33
	/**
34
	 * @var IL10N 
35
	 */
36
	protected $l10n;
37
	
38
	/**
39
	 * @var ILogger;
40
	 */
41
	protected $logger;
42
43
	/**
44
	 * @param array $parameters
45
	 */
46 2
	public function __construct($parameters) {
47 2
		parent::__construct($parameters);
48 2
		$this->scannerFactory = $parameters['scannerFactory'];
49 2
		$this->l10n = $parameters['l10n'];
50 2
		$this->logger = $parameters['logger'];
51 2
	}
52
	
53
	/**
54
	 * Asynchronously scan data that are written to the file
55
	 * @param string $path
56
	 * @param string $mode
57
	 * @return resource | bool
58
	 */
59
	public function fopen($path, $mode){
60
		$stream = $this->storage->fopen($path, $mode);
61
		if (is_resource($stream) && $this->isWritingMode($mode)) {
62
			try {
63
				$scanner = $this->scannerFactory->getScanner();
64
				$scanner->initScanner();
65
				return CallbackWrapper::wrap(
66
					$stream,
67
					null,
68
					function ($data) use ($scanner){
69
						$scanner->onAsyncData($data);
70
					}, 
71
					function () use ($scanner, $path) {
72
						$status = $scanner->completeAsyncScan();
73
						if ((int)$status->getNumericStatus() === Status::SCANRESULT_INFECTED){
74
							//prevent from going to trashbin
75
							if (App::isEnabled('files_trashbin')) {
76
								\OCA\Files_Trashbin\Storage::preRenameHook([
77
									'oldpath' => '',
78
									'newpath' => ''
79
								]);
80
							}
81
							
82
							$owner = $this->getOwner($path);
83
							$this->unlink($path);
84
85
							if (App::isEnabled('files_trashbin')) {
86
								\OCA\Files_Trashbin\Storage::preRenameHook([
87
									'oldpath' => '',
88
									'newpath' => ''
89
								]);
90
							}
91
							$this->logger->warning(
92
								'Infected file deleted. ' . $status->getDetails()
93
								. ' Account: ' . $owner . ' Path: ' . $path,
94
								['app' => 'files_antivirus']
95
							);
96
97
							\OC::$server->getActivityManager()->publishActivity(
98
								'files_antivirus',
99
								Activity::SUBJECT_VIRUS_DETECTED,
100
								[$path, $status->getDetails()],
101
								Activity::MESSAGE_FILE_DELETED,
102
								[],
103
								$path,
104
								'',
105
								$owner,
106
								Activity::TYPE_VIRUS_DETECTED,
107
								Activity::PRIORITY_HIGH
108
							);
109
110
							$this->logger->error('Infected file deleted. ' . $status->getDetails() . 
111
							' File: ' . $path . ' Acccount: ' . $owner, ['app' => 'files_antivirus']);
112
113
							throw new InvalidContentException(
114
								$this->l10n->t(
115
									'Virus %s is detected in the file. Upload cannot be completed.',
116
									$status->getDetails()
117
								)
118
							);
119
						}
120
					}
121
				);
122
			} catch (\Exception $e){
123
				$this->logger->logException($e);
124
			}
125
		}
126
		return $stream;
127
	}
128
	
129
	/**
130
	 * Checks whether passed mode is suitable for writing 
131
	 * @param string $mode
132
	 * @return bool
133
	 */
134
	private function isWritingMode($mode){
135
		// Strip unessential binary/text flags
136
		$cleanMode = str_replace(
137
			['t', 'b'],
138
			['', ''],
139
			$mode
140
		);
141
		return in_array($cleanMode, $this->writingModes);
142
	}
143
}
144