ILIAS PHP Cross Reference Learning Management Systems

Source: /Services/Form/classes/class.ilFileInputGUI.php - 518 lines - 11552 bytes - Summary - Text - Print

   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  
   7  /**
   8  * This class represents a file property in a property form.
   9  *
  10  * @author Alex Killing <alex.killing@gmx.de> 
  11  * @version $Id$
  12  * @ingroup    ServicesForm
  13  */
  14  class ilFileInputGUI extends ilSubEnabledFormPropertyGUI implements ilToolbarItem
  15  {
  16      private $filename;
  17      private $filename_post;
  18      protected $size = 40;
  19      protected $pending;
  20      protected $allow_deletion;
  21      
  22      static protected $check_wsp_quota;
  23      
  24      /**
  25      * Constructor
  26      *
  27      * @param    string    $a_title    Title
  28      * @param    string    $a_postvar    Post Variable
  29      */
  30  	function __construct($a_title = "", $a_postvar = "")
  31      {
  32          global $lng;
  33          
  34          parent::__construct($a_title, $a_postvar);
  35          $this->setType("file");
  36          $this->setHiddenTitle("(".$lng->txt("form_file_input").")");
  37      }
  38      
  39      /**
  40      * Set value by array
  41      *
  42      * @param    array    $a_values    value array
  43      */
  44  	function setValueByArray($a_values)
  45      {
  46          if (!is_array($a_values[$this->getPostVar()]))
  47          {
  48              $this->setValue($a_values[$this->getPostVar()]);
  49          }
  50          $this->setFilename($a_values[$this->getFileNamePostVar()]);
  51      }
  52  
  53      /**
  54      * Set Value. (used for displaying file title of existing file below input field)
  55      *
  56      * @param    string    $a_value    Value
  57      */
  58  	function setValue($a_value)
  59      {
  60          $this->value = $a_value;
  61      }
  62  
  63      /**
  64      * Get Value.
  65      *
  66      * @return    string    Value
  67      */
  68  	function getValue()
  69      {
  70          return $this->value;
  71      }
  72      
  73      /**
  74      * Set Size.
  75      *
  76      * @param    int    $a_size    Size
  77      */
  78  	function setSize($a_size)
  79      {
  80          $this->size = $a_size;
  81      }
  82  
  83      /**
  84      * Get Size.
  85      *
  86      * @return    int    Size
  87      */
  88  	function getSize()
  89      {
  90          return $this->size;
  91      }
  92  
  93      /**
  94       * Set filename value (if filename selection is enabled)
  95       *  
  96       * @param string $a_val
  97       */
  98  	public function setFilename($a_val)
  99      {
 100          $this->filename = $a_val;
 101      }
 102      
 103      /**
 104      * Get Value.
 105      *
 106      * @return    string    Value
 107      */
 108  	function getFilename()
 109      {
 110          return $this->filename;
 111      }
 112      
 113      
 114  
 115      /**
 116      * Set Accepted Suffixes.
 117      *
 118      * @param    array    $a_suffixes    Accepted Suffixes
 119      */
 120  	function setSuffixes($a_suffixes)
 121      {
 122          $this->suffixes = $a_suffixes;
 123      }
 124  
 125      /**
 126      * Get Accepted Suffixes.
 127      *
 128      * @return    array    Accepted Suffixes
 129      */
 130  	function getSuffixes()
 131      {
 132          return $this->suffixes;
 133      }
 134      
 135      /**
 136       * Set pending filename value 
 137       *  
 138       * @param string $a_val
 139       */
 140  	public function setPending($a_val)
 141      {
 142          $this->pending = $a_val;
 143      }
 144      
 145      /**
 146      * Get pending filename
 147      *
 148      * @return    string    Value
 149      */
 150  	function getPending()
 151      {
 152          return $this->pending;
 153      }
 154      
 155      /**
 156       * If enabled, users get the possibility to enter a filename for the uploaded file 
 157       *
 158       * @access public
 159       * @param string post variable
 160       * 
 161       */
 162  	public function enableFileNameSelection($a_post_var)
 163      {
 164           $this->filename_selection = true;
 165           $this->filename_post = $a_post_var;
 166      }
 167      
 168      /**
 169       * Check if filename selection is enabled
 170       *
 171       * @access public
 172       * @return bool enabled/disabled 
 173       */
 174  	public function isFileNameSelectionEnabled()
 175      {
 176           return $this->filename_selection ? true : false;
 177      }
 178      
 179      /**
 180       * Get file name post var
 181       *
 182       * @access public
 183       * @param string file name post var
 184       * 
 185       */
 186  	public function getFileNamePostVar()
 187      {
 188           return $this->filename_post;
 189      }
 190      
 191      /**
 192       * Set allow deletion
 193       *
 194       * @param boolean $a_val allow deletion    
 195       */
 196  	function setALlowDeletion($a_val)
 197      {
 198          $this->allow_deletion = $a_val;
 199      }
 200      
 201      /**
 202       * Get allow deletion
 203       *
 204       * @return boolean allow deletion
 205       */
 206  	function getALlowDeletion()
 207      {
 208          return $this->allow_deletion;
 209      }
 210  
 211      /**
 212      * Check input, strip slashes etc. set alert, if input is not ok.
 213      *
 214      * @return    boolean        Input ok, true/false
 215      */    
 216  	function checkInput()
 217      {
 218          global $lng;
 219  
 220          $_FILES[$this->getPostVar()]["name"] = ilUtil::stripSlashes($_FILES[$this->getPostVar()]["name"]);
 221          
 222          // remove trailing '/'
 223          while (substr($_FILES[$this->getPostVar()]["name"],-1) == '/')
 224          {
 225              $_FILES[$this->getPostVar()]["name"] = substr($_FILES[$this->getPostVar()]["name"],0,-1);
 226          }
 227  
 228          $filename = $_FILES[$this->getPostVar()]["name"];
 229          $filename_arr = pathinfo($_FILES[$this->getPostVar()]["name"]);
 230          $suffix = $filename_arr["extension"];
 231          $mimetype = $_FILES[$this->getPostVar()]["type"];
 232          $size_bytes = $_FILES[$this->getPostVar()]["size"];
 233          $temp_name = $_FILES[$this->getPostVar()]["tmp_name"];
 234          $error = $_FILES[$this->getPostVar()]["error"];
 235          $_POST[$this->getPostVar()] = $_FILES[$this->getPostVar()];
 236          
 237          // if no information is received, something went wrong
 238          // this is e.g. the case, if the post_max_size has been exceeded
 239          if (!is_array($_FILES[$this->getPostVar()]))
 240          {
 241              $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
 242              return false;
 243          }
 244  
 245          // error handling
 246          if ($error > 0)
 247          {
 248              switch ($error)
 249              {
 250                  case UPLOAD_ERR_INI_SIZE:
 251                      $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
 252                      return false;
 253                      break;
 254                       
 255                  case UPLOAD_ERR_FORM_SIZE:
 256                      $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
 257                      return false;
 258                      break;
 259      
 260                  case UPLOAD_ERR_PARTIAL:
 261                      $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
 262                      return false;
 263                      break;
 264      
 265                  case UPLOAD_ERR_NO_FILE:
 266                      if ($this->getRequired())
 267                      {
 268                          if (!strlen($this->getValue()))
 269                          {
 270                              $this->setAlert($lng->txt("form_msg_file_no_upload"));
 271                              return false;
 272                          }
 273                      }
 274                      break;
 275       
 276                  case UPLOAD_ERR_NO_TMP_DIR:
 277                      $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
 278                      return false;
 279                      break;
 280                       
 281                  case UPLOAD_ERR_CANT_WRITE:
 282                      $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
 283                      return false;
 284                      break;
 285       
 286                  case UPLOAD_ERR_EXTENSION:
 287                      $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
 288                      return false;
 289                      break;
 290              }
 291          }
 292          
 293          // check suffixes
 294          if ($_FILES[$this->getPostVar()]["tmp_name"] != "" &&
 295              is_array($this->getSuffixes()) && count($this->getSuffixes()) > 0)
 296          {
 297              if (!in_array(strtolower($suffix), $this->getSuffixes()))
 298              {
 299                  $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
 300                  return false;
 301              }
 302          }
 303          
 304          // virus handling
 305          if ($_FILES[$this->getPostVar()]["tmp_name"] != "")
 306          {
 307              $vir = ilUtil::virusHandling($temp_name, $filename);
 308              if ($vir[0] == false)
 309              {
 310                  $this->setAlert($lng->txt("form_msg_file_virus_found")."<br />".$vir[1]);
 311                  return false;
 312              }
 313          }
 314          
 315          return true;
 316      }
 317  
 318      /**
 319      * Render html
 320      */
 321  	function render($a_mode = "")
 322      {
 323          global $lng;        
 324          
 325          $quota_exceeded = $quota_legend = false;
 326          if(self::$check_wsp_quota)
 327          {
 328              include_once  "Services/DiskQuota/classes/class.ilDiskQuotaHandler.php";
 329              if(!ilDiskQuotaHandler::isUploadPossible())
 330              {
 331                  $lng->loadLanguageModule("file");
 332                  $quota_exceeded = $lng->txt("personal_workspace_quota_exceeded_warning");            
 333              }
 334              else
 335              {                            
 336                  $quota_legend = ilDiskQuotaHandler::getStatusLegend();
 337              }
 338          }
 339                  
 340          $f_tpl = new ilTemplate("tpl.prop_file.html", true, true, "Services/Form");
 341          
 342          
 343          // show filename selection if enabled
 344          if($this->isFileNameSelectionEnabled())
 345          {
 346              $f_tpl->setCurrentBlock('filename');
 347              $f_tpl->setVariable('POST_FILENAME',$this->getFileNamePostVar());
 348              $f_tpl->setVariable('VAL_FILENAME',$this->getFilename());
 349              $f_tpl->setVariable('FILENAME_ID',$this->getFieldId());
 350              $f_tpl->setVAriable('TXT_FILENAME_HINT',$lng->txt('if_no_title_then_filename'));
 351              $f_tpl->parseCurrentBlock();
 352          }
 353          else
 354          {
 355              if (trim($this->getValue() != ""))
 356              {
 357                  if (!$this->getDisabled() && $this->getALlowDeletion())
 358                  {
 359                      $f_tpl->setCurrentBlock("delete_bl");
 360                      $f_tpl->setVariable("POST_VAR_D", $this->getPostVar());
 361                      $f_tpl->setVariable("TXT_DELETE_EXISTING",
 362                          $lng->txt("delete_existing_file"));
 363                      $f_tpl->parseCurrentBlock();
 364                  }
 365                  
 366                  $f_tpl->setCurrentBlock('prop_file_propval');
 367                  $f_tpl->setVariable('FILE_VAL', $this->getValue());
 368                  $f_tpl->parseCurrentBlock();
 369              }
 370          }
 371  
 372          if ($a_mode != "toolbar")
 373          {
 374              if(!$quota_exceeded)
 375              {
 376                  $this->outputSuffixes($f_tpl);
 377  
 378                  $f_tpl->setCurrentBlock("max_size");
 379                  $f_tpl->setVariable("TXT_MAX_SIZE", $lng->txt("file_notice")." ".
 380                      $this->getMaxFileSizeString());                                        
 381                  $f_tpl->parseCurrentBlock();
 382                  
 383                  if($quota_legend)
 384                  {
 385                      $f_tpl->setVariable("TXT_MAX_SIZE", $quota_legend);                                        
 386                      $f_tpl->parseCurrentBlock();
 387                  }
 388              }
 389              else
 390              {
 391                  $f_tpl->setCurrentBlock("max_size");
 392                  $f_tpl->setVariable("TXT_MAX_SIZE", $quota_exceeded);
 393                  $f_tpl->parseCurrentBlock();
 394              }
 395          }
 396          else if($quota_exceeded)
 397          {
 398              return $quota_exceeded;
 399          }
 400  
 401          $pending = $this->getPending();
 402          if($pending)
 403          {
 404              $f_tpl->setCurrentBlock("pending");
 405              $f_tpl->setVariable("TXT_PENDING", $lng->txt("file_upload_pending").
 406                  ": ".$pending);
 407              $f_tpl->parseCurrentBlock();
 408          }
 409          
 410          if ($this->getDisabled() || $quota_exceeded)
 411          {
 412              $f_tpl->setVariable("DISABLED",
 413                  " disabled=\"disabled\"");
 414          }
 415              
 416          $f_tpl->setVariable("POST_VAR", $this->getPostVar());
 417          $f_tpl->setVariable("ID", $this->getFieldId());
 418          $f_tpl->setVariable("SIZE", $this->getSize());
 419          
 420          return $f_tpl->get();
 421      }
 422      
 423      /**
 424      * Insert property html
 425      *
 426      * @return    int    Size
 427      */
 428  	function insert(&$a_tpl)
 429      {
 430          $html = $this->render();
 431  
 432          $a_tpl->setCurrentBlock("prop_generic");
 433          $a_tpl->setVariable("PROP_GENERIC", $html);
 434          $a_tpl->parseCurrentBlock();
 435      }
 436  
 437  
 438  	protected function outputSuffixes($a_tpl, $a_block = "allowed_suffixes")
 439      {
 440          global $lng;
 441          
 442          if (is_array($this->getSuffixes()) && count($this->getSuffixes()) > 0)
 443          {
 444              $suff_str = $delim = "";
 445              foreach($this->getSuffixes() as $suffix)
 446              {
 447                  $suff_str.= $delim.".".$suffix;
 448                  $delim = ", ";
 449              }
 450              $a_tpl->setCurrentBlock($a_block);
 451              $a_tpl->setVariable("TXT_ALLOWED_SUFFIXES",
 452                  $lng->txt("file_allowed_suffixes")." ".$suff_str);
 453              $a_tpl->parseCurrentBlock();
 454          }
 455      }
 456      
 457  	protected function getMaxFileSizeString()
 458      {
 459          // get the value for the maximal uploadable filesize from the php.ini (if available)
 460          $umf = ini_get("upload_max_filesize");
 461          // get the value for the maximal post data from the php.ini (if available)
 462          $pms = ini_get("post_max_size");
 463          
 464          //convert from short-string representation to "real" bytes
 465          $multiplier_a=array("K"=>1024, "M"=>1024*1024, "G"=>1024*1024*1024);
 466          
 467          $umf_parts=preg_split("/(\d+)([K|G|M])/", $umf, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
 468          $pms_parts=preg_split("/(\d+)([K|G|M])/", $pms, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
 469          
 470          if (count($umf_parts) == 2) { $umf = $umf_parts[0]*$multiplier_a[$umf_parts[1]]; }
 471          if (count($pms_parts) == 2) { $pms = $pms_parts[0]*$multiplier_a[$pms_parts[1]]; }
 472          
 473          // use the smaller one as limit
 474          $max_filesize = min($umf, $pms);
 475  
 476          if (!$max_filesize) $max_filesize=max($umf, $pms);
 477      
 478          //format for display in mega-bytes
 479          $max_filesize = sprintf("%.1f MB",$max_filesize/1024/1024);
 480          
 481          return $max_filesize;
 482      }
 483      
 484      /**
 485      * Get deletion flag
 486      */
 487  	function getDeletionFlag()
 488      {
 489          if ($_POST[$this->getPostVar()."_delete"])
 490          {
 491              return true;
 492          }
 493          return false;
 494      }
 495      
 496      /**
 497      * Get HTML for toolbar
 498      */
 499  	function getToolbarHTML()
 500      {
 501          $html = $this->render("toolbar");
 502          return $html;
 503      }    
 504      
 505  	function setPersonalWorkspaceQuotaCheck($a_value)
 506      {
 507          if((bool)$a_value)
 508          {
 509              include_once  "Services/WebDAV/classes/class.ilDiskQuotaActivationChecker.php";
 510              if(ilDiskQuotaActivationChecker::_isPersonalWorkspaceActive())
 511              {
 512                  self::$check_wsp_quota = true;
 513                  return;
 514              }            
 515          }
 516          self::$check_wsp_quota = false;
 517      }
 518  }

title

Description

title

Description

title

Description

title

title

Body