1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
<?php
/**
* Copyright (c) 2015 Victor Dubiniuk <victor.dubiniuk@gmail.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OCA\Files_Antivirus;
use OCA\Files_Antivirus\Activity\Provider;
use OCA\Files_Antivirus\AppInfo\Application;
use OCA\Files_Antivirus\Db\ItemMapper;
use OCP\Activity\IManager as ActivityManager;
use OCP\App;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCA\Files_Trashbin\Trash\ITrashManager;
use OCP\ILogger;
class Item {
/**
* file handle, user to read from the file
* @var resource
*/
protected $fileHandle;
/** @var AppConfig */
private $config;
/** @var ActivityManager */
private $activityManager;
/** @var ItemMapper */
private $itemMapper;
/** @var ILogger */
private $logger;
/** @var IRootFolder */
private $rootFolder;
/** @var File */
private $file;
/**
* Item constructor.
*
* @param AppConfig $appConfig
* @param ActivityManager $activityManager
* @param ItemMapper $itemMapper
* @param ILogger $logger
* @param IRootFolder $rootFolder
* @param File $file
*/
public function __construct(AppConfig $appConfig,
ActivityManager $activityManager,
ItemMapper $itemMapper,
ILogger $logger,
IRootFolder $rootFolder,
File $file) {
$this->config = $appConfig;
$this->activityManager = $activityManager;
$this->itemMapper = $itemMapper;
$this->logger = $logger;
$this->rootFolder = $rootFolder;
$this->file = $file;
}
/**
* Reads a file portion by portion until the very end
* @return string|boolean
*/
public function fread() {
if (!($this->file->getSize() > 0)) {
return false;
}
if (is_null($this->fileHandle)) {
$this->getFileHandle();
}
if (!is_null($this->fileHandle) && !$this->feof()) {
return fread($this->fileHandle, $this->config->getAvChunkSize());
}
return false;
}
/**
* Action to take if this item is infected
*/
public function processInfected(Status $status) {
$infectedAction = $this->config->getAvInfectedAction();
$shouldDelete = $infectedAction === 'delete';
$message = $shouldDelete ? Provider::MESSAGE_FILE_DELETED : '';
$userFolder = $this->rootFolder->getUserFolder($this->file->getOwner()->getUID());
$path = $userFolder->getRelativePath($this->file->getPath());
$activity = $this->activityManager->generateEvent();
$activity->setApp(Application::APP_NAME)
->setSubject(Provider::SUBJECT_VIRUS_DETECTED_SCAN, [$status->getDetails()])
->setMessage($message)
->setObject('file', $this->file->getId(), $path)
->setAffectedUser($this->file->getOwner()->getUID())
->setType(Provider::TYPE_VIRUS_DETECTED);
$this->activityManager->publish($activity);
if ($shouldDelete) {
$this->logError('Infected file deleted. ' . $status->getDetails());
$this->deleteFile();
} else {
$this->logError('File is infected. ' . $status->getDetails());
}
}
/**
* Action to take if this item status is unclear
* @param Status $status
*/
public function processUnchecked(Status $status) {
//TODO: Show warning to the user: The file can not be checked
$this->logError('Not Checked. ' . $status->getDetails());
}
/**
* Action to take if this item status is not infected
*/
public function processClean() {
try {
try {
$item = $this->itemMapper->findByFileId($this->file->getId());
$this->itemMapper->delete($item);
} catch (DoesNotExistException $e) {
//Just ignore
}
$item = new \OCA\Files_Antivirus\Db\Item();
$item->setFileid($this->file->getId());
$item->setCheckTime(time());
$this->itemMapper->insert($item);
} catch(\Exception $e) {
$this->logger->error(__METHOD__.', exception: '.$e->getMessage(), ['app' => 'files_antivirus']);
}
}
/**
* Check if the end of file is reached
* @return boolean
*/
private function feof() {
$isDone = feof($this->fileHandle);
if ($isDone) {
$this->logDebug('Scan is done');
fclose($this->fileHandle);
$this->fileHandle = null;
}
return $isDone;
}
/**
* Opens a file for reading
* @throws \RuntimeException
*/
private function getFileHandle() {
$fileHandle = $this->file->fopen('r');
if ($fileHandle === false) {
$this->logError('Can not open for reading.');
throw new \RuntimeException();
}
$this->logDebug('Scan started');
$this->fileHandle = $fileHandle;
}
/**
* Delete infected file
*/
private function deleteFile() {
//prevent from going to trashbin
if (App::isEnabled('files_trashbin')) {
/** @var ITrashManager $trashManager */
$trashManager = \OC::$server->query(ITrashManager::class);
$trashManager->pauseTrash();
}
$this->file->delete();
if (App::isEnabled('files_trashbin')) {
/** @var ITrashManager $trashManager */
$trashManager = \OC::$server->query(ITrashManager::class);
$trashManager->resumeTrash();
}
}
private function generateExtraInfo() {
$owner = $this->file->getOwner();
if ($owner === null) {
$ownerInfo = 'Account: NO OWNER FOUND';
} else {
$ownerInfo = 'Account: ' . $owner->getUID();
}
$extra = ' File: ' . $this->file->getId()
. $ownerInfo
. ' Path: ' . $this->file->getPath();
return $extra;
}
/**
* @param string $message
*/
public function logDebug($message) {
$this->logger->debug($message . $this->generateExtraInfo(), ['app' => 'files_antivirus']);
}
/**
* @param string $message
*/
public function logError($message) {
$this->logger->error($message . $this->generateExtraInfo(), ['app' => 'files_antivirus']);
}
}
|