Completed
Push — stable9 ( 66c84e...e336ce )
by Victor
7s
created

BackgroundScanner::run()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 39
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 6.151

Importance

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