ILIAS  Release_4_1_x_branch Revision 61804
 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  $_FILES[$this->getPostVar()]["name"] = ilUtil::stripSlashes($_FILES[$this->getPostVar()]["name"]);
177 
178  // remove trailing '/'
179  while (substr($_FILES[$this->getPostVar()]["name"],-1) == '/')
180  {
181  $_FILES[$this->getPostVar()]["name"] = substr($_FILES[$this->getPostVar()]["name"],0,-1);
182  }
183 
184  $filename = $_FILES[$this->getPostVar()]["name"];
185  $filename_arr = pathinfo($_FILES[$this->getPostVar()]["name"]);
186  $suffix = $filename_arr["extension"];
187  $mimetype = $_FILES[$this->getPostVar()]["type"];
188  $size_bytes = $_FILES[$this->getPostVar()]["size"];
189  $temp_name = $_FILES[$this->getPostVar()]["tmp_name"];
190  $error = $_FILES[$this->getPostVar()]["error"];
191  $_POST[$this->getPostVar()] = $_FILES[$this->getPostVar()];
192 
193  // if no information is received, something went wrong
194  // this is e.g. the case, if the post_max_size has been exceeded
195  if (!is_array($_FILES[$this->getPostVar()]))
196  {
197  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
198  return false;
199  }
200 
201  // error handling
202  if ($error > 0)
203  {
204  switch ($error)
205  {
206  case UPLOAD_ERR_INI_SIZE:
207  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
208  return false;
209  break;
210 
211  case UPLOAD_ERR_FORM_SIZE:
212  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
213  return false;
214  break;
215 
216  case UPLOAD_ERR_PARTIAL:
217  $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
218  return false;
219  break;
220 
221  case UPLOAD_ERR_NO_FILE:
222  if ($this->getRequired())
223  {
224  if (!strlen($this->getValue()))
225  {
226  $this->setAlert($lng->txt("form_msg_file_no_upload"));
227  return false;
228  }
229  }
230  break;
231 
232  case UPLOAD_ERR_NO_TMP_DIR:
233  $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
234  return false;
235  break;
236 
237  case UPLOAD_ERR_CANT_WRITE:
238  $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
239  return false;
240  break;
241 
242  case UPLOAD_ERR_EXTENSION:
243  $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
244  return false;
245  break;
246  }
247  }
248 
249  // check suffixes
250  if ($_FILES[$this->getPostVar()]["tmp_name"] != "" &&
251  is_array($this->getSuffixes()))
252  {
253  if (!in_array(strtolower($suffix), $this->getSuffixes()))
254  {
255  $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
256  return false;
257  }
258  }
259 
260  // virus handling
261  if ($_FILES[$this->getPostVar()]["tmp_name"] != "")
262  {
263  $vir = ilUtil::virusHandling($temp_name, $filename);
264  if ($vir[0] == false)
265  {
266  $this->setAlert($lng->txt("form_msg_file_virus_found")."<br />".$vir[1]);
267  return false;
268  }
269  }
270 
271  return true;
272  }
273 
277  function render($a_mode = "")
278  {
279  global $lng;
280 
281  $f_tpl = new ilTemplate("tpl.prop_file.html", true, true, "Services/Form");
282 
283 
284  // show filename selection if enabled
285  if($this->isFileNameSelectionEnabled())
286  {
287  $f_tpl->setCurrentBlock('filename');
288  $f_tpl->setVariable('POST_FILENAME',$this->getFileNamePostVar());
289  $f_tpl->setVariable('VAL_FILENAME',$this->getFilename());
290  $f_tpl->setVariable('FILENAME_ID',$this->getFieldId());
291  $f_tpl->setVAriable('TXT_FILENAME_HINT',$lng->txt('if_no_title_then_filename'));
292  $f_tpl->parseCurrentBlock();
293  }
294  else
295  {
296  if (trim($this->getValue() != ""))
297  {
298  $f_tpl->setCurrentBlock('prop_file_propval');
299  $f_tpl->setVariable('FILE_VAL', $this->getValue());
300  $f_tpl->parseCurrentBlock();
301  }
302  }
303 
304  if ($a_mode != "toolbar")
305  {
306  $this->outputSuffixes($f_tpl);
307 
308  $f_tpl->setCurrentBlock("max_size");
309  $f_tpl->setVariable("TXT_MAX_SIZE", $lng->txt("file_notice")." ".
310  $this->getMaxFileSizeString());
311  $f_tpl->parseCurrentBlock();
312  }
313 
314  $f_tpl->setVariable("POST_VAR", $this->getPostVar());
315  $f_tpl->setVariable("ID", $this->getFieldId());
316  $f_tpl->setVariable("SIZE", $this->getSize());
317 
318  return $f_tpl->get();
319  }
320 
326  function insert(&$a_tpl)
327  {
328  $html = $this->render();
329 
330  $a_tpl->setCurrentBlock("prop_generic");
331  $a_tpl->setVariable("PROP_GENERIC", $html);
332  $a_tpl->parseCurrentBlock();
333  }
334 
335 
336  protected function outputSuffixes($a_tpl, $a_block = "allowed_suffixes")
337  {
338  global $lng;
339 
340  if (is_array($this->getSuffixes()))
341  {
342  $suff_str = $delim = "";
343  foreach($this->getSuffixes() as $suffix)
344  {
345  $suff_str.= $delim.".".$suffix;
346  $delim = ", ";
347  }
348  $a_tpl->setCurrentBlock($a_block);
349  $a_tpl->setVariable("TXT_ALLOWED_SUFFIXES",
350  $lng->txt("file_allowed_suffixes")." ".$suff_str);
351  $a_tpl->parseCurrentBlock();
352  }
353  }
354 
355  protected function getMaxFileSizeString()
356  {
357  // get the value for the maximal uploadable filesize from the php.ini (if available)
358  $umf = ini_get("upload_max_filesize");
359  // get the value for the maximal post data from the php.ini (if available)
360  $pms = ini_get("post_max_size");
361 
362  //convert from short-string representation to "real" bytes
363  $multiplier_a=array("K"=>1024, "M"=>1024*1024, "G"=>1024*1024*1024);
364 
365  $umf_parts=preg_split("/(\d+)([K|G|M])/", $umf, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
366  $pms_parts=preg_split("/(\d+)([K|G|M])/", $pms, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
367 
368  if (count($umf_parts) == 2) { $umf = $umf_parts[0]*$multiplier_a[$umf_parts[1]]; }
369  if (count($pms_parts) == 2) { $pms = $pms_parts[0]*$multiplier_a[$pms_parts[1]]; }
370 
371  // use the smaller one as limit
372  $max_filesize = min($umf, $pms);
373 
374  if (!$max_filesize) $max_filesize=max($umf, $pms);
375 
376  //format for display in mega-bytes
377  $max_filesize = sprintf("%.1f MB",$max_filesize/1024/1024);
378 
379  return $max_filesize;
380  }
381 
385  function getToolbarHTML()
386  {
387  $html = $this->render("toolbar");
388  return $html;
389  }
390 
391 }