ILIAS  release_8 Revision v8.24
class.ilVirusScanner.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
23 public string $type;
24
25 public bool $scanZipFiles;
26
27 public string $scanCommand;
28
29 public string $cleanCommand;
30
31 public string $scanFilePath;
32
33 public string $scanFileOrigName;
34
35 public string $cleanFilePath;
36
37 public string $cleanFileOrigName;
38
40
42
43 public string $scanResult;
44
45 public string $cleanResult;
46
48
50
51 public ilLogger $log;
52
53 public function __construct(string $scan_command, string $clean_command)
54 {
55 global $DIC;
56 $error = $DIC['ilErr'];
57 $lng = $DIC->language();
58 $log = $DIC->logger()->root();
59
60 $this->error = $error;
61 $this->lng = $lng;
62 $this->log = $log;
63 $this->scanCommand = $scan_command;
64 $this->cleanCommand = $clean_command;
65
66 $this->type = "simulate";
67 $this->scanZipFiles = false;
68 }
69
76 public static function virusHandling(string $a_file, string $a_orig_name = '', bool $a_clean = true): array
77 {
78 global $DIC;
79
80 $lng = $DIC->language();
81
82 if ((defined('IL_VIRUS_SCANNER') && IL_VIRUS_SCANNER !== 'None') || (defined(
83 'IL_ICAP_HOST'
84 ) && IL_ICAP_HOST !== '')) {
86 if (($vs_txt = $vs->scanFile($a_file, $a_orig_name)) !== '') {
87 if ($a_clean && defined('IL_VIRUS_CLEAN_COMMAND') && IL_VIRUS_CLEAN_COMMAND !== '') {
88 $clean_txt = $vs->cleanFile($a_file, $a_orig_name);
89 if ($vs->fileCleaned()) {
90 $vs_txt .= '<br />' . $lng->txt('cleaned_file') . '<br />' . $clean_txt;
91 $vs_txt .= '<br />' . $lng->txt('repeat_scan');
92 if (($vs2_txt = $vs->scanFile($a_file, $a_orig_name)) !== '') {
93 return [
94 false,
95 nl2br($vs_txt) . '<br />' . $lng->txt('repeat_scan_failed') . '<br />' . nl2br($vs2_txt)
96 ];
97 }
98
99 return [true, nl2br($vs_txt) . '<br />' . $lng->txt('repeat_scan_succeded')];
100 }
101
102 return [false, nl2br($vs_txt) . '<br />' . $lng->txt('cleaning_failed')];
103 }
104
105 return [false, nl2br($vs_txt)];
106 }
107 }
108
109 return [true, ''];
110 }
111
112 public function scanBuffer(string $buffer): bool
113 {
114 return $this->scanFileFromBuffer($buffer);
115 }
116
117 protected function scanFileFromBuffer(string $buffer): bool
118 {
119 $bufferFile = $this->createBufferFile($buffer);
120 $infection = $this->scanFile($bufferFile);
121 $this->removeBufferFile($bufferFile);
122 return $infection !== '';
123 }
124
125 protected function createBufferFile(string $buffer): string
126 {
127 $bufferFile = ilFileUtils::ilTempnam();
128 file_put_contents($bufferFile, $buffer);
129 return $bufferFile;
130 }
131
132 public function scanFile(string $file_path, string $org_name = ""): string
133 {
134 $this->scanFilePath = $file_path;
135 $this->scanFileOrigName = $org_name;
136
137 if ($org_name === "infected.txt" || $org_name === "cleanable.txt") {
138 $this->scanFileIsInfected = true;
139 $this->scanResult =
140 "FILE INFECTED: [" . $file_path . "] (VIRUS: simulated)";
141 $this->logScanResult();
142 return $this->scanResult;
143 }
144
145 $this->scanFileIsInfected = false;
146 $this->scanResult = "";
147 return "";
148 }
149
150 public function logScanResult(): void
151 {
152 $mess = "Virus Scanner (" . $this->type . ")";
153 if ($this->scanFileOrigName) {
154 $mess .= " (File " . $this->scanFileOrigName . ")";
155 }
156 $mess .= ": " . preg_replace('/[\r\n]+/', "; ", $this->scanResult);
157
158 $this->log->write($mess);
159 }
160
161 protected function removeBufferFile(string $bufferFile): void
162 {
163 unlink($bufferFile);
164 }
165
166 public function cleanFile(string $file_path, string $org_name = ""): string
167 {
168 $this->cleanFilePath = $file_path;
169 $this->cleanFileOrigName = $org_name;
170
171 if ($org_name === "cleanable.txt") {
172 $this->cleanFileIsCleaned = true;
173 $this->cleanResult =
174 "FILE CLEANED: [" . $file_path . "] (VIRUS: simulated)";
175 $this->logCleanResult();
176 return $this->cleanResult;
177 }
178
179 $this->cleanFileIsCleaned = false;
180 $this->cleanResult =
181 "FILE NOT CLEANED: [" . $file_path . "] (VIRUS: simulated)";
182 $this->logCleanResult();
183 return "";
184 }
185
186 public function logCleanResult(): void
187 {
188 $mess = "Virus Cleaner (" . $this->type . ")";
189 if ($this->cleanFileOrigName) {
190 $mess .= " (File " . $this->cleanFileOrigName . ")";
191 }
192 $mess .= ": " . preg_replace('/[\r\n]+/', "; ", $this->cleanResult);
193
194 $this->log->write($mess);
195 }
196
197 public function fileCleaned(): bool
198 {
200 }
201
202 public function getScanResult(): string
203 {
204 return $this->scanResult;
205 }
206
207 public function getCleanResult(): string
208 {
209 return $this->cleanResult;
210 }
211
212 public function getScanMessage(): string
213 {
214 if ($this->scanFileIsInfected) {
215 $ret = sprintf($this->lng->txt("virus_infected"), $this->scanFileOrigName);
216 } else {
217 $ret = sprintf($this->lng->txt("virus_not_infected"), $this->scanFileOrigName);
218 }
219
220 if ($this->scanResult) {
221 $ret .= " " . $this->lng->txt("virus_scan_message")
222 . "<br />"
223 . str_replace(
224 $this->scanFilePath,
225 $this->scanFileOrigName,
226 nl2br($this->scanResult)
227 );
228 }
229 return $ret;
230 }
231
232 public function getCleanMessage(): string
233 {
234 if ($this->cleanFileIsCleaned) {
235 $ret = sprintf($this->lng->txt("virus_cleaned"), $this->cleanFileOrigName);
236 } else {
237 $ret = sprintf($this->lng->txt("virus_not_cleaned"), $this->cleanFileOrigName);
238 }
239
240 if ($this->cleanResult) {
241 $ret .= " " . $this->lng->txt("virus_clean_message")
242 . "<br />"
243 . str_replace(
244 $this->cleanFilePath,
245 $this->cleanFileOrigName,
246 nl2br($this->cleanResult)
247 );
248 }
249 return $ret;
250 }
251
252 public function getScanZipFiles(): bool
253 {
254 return $this->scanZipFiles;
255 }
256}
error(string $a_errmsg)
Error Handling & global info handling uses PEAR error class.
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
language handling
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
Component logger with individual log levels by component id.
scanFileFromBuffer(string $buffer)
ilErrorHandling $error
createBufferFile(string $buffer)
cleanFile(string $file_path, string $org_name="")
scanFile(string $file_path, string $org_name="")
static virusHandling(string $a_file, string $a_orig_name='', bool $a_clean=true)
scanBuffer(string $buffer)
removeBufferFile(string $bufferFile)
__construct(string $scan_command, string $clean_command)
global $DIC
Definition: feed.php:28