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