Completed
Push — stable8.1 ( eec587...17e2b5 )
by Victor
01:57
created

BackgroundScanner   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 91.3%

Importance

Changes 10
Bugs 6 Features 0
Metric Value
wmc 10
c 10
b 6
f 0
lcom 1
cbo 5
dl 0
loc 101
ccs 42
cts 46
cp 0.913
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B run() 0 37 5
A initFS() 0 7 1
A getDirectoryMimetype() 0 6 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
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
		$this->initFS();
51
		// locate files that are not checked yet
52 1
		$dirMimetypeId = $this->getDirectoryMimetype();
53
		$sql = 'SELECT `*PREFIX*filecache`.`fileid`, `*PREFIX*storages`.*'
54
			.' FROM `*PREFIX*filecache`'
55 1
			.' LEFT JOIN `*PREFIX*files_antivirus` ON `*PREFIX*files_antivirus`.`fileid` = `*PREFIX*filecache`.`fileid`'
56 1
			.' JOIN `*PREFIX*storages` ON `*PREFIX*storages`.`numeric_id` = `*PREFIX*filecache`.`storage`'
57 1
			.' WHERE `mimetype` != ?'
58 1
			.' AND (`*PREFIX*storages`.`id` LIKE ? OR `*PREFIX*storages`.`id` LIKE ?)'
59 1
			.' AND (`*PREFIX*files_antivirus`.`fileid` IS NULL OR `mtime` > `check_time`)'
60 1
			.' AND `path` LIKE ?'
61 1
			.' AND `*PREFIX*filecache`.`size` != 0';
62 1
		$stmt = \OCP\DB::prepare($sql, 5);
63
		try {
64 1
			$result = $stmt->execute(array($dirMimetypeId, 'local::%', 'home::%', 'files/%'));
65 1
			if (\OCP\DB::isError($result)) {
66
				\OCP\Util::writeLog('files_antivirus', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage($result), \OCP\Util::ERROR);
67
				return;
68
			}
69 1
		} catch(\Exception $e) {
70
			\OCP\Util::writeLog('files_antivirus', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR);
71
			return;
72
		}
73
	
74 1
		$view = new \OC\Files\View('/');
75 1
		while ($row = $result->fetchRow()) {
76 1
			$path = $view->getPath($row['fileid']);
77 1
			if (!is_null($path)) {
78 1
				$item = new Item($this->l10n, $view, $path, $row['fileid']);
79 1
				$scanner = $this->scannerFactory->getScanner();
80 1
				$status = $scanner->scan($item);					
81 1
				$status->dispatch($item, true);
82 1
			}
83 1
		}
84 1
		\OC_Util::tearDownFS();
85 1
	}
86
	
87
	/**
88
	 * A hack to access files and views. Better than before.
89
	 */
90 1
	protected function initFS(){
91
		//Need any valid user to mount FS
92 1
		$results = $this->userManager->search('', 2, 0);
93 1
		$anyUser = array_pop($results);
94 1
		\OC_Util::tearDownFS();
95 1
		\OC_Util::setupFS($anyUser->getUID());
96 1
	}
97
98
99
	/**
100
	 * Get a mimetypeId for httpd/unix-directory
101
	 * @return int
102
	 */
103 1
	protected function getDirectoryMimetype(){
104 1
		$storage = \OC\Files\Filesystem::getStorage('');
105 1
		$cache = $storage->getCache('');
106 1
		$dirMimetypeId = $cache->getMimetypeId('httpd/unix-directory');
107 1
		return $dirMimetypeId ? $dirMimetypeId : 0;
108
	}
109
	
110
	/**
111
	 * @deprecated 
112
	 */
113
	public static function check(){
114
		return \OCA\Files_Antivirus\Cron\Task::run();
115
	}
116
}
117