ILIAS  release_8 Revision v8.24
class.ilFileInputGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
29{
30 private string $filename = "";
31 private string $filename_post = "";
32 protected int $size = 40;
33 protected string $pending = "";
34 protected bool $allow_deletion = false;
35 protected bool $filename_selection = false;
36 protected array $forbidden_suffixes = [];
37 protected array $suffixes = [];
38 protected string $value = "";
39 protected \ILIAS\FileUpload\FileUpload $upload_service;
40
41 public function __construct(
42 string $a_title = "",
43 string $a_postvar = ""
44 ) {
45 global $DIC;
46
47 $this->lng = $DIC->language();
48 $lng = $DIC->language();
49 $this->upload_service = $DIC->upload();
50
51 parent::__construct($a_title, $a_postvar);
52 $this->setType("file");
53 $this->setHiddenTitle("(" . $lng->txt("form_file_input") . ")");
54 }
55
56 public function setValueByArray(array $a_values): void
57 {
58 $value = $a_values[$this->getPostVar()] ?? null;
59 if (!is_array($value)) {
60 $this->setValue((string) $value);
61 }
62 $filenam = $a_values[$this->getFileNamePostVar()] ?? '';
63 $this->setFilename($filenam);
64 }
65
69 public function setValue(string $a_value): void
70 {
71 $this->value = $a_value;
72 }
73
74 public function getValue(): string
75 {
76 return $this->value;
77 }
78
79 public function setSize(int $a_size): void
80 {
81 $this->size = $a_size;
82 }
83
84 public function getSize(): int
85 {
86 return $this->size;
87 }
88
89 // Set filename value (if filename selection is enabled)
90 public function setFilename(string $a_val): void
91 {
92 $this->filename = $a_val;
93 }
94
95 public function getFilename(): string
96 {
97 return $this->filename;
98 }
99
100 public function setSuffixes(array $a_suffixes): void
101 {
102 $this->suffixes = $a_suffixes;
103 }
104
105 public function getSuffixes(): array
106 {
107 return $this->suffixes;
108 }
109
110 public function setForbiddenSuffixes(array $a_suffixes): void
111 {
112 $this->forbidden_suffixes = $a_suffixes;
113 }
114
115 public function getForbiddenSuffixes(): array
116 {
118 }
119
120 // Set pending filename value
121 public function setPending(string $a_val): void
122 {
123 $this->pending = $a_val;
124 }
125
126 public function getPending(): string
127 {
128 return $this->pending;
129 }
130
131 // If enabled, users get the possibility to enter a filename for the uploaded file
132 public function enableFileNameSelection(string $a_post_var): void
133 {
134 $this->filename_selection = true;
135 $this->filename_post = $a_post_var;
136 }
137
138 public function isFileNameSelectionEnabled(): bool
139 {
141 }
142
143 public function getFileNamePostVar(): string
144 {
146 }
147
148 public function setAllowDeletion(bool $a_val): void
149 {
150 $this->allow_deletion = $a_val;
151 }
152
153 public function getALlowDeletion(): bool
154 {
156 }
157
158 public function checkInput(): bool
159 {
160 if (!$this->upload_service->hasBeenProcessed()) {
161 try {
162 $this->upload_service->process();
163 } catch (IllegalStateException $e) {
164 $this->setAlert($e->getMessage());
165 return false;
166 }
167 }
168
170
171 // #18756
172 if ($this->getDisabled()) {
173 return true;
174 }
175
176 // if no information is received, something went wrong
177 // this is e.g. the case, if the post_max_size has been exceeded
178 if (!isset($_FILES[$this->getPostVar()]) || !is_array($_FILES[$this->getPostVar()])) {
179 $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
180 return false;
181 }
182
183 $_FILES[$this->getPostVar()]["name"] = ilUtil::stripSlashes($_FILES[$this->getPostVar()]["name"]);
184
185 $utf_normal = $this->refinery->string()->utfnormal()->formC();
186 $_FILES[$this->getPostVar()]["name"] = $utf_normal->transform(($_FILES[$this->getPostVar()]["name"]));
187
188 // remove trailing '/'
189 $_FILES[$this->getPostVar()]["name"] = rtrim($_FILES[$this->getPostVar()]["name"], '/');
190
191 $filename = $_FILES[$this->getPostVar()]["name"];
192 $filename_arr = pathinfo($_FILES[$this->getPostVar()]["name"]);
193 $suffix = $filename_arr["extension"] ?? '';
194 $temp_name = $_FILES[$this->getPostVar()]["tmp_name"];
195 $error = $_FILES[$this->getPostVar()]["error"];
196
197 // error handling
198 if ($error > 0) {
199 switch ($error) {
200 case UPLOAD_ERR_FORM_SIZE:
201 case UPLOAD_ERR_INI_SIZE:
202 $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
203 return false;
204
205 case UPLOAD_ERR_PARTIAL:
206 $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
207 return false;
208
209 case UPLOAD_ERR_NO_FILE:
210 if ($this->getRequired()) {
211 if (!strlen($this->getValue())) {
212 $this->setAlert($lng->txt("form_msg_file_no_upload"));
213 return false;
214 }
215 }
216 break;
217
218 case UPLOAD_ERR_NO_TMP_DIR:
219 $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
220 return false;
221
222 case UPLOAD_ERR_CANT_WRITE:
223 $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
224 return false;
225
226 case UPLOAD_ERR_EXTENSION:
227 $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
228 return false;
229 }
230 }
231
232 // check suffixes
233 if ($_FILES[$this->getPostVar()]["tmp_name"] != "") {
234 if (is_array($this->forbidden_suffixes) && in_array(strtolower($suffix), $this->forbidden_suffixes)) {
235 $this->setAlert($lng->txt("form_msg_file_type_is_not_allowed") . " (" . $suffix . ")");
236 return false;
237 }
238 if (is_array($this->getSuffixes()) && count($this->getSuffixes()) > 0) {
239 if (!in_array(strtolower($suffix), $this->getSuffixes())) {
240 $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
241 return false;
242 }
243 }
244 }
245
246 // virus handling
247 if ($_FILES[$this->getPostVar()]["tmp_name"] != "") {
248 $vir = ilVirusScanner::virusHandling($temp_name, $filename);
249 if ($vir[0] == false) {
250 $this->setAlert($lng->txt("form_msg_file_virus_found") . "<br />" . $vir[1]);
251 return false;
252 }
253 }
254
255 $file_name = $this->str('file_name');
256 if ($file_name === "") {
257 $file_name = $_FILES[$this->getPostVar()]["name"];
258 }
259 $this->setFilename($file_name);
260
261 return true;
262 }
263
264 public function getInput(): array
265 {
266 return $_FILES[$this->getPostVar()] ?? [];
267 }
268
269 public function render(string $a_mode = ""): string
270 {
272
273 $quota_exceeded = $quota_legend = false;
274
275 $f_tpl = new ilTemplate("tpl.prop_file.html", true, true, "Services/Form");
276
277
278 // show filename selection if enabled
279 if ($this->isFileNameSelectionEnabled()) {
280 $f_tpl->setCurrentBlock('filename');
281 $f_tpl->setVariable('POST_FILENAME', $this->getFileNamePostVar());
282 $f_tpl->setVariable('VAL_FILENAME', $this->getFilename());
283 $f_tpl->setVariable('FILENAME_ID', $this->getFieldId());
284 $f_tpl->setVariable('TXT_FILENAME_HINT', $lng->txt('if_no_title_then_filename'));
285 $f_tpl->parseCurrentBlock();
286 } else {
287 if (trim($this->getValue()) != "") {
288 if (!$this->getDisabled() && $this->getALlowDeletion()) {
289 $f_tpl->setCurrentBlock("delete_bl");
290 $f_tpl->setVariable("POST_VAR_D", $this->getPostVar());
291 $f_tpl->setVariable(
292 "TXT_DELETE_EXISTING",
293 $lng->txt("delete_existing_file")
294 );
295 $f_tpl->parseCurrentBlock();
296 }
297
298 $f_tpl->setCurrentBlock('prop_file_propval');
299 $f_tpl->setVariable('FILE_VAL', $this->getValue());
300 $f_tpl->parseCurrentBlock();
301 }
302 }
303
304 if ($a_mode != "toolbar") {
305 if (!$quota_exceeded) {
306 $this->outputSuffixes($f_tpl);
307
308 $f_tpl->setCurrentBlock("max_size");
309 $f_tpl->setVariable("TXT_MAX_SIZE", $lng->txt("file_notice") . " " .
310 $this->getMaxFileSizeString());
311 $f_tpl->parseCurrentBlock();
312
313 if ($quota_legend) {
314 $f_tpl->setVariable("TXT_MAX_SIZE", true);
315 $f_tpl->parseCurrentBlock();
316 }
317 } else {
318 $f_tpl->setCurrentBlock("max_size");
319 $f_tpl->setVariable("TXT_MAX_SIZE", $quota_exceeded);
320 $f_tpl->parseCurrentBlock();
321 }
322 } elseif ($quota_exceeded) {
323 return $quota_exceeded;
324 }
325
326 $pending = $this->getPending();
327 if ($pending) {
328 $f_tpl->setCurrentBlock("pending");
329 $f_tpl->setVariable("TXT_PENDING", $lng->txt("file_upload_pending") .
330 ": " . htmlentities($pending));
331 $f_tpl->parseCurrentBlock();
332 }
333
334 if ($this->getDisabled() || $quota_exceeded) {
335 $f_tpl->setVariable(
336 "DISABLED",
337 " disabled=\"disabled\""
338 );
339 }
340
341 $f_tpl->setVariable("POST_VAR", $this->getPostVar());
342 $f_tpl->setVariable("ID", $this->getFieldId());
343 $f_tpl->setVariable("SIZE", $this->getSize());
344
345
346 /* experimental: bootstrap'ed file upload */
347 $f_tpl->setVariable("TXT_BROWSE", $lng->txt("select_file"));
348
349
350 return $f_tpl->get();
351 }
352
353 public function insert(ilTemplate $a_tpl): void
354 {
355 $html = $this->render();
356
357 $a_tpl->setCurrentBlock("prop_generic");
358 $a_tpl->setVariable("PROP_GENERIC", $html);
359 $a_tpl->parseCurrentBlock();
360 }
361
362
363 protected function outputSuffixes(
364 ilTemplate $a_tpl,
365 string $a_block = "allowed_suffixes"
366 ): void {
367 $lng = $this->lng;
368
369 if (is_array($this->getSuffixes()) && count($this->getSuffixes()) > 0) {
370 $suff_str = $delim = "";
371 foreach ($this->getSuffixes() as $suffix) {
372 $suff_str .= $delim . "." . $suffix;
373 $delim = ", ";
374 }
375 $a_tpl->setCurrentBlock($a_block);
376 $a_tpl->setVariable(
377 "TXT_ALLOWED_SUFFIXES",
378 $lng->txt("file_allowed_suffixes") . " " . $suff_str
379 );
380 $a_tpl->parseCurrentBlock();
381 }
382 }
383
384 protected function getMaxFileSizeString(): string
385 {
386 // get the value for the maximal uploadable filesize from the php.ini (if available)
387 $umf = ini_get("upload_max_filesize");
388 // get the value for the maximal post data from the php.ini (if available)
389 $pms = ini_get("post_max_size");
390
391 //convert from short-string representation to "real" bytes
392 $multiplier_a = array("K" => 1024, "M" => 1024 * 1024, "G" => 1024 * 1024 * 1024);
393
394 $umf_parts = preg_split("/(\d+)([K|G|M])/", $umf, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
395 $pms_parts = preg_split("/(\d+)([K|G|M])/", $pms, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
396
397 if (count($umf_parts) == 2) {
398 $umf = $umf_parts[0] * $multiplier_a[$umf_parts[1]];
399 }
400 if (count($pms_parts) == 2) {
401 $pms = $pms_parts[0] * $multiplier_a[$pms_parts[1]];
402 }
403
404 // use the smaller one as limit
405 $max_filesize = min($umf, $pms);
406
407 if (!$max_filesize) {
408 $max_filesize = max($umf, $pms);
409 }
410
411 //format for display in mega-bytes
412 $max_filesize = sprintf("%.1f MB", $max_filesize / 1024 / 1024);
413
414 return $max_filesize;
415 }
416
420 protected function getMaxFileUploads(): int
421 {
422 return (int) ini_get("max_file_uploads");
423 }
424
425 public function getDeletionFlag(): bool
426 {
427 if ($this->int($this->getPostVar() . "_delete")) {
428 return true;
429 }
430 return false;
431 }
432
433 public function getToolbarHTML(): string
434 {
435 $html = $this->render("toolbar");
436 return $html;
437 }
438}
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:514
This class represents a file property in a property form.
__construct(string $a_title="", string $a_postvar="")
setFilename(string $a_val)
setForbiddenSuffixes(array $a_suffixes)
setSuffixes(array $a_suffixes)
setAllowDeletion(bool $a_val)
setPending(string $a_val)
setValueByArray(array $a_values)
setValue(string $a_value)
Set Value.
getMaxFileUploads()
Get number of maximum file uploads as declared in php.ini.
outputSuffixes(ilTemplate $a_tpl, string $a_block="allowed_suffixes")
getToolbarHTML()
Get input item HTML to be inserted into ilToolbarGUI.
insert(ilTemplate $a_tpl)
ILIAS FileUpload FileUpload $upload_service
render(string $a_mode="")
enableFileNameSelection(string $a_post_var)
checkInput()
Check input, strip slashes etc.
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...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
special template class to simplify handling of ITX/PEAR
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
static virusHandling(string $a_file, string $a_orig_name='', bool $a_clean=true)
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc