Completed
Pull Request — stable8.2 (#145)
by Victor
02:36
created

BackgroundScanner   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 84.78%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 10
c 4
b 1
f 0
lcom 1
cbo 5
dl 0
loc 102
ccs 39
cts 46
cp 0.8478
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B run() 0 38 6
A initFS() 0 12 2
A check() 0 3 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 OCP\IUserManager;
12
use OCP\IL10N;
13
14
class BackgroundScanner {
15
16
	/** @var ScannerFactory */
17
	private $scannerFactory;
18
	
19
	/** @var IUserManager */
20
	private $userManager;
21
22
	/** @var IL10N */
23
	private $l10n;
24
25
	/** @var  AppConfig  */
26
	private $appConfig;
27
28
	/** @var string */
29
	protected $currentFilesystemUser;
30
31
	/** @var \OCP\IUserSession */
32
	protected $userSession;
33
34
	/**
35
	 * A constructor
36
	 *
37
	 * @param \OCA\Files_Antivirus\ScannerFactory $scannerFactory
38
	 * @param AppConfig $appConfig
39
	 * @param IUserManager $userManager
40
	 * @param IL10N $l10n
41
	 */
42 1
	public function __construct(ScannerFactory $scannerFactory, AppConfig $appConfig, IUserManager $userManager, IL10N $l10n){
43 1
		$this->scannerFactory = $scannerFactory;
44 1
		$this->userManager = $userManager;
45 1
		$this->l10n = $l10n;
46 1
		$this->appConfig = $appConfig;
47 1
	}
48
	
49
	/**
50
	 * Background scanner main job
51
	 * @return null
52
	 */
53 1
	public function run(){
54 1
		if (!$this->initFS()) {
55
			return;
56
		}
57
		// locate files that are not checked yet
58 1
		$dirMimetypeId = \OC::$server->getMimeTypeLoader()->getId('httpd/unix-directory');
59
		$sql = 'SELECT `*PREFIX*filecache`.`fileid`, `*PREFIX*storages`.*'
60
			.' FROM `*PREFIX*filecache`'
61 1
			.' LEFT JOIN `*PREFIX*files_antivirus` ON `*PREFIX*files_antivirus`.`fileid` = `*PREFIX*filecache`.`fileid`'
62 1
			.' JOIN `*PREFIX*storages` ON `*PREFIX*storages`.`numeric_id` = `*PREFIX*filecache`.`storage`'
63 1
			.' WHERE `mimetype` != ?'
64 1
			.' AND (`*PREFIX*storages`.`id` LIKE ? OR `*PREFIX*storages`.`id` LIKE ?)'
65 1
			.' AND (`*PREFIX*files_antivirus`.`fileid` IS NULL OR `mtime` > `check_time`)'
66 1
			.' AND `path` LIKE ?'
67 1
			.' AND `*PREFIX*filecache`.`size` != 0';
68 1
		$stmt = \OCP\DB::prepare($sql, 5);
69
		try {
70 1
			$result = $stmt->execute(array($dirMimetypeId, 'local::%', 'home::%', 'files/%'));
71 1
			if (\OCP\DB::isError($result)) {
72
				\OCP\Util::writeLog('files_antivirus', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
73
				return;
74
			}
75 1
		} catch(\Exception $e) {
76
			\OCP\Util::writeLog('files_antivirus', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR);
77
			return;
78
		}
79 1
		$view = new \OC\Files\View('/');
80 1
		while ($row = $result->fetchRow()) {
81 1
			$path = $view->getPath($row['fileid']);
82 1
			if (!is_null($path)) {
83 1
				$item = new Item($this->l10n, $view, $path, $row['fileid']);
84 1
				$scanner = $this->scannerFactory->getScanner();
85 1
				$status = $scanner->scan($item);					
86 1
				$status->dispatch($item, true);
87 1
			}
88 1
		}
89 1
		\OC_Util::tearDownFS();
90 1
	}
91
	
92
	/**
93
	 * A hack to access files and views. Better than before.
94
	 */
95 1
	protected function initFS(){
96
		//Need any valid user to mount FS
97 1
		$results = $this->userManager->search('', 2, 0);
98 1
		$anyUser = array_pop($results);
99 1
		if (is_null($anyUser)) {
100
			\OCP\Util::writeLog('files_antivirus', "Failed to setup file system", \OCP\Util::ERROR);
101
			return false;
102
		}
103 1
		\OC_Util::tearDownFS();
104 1
		\OC_Util::setupFS($anyUser->getUID());
105 1
		return true;
106
	}
107
108
	
109
	/**
110
	 * @deprecated 
111
	 */
112
	public static function check(){
113
		return \OCA\Files_Antivirus\Cron\Task::run();
114
	}
115
}
116