Completed
Push — stable9.1 ( 2dd5fd...bfe92a )
by
unknown
17s
created

BackgroundScanner::getOwner()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.8449

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 6
cts 11
cp 0.5455
rs 9.4285
cc 3
eloc 10
nc 3
nop 1
crap 3.8449
1
<?php
2
/**
3
 * Copyright (c) 2012 Bart Visscher <[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 OCP\IL10N;
13
use OCP\Files\IRootFolder;
14
use OCP\IUser;
15
use OCP\IUserSession;
16
17
class BackgroundScanner {
18
19
	const BATCH_SIZE = 10;
20
21
	/** @var IRootFolder */
22
	protected $rootFolder;
23
24
	/** @var \OCP\Files\Folder[] */
25
	protected $userFolders;
26
27
	/** @var ScannerFactory */
28
	private $scannerFactory;
29
30
	/** @var IL10N */
31
	private $l10n;
32
33
	/** @var  AppConfig  */
34
	private $appConfig;
35
36
	/** @var string */
37
	protected $currentFilesystemUser;
38
39
	/** @var \OCP\IUserSession */
40
	protected $userSession;
41
42
	/**
43
	 * A constructor
44
	 *
45
	 * @param \OCA\Files_Antivirus\ScannerFactory $scannerFactory
46
	 * @param IL10N $l10n
47
	 * @param AppConfig $appConfig
48
	 * @param IRootFolder $rootFolder
49
	 * @param IUserSession $userSession
50
	 */
51 1
	public function __construct(ScannerFactory $scannerFactory,
52
								IL10N $l10n,
53
								AppConfig $appConfig,
54
								IRootFolder $rootFolder,
55
								IUserSession $userSession
56
	){
57 1
		$this->rootFolder = $rootFolder;
58 1
		$this->scannerFactory = $scannerFactory;
59 1
		$this->l10n = $l10n;
60 1
		$this->appConfig = $appConfig;
61 1
		$this->userSession = $userSession;
62 1
	}
63
	
64
	/**
65
	 * Background scanner main job
66
	 * @return null
67
	 */
68 1
	public function run(){
69
		// locate files that are not checked yet
70
		try {
71 1
			$result = $this->getFilesForScan();
72 1
		} catch(\Exception $e) {
73
			\OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']);
74
			return;
75
		}
76
77 1
		$cnt = 0;
78 1
		while (($row = $result->fetch()) && $cnt < self::BATCH_SIZE) {
79
			try {
80 1
				$fileId = $row['fileid'];
81 1
				$owner = $this->getOwner($fileId);
82
				/** @var IUser $owner */
83 1
				if (!$owner instanceof IUser){
0 ignored issues
show
Bug introduced by
The class OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
84 1
					continue;
85
				}
86
				$this->scanOneFile($owner, $fileId);
87
				// increased only for successfully scanned files
88
				$cnt = $cnt + 1;
89
			} catch (\Exception $e){
90
				\OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']);
91
			}
92
		}
93 1
		$this->tearDownFilesystem();
94 1
	}
95
96 1
	protected function getFilesForScan(){
97 1
		$dirMimeTypeId = \OC::$server->getMimeTypeLoader()->getId('httpd/unix-directory');
98 1
		$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
99
100 1
		$sizeLimit = intval($this->appConfig->getAvMaxFileSize());
101 1
		if ( $sizeLimit === -1 ){
102 1
			$sizeLimitExpr = $qb->expr()->neq('fc.size', $qb->expr()->literal('0'));
103 1
		} else {
104
			$sizeLimitExpr = $qb->expr()->andX(
105
				$qb->expr()->neq('fc.size', $qb->expr()->literal('0')),
106
				$qb->expr()->lt('fc.size', $qb->expr()->literal((string) $sizeLimit))
107
			);
108
		}
109
110 1
		$qb->select(['fc.fileid'])
111 1
			->from('filecache', 'fc')
112 1
			->leftJoin('fc', 'files_antivirus', 'fa', $qb->expr()->eq('fa.fileid', 'fc.fileid'))
113 1
			->innerJoin(
114 1
				'fc',
115 1
				'storages',
116 1
				'ss',
117 1
				$qb->expr()->andX(
118 1
					$qb->expr()->eq('fc.storage', 'ss.numeric_id'),
119 1
					$qb->expr()->orX(
120 1
						$qb->expr()->like('ss.id', $qb->expr()->literal('local::%')),
121 1
						$qb->expr()->like('ss.id', $qb->expr()->literal('home::%'))
122 1
					)
123 1
				)
124 1
			)
125 1
			->where(
126 1
				$qb->expr()->neq('fc.mimetype', $qb->expr()->literal($dirMimeTypeId))
127 1
			)
128 1
			->andWhere(
129 1
				$qb->expr()->orX(
130 1
					$qb->expr()->isNull('fa.fileid'),
131 1
					$qb->expr()->gt('fc.mtime', 'fa.check_time')
132 1
				)
133 1
			)
134 1
			->andWhere(
135 1
				$qb->expr()->like('fc.path', $qb->expr()->literal('files/%'))
136 1
			)
137 1
			->andWhere( $sizeLimitExpr )
138
		;
139 1
		return $qb->execute();
140
	}
141
142
	/**
143
	 * @param IUser $owner
144
	 * @param int $fileId
145
	 */
146
	protected function scanOneFile($owner, $fileId){
147
		$this->initFilesystemForUser($owner);
148
		$view = Filesystem::getView();
149
		$path = $view->getPath($fileId);
150
		if (!is_null($path)) {
151
			$item = new Item($this->l10n, $view, $path, $fileId);
152
			$scanner = $this->scannerFactory->getScanner();
153
			$status = $scanner->scan($item);
154
			$status->dispatch($item, true);
155
		}
156
	}
157
158
	/**
159
	 * @param int $fileId
160
	 * @return IUser|null
161
	 */
162 1
	protected function getOwner($fileId){
163 1
		$mountProviderCollection = \OC::$server->getMountProviderCollection();
164 1
		$mountCache = $mountProviderCollection->getMountCache();
165 1
		$mounts = $mountCache->getMountsForFileId($fileId);
166 1
		if (!empty($mounts)) {
167
			$mount = reset($mounts);
168
			$user = $mount->getUser();
169
			if ($user instanceof IUser) {
0 ignored issues
show
Bug introduced by
The class OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
170
				return $user;
171
			}
172
		}
173 1
		return null;
174
	}
175
176
	/**
177
	 * @param \OCP\IUser $user
178
	 * @return \OCP\Files\Folder
179
	 */
180
	protected function getUserFolder(IUser $user) {
181
		if (!isset($this->userFolders[$user->getUID()])) {
182
			$userFolder = $this->rootFolder->getUserFolder($user->getUID());
183
			$this->userFolders[$user->getUID()] = $userFolder;
184
		}
185
		return $this->userFolders[$user->getUID()];
186
	}
187
188
	/**
189
	 * @param IUser $user
190
	 */
191
	protected function initFilesystemForUser(IUser $user) {
192
		if ($this->currentFilesystemUser !== $user->getUID()) {
193
			if ($this->currentFilesystemUser !== '') {
194
				$this->tearDownFilesystem();
195
			}
196
			Filesystem::init($user->getUID(), '/' . $user->getUID() . '/files');
197
			$this->userSession->setUser($user);
198
			$this->currentFilesystemUser = $user->getUID();
199
			Filesystem::initMountPoints($user->getUID());
200
		}
201
	}
202
203
	/**
204
	 *
205
	 */
206 1
	protected function tearDownFilesystem(){
207 1
		$this->userSession->setUser(null);
208 1
		\OC_Util::tearDownFS();
209 1
	}
210
211
	/**
212
	 * @deprecated since  v8.0.0
213
	 */
214
	public static function check(){
215
	}
216
}
217