ILIAS  eassessment Revision 61809
 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 
26  function __construct($a_title = "", $a_postvar = "")
27  {
28  global $lng;
29 
30  parent::__construct($a_title, $a_postvar);
31  $this->setType("file");
32  $this->setHiddenTitle("(".$lng->txt("form_file_input").")");
33  }
34 
40  function setValueByArray($a_values)
41  {
42  if (!is_array($a_values[$this->getPostVar()]))
43  {
44  $this->setValue($a_values[$this->getPostVar()]);
45  }
46  $this->setFilename($a_values[$this->getFileNamePostVar()]);
47  }
48 
54  function setValue($a_value)
55  {
56  $this->value = $a_value;
57  }
58 
64  function getValue()
65  {
66  return $this->value;
67  }
68 
74  function setSize($a_size)
75  {
76  $this->size = $a_size;
77  }
78 
84  function getSize()
85  {
86  return $this->size;
87  }
88 
94  public function setFilename($a_val)
95  {
96  $this->filename = $a_val;
97  }
98 
104  function getFilename()
105  {
106  return $this->filename;
107  }
108 
109 
110 
116  function setSuffixes($a_suffixes)
117  {
118  $this->suffixes = $a_suffixes;
119  }
120 
126  function getSuffixes()
127  {
128  return $this->suffixes;
129  }
130 
138  public function enableFileNameSelection($a_post_var)
139  {
140  $this->filename_selection = true;
141  $this->filename_post = $a_post_var;
142  }
143 
150  public function isFileNameSelectionEnabled()
151  {
152  return $this->filename_selection ? true : false;
153  }
154 
162  public function getFileNamePostVar()
163  {
164  return $this->filename_post;
165  }
166 
172  function checkInput()
173  {
174  global $lng;
175 
176  // remove trailing '/'
177  while (substr($_FILES[$this->getPostVar()]["name"],-1) == '/')
178  {
179  $_FILES[$this->getPostVar()]["name"] = substr($_FILES[$this->getPostVar()]["name"],0,-1);
180  }
181 
182  $filename = $_FILES[$this->getPostVar()]["name"];
183  $filename_arr = pathinfo($_FILES[$this->getPostVar()]["name"]);
184  $suffix = $filename_arr["extension"];
185  $mimetype = $_FILES[$this->getPostVar()]["type"];
186  $size_bytes = $_FILES[$this->getPostVar()]["size"];
187  $temp_name = $_FILES[$this->getPostVar()]["tmp_name"];
188  $error = $_FILES[$this->getPostVar()]["error"];
189  $_POST[$this->getPostVar()] = $_FILES[$this->getPostVar()];
190 
191  // error handling
192  if ($error > 0)
193  {
194  switch ($error)
195  {
196  case UPLOAD_ERR_INI_SIZE:
197  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
198  return false;
199  break;
200 
201  case UPLOAD_ERR_FORM_SIZE:
202  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
203  return false;
204  break;
205 
206  case UPLOAD_ERR_PARTIAL:
207  $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
208  return false;
209  break;
210 
211  case UPLOAD_ERR_NO_FILE:
212  if ($this->getRequired())
213  {
214  if (!strlen($this->getValue()))
215  {
216  $this->setAlert($lng->txt("form_msg_file_no_upload"));
217  return false;
218  }
219  }
220  break;
221 
222  case UPLOAD_ERR_NO_TMP_DIR:
223  $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
224  return false;
225  break;
226 
227  case UPLOAD_ERR_CANT_WRITE:
228  $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
229  return false;
230  break;
231 
232  case UPLOAD_ERR_EXTENSION:
233  $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
234  return false;
235  break;
236  }
237  }
238 
239  // check suffixes
240  if ($_FILES[$this->getPostVar()]["tmp_name"] != "" &&
241  is_array($this->getSuffixes()))
242  {
243  if (!in_array(strtolower($suffix), $this->getSuffixes()))
244  {
245  $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
246  return false;
247  }
248  }
249 
250  // virus handling
251  if ($_FILES[$this->getPostVar()]["tmp_name"] != "")
252  {
253  $vir = ilUtil::virusHandling($temp_name, $filename);
254  if ($vir[0] == false)
255  {
256  $this->setAlert($lng->txt("form_msg_file_virus_found")."<br />".$vir[1]);
257  return false;
258  }
259  }
260 
261  return true;
262  }
263 
267  function render($a_mode = "")
268  {
269  global $lng;
270 
271  $f_tpl = new ilTemplate("tpl.prop_file.html", true, true, "Services/Form");
272 
273 
274  // show filename selection if enabled
275  if($this->isFileNameSelectionEnabled())
276  {
277  $f_tpl->setCurrentBlock('filename');
278  $f_tpl->setVariable('POST_FILENAME',$this->getFileNamePostVar());
279  $f_tpl->setVariable('VAL_FILENAME',$this->getFilename());
280  $f_tpl->setVariable('FILENAME_ID',$this->getFieldId());
281  $f_tpl->setVAriable('TXT_FILENAME_HINT',$lng->txt('if_no_title_then_filename'));
282  $f_tpl->parseCurrentBlock();
283  }
284  else
285  {
286  if (trim($this->getValue() != ""))
287  {
288  $f_tpl->setCurrentBlock('prop_file_propval');
289  $f_tpl->setVariable('FILE_VAL', $this->getValue());
290  $f_tpl->parseCurrentBlock();
291  }
292  }
293 
294  if ($a_mode != "toolbar")
295  {
296  $this->outputSuffixes($f_tpl);
297 
298  $f_tpl->setCurrentBlock("max_size");
299  $f_tpl->setVariable("TXT_MAX_SIZE", $lng->txt("file_notice")." ".
300  $this->getMaxFileSizeString());
301  $f_tpl->parseCurrentBlock();
302  }
303 
304  $f_tpl->setVariable("POST_VAR", $this->getPostVar());
305  $f_tpl->setVariable("ID", $this->getFieldId());
306  $f_tpl->setVariable("SIZE", $this->getSize());
307 
308  return $f_tpl->get();
309  }
310 
316  function insert(&$a_tpl)
317  {
318  $html = $this->render();
319 
320  $a_tpl->setCurrentBlock("prop_generic");
321  $a_tpl->setVariable("PROP_GENERIC", $html);
322  $a_tpl->parseCurrentBlock();
323  }
324 
325 
326  protected function outputSuffixes($a_tpl, $a_block = "allowed_suffixes")
327  {
328  global $lng;
329 
330  if (is_array($this->getSuffixes()))
331  {
332  $suff_str = $delim = "";
333  foreach($this->getSuffixes() as $suffix)
334  {
335  $suff_str.= $delim.".".$suffix;
336  $delim = ", ";
337  }
338  $a_tpl->setCurrentBlock($a_block);
339  $a_tpl->setVariable("TXT_ALLOWED_SUFFIXES",
340  $lng->txt("file_allowed_suffixes")." ".$suff_str);
341  $a_tpl->parseCurrentBlock();
342  }
343  }
344 
345  protected function getMaxFileSizeString()
346  {
347  // get the value for the maximal uploadable filesize from the php.ini (if available)
348  $umf = ini_get("upload_max_filesize");
349  // get the value for the maximal post data from the php.ini (if available)
350  $pms = ini_get("post_max_size");
351 
352  //convert from short-string representation to "real" bytes
353  $multiplier_a=array("K"=>1024, "M"=>1024*1024, "G"=>1024*1024*1024);
354 
355  $umf_parts=preg_split("/(\d+)([K|G|M])/", $umf, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
356  $pms_parts=preg_split("/(\d+)([K|G|M])/", $pms, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
357 
358  if (count($umf_parts) == 2) { $umf = $umf_parts[0]*$multiplier_a[$umf_parts[1]]; }
359  if (count($pms_parts) == 2) { $pms = $pms_parts[0]*$multiplier_a[$pms_parts[1]]; }
360 
361  // use the smaller one as limit
362  $max_filesize = min($umf, $pms);
363 
364  if (!$max_filesize) $max_filesize=max($umf, $pms);
365 
366  //format for display in mega-bytes
367  $max_filesize = sprintf("%.1f MB",$max_filesize/1024/1024);
368 
369  return $max_filesize;
370  }
371 
375  function getToolbarHTML()
376  {
377  $html = $this->render("toolbar");
378  return $html;
379  }
380 
381 }