ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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
73 public static function virusHandling(string $a_file, string $a_orig_name = '', bool $a_clean = true): array
74 {
75 global $DIC;
76
77 $lng = $DIC->language();
78
79 if ((defined('IL_VIRUS_SCANNER') && IL_VIRUS_SCANNER !== 'None') || (defined(
80 'IL_ICAP_HOST'
81 ) && IL_ICAP_HOST !== '')) {
83 if (($vs_txt = $vs->scanFile($a_file, $a_orig_name)) !== '') {
84 if ($a_clean && defined('IL_VIRUS_CLEAN_COMMAND') && IL_VIRUS_CLEAN_COMMAND !== '') {
85 $clean_txt = $vs->cleanFile($a_file, $a_orig_name);
86 if ($vs->fileCleaned()) {
87 $vs_txt .= '<br />' . $lng->txt('cleaned_file') . '<br />' . $clean_txt;
88 $vs_txt .= '<br />' . $lng->txt('repeat_scan');
89 if (($vs2_txt = $vs->scanFile($a_file, $a_orig_name)) !== '') {
90 return [
91 false,
92 nl2br($vs_txt) . '<br />' . $lng->txt('repeat_scan_failed') . '<br />' . nl2br($vs2_txt)
93 ];
94 }
95
96 return [true, nl2br($vs_txt) . '<br />' . $lng->txt('repeat_scan_succeded')];
97 }
98
99 return [false, nl2br($vs_txt) . '<br />' . $lng->txt('cleaning_failed')];
100 }
101
102 return [false, nl2br($vs_txt)];
103 }
104 }
105
106 return [true, ''];
107 }
108
109 public function scanBuffer(string $buffer): bool
110 {
111 return $this->scanFileFromBuffer($buffer);
112 }
113
114 protected function scanFileFromBuffer(string $buffer): bool
115 {
116 $bufferFile = $this->createBufferFile($buffer);
117 $infection = $this->scanFile($bufferFile);
118 $this->removeBufferFile($bufferFile);
119 return $infection !== '';
120 }
121
122 protected function createBufferFile(string $buffer): string
123 {
124 $bufferFile = ilFileUtils::ilTempnam();
125 file_put_contents($bufferFile, $buffer);
126 return $bufferFile;
127 }
128
129 public function scanFile(string $file_path, string $org_name = ''): string
130 {
131 $this->scanFilePath = $file_path;
132 $this->scanFileOrigName = $org_name;
133
134 if ($org_name === 'infected.txt' || $org_name === 'cleanable.txt') {
135 $this->scanFileIsInfected = true;
136 $this->scanResult =
137 'FILE INFECTED: [' . $file_path . '] (VIRUS: simulated)';
138 $this->logScanResult();
139 return $this->scanResult;
140 }
141
142 $this->scanFileIsInfected = false;
143 $this->scanResult = '';
144 return '';
145 }
146
147 public function logScanResult(): void
148 {
149 $mess = 'Virus Scanner (' . $this->type . ')';
150 if ($this->scanFileOrigName) {
151 $mess .= ' (File ' . $this->scanFileOrigName . ')';
152 }
153 $mess .= ': ' . preg_replace('/[\r\n]+/', '; ', $this->scanResult);
154
155 $this->log->write($mess);
156 }
157
158 protected function removeBufferFile(string $bufferFile): void
159 {
160 unlink($bufferFile);
161 }
162
163 public function cleanFile(string $file_path, string $org_name = ''): string
164 {
165 $this->cleanFilePath = $file_path;
166 $this->cleanFileOrigName = $org_name;
167
168 if ($org_name === 'cleanable.txt') {
169 $this->cleanFileIsCleaned = true;
170 $this->cleanResult =
171 'FILE CLEANED: [' . $file_path . '] (VIRUS: simulated)';
172 $this->logCleanResult();
173 return $this->cleanResult;
174 }
175
176 $this->cleanFileIsCleaned = false;
177 $this->cleanResult =
178 'FILE NOT CLEANED: [' . $file_path . '] (VIRUS: simulated)';
179 $this->logCleanResult();
180 return '';
181 }
182
183 public function logCleanResult(): void
184 {
185 $mess = 'Virus Cleaner (' . $this->type . ')';
186 if ($this->cleanFileOrigName) {
187 $mess .= ' (File ' . $this->cleanFileOrigName . ')';
188 }
189 $mess .= ': ' . preg_replace('/[\r\n]+/', '; ', $this->cleanResult);
190
191 $this->log->write($mess);
192 }
193
194 public function fileCleaned(): bool
195 {
197 }
198
199 public function getScanResult(): string
200 {
201 return $this->scanResult;
202 }
203
204 public function getCleanResult(): string
205 {
206 return $this->cleanResult;
207 }
208
209 public function getScanMessage(): string
210 {
211 if ($this->scanFileIsInfected) {
212 $ret = sprintf($this->lng->txt('virus_infected'), $this->scanFileOrigName);
213 } else {
214 $ret = sprintf($this->lng->txt('virus_not_infected'), $this->scanFileOrigName);
215 }
216
217 if ($this->scanResult) {
218 $ret .= ' ' . $this->lng->txt('virus_scan_message')
219 . '<br />'
220 . str_replace(
221 $this->scanFilePath,
222 $this->scanFileOrigName,
223 nl2br($this->scanResult)
224 );
225 }
226 return $ret;
227 }
228
229 public function getCleanMessage(): string
230 {
231 if ($this->cleanFileIsCleaned) {
232 $ret = sprintf($this->lng->txt('virus_cleaned'), $this->cleanFileOrigName);
233 } else {
234 $ret = sprintf($this->lng->txt('virus_not_cleaned'), $this->cleanFileOrigName);
235 }
236
237 if ($this->cleanResult) {
238 $ret .= ' ' . $this->lng->txt('virus_clean_message')
239 . '<br />'
240 . str_replace(
241 $this->cleanFilePath,
242 $this->cleanFileOrigName,
243 nl2br($this->cleanResult)
244 );
245 }
246 return $ret;
247 }
248
249 public function getScanZipFiles(): bool
250 {
251 return $this->scanZipFiles;
252 }
253}
error(string $a_errmsg)
Error Handling & global info handling.
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
cleanFile(string $file_path, string $org_name='')
scanFile(string $file_path, string $org_name='')
createBufferFile(string $buffer)
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: shib_login.php:26