Completed
Pull Request — stable8.2 (#145)
by Victor
03:04
created

AvirWrapper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
c 3
b 0
f 0
lcom 1
cbo 2
dl 0
loc 97
ccs 45
cts 45
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isWritingMode() 0 9 1
C fopen() 0 42 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 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 4
	public function __construct($parameters) {
47 4
		parent::__construct($parameters);
48 4
		$this->scannerFactory = $parameters['scannerFactory'];
49 4
		$this->l10n = $parameters['l10n'];
50 4
		$this->logger = $parameters['logger'];
51 4
	}
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 4
	public function fopen($path, $mode){
60 4
		$stream = $this->storage->fopen($path, $mode);
61 4
		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
							}
78
79 2
							$this->unlink($path);
80
81 2
							if (App::isEnabled('files_trashbin')) {
82 2
								\OCA\Files_Trashbin\Storage::postRenameHook([]);
83 2
							}
84
											
85 2
							throw new InvalidContentException(
86 2
								$this->l10n->t(
87 2
									'Virus %s is detected in the file. Upload cannot be completed.',
88 2
									$status->getDetails()
89 2
								)
90 2
							);
91
						}
92 1
					}
93 2
				);
94 2
			} catch (\Exception $e){
95 2
				$message = 	implode(' ', [ __CLASS__, __METHOD__, $e->getMessage()]);
96 2
				$this->logger->warning($message);
97
			}
98 2
		}
99 4
		return $stream;
100
	}
101
	
102
	/**
103
	 * Checks whether passed mode is suitable for writing 
104
	 * @param string $mode
105
	 * @return bool
106
	 */
107 4
	private function isWritingMode($mode){
108
		// Strip unessential binary/text flags
109 4
		$cleanMode = str_replace(
110 4
			['t', 'b'],
111 4
			['', ''],
112
			$mode
113 4
		);
114 4
		return in_array($cleanMode, $this->writingModes);
115
	}
116
}
117