ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilFileInputGUI.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once 'Services/UIComponent/Toolbar/interfaces/interface.ilToolbarItem.php';
5include_once("./Services/Form/classes/class.ilSubEnabledFormPropertyGUI.php");
6
15{
19 protected $lng;
20
21 private $filename;
23 protected $size = 40;
24 protected $pending;
25 protected $allow_deletion;
26
27 protected static $check_wsp_quota;
28
32 protected $forbidden_suffixes = array();
33
40 public function __construct($a_title = "", $a_postvar = "")
41 {
42 global $DIC;
43
44 $this->lng = $DIC->language();
45 $lng = $DIC->language();
46
47 parent::__construct($a_title, $a_postvar);
48 $this->setType("file");
49 $this->setHiddenTitle("(" . $lng->txt("form_file_input") . ")");
50 }
51
57 public function setValueByArray($a_values)
58 {
59 if (!is_array($a_values[$this->getPostVar()])) {
60 $this->setValue($a_values[$this->getPostVar()]);
61 }
62 $this->setFilename($a_values[$this->getFileNamePostVar()]);
63 }
64
70 public function setValue($a_value)
71 {
72 $this->value = $a_value;
73 }
74
80 public function getValue()
81 {
82 return $this->value;
83 }
84
90 public function setSize($a_size)
91 {
92 $this->size = $a_size;
93 }
94
100 public function getSize()
101 {
102 return $this->size;
103 }
104
110 public function setFilename($a_val)
111 {
112 $this->filename = $a_val;
113 }
114
120 public function getFilename()
121 {
122 return $this->filename;
123 }
124
125
126
132 public function setSuffixes($a_suffixes)
133 {
134 $this->suffixes = $a_suffixes;
135 }
136
142 public function getSuffixes()
143 {
144 return $this->suffixes;
145 }
146
152 public function setForbiddenSuffixes($a_suffixes)
153 {
154 $this->forbidden_suffixes = $a_suffixes;
155 }
156
162 public function getForbiddenSuffixes()
163 {
165 }
166
172 public function setPending($a_val)
173 {
174 $this->pending = $a_val;
175 }
176
182 public function getPending()
183 {
184 return $this->pending;
185 }
186
194 public function enableFileNameSelection($a_post_var)
195 {
196 $this->filename_selection = true;
197 $this->filename_post = $a_post_var;
198 }
199
207 {
208 return $this->filename_selection ? true : false;
209 }
210
218 public function getFileNamePostVar()
219 {
221 }
222
228 public function setALlowDeletion($a_val)
229 {
230 $this->allow_deletion = $a_val;
231 }
232
238 public function getALlowDeletion()
239 {
241 }
242
248 public function checkInput()
249 {
251
252 // #18756
253 if ($this->getDisabled()) {
254 return true;
255 }
256
257 // if no information is received, something went wrong
258 // this is e.g. the case, if the post_max_size has been exceeded
259 if (!is_array($_FILES[$this->getPostVar()])) {
260 $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
261 return false;
262 }
263
264 $_FILES[$this->getPostVar()]["name"] = ilUtil::stripSlashes($_FILES[$this->getPostVar()]["name"]);
265
266 include_once("./Services/Utilities/classes/class.ilStr.php");
267 $_FILES[$this->getPostVar()]["name"] = ilStr::normalizeUtf8String($_FILES[$this->getPostVar()]["name"]);
268
269 // remove trailing '/'
270 $_FILES[$this->getPostVar()]["name"] = rtrim($_FILES[$this->getPostVar()]["name"], '/');
271
272 $filename = $_FILES[$this->getPostVar()]["name"];
273 $filename_arr = pathinfo($_FILES[$this->getPostVar()]["name"]);
274 $suffix = $filename_arr["extension"];
275 $mimetype = $_FILES[$this->getPostVar()]["type"];
276 $size_bytes = $_FILES[$this->getPostVar()]["size"];
277 $temp_name = $_FILES[$this->getPostVar()]["tmp_name"];
278 $error = $_FILES[$this->getPostVar()]["error"];
279 $_POST[$this->getPostVar()] = $_FILES[$this->getPostVar()];
280
281 // error handling
282 if ($error > 0) {
283 switch ($error) {
284 case UPLOAD_ERR_INI_SIZE:
285 $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
286 return false;
287 break;
288
289 case UPLOAD_ERR_FORM_SIZE:
290 $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
291 return false;
292 break;
293
294 case UPLOAD_ERR_PARTIAL:
295 $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
296 return false;
297 break;
298
299 case UPLOAD_ERR_NO_FILE:
300 if ($this->getRequired()) {
301 if (!strlen($this->getValue())) {
302 $this->setAlert($lng->txt("form_msg_file_no_upload"));
303 return false;
304 }
305 }
306 break;
307
308 case UPLOAD_ERR_NO_TMP_DIR:
309 $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
310 return false;
311 break;
312
313 case UPLOAD_ERR_CANT_WRITE:
314 $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
315 return false;
316 break;
317
318 case UPLOAD_ERR_EXTENSION:
319 $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
320 return false;
321 break;
322 }
323 }
324
325 // check suffixes
326 if ($_FILES[$this->getPostVar()]["tmp_name"] != "") {
327 if (is_array($this->forbidden_suffixes) && in_array(strtolower($suffix), $this->forbidden_suffixes)) {
328 $this->setAlert($lng->txt("form_msg_file_type_is_not_allowed") . " (" . $suffix . ")");
329 return false;
330 }
331 if (is_array($this->getSuffixes()) && count($this->getSuffixes()) > 0) {
332 if (!in_array(strtolower($suffix), $this->getSuffixes())) {
333 $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
334 return false;
335 }
336 }
337 }
338
339 // virus handling
340 if ($_FILES[$this->getPostVar()]["tmp_name"] != "") {
341 $vir = ilUtil::virusHandling($temp_name, $filename);
342 if ($vir[0] == false) {
343 $this->setAlert($lng->txt("form_msg_file_virus_found") . "<br />" . $vir[1]);
344 return false;
345 }
346 }
347
348 return true;
349 }
350
354 public function render($a_mode = "")
355 {
357
358 $quota_exceeded = $quota_legend = false;
359
360 $f_tpl = new ilTemplate("tpl.prop_file.html", true, true, "Services/Form");
361
362
363 // show filename selection if enabled
364 if ($this->isFileNameSelectionEnabled()) {
365 $f_tpl->setCurrentBlock('filename');
366 $f_tpl->setVariable('POST_FILENAME', $this->getFileNamePostVar());
367 $f_tpl->setVariable('VAL_FILENAME', $this->getFilename());
368 $f_tpl->setVariable('FILENAME_ID', $this->getFieldId());
369 $f_tpl->setVAriable('TXT_FILENAME_HINT', $lng->txt('if_no_title_then_filename'));
370 $f_tpl->parseCurrentBlock();
371 } else {
372 if (trim($this->getValue() != "")) {
373 if (!$this->getDisabled() && $this->getALlowDeletion()) {
374 $f_tpl->setCurrentBlock("delete_bl");
375 $f_tpl->setVariable("POST_VAR_D", $this->getPostVar());
376 $f_tpl->setVariable(
377 "TXT_DELETE_EXISTING",
378 $lng->txt("delete_existing_file")
379 );
380 $f_tpl->parseCurrentBlock();
381 }
382
383 $f_tpl->setCurrentBlock('prop_file_propval');
384 $f_tpl->setVariable('FILE_VAL', $this->getValue());
385 $f_tpl->parseCurrentBlock();
386 }
387 }
388
389 if ($a_mode != "toolbar") {
390 if (!$quota_exceeded) {
391 $this->outputSuffixes($f_tpl);
392
393 $f_tpl->setCurrentBlock("max_size");
394 $f_tpl->setVariable("TXT_MAX_SIZE", $lng->txt("file_notice") . " " .
395 $this->getMaxFileSizeString());
396 $f_tpl->parseCurrentBlock();
397
398 if ($quota_legend) {
399 $f_tpl->setVariable("TXT_MAX_SIZE", $quota_legend);
400 $f_tpl->parseCurrentBlock();
401 }
402 } else {
403 $f_tpl->setCurrentBlock("max_size");
404 $f_tpl->setVariable("TXT_MAX_SIZE", $quota_exceeded);
405 $f_tpl->parseCurrentBlock();
406 }
407 } elseif ($quota_exceeded) {
408 return $quota_exceeded;
409 }
410
411 $pending = $this->getPending();
412 if ($pending) {
413 $f_tpl->setCurrentBlock("pending");
414 $f_tpl->setVariable("TXT_PENDING", $lng->txt("file_upload_pending") .
415 ": " . htmlentities($pending));
416 $f_tpl->parseCurrentBlock();
417 }
418
419 if ($this->getDisabled() || $quota_exceeded) {
420 $f_tpl->setVariable(
421 "DISABLED",
422 " disabled=\"disabled\""
423 );
424 }
425
426 $f_tpl->setVariable("POST_VAR", $this->getPostVar());
427 $f_tpl->setVariable("ID", $this->getFieldId());
428 $f_tpl->setVariable("SIZE", $this->getSize());
429
430
431 /* experimental: bootstrap'ed file upload */
432 $f_tpl->setVariable("TXT_BROWSE", $lng->txt("select_file"));
433
434
435 return $f_tpl->get();
436 }
437
443 public function insert($a_tpl)
444 {
445 $html = $this->render();
446
447 $a_tpl->setCurrentBlock("prop_generic");
448 $a_tpl->setVariable("PROP_GENERIC", $html);
449 $a_tpl->parseCurrentBlock();
450 }
451
452
453 protected function outputSuffixes($a_tpl, $a_block = "allowed_suffixes")
454 {
456
457 if (is_array($this->getSuffixes()) && count($this->getSuffixes()) > 0) {
458 $suff_str = $delim = "";
459 foreach ($this->getSuffixes() as $suffix) {
460 $suff_str .= $delim . "." . $suffix;
461 $delim = ", ";
462 }
463 $a_tpl->setCurrentBlock($a_block);
464 $a_tpl->setVariable(
465 "TXT_ALLOWED_SUFFIXES",
466 $lng->txt("file_allowed_suffixes") . " " . $suff_str
467 );
468 $a_tpl->parseCurrentBlock();
469 }
470 }
471
472 protected function getMaxFileSizeString()
473 {
474 // get the value for the maximal uploadable filesize from the php.ini (if available)
475 $umf = ini_get("upload_max_filesize");
476 // get the value for the maximal post data from the php.ini (if available)
477 $pms = ini_get("post_max_size");
478
479 //convert from short-string representation to "real" bytes
480 $multiplier_a = array("K" => 1024, "M" => 1024 * 1024, "G" => 1024 * 1024 * 1024);
481
482 $umf_parts = preg_split("/(\d+)([K|G|M])/", $umf, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
483 $pms_parts = preg_split("/(\d+)([K|G|M])/", $pms, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
484
485 if (count($umf_parts) == 2) {
486 $umf = $umf_parts[0] * $multiplier_a[$umf_parts[1]];
487 }
488 if (count($pms_parts) == 2) {
489 $pms = $pms_parts[0] * $multiplier_a[$pms_parts[1]];
490 }
491
492 // use the smaller one as limit
493 $max_filesize = min($umf, $pms);
494
495 if (!$max_filesize) {
496 $max_filesize = max($umf, $pms);
497 }
498
499 //format for display in mega-bytes
500 $max_filesize = sprintf("%.1f MB", $max_filesize / 1024 / 1024);
501
502 return $max_filesize;
503 }
504
510 protected function getMaxFileUploads()
511 {
512 return (int) ini_get("max_file_uploads");
513 }
514
518 public function getDeletionFlag()
519 {
520 if ($_POST[$this->getPostVar() . "_delete"]) {
521 return true;
522 }
523 return false;
524 }
525
529 public function getToolbarHTML()
530 {
531 $html = $this->render("toolbar");
532 return $html;
533 }
534}
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
return true
Flag indicating whether or not HTTP headers will be sent when outputting captcha image/audio.
This class represents a file property in a property form.
getForbiddenSuffixes()
Get Accepted Suffixes.
setValueByArray($a_values)
Set value by array.
enableFileNameSelection($a_post_var)
If enabled, users get the possibility to enter a filename for the uploaded file.
setValue($a_value)
Set Value.
outputSuffixes($a_tpl, $a_block="allowed_suffixes")
setFilename($a_val)
Set filename value (if filename selection is enabled)
setPending($a_val)
Set pending filename value.
__construct($a_title="", $a_postvar="")
Constructor.
getSuffixes()
Get Accepted Suffixes.
getDeletionFlag()
Get deletion flag.
setForbiddenSuffixes($a_suffixes)
Set forbidden Suffixes.
setSuffixes($a_suffixes)
Set Accepted Suffixes.
isFileNameSelectionEnabled()
Check if filename selection is enabled.
setSize($a_size)
Set Size.
getFileNamePostVar()
Get file name post var.
insert($a_tpl)
Insert property html.
render($a_mode="")
Render html.
getMaxFileUploads()
Get number of maximum file uploads as declared in php.ini.
getToolbarHTML()
Get HTML for toolbar.
setALlowDeletion($a_val)
Set allow deletion.
getPending()
Get pending filename.
getALlowDeletion()
Get allow deletion.
checkInput()
Check input, strip slashes etc.
setType($a_type)
Set Type.
getPostVar()
Get Post Variable.
setAlert($a_alert)
Set Alert Text.
getFieldId()
Get Post Variable.
setHiddenTitle($a_val)
Set hidden title (for screenreaders)
static normalizeUtf8String($a_str)
Normalize UTF8 string.
This class represents a property that may include a sub form.
special template class to simplify handling of ITX/PEAR
static virusHandling($a_file, $a_orig_name="", $a_clean=true)
scan file for viruses and clean files if possible
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
global $DIC
Definition: goto.php:24
Interface for property form input GUI classes that can be used in ilToolbarGUI.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc