Completed
Push — master ( 5c07ba...e845b9 )
by Victor
14s
created

AvirWrapper   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 98.48%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 17
c 6
b 0
f 0
lcom 1
cbo 3
dl 0
loc 146
ccs 65
cts 66
cp 0.9848
rs 10

4 Methods

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