Completed
Pull Request — master (#76)
by
unknown
01:53
created

AvirWrapper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 35.09%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 9
c 4
b 0
f 0
lcom 1
cbo 2
dl 0
loc 118
ccs 20
cts 57
cp 0.3509
rs 10

3 Methods

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