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