[ Index ]

PHP Cross Reference of phpBB 3.0 Beta 3

title

Body

[close]

/includes/ -> functions_upload.php (source)

   1  <?php
   2  /** 
   3  *
   4  * @package phpBB3
   5  * @version $Id: functions_upload.php,v 1.22 2006/10/07 17:40:06 acydburn Exp $ 
   6  * @copyright (c) 2005 phpBB Group 
   7  * @license http://opensource.org/licenses/gpl-license.php GNU Public License 
   8  *
   9  */
  10  
  11  /**
  12  * Responsible for holding all file relevant informations, as well as doing file-specific operations.
  13  * The {@link fileupload fileupload class} can be used to upload several files, each of them being this object to operate further on.
  14  * @package phpBB3
  15  */
  16  class filespec
  17  {
  18      var $filename = '';
  19      var $realname = '';
  20      var $uploadname = '';
  21      var $mimetype = '';
  22      var $extension = '';
  23      var $filesize = 0;
  24      var $width = 0;
  25      var $height = 0;
  26      var $image_info = array();
  27  
  28      var $destination_file = '';
  29      var $destination_path = '';
  30  
  31      var $file_moved = false;
  32      var $init_error = false;
  33      var $local = false;
  34  
  35      var $error = array();
  36  
  37      var $upload = '';
  38  
  39      /**
  40      * File Class
  41      * @access private
  42      */
  43  	function filespec($upload_ary, $upload_namespace)
  44      {
  45          if (!isset($upload_ary))
  46          {
  47              $this->init_error = true;
  48              return;
  49          }
  50  
  51          $this->filename = $upload_ary['tmp_name'];
  52          $this->filesize = $upload_ary['size'];
  53          $this->realname = $this->uploadname = trim(htmlspecialchars(basename($upload_ary['name'])));
  54          $this->mimetype = $upload_ary['type'];
  55  
  56          // Opera adds the name to the mime type
  57          $this->mimetype    = (strpos($this->mimetype, '; name') !== false) ? str_replace(strstr($this->mimetype, '; name'), '', $this->mimetype) : $this->mimetype;
  58  
  59          if (!$this->mimetype)
  60          {
  61              $this->mimetype = 'application/octetstream';
  62          }
  63  
  64          $this->extension = strtolower($this->get_extension($this->realname));
  65  
  66          // Try to get real filesize from temporary folder (not always working) ;)
  67          $this->filesize = (@filesize($this->filename)) ? @filesize($this->filename) : $this->filesize;
  68  
  69          $this->width = $this->height = 0;
  70          $this->file_moved = false;
  71  
  72          $this->local = (isset($upload_ary['local_mode'])) ? true : false;
  73          $this->upload = $upload_namespace;
  74      }
  75  
  76      /**
  77      * Cleans destination filename
  78      * 
  79      * @param real|unique $mode real creates a realname, filtering some characters, lowering every character. Unique creates an unique filename
  80      * @param string $prefix Prefix applied to filename
  81      * @access public
  82      */
  83  	function clean_filename($mode = 'unique', $prefix = '')
  84      {
  85          if ($this->init_error)
  86          {
  87              return;
  88          }
  89  
  90          switch ($mode)
  91          {
  92              case 'real':
  93                  // Remove every extension from filename (to not let the mime bug being exposed)
  94                  if (strpos($this->realname, '.') !== false)
  95                  {
  96                      $this->realname = substr($this->realname, 0, strpos($this->realname, '.'));
  97                  }
  98  
  99                  // Replace any chars which may cause us problems with _
 100                  $bad_chars = array("'", "\\", ' ', '/', ':', '*', '?', '"', '<', '>', '|');
 101  
 102                  $this->realname = rawurlencode(str_replace($bad_chars, '_', strtolower($this->realname)));
 103                  $this->realname = preg_replace("/%(\w{2})/", '_', $this->realname);
 104  
 105                  $this->realname = $prefix . $this->realname . '.' . $this->extension;
 106              break;
 107  
 108              case 'unique':
 109              default:
 110                  $this->realname = $prefix . md5(unique_id()) . '.' . $this->extension;
 111              break;
 112          }
 113      }
 114  
 115      /**
 116      * Get property from file object
 117      */
 118  	function get($property)
 119      {
 120          if ($this->init_error || !isset($this->$property))
 121          {
 122              return false;
 123          }
 124  
 125          return $this->$property;
 126      }
 127  
 128      /**
 129      * Check if file is an image (mimetype)
 130      *
 131      * @return true if it is an image, false if not
 132      */
 133  	function is_image()
 134      {
 135          return (strpos($this->mimetype, 'image/') !== false) ? true : false;
 136      }
 137  
 138      /**
 139      * Check if the file got correctly uploaded
 140      *
 141      * @return true if it is a valid upload, false if not
 142      */
 143  	function is_uploaded()
 144      {
 145          if (!$this->local && !is_uploaded_file($this->filename))
 146          {
 147              return false;
 148          }
 149  
 150          if ($this->local && !file_exists($this->filename))
 151          {
 152              return false;
 153          }
 154  
 155          return true;
 156      }
 157  
 158      /**
 159      * Remove file
 160      */
 161  	function remove()
 162      {
 163          if ($this->file_moved)
 164          {
 165              @unlink($this->destination_file);
 166          }
 167      }
 168  
 169      /**
 170      * Get file extension
 171      */
 172  	function get_extension($filename)
 173      {
 174          if (strpos($filename, '.') === false)
 175          {
 176              return '';
 177          }
 178  
 179          $filename = explode('.', $filename);
 180          return array_pop($filename);
 181      }
 182  
 183      /**
 184      * Get mimetype. Utilize mime_content_type if the function exist.
 185      */
 186  	function get_mimetype($filename)
 187      {
 188          $mimetype = '';
 189  
 190          if (function_exists('mime_content_type'))
 191          {
 192              $mimetype = mime_content_type($filename);
 193          }
 194  
 195          // Some browsers choke on a mimetype of application/octet-stream
 196          if (!$mimetype || $mimetype == 'application/octet-stream')
 197          {
 198              $mimetype = 'application/octetstream';
 199          }
 200  
 201          return $mimetype;
 202      }
 203  
 204      /**
 205      * Get filesize
 206      */
 207  	function get_filesize($filename)
 208      {
 209          return @filesize($filename);
 210      }
 211  
 212      /**
 213      * Move file to destination folder
 214      * The phpbb_root_path variable will be applied to the destination path
 215      *
 216      * @param string $destination_path Destination path, for example $config['avatar_path']
 217      * @param octal $chmod Permission mask for chmodding the file after a successful move
 218      * @access public
 219      */
 220  	function move_file($destination, $chmod = 0666)
 221      {
 222          global $user, $phpbb_root_path;
 223  
 224          if (sizeof($this->error))
 225          {
 226              return false;
 227          }
 228  
 229          // We need to trust the admin in specifying valid upload directories and an attacker not being able to overwrite it...
 230          $this->destination_path = $phpbb_root_path . $destination;
 231  
 232          // Check if the destination path exist...
 233          if (!file_exists($this->destination_path))
 234          {
 235              @unlink($this->filename);
 236              return false;
 237          }
 238  
 239          $upload_mode = (@ini_get('open_basedir') || @ini_get('safe_mode')) ? 'move' : 'copy';
 240          $upload_mode = ($this->local) ? 'local' : $upload_mode;
 241          $this->destination_file = $this->destination_path . '/' . basename($this->realname);
 242  
 243          // Check if the file already exist, else there is something wrong...
 244          if (file_exists($this->destination_file))
 245          {
 246              @unlink($this->filename);
 247              return false;
 248          }
 249  
 250          switch ($upload_mode)
 251          {
 252              case 'copy':
 253  
 254                  if (!@copy($this->filename, $this->destination_file)) 
 255                  {
 256                      if (!@move_uploaded_file($this->filename, $this->destination_file)) 
 257                      {
 258                          $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file);
 259                          return false;
 260                      }
 261                  }
 262                  else
 263                  {
 264                      @unlink($this->filename);
 265                  }
 266  
 267              break;
 268  
 269              case 'move':
 270  
 271                  if (!@move_uploaded_file($this->filename, $this->destination_file)) 
 272                  {
 273                      if (!@copy($this->filename, $this->destination_file)) 
 274                      {
 275                          $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file);
 276                          return false;
 277                      }
 278                      else
 279                      {
 280                          @unlink($this->filename);
 281                      }
 282                  }
 283  
 284              break;
 285  
 286              case 'local':
 287  
 288                  if (!@copy($this->filename, $this->destination_file)) 
 289                  {
 290                      $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file);
 291                      return false;
 292                  }
 293                  @unlink($this->filename);
 294  
 295              break;
 296          }
 297  
 298          @chmod($this->destination_file, $chmod);
 299  
 300          // Try to get real filesize from destination folder
 301          $this->filesize = (@filesize($this->destination_file)) ? @filesize($this->destination_file) : $this->filesize;
 302  
 303          if ($this->is_image())
 304          {
 305              $this->width = $this->height = 0;
 306  
 307              if (($this->image_info = @getimagesize($this->destination_file)) !== false)
 308              {
 309                  $this->width = $this->image_info[0];
 310                  $this->height = $this->image_info[1];
 311  
 312                  if (!empty($this->image_info['mime']))
 313                  {
 314                      $this->mimetype = $this->image_info['mime'];
 315                  }
 316  
 317                  // Check image type
 318                  $types = $this->upload->image_types();
 319  
 320                  if (!isset($types[$this->image_info[2]]) || !in_array($this->extension, $types[$this->image_info[2]]))
 321                  {
 322                      if (!isset($types[$this->image_info[2]]))
 323                      {
 324                          $this->error[] = sprintf($user->lang['IMAGE_FILETYPE_INVALID'], $this->image_info[2], $this->mimetype);
 325                      }
 326                      else
 327                      {
 328                          $this->error[] = sprintf($user->lang['IMAGE_FILETYPE_MISMATCH'], $types[$this->image_info[2]][0], $this->extension);
 329                      }
 330                  }
 331              }
 332              else
 333              {
 334                  $this->error[] = $user->lang['UNABLE_GET_IMAGE_SIZE'];
 335              }
 336          }
 337  
 338          $this->file_moved = true;
 339          $this->additional_checks();
 340          unset($this->upload);
 341  
 342          return true;
 343      }
 344  
 345      /**
 346      * Performing additional checks
 347      */
 348  	function additional_checks()
 349      {
 350          global $user;
 351  
 352          if (!$this->file_moved)
 353          {
 354              return false;
 355          }
 356  
 357          // Filesize is too big or it's 0 if it was larger than the maxsize in the upload form
 358          if ($this->upload->max_filesize && ($this->get('filesize') > $this->upload->max_filesize || $this->filesize == 0))
 359          {
 360              $size_lang = ($this->upload->max_filesize >= 1048576) ? $user->lang['MB'] : (($this->upload->max_filesize >= 1024) ? $user->lang['KB'] : $user->lang['BYTES'] );
 361              $max_filesize = ($this->upload->max_filesize >= 1048576) ? round($this->upload->max_filesize / 1048576 * 100) / 100 : (($this->upload->max_filesize >= 1024) ? round($this->upload->max_filesize / 1024 * 100) / 100 : $this->upload->max_filesize);
 362      
 363              $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'WRONG_FILESIZE'], $max_filesize, $size_lang);
 364  
 365              return false;
 366          }
 367  
 368          if (!$this->upload->valid_dimensions($this))
 369          {
 370              $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'WRONG_SIZE'], $this->upload->min_width, $this->upload->min_height, $this->upload->max_width, $this->upload->max_height, $this->width, $this->height);
 371  
 372              return false;
 373          }
 374  
 375          return true;
 376      }
 377  }
 378  
 379  /**
 380  * Class for assigning error messages before a real filespec class can be assigned
 381  *
 382  * @package phpBB3
 383  */
 384  class fileerror extends filespec
 385  {
 386  	function fileerror($error_msg)
 387      {
 388          $this->error[] = $error_msg;
 389      }
 390  }
 391  
 392  /**
 393  * File upload class
 394  * Init class (all parameters optional and able to be set/overwritten seperatly) - scope is global and valid for all uploads
 395  *
 396  * @package phpBB3
 397  */
 398  class fileupload
 399  {
 400      var $allowed_extensions = array();
 401      var $max_filesize = 0;
 402      var $min_width = 0;
 403      var $min_height = 0;
 404      var $max_width = 0;
 405      var $max_height = 0;
 406      var $error_prefix = '';
 407  
 408      /**
 409      * Init file upload class.
 410      *
 411      * @param string $error_prefix Used error messages will get prefixed by this string
 412      * @param array $allowed_extensions Array of allowed extensions, for example array('jpg', 'jpeg', 'gif', 'png')
 413      * @param int $max_filesize Maximum filesize
 414      * @param int $min_width Minimum image width (only checked for images)
 415      * @param int $min_height Minimum image height (only checked for images)
 416      * @param int $max_width Maximum image width (only checked for images)
 417      * @param int $max_height Maximum image height (only checked for images)
 418      *
 419      */
 420  	function fileupload($error_prefix = '', $allowed_extensions = false, $max_filesize = false, $min_width = false, $min_height = false, $max_width = false, $max_height = false)
 421      {
 422          $this->set_allowed_extensions($allowed_extensions);
 423          $this->set_max_filesize($max_filesize);
 424          $this->set_allowed_dimensions($min_width, $min_height, $max_width, $max_height);
 425          $this->set_error_prefix($error_prefix);
 426      }
 427  
 428      /**
 429      * Reset vars
 430      */
 431  	function reset_vars()
 432      {
 433          $this->max_filesize = 0;
 434          $this->min_width = $this->min_height = $this->max_width = $this->max_height = 0;
 435          $this->error_prefix = '';
 436          $this->allowed_extensions = array();
 437      }
 438  
 439      /**
 440      * Set allowed extensions
 441      */
 442  	function set_allowed_extensions($allowed_extensions)
 443      {
 444          if ($allowed_extensions !== false && is_array($allowed_extensions))
 445          {
 446              $this->allowed_extensions = $allowed_extensions;
 447          }
 448      }
 449  
 450      /**
 451      * Set allowed dimensions
 452      */
 453  	function set_allowed_dimensions($min_width, $min_height, $max_width, $max_height)
 454      {
 455          $this->min_width = (int) $min_width;
 456          $this->min_height = (int) $min_height;
 457          $this->max_width = (int) $max_width;
 458          $this->max_height = (int) $max_height;
 459      }
 460  
 461      /**
 462      * Set maximum allowed filesize
 463      */
 464  	function set_max_filesize($max_filesize)
 465      {
 466          if ($max_filesize !== false && (int) $max_filesize)
 467          {
 468              $this->max_filesize = (int) $max_filesize;
 469          }
 470      }
 471  
 472      /**
 473      * Set error prefix
 474      */
 475  	function set_error_prefix($error_prefix)
 476      {
 477          $this->error_prefix = $error_prefix;
 478      }
 479  
 480      /**
 481      * Form upload method
 482      * Upload file from users harddisk
 483      *
 484      * @param string $form_name Form name assigned to the file input field (if it is an array, the key has to be specified)
 485      * @return object $file Object "filespec" is returned, all further operations can be done with this object
 486      * @access public
 487      */
 488  	function form_upload($form_name)
 489      {
 490          global $user;
 491  
 492          unset($_FILES[$form_name]['local_mode']);
 493          $file = new filespec($_FILES[$form_name], $this);
 494  
 495          if ($file->init_error)
 496          {
 497              $file->error[] = '';
 498              return $file;
 499          }
 500  
 501          // Error array filled?
 502          if (isset($_FILES[$form_name]['error']))
 503          {
 504              $error = $this->assign_internal_error($_FILES[$form_name]['error']);
 505  
 506              if ($error !== false)
 507              {
 508                  $file->error[] = $error;
 509                  return $file;
 510              }
 511          }
 512  
 513          // Check if empty file got uploaded (not catched by is_uploaded_file)
 514          if (isset($_FILES[$form_name]['size']) && $_FILES[$form_name]['size'] == 0)
 515          {
 516              $file->error[] = $user->lang[$this->error_prefix . 'EMPTY_FILEUPLOAD'];
 517              return $file;
 518          }
 519  
 520          // PHP Upload filesize exceeded
 521          if ($file->get('filename') == 'none')
 522          {
 523              $file->error[] = (@ini_get('upload_max_filesize') == '') ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], @ini_get('upload_max_filesize'));
 524              return $file;
 525          }
 526  
 527          // Not correctly uploaded
 528          if (!$file->is_uploaded())
 529          {
 530              $file->error[] = $user->lang[$this->error_prefix . 'NOT_UPLOADED'];
 531              return $file;
 532          }
 533  
 534          $this->common_checks($file);
 535  
 536          return $file;
 537      }
 538  
 539      /**
 540      * Move file from another location to phpBB
 541      */
 542  	function local_upload($source_file, $filedata = false)
 543      {
 544          global $user;
 545  
 546          $form_name = 'local';
 547  
 548          $_FILES[$form_name]['local_mode'] = true;
 549          $_FILES[$form_name]['tmp_name'] = $source_file;
 550  
 551          if ($filedata === false)
 552          {
 553              $_FILES[$form_name]['name'] = basename($source_file);
 554              $_FILES[$form_name]['size'] = 0;
 555              $_FILES[$form_name]['type'] = '';
 556          }
 557          else
 558          {
 559              $_FILES[$form_name]['name'] = $filedata['realname'];
 560              $_FILES[$form_name]['size'] = $filedata['size'];
 561              $_FILES[$form_name]['type'] = $filedata['type'];
 562          }
 563  
 564          $file = new filespec($_FILES[$form_name], $this);
 565  
 566          if ($file->init_error)
 567          {
 568              $file->error[] = '';
 569              return $file;
 570          }
 571  
 572          if (isset($_FILES[$form_name]['error']))
 573          {
 574              $error = $this->assign_internal_error($_FILES[$form_name]['error']);
 575  
 576              if ($error !== false)
 577              {
 578                  $file->error[] = $error;
 579                  return $file;
 580              }
 581          }
 582  
 583          // PHP Upload filesize exceeded
 584          if ($file->get('filename') == 'none')
 585          {
 586              $file->error[] = (@ini_get('upload_max_filesize') == '') ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], @ini_get('upload_max_filesize'));
 587              return $file;
 588          }
 589  
 590          // Not correctly uploaded
 591          if (!$file->is_uploaded())
 592          {
 593              $file->error[] = $user->lang[$this->error_prefix . 'NOT_UPLOADED'];
 594              return $file;
 595          }
 596  
 597          $this->common_checks($file);
 598  
 599          return $file;
 600      }
 601  
 602      /**
 603      * Remote upload method
 604      * Uploads file from given url
 605      *
 606      * @param string $upload_url URL pointing to file to upload, for example http://www.foobar.com/example.gif
 607      * @return object $file Object "filespec" is returned, all further operations can be done with this object
 608      * @access public
 609      */
 610  	function remote_upload($upload_url)
 611      {
 612          global $user, $phpbb_root_path;
 613  
 614          $upload_ary = array();
 615          $upload_ary['local_mode'] = true;
 616  
 617          if (!preg_match('#^(https?://).*?\.(' . implode('|', $this->allowed_extensions) . ')$#i', $upload_url, $match))
 618          {
 619              $file = new fileerror($user->lang[$this->error_prefix . 'URL_INVALID']);
 620              return $file;
 621          }
 622   
 623          if (empty($match[2]))
 624          {
 625              $file = new fileerror($user->lang[$this->error_prefix . 'URL_INVALID']);
 626              return $file;
 627          }
 628  
 629          $url = parse_url($upload_url);
 630  
 631          $host = $url['host'];
 632          $path = $url['path'];
 633          $port = (!empty($url['port'])) ? (int) $url['port'] : 80;
 634  
 635          $upload_ary['type'] = 'application/octet-stream';
 636  
 637          $url['path'] = explode('.', $url['path']);
 638          $ext = array_pop($url['path']);
 639  
 640          $url['path'] = implode('', $url['path']);
 641          $upload_ary['name'] = basename($url['path']) . (($ext) ? '.' . $ext : '');
 642          $filename = $url['path'];
 643          $filesize = 0;
 644  
 645          if (!($fsock = @fsockopen($host, $port, $errno, $errstr)))
 646          {
 647              $file = new fileerror($user->lang[$this->error_prefix . 'NOT_UPLOADED']);
 648              return $file;
 649          }
 650  
 651          fputs($fsock, 'GET /' . $path . " HTTP/1.1\r\n");
 652          fputs($fsock, "HOST: " . $host . "\r\n");
 653          fputs($fsock, "Connection: close\r\n\r\n");
 654  
 655          $get_info = false;
 656          $data = '';
 657          while (!@feof($fsock))
 658          {
 659              if ($get_info)
 660              {
 661                  $data .= @fread($fsock, 1024);
 662              }
 663              else
 664              {
 665                  $line = @fgets($fsock, 1024);
 666  
 667                  if ($line == "\r\n")
 668                  {
 669                      $get_info = true;
 670                  }
 671                  else
 672                  {
 673                      if (strpos($line, 'Content-Type: ') !== false)
 674                      {
 675                          $upload_ary['type'] = rtrim(str_replace('Content-Type: ', '', $line));
 676                      }
 677                      else if (strpos($line, '404 Not Found') !== false)
 678                      {
 679                          $file = new fileerror($user->lang[$this->error_prefix . 'URL_NOT_FOUND']);
 680                          return $file;
 681                      }
 682                  }
 683              }
 684          }
 685          @fclose($fsock);
 686  
 687          if (empty($data))
 688          {
 689              $file = new fileerror($user->lang[$this->error_prefix . 'EMPTY_REMOTE_DATA']);
 690              return $file;
 691          }
 692  
 693          $tmp_path = (!@ini_get('safe_mode')) ? false : $phpbb_root_path . 'cache';
 694          $filename = tempnam($tmp_path, unique_id() . '-');
 695  
 696          if (!($fp = @fopen($filename, 'wb')))
 697          {
 698              $file = new fileerror($user->lang[$this->error_prefix . 'NOT_UPLOADED']);
 699              return $file;
 700          }
 701  
 702          $upload_ary['size'] = fwrite($fp, $data);
 703          fclose($fp);
 704          unset($data);
 705  
 706          $upload_ary['tmp_name'] = $filename;
 707  
 708          $file = new filespec($upload_ary, $this);
 709          $this->common_checks($file);
 710  
 711          return $file;
 712      }
 713  
 714      /**
 715      * Assign internal error
 716      * @access private
 717      */
 718  	function assign_internal_error($errorcode)
 719      {
 720          global $user;
 721  
 722          switch ($errorcode)
 723          {
 724              case 1:
 725                  $error = (@ini_get('upload_max_filesize') == '') ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], @ini_get('upload_max_filesize'));
 726              break;
 727  
 728              case 2:
 729                  $size_lang = ($this->max_filesize >= 1048576) ? $user->lang['MB'] : (($this->max_filesize >= 1024) ? $user->lang['KB'] : $user->lang['BYTES'] );
 730                  $max_filesize = ($this->max_filesize >= 1048576) ? round($this->max_filesize / 1048576 * 100) / 100 : (($this->max_filesize >= 1024) ? round($this->max_filesize / 1024 * 100) / 100 : $this->max_filesize);
 731  
 732                  $error = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize, $size_lang);
 733              break;
 734  
 735              case 3:
 736                  $error = $user->lang[$this->error_prefix . 'PARTIAL_UPLOAD'];
 737              break;
 738  
 739              case 4:
 740                  $error = $user->lang[$this->error_prefix . 'NOT_UPLOADED'];
 741              break;
 742  
 743              case 6:
 744                  $error = 'Temporary folder could not be found. Please check your PHP installation.';
 745              break;
 746  
 747              default:
 748                  $error = false;
 749              break;
 750          }
 751  
 752          return $error;
 753      }
 754  
 755      /**
 756      * Perform common checks
 757      */
 758  	function common_checks(&$file)
 759      {
 760          global $user;
 761  
 762          // Filesize is too big or it's 0 if it was larger than the maxsize in the upload form
 763          if ($this->max_filesize && ($file->get('filesize') > $this->max_filesize || $file->get('filesize') == 0))
 764          {
 765              $size_lang = ($this->max_filesize >= 1048576) ? $user->lang['MB'] : (($this->max_filesize >= 1024) ? $user->lang['KB'] : $user->lang['BYTES'] );
 766              $max_filesize = ($this->max_filesize >= 1048576) ? round($this->max_filesize / 1048576 * 100) / 100 : (($this->max_filesize >= 1024) ? round($this->max_filesize / 1024 * 100) / 100 : $this->max_filesize);
 767  
 768              $file->error[] = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize, $size_lang);
 769          }
 770  
 771          // check Filename
 772          if (preg_match("#[\\/:*?\"<>|]#i", $file->get('realname')))
 773          { 
 774              $file->error[] = sprintf($user->lang[$this->error_prefix . 'INVALID_FILENAME'], $file->get('realname'));
 775          }
 776  
 777          // Invalid Extension
 778          if (!$this->valid_extension($file))
 779          {
 780              $file->error[] = sprintf($user->lang[$this->error_prefix . 'DISALLOWED_EXTENSION'], $file->get('extension'));
 781          }
 782      }
 783  
 784      /**
 785      * Check for allowed extension
 786      */
 787  	function valid_extension(&$file)
 788      {
 789          return (in_array($file->get('extension'), $this->allowed_extensions)) ? true : false;
 790      }
 791  
 792      /**
 793      * Check for allowed dimension
 794      */
 795  	function valid_dimensions(&$file)
 796      {
 797          if (!$this->max_width && !$this->max_height && !$this->min_width && !$this->min_height)
 798          {
 799              return true;
 800          }
 801  
 802          if (($file->get('width') > $this->max_width && $this->max_width) || 
 803              ($file->get('height') > $this->max_height && $this->max_height) || 
 804              ($file->get('width') < $this->min_width && $this->min_width) ||
 805              ($file->get('height') < $this->min_height && $this->min_height))
 806          {
 807              return false;
 808          }
 809  
 810          return true;
 811      }
 812  
 813      /**
 814      * Check if form upload is valid
 815      */
 816  	function is_valid($form_name)
 817      {
 818          return (isset($_FILES[$form_name]) && $_FILES[$form_name]['name'] != 'none') ? true : false;
 819      }
 820  
 821      /**
 822      * Return image type/extension mapping 
 823      */
 824  	function image_types()
 825      {
 826          return array(
 827              1 => array('gif'),
 828              2 => array('jpg', 'jpeg'),
 829              3 => array('png'),
 830              4 => array('swf'),
 831              5 => array('psd'),
 832              6 => array('bmp'),
 833              7 => array('tif', 'tiff'),
 834              8 => array('tif', 'tiff'),
 835              9 => array('jpg', 'jpeg'),
 836              10 => array('jpg', 'jpeg'),
 837              11 => array('jpg', 'jpeg'),
 838              12 => array('jpg', 'jpeg'),
 839              13 => array('swc'),
 840              14 => array('iff'),
 841              15 => array('wbmp'),
 842              16 => array('xbm'),
 843          );
 844      }
 845  }
 846  
 847  ?>


Generated: Wed Nov 22 00:35:05 2006 Cross-referenced by PHPXref 0.6