Completed
Push — stable10 ( bee328...ea0847 )
by
unknown
11s
created

AvirWrapper::fopen()   C

Complexity

Conditions 8
Paths 5

Size

Total Lines 67
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 53
CRAP Score 8

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 67
ccs 53
cts 53
cp 1
rs 6.6523
cc 8
eloc 46
nc 5
nop 2
crap 8

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