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\IL10N; |
14
|
|
|
use OCP\IUser; |
15
|
|
|
use OCP\Files\IRootFolder; |
16
|
|
|
use OCP\IUserSession; |
17
|
|
|
|
18
|
|
|
class BackgroundScanner { |
19
|
|
|
|
20
|
|
|
const BATCH_SIZE = 10; |
21
|
|
|
|
22
|
|
|
/** @var IRootFolder */ |
23
|
|
|
protected $rootFolder; |
24
|
|
|
|
25
|
|
|
/** @var \OCP\Files\Folder[] */ |
26
|
|
|
protected $userFolders; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ScannerFactory |
30
|
|
|
*/ |
31
|
|
|
private $scannerFactory; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var IL10N |
36
|
|
|
*/ |
37
|
|
|
private $l10n; |
38
|
|
|
|
39
|
|
|
/** @var string */ |
40
|
|
|
protected $currentFilesystemUser; |
41
|
|
|
|
42
|
|
|
/** @var \OCP\IUserSession */ |
43
|
|
|
protected $userSession; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* A constructor |
47
|
|
|
* |
48
|
|
|
* @param \OCA\Files_Antivirus\ScannerFactory $scannerFactory |
49
|
|
|
* @param IL10N $l10n |
50
|
|
|
* @param IRootFolder $rootFolder |
51
|
|
|
* @param IUserSession $userSession |
52
|
|
|
*/ |
53
|
|
|
public function __construct(ScannerFactory $scannerFactory, |
54
|
|
|
IL10N $l10n, |
55
|
|
|
IRootFolder $rootFolder, |
56
|
|
|
IUserSession $userSession |
57
|
|
|
){ |
58
|
|
|
$this->rootFolder = $rootFolder; |
59
|
|
|
$this->scannerFactory = $scannerFactory; |
60
|
|
|
$this->l10n = $l10n; |
61
|
|
|
$this->userSession = $userSession; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Background scanner main job |
66
|
|
|
* @return null |
67
|
|
|
*/ |
68
|
|
|
public function run(){ |
69
|
|
|
// locate files that are not checked yet |
70
|
|
|
$dirMimeTypeId = \OC::$server->getMimeTypeLoader()->getId('httpd/unix-directory'); |
71
|
|
|
try { |
72
|
|
|
$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
73
|
|
|
$qb->select(['fc.fileid']) |
74
|
|
|
->from('filecache', 'fc') |
75
|
|
|
->leftJoin('fc', 'files_antivirus', 'fa', $qb->expr()->eq('fa.fileid', 'fc.fileid')) |
76
|
|
|
->innerJoin( |
77
|
|
|
'fc', |
78
|
|
|
'storages', |
79
|
|
|
'ss', |
80
|
|
|
$qb->expr()->andX( |
81
|
|
|
$qb->expr()->eq('fc.storage', 'ss.numeric_id'), |
82
|
|
|
$qb->expr()->orX( |
83
|
|
|
$qb->expr()->like('ss.id', $qb->expr()->literal('local::%')), |
84
|
|
|
$qb->expr()->like('ss.id', $qb->expr()->literal('home::%')) |
85
|
|
|
) |
86
|
|
|
) |
87
|
|
|
) |
88
|
|
|
->where( |
89
|
|
|
$qb->expr()->neq('fc.mimetype', $qb->expr()->literal($dirMimeTypeId)) |
90
|
|
|
) |
91
|
|
|
->andWhere( |
92
|
|
|
$qb->expr()->orX( |
93
|
|
|
$qb->expr()->isNull('fa.fileid'), |
94
|
|
|
$qb->expr()->gt('fc.mtime', 'fa.check_time') |
95
|
|
|
) |
96
|
|
|
) |
97
|
|
|
->andWhere( |
98
|
|
|
$qb->expr()->like('fc.path', $qb->expr()->literal('files/%')) |
99
|
|
|
) |
100
|
|
|
->andWhere( |
101
|
|
|
$qb->expr()->neq('fc.size', $qb->expr()->literal('0')) |
102
|
|
|
) |
103
|
|
|
; |
104
|
|
|
$result = $qb->execute(); |
105
|
|
|
} catch(\Exception $e) { |
106
|
|
|
\OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']); |
107
|
|
|
return; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$cnt = 0; |
111
|
|
|
while ($row = $result->fetch() && $cnt++ < self::BATCH_SIZE) { |
|
|
|
|
112
|
|
|
try { |
113
|
|
|
$fileId = $row['fileid']; |
114
|
|
|
$owner = $this->getOwner($fileId); |
|
|
|
|
115
|
|
|
if (!$owner){ |
116
|
|
|
continue; |
117
|
|
|
} |
118
|
|
|
$this->initFilesystemForUser($owner); |
119
|
|
|
$view = \OC\Files\Filesystem::getView(); |
120
|
|
|
$path = $view->getPath($fileId); |
121
|
|
|
if (!is_null($path)) { |
122
|
|
|
$item = new Item($this->l10n, $view, $path, $fileId); |
123
|
|
|
$scanner = $this->scannerFactory->getScanner(); |
124
|
|
|
$status = $scanner->scan($item); |
125
|
|
|
$status->dispatch($item, true); |
126
|
|
|
} |
127
|
|
|
$this->tearDownFilesystem(); |
128
|
|
|
|
129
|
|
|
// increased only for successfully scanned files |
130
|
|
|
$cnt = $cnt + 1; |
131
|
|
|
} catch (\Exception $e){ |
132
|
|
|
\OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
protected function getOwner($fileId){ |
138
|
|
|
$mountProviderCollection = \OC::$server->getMountProviderCollection(); |
139
|
|
|
$mountCache = $mountProviderCollection->getMountCache(); |
140
|
|
|
$mounts = $mountCache->getMountsForFileId($fileId); |
141
|
|
|
if (!empty($mounts)) { |
142
|
|
|
$user = $mounts[0]->getUser(); |
143
|
|
|
if ($user instanceof IUser) { |
|
|
|
|
144
|
|
|
return $user; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param \OCP\IUser $user |
151
|
|
|
* @return \OCP\Files\Folder |
152
|
|
|
*/ |
153
|
|
|
protected function getUserFolder(IUser $user) { |
154
|
|
|
if (!isset($this->userFolders[$user->getUID()])) { |
155
|
|
|
$userFolder = $this->rootFolder->getUserFolder($user->getUID()); |
156
|
|
|
$this->userFolders[$user->getUID()] = $userFolder; |
157
|
|
|
} |
158
|
|
|
return $this->userFolders[$user->getUID()]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param IUser $user |
163
|
|
|
*/ |
164
|
|
|
protected function initFilesystemForUser(IUser $user) { |
165
|
|
|
if ($this->currentFilesystemUser !== $user->getUID()) { |
166
|
|
|
if ($this->currentFilesystemUser !== '') { |
167
|
|
|
Filesystem::tearDown(); |
168
|
|
|
} |
169
|
|
|
Filesystem::init($user->getUID(), '/' . $user->getUID() . '/files'); |
170
|
|
|
$this->userSession->setUser($user); |
171
|
|
|
$this->currentFilesystemUser = $user->getUID(); |
172
|
|
|
\OC\Files\Filesystem::initMountPoints($user->getUID()); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* |
178
|
|
|
*/ |
179
|
|
|
protected function tearDownFilesystem(){ |
180
|
|
|
$this->userSession->setUser(null); |
181
|
|
|
\OC_Util::tearDownFS(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @deprecated since v8.0.0 |
186
|
|
|
*/ |
187
|
|
|
public static function check(){ |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|