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