Completed
Pull Request — master (#118)
by Victor
02:42
created

BackgroundScanner   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 16
Bugs 7 Features 0
Metric Value
wmc 16
c 16
b 7
f 0
lcom 2
cbo 4
dl 0
loc 152
ccs 0
cts 90
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B run() 0 66 6
A getOwner() 0 11 3
A getUserFolder() 0 7 2
A initFilesystemForUser() 0 10 3
A check() 0 2 1
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 OC\Files\View;
13
use OCP\Files\IRootFolder;
14
use OCP\IUser;
15
use OCP\IL10N;
16
17
use OCA\Files_Antivirus\Item;
18
19
class BackgroundScanner {
20
21
	const BATCH_SIZE = 10;
22
23
	/** @var IRootFolder */
24
	protected $rootFolder;
25
26
	/** @var \OCP\Files\Folder[] */
27
	protected $userFolders;
28
29
	/**
30
	 * @var ScannerFactory
31
	 */
32
	private $scannerFactory;
33
34
	
35
	/**
36
	 * @var IL10N
37
	 */
38
	private $l10n;
39
40
	/** @var string */
41
	protected $currentFilesystemUser;
42
43
	/**
44
	 * A constructor
45
	 *
46
	 * @param IRootFolder $rootFolder
47
	 * @param \OCA\Files_Antivirus\ScannerFactory $scannerFactory
48
	 * @param IL10N $l10n
49
	 */
50
	public function __construct(IRootFolder $rootFolder = null, ScannerFactory $scannerFactory, IL10N $l10n){
51
		$this->rootFolder = $rootFolder;
52
		$this->scannerFactory = $scannerFactory;
53
		$this->l10n = $l10n;
54
	}
55
	
56
	/**
57
	 * Background scanner main job
58
	 * @return null
59
	 */
60
	public function run(){
61
		// locate files that are not checked yet
62
		$dirMimeTypeId = \OC::$server->getMimeTypeLoader()->getId('httpd/unix-directory');
63
		try {
64
			$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
65
			$qb->select(['fc.fileid'])
66
				->from('filecache', 'fc')
67
				->leftJoin('fc', 'files_antivirus', 'fa', $qb->expr()->eq('fa.fileid', 'fc.fileid'))
68
				->innerJoin(
69
					'fc',
70
					'storages',
71
					'ss',
72
					$qb->expr()->andX(
73
						$qb->expr()->eq('fc.storage', 'ss.numeric_id'),
74
						$qb->expr()->orX(
75
							$qb->expr()->like('ss.id', $qb->expr()->literal('local::%')),
76
							$qb->expr()->like('ss.id', $qb->expr()->literal('home::%'))
77
						)
78
					)
79
				)
80
				->where(
81
					$qb->expr()->neq('fc.mimetype', $qb->expr()->literal($dirMimeTypeId))
82
				)
83
				->andWhere(
84
					$qb->expr()->orX(
85
						$qb->expr()->isNull('fa.fileid'),
86
						$qb->expr()->gt('fc.mtime', 'fa.check_time')
87
					)
88
				)
89
				->andWhere(
90
					$qb->expr()->like('fc.path', $qb->expr()->literal('files/%'))
91
				)
92
				->andWhere(
93
					$qb->expr()->neq('fc.size', '0')
94
				)
95
				->setMaxResults(self::BATCH_SIZE)
96
			;
97
			$result = $qb->execute();
98
		} catch(\Exception $e) {
99
			\OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']);
100
			return;
101
		}
102
103
		try {
104
			while ($row = $result->fetch()) {
105
				$fileId = $row['fileid'];
106
				$owner = $this->getOwner($fileId);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $owner is correct as $this->getOwner($fileId) (which targets OCA\Files_Antivirus\BackgroundScanner::getOwner()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
107
				if (!$owner){
108
					continue;
109
				}
110
				$this->initFilesystemForUser($owner);
111
				$view = new View('');
112
113
				$path = $view->getPath($fileId);
114
				if (!is_null($path)) {
115
					$item = new Item($this->l10n, $view, $path, $fileId);
116
					$scanner = $this->scannerFactory->getScanner();
117
					$status = $scanner->scan($item);
118
					$status->dispatch($item, true);
119
				}
120
			}
121
		} catch (\Exception $e){
122
			\OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']);
123
		}
124
		\OC_Util::tearDownFS();
125
	}
126
127
	protected function getOwner($fileId){
128
		$mountProviderCollection = \OC::$server->getMountProviderCollection();
129
		$mountCache = $mountProviderCollection->getMountCache();
130
		$mounts = $mountCache->getMountsForFileId($fileId);
131
		if (!empty($mounts)) {
132
			$user = $mounts[0]->getUser();
133
			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...
134
				return $user;
135
			}
136
		}
137
	}
138
139
	/**
140
	 * @param \OCP\IUser $user
141
	 * @return \OCP\Files\Folder
142
	 */
143
	protected function getUserFolder(IUser $user) {
144
		if (!isset($this->userFolders[$user->getUID()])) {
145
			$userFolder = $this->rootFolder->getUserFolder($user->getUID());
146
			$this->userFolders[$user->getUID()] = $userFolder;
147
		}
148
		return $this->userFolders[$user->getUID()];
149
	}
150
151
	/**
152
	 * @param IUser $user
153
	 */
154
	protected function initFilesystemForUser(IUser $user) {
155
		if ($this->currentFilesystemUser !== $user->getUID()) {
156
			if ($this->currentFilesystemUser !== '') {
157
				Filesystem::tearDown();
158
			}
159
			Filesystem::init($user->getUID(), '/' . $user->getUID() . '/files');
160
			$this->userSession->setUser($user);
0 ignored issues
show
Bug introduced by
The property userSession does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
161
			$this->currentFilesystemUser = $user->getUID();
162
		}
163
	}
164
165
	/**
166
	 * @deprecated since  v8.0.0
167
	 */
168
	public static function check(){
169
	}
170
}
171