ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
13require_once "./Services/VirusScanner/classes/class.ilVirusScanner.php";
14
16{
17 const ADD_SCAN_PARAMS = '--no-summary -i';
18
24 function ilVirusScannerClamAV($a_scancommand, $a_cleancommand)
25 {
26 $this->ilVirusScanner($a_scancommand, $a_cleancommand);
27 $this->type = "clamav";
28 $this->scanZipFiles = true;
29 }
30
34 protected function buildScanCommand($file = '-') // default means piping
35 {
36 return $this->scanCommand.' '.self::ADD_SCAN_PARAMS.' '.$file;
37 }
38
42 protected function isBufferScanPossible()
43 {
44 $functions = array('proc_open', 'proc_close');
45
46 foreach($functions as $func)
47 {
48 if( function_exists($func) )
49 {
50 continue;
51 }
52
53 return false;
54 }
55
56 return true;
57 }
58
63 public function scanBuffer($buffer)
64 {
65 if( !$this->isBufferScanPossible() )
66 {
67 return $this->scanFileFromBuffer($buffer);
68 }
69
70 return $this->processBufferScan($buffer);
71 }
72
77 protected function processBufferScan($buffer)
78 {
79 $descriptorspec = array(
80 0 => array("pipe", "r"), // stdin is a pipe that the child will read from
81 1 => array("pipe", "w"), // stdout is a pipe that the child will write to
82 2 => array("pipe", "w") // stderr for the child
83 );
84
85 $pipes = array(); // will look like follows after passing
86 // 0 => writeable handle connected to child stdin
87 // 1 => readable handle connected to child stdout
88
89 $process = proc_open($this->buildScanCommand(), $descriptorspec, $pipes);
90
91 if( !is_resource($process) )
92 {
93 return false; // no scan, no virus detected
94 }
95
96 fwrite($pipes[0], $buffer);
97 fclose($pipes[0]);
98
99 $detectionReport = stream_get_contents($pipes[1]);
100 fclose($pipes[1]);
101
102 $errorReport = stream_get_contents($pipes[2]);
103 fclose($pipes[2]);
104
105 $return = proc_close($process);
106
107 return $this->hasDetections($detectionReport);
108 }
109
114 protected function hasDetections($detectionReport)
115 {
116 return preg_match("/FOUND/", $detectionReport);
117 }
118
127 function scanFile($a_filepath, $a_origname = "")
128 {
129 // This function should:
130 // - call the external scanner for a_filepath
131 // - set scanFilePath to a_filepath
132 // - set scanFileOrigName to a_origname
133 // - set scanFileIsInfected according the scan result
134 // - set scanResult to the scanner output message
135 // - call logScanResult() if file is infected
136 // - return the scanResult, if file is infected
137 // - return an empty string, if file is not infected
138
139 $this->scanFilePath = $a_filepath;
140 $this->scanFileOrigName = $a_origname;
141
142 // Call of antivir command
143 $cmd = $this->buildScanCommand($a_filepath)." 2>&1";
144 exec($cmd, $out, $ret);
145 $this->scanResult = implode("\n", $out);
146
147 // sophie could be called
148 if( $this->hasDetections($this->scanResult) )
149 {
150 $this->scanFileIsInfected = true;
151 $this->logScanResult();
152 return $this->scanResult;
153 }
154 else
155 {
156 $this->scanFileIsInfected = false;
157 return "";
158 }
159
160 // antivir has failed (todo)
161 $this->log->write("ERROR (Virus Scanner failed): "
162 . $this->scanResult
163 . "; COMMAMD=" . $cmd);
164
165 }
166
167
168}
169?>
print $file
Interface to the ClamAV virus protector.
scanFile($a_filepath, $a_origname="")
scan a file for viruses
ilVirusScannerClamAV($a_scancommand, $a_cleancommand)
Constructor @access public.
Base class for the interface to an external virus scanner.
logScanResult()
write the result of the last scan to the log
ilVirusScanner($a_scancommand, $a_cleancommand)
Constructor @access public.
$cmd
Definition: sahs_server.php:35