ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilVirusScanner.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
15{
22 public $type;
23
31
38
45
52
59
66
73
80
87
94
101
107 public $ilias;
108
114 public $lng;
115
121 public $log;
122
127 public function __construct($a_scancommand, $a_cleancommand)
128 {
129 global $DIC;
130 $ilias = $DIC['ilias'];
131 $lng = $DIC['lng'];
132 $log = $DIC['log'];
133
134 $this->ilias = $ilias;
135 $this->lng = $lng;
136 $this->log = $log;
137 $this->scanCommand = $a_scancommand;
138 $this->cleanCommand = $a_cleancommand;
139
140 $this->type = "simulate";
141 $this->scanZipFiles = false;
142 }
143
148 public function scanBuffer($buffer)
149 {
150 return $this->scanFileFromBuffer($buffer);
151 }
152
157 protected function scanFileFromBuffer($buffer)
158 {
159 $bufferFile = $this->createBufferFile($buffer);
160 $isInfected = $this->scanFile($bufferFile);
161 $this->removeBufferFile($bufferFile);
162 return $isInfected;
163 }
164
169 protected function createBufferFile($buffer)
170 {
171 $bufferFile = ilUtil::ilTempnam();
172 file_put_contents($bufferFile, $buffer);
173 return $bufferFile;
174 }
175
179 protected function removeBufferFile($bufferFile)
180 {
181 unlink($bufferFile);
182 }
183
194 public function scanFile($a_filepath, $a_origname = "")
195 {
196 // This function needs to be redefined in child classes.
197 // It should:
198 // - call the external scanner for a_filepath
199 // - set scanFilePath to a_filepath
200 // - set scanFileOrigName to a_origname
201 // - set scanFileIsInfected according the scan result
202 // - set scanResult to the scanner output message
203 // - call logScanResult() if file is infected
204 // - return the output message, if file is infected
205 // - return an empty string, if file is not infected
206
207 $this->scanFilePath = $a_filepath;
208 $this->scanFileOrigName = $a_origname;
209
210 if ($a_origname == "infected.txt" or $a_origname == "cleanable.txt") {
211 $this->scanFileIsInfected = true;
212 $this->scanResult =
213 "FILE INFECTED: [" . $a_filepath . "] (VIRUS: simulated)";
214 $this->logScanResult();
215 return $this->scanResult;
216 } else {
217 $this->scanFileIsInfected = false;
218 $this->scanResult = "";
219 return "";
220 }
221 }
222
233 public function cleanFile($a_filepath, $a_origname = "")
234 {
235 // This function needs to be redefined in child classes
236 // It should:
237 // - call the external cleaner
238 // - set cleanFilePath to a_filepath
239 // - set cleanFileOrigName to a_origname
240 // - set cleanFileIsCleaned according the clean result
241 // - set cleanResult to the cleaner output message
242 // - call logCleanResult in any case
243 // - return the output message, if file is cleaned
244 // - return an empty string, if file is not cleaned
245
246 $this->cleanFilePath = $a_filepath;
247 $this->cleanFileOrigName = $a_origname;
248
249 if ($a_origname == "cleanable.txt") {
250 $this->cleanFileIsCleaned = true;
251 $this->cleanResult =
252 "FILE CLEANED: [" . $a_filepath . "] (VIRUS: simulated)";
253 $this->logCleanResult();
254 return $this->cleanResult;
255 } else {
256 $this->cleanFileIsCleaned = false;
257 $this->cleanResult =
258 "FILE NOT CLEANED: [" . $a_filepath . "] (VIRUS: simulated)";
259 $this->logCleanResult();
260 return "";
261 }
262 }
263
268 public function fileCleaned()
269 {
271 }
272
277 public function logScanResult()
278 {
279 $mess = "Virus Scanner (" . $this->type . ")";
280 if ($this->scanFileOrigName) {
281 $mess .= " (File " . $this->scanFileOrigName . ")";
282 }
283 $mess .= ": " . preg_replace('/[\r\n]+/', "; ", $this->scanResult);
284
285 $this->log->write($mess);
286 }
287
292 public function logCleanResult()
293 {
294 $mess = "Virus Cleaner (" . $this->type . ")";
295 if ($this->cleanFileOrigName) {
296 $mess .= " (File " . $this->cleanFileOrigName . ")";
297 }
298 $mess .= ": " . preg_replace('/[\r\n]+/', "; ", $this->cleanResult);
299
300 $this->log->write($mess);
301 }
302
308 public function getScanResult()
309 {
310 return $this->scanResult;
311 }
312
318 public function getCleanResult()
319 {
320 return $this->cleanResult;
321 }
322
328 public function getScanMessage()
329 {
330 if ($this->scanFileIsInfected) {
331 $ret = sprintf($this->lng->txt("virus_infected"), $this->scanFileOrigName);
332 } else {
333 $ret = sprintf($this->lng->txt("virus_not_infected"), $this->scanFileOrigName);
334 }
335
336 if ($this->scanResult) {
337 $ret .= " " . $this->lng->txt("virus_scan_message")
338 . "<br />"
339 . str_replace(
340 $this->scanFilePath,
341 $this->scanFileOrigName,
342 nl2br($this->scanResult)
343 );
344 }
345 return $ret;
346 }
347
353 public function getCleanMessage()
354 {
355 if ($this->cleanFileIsCleaned) {
356 $ret = sprintf($this->lng->txt("virus_cleaned"), $this->cleanFileOrigName);
357 } else {
358 $ret = sprintf($this->lng->txt("virus_not_cleaned"), $this->cleanFileOrigName);
359 }
360
361 if ($this->cleanResult) {
362 $ret .= " " . $this->lng->txt("virus_clean_message")
363 . "<br />"
364 . str_replace(
365 $this->cleanFilePath,
366 $this->cleanFileOrigName,
367 nl2br($this->cleanResult)
368 );
369 }
370 return $ret;
371 }
372
378 public function getScanZipFiles()
379 {
380 return $this->scanZipFiles;
381 }
382}
An exception for terminatinating execution or to throw for unit testing.
static ilTempnam($a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
Base class for the interface to an external virus scanner This class is abstract and needs to be exte...
getCleanMessage()
get a located message with the result from the last clean
logScanResult()
write the result of the last scan to the log @access public
scanFile($a_filepath, $a_origname="")
scan a file for viruses needs to be redefined in child classes here it simulates a scan "infected....
__construct($a_scancommand, $a_cleancommand)
Constructor @access public.
getScanResult()
get the pure output of the external scan
logCleanResult()
write the result of the last clean to the log @access public
removeBufferFile($bufferFile)
getScanZipFiles()
get info if class can scan ZIP files
getCleanResult()
get the pure output of the external scan
cleanFile($a_filepath, $a_origname="")
clean an infected file needs to be redefined in child classes here it simulates a clean "cleanable....
getScanMessage()
get a located message with the result from the last scan
fileCleaned()
returns wether file has been cleaned successfully or not
redirection script todo: (a better solution should control the processing via a xml file)
$ret
Definition: parser.php:6
$DIC
Definition: xapitoken.php:46