ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilFileInputGUI.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2007 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
32 {
33  private $filename;
34  private $filename_post;
35 
36 
43  function __construct($a_title = "", $a_postvar = "")
44  {
45  parent::__construct($a_title, $a_postvar);
46  $this->setType("file");
47  }
48 
54  function setValueByArray($a_values)
55  {
56  $this->setValue($a_values[$this->getPostVar()]);
57  $this->setFilename($a_values[$this->getFileNamePostVar()]);
58  }
59 
65  function setValue($a_value)
66  {
67  $this->value = $a_value;
68  }
69 
75  function getValue()
76  {
77  return $this->value;
78  }
79 
85  public function setFilename($a_val)
86  {
87  $this->filename = $a_val;
88  }
89 
95  function getFilename()
96  {
97  return $this->filename;
98  }
99 
100 
101 
107  function setSuffixes($a_suffixes)
108  {
109  $this->suffixes = $a_suffixes;
110  }
111 
117  function getSuffixes()
118  {
119  return $this->suffixes;
120  }
121 
129  public function enableFileNameSelection($a_post_var)
130  {
131  $this->filename_selection = true;
132  $this->filename_post = $a_post_var;
133  }
134 
141  public function isFileNameSelectionEnabled()
142  {
143  return $this->filename_selection ? true : false;
144  }
145 
153  public function getFileNamePostVar()
154  {
155  return $this->filename_post;
156  }
157 
163  function checkInput()
164  {
165  global $lng;
166 
167  // remove trailing '/'
168  while (substr($_FILES[$this->getPostVar()]["name"],-1) == '/')
169  {
170  $_FILES[$this->getPostVar()]["name"] = substr($_FILES[$this->getPostVar()]["name"],0,-1);
171  }
172 
173  $filename = $_FILES[$this->getPostVar()]["name"];
174  $filename_arr = pathinfo($_FILES[$this->getPostVar()]["name"]);
175  $suffix = $filename_arr["extension"];
176  $mimetype = $_FILES[$this->getPostVar()]["type"];
177  $size_bytes = $_FILES[$this->getPostVar()]["size"];
178  $temp_name = $_FILES[$this->getPostVar()]["tmp_name"];
179  $error = $_FILES[$this->getPostVar()]["error"];
180  $_POST[$this->getPostVar()] = $_FILES[$this->getPostVar()];
181 
182  // error handling
183  if ($error > 0)
184  {
185  switch ($error)
186  {
187  case UPLOAD_ERR_INI_SIZE:
188  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
189  return false;
190  break;
191 
192  case UPLOAD_ERR_FORM_SIZE:
193  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
194  return false;
195  break;
196 
197  case UPLOAD_ERR_PARTIAL:
198  $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
199  return false;
200  break;
201 
202  case UPLOAD_ERR_NO_FILE:
203  if ($this->getRequired())
204  {
205  if (!strlen($this->getValue()))
206  {
207  $this->setAlert($lng->txt("form_msg_file_no_upload"));
208  return false;
209  }
210  }
211  break;
212 
213  case UPLOAD_ERR_NO_TMP_DIR:
214  $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
215  return false;
216  break;
217 
218  case UPLOAD_ERR_CANT_WRITE:
219  $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
220  return false;
221  break;
222 
223  case UPLOAD_ERR_EXTENSION:
224  $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
225  return false;
226  break;
227  }
228  }
229 
230  // check suffixes
231  if ($_FILES[$this->getPostVar()]["tmp_name"] != "" &&
232  is_array($this->getSuffixes()))
233  {
234  if (!in_array(strtolower($suffix), $this->getSuffixes()))
235  {
236  $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
237  return false;
238  }
239  }
240 
241  // virus handling
242  if ($_FILES[$this->getPostVar()]["tmp_name"] != "")
243  {
244  $vir = ilUtil::virusHandling($temp_name, $filename);
245  if ($vir[0] == false)
246  {
247  $this->setAlert($lng->txt("form_msg_file_virus_found")."<br />".$vir[1]);
248  return false;
249  }
250  }
251 
252  return true;
253  }
254 
258  function insert(&$a_tpl)
259  {
260  global $lng;
261 
262  // show filename selection if enabled
263  if($this->isFileNameSelectionEnabled())
264  {
265  $a_tpl->setCurrentBlock('filename');
266  $a_tpl->setVariable('POST_FILENAME',$this->getFileNamePostVar());
267  $a_tpl->setVariable('VAL_FILENAME',$this->getFilename());
268  $a_tpl->setVariable('FILENAME_ID',$this->getFieldId());
269  $a_tpl->setVAriable('TXT_FILENAME_HINT',$lng->txt('if_no_title_then_filename'));
270  $a_tpl->parseCurrentBlock();
271  }
272 
273  $this->outputSuffixes($a_tpl);
274 
275  $a_tpl->setCurrentBlock("prop_file");
276  $a_tpl->setVariable("POST_VAR", $this->getPostVar());
277  $a_tpl->setVariable("ID", $this->getFieldId());
278  $a_tpl->setVariable("TXT_MAX_SIZE", $lng->txt("file_notice")." ".
279  $this->getMaxFileSizeString());
280  $a_tpl->parseCurrentBlock();
281  }
282 
283  protected function outputSuffixes($a_tpl, $a_block = "allowed_suffixes")
284  {
285  global $lng;
286 
287  if (is_array($this->getSuffixes()))
288  {
289  $suff_str = $delim = "";
290  foreach($this->getSuffixes() as $suffix)
291  {
292  $suff_str.= $delim.".".$suffix;
293  $delim = ", ";
294  }
295  $a_tpl->setCurrentBlock($a_block);
296  $a_tpl->setVariable("TXT_ALLOWED_SUFFIXES",
297  $lng->txt("file_allowed_suffixes")." ".$suff_str);
298  $a_tpl->parseCurrentBlock();
299  }
300  }
301 
302  protected function getMaxFileSizeString()
303  {
304  // get the value for the maximal uploadable filesize from the php.ini (if available)
305  $umf = ini_get("upload_max_filesize");
306  // get the value for the maximal post data from the php.ini (if available)
307  $pms = ini_get("post_max_size");
308 
309  //convert from short-string representation to "real" bytes
310  $multiplier_a=array("K"=>1024, "M"=>1024*1024, "G"=>1024*1024*1024);
311 
312  $umf_parts=preg_split("/(\d+)([K|G|M])/", $umf, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
313  $pms_parts=preg_split("/(\d+)([K|G|M])/", $pms, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
314 
315  if (count($umf_parts) == 2) { $umf = $umf_parts[0]*$multiplier_a[$umf_parts[1]]; }
316  if (count($pms_parts) == 2) { $pms = $pms_parts[0]*$multiplier_a[$pms_parts[1]]; }
317 
318  // use the smaller one as limit
319  $max_filesize = min($umf, $pms);
320 
321  if (!$max_filesize) $max_filesize=max($umf, $pms);
322 
323  //format for display in mega-bytes
324  $max_filesize = sprintf("%.1f MB",$max_filesize/1024/1024);
325 
326  return $max_filesize;
327  }
328 }