|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2014 Victor Dubiniuk <[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\Storage\Wrapper\Wrapper; |
|
12
|
|
|
use \OCP\App; |
|
13
|
|
|
use \OCP\IConfig; |
|
14
|
|
|
use \OCP\IL10N; |
|
15
|
|
|
use \OCP\ILogger; |
|
16
|
|
|
use \OCP\Files\InvalidContentException; |
|
17
|
|
|
use Icewind\Streams\CallbackWrapper; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
class AvirWrapper extends Wrapper{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Modes that are used for writing |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
private $writingModes = array('r+', 'w', 'w+', 'a', 'a+', 'x', 'x+', 'c', 'c+'); |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var \OCA\Files_Antivirus\ScannerFactory |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $scannerFactory; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var IL10N |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $l10n; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var ILogger; |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $logger; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param array $parameters |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct($parameters) { |
|
47
|
|
|
parent::__construct($parameters); |
|
48
|
|
|
$this->scannerFactory = $parameters['scannerFactory']; |
|
49
|
|
|
$this->l10n = $parameters['l10n']; |
|
50
|
|
|
$this->logger = $parameters['logger']; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Asynchronously scan data that are written to the file |
|
55
|
|
|
* @param string $path |
|
56
|
|
|
* @param string $mode |
|
57
|
|
|
* @return resource | bool |
|
58
|
|
|
*/ |
|
59
|
|
|
public function fopen($path, $mode){ |
|
60
|
|
|
$stream = $this->storage->fopen($path, $mode); |
|
61
|
|
|
if (is_resource($stream) && $this->isWritingMode($mode)) { |
|
62
|
|
|
try { |
|
63
|
|
|
$scanner = $this->scannerFactory->getScanner(); |
|
64
|
|
|
$scanner->initAsyncScan(); |
|
65
|
|
|
return CallBackWrapper::wrap( |
|
66
|
|
|
$stream, |
|
67
|
|
|
null, |
|
68
|
|
|
function ($data) use ($scanner){ |
|
69
|
|
|
$scanner->onAsyncData($data); |
|
70
|
|
|
}, |
|
71
|
|
|
function () use ($scanner, $path) { |
|
72
|
|
|
$status = $scanner->completeAsyncScan(); |
|
73
|
|
|
if (intval($status->getNumericStatus()) === \OCA\Files_Antivirus\Status::SCANRESULT_INFECTED){ |
|
74
|
|
|
//prevent from going to trashbin |
|
75
|
|
|
if (App::isEnabled('files_trashbin')) { |
|
76
|
|
|
\OCA\Files_Trashbin\Storage::preRenameHook([]); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$owner = $this->getOwner($path); |
|
80
|
|
|
$this->unlink($path); |
|
81
|
|
|
|
|
82
|
|
|
if (App::isEnabled('files_trashbin')) { |
|
83
|
|
|
\OCA\Files_Trashbin\Storage::postRenameHook([]); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
\OC::$server->getActivityManager()->publishActivity( |
|
87
|
|
|
'files_antivirus', |
|
88
|
|
|
Activity::SUBJECT_VIRUS_DETECTED, |
|
89
|
|
|
[$path, $status->getDetails()], |
|
90
|
|
|
Activity::MESSAGE_FILE_DELETED, |
|
91
|
|
|
[], |
|
92
|
|
|
$path, |
|
93
|
|
|
'', |
|
94
|
|
|
$owner, |
|
95
|
|
|
Activity::TYPE_VIRUS_DETECTED, |
|
96
|
|
|
Activity::PRIORITY_HIGH |
|
97
|
|
|
); |
|
98
|
|
|
|
|
99
|
|
|
$this->logger->error('Infected file deleted. ' . $status->getDetails() . |
|
100
|
|
|
' File: ' . $path . ' Acccount: ' . $owner, ['app' => 'files_antivirus']); |
|
101
|
|
|
|
|
102
|
|
|
throw new InvalidContentException( |
|
103
|
|
|
$this->l10n->t( |
|
104
|
|
|
'Virus %s is detected in the file. Upload cannot be completed.', |
|
105
|
|
|
$status->getDetails() |
|
106
|
|
|
) |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
); |
|
111
|
|
|
} catch (\Exception $e){ |
|
112
|
|
|
$message = implode(' ', [ __CLASS__, __METHOD__, $e->getMessage()]); |
|
113
|
|
|
$this->logger->warning($message); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
return $stream; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Checks whether passed mode is suitable for writing |
|
121
|
|
|
* @param string $mode |
|
122
|
|
|
* @return bool |
|
123
|
|
|
*/ |
|
124
|
|
|
private function isWritingMode($mode){ |
|
125
|
|
|
// Strip unessential binary/text flags |
|
126
|
|
|
$cleanMode = str_replace( |
|
127
|
|
|
['t', 'b'], |
|
128
|
|
|
['', ''], |
|
129
|
|
|
$mode |
|
130
|
|
|
); |
|
131
|
|
|
return in_array($cleanMode, $this->writingModes); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|