ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
30  function __construct($a_title = "", $a_postvar = "")
31  {
32  global $lng;
33 
34  parent::__construct($a_title, $a_postvar);
35  $this->setType("file");
36  $this->setHiddenTitle("(".$lng->txt("form_file_input").")");
37  }
38 
44  function setValueByArray($a_values)
45  {
46  if (!is_array($a_values[$this->getPostVar()]))
47  {
48  $this->setValue($a_values[$this->getPostVar()]);
49  }
50  $this->setFilename($a_values[$this->getFileNamePostVar()]);
51  }
52 
58  function setValue($a_value)
59  {
60  $this->value = $a_value;
61  }
62 
68  function getValue()
69  {
70  return $this->value;
71  }
72 
78  function setSize($a_size)
79  {
80  $this->size = $a_size;
81  }
82 
88  function getSize()
89  {
90  return $this->size;
91  }
92 
98  public function setFilename($a_val)
99  {
100  $this->filename = $a_val;
101  }
102 
108  function getFilename()
109  {
110  return $this->filename;
111  }
112 
113 
114 
120  function setSuffixes($a_suffixes)
121  {
122  $this->suffixes = $a_suffixes;
123  }
124 
130  function getSuffixes()
131  {
132  return $this->suffixes;
133  }
134 
140  public function setPending($a_val)
141  {
142  $this->pending = $a_val;
143  }
144 
150  function getPending()
151  {
152  return $this->pending;
153  }
154 
162  public function enableFileNameSelection($a_post_var)
163  {
164  $this->filename_selection = true;
165  $this->filename_post = $a_post_var;
166  }
167 
174  public function isFileNameSelectionEnabled()
175  {
176  return $this->filename_selection ? true : false;
177  }
178 
186  public function getFileNamePostVar()
187  {
188  return $this->filename_post;
189  }
190 
196  function setALlowDeletion($a_val)
197  {
198  $this->allow_deletion = $a_val;
199  }
200 
206  function getALlowDeletion()
207  {
208  return $this->allow_deletion;
209  }
210 
216  function checkInput()
217  {
218  global $lng;
219 
220  $_FILES[$this->getPostVar()]["name"] = ilUtil::stripSlashes($_FILES[$this->getPostVar()]["name"]);
221 
222  $_FILES[$this->getPostVar()]["name"] = rtrim($_FILES[$this->getPostVar()]["name"], "/");
223 
224  $filename = $_FILES[$this->getPostVar()]["name"];
225  $filename_arr = pathinfo($_FILES[$this->getPostVar()]["name"]);
226  $suffix = $filename_arr["extension"];
227  $mimetype = $_FILES[$this->getPostVar()]["type"];
228  $size_bytes = $_FILES[$this->getPostVar()]["size"];
229  $temp_name = $_FILES[$this->getPostVar()]["tmp_name"];
230  $error = $_FILES[$this->getPostVar()]["error"];
231  $_POST[$this->getPostVar()] = $_FILES[$this->getPostVar()];
232 
233  // if no information is received, something went wrong
234  // this is e.g. the case, if the post_max_size has been exceeded
235  if (!is_array($_FILES[$this->getPostVar()]))
236  {
237  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
238  return false;
239  }
240 
241  // error handling
242  if ($error > 0)
243  {
244  switch ($error)
245  {
246  case UPLOAD_ERR_INI_SIZE:
247  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
248  return false;
249  break;
250 
251  case UPLOAD_ERR_FORM_SIZE:
252  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
253  return false;
254  break;
255 
256  case UPLOAD_ERR_PARTIAL:
257  $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
258  return false;
259  break;
260 
261  case UPLOAD_ERR_NO_FILE:
262  if ($this->getRequired())
263  {
264  if (!strlen($this->getValue()))
265  {
266  $this->setAlert($lng->txt("form_msg_file_no_upload"));
267  return false;
268  }
269  }
270  break;
271 
272  case UPLOAD_ERR_NO_TMP_DIR:
273  $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
274  return false;
275  break;
276 
277  case UPLOAD_ERR_CANT_WRITE:
278  $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
279  return false;
280  break;
281 
282  case UPLOAD_ERR_EXTENSION:
283  $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
284  return false;
285  break;
286  }
287  }
288 
289  // check suffixes
290  if ($_FILES[$this->getPostVar()]["tmp_name"] != "" &&
291  is_array($this->getSuffixes()) && count($this->getSuffixes()) > 0)
292  {
293  if (!in_array(strtolower($suffix), $this->getSuffixes()))
294  {
295  $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
296  return false;
297  }
298  }
299 
300  // virus handling
301  if ($_FILES[$this->getPostVar()]["tmp_name"] != "")
302  {
303  $vir = ilUtil::virusHandling($temp_name, $filename);
304  if ($vir[0] == false)
305  {
306  $this->setAlert($lng->txt("form_msg_file_virus_found")."<br />".$vir[1]);
307  return false;
308  }
309  }
310 
311  return true;
312  }
313 
317  function render($a_mode = "")
318  {
319  global $lng;
320 
321  $quota_exceeded = $quota_legend = false;
322  if(self::$check_wsp_quota)
323  {
324  include_once "Services/DiskQuota/classes/class.ilDiskQuotaHandler.php";
326  {
327  $lng->loadLanguageModule("file");
328  $quota_exceeded = $lng->txt("personal_workspace_quota_exceeded_warning");
329  }
330  else
331  {
332  $quota_legend = ilDiskQuotaHandler::getStatusLegend();
333  }
334  }
335 
336  $f_tpl = new ilTemplate("tpl.prop_file.html", true, true, "Services/Form");
337 
338 
339  // show filename selection if enabled
340  if($this->isFileNameSelectionEnabled())
341  {
342  $f_tpl->setCurrentBlock('filename');
343  $f_tpl->setVariable('POST_FILENAME',$this->getFileNamePostVar());
344  $f_tpl->setVariable('VAL_FILENAME',$this->getFilename());
345  $f_tpl->setVariable('FILENAME_ID',$this->getFieldId());
346  $f_tpl->setVAriable('TXT_FILENAME_HINT',$lng->txt('if_no_title_then_filename'));
347  $f_tpl->parseCurrentBlock();
348  }
349  else
350  {
351  if (trim($this->getValue() != ""))
352  {
353  if (!$this->getDisabled() && $this->getALlowDeletion())
354  {
355  $f_tpl->setCurrentBlock("delete_bl");
356  $f_tpl->setVariable("POST_VAR_D", $this->getPostVar());
357  $f_tpl->setVariable("TXT_DELETE_EXISTING",
358  $lng->txt("delete_existing_file"));
359  $f_tpl->parseCurrentBlock();
360  }
361 
362  $f_tpl->setCurrentBlock('prop_file_propval');
363  $f_tpl->setVariable('FILE_VAL', $this->getValue());
364  $f_tpl->parseCurrentBlock();
365  }
366  }
367 
368  if ($a_mode != "toolbar")
369  {
370  if(!$quota_exceeded)
371  {
372  $this->outputSuffixes($f_tpl);
373 
374  $f_tpl->setCurrentBlock("max_size");
375  $f_tpl->setVariable("TXT_MAX_SIZE", $lng->txt("file_notice")." ".
376  $this->getMaxFileSizeString());
377  $f_tpl->parseCurrentBlock();
378 
379  if($quota_legend)
380  {
381  $f_tpl->setVariable("TXT_MAX_SIZE", $quota_legend);
382  $f_tpl->parseCurrentBlock();
383  }
384  }
385  else
386  {
387  $f_tpl->setCurrentBlock("max_size");
388  $f_tpl->setVariable("TXT_MAX_SIZE", $quota_exceeded);
389  $f_tpl->parseCurrentBlock();
390  }
391  }
392  else if($quota_exceeded)
393  {
394  return $quota_exceeded;
395  }
396 
397  $pending = $this->getPending();
398  if($pending)
399  {
400  $f_tpl->setCurrentBlock("pending");
401  $f_tpl->setVariable("TXT_PENDING", $lng->txt("file_upload_pending").
402  ": ".$pending);
403  $f_tpl->parseCurrentBlock();
404  }
405 
406  if ($this->getDisabled() || $quota_exceeded)
407  {
408  $f_tpl->setVariable("DISABLED",
409  " disabled=\"disabled\"");
410  }
411 
412  $f_tpl->setVariable("POST_VAR", $this->getPostVar());
413  $f_tpl->setVariable("ID", $this->getFieldId());
414  $f_tpl->setVariable("SIZE", $this->getSize());
415 
416  return $f_tpl->get();
417  }
418 
424  function insert(&$a_tpl)
425  {
426  $html = $this->render();
427 
428  $a_tpl->setCurrentBlock("prop_generic");
429  $a_tpl->setVariable("PROP_GENERIC", $html);
430  $a_tpl->parseCurrentBlock();
431  }
432 
433 
434  protected function outputSuffixes($a_tpl, $a_block = "allowed_suffixes")
435  {
436  global $lng;
437 
438  if (is_array($this->getSuffixes()) && count($this->getSuffixes()) > 0)
439  {
440  $suff_str = $delim = "";
441  foreach($this->getSuffixes() as $suffix)
442  {
443  $suff_str.= $delim.".".$suffix;
444  $delim = ", ";
445  }
446  $a_tpl->setCurrentBlock($a_block);
447  $a_tpl->setVariable("TXT_ALLOWED_SUFFIXES",
448  $lng->txt("file_allowed_suffixes")." ".$suff_str);
449  $a_tpl->parseCurrentBlock();
450  }
451  }
452 
453  protected function getMaxFileSizeString()
454  {
455  // get the value for the maximal uploadable filesize from the php.ini (if available)
456  $umf = ini_get("upload_max_filesize");
457  // get the value for the maximal post data from the php.ini (if available)
458  $pms = ini_get("post_max_size");
459 
460  //convert from short-string representation to "real" bytes
461  $multiplier_a=array("K"=>1024, "M"=>1024*1024, "G"=>1024*1024*1024);
462 
463  $umf_parts=preg_split("/(\d+)([K|G|M])/", $umf, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
464  $pms_parts=preg_split("/(\d+)([K|G|M])/", $pms, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
465 
466  if (count($umf_parts) == 2) { $umf = $umf_parts[0]*$multiplier_a[$umf_parts[1]]; }
467  if (count($pms_parts) == 2) { $pms = $pms_parts[0]*$multiplier_a[$pms_parts[1]]; }
468 
469  // use the smaller one as limit
470  $max_filesize = min($umf, $pms);
471 
472  if (!$max_filesize) $max_filesize=max($umf, $pms);
473 
474  //format for display in mega-bytes
475  $max_filesize = sprintf("%.1f MB",$max_filesize/1024/1024);
476 
477  return $max_filesize;
478  }
479 
483  function getDeletionFlag()
484  {
485  if ($_POST[$this->getPostVar()."_delete"])
486  {
487  return true;
488  }
489  return false;
490  }
491 
495  function getToolbarHTML()
496  {
497  $html = $this->render("toolbar");
498  return $html;
499  }
500 
502  {
503  if((bool)$a_value)
504  {
505  include_once "Services/WebDAV/classes/class.ilDiskQuotaActivationChecker.php";
507  {
508  self::$check_wsp_quota = true;
509  return;
510  }
511  }
512  self::$check_wsp_quota = false;
513  }
514 }