ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
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 buildScanCommandArguments($file = '-') // default means piping
33  {
34  return ' ' . 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->scanCommand . $this->buildScanCommandArguments(), $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  // Make group readable for clamdscan
135  $perm = fileperms($a_filepath) | 0640;
136  chmod($a_filepath, $perm);
137 
138  // Call of antivir command
139  $a_filepath = realpath($a_filepath);
140  if(file_exists($a_filepath)) {
141  $args = ilUtil::escapeShellArg($a_filepath);
142  $arguments = $this->buildScanCommandArguments($args) . " 2>&1";
143  $cmd = ilUtil::escapeShellCmd($this->scanCommand);
144  $out = ilUtil::execQuoted($cmd, $arguments);
145  $this->scanResult = implode("\n", $out);
146 
147  // sophie could be called
148  if ($this->hasDetections($this->scanResult)) {
149  $this->scanFileIsInfected = true;
150  $this->logScanResult();
151  return $this->scanResult;
152  } else {
153  $this->scanFileIsInfected = false;
154  return "";
155  }
156  }
157 
158  $this->log->write("ERROR (Virus Scanner failed): "
159  . $this->scanResult
160  . "; Path=" . $a_filepath);
161  }
162 }
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
static escapeShellCmd($a_arg)
escape shell cmd
Interface to the ClamAV virus protector.
static execQuoted($cmd, $args=null)
exec command and fix spaces on windows
Base class for the interface to an external virus scanner This class is abstract and needs to be exte...
static escapeShellArg($a_arg)
__construct(Container $dic, ilPlugin $plugin)