ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
4 
19 {
26  var $type;
27 
35 
42 
49 
56 
63 
70 
77 
84 
91 
98 
105 
111  var $ilias;
112 
118  var $lng;
119 
125  var $log;
126 
131  function ilVirusScanner($a_scancommand, $a_cleancommand)
132  {
133  global $ilias, $lng, $log;
134 
135  $this->ilias = & $ilias;
136  $this->lng = & $lng;
137  $this->log = & $log;
138  $this->scanCommand = $a_scancommand;
139  $this->cleanCommand = $a_cleancommand;
140 
141  $this->type = "simulate";
142  $this->scanZipFiles = false;
143  }
144 
149  public function scanBuffer($buffer)
150  {
151  return $this->scanFileFromBuffer($buffer);
152  }
153 
158  protected function scanFileFromBuffer($buffer)
159  {
160  $bufferFile = $this->createBufferFile($buffer);
161  $isInfected = $this->scanFile($bufferFile);
162  $this->removeBufferFile($bufferFile);
163  return $isInfected;
164  }
165 
170  protected function createBufferFile($buffer)
171  {
172  $bufferFile = ilUtil::ilTempnam();
173  file_put_contents($bufferFile, $buffer);
174  return $bufferFile;
175  }
176 
180  protected function removeBufferFile($bufferFile)
181  {
182  unlink($bufferFile);
183  }
184 
197  function scanFile($a_filepath, $a_origname = "")
198  {
199  // This function needs to be redefined in child classes.
200  // It should:
201  // - call the external scanner for a_filepath
202  // - set scanFilePath to a_filepath
203  // - set scanFileOrigName to a_origname
204  // - set scanFileIsInfected according the scan result
205  // - set scanResult to the scanner output message
206  // - call logScanResult() if file is infected
207  // - return the output message, if file is infected
208  // - return an empty string, if file is not infected
209 
210  $this->scanFilePath = $a_filepath;
211  $this->scanFileOrigName = $a_origname;
212 
213  if ($a_origname == "infected.txt" or $a_origname == "cleanable.txt")
214  {
215  $this->scanFileIsInfected = true;
216  $this->scanResult =
217  "FILE INFECTED: [". $a_filepath. "] (VIRUS: simulated)";
218  $this->logScanResult();
219  return $this->scanResult;
220  }
221  else
222  {
223  $this->scanFileIsInfected = false;
224  $this->scanResult = "";
225  return "";
226  }
227  }
228 
229 
242  function cleanFile($a_filepath, $a_origname = "")
243  {
244  // This function needs to be redefined in child classes
245  // It should:
246  // - call the external cleaner
247  // - set cleanFilePath to a_filepath
248  // - set cleanFileOrigName to a_origname
249  // - set cleanFileIsCleaned according the clean result
250  // - set cleanResult to the cleaner output message
251  // - call logCleanResult in any case
252  // - return the output message, if file is cleaned
253  // - return an empty string, if file is not cleaned
254 
255  $this->cleanFilePath = $a_filepath;
256  $this->cleanFileOrigName = $a_origname;
257 
258  if ($a_origname == "cleanable.txt")
259  {
260  $this->cleanFileIsCleaned = true;
261  $this->cleanResult =
262  "FILE CLEANED: [". $a_filepath. "] (VIRUS: simulated)";
263  $this->logCleanResult();
264  return $this->cleanResult;
265  }
266  else
267  {
268  $this->cleanFileIsCleaned = false;
269  $this->cleanResult =
270  "FILE NOT CLEANED: [". $a_filepath. "] (VIRUS: simulated)";
271  $this->logCleanResult();
272  return "";
273  }
274  }
275 
281  function fileCleaned()
282  {
284  }
285 
291  function logScanResult()
292  {
293  $mess = "Virus Scanner (". $this->type. ")";
294  if ($this->scanFileOrigName)
295  {
296  $mess .= " (File " . $this->scanFileOrigName . ")";
297  }
298  $mess .= ": " . ereg_replace("(\r|\n)+", "; ", $this->scanResult);
299 
300  $this->log->write($mess);
301  }
302 
308  function logCleanResult()
309  {
310  $mess = "Virus Cleaner (". $this->type. ")";
311  if ($this->cleanFileOrigName)
312  {
313  $mess .= " (File ". $this->cleanFileOrigName. ")";
314  }
315  $mess .= ": " . ereg_replace("(\r|\n)+", "; ", $this->cleanResult);
316 
317  $this->log->write($mess);
318  }
319 
326  function getScanResult()
327  {
328  return $this->scanResult;
329  }
330 
337  function getCleanResult()
338  {
339  return $this->cleanResult;
340  }
341 
348  function getScanMessage()
349  {
350  if ($this->scanFileIsInfected)
351  {
352  $ret = sprintf($this->lng->txt("virus_infected"), $this->scanFileOrigName);
353  }
354  else
355  {
356  $ret = sprintf($this->lng->txt("virus_not_infected"),$this->scanFileOrigName);
357  }
358 
359  if ($this->scanResult)
360  {
361  $ret .= " ". $this->lng->txt("virus_scan_message")
362  . "<br />"
363  . str_replace($this->scanFilePath, $this->scanFileOrigName,
364  nl2br($this->scanResult));
365  }
366  return $ret;
367  }
368 
375  function getCleanMessage()
376  {
377  if ($this->cleanFileIsCleaned)
378  {
379  $ret = sprintf($this->lng->txt("virus_cleaned"), $this->cleanFileOrigName);
380  }
381  else
382  {
383  $ret = sprintf($this->lng->txt("virus_not_cleaned"),$this->cleanFileOrigName);
384  }
385 
386  if ($this->cleanResult)
387  {
388  $ret .= " ". $this->lng->txt("virus_clean_message")
389  . "<br />"
390  . str_replace($this->cleanFilePath, $this->cleanFileOrigName,
391  nl2br($this->cleanResult));
392  }
393  return $ret;
394  }
395 
402  function getScanZipFiles()
403  {
404  return $this->scanZipFiles;
405  }
406 }
407 ?>
fileCleaned()
returns wether file has been cleaned successfully or not
getScanZipFiles()
get info if class can scan ZIP files
removeBufferFile($bufferFile)
scanFile($a_filepath, $a_origname="")
scan a file for viruses
logScanResult()
write the result of the last scan to the log
logCleanResult()
write the result of the last clean to the log
cleanFile($a_filepath, $a_origname="")
clean an infected file
redirection script todo: (a better solution should control the processing via a xml file) ...
ilVirusScanner($a_scancommand, $a_cleancommand)
Constructor public.
getScanMessage()
get a located message with the result from the last scan
Base class for the interface to an external virus scanner.
getCleanResult()
get the pure output of the external scan
static ilTempnam()
Create a temporary file in an ILIAS writable directory.
getCleanMessage()
get a located message with the result from the last clean
getScanResult()
get the pure output of the external scan