ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 var $type;
23
31
38
45
52
59
66
73
80
87
94
101
108
114 var $lng;
115
121 var $log;
122
127 public function __construct($a_scancommand, $a_cleancommand)
128 {
129 global $ilias, $lng, $log;
130
131 $this->ilias = $ilias;
132 $this->lng = $lng;
133 $this->log = $log;
134 $this->scanCommand = $a_scancommand;
135 $this->cleanCommand = $a_cleancommand;
136
137 $this->type = "simulate";
138 $this->scanZipFiles = false;
139 }
140
145 public function scanBuffer($buffer)
146 {
147 return $this->scanFileFromBuffer($buffer);
148 }
149
154 protected function scanFileFromBuffer($buffer)
155 {
156 $bufferFile = $this->createBufferFile($buffer);
157 $isInfected = $this->scanFile($bufferFile);
158 $this->removeBufferFile($bufferFile);
159 return $isInfected;
160 }
161
166 protected function createBufferFile($buffer)
167 {
168 $bufferFile = ilUtil::ilTempnam();
169 file_put_contents($bufferFile, $buffer);
170 return $bufferFile;
171 }
172
176 protected function removeBufferFile($bufferFile)
177 {
178 unlink($bufferFile);
179 }
180
191 function scanFile($a_filepath, $a_origname = "")
192 {
193 // This function needs to be redefined in child classes.
194 // It should:
195 // - call the external scanner for a_filepath
196 // - set scanFilePath to a_filepath
197 // - set scanFileOrigName to a_origname
198 // - set scanFileIsInfected according the scan result
199 // - set scanResult to the scanner output message
200 // - call logScanResult() if file is infected
201 // - return the output message, if file is infected
202 // - return an empty string, if file is not infected
203
204 $this->scanFilePath = $a_filepath;
205 $this->scanFileOrigName = $a_origname;
206
207 if($a_origname == "infected.txt" or $a_origname == "cleanable.txt")
208 {
209 $this->scanFileIsInfected = true;
210 $this->scanResult =
211 "FILE INFECTED: [" . $a_filepath . "] (VIRUS: simulated)";
212 $this->logScanResult();
213 return $this->scanResult;
214 }
215 else
216 {
217 $this->scanFileIsInfected = false;
218 $this->scanResult = "";
219 return "";
220 }
221 }
222
233 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 {
251 $this->cleanFileIsCleaned = true;
252 $this->cleanResult =
253 "FILE CLEANED: [" . $a_filepath . "] (VIRUS: simulated)";
254 $this->logCleanResult();
255 return $this->cleanResult;
256 }
257 else
258 {
259 $this->cleanFileIsCleaned = false;
260 $this->cleanResult =
261 "FILE NOT CLEANED: [" . $a_filepath . "] (VIRUS: simulated)";
262 $this->logCleanResult();
263 return "";
264 }
265 }
266
271 function fileCleaned()
272 {
274 }
275
280 function logScanResult()
281 {
282 $mess = "Virus Scanner (" . $this->type . ")";
283 if($this->scanFileOrigName)
284 {
285 $mess .= " (File " . $this->scanFileOrigName . ")";
286 }
287 $mess .= ": " . preg_replace('/[\r\n]+/', "; ", $this->scanResult);
288
289 $this->log->write($mess);
290 }
291
296 function logCleanResult()
297 {
298 $mess = "Virus Cleaner (" . $this->type . ")";
299 if($this->cleanFileOrigName)
300 {
301 $mess .= " (File " . $this->cleanFileOrigName . ")";
302 }
303 $mess .= ": " . preg_replace('/[\r\n]+/', "; ", $this->cleanResult);
304
305 $this->log->write($mess);
306 }
307
313 function getScanResult()
314 {
315 return $this->scanResult;
316 }
317
323 function getCleanResult()
324 {
325 return $this->cleanResult;
326 }
327
333 function getScanMessage()
334 {
335 if($this->scanFileIsInfected)
336 {
337 $ret = sprintf($this->lng->txt("virus_infected"), $this->scanFileOrigName);
338 }
339 else
340 {
341 $ret = sprintf($this->lng->txt("virus_not_infected"), $this->scanFileOrigName);
342 }
343
344 if($this->scanResult)
345 {
346 $ret .= " " . $this->lng->txt("virus_scan_message")
347 . "<br />"
348 . str_replace($this->scanFilePath, $this->scanFileOrigName,
349 nl2br($this->scanResult));
350 }
351 return $ret;
352 }
353
360 {
361 if($this->cleanFileIsCleaned)
362 {
363 $ret = sprintf($this->lng->txt("virus_cleaned"), $this->cleanFileOrigName);
364 }
365 else
366 {
367 $ret = sprintf($this->lng->txt("virus_not_cleaned"), $this->cleanFileOrigName);
368 }
369
370 if($this->cleanResult)
371 {
372 $ret .= " " . $this->lng->txt("virus_clean_message")
373 . "<br />"
374 . str_replace($this->cleanFilePath, $this->cleanFileOrigName,
375 nl2br($this->cleanResult));
376 }
377 return $ret;
378 }
379
386 {
387 return $this->scanZipFiles;
388 }
389}
sprintf('%.4f', $callTime)
An exception for terminatinating execution or to throw for unit testing.
static ilTempnam($a_temp_path=null)
Create a temporary file in an ILIAS writable 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