ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 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 // Make group readable for clamdscan
136 $currentPermission = fileperms($a_filepath);
137 $perm = $currentPermission | 0640;
138 chmod($a_filepath, $perm);
139
140 // Call of antivir command
141 $cmd = $this->buildScanCommand($a_filepath) . " 2>&1";
142 exec($cmd, $out, $ret);
143 $this->scanResult = implode("\n", $out);
144
145 if ($perm != $currentPermission) {
146 chmod($a_filepath, $currentPermission);
147 }
148
149 // sophie could be called
150 if ($this->hasDetections($this->scanResult)) {
151 $this->scanFileIsInfected = true;
152 $this->logScanResult();
153 return $this->scanResult;
154 } else {
155 $this->scanFileIsInfected = false;
156 return "";
157 }
158
159 // antivir has failed (todo)
160 $this->log->write("ERROR (Virus Scanner failed): "
161 . $this->scanResult
162 . "; COMMAMD=" . $cmd);
163 }
164}
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