1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/includes/functions_upload.php Wed Mar 07 16:41:10 2012 +0400
1.3 @@ -0,0 +1,1012 @@
1.4 +<?php
1.5 +/**
1.6 +*
1.7 +* @package phpBB3
1.8 +* @version $Id$
1.9 +* @copyright (c) 2005 phpBB Group
1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
1.11 +*
1.12 +*/
1.13 +
1.14 +/**
1.15 +* @ignore
1.16 +*/
1.17 +if (!defined('IN_PHPBB'))
1.18 +{
1.19 + exit;
1.20 +}
1.21 +
1.22 +/**
1.23 +* Responsible for holding all file relevant information, as well as doing file-specific operations.
1.24 +* The {@link fileupload fileupload class} can be used to upload several files, each of them being this object to operate further on.
1.25 +* @package phpBB3
1.26 +*/
1.27 +class filespec
1.28 +{
1.29 + var $filename = '';
1.30 + var $realname = '';
1.31 + var $uploadname = '';
1.32 + var $mimetype = '';
1.33 + var $extension = '';
1.34 + var $filesize = 0;
1.35 + var $width = 0;
1.36 + var $height = 0;
1.37 + var $image_info = array();
1.38 +
1.39 + var $destination_file = '';
1.40 + var $destination_path = '';
1.41 +
1.42 + var $file_moved = false;
1.43 + var $init_error = false;
1.44 + var $local = false;
1.45 +
1.46 + var $error = array();
1.47 +
1.48 + var $upload = '';
1.49 +
1.50 + /**
1.51 + * File Class
1.52 + * @access private
1.53 + */
1.54 + function filespec($upload_ary, $upload_namespace)
1.55 + {
1.56 + if (!isset($upload_ary))
1.57 + {
1.58 + $this->init_error = true;
1.59 + return;
1.60 + }
1.61 +
1.62 + $this->filename = $upload_ary['tmp_name'];
1.63 + $this->filesize = $upload_ary['size'];
1.64 + $name = (STRIP) ? stripslashes($upload_ary['name']) : $upload_ary['name'];
1.65 + $name = trim(utf8_htmlspecialchars(utf8_basename($name)));
1.66 + $this->realname = $this->uploadname = $name;
1.67 + $this->mimetype = $upload_ary['type'];
1.68 +
1.69 + // Opera adds the name to the mime type
1.70 + $this->mimetype = (strpos($this->mimetype, '; name') !== false) ? str_replace(strstr($this->mimetype, '; name'), '', $this->mimetype) : $this->mimetype;
1.71 +
1.72 + if (!$this->mimetype)
1.73 + {
1.74 + $this->mimetype = 'application/octetstream';
1.75 + }
1.76 +
1.77 + $this->extension = strtolower($this->get_extension($this->realname));
1.78 +
1.79 + // Try to get real filesize from temporary folder (not always working) ;)
1.80 + $this->filesize = (@filesize($this->filename)) ? @filesize($this->filename) : $this->filesize;
1.81 +
1.82 + $this->width = $this->height = 0;
1.83 + $this->file_moved = false;
1.84 +
1.85 + $this->local = (isset($upload_ary['local_mode'])) ? true : false;
1.86 + $this->upload = $upload_namespace;
1.87 + }
1.88 +
1.89 + /**
1.90 + * Cleans destination filename
1.91 + *
1.92 + * @param real|unique|unique_ext $mode real creates a realname, filtering some characters, lowering every character. Unique creates an unique filename
1.93 + * @param string $prefix Prefix applied to filename
1.94 + * @access public
1.95 + */
1.96 + function clean_filename($mode = 'unique', $prefix = '', $user_id = '')
1.97 + {
1.98 + if ($this->init_error)
1.99 + {
1.100 + return;
1.101 + }
1.102 +
1.103 + switch ($mode)
1.104 + {
1.105 + case 'real':
1.106 + // Remove every extension from filename (to not let the mime bug being exposed)
1.107 + if (strpos($this->realname, '.') !== false)
1.108 + {
1.109 + $this->realname = substr($this->realname, 0, strpos($this->realname, '.'));
1.110 + }
1.111 +
1.112 + // Replace any chars which may cause us problems with _
1.113 + $bad_chars = array("'", "\\", ' ', '/', ':', '*', '?', '"', '<', '>', '|');
1.114 +
1.115 + $this->realname = rawurlencode(str_replace($bad_chars, '_', strtolower($this->realname)));
1.116 + $this->realname = preg_replace("/%(\w{2})/", '_', $this->realname);
1.117 +
1.118 + $this->realname = $prefix . $this->realname . '.' . $this->extension;
1.119 + break;
1.120 +
1.121 + case 'unique':
1.122 + $this->realname = $prefix . md5(unique_id());
1.123 + break;
1.124 +
1.125 + case 'avatar':
1.126 + $this->extension = strtolower($this->extension);
1.127 + $this->realname = $prefix . $user_id . '.' . $this->extension;
1.128 +
1.129 + break;
1.130 +
1.131 + case 'unique_ext':
1.132 + default:
1.133 + $this->realname = $prefix . md5(unique_id()) . '.' . $this->extension;
1.134 + break;
1.135 + }
1.136 + }
1.137 +
1.138 + /**
1.139 + * Get property from file object
1.140 + */
1.141 + function get($property)
1.142 + {
1.143 + if ($this->init_error || !isset($this->$property))
1.144 + {
1.145 + return false;
1.146 + }
1.147 +
1.148 + return $this->$property;
1.149 + }
1.150 +
1.151 + /**
1.152 + * Check if file is an image (mimetype)
1.153 + *
1.154 + * @return true if it is an image, false if not
1.155 + */
1.156 + function is_image()
1.157 + {
1.158 + return (strpos($this->mimetype, 'image/') !== false) ? true : false;
1.159 + }
1.160 +
1.161 + /**
1.162 + * Check if the file got correctly uploaded
1.163 + *
1.164 + * @return true if it is a valid upload, false if not
1.165 + */
1.166 + function is_uploaded()
1.167 + {
1.168 + if (!$this->local && !is_uploaded_file($this->filename))
1.169 + {
1.170 + return false;
1.171 + }
1.172 +
1.173 + if ($this->local && !file_exists($this->filename))
1.174 + {
1.175 + return false;
1.176 + }
1.177 +
1.178 + return true;
1.179 + }
1.180 +
1.181 + /**
1.182 + * Remove file
1.183 + */
1.184 + function remove()
1.185 + {
1.186 + if ($this->file_moved)
1.187 + {
1.188 + @unlink($this->destination_file);
1.189 + }
1.190 + }
1.191 +
1.192 + /**
1.193 + * Get file extension
1.194 + */
1.195 + function get_extension($filename)
1.196 + {
1.197 + if (strpos($filename, '.') === false)
1.198 + {
1.199 + return '';
1.200 + }
1.201 +
1.202 + $filename = explode('.', $filename);
1.203 + return array_pop($filename);
1.204 + }
1.205 +
1.206 + /**
1.207 + * Get mimetype. Utilize mime_content_type if the function exist.
1.208 + * Not used at the moment...
1.209 + */
1.210 + function get_mimetype($filename)
1.211 + {
1.212 + $mimetype = '';
1.213 +
1.214 + if (function_exists('mime_content_type'))
1.215 + {
1.216 + $mimetype = mime_content_type($filename);
1.217 + }
1.218 +
1.219 + // Some browsers choke on a mimetype of application/octet-stream
1.220 + if (!$mimetype || $mimetype == 'application/octet-stream')
1.221 + {
1.222 + $mimetype = 'application/octetstream';
1.223 + }
1.224 +
1.225 + return $mimetype;
1.226 + }
1.227 +
1.228 + /**
1.229 + * Get filesize
1.230 + */
1.231 + function get_filesize($filename)
1.232 + {
1.233 + return @filesize($filename);
1.234 + }
1.235 +
1.236 +
1.237 + /**
1.238 + * Check the first 256 bytes for forbidden content
1.239 + */
1.240 + function check_content($disallowed_content)
1.241 + {
1.242 + if (empty($disallowed_content))
1.243 + {
1.244 + return true;
1.245 + }
1.246 +
1.247 + $fp = @fopen($this->filename, 'rb');
1.248 +
1.249 + if ($fp !== false)
1.250 + {
1.251 + $ie_mime_relevant = fread($fp, 256);
1.252 + fclose($fp);
1.253 + foreach ($disallowed_content as $forbidden)
1.254 + {
1.255 + if (stripos($ie_mime_relevant, '<' . $forbidden) !== false)
1.256 + {
1.257 + return false;
1.258 + }
1.259 + }
1.260 + }
1.261 + return true;
1.262 + }
1.263 +
1.264 + /**
1.265 + * Move file to destination folder
1.266 + * The phpbb_root_path variable will be applied to the destination path
1.267 + *
1.268 + * @param string $destination_path Destination path, for example $config['avatar_path']
1.269 + * @param bool $overwrite If set to true, an already existing file will be overwritten
1.270 + * @param string $chmod Permission mask for chmodding the file after a successful move. The mode entered here reflects the mode defined by {@link phpbb_chmod()}
1.271 + *
1.272 + * @access public
1.273 + */
1.274 + function move_file($destination, $overwrite = false, $skip_image_check = false, $chmod = false)
1.275 + {
1.276 + global $user, $phpbb_root_path;
1.277 +
1.278 + if (sizeof($this->error))
1.279 + {
1.280 + return false;
1.281 + }
1.282 +
1.283 + $chmod = ($chmod === false) ? CHMOD_READ | CHMOD_WRITE : $chmod;
1.284 +
1.285 + // We need to trust the admin in specifying valid upload directories and an attacker not being able to overwrite it...
1.286 + $this->destination_path = $phpbb_root_path . $destination;
1.287 +
1.288 + // Check if the destination path exist...
1.289 + if (!file_exists($this->destination_path))
1.290 + {
1.291 + @unlink($this->filename);
1.292 + return false;
1.293 + }
1.294 +
1.295 + $upload_mode = (@ini_get('open_basedir') || @ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'on') ? 'move' : 'copy';
1.296 + $upload_mode = ($this->local) ? 'local' : $upload_mode;
1.297 + $this->destination_file = $this->destination_path . '/' . utf8_basename($this->realname);
1.298 +
1.299 + // Check if the file already exist, else there is something wrong...
1.300 + if (file_exists($this->destination_file) && !$overwrite)
1.301 + {
1.302 + @unlink($this->filename);
1.303 + }
1.304 + else
1.305 + {
1.306 + if (file_exists($this->destination_file))
1.307 + {
1.308 + @unlink($this->destination_file);
1.309 + }
1.310 +
1.311 + switch ($upload_mode)
1.312 + {
1.313 + case 'copy':
1.314 +
1.315 + if (!@copy($this->filename, $this->destination_file))
1.316 + {
1.317 + if (!@move_uploaded_file($this->filename, $this->destination_file))
1.318 + {
1.319 + $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file);
1.320 + }
1.321 + }
1.322 +
1.323 + break;
1.324 +
1.325 + case 'move':
1.326 +
1.327 + if (!@move_uploaded_file($this->filename, $this->destination_file))
1.328 + {
1.329 + if (!@copy($this->filename, $this->destination_file))
1.330 + {
1.331 + $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file);
1.332 + }
1.333 + }
1.334 +
1.335 + break;
1.336 +
1.337 + case 'local':
1.338 +
1.339 + if (!@copy($this->filename, $this->destination_file))
1.340 + {
1.341 + $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file);
1.342 + }
1.343 +
1.344 + break;
1.345 + }
1.346 +
1.347 + // Remove temporary filename
1.348 + @unlink($this->filename);
1.349 +
1.350 + if (sizeof($this->error))
1.351 + {
1.352 + return false;
1.353 + }
1.354 +
1.355 + phpbb_chmod($this->destination_file, $chmod);
1.356 + }
1.357 +
1.358 + // Try to get real filesize from destination folder
1.359 + $this->filesize = (@filesize($this->destination_file)) ? @filesize($this->destination_file) : $this->filesize;
1.360 +
1.361 + if ($this->is_image() && !$skip_image_check)
1.362 + {
1.363 + $this->width = $this->height = 0;
1.364 +
1.365 + if (($this->image_info = @getimagesize($this->destination_file)) !== false)
1.366 + {
1.367 + $this->width = $this->image_info[0];
1.368 + $this->height = $this->image_info[1];
1.369 +
1.370 + if (!empty($this->image_info['mime']))
1.371 + {
1.372 + $this->mimetype = $this->image_info['mime'];
1.373 + }
1.374 +
1.375 + // Check image type
1.376 + $types = $this->upload->image_types();
1.377 +
1.378 + if (!isset($types[$this->image_info[2]]) || !in_array($this->extension, $types[$this->image_info[2]]))
1.379 + {
1.380 + if (!isset($types[$this->image_info[2]]))
1.381 + {
1.382 + $this->error[] = sprintf($user->lang['IMAGE_FILETYPE_INVALID'], $this->image_info[2], $this->mimetype);
1.383 + }
1.384 + else
1.385 + {
1.386 + $this->error[] = sprintf($user->lang['IMAGE_FILETYPE_MISMATCH'], $types[$this->image_info[2]][0], $this->extension);
1.387 + }
1.388 + }
1.389 +
1.390 + // Make sure the dimensions match a valid image
1.391 + if (empty($this->width) || empty($this->height))
1.392 + {
1.393 + $this->error[] = $user->lang['ATTACHED_IMAGE_NOT_IMAGE'];
1.394 + }
1.395 + }
1.396 + else
1.397 + {
1.398 + $this->error[] = $user->lang['UNABLE_GET_IMAGE_SIZE'];
1.399 + }
1.400 + }
1.401 +
1.402 + $this->file_moved = true;
1.403 + $this->additional_checks();
1.404 + unset($this->upload);
1.405 +
1.406 + return true;
1.407 + }
1.408 +
1.409 + /**
1.410 + * Performing additional checks
1.411 + */
1.412 + function additional_checks()
1.413 + {
1.414 + global $user;
1.415 +
1.416 + if (!$this->file_moved)
1.417 + {
1.418 + return false;
1.419 + }
1.420 +
1.421 + // Filesize is too big or it's 0 if it was larger than the maxsize in the upload form
1.422 + if ($this->upload->max_filesize && ($this->get('filesize') > $this->upload->max_filesize || $this->filesize == 0))
1.423 + {
1.424 + $max_filesize = get_formatted_filesize($this->upload->max_filesize, false);
1.425 +
1.426 + $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']);
1.427 +
1.428 + return false;
1.429 + }
1.430 +
1.431 + if (!$this->upload->valid_dimensions($this))
1.432 + {
1.433 + $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);
1.434 +
1.435 + return false;
1.436 + }
1.437 +
1.438 + return true;
1.439 + }
1.440 +}
1.441 +
1.442 +/**
1.443 +* Class for assigning error messages before a real filespec class can be assigned
1.444 +*
1.445 +* @package phpBB3
1.446 +*/
1.447 +class fileerror extends filespec
1.448 +{
1.449 + function fileerror($error_msg)
1.450 + {
1.451 + $this->error[] = $error_msg;
1.452 + }
1.453 +}
1.454 +
1.455 +/**
1.456 +* File upload class
1.457 +* Init class (all parameters optional and able to be set/overwritten separately) - scope is global and valid for all uploads
1.458 +*
1.459 +* @package phpBB3
1.460 +*/
1.461 +class fileupload
1.462 +{
1.463 + var $allowed_extensions = array();
1.464 + var $disallowed_content = array('body', 'head', 'html', 'img', 'plaintext', 'a href', 'pre', 'script', 'table', 'title');
1.465 + var $max_filesize = 0;
1.466 + var $min_width = 0;
1.467 + var $min_height = 0;
1.468 + var $max_width = 0;
1.469 + var $max_height = 0;
1.470 + var $error_prefix = '';
1.471 +
1.472 + /**
1.473 + * Init file upload class.
1.474 + *
1.475 + * @param string $error_prefix Used error messages will get prefixed by this string
1.476 + * @param array $allowed_extensions Array of allowed extensions, for example array('jpg', 'jpeg', 'gif', 'png')
1.477 + * @param int $max_filesize Maximum filesize
1.478 + * @param int $min_width Minimum image width (only checked for images)
1.479 + * @param int $min_height Minimum image height (only checked for images)
1.480 + * @param int $max_width Maximum image width (only checked for images)
1.481 + * @param int $max_height Maximum image height (only checked for images)
1.482 + *
1.483 + */
1.484 + function fileupload($error_prefix = '', $allowed_extensions = false, $max_filesize = false, $min_width = false, $min_height = false, $max_width = false, $max_height = false, $disallowed_content = false)
1.485 + {
1.486 + $this->set_allowed_extensions($allowed_extensions);
1.487 + $this->set_max_filesize($max_filesize);
1.488 + $this->set_allowed_dimensions($min_width, $min_height, $max_width, $max_height);
1.489 + $this->set_error_prefix($error_prefix);
1.490 + $this->set_disallowed_content($disallowed_content);
1.491 + }
1.492 +
1.493 + /**
1.494 + * Reset vars
1.495 + */
1.496 + function reset_vars()
1.497 + {
1.498 + $this->max_filesize = 0;
1.499 + $this->min_width = $this->min_height = $this->max_width = $this->max_height = 0;
1.500 + $this->error_prefix = '';
1.501 + $this->allowed_extensions = array();
1.502 + $this->disallowed_content = array();
1.503 + }
1.504 +
1.505 + /**
1.506 + * Set allowed extensions
1.507 + */
1.508 + function set_allowed_extensions($allowed_extensions)
1.509 + {
1.510 + if ($allowed_extensions !== false && is_array($allowed_extensions))
1.511 + {
1.512 + $this->allowed_extensions = $allowed_extensions;
1.513 + }
1.514 + }
1.515 +
1.516 + /**
1.517 + * Set allowed dimensions
1.518 + */
1.519 + function set_allowed_dimensions($min_width, $min_height, $max_width, $max_height)
1.520 + {
1.521 + $this->min_width = (int) $min_width;
1.522 + $this->min_height = (int) $min_height;
1.523 + $this->max_width = (int) $max_width;
1.524 + $this->max_height = (int) $max_height;
1.525 + }
1.526 +
1.527 + /**
1.528 + * Set maximum allowed filesize
1.529 + */
1.530 + function set_max_filesize($max_filesize)
1.531 + {
1.532 + if ($max_filesize !== false && (int) $max_filesize)
1.533 + {
1.534 + $this->max_filesize = (int) $max_filesize;
1.535 + }
1.536 + }
1.537 +
1.538 + /**
1.539 + * Set disallowed strings
1.540 + */
1.541 + function set_disallowed_content($disallowed_content)
1.542 + {
1.543 + if ($disallowed_content !== false && is_array($disallowed_content))
1.544 + {
1.545 + $this->disallowed_content = array_diff($disallowed_content, array(''));
1.546 + }
1.547 + }
1.548 +
1.549 + /**
1.550 + * Set error prefix
1.551 + */
1.552 + function set_error_prefix($error_prefix)
1.553 + {
1.554 + $this->error_prefix = $error_prefix;
1.555 + }
1.556 +
1.557 + /**
1.558 + * Form upload method
1.559 + * Upload file from users harddisk
1.560 + *
1.561 + * @param string $form_name Form name assigned to the file input field (if it is an array, the key has to be specified)
1.562 + * @return object $file Object "filespec" is returned, all further operations can be done with this object
1.563 + * @access public
1.564 + */
1.565 + function form_upload($form_name)
1.566 + {
1.567 + global $user;
1.568 +
1.569 + unset($_FILES[$form_name]['local_mode']);
1.570 + $file = new filespec($_FILES[$form_name], $this);
1.571 +
1.572 + if ($file->init_error)
1.573 + {
1.574 + $file->error[] = '';
1.575 + return $file;
1.576 + }
1.577 +
1.578 + // Error array filled?
1.579 + if (isset($_FILES[$form_name]['error']))
1.580 + {
1.581 + $error = $this->assign_internal_error($_FILES[$form_name]['error']);
1.582 +
1.583 + if ($error !== false)
1.584 + {
1.585 + $file->error[] = $error;
1.586 + return $file;
1.587 + }
1.588 + }
1.589 +
1.590 + // Check if empty file got uploaded (not catched by is_uploaded_file)
1.591 + if (isset($_FILES[$form_name]['size']) && $_FILES[$form_name]['size'] == 0)
1.592 + {
1.593 + $file->error[] = $user->lang[$this->error_prefix . 'EMPTY_FILEUPLOAD'];
1.594 + return $file;
1.595 + }
1.596 +
1.597 + // PHP Upload filesize exceeded
1.598 + if ($file->get('filename') == 'none')
1.599 + {
1.600 + $max_filesize = @ini_get('upload_max_filesize');
1.601 + $unit = 'MB';
1.602 +
1.603 + if (!empty($max_filesize))
1.604 + {
1.605 + $unit = strtolower(substr($max_filesize, -1, 1));
1.606 + $max_filesize = (int) $max_filesize;
1.607 +
1.608 + $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB');
1.609 + }
1.610 +
1.611 + $file->error[] = (empty($max_filesize)) ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], $max_filesize, $user->lang[$unit]);
1.612 + return $file;
1.613 + }
1.614 +
1.615 + // Not correctly uploaded
1.616 + if (!$file->is_uploaded())
1.617 + {
1.618 + $file->error[] = $user->lang[$this->error_prefix . 'NOT_UPLOADED'];
1.619 + return $file;
1.620 + }
1.621 +
1.622 + $this->common_checks($file);
1.623 +
1.624 + return $file;
1.625 + }
1.626 +
1.627 + /**
1.628 + * Move file from another location to phpBB
1.629 + */
1.630 + function local_upload($source_file, $filedata = false)
1.631 + {
1.632 + global $user;
1.633 +
1.634 + $form_name = 'local';
1.635 +
1.636 + $_FILES[$form_name]['local_mode'] = true;
1.637 + $_FILES[$form_name]['tmp_name'] = $source_file;
1.638 +
1.639 + if ($filedata === false)
1.640 + {
1.641 + $_FILES[$form_name]['name'] = utf8_basename($source_file);
1.642 + $_FILES[$form_name]['size'] = 0;
1.643 + $mimetype = '';
1.644 +
1.645 + if (function_exists('mime_content_type'))
1.646 + {
1.647 + $mimetype = mime_content_type($source_file);
1.648 + }
1.649 +
1.650 + // Some browsers choke on a mimetype of application/octet-stream
1.651 + if (!$mimetype || $mimetype == 'application/octet-stream')
1.652 + {
1.653 + $mimetype = 'application/octetstream';
1.654 + }
1.655 +
1.656 + $_FILES[$form_name]['type'] = $mimetype;
1.657 + }
1.658 + else
1.659 + {
1.660 + $_FILES[$form_name]['name'] = $filedata['realname'];
1.661 + $_FILES[$form_name]['size'] = $filedata['size'];
1.662 + $_FILES[$form_name]['type'] = $filedata['type'];
1.663 + }
1.664 +
1.665 + $file = new filespec($_FILES[$form_name], $this);
1.666 +
1.667 + if ($file->init_error)
1.668 + {
1.669 + $file->error[] = '';
1.670 + return $file;
1.671 + }
1.672 +
1.673 + if (isset($_FILES[$form_name]['error']))
1.674 + {
1.675 + $error = $this->assign_internal_error($_FILES[$form_name]['error']);
1.676 +
1.677 + if ($error !== false)
1.678 + {
1.679 + $file->error[] = $error;
1.680 + return $file;
1.681 + }
1.682 + }
1.683 +
1.684 + // PHP Upload filesize exceeded
1.685 + if ($file->get('filename') == 'none')
1.686 + {
1.687 + $max_filesize = @ini_get('upload_max_filesize');
1.688 + $unit = 'MB';
1.689 +
1.690 + if (!empty($max_filesize))
1.691 + {
1.692 + $unit = strtolower(substr($max_filesize, -1, 1));
1.693 + $max_filesize = (int) $max_filesize;
1.694 +
1.695 + $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB');
1.696 + }
1.697 +
1.698 + $file->error[] = (empty($max_filesize)) ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], $max_filesize, $user->lang[$unit]);
1.699 + return $file;
1.700 + }
1.701 +
1.702 + // Not correctly uploaded
1.703 + if (!$file->is_uploaded())
1.704 + {
1.705 + $file->error[] = $user->lang[$this->error_prefix . 'NOT_UPLOADED'];
1.706 + return $file;
1.707 + }
1.708 +
1.709 + $this->common_checks($file);
1.710 +
1.711 + return $file;
1.712 + }
1.713 +
1.714 + /**
1.715 + * Remote upload method
1.716 + * Uploads file from given url
1.717 + *
1.718 + * @param string $upload_url URL pointing to file to upload, for example http://www.foobar.com/example.gif
1.719 + * @return object $file Object "filespec" is returned, all further operations can be done with this object
1.720 + * @access public
1.721 + */
1.722 + function remote_upload($upload_url)
1.723 + {
1.724 + global $user, $phpbb_root_path;
1.725 +
1.726 + $upload_ary = array();
1.727 + $upload_ary['local_mode'] = true;
1.728 +
1.729 + if (!preg_match('#^(https?://).*?\.(' . implode('|', $this->allowed_extensions) . ')$#i', $upload_url, $match))
1.730 + {
1.731 + $file = new fileerror($user->lang[$this->error_prefix . 'URL_INVALID']);
1.732 + return $file;
1.733 + }
1.734 +
1.735 + if (empty($match[2]))
1.736 + {
1.737 + $file = new fileerror($user->lang[$this->error_prefix . 'URL_INVALID']);
1.738 + return $file;
1.739 + }
1.740 +
1.741 + $url = parse_url($upload_url);
1.742 +
1.743 + $host = $url['host'];
1.744 + $path = $url['path'];
1.745 + $port = (!empty($url['port'])) ? (int) $url['port'] : 80;
1.746 +
1.747 + $upload_ary['type'] = 'application/octet-stream';
1.748 +
1.749 + $url['path'] = explode('.', $url['path']);
1.750 + $ext = array_pop($url['path']);
1.751 +
1.752 + $url['path'] = implode('', $url['path']);
1.753 + $upload_ary['name'] = utf8_basename($url['path']) . (($ext) ? '.' . $ext : '');
1.754 + $filename = $url['path'];
1.755 + $filesize = 0;
1.756 +
1.757 + $errno = 0;
1.758 + $errstr = '';
1.759 +
1.760 + if (!($fsock = @fsockopen($host, $port, $errno, $errstr)))
1.761 + {
1.762 + $file = new fileerror($user->lang[$this->error_prefix . 'NOT_UPLOADED']);
1.763 + return $file;
1.764 + }
1.765 +
1.766 + // Make sure $path not beginning with /
1.767 + if (strpos($path, '/') === 0)
1.768 + {
1.769 + $path = substr($path, 1);
1.770 + }
1.771 +
1.772 + fputs($fsock, 'GET /' . $path . " HTTP/1.1\r\n");
1.773 + fputs($fsock, "HOST: " . $host . "\r\n");
1.774 + fputs($fsock, "Connection: close\r\n\r\n");
1.775 +
1.776 + $get_info = false;
1.777 + $data = '';
1.778 + while (!@feof($fsock))
1.779 + {
1.780 + if ($get_info)
1.781 + {
1.782 + $block = @fread($fsock, 1024);
1.783 + $filesize += strlen($block);
1.784 +
1.785 + if ($this->max_filesize && $filesize > $this->max_filesize)
1.786 + {
1.787 + $max_filesize = get_formatted_filesize($this->max_filesize, false);
1.788 +
1.789 + $file = new fileerror(sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']));
1.790 + return $file;
1.791 + }
1.792 +
1.793 + $data .= $block;
1.794 + }
1.795 + else
1.796 + {
1.797 + $line = @fgets($fsock, 1024);
1.798 +
1.799 + if ($line == "\r\n")
1.800 + {
1.801 + $get_info = true;
1.802 + }
1.803 + else
1.804 + {
1.805 + if (stripos($line, 'content-type: ') !== false)
1.806 + {
1.807 + $upload_ary['type'] = rtrim(str_replace('content-type: ', '', strtolower($line)));
1.808 + }
1.809 + else if ($this->max_filesize && stripos($line, 'content-length: ') !== false)
1.810 + {
1.811 + $length = (int) str_replace('content-length: ', '', strtolower($line));
1.812 +
1.813 + if ($length && $length > $this->max_filesize)
1.814 + {
1.815 + $max_filesize = get_formatted_filesize($this->max_filesize, false);
1.816 +
1.817 + $file = new fileerror(sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']));
1.818 + return $file;
1.819 + }
1.820 + }
1.821 + else if (stripos($line, '404 not found') !== false)
1.822 + {
1.823 + $file = new fileerror($user->lang[$this->error_prefix . 'URL_NOT_FOUND']);
1.824 + return $file;
1.825 + }
1.826 + }
1.827 + }
1.828 + }
1.829 + @fclose($fsock);
1.830 +
1.831 + if (empty($data))
1.832 + {
1.833 + $file = new fileerror($user->lang[$this->error_prefix . 'EMPTY_REMOTE_DATA']);
1.834 + return $file;
1.835 + }
1.836 +
1.837 + $tmp_path = (!@ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'off') ? false : $phpbb_root_path . 'cache';
1.838 + $filename = tempnam($tmp_path, unique_id() . '-');
1.839 +
1.840 + if (!($fp = @fopen($filename, 'wb')))
1.841 + {
1.842 + $file = new fileerror($user->lang[$this->error_prefix . 'NOT_UPLOADED']);
1.843 + return $file;
1.844 + }
1.845 +
1.846 + $upload_ary['size'] = fwrite($fp, $data);
1.847 + fclose($fp);
1.848 + unset($data);
1.849 +
1.850 + $upload_ary['tmp_name'] = $filename;
1.851 +
1.852 + $file = new filespec($upload_ary, $this);
1.853 + $this->common_checks($file);
1.854 +
1.855 + return $file;
1.856 + }
1.857 +
1.858 + /**
1.859 + * Assign internal error
1.860 + * @access private
1.861 + */
1.862 + function assign_internal_error($errorcode)
1.863 + {
1.864 + global $user;
1.865 +
1.866 + switch ($errorcode)
1.867 + {
1.868 + case 1:
1.869 + $max_filesize = @ini_get('upload_max_filesize');
1.870 + $unit = 'MB';
1.871 +
1.872 + if (!empty($max_filesize))
1.873 + {
1.874 + $unit = strtolower(substr($max_filesize, -1, 1));
1.875 + $max_filesize = (int) $max_filesize;
1.876 +
1.877 + $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB');
1.878 + }
1.879 +
1.880 + $error = (empty($max_filesize)) ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], $max_filesize, $user->lang[$unit]);
1.881 + break;
1.882 +
1.883 + case 2:
1.884 + $max_filesize = get_formatted_filesize($this->max_filesize, false);
1.885 +
1.886 + $error = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']);
1.887 + break;
1.888 +
1.889 + case 3:
1.890 + $error = $user->lang[$this->error_prefix . 'PARTIAL_UPLOAD'];
1.891 + break;
1.892 +
1.893 + case 4:
1.894 + $error = $user->lang[$this->error_prefix . 'NOT_UPLOADED'];
1.895 + break;
1.896 +
1.897 + case 6:
1.898 + $error = 'Temporary folder could not be found. Please check your PHP installation.';
1.899 + break;
1.900 +
1.901 + default:
1.902 + $error = false;
1.903 + break;
1.904 + }
1.905 +
1.906 + return $error;
1.907 + }
1.908 +
1.909 + /**
1.910 + * Perform common checks
1.911 + */
1.912 + function common_checks(&$file)
1.913 + {
1.914 + global $user;
1.915 +
1.916 + // Filesize is too big or it's 0 if it was larger than the maxsize in the upload form
1.917 + if ($this->max_filesize && ($file->get('filesize') > $this->max_filesize || $file->get('filesize') == 0))
1.918 + {
1.919 + $max_filesize = get_formatted_filesize($this->max_filesize, false);
1.920 +
1.921 + $file->error[] = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']);
1.922 + }
1.923 +
1.924 + // check Filename
1.925 + if (preg_match("#[\\/:*?\"<>|]#i", $file->get('realname')))
1.926 + {
1.927 + $file->error[] = sprintf($user->lang[$this->error_prefix . 'INVALID_FILENAME'], $file->get('realname'));
1.928 + }
1.929 +
1.930 + // Invalid Extension
1.931 + if (!$this->valid_extension($file))
1.932 + {
1.933 + $file->error[] = sprintf($user->lang[$this->error_prefix . 'DISALLOWED_EXTENSION'], $file->get('extension'));
1.934 + }
1.935 +
1.936 + // MIME Sniffing
1.937 + if (!$this->valid_content($file))
1.938 + {
1.939 + $file->error[] = sprintf($user->lang[$this->error_prefix . 'DISALLOWED_CONTENT']);
1.940 + }
1.941 + }
1.942 +
1.943 + /**
1.944 + * Check for allowed extension
1.945 + */
1.946 + function valid_extension(&$file)
1.947 + {
1.948 + return (in_array($file->get('extension'), $this->allowed_extensions)) ? true : false;
1.949 + }
1.950 +
1.951 + /**
1.952 + * Check for allowed dimension
1.953 + */
1.954 + function valid_dimensions(&$file)
1.955 + {
1.956 + if (!$this->max_width && !$this->max_height && !$this->min_width && !$this->min_height)
1.957 + {
1.958 + return true;
1.959 + }
1.960 +
1.961 + if (($file->get('width') > $this->max_width && $this->max_width) ||
1.962 + ($file->get('height') > $this->max_height && $this->max_height) ||
1.963 + ($file->get('width') < $this->min_width && $this->min_width) ||
1.964 + ($file->get('height') < $this->min_height && $this->min_height))
1.965 + {
1.966 + return false;
1.967 + }
1.968 +
1.969 + return true;
1.970 + }
1.971 +
1.972 + /**
1.973 + * Check if form upload is valid
1.974 + */
1.975 + function is_valid($form_name)
1.976 + {
1.977 + return (isset($_FILES[$form_name]) && $_FILES[$form_name]['name'] != 'none') ? true : false;
1.978 + }
1.979 +
1.980 +
1.981 + /**
1.982 + * Check for allowed extension
1.983 + */
1.984 + function valid_content(&$file)
1.985 + {
1.986 + return ($file->check_content($this->disallowed_content));
1.987 + }
1.988 +
1.989 + /**
1.990 + * Return image type/extension mapping
1.991 + */
1.992 + function image_types()
1.993 + {
1.994 + return array(
1.995 + 1 => array('gif'),
1.996 + 2 => array('jpg', 'jpeg'),
1.997 + 3 => array('png'),
1.998 + 4 => array('swf'),
1.999 + 5 => array('psd'),
1.1000 + 6 => array('bmp'),
1.1001 + 7 => array('tif', 'tiff'),
1.1002 + 8 => array('tif', 'tiff'),
1.1003 + 9 => array('jpg', 'jpeg'),
1.1004 + 10 => array('jpg', 'jpeg'),
1.1005 + 11 => array('jpg', 'jpeg'),
1.1006 + 12 => array('jpg', 'jpeg'),
1.1007 + 13 => array('swc'),
1.1008 + 14 => array('iff'),
1.1009 + 15 => array('wbmp'),
1.1010 + 16 => array('xbm'),
1.1011 + );
1.1012 + }
1.1013 +}
1.1014 +
1.1015 +?>
1.1016 \ No newline at end of file