Completed
Pull Request — master (#50)
by Roeland
01:32
created

AvirWrapper::fopen()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 69
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 69
ccs 0
cts 47
cp 0
rs 6.9081
c 2
b 0
f 0
cc 7
eloc 47
nc 5
nop 2
crap 56

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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