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