1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* ownCloud - files_antivirus |
5
|
|
|
* |
6
|
|
|
* @author Manuel Deglado |
7
|
|
|
* @copyright 2012 Manuel Deglado [email protected] |
8
|
|
|
* |
9
|
|
|
* This library is free software; you can redistribute it and/or |
10
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
11
|
|
|
* License as published by the Free Software Foundation; either |
12
|
|
|
* version 3 of the License, or any later version. |
13
|
|
|
* |
14
|
|
|
* This library is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Affero General Public |
20
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace OCA\Files_Antivirus\Scanner; |
25
|
|
|
|
26
|
|
|
use OCA\Files_Antivirus\AppConfig; |
27
|
|
|
use OCA\Files_Antivirus\IScannable; |
28
|
|
|
use OCA\Files_Antivirus\Status; |
29
|
|
|
use OCP\ILogger; |
30
|
|
|
|
31
|
|
|
abstract class ScannerBase { |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Scan result |
35
|
|
|
* @var Status |
36
|
|
|
*/ |
37
|
|
|
protected $status; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* If scanning was done part by part |
41
|
|
|
* the first detected infected part is stored here |
42
|
|
|
* @var Status |
43
|
|
|
*/ |
44
|
|
|
protected $infectedStatus; |
45
|
|
|
|
46
|
|
|
/** @var int */ |
47
|
|
|
protected $byteCount; |
48
|
|
|
|
49
|
|
|
/** @var resource */ |
50
|
|
|
protected $writeHandle; |
51
|
|
|
|
52
|
|
|
/** @var AppConfig */ |
53
|
|
|
protected $appConfig; |
54
|
|
|
|
55
|
|
|
/** @var ILogger */ |
56
|
|
|
protected $logger; |
57
|
|
|
|
58
|
|
|
/** @var string */ |
59
|
|
|
protected $lastChunk; |
60
|
|
|
|
61
|
|
|
/** @var bool */ |
62
|
|
|
protected $isLogUsed = false; |
63
|
|
|
|
64
|
|
|
/** @var bool */ |
65
|
|
|
protected $isAborted = false; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Close used resources |
69
|
|
|
*/ |
70
|
|
|
abstract protected function shutdownScanner(); |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
public function getStatus(){ |
74
|
|
|
if ($this->infectedStatus instanceof Status){ |
75
|
|
|
return $this->infectedStatus; |
76
|
|
|
} |
77
|
|
|
if ($this->status instanceof Status){ |
78
|
|
|
return $this->status; |
79
|
|
|
} |
80
|
|
|
return new Status(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Synchronous scan |
85
|
|
|
* @param IScannable $item |
86
|
|
|
* @return Status |
87
|
|
|
*/ |
88
|
|
|
public function scan(IScannable $item) { |
89
|
|
|
$this->initScanner(); |
90
|
|
|
|
91
|
|
|
while (false !== ($chunk = $item->fread())) { |
92
|
|
|
$this->writeChunk($chunk); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->shutdownScanner(); |
96
|
|
|
return $this->getStatus(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Async scan - new portion of data is available |
101
|
|
|
* @param string $data |
102
|
|
|
*/ |
103
|
|
|
public function onAsyncData($data){ |
104
|
|
|
$this->writeChunk($data); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Async scan - resource is closed |
109
|
|
|
* @return Status |
110
|
|
|
*/ |
111
|
|
|
public function completeAsyncScan(){ |
112
|
|
|
$this->shutdownScanner(); |
113
|
|
|
return $this->getStatus(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Open write handle. etc |
118
|
|
|
*/ |
119
|
|
|
public function initScanner(){ |
120
|
|
|
$this->byteCount = 0; |
121
|
|
|
if ($this->status instanceof Status && $this->status->getNumericStatus() === Status::SCANRESULT_INFECTED){ |
122
|
|
|
$this->infectedStatus = clone $this->status; |
123
|
|
|
} |
124
|
|
|
$this->status = new Status(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $chunk |
129
|
|
|
*/ |
130
|
|
|
protected function writeChunk($chunk){ |
131
|
|
|
$this->fwrite( |
132
|
|
|
$this->prepareChunk($chunk) |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $data |
138
|
|
|
*/ |
139
|
|
|
final protected function fwrite($data){ |
140
|
|
|
if ($this->isAborted){ |
141
|
|
|
return; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$dataLength = strlen($data); |
145
|
|
|
$streamSizeLimit = (int)$this->appConfig->getAvStreamMaxLength(); |
|
|
|
|
146
|
|
|
if ($this->byteCount + $dataLength > $streamSizeLimit){ |
147
|
|
|
\OC::$server->getLogger()->debug( |
148
|
|
|
'reinit scanner', |
149
|
|
|
['app' => 'files_antivirus'] |
150
|
|
|
); |
151
|
|
|
$this->shutdownScanner(); |
152
|
|
|
$isReopenSuccessful = $this->retry(); |
153
|
|
|
} else { |
154
|
|
|
$isReopenSuccessful = true; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (!$isReopenSuccessful || !$this->writeRaw($data)){ |
158
|
|
|
if (!$this->isLogUsed) { |
159
|
|
|
$this->isLogUsed = true; |
160
|
|
|
\OC::$server->getLogger()->warning( |
161
|
|
|
'Failed to write a chunk. Check if Stream Length matches StreamMaxLength in ClamAV daemon settings', |
162
|
|
|
['app' => 'files_antivirus'] |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
// retry on error |
166
|
|
|
$isRetrySuccessful = $this->retry() && $this->writeRaw($data); |
167
|
|
|
$this->isAborted = !$isRetrySuccessful; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
|
|
protected function retry(){ |
175
|
|
|
$this->initScanner(); |
176
|
|
|
if (!is_null($this->lastChunk)) { |
177
|
|
|
return $this->writeRaw($this->lastChunk); |
178
|
|
|
} |
179
|
|
|
return true; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param $data |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
|
|
protected function writeRaw($data){ |
187
|
|
|
$dataLength = strlen($data); |
188
|
|
|
$bytesWritten = @fwrite($this->getWriteHandle(), $data); |
189
|
|
|
if ($bytesWritten === $dataLength){ |
190
|
|
|
$this->byteCount += $bytesWritten; |
191
|
|
|
$this->lastChunk = $data; |
192
|
|
|
return true; |
193
|
|
|
} |
194
|
|
|
return false; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get a resource to write data into |
199
|
|
|
* @return resource |
200
|
|
|
*/ |
201
|
|
|
protected function getWriteHandle(){ |
202
|
|
|
return $this->writeHandle; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Prepare chunk (if required) |
207
|
|
|
* @param string $data |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
|
|
protected function prepareChunk($data){ |
211
|
|
|
return $data; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: