ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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
11require_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 {
46 if( function_exists($func) )
47 {
48 continue;
49 }
50
51 return false;
52 }
53
54 return true;
55 }
56
61 public function scanBuffer($buffer)
62 {
63 if( !$this->isBufferScanPossible() )
64 {
65 return $this->scanFileFromBuffer($buffer);
66 }
67
68 return $this->processBufferScan($buffer);
69 }
70
75 protected function processBufferScan($buffer)
76 {
77 $descriptorspec = array(
78 0 => array("pipe", "r"), // stdin is a pipe that the child will read from
79 1 => array("pipe", "w"), // stdout is a pipe that the child will write to
80 2 => array("pipe", "w") // stderr for the child
81 );
82
83 $pipes = array(); // will look like follows after passing
84 // 0 => writeable handle connected to child stdin
85 // 1 => readable handle connected to child stdout
86
87 $process = proc_open($this->buildScanCommand(), $descriptorspec, $pipes);
88
89 if( !is_resource($process) )
90 {
91 return false; // no scan, no virus detected
92 }
93
94 fwrite($pipes[0], $buffer);
95 fclose($pipes[0]);
96
97 $detectionReport = stream_get_contents($pipes[1]);
98 fclose($pipes[1]);
99
100 $errorReport = stream_get_contents($pipes[2]);
101 fclose($pipes[2]);
102
103 $return = proc_close($process);
104
105 return $this->hasDetections($detectionReport);
106 }
107
112 protected function hasDetections($detectionReport)
113 {
114 return preg_match("/FOUND/", $detectionReport);
115 }
116
124 function scanFile($a_filepath, $a_origname = "")
125 {
126 // This function should:
127 // - call the external scanner for a_filepath
128 // - set scanFilePath to a_filepath
129 // - set scanFileOrigName to a_origname
130 // - set scanFileIsInfected according the scan result
131 // - set scanResult to the scanner output message
132 // - call logScanResult() if file is infected
133 // - return the scanResult, if file is infected
134 // - return an empty string, if file is not infected
135
136 $this->scanFilePath = $a_filepath;
137 $this->scanFileOrigName = $a_origname;
138
139 // Call of antivir command
140 $cmd = $this->buildScanCommand($a_filepath)." 2>&1";
141 exec($cmd, $out, $ret);
142 $this->scanResult = implode("\n", $out);
143
144 // sophie could be called
145 if( $this->hasDetections($this->scanResult) )
146 {
147 $this->scanFileIsInfected = true;
148 $this->logScanResult();
149 return $this->scanResult;
150 }
151 else
152 {
153 $this->scanFileIsInfected = false;
154 return "";
155 }
156
157 // antivir has failed (todo)
158 $this->log->write("ERROR (Virus Scanner failed): "
159 . $this->scanResult
160 . "; COMMAMD=" . $cmd);
161
162 }
163}
An exception for terminatinating execution or to throw for unit testing.
Interface to the ClamAV virus protector.
scanFile($a_filepath, $a_origname="")
scan a file for viruses
__construct($a_scancommand, $a_cleancommand)
Constructor @access public.
Base class for the interface to an external virus scanner This class is abstract and needs to be exte...
logScanResult()
write the result of the last scan to the log @access public
$ret
Definition: parser.php:6
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file
$cmd
Definition: sahs_server.php:35