• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

Services/Form/classes/class.ilFileInputGUI.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2007 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00031 class ilFileInputGUI extends ilFormPropertyGUI
00032 {
00033         
00040         function __construct($a_title = "", $a_postvar = "")
00041         {
00042                 parent::__construct($a_title, $a_postvar);
00043                 $this->setType("file");
00044         }
00045         
00051         function setValueByArray($a_values)
00052         {
00053         }
00054 
00060         function setSuffixes($a_suffixes)
00061         {
00062                 $this->suffixes = $a_suffixes;
00063         }
00064 
00070         function getSuffixes()
00071         {
00072                 return $this->suffixes;
00073         }
00074         
00082         public function enableFileNameSelection($a_post_var)
00083         {
00084                 $this->filename_selection = true;
00085                 $this->filename = $a_post_var;
00086         }
00087         
00094         public function isFileNameSelectionEnabled()
00095         {
00096                 return $this->filename_selection ? true : false;
00097         }
00098         
00106         public function getFileNamePostVar()
00107         {
00108                 return $this->filename;
00109         }
00110 
00116         function checkInput()
00117         {
00118                 global $lng;
00119 
00120                 // remove trailing '/'
00121                 while (substr($_FILES[$this->getPostVar()]["name"],-1) == '/')
00122                 {
00123                         $_FILES[$this->getPostVar()]["name"] = substr($_FILES[$this->getPostVar()]["name"],0,-1);
00124                 }
00125 
00126                 $filename = $_FILES[$this->getPostVar()]["name"];
00127                 $filename_arr = pathinfo($_FILES[$this->getPostVar()]["name"]);
00128                 $suffix = $filename_arr["extension"];
00129                 $mimetype = $_FILES[$this->getPostVar()]["type"];
00130                 $size_bytes = $_FILES[$this->getPostVar()]["size"];
00131                 $temp_name = $_FILES[$this->getPostVar()]["tmp_name"];
00132                 $error = $_FILES[$this->getPostVar()]["error"];
00133 
00134                 $_POST[$this->getPostVar()] = $_FILES[$this->getPostVar()];
00135                 
00136                 // error handling
00137                 if ($error > 0)
00138                 {
00139                         switch ($error)
00140                         {
00141                                 case UPLOAD_ERR_INI_SIZE:
00142                                         $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
00143                                         return false;
00144                                         break;
00145                                          
00146                                 case UPLOAD_ERR_FORM_SIZE:
00147                                         $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
00148                                         return false;
00149                                         break;
00150         
00151                                 case UPLOAD_ERR_PARTIAL:
00152                                         $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
00153                                         return false;
00154                                         break;
00155         
00156                                 case UPLOAD_ERR_NO_FILE:
00157                                         if ($this->getRequired())
00158                                         {
00159                                                 $this->setAlert($lng->txt("form_msg_file_no_upload"));
00160                                                 return false;
00161                                         }
00162                                         break;
00163          
00164                                 case UPLOAD_ERR_NO_TMP_DIR:
00165                                         $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
00166                                         return false;
00167                                         break;
00168                                          
00169                                 case UPLOAD_ERR_CANT_WRITE:
00170                                         $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
00171                                         return false;
00172                                         break;
00173          
00174                                 case UPLOAD_ERR_EXTENSION:
00175                                         $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
00176                                         return false;
00177                                         break;
00178                         }
00179                 }
00180                 
00181                 // check suffixes
00182                 if ($_FILES[$this->getPostVar()]["tmp_name"] != "" &&
00183                         is_array($this->getSuffixes()))
00184                 {
00185                         if (!in_array(strtolower($suffix), $this->getSuffixes()))
00186                         {
00187                                 $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
00188                                 return false;
00189                         }
00190                 }
00191                 
00192                 // virus handling
00193                 if ($_FILES[$this->getPostVar()]["tmp_name"] != "")
00194                 {
00195                         $vir = ilUtil::virusHandling($temp_name, $filename);
00196                         if ($vir[0] == false)
00197                         {
00198                                 $this->setAlert($lng->txt("form_msg_file_virus_found")."<br />".$vir[1]);
00199                                 return false;
00200                         }
00201                 }
00202                 
00203                 return true;
00204         }
00205 
00209         function insert(&$a_tpl)
00210         {
00211                 global $lng;
00212                 
00213                 // show filename selection if enabled
00214                 if($this->isFileNameSelectionEnabled())
00215                 {
00216                         $a_tpl->setCurrentBlock('filename');
00217                         $a_tpl->setVariable('POST_FILENAME',$this->getFileNamePostVar());
00218                         $a_tpl->setVariable('FILENAME_ID',$this->getFieldId());
00219                         $a_tpl->setVAriable('TXT_FILENAME_HINT',$lng->txt('if_no_title_then_filename'));
00220                         $a_tpl->parseCurrentBlock();
00221                 }
00222 
00223                 $this->outputSuffixes($a_tpl);
00224                 
00225                 $a_tpl->setCurrentBlock("prop_file");
00226                 $a_tpl->setVariable("POST_VAR", $this->getPostVar());
00227                 $a_tpl->setVariable("ID", $this->getFieldId());
00228                 $a_tpl->setVariable("TXT_MAX_SIZE", $lng->txt("file_notice")." ".
00229                         $this->getMaxFileSizeString());
00230                 $a_tpl->parseCurrentBlock();
00231         }
00232 
00233         protected function outputSuffixes($a_tpl, $a_block = "allowed_suffixes")
00234         {
00235                 global $lng;
00236                 
00237                 if (is_array($this->getSuffixes()))
00238                 {
00239                         $suff_str = $delim = "";
00240                         foreach($this->getSuffixes() as $suffix)
00241                         {
00242                                 $suff_str.= $delim.".".$suffix;
00243                                 $delim = ", ";
00244                         }
00245                         $a_tpl->setCurrentBlock($a_block);
00246                         $a_tpl->setVariable("TXT_ALLOWED_SUFFIXES",
00247                                 $lng->txt("file_allowed_suffixes")." ".$suff_str);
00248                         $a_tpl->parseCurrentBlock();
00249                 }
00250         }
00251         
00252         protected function getMaxFileSizeString()
00253         {
00254                 // get the value for the maximal uploadable filesize from the php.ini (if available)
00255                 $umf = get_cfg_var("upload_max_filesize");
00256                 // get the value for the maximal post data from the php.ini (if available)
00257                 $pms = get_cfg_var("post_max_size");
00258                 
00259                 //convert from short-string representation to "real" bytes
00260                 $multiplier_a=array("K"=>1024, "M"=>1024*1024, "G"=>1024*1024*1024);
00261                 
00262                 $umf_parts=preg_split("/(\d+)([K|G|M])/", $umf, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
00263         $pms_parts=preg_split("/(\d+)([K|G|M])/", $pms, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
00264         
00265         if (count($umf_parts) == 2) { $umf = $umf_parts[0]*$multiplier_a[$umf_parts[1]]; }
00266         if (count($pms_parts) == 2) { $pms = $pms_parts[0]*$multiplier_a[$pms_parts[1]]; }
00267         
00268         // use the smaller one as limit
00269                 $max_filesize = min($umf, $pms);
00270 
00271                 if (!$max_filesize) $max_filesize=max($umf, $pms);
00272         
00273         //format for display in mega-bytes
00274                 $max_filesize = sprintf("%.1f MB",$max_filesize/1024/1024);
00275                 
00276                 return $max_filesize;
00277         }
00278 }

Generated on Fri Dec 13 2013 17:56:57 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1