Completed
Push — master ( 78adc8...c5d3da )
by Jörn Friedrich
07:54
created

AvirWrapper::isScannableSize()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 0
cp 0
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 6
nc 6
nop 1
crap 42
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\Filesystem;
12
use OC\Files\Storage\Wrapper\Wrapper;
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 = array('r+', 'w', 'w+', 'a', 'a+', 'x', 'x+', 'c', 'c+');
27
28
	/**
29
	 * @var AppConfig
30
	 */
31
	protected $appConfig;
32
33
	/**
34
	 * @var \OCA\Files_Antivirus\ScannerFactory
35
	 */
36
	protected $scannerFactory;
37
	
38
	/**
39
	 * @var IL10N 
40
	 */
41
	protected $l10n;
42
	
43
	/**
44
	 * @var ILogger;
45
	 */
46
	protected $logger;
47 2
48 2
	/**
49 2
	 * @param array $parameters
50 2
	 */
51 2
	public function __construct($parameters) {
52 2
		parent::__construct($parameters);
53
		$this->appConfig = $parameters['appConfig'];
54
		$this->scannerFactory = $parameters['scannerFactory'];
55
		$this->l10n = $parameters['l10n'];
56
		$this->logger = $parameters['logger'];
57
	}
58
	
59
	/**
60 3
	 * Asynchronously scan data that are written to the file
61 3
	 * @param string $path
62 3
	 * @param string $mode
63
	 * @return resource | bool
64 2
	 */
65 2
	public function fopen($path, $mode){
66 2
		$stream = $this->storage->fopen($path, $mode);
67
		if (is_resource($stream)
68 2
			&& $this->isWritingMode($mode)
69
			&& $this->isScannableSize(basename($path))
70 2
		) {
71 2
			try {
72 2
				$scanner = $this->scannerFactory->getScanner();
73 2
				$scanner->initScanner();
74 2
				return CallBackWrapper::wrap(
75
					$stream,
76 2
					null,
77 2
					function ($data) use ($scanner){
78 2
						$scanner->onAsyncData($data);
79
					}, 
80
					function () use ($scanner, $path) {
81
						$status = $scanner->completeAsyncScan();
82
						if (intval($status->getNumericStatus()) === \OCA\Files_Antivirus\Status::SCANRESULT_INFECTED){
83 2
							//prevent from going to trashbin
84 2
							if (App::isEnabled('files_trashbin')) {
85
								\OCA\Files_Trashbin\Storage::preRenameHook([
86 2
									Filesystem::signal_param_oldpath => '',
87 2
									Filesystem::signal_param_newpath => ''
88
								]);
89 2
							}
90 2
							
91 2
							$owner = $this->getOwner($path);
92 2
							$this->unlink($path);
93
94
							if (App::isEnabled('files_trashbin')) {
95 2
								\OCA\Files_Trashbin\Storage::postRenameHook([]);
96 2
							}
97 2
							$this->logger->warning(
98 2
								'Infected file deleted. ' . $status->getDetails()
99 2
								. ' Account: ' . $owner . ' Path: ' . $path,
100 2
								['app' => 'files_antivirus']
101
							);
102 2
103
							\OC::$server->getActivityManager()->publishActivity(
104 2
								'files_antivirus',
105 2
								Activity::SUBJECT_VIRUS_DETECTED,
106
								[$path, $status->getDetails()],
107
								Activity::MESSAGE_FILE_DELETED,
108 2
								[],
109 2
								$path,
110 2
								'',
111 2
								$owner,
112
								Activity::TYPE_VIRUS_DETECTED,
113
								Activity::PRIORITY_HIGH
114
							);
115 2
											
116
							throw new InvalidContentException(
117 2
								$this->l10n->t(
118 2
									'Virus %s is detected in the file. Upload cannot be completed.',
119 2
									$status->getDetails()
120
								)
121
							);
122 3
						}
123
					}
124
				);
125
			} catch (\Exception $e){
126
				$message = 	implode(' ', [ __CLASS__, __METHOD__, $e->getMessage()]);
127
				$this->logger->warning($message);
128
			}
129
		}
130 3
		return $stream;
131
	}
132 3
	
133 3
	/**
134 3
	 * Checks whether passed mode is suitable for writing 
135
	 * @param string $mode
136
	 * @return bool
137 3
	 */
138
	private function isWritingMode($mode){
139
		// Strip unessential binary/text flags
140
		$cleanMode = str_replace(
141
			['t', 'b'],
142
			['', ''],
143
			$mode
144
		);
145
		return in_array($cleanMode, $this->writingModes);
146
	}
147
148
	/**
149
	 * checks the size for webdav PUT requests. defaults to true
150
	 * @param $filename
151
	 * @return bool
152
	 */
153
	private function isScannableSize($filename) {
154
		$scanSizeLimit = intval($this->appConfig->getAvMaxFileSize());
155
		$size = false;
156
		
157
		// PUT via webdav
158
		if (isset($_SERVER['REQUEST_METHOD']) && isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] === 'PUT'){
159
			$size = intval($_SERVER['CONTENT_LENGTH']);
160
		}
161
162
		return $scanSizeLimit === -1 || $size === false || $scanSizeLimit >= $size;
163
	}
164
}
165