ILIAS  release_4-3 Revision
 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 
28  function __construct($a_title = "", $a_postvar = "")
29  {
30  global $lng;
31 
32  parent::__construct($a_title, $a_postvar);
33  $this->setType("file");
34  $this->setHiddenTitle("(".$lng->txt("form_file_input").")");
35  }
36 
42  function setValueByArray($a_values)
43  {
44  if (!is_array($a_values[$this->getPostVar()]))
45  {
46  $this->setValue($a_values[$this->getPostVar()]);
47  }
48  $this->setFilename($a_values[$this->getFileNamePostVar()]);
49  }
50 
56  function setValue($a_value)
57  {
58  $this->value = $a_value;
59  }
60 
66  function getValue()
67  {
68  return $this->value;
69  }
70 
76  function setSize($a_size)
77  {
78  $this->size = $a_size;
79  }
80 
86  function getSize()
87  {
88  return $this->size;
89  }
90 
96  public function setFilename($a_val)
97  {
98  $this->filename = $a_val;
99  }
100 
106  function getFilename()
107  {
108  return $this->filename;
109  }
110 
111 
112 
118  function setSuffixes($a_suffixes)
119  {
120  $this->suffixes = $a_suffixes;
121  }
122 
128  function getSuffixes()
129  {
130  return $this->suffixes;
131  }
132 
138  public function setPending($a_val)
139  {
140  $this->pending = $a_val;
141  }
142 
148  function getPending()
149  {
150  return $this->pending;
151  }
152 
160  public function enableFileNameSelection($a_post_var)
161  {
162  $this->filename_selection = true;
163  $this->filename_post = $a_post_var;
164  }
165 
172  public function isFileNameSelectionEnabled()
173  {
174  return $this->filename_selection ? true : false;
175  }
176 
184  public function getFileNamePostVar()
185  {
186  return $this->filename_post;
187  }
188 
194  function setALlowDeletion($a_val)
195  {
196  $this->allow_deletion = $a_val;
197  }
198 
204  function getALlowDeletion()
205  {
206  return $this->allow_deletion;
207  }
208 
214  function checkInput()
215  {
216  global $lng;
217 
218  $_FILES[$this->getPostVar()]["name"] = ilUtil::stripSlashes($_FILES[$this->getPostVar()]["name"]);
219 
220  // remove trailing '/'
221  while (substr($_FILES[$this->getPostVar()]["name"],-1) == '/')
222  {
223  $_FILES[$this->getPostVar()]["name"] = substr($_FILES[$this->getPostVar()]["name"],0,-1);
224  }
225 
226  $filename = $_FILES[$this->getPostVar()]["name"];
227  $filename_arr = pathinfo($_FILES[$this->getPostVar()]["name"]);
228  $suffix = $filename_arr["extension"];
229  $mimetype = $_FILES[$this->getPostVar()]["type"];
230  $size_bytes = $_FILES[$this->getPostVar()]["size"];
231  $temp_name = $_FILES[$this->getPostVar()]["tmp_name"];
232  $error = $_FILES[$this->getPostVar()]["error"];
233  $_POST[$this->getPostVar()] = $_FILES[$this->getPostVar()];
234 
235  // if no information is received, something went wrong
236  // this is e.g. the case, if the post_max_size has been exceeded
237  if (!is_array($_FILES[$this->getPostVar()]))
238  {
239  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
240  return false;
241  }
242 
243  // error handling
244  if ($error > 0)
245  {
246  switch ($error)
247  {
248  case UPLOAD_ERR_INI_SIZE:
249  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
250  return false;
251  break;
252 
253  case UPLOAD_ERR_FORM_SIZE:
254  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
255  return false;
256  break;
257 
258  case UPLOAD_ERR_PARTIAL:
259  $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
260  return false;
261  break;
262 
263  case UPLOAD_ERR_NO_FILE:
264  if ($this->getRequired())
265  {
266  if (!strlen($this->getValue()))
267  {
268  $this->setAlert($lng->txt("form_msg_file_no_upload"));
269  return false;
270  }
271  }
272  break;
273 
274  case UPLOAD_ERR_NO_TMP_DIR:
275  $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
276  return false;
277  break;
278 
279  case UPLOAD_ERR_CANT_WRITE:
280  $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
281  return false;
282  break;
283 
284  case UPLOAD_ERR_EXTENSION:
285  $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
286  return false;
287  break;
288  }
289  }
290 
291  // check suffixes
292  if ($_FILES[$this->getPostVar()]["tmp_name"] != "" &&
293  is_array($this->getSuffixes()) && count($this->getSuffixes()) > 0)
294  {
295  if (!in_array(strtolower($suffix), $this->getSuffixes()))
296  {
297  $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
298  return false;
299  }
300  }
301 
302  // virus handling
303  if ($_FILES[$this->getPostVar()]["tmp_name"] != "")
304  {
305  $vir = ilUtil::virusHandling($temp_name, $filename);
306  if ($vir[0] == false)
307  {
308  $this->setAlert($lng->txt("form_msg_file_virus_found")."<br />".$vir[1]);
309  return false;
310  }
311  }
312 
313  return true;
314  }
315 
319  function render($a_mode = "")
320  {
321  global $lng;
322 
323  $f_tpl = new ilTemplate("tpl.prop_file.html", true, true, "Services/Form");
324 
325 
326  // show filename selection if enabled
327  if($this->isFileNameSelectionEnabled())
328  {
329  $f_tpl->setCurrentBlock('filename');
330  $f_tpl->setVariable('POST_FILENAME',$this->getFileNamePostVar());
331  $f_tpl->setVariable('VAL_FILENAME',$this->getFilename());
332  $f_tpl->setVariable('FILENAME_ID',$this->getFieldId());
333  $f_tpl->setVAriable('TXT_FILENAME_HINT',$lng->txt('if_no_title_then_filename'));
334  $f_tpl->parseCurrentBlock();
335  }
336  else
337  {
338  if (trim($this->getValue() != ""))
339  {
340  if (!$this->getDisabled() && $this->getALlowDeletion())
341  {
342  $f_tpl->setCurrentBlock("delete_bl");
343  $f_tpl->setVariable("POST_VAR_D", $this->getPostVar());
344  $f_tpl->setVariable("TXT_DELETE_EXISTING",
345  $lng->txt("delete_existing_file"));
346  $f_tpl->parseCurrentBlock();
347  }
348 
349  $f_tpl->setCurrentBlock('prop_file_propval');
350  $f_tpl->setVariable('FILE_VAL', $this->getValue());
351  $f_tpl->parseCurrentBlock();
352  }
353  }
354 
355  if ($a_mode != "toolbar")
356  {
357  $this->outputSuffixes($f_tpl);
358 
359  $f_tpl->setCurrentBlock("max_size");
360  $f_tpl->setVariable("TXT_MAX_SIZE", $lng->txt("file_notice")." ".
361  $this->getMaxFileSizeString());
362  $f_tpl->parseCurrentBlock();
363  }
364 
365  $pending = $this->getPending();
366  if($pending)
367  {
368  $f_tpl->setCurrentBlock("pending");
369  $f_tpl->setVariable("TXT_PENDING", $lng->txt("file_upload_pending").
370  ": ".$pending);
371  $f_tpl->parseCurrentBlock();
372  }
373 
374  $f_tpl->setVariable("POST_VAR", $this->getPostVar());
375  $f_tpl->setVariable("ID", $this->getFieldId());
376  $f_tpl->setVariable("SIZE", $this->getSize());
377 
378  return $f_tpl->get();
379  }
380 
386  function insert(&$a_tpl)
387  {
388  $html = $this->render();
389 
390  $a_tpl->setCurrentBlock("prop_generic");
391  $a_tpl->setVariable("PROP_GENERIC", $html);
392  $a_tpl->parseCurrentBlock();
393  }
394 
395 
396  protected function outputSuffixes($a_tpl, $a_block = "allowed_suffixes")
397  {
398  global $lng;
399 
400  if (is_array($this->getSuffixes()) && count($this->getSuffixes()) > 0)
401  {
402  $suff_str = $delim = "";
403  foreach($this->getSuffixes() as $suffix)
404  {
405  $suff_str.= $delim.".".$suffix;
406  $delim = ", ";
407  }
408  $a_tpl->setCurrentBlock($a_block);
409  $a_tpl->setVariable("TXT_ALLOWED_SUFFIXES",
410  $lng->txt("file_allowed_suffixes")." ".$suff_str);
411  $a_tpl->parseCurrentBlock();
412  }
413  }
414 
415  protected function getMaxFileSizeString()
416  {
417  // get the value for the maximal uploadable filesize from the php.ini (if available)
418  $umf = ini_get("upload_max_filesize");
419  // get the value for the maximal post data from the php.ini (if available)
420  $pms = ini_get("post_max_size");
421 
422  //convert from short-string representation to "real" bytes
423  $multiplier_a=array("K"=>1024, "M"=>1024*1024, "G"=>1024*1024*1024);
424 
425  $umf_parts=preg_split("/(\d+)([K|G|M])/", $umf, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
426  $pms_parts=preg_split("/(\d+)([K|G|M])/", $pms, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
427 
428  if (count($umf_parts) == 2) { $umf = $umf_parts[0]*$multiplier_a[$umf_parts[1]]; }
429  if (count($pms_parts) == 2) { $pms = $pms_parts[0]*$multiplier_a[$pms_parts[1]]; }
430 
431  // use the smaller one as limit
432  $max_filesize = min($umf, $pms);
433 
434  if (!$max_filesize) $max_filesize=max($umf, $pms);
435 
436  //format for display in mega-bytes
437  $max_filesize = sprintf("%.1f MB",$max_filesize/1024/1024);
438 
439  return $max_filesize;
440  }
441 
445  function getDeletionFlag()
446  {
447  if ($_POST[$this->getPostVar()."_delete"])
448  {
449  return true;
450  }
451  return false;
452  }
453 
457  function getToolbarHTML()
458  {
459  $html = $this->render("toolbar");
460  return $html;
461  }
462 }