ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 
4 include_once 'Services/UIComponent/Toolbar/interfaces/interface.ilToolbarItem.php';
5 include_once("./Services/Form/classes/class.ilSubEnabledFormPropertyGUI.php");
6 
15 {
16  private $filename;
17  private $filename_post;
18  protected $size = 40;
19  protected $pending;
20  protected $allow_deletion;
21 
22  static protected $check_wsp_quota;
23 
27  protected $forbidden_suffixes = array();
28 
35  function __construct($a_title = "", $a_postvar = "")
36  {
37  global $lng;
38 
39  parent::__construct($a_title, $a_postvar);
40  $this->setType("file");
41  $this->setHiddenTitle("(".$lng->txt("form_file_input").")");
42  }
43 
49  function setValueByArray($a_values)
50  {
51  if (!is_array($a_values[$this->getPostVar()]))
52  {
53  $this->setValue($a_values[$this->getPostVar()]);
54  }
55  $this->setFilename($a_values[$this->getFileNamePostVar()]);
56  }
57 
63  function setValue($a_value)
64  {
65  $this->value = $a_value;
66  }
67 
73  function getValue()
74  {
75  return $this->value;
76  }
77 
83  function setSize($a_size)
84  {
85  $this->size = $a_size;
86  }
87 
93  function getSize()
94  {
95  return $this->size;
96  }
97 
103  public function setFilename($a_val)
104  {
105  $this->filename = $a_val;
106  }
107 
113  function getFilename()
114  {
115  return $this->filename;
116  }
117 
118 
119 
125  function setSuffixes($a_suffixes)
126  {
127  $this->suffixes = $a_suffixes;
128  }
129 
135  function getSuffixes()
136  {
137  return $this->suffixes;
138  }
139 
145  function setForbiddenSuffixes($a_suffixes)
146  {
147  $this->forbidden_suffixes = $a_suffixes;
148  }
149 
156  {
158  }
159 
165  public function setPending($a_val)
166  {
167  $this->pending = $a_val;
168  }
169 
175  function getPending()
176  {
177  return $this->pending;
178  }
179 
187  public function enableFileNameSelection($a_post_var)
188  {
189  $this->filename_selection = true;
190  $this->filename_post = $a_post_var;
191  }
192 
199  public function isFileNameSelectionEnabled()
200  {
201  return $this->filename_selection ? true : false;
202  }
203 
211  public function getFileNamePostVar()
212  {
213  return $this->filename_post;
214  }
215 
221  function setALlowDeletion($a_val)
222  {
223  $this->allow_deletion = $a_val;
224  }
225 
231  function getALlowDeletion()
232  {
233  return $this->allow_deletion;
234  }
235 
241  function checkInput()
242  {
243  global $lng;
244 
245  // #18756
246  if($this->getDisabled())
247  {
248  return true;
249  }
250 
251  // if no information is received, something went wrong
252  // this is e.g. the case, if the post_max_size has been exceeded
253  if (!is_array($_FILES[$this->getPostVar()]))
254  {
255  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
256  return false;
257  }
258 
259 
260  $_FILES[$this->getPostVar()]["name"] = ilUtil::stripSlashes($_FILES[$this->getPostVar()]["name"]);
261 
262  include_once("./Services/Utilities/classes/class.ilStr.php");
263  $_FILES[$this->getPostVar()]["name"] = ilStr::normalizeUtf8String($_FILES[$this->getPostVar()]["name"]);
264 
265  // remove trailing '/'
266  while (substr($_FILES[$this->getPostVar()]["name"],-1) == '/')
267  {
268  $_FILES[$this->getPostVar()]["name"] = substr($_FILES[$this->getPostVar()]["name"],0,-1);
269  }
270 
271  $filename = $_FILES[$this->getPostVar()]["name"];
272  $filename_arr = pathinfo($_FILES[$this->getPostVar()]["name"]);
273  $suffix = $filename_arr["extension"];
274  $mimetype = $_FILES[$this->getPostVar()]["type"];
275  $size_bytes = $_FILES[$this->getPostVar()]["size"];
276  $temp_name = $_FILES[$this->getPostVar()]["tmp_name"];
277  $error = $_FILES[$this->getPostVar()]["error"];
278  $_POST[$this->getPostVar()] = $_FILES[$this->getPostVar()];
279 
280  // error handling
281  if ($error > 0)
282  {
283  switch ($error)
284  {
285  case UPLOAD_ERR_INI_SIZE:
286  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
287  return false;
288  break;
289 
290  case UPLOAD_ERR_FORM_SIZE:
291  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
292  return false;
293  break;
294 
295  case UPLOAD_ERR_PARTIAL:
296  $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
297  return false;
298  break;
299 
300  case UPLOAD_ERR_NO_FILE:
301  if ($this->getRequired())
302  {
303  if (!strlen($this->getValue()))
304  {
305  $this->setAlert($lng->txt("form_msg_file_no_upload"));
306  return false;
307  }
308  }
309  break;
310 
311  case UPLOAD_ERR_NO_TMP_DIR:
312  $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
313  return false;
314  break;
315 
316  case UPLOAD_ERR_CANT_WRITE:
317  $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
318  return false;
319  break;
320 
321  case UPLOAD_ERR_EXTENSION:
322  $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
323  return false;
324  break;
325  }
326  }
327 
328  // check suffixes
329  if ($_FILES[$this->getPostVar()]["tmp_name"] != "")
330  {
331  if (is_array($this->forbidden_suffixes) && in_array(strtolower($suffix), $this->forbidden_suffixes))
332  {
333  $this->setAlert($lng->txt("form_msg_file_type_is_not_allowed")." (".$suffix.")");
334  return false;
335  }
336  if (is_array($this->getSuffixes()) && count($this->getSuffixes()) > 0)
337  {
338  if (!in_array(strtolower($suffix), $this->getSuffixes()))
339  {
340  $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
341  return false;
342  }
343  }
344  }
345 
346  // virus handling
347  if ($_FILES[$this->getPostVar()]["tmp_name"] != "")
348  {
349  $vir = ilUtil::virusHandling($temp_name, $filename);
350  if ($vir[0] == false)
351  {
352  $this->setAlert($lng->txt("form_msg_file_virus_found")."<br />".$vir[1]);
353  return false;
354  }
355  }
356 
357  return true;
358  }
359 
363  function render($a_mode = "")
364  {
365  global $lng;
366 
367  $quota_exceeded = $quota_legend = false;
368  if(self::$check_wsp_quota)
369  {
370  include_once "Services/DiskQuota/classes/class.ilDiskQuotaHandler.php";
372  {
373  $lng->loadLanguageModule("file");
374  $quota_exceeded = $lng->txt("personal_workspace_quota_exceeded_warning");
375  }
376  else
377  {
378  $quota_legend = ilDiskQuotaHandler::getStatusLegend();
379  }
380  }
381 
382  $f_tpl = new ilTemplate("tpl.prop_file.html", true, true, "Services/Form");
383 
384 
385  // show filename selection if enabled
386  if($this->isFileNameSelectionEnabled())
387  {
388  $f_tpl->setCurrentBlock('filename');
389  $f_tpl->setVariable('POST_FILENAME',$this->getFileNamePostVar());
390  $f_tpl->setVariable('VAL_FILENAME',$this->getFilename());
391  $f_tpl->setVariable('FILENAME_ID',$this->getFieldId());
392  $f_tpl->setVAriable('TXT_FILENAME_HINT',$lng->txt('if_no_title_then_filename'));
393  $f_tpl->parseCurrentBlock();
394  }
395  else
396  {
397  if (trim($this->getValue() != ""))
398  {
399  if (!$this->getDisabled() && $this->getALlowDeletion())
400  {
401  $f_tpl->setCurrentBlock("delete_bl");
402  $f_tpl->setVariable("POST_VAR_D", $this->getPostVar());
403  $f_tpl->setVariable("TXT_DELETE_EXISTING",
404  $lng->txt("delete_existing_file"));
405  $f_tpl->parseCurrentBlock();
406  }
407 
408  $f_tpl->setCurrentBlock('prop_file_propval');
409  $f_tpl->setVariable('FILE_VAL', $this->getValue());
410  $f_tpl->parseCurrentBlock();
411  }
412  }
413 
414  if ($a_mode != "toolbar")
415  {
416  if(!$quota_exceeded)
417  {
418  $this->outputSuffixes($f_tpl);
419 
420  $f_tpl->setCurrentBlock("max_size");
421  $f_tpl->setVariable("TXT_MAX_SIZE", $lng->txt("file_notice")." ".
422  $this->getMaxFileSizeString());
423  $f_tpl->parseCurrentBlock();
424 
425  if($quota_legend)
426  {
427  $f_tpl->setVariable("TXT_MAX_SIZE", $quota_legend);
428  $f_tpl->parseCurrentBlock();
429  }
430  }
431  else
432  {
433  $f_tpl->setCurrentBlock("max_size");
434  $f_tpl->setVariable("TXT_MAX_SIZE", $quota_exceeded);
435  $f_tpl->parseCurrentBlock();
436  }
437  }
438  else if($quota_exceeded)
439  {
440  return $quota_exceeded;
441  }
442 
443  $pending = $this->getPending();
444  if($pending)
445  {
446  $f_tpl->setCurrentBlock("pending");
447  $f_tpl->setVariable("TXT_PENDING", $lng->txt("file_upload_pending").
448  ": ".$pending);
449  $f_tpl->parseCurrentBlock();
450  }
451 
452  if ($this->getDisabled() || $quota_exceeded)
453  {
454  $f_tpl->setVariable("DISABLED",
455  " disabled=\"disabled\"");
456  }
457 
458  $f_tpl->setVariable("POST_VAR", $this->getPostVar());
459  $f_tpl->setVariable("ID", $this->getFieldId());
460  $f_tpl->setVariable("SIZE", $this->getSize());
461 
462  return $f_tpl->get();
463  }
464 
470  function insert(&$a_tpl)
471  {
472  $html = $this->render();
473 
474  $a_tpl->setCurrentBlock("prop_generic");
475  $a_tpl->setVariable("PROP_GENERIC", $html);
476  $a_tpl->parseCurrentBlock();
477  }
478 
479 
480  protected function outputSuffixes($a_tpl, $a_block = "allowed_suffixes")
481  {
482  global $lng;
483 
484  if (is_array($this->getSuffixes()) && count($this->getSuffixes()) > 0)
485  {
486  $suff_str = $delim = "";
487  foreach($this->getSuffixes() as $suffix)
488  {
489  $suff_str.= $delim.".".$suffix;
490  $delim = ", ";
491  }
492  $a_tpl->setCurrentBlock($a_block);
493  $a_tpl->setVariable("TXT_ALLOWED_SUFFIXES",
494  $lng->txt("file_allowed_suffixes")." ".$suff_str);
495  $a_tpl->parseCurrentBlock();
496  }
497  }
498 
499  protected function getMaxFileSizeString()
500  {
501  // get the value for the maximal uploadable filesize from the php.ini (if available)
502  $umf = ini_get("upload_max_filesize");
503  // get the value for the maximal post data from the php.ini (if available)
504  $pms = ini_get("post_max_size");
505 
506  //convert from short-string representation to "real" bytes
507  $multiplier_a=array("K"=>1024, "M"=>1024*1024, "G"=>1024*1024*1024);
508 
509  $umf_parts=preg_split("/(\d+)([K|G|M])/", $umf, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
510  $pms_parts=preg_split("/(\d+)([K|G|M])/", $pms, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
511 
512  if (count($umf_parts) == 2) { $umf = $umf_parts[0]*$multiplier_a[$umf_parts[1]]; }
513  if (count($pms_parts) == 2) { $pms = $pms_parts[0]*$multiplier_a[$pms_parts[1]]; }
514 
515  // use the smaller one as limit
516  $max_filesize = min($umf, $pms);
517 
518  if (!$max_filesize) $max_filesize=max($umf, $pms);
519 
520  //format for display in mega-bytes
521  $max_filesize = sprintf("%.1f MB",$max_filesize/1024/1024);
522 
523  return $max_filesize;
524  }
525 
529  function getDeletionFlag()
530  {
531  if ($_POST[$this->getPostVar()."_delete"])
532  {
533  return true;
534  }
535  return false;
536  }
537 
541  function getToolbarHTML()
542  {
543  $html = $this->render("toolbar");
544  return $html;
545  }
546 
548  {
549  if((bool)$a_value)
550  {
551  include_once "Services/WebDAV/classes/class.ilDiskQuotaActivationChecker.php";
553  {
554  self::$check_wsp_quota = true;
555  return;
556  }
557  }
558  self::$check_wsp_quota = false;
559  }
560 }
getSuffixes()
Get Accepted Suffixes.
__construct($a_title="", $a_postvar="")
Constructor.
$_POST['username']
Definition: cron.php:12
static virusHandling($a_file, $a_orig_name="", $a_clean=true)
scan file for viruses and clean files if possible
render($a_mode="")
Render html.
setALlowDeletion($a_val)
Set allow deletion.
getPostVar()
Get Post Variable.
getFileNamePostVar()
Get file name post var.
This class represents a file property in a property form.
getToolbarHTML()
Get HTML for toolbar.
insert(&$a_tpl)
Insert property html.
Interface for property form input GUI classes that can be used in ilToolbarGUI.
getFilename()
Get Value.
getPending()
Get pending filename.
setSize($a_size)
Set Size.
getALlowDeletion()
Get allow deletion.
getForbiddenSuffixes()
Get Accepted Suffixes.
setAlert($a_alert)
Set Alert Text.
getDeletionFlag()
Get deletion flag.
setType($a_type)
Set Type.
isFileNameSelectionEnabled()
Check if filename selection is enabled.
setValue($a_value)
Set Value.
normalizeUtf8String($a_str)
Normalize UTF8 string.
checkInput()
Check input, strip slashes etc.
getFieldId()
Get Post Variable.
setFilename($a_val)
Set filename value (if filename selection is enabled)
special template class to simplify handling of ITX/PEAR
setPending($a_val)
Set pending filename value.
static isUploadPossible($a_additional_size=null)
enableFileNameSelection($a_post_var)
If enabled, users get the possibility to enter a filename for the uploaded file.
setForbiddenSuffixes($a_suffixes)
Set forbidden Suffixes.
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
setHiddenTitle($a_val)
Set hidden title (for screenreaders)
outputSuffixes($a_tpl, $a_block="allowed_suffixes")
setValueByArray($a_values)
Set value by array.
This class represents a property that may include a sub form.
global $lng
Definition: privfeed.php:40
setPersonalWorkspaceQuotaCheck($a_value)
setSuffixes($a_suffixes)
Set Accepted Suffixes.
$html
Definition: example_001.php:87