ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilVirusScannerClamAV.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
11 require_once "./Services/VirusScanner/classes/class.ilVirusScanner.php";
12 
14 {
15  const ADD_SCAN_PARAMS = '--no-summary -i';
16 
22  public function __construct($a_scancommand, $a_cleancommand)
23  {
24  parent::__construct($a_scancommand, $a_cleancommand);
25  $this->type = "clamav";
26  $this->scanZipFiles = true;
27  }
28 
32  protected function buildScanCommand($file = '-') // default means piping
33  {
34  return $this->scanCommand . ' ' . self::ADD_SCAN_PARAMS . ' ' . $file;
35  }
36 
40  protected function isBufferScanPossible()
41  {
42  $functions = array('proc_open', 'proc_close');
43 
44  foreach ($functions as $func) {
45  if (function_exists($func)) {
46  continue;
47  }
48 
49  return false;
50  }
51 
52  return true;
53  }
54 
59  public function scanBuffer($buffer)
60  {
61  if (!$this->isBufferScanPossible()) {
62  return $this->scanFileFromBuffer($buffer);
63  }
64 
65  return $this->processBufferScan($buffer);
66  }
67 
72  protected function processBufferScan($buffer)
73  {
74  $descriptorspec = array(
75  0 => array("pipe", "r"), // stdin is a pipe that the child will read from
76  1 => array("pipe", "w"), // stdout is a pipe that the child will write to
77  2 => array("pipe", "w") // stderr for the child
78  );
79 
80  $pipes = array(); // will look like follows after passing
81  // 0 => writeable handle connected to child stdin
82  // 1 => readable handle connected to child stdout
83 
84  $process = proc_open($this->buildScanCommand(), $descriptorspec, $pipes);
85 
86  if (!is_resource($process)) {
87  return false; // no scan, no virus detected
88  }
89 
90  fwrite($pipes[0], $buffer);
91  fclose($pipes[0]);
92 
93  $detectionReport = stream_get_contents($pipes[1]);
94  fclose($pipes[1]);
95 
96  $errorReport = stream_get_contents($pipes[2]);
97  fclose($pipes[2]);
98 
99  $return = proc_close($process);
100 
101  return $this->hasDetections($detectionReport);
102  }
103 
108  protected function hasDetections($detectionReport)
109  {
110  return preg_match("/FOUND/", $detectionReport);
111  }
112 
120  public function scanFile($a_filepath, $a_origname = "")
121  {
122  // This function should:
123  // - call the external scanner for a_filepath
124  // - set scanFilePath to a_filepath
125  // - set scanFileOrigName to a_origname
126  // - set scanFileIsInfected according the scan result
127  // - set scanResult to the scanner output message
128  // - call logScanResult() if file is infected
129  // - return the scanResult, if file is infected
130  // - return an empty string, if file is not infected
131 
132  $this->scanFilePath = $a_filepath;
133  $this->scanFileOrigName = $a_origname;
134 
135  // Call of antivir command
136  $cmd = $this->buildScanCommand($a_filepath) . " 2>&1";
137  exec($cmd, $out, $ret);
138  $this->scanResult = implode("\n", $out);
139 
140  // sophie could be called
141  if ($this->hasDetections($this->scanResult)) {
142  $this->scanFileIsInfected = true;
143  $this->logScanResult();
144  return $this->scanResult;
145  } else {
146  $this->scanFileIsInfected = false;
147  return "";
148  }
149 
150  // antivir has failed (todo)
151  $this->log->write("ERROR (Virus Scanner failed): "
152  . $this->scanResult
153  . "; COMMAMD=" . $cmd);
154  }
155 }
logScanResult()
write the result of the last scan to the log public
__construct($a_scancommand, $a_cleancommand)
Constructor public.
scanFile($a_filepath, $a_origname="")
scan a file for viruses
Interface to the ClamAV virus protector.
Create styles array
The data for the language used.
Base class for the interface to an external virus scanner This class is abstract and needs to be exte...
$ret
Definition: parser.php:6
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file