|
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
|
1 |
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
// locate files that are not checked yet |
|
54
|
|
|
$dirMimeTypeId = \OC::$server->getMimeTypeLoader()->getId('httpd/unix-directory'); |
|
55
|
|
|
try { |
|
56
|
|
|
$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
57
|
|
|
$qb->select(['fc.fileid']) |
|
58
|
|
|
->from('filecache', 'fc') |
|
59
|
|
|
->leftJoin('fc', 'files_antivirus', 'fa', $qb->expr()->eq('fa.fileid', 'fc.fileid')) |
|
60
|
|
|
->innerJoin( |
|
61
|
|
|
'fc', |
|
62
|
|
|
'storages', |
|
63
|
|
|
'ss', |
|
64
|
|
|
$qb->expr()->andX( |
|
65
|
|
|
$qb->expr()->eq('fc.storage', 'ss.numeric_id'), |
|
66
|
|
|
$qb->expr()->orX( |
|
67
|
|
|
$qb->expr()->like('ss.id', $qb->expr()->literal('local::%')), |
|
68
|
|
|
$qb->expr()->like('ss.id', $qb->expr()->literal('home::%')) |
|
69
|
|
|
) |
|
70
|
|
|
) |
|
71
|
|
|
) |
|
72
|
|
|
->where( |
|
73
|
|
|
$qb->expr()->neq('fc.mimetype', $qb->expr()->literal($dirMimeTypeId)) |
|
74
|
|
|
) |
|
75
|
|
|
->andWhere( |
|
76
|
|
|
$qb->expr()->orX( |
|
77
|
|
|
$qb->expr()->isNull('fa.fileid'), |
|
78
|
|
|
$qb->expr()->gt('fc.mtime', 'fa.check_time') |
|
79
|
|
|
) |
|
80
|
|
|
) |
|
81
|
|
|
->andWhere( |
|
82
|
|
|
$qb->expr()->like('fc.path', $qb->expr()->literal('files/%')) |
|
83
|
|
|
) |
|
84
|
|
|
->andWhere( |
|
85
|
|
|
$qb->expr()->neq('fc.size', $qb->expr()->literal('0')) |
|
86
|
|
|
) |
|
87
|
|
|
->setMaxResults(5) |
|
88
|
|
|
; |
|
89
|
|
|
$result = $qb->execute(); |
|
90
|
|
|
} catch(\Exception $e) { |
|
91
|
|
|
\OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']); |
|
92
|
|
|
return; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
$view = new \OC\Files\View(''); |
|
97
|
|
|
try { |
|
98
|
|
|
while ($row = $result->fetch()) { |
|
99
|
|
|
$path = $view->getPath($row['fileid']); |
|
100
|
|
|
if (!is_null($path)) { |
|
101
|
|
|
$item = new Item($this->l10n, $view, $path, $row['fileid']); |
|
102
|
|
|
$scanner = $this->scannerFactory->getScanner(); |
|
103
|
|
|
$status = $scanner->scan($item); |
|
104
|
|
|
$status->dispatch($item, true); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} catch (\Exception $e){ |
|
108
|
|
|
\OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']); |
|
109
|
|
|
} |
|
110
|
|
|
\OC_Util::tearDownFS(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* A hack to access files and views. Better than before. |
|
115
|
|
|
* |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
1 |
|
protected function initFS(){ |
|
119
|
|
|
//Need any valid user to mount FS |
|
120
|
1 |
|
$results = $this->userManager->search('', 2, 0); |
|
121
|
1 |
|
$anyUser = array_pop($results); |
|
122
|
1 |
|
if (is_null($anyUser)) { |
|
123
|
1 |
|
\OC::$server->getLogger()->error("Failed to setup file system", ['app' => 'files_antivirus']); |
|
124
|
1 |
|
return false; |
|
125
|
|
|
} |
|
126
|
|
|
\OC_Util::tearDownFS(); |
|
127
|
|
|
\OC_Util::setupFS($anyUser->getUID()); |
|
128
|
|
|
return true; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @deprecated since v8.0.0 |
|
133
|
|
|
*/ |
|
134
|
|
|
public static function check(){ |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|