Dokeos PHP Cross Reference Learning Management Systems

Source: /main/inc/lib/fckeditor/editor/plugins/ajaxfilemanager/inc/class.upload.php - 591 lines - 14669 bytes - Summary - Text - Print

   1  <?php
   2      /**
   3       * This class provide all file upload functionalities
   4       * @author Logan Cai (cailongqun [at] yahoo [dot] com [dot] cn)
   5       * @link www.phpletter.com
   6       * @since 22/April/2007
   7       *
   8       */
   9       require_once api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php';//from Dokeos
  10  class Upload
  11  {
  12      var $fileType = ""; //the file type
  13      var $originalFileName = "";
  14      var $fileName = ""; //the file final name
  15      var $fileExtension = "";
  16      var $img_x = 0;
  17      var $img_y = 0;
  18      var $img_new_x = 0;
  19      var $img_new_y = 0;
  20      var $imgHandler = null;
  21      var $fileBaseName = ""; //file name without the file extension and .
  22      var $filePath = ""; //the file path which the file uploaded to
  23      var $fileSize = 0;
  24      var $validImageExts = array("gif", "jpg", "png");
  25      var $errors = array();
  26      var $_value  = null;  //an array holding the uploaded file details
  27      var $dirPath = "";
  28      var $invalidFileExt = array(); //var $invalidFileExt = array('php,inc,asp,aspx');
  29      var $errCode = "";
  30      var $safeMode;
  31      var $uploadFileMode = 0755;
  32      var $errorCodes = array(
  33          0=>'the file uploaded with success',
  34          1=>'The uploaded file exceeds the upload_max_filesize directive in php.ini',
  35          2=>'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
  36          3=>'The uploaded file was only partially uploaded',
  37          4=>'No file was uploaded.',
  38          6=>'Missing a temporary folder',
  39          7=>'Failed to write file to disk',
  40          8=>'File upload stopped by extension',
  41          999=>'No error code avaiable',
  42      );
  43  
  44  
  45  	function Upload()
  46      {
  47          //doing nothing
  48      }
  49  
  50  	function isFileUploaded($indexInPost="file")
  51      {
  52  
  53          $this->errCode = isset($_FILES[$indexInPost]['error'])?$_FILES[$indexInPost]['error']:999;
  54          if((isset($_FILES[$indexInPost]['error']) && $_FILES[$indexInPost] == 0) ||
  55          (!empty($_FILES[$indexInPost]['tmp_name']) && $_FILES[$indexInPost]['tmp_name'] != 'none')
  56          )
  57          {
  58              $this->_value = $_FILES[$indexInPost];
  59              $this->fileSize = @filesize($this->_value['tmp_name']);
  60              $this->originalFileName = $this->_value['name'];
  61              $this->fileType = $this->_value['type'];
  62  
  63              return true;
  64          }else
  65          {
  66  
  67              array_push($this->errors, 'Unable to upload file');
  68              return false;
  69          }
  70      }
  71  
  72  	function getErrorCodeMsg()
  73      {
  74          return (isset($this->errorCodes[$this->errCode])?$this->errorCodes[$this->errCode]:"");
  75      }
  76      /**
  77       * check if the uploaded file extension is allowed against the validFile Extension
  78       * or against the invalid extension list when the list of valid file extension is not set
  79       *
  80       * @param array $validFileExt
  81       * @return boolean
  82       */
  83  	function isPermittedFileExt($validFileExt = array())
  84      {
  85          $tem = array();
  86  
  87          if(sizeof($validFileExt))
  88          {
  89              foreach($validFileExt as $k=>$v)
  90              {
  91                  $tem[$k] = strtolower(trim($v));
  92              }
  93          }
  94          $validFileExt = $tem;
  95  
  96          if(sizeof($validFileExt) && sizeof($this->invalidFileExt))
  97          {
  98              foreach($validFileExt as  $k=>$ext)
  99              {
 100                  if(array_search(strtolower($ext), $this->invalidFileExt) !== false)
 101                  {
 102                      unset($validFileExt[$k]);
 103                  }
 104              }
 105          }
 106  
 107  
 108  
 109  
 110          if(sizeof($validFileExt))
 111          {
 112              if(array_search(strtolower($this->getFileExt()), $validFileExt) !== false)
 113              {
 114                  return true;
 115              }
 116          }elseif(array_search(strtolower($this->getFileExt()), $this->invalidFileExt) === false)
 117          {
 118              return true;
 119          }
 120  
 121  
 122          $this->deleteUploadedFile();
 123          return false;
 124  
 125      }
 126      /**
 127       * check if the uploaded file size is too big
 128       *
 129       * @param integer $maxSize
 130       */
 131  	function isSizeTooBig($maxSize="")
 132      {
 133          if($this->fileSize > $maxSize)
 134          {
 135              $this->deleteUploadedFile();
 136              return true;
 137          }else
 138          {
 139              return false;
 140          }
 141      }
 142      /**
 143       * set the invali file extensions
 144       *
 145       * @param array $invalidFileExt
 146       */
 147  	function setInvalidFileExt($invalidFileExt=array())
 148      {
 149          $tem = array();
 150          if(sizeof($invalidFileExt))
 151          {
 152              foreach($invalidFileExt as $k=>$v)
 153              {
 154                  $tem[$k]= strtolower(trim($v));
 155              }
 156          }
 157  
 158          $this->invalidFileExt = $tem;
 159      }
 160      /**
 161       * get file type
 162       *
 163       * @return string
 164       */
 165  	function getFileType()
 166      {
 167          return $this->fileType;
 168      }
 169      /**
 170       * get a file extension
 171       *
 172       * @param string $fileName the path to a file or just the file name
 173       */
 174  	function getFileExt()
 175      {
 176          //return strtolower(substr(strrchr($this->fileName, "."), 1));
 177          return substr(strrchr($this->originalFileName, "."), 1);
 178      }
 179      /**
 180           * move the uploaded file to a specific location
 181           *
 182           * @param string $dest  the path to the directory which the uploaded file will be moved to
 183           * @param string $fileBaseName the base name which the uploaded file will be renamed to
 184           * @param unknown_type $overwrite
 185           * @return unknown
 186           */
 187  	function moveUploadedFile($dest, $fileBaseName = '', $overwrite=false)
 188      {
 189          //ensure the directory path ending with /
 190          if ($dest != ''  && substr($dest, -1) != '/') {
 191              $dest .= '/';
 192          }
 193          $this->dirPath = $dest;
 194          $fileName =  basename($this->_value['name']);
 195  
 196          $dotIndex = strrpos($fileName, '.');
 197          $this->fileExtension = '';
 198          if(is_int($dotIndex))
 199          {
 200              $this->fileExtension = substr($fileName, $dotIndex);
 201              $this->fileBaseName = substr($fileName, 0, $dotIndex);
 202          }
 203          if(!empty($fileBaseName))
 204          {
 205              $this->fileBaseName = $fileBaseName;
 206          }
 207          $this->fileBaseName=disable_dangerous_file(replace_dangerous_char(str_replace(' ','_',$this->fileBaseName),'strict'));// Juan Carlos RaƱa replace space by _ because fix long names. See: ajaxfilemanager/inc/class.manager.php. And add cleaning from dokeos replace_dangerous_char() and disable_dangerous_file()
 208          $fileName = $this->fileBaseName . $this->fileExtension;
 209          $filePath = $dest . $fileName;
 210  
 211          if(!$overwrite && file_exists($filePath) && is_file($filePath))
 212          {//rename
 213  
 214              $counter = 0;
 215              while(file_exists($dest.$fileName) && is_file($dest .$fileName))
 216              {
 217                  $counter++;
 218                  $fileName = $this->fileBaseName.'_'.$counter.$this->fileExtension;
 219              }
 220              $this->fileBaseName .= "_" . $counter;
 221  
 222          }
 223  
 224          if (@move_uploaded_file($this->_value['tmp_name'], $dest . $fileName)) {
 225              @chmod($dest . $fileName, $this->uploadFileMode);
 226              $this->fileName = $fileName;
 227              $this->filePath = $dest . $fileName;
 228              return true;
 229          } else {
 230              return false;
 231          }
 232      }
 233  
 234  
 235      /**
 236           * check if the uploaded is permitted to upload
 237           *
 238           * @param mixed $invalidImageExts invalid image extension
 239           * @param bool $delete force to delete the uploaded file
 240           */
 241  	function isImage($invalidImageExts = array(), $delete = true)
 242      {
 243          if(!is_array($invalidImageExts) && !empty($invalidImageExts))
 244          {
 245              $invalidImageExts = explode(",", $invalidImageExts);
 246          }
 247          foreach ($invalidImageExts as $k=>$v)
 248          {
 249              $invalidImageExts[$k] = strtolower(trim($v));
 250          }
 251          foreach ($this->validImageExts as $k=>$v)
 252          {
 253              $ValidImageExts[$k] = strtolower(trim($v));
 254          }
 255          if(sizeof($invalidImageExts))
 256          {
 257              foreach ($ValidImageExts as $k=>$v)
 258              {
 259                  if(array_search(strtolower($v), $invalidImageExts) !== false)
 260                  {
 261                      unset($ValidImageExts[$k]);
 262                  }
 263              }
 264          }
 265          if(array_search(strtolower($this->getFileExt()), $ValidImageExts)!==false)
 266          {
 267              $this->_get_image_details($this->filePath);
 268              if(!empty($this->fileType))
 269              {
 270                  return true;
 271              }
 272          }else
 273          {
 274              if($delete)
 275              {
 276                  $this->deleteUploadedFile();
 277              }
 278          }
 279  
 280          array($this->errors, "This file is not a image type file.");
 281          return false;
 282      }
 283  
 284      /**
 285       * Resize the Image in the X and/or Y direction
 286       * If either is 0 it will be scaled proportionally
 287       *
 288       * @access public
 289       *
 290       * @param mixed $new_x
 291       * @param mixed $new_y
 292       * @param string $thumb_suffix
 293       *
 294       * @return mixed none or PEAR_error
 295       */
 296  	function resize($filePath, $thumb_suffix="", $new_x = 0, $new_y = 0)
 297      {
 298  
 299          if(empty($filePath))
 300          {
 301              $filePath = $this->dirPath . $this->fileBaseName . $thumb_suffix  . $this->fileExtension;
 302          }
 303          // 0 means keep original size
 304          if ($this->img_x > $this->img_y)
 305          $new_y = intval($new_y/$this->img_x*$this->img_y);
 306          else if ($this->img_y > $this->img_x)
 307          $new_x = intval($new_x/$this->img_y*$this->img_x);
 308          // Now do the library specific resizing.
 309          return $this->_resize($filePath,$new_x, $new_y);
 310      } // End resize
 311  
 312      /**
 313       * resize the image and return the thumbnail image  details array("width"=>, "height"=>, "name")
 314       *
 315       * @param string $fileName
 316       * @param int $new_x the thumbnail width
 317       * @param int $new_y the thumbnail height
 318       * @return unknown
 319       */
 320  	function _resize($fileName, $new_x, $new_y) {
 321          $functionName = 'ImageCreateFrom' . $this->fileType;
 322  
 323  
 324          if(function_exists($functionName))
 325          {
 326              $this->imgHandler = $functionName($this->filePath);
 327          }else
 328          {
 329              array_push($this->errors, $functionName . " function is unavailable");
 330              return false;
 331          }
 332  
 333          if(function_exists('ImageCreateTrueColor')){
 334              $new_img =ImageCreateTrueColor($new_x,$new_y);
 335          } else {
 336              $new_img =ImageCreate($new_x,$new_y);
 337          }
 338          if(function_exists('ImageCopyResampled')){
 339              ImageCopyResampled($new_img, $this->imgHandler, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y);
 340          } else {
 341              ImageCopyResized($new_img, $this->imgHandler, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y);
 342          }
 343          if($this->_imageSave($new_img, $fileName, 80))
 344          {
 345              return array("width"=>$new_x, "height"=>$new_y, "name"=>basename($fileName));
 346          }else
 347          {
 348  
 349              array_push($this->errors, "Unable to resize the image");
 350              return false;
 351          }
 352  
 353      }
 354      /**
 355           * save the thumbnail file and destroy the opened image
 356           *
 357           * @param resource $newImageHandler
 358           * @param string $fileName
 359           * @param int $quality
 360           * @return boolean
 361           */
 362  	function _imageSave($newImageHandler, $fileName, $quality = 90)
 363      {
 364          $functionName = 'image' . $this->fileType;
 365          if($functionName($newImageHandler, $fileName, $quality))
 366          {
 367              imagedestroy($newImageHandler);
 368              return true;
 369          }else
 370          {
 371              imagedestroy($newImageHandler);
 372              array_push($this->errors, "Unable to save the thumbnail file.");
 373              return false;
 374          }
 375  
 376      }
 377      /**
 378       *
 379       * @access public
 380       * @return void
 381       */
 382  	function _get_image_details($image)
 383      {
 384  
 385          //echo $image;
 386          $data = @GetImageSize($image);
 387          #1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order,
 388          # 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC
 389          if (is_array($data)){
 390              switch($data[2]){
 391                  case 1:
 392                      $type = 'gif';
 393                      break;
 394                  case 2:
 395                      $type = 'jpeg';
 396                      break;
 397                  case 3:
 398                      $type = 'png';
 399                      break;
 400                  case 4:
 401                      $type = 'swf';
 402                      break;
 403                  case 5:
 404                      $type = 'psd';
 405                  case 6:
 406                      $type = 'bmp';
 407                  case 7:
 408                  case 8:
 409                      $type = 'tiff';
 410                  default:
 411                      array_push($this->errors, "We do not recognize this image format");
 412  
 413              }
 414              $this->img_x = $data[0];
 415              $this->img_y = $data[1];
 416              $this->fileType = $type;
 417  
 418              return true;
 419          } else {
 420              array_push($this->errors, "Cannot fetch image or images details.");
 421              return null;
 422          }
 423      }
 424      /**
 425       * caculate the thumbnail details from the original image file
 426       *
 427       * @param string $originalImageName
 428       * @param int $originaleImageWidth
 429       * @param int $originalImageHeight
 430       * @param string $thumbnailSuffix
 431       * @param int $thumbnailWidth
 432       * @param int $thumbnailHeight
 433       * @return array array("name"=>"image name", "width"=>"image width", "height"=>"image height")
 434       */
 435  	function getThumbInfo($originalImageName, $originaleImageWidth, $originalImageHeight, $thumbnailSuffix, $thumbnailWidth, $thumbnailHeight)
 436      {
 437          $outputs = array("name"=>"", "width"=>0, "height"=>0);
 438          $thumbnailWidth    = intval($thumbnailWidth);
 439          $thumbnailHeight = intval($thumbnailHeight);
 440          if(!empty($originalImageName) && !empty($originaleImageWidth) && !empty($originalImageHeight))
 441          {
 442              $dotIndex = strrpos($originalImageName, '.');
 443              //begin to get the thumbnail image name
 444              $fileExtension = '';
 445              $fileBaseName = '';
 446              if(is_int($dotIndex))
 447              {
 448                  $fileExtension = substr($originalImageName, $dotIndex);
 449                  $fileBaseName = substr($originalImageName, 0, $dotIndex);
 450              }
 451              $outputs['name'] = $fileBaseName . $thumbnailSuffix . $fileExtension;
 452              //start to get the thumbnail width & height
 453              if($thumbnailWidth < 1 && $thumbnailHeight < 1)
 454              {
 455                  $thumbnailWidth =$originaleImageWidth;
 456                  $thumbnailHeight = $originalImageHeight;
 457              }elseif($thumbnailWidth < 1)
 458              {
 459                  $thumbnailWidth = floor($thumbnailHeight / $originalImageHeight * $originaleImageWidth);
 460  
 461              }elseif($thumbnailHeight < 1)
 462              {
 463                  $thumbnailHeight = floor($thumbnailWidth / $originaleImageWidth * $originalImageHeight);
 464              }else
 465              {
 466                  $scale = min($thumbnailWidth/$originaleImageWidth, $thumbnailHeight/$originalImageHeight);
 467                  $thumbnailWidth = floor($scale*$originaleImageWidth);
 468                  $thumbnailHeight = floor($scale*$originalImageHeight);
 469              }
 470              $outputs['width'] = $thumbnailWidth;
 471              $outputs['height'] = $thumbnailHeight;
 472          }
 473          return $outputs;
 474  
 475      }
 476  
 477  
 478      /**
 479       * get the uploaded file
 480       */
 481  	function deleteUploadedFile()
 482      {
 483          @unlink($this->filePath);
 484      }
 485      /**
 486       * destroy the tmp file
 487       *
 488       */
 489  	function finish()
 490      {
 491          @unlink($this->_value['tmp_name']);
 492      }
 493  
 494  	function displayError()
 495      {
 496          if(sizeof($this->errors))
 497          {
 498              echo "<pre>";
 499              print_r($this->errors);
 500              echo "</pre>";
 501          }
 502      }
 503      /**
 504       * get the path which the file uploaded to
 505       *
 506       */
 507  	function getFilePath()
 508      {
 509          return $this->filePath;
 510      }
 511      /**
 512       * return the directory path witch the file uploaded to
 513       *
 514       * @return unknown
 515       */
 516  	function getDirPath()
 517      {
 518          return $this->dirPath;
 519      }
 520  
 521  	function getFileBaseName()
 522      {
 523          return $this->fileBaseName;
 524      }
 525  
 526  	function getFileName()
 527      {
 528          return $this->fileName;
 529      }
 530      /**
 531       * get image width
 532       *
 533       * @return integer
 534       */
 535  	function getImageWidth()
 536      {
 537          return $this->img_x;
 538      }
 539      /**
 540       * get image height
 541       *
 542       * @return integer
 543       */
 544  	function getImageHeight()
 545      {
 546          return $this->img_y;
 547      }
 548      /**
 549       * get uploaded file size
 550       *
 551       * @return string
 552       */
 553  	function getFileSize()
 554      {
 555          return $this->fileSize;
 556      }
 557      /**
 558       * delete the uploaded image file & associated thumnails
 559       *
 560       * @param string $dirPath
 561       * @param string $originalImageName
 562       * @param string $arrayThumbnailSuffix
 563       */
 564  	function deleteFileAndThumbs($dirPath, $originalImageName, $arrayThumbnailSuffix)
 565      {
 566          //ensure the directory path ending with /
 567          if ($dirPath != ''  && substr($dirPath, -1) != '/') {
 568              $dirPath .= '/';
 569          }
 570          if(!empty($originalImageName) && file_exists($dirPath . $originalImageName) && is_file($dirPath . $originalImageName))
 571          {
 572              @unlink($dirPath . $originalImageName);
 573              foreach($arrayThumbnailSuffix as $v)
 574              {
 575                  $dotIndex = strrpos($originalImageName, '.');
 576                  //begin to get the thumbnail image name
 577                  $fileExtension = '';
 578                  $fileBaseName = '';
 579                  if(is_int($dotIndex))
 580                  {
 581                      $fileExtension = substr($originalImageName, $dotIndex);
 582                      $fileBaseName = substr($originalImageName, 0, $dotIndex);
 583                  }
 584                  @unlink($dirPath . $fileBaseName . $v . $fileExtension);
 585              }
 586          }
 587  
 588  
 589      }
 590  }
 591  ?>

title

Description

title

Description

title

Description

title

title

Body