Completed
Push — master ( 28c19e...a4e9ca )
by Joas
04:02
created

AvirWrapper::fopen()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 70
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 50
CRAP Score 7.0601

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 70
ccs 50
cts 56
cp 0.8929
rs 6.8519
cc 7
eloc 48
nc 5
nop 2
crap 7.0601

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\IConfig;
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 \OCA\Files_Antivirus\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 2
	public function fopen($path, $mode){
60 2
		$stream = $this->storage->fopen($path, $mode);
61 2
		if (is_resource($stream) && $this->isWritingMode($mode)) {
62
			try {
63 2
				$scanner = $this->scannerFactory->getScanner();
64 2
				$scanner->initScanner();
65 2
				return CallBackWrapper::wrap(
66 2
					$stream,
67 2
					null,
68
					function ($data) use ($scanner){
69 2
						$scanner->onAsyncData($data);
70 2
					}, 
71 2
					function () use ($scanner, $path) {
72 2
						$status = $scanner->completeAsyncScan();
73 2
						if (intval($status->getNumericStatus()) === \OCA\Files_Antivirus\Status::SCANRESULT_INFECTED){
74
							//prevent from going to trashbin
75 2
							if (App::isEnabled('files_trashbin')) {
76 2
								\OCA\Files_Trashbin\Storage::preRenameHook([
77 2
									'oldpath' => '',
78
									'newpath' => ''
79 2
								]);
80 2
							}
81
							
82 2
							$owner = $this->getOwner($path);
83 2
							$this->unlink($path);
84
85 2
							if (App::isEnabled('files_trashbin')) {
86 2
								\OCA\Files_Trashbin\Storage::preRenameHook([
87 2
									'oldpath' => '',
88
									'newpath' => ''
89 2
								]);
90 2
							}
91 2
							$this->logger->warning(
92 2
								'Infected file deleted. ' . $status->getDetails()
93 2
								. ' Account: ' . $owner . ' Path: ' . $path,
94 2
								['app' => 'files_antivirus']
95 2
							);
96
97 2
							\OC::$server->getActivityManager()->publishActivity(
98 2
								'files_antivirus',
99 2
								Activity::SUBJECT_VIRUS_DETECTED,
100 2
								[$path, $status->getDetails()],
101 2
								Activity::MESSAGE_FILE_DELETED,
102 2
								[],
103 2
								$path,
104 2
								'',
105 2
								$owner,
106 2
								Activity::TYPE_VIRUS_DETECTED,
107
								Activity::PRIORITY_HIGH
108 2
							);
109
110 2
							$this->logger->error('Infected file deleted. ' . $status->getDetails() . 
111 2
							' File: ' . $path . ' Acccount: ' . $owner, ['app' => 'files_antivirus']);
112
113 2
							throw new InvalidContentException(
114 2
								$this->l10n->t(
115 2
									'Virus %s is detected in the file. Upload cannot be completed.',
116 2
									$status->getDetails()
117 2
								)
118 2
							);
119
						}
120
					}
121 2
				);
122
			} catch (\Exception $e){
123
				var_dump($e->getMessage());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($e->getMessage()); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
124
				$this->logger->logException($e);
125
			}
126
		}
127
		return $stream;
128
	}
129
	
130
	/**
131
	 * Checks whether passed mode is suitable for writing 
132
	 * @param string $mode
133
	 * @return bool
134
	 */
135 2
	private function isWritingMode($mode){
136
		// Strip unessential binary/text flags
137 2
		$cleanMode = str_replace(
138 2
			['t', 'b'],
139 2
			['', ''],
140
			$mode
141 2
		);
142 2
		return in_array($cleanMode, $this->writingModes);
143
	}
144
}
145