Blame Identity/Models/Html/phpBB/3.0.4/includes/functions_upload.php

d6e8d8
d6e8d8
/**
d6e8d8
*
d6e8d8
* @package phpBB3
d6e8d8
* @version $Id: functions_upload.php 8783 2008-08-23 17:23:40Z acydburn $
d6e8d8
* @copyright (c) 2005 phpBB Group
d6e8d8
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
d6e8d8
*
d6e8d8
*/
d6e8d8
d6e8d8
/**
d6e8d8
* @ignore
d6e8d8
*/
d6e8d8
if (!defined('IN_PHPBB'))
d6e8d8
{
d6e8d8
	exit;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Responsible for holding all file relevant information, as well as doing file-specific operations.
d6e8d8
* The {@link fileupload fileupload class} can be used to upload several files, each of them being this object to operate further on.
d6e8d8
* @package phpBB3
d6e8d8
*/
d6e8d8
class filespec
d6e8d8
{
d6e8d8
	var $filename = '';
d6e8d8
	var $realname = '';
d6e8d8
	var $uploadname = '';
d6e8d8
	var $mimetype = '';
d6e8d8
	var $extension = '';
d6e8d8
	var $filesize = 0;
d6e8d8
	var $width = 0;
d6e8d8
	var $height = 0;
d6e8d8
	var $image_info = array();
d6e8d8
d6e8d8
	var $destination_file = '';
d6e8d8
	var $destination_path = '';
d6e8d8
d6e8d8
	var $file_moved = false;
d6e8d8
	var $init_error = false;
d6e8d8
	var $local = false;
d6e8d8
d6e8d8
	var $error = array();
d6e8d8
d6e8d8
	var $upload = '';
d6e8d8
d6e8d8
	/**
d6e8d8
	* File Class
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function filespec($upload_ary, $upload_namespace)
d6e8d8
	{
d6e8d8
		if (!isset($upload_ary))
d6e8d8
		{
d6e8d8
			$this->init_error = true;
d6e8d8
			return;
d6e8d8
		}
d6e8d8
d6e8d8
		$this->filename = $upload_ary['tmp_name'];
d6e8d8
		$this->filesize = $upload_ary['size'];
d6e8d8
		$name = trim(htmlspecialchars(basename($upload_ary['name'])));
d6e8d8
		$this->realname = $this->uploadname = (STRIP) ? stripslashes($name) : $name;
d6e8d8
		$this->mimetype = $upload_ary['type'];
d6e8d8
d6e8d8
		// Opera adds the name to the mime type
d6e8d8
		$this->mimetype	= (strpos($this->mimetype, '; name') !== false) ? str_replace(strstr($this->mimetype, '; name'), '', $this->mimetype) : $this->mimetype;
d6e8d8
d6e8d8
		if (!$this->mimetype)
d6e8d8
		{
d6e8d8
			$this->mimetype = 'application/octetstream';
d6e8d8
		}
d6e8d8
d6e8d8
		$this->extension = strtolower($this->get_extension($this->realname));
d6e8d8
d6e8d8
		// Try to get real filesize from temporary folder (not always working) ;)
d6e8d8
		$this->filesize = (@filesize($this->filename)) ? @filesize($this->filename) : $this->filesize;
d6e8d8
d6e8d8
		$this->width = $this->height = 0;
d6e8d8
		$this->file_moved = false;
d6e8d8
d6e8d8
		$this->local = (isset($upload_ary['local_mode'])) ? true : false;
d6e8d8
		$this->upload = $upload_namespace;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Cleans destination filename
d6e8d8
	*
d6e8d8
	* @param real|unique|unique_ext $mode real creates a realname, filtering some characters, lowering every character. Unique creates an unique filename
d6e8d8
	* @param string $prefix Prefix applied to filename
d6e8d8
	* @access public
d6e8d8
	*/
d6e8d8
	function clean_filename($mode = 'unique', $prefix = '', $user_id = '')
d6e8d8
	{
d6e8d8
		if ($this->init_error)
d6e8d8
		{
d6e8d8
			return;
d6e8d8
		}
d6e8d8
d6e8d8
		switch ($mode)
d6e8d8
		{
d6e8d8
			case 'real':
d6e8d8
				// Remove every extension from filename (to not let the mime bug being exposed)
d6e8d8
				if (strpos($this->realname, '.') !== false)
d6e8d8
				{
d6e8d8
					$this->realname = substr($this->realname, 0, strpos($this->realname, '.'));
d6e8d8
				}
d6e8d8
d6e8d8
				// Replace any chars which may cause us problems with _
d6e8d8
				$bad_chars = array("'", "\\", ' ', '/', ':', '*', '?', '"', '<', '>', '|');
d6e8d8
d6e8d8
				$this->realname = rawurlencode(str_replace($bad_chars, '_', strtolower($this->realname)));
d6e8d8
				$this->realname = preg_replace("/%(\w{2})/", '_', $this->realname);
d6e8d8
d6e8d8
				$this->realname = $prefix . $this->realname . '.' . $this->extension;
d6e8d8
			break;
d6e8d8
d6e8d8
			case 'unique':
d6e8d8
				$this->realname = $prefix . md5(unique_id());
d6e8d8
			break;
d6e8d8
d6e8d8
			case 'avatar':
d6e8d8
				$this->extension = strtolower($this->extension);
d6e8d8
				$this->realname = $prefix . $user_id . '.' . $this->extension;
d6e8d8
d6e8d8
			break;
d6e8d8
d6e8d8
			case 'unique_ext':
d6e8d8
			default:
d6e8d8
				$this->realname = $prefix . md5(unique_id()) . '.' . $this->extension;
d6e8d8
			break;
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Get property from file object
d6e8d8
	*/
d6e8d8
	function get($property)
d6e8d8
	{
d6e8d8
		if ($this->init_error || !isset($this->$property))
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		return $this->$property;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Check if file is an image (mimetype)
d6e8d8
	*
d6e8d8
	* @return true if it is an image, false if not
d6e8d8
	*/
d6e8d8
	function is_image()
d6e8d8
	{
d6e8d8
		return (strpos($this->mimetype, 'image/') !== false) ? true : false;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Check if the file got correctly uploaded
d6e8d8
	*
d6e8d8
	* @return true if it is a valid upload, false if not
d6e8d8
	*/
d6e8d8
	function is_uploaded()
d6e8d8
	{
d6e8d8
		if (!$this->local && !is_uploaded_file($this->filename))
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		if ($this->local && !file_exists($this->filename))
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		return true;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Remove file
d6e8d8
	*/
d6e8d8
	function remove()
d6e8d8
	{
d6e8d8
		if ($this->file_moved)
d6e8d8
		{
d6e8d8
			@unlink($this->destination_file);
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Get file extension
d6e8d8
	*/
d6e8d8
	function get_extension($filename)
d6e8d8
	{
d6e8d8
		if (strpos($filename, '.') === false)
d6e8d8
		{
d6e8d8
			return '';
d6e8d8
		}
d6e8d8
d6e8d8
		$filename = explode('.', $filename);
d6e8d8
		return array_pop($filename);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Get mimetype. Utilize mime_content_type if the function exist.
d6e8d8
	* Not used at the moment...
d6e8d8
	*/
d6e8d8
	function get_mimetype($filename)
d6e8d8
	{
d6e8d8
		$mimetype = '';
d6e8d8
d6e8d8
		if (function_exists('mime_content_type'))
d6e8d8
		{
d6e8d8
			$mimetype = mime_content_type($filename);
d6e8d8
		}
d6e8d8
d6e8d8
		// Some browsers choke on a mimetype of application/octet-stream
d6e8d8
		if (!$mimetype || $mimetype == 'application/octet-stream')
d6e8d8
		{
d6e8d8
			$mimetype = 'application/octetstream';
d6e8d8
		}
d6e8d8
d6e8d8
		return $mimetype;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Get filesize
d6e8d8
	*/
d6e8d8
	function get_filesize($filename)
d6e8d8
	{
d6e8d8
		return @filesize($filename);
d6e8d8
	}
d6e8d8
d6e8d8
d6e8d8
	/**
d6e8d8
	* Check the first 256 bytes for forbidden content
d6e8d8
	*/
d6e8d8
	function check_content($disallowed_content)
d6e8d8
	{
d6e8d8
		if (empty($disallowed_content))
d6e8d8
		{
d6e8d8
			return true;
d6e8d8
		}
d6e8d8
d6e8d8
		$fp = @fopen($this->filename, 'rb');
d6e8d8
d6e8d8
		if ($fp !== false)
d6e8d8
		{
d6e8d8
			$ie_mime_relevant = fread($fp, 256);
d6e8d8
			fclose($fp);
d6e8d8
			foreach ($disallowed_content as $forbidden)
d6e8d8
			{
d6e8d8
				if (stripos($ie_mime_relevant, '<' . $forbidden) !== false)
d6e8d8
				{
d6e8d8
					return false;
d6e8d8
				}
d6e8d8
			}
d6e8d8
		}
d6e8d8
		return true;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Move file to destination folder
d6e8d8
	* The phpbb_root_path variable will be applied to the destination path
d6e8d8
	*
d6e8d8
	* @param string $destination_path Destination path, for example $config['avatar_path']
d6e8d8
	* @param bool $overwrite If set to true, an already existing file will be overwritten
d6e8d8
	* @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()}
d6e8d8
	*
d6e8d8
	* @access public
d6e8d8
	*/
d6e8d8
	function move_file($destination, $overwrite = false, $skip_image_check = false, $chmod = false)
d6e8d8
	{
d6e8d8
		global $user, $phpbb_root_path;
d6e8d8
d6e8d8
		if (sizeof($this->error))
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		$chmod = ($chmod === false) ? CHMOD_READ | CHMOD_WRITE : $chmod;
d6e8d8
d6e8d8
		// We need to trust the admin in specifying valid upload directories and an attacker not being able to overwrite it...
d6e8d8
		$this->destination_path = $phpbb_root_path . $destination;
d6e8d8
d6e8d8
		// Check if the destination path exist...
d6e8d8
		if (!file_exists($this->destination_path))
d6e8d8
		{
d6e8d8
			@unlink($this->filename);
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		$upload_mode = (@ini_get('open_basedir') || @ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'on') ? 'move' : 'copy';
d6e8d8
		$upload_mode = ($this->local) ? 'local' : $upload_mode;
d6e8d8
		$this->destination_file = $this->destination_path . '/' . basename($this->realname);
d6e8d8
d6e8d8
		// Check if the file already exist, else there is something wrong...
d6e8d8
		if (file_exists($this->destination_file) && !$overwrite)
d6e8d8
		{
d6e8d8
			@unlink($this->filename);
d6e8d8
		}
d6e8d8
		else
d6e8d8
		{
d6e8d8
			if (file_exists($this->destination_file))
d6e8d8
			{
d6e8d8
				@unlink($this->destination_file);
d6e8d8
			}
d6e8d8
d6e8d8
			switch ($upload_mode)
d6e8d8
			{
d6e8d8
				case 'copy':
d6e8d8
d6e8d8
					if (!@copy($this->filename, $this->destination_file))
d6e8d8
					{
d6e8d8
						if (!@move_uploaded_file($this->filename, $this->destination_file))
d6e8d8
						{
d6e8d8
							$this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file);
d6e8d8
							return false;
d6e8d8
						}
d6e8d8
					}
d6e8d8
d6e8d8
					@unlink($this->filename);
d6e8d8
d6e8d8
				break;
d6e8d8
d6e8d8
				case 'move':
d6e8d8
d6e8d8
					if (!@move_uploaded_file($this->filename, $this->destination_file))
d6e8d8
					{
d6e8d8
						if (!@copy($this->filename, $this->destination_file))
d6e8d8
						{
d6e8d8
							$this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file);
d6e8d8
							return false;
d6e8d8
						}
d6e8d8
					}
d6e8d8
d6e8d8
					@unlink($this->filename);
d6e8d8
d6e8d8
				break;
d6e8d8
d6e8d8
				case 'local':
d6e8d8
d6e8d8
					if (!@copy($this->filename, $this->destination_file))
d6e8d8
					{
d6e8d8
						$this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file);
d6e8d8
						return false;
d6e8d8
					}
d6e8d8
					@unlink($this->filename);
d6e8d8
d6e8d8
				break;
d6e8d8
			}
d6e8d8
d6e8d8
			phpbb_chmod($this->destination_file, $chmod);
d6e8d8
		}
d6e8d8
d6e8d8
		// Try to get real filesize from destination folder
d6e8d8
		$this->filesize = (@filesize($this->destination_file)) ? @filesize($this->destination_file) : $this->filesize;
d6e8d8
d6e8d8
		if ($this->is_image() && !$skip_image_check)
d6e8d8
		{
d6e8d8
			$this->width = $this->height = 0;
d6e8d8
d6e8d8
			if (($this->image_info = @getimagesize($this->destination_file)) !== false)
d6e8d8
			{
d6e8d8
				$this->width = $this->image_info[0];
d6e8d8
				$this->height = $this->image_info[1];
d6e8d8
d6e8d8
				if (!empty($this->image_info['mime']))
d6e8d8
				{
d6e8d8
					$this->mimetype = $this->image_info['mime'];
d6e8d8
				}
d6e8d8
d6e8d8
				// Check image type
d6e8d8
				$types = $this->upload->image_types();
d6e8d8
d6e8d8
				if (!isset($types[$this->image_info[2]]) || !in_array($this->extension, $types[$this->image_info[2]]))
d6e8d8
				{
d6e8d8
					if (!isset($types[$this->image_info[2]]))
d6e8d8
					{
d6e8d8
						$this->error[] = sprintf($user->lang['IMAGE_FILETYPE_INVALID'], $this->image_info[2], $this->mimetype);
d6e8d8
					}
d6e8d8
					else
d6e8d8
					{
d6e8d8
						$this->error[] = sprintf($user->lang['IMAGE_FILETYPE_MISMATCH'], $types[$this->image_info[2]][0], $this->extension);
d6e8d8
					}
d6e8d8
				}
d6e8d8
d6e8d8
				// Make sure the dimensions match a valid image
d6e8d8
				if (empty($this->width) || empty($this->height))
d6e8d8
				{
d6e8d8
					$this->error[] = $user->lang['ATTACHED_IMAGE_NOT_IMAGE'];
d6e8d8
				}
d6e8d8
			}
d6e8d8
			else
d6e8d8
			{
d6e8d8
				$this->error[] = $user->lang['UNABLE_GET_IMAGE_SIZE'];
d6e8d8
			}
d6e8d8
		}
d6e8d8
d6e8d8
		$this->file_moved = true;
d6e8d8
		$this->additional_checks();
d6e8d8
		unset($this->upload);
d6e8d8
d6e8d8
		return true;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Performing additional checks
d6e8d8
	*/
d6e8d8
	function additional_checks()
d6e8d8
	{
d6e8d8
		global $user;
d6e8d8
d6e8d8
		if (!$this->file_moved)
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		// Filesize is too big or it's 0 if it was larger than the maxsize in the upload form
d6e8d8
		if ($this->upload->max_filesize && ($this->get('filesize') > $this->upload->max_filesize || $this->filesize == 0))
d6e8d8
		{
d6e8d8
			$size_lang = ($this->upload->max_filesize >= 1048576) ? $user->lang['MIB'] : (($this->upload->max_filesize >= 1024) ? $user->lang['KIB'] : $user->lang['BYTES'] );
d6e8d8
			$max_filesize = get_formatted_filesize($this->upload->max_filesize, false);
d6e8d8
d6e8d8
			$this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'WRONG_FILESIZE'], $max_filesize, $size_lang);
d6e8d8
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		if (!$this->upload->valid_dimensions($this))
d6e8d8
		{
d6e8d8
			$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);
d6e8d8
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		return true;
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Class for assigning error messages before a real filespec class can be assigned
d6e8d8
*
d6e8d8
* @package phpBB3
d6e8d8
*/
d6e8d8
class fileerror extends filespec
d6e8d8
{
d6e8d8
	function fileerror($error_msg)
d6e8d8
	{
d6e8d8
		$this->error[] = $error_msg;
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* File upload class
d6e8d8
* Init class (all parameters optional and able to be set/overwritten separately) - scope is global and valid for all uploads
d6e8d8
*
d6e8d8
* @package phpBB3
d6e8d8
*/
d6e8d8
class fileupload
d6e8d8
{
d6e8d8
	var $allowed_extensions = array();
d6e8d8
	var $disallowed_content = array();
d6e8d8
	var $max_filesize = 0;
d6e8d8
	var $min_width = 0;
d6e8d8
	var $min_height = 0;
d6e8d8
	var $max_width = 0;
d6e8d8
	var $max_height = 0;
d6e8d8
	var $error_prefix = '';
d6e8d8
d6e8d8
	/**
d6e8d8
	* Init file upload class.
d6e8d8
	*
d6e8d8
	* @param string $error_prefix Used error messages will get prefixed by this string
d6e8d8
	* @param array $allowed_extensions Array of allowed extensions, for example array('jpg', 'jpeg', 'gif', 'png')
d6e8d8
	* @param int $max_filesize Maximum filesize
d6e8d8
	* @param int $min_width Minimum image width (only checked for images)
d6e8d8
	* @param int $min_height Minimum image height (only checked for images)
d6e8d8
	* @param int $max_width Maximum image width (only checked for images)
d6e8d8
	* @param int $max_height Maximum image height (only checked for images)
d6e8d8
	*
d6e8d8
	*/
d6e8d8
	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)
d6e8d8
	{
d6e8d8
		$this->set_allowed_extensions($allowed_extensions);
d6e8d8
		$this->set_max_filesize($max_filesize);
d6e8d8
		$this->set_allowed_dimensions($min_width, $min_height, $max_width, $max_height);
d6e8d8
		$this->set_error_prefix($error_prefix);
d6e8d8
		$this->set_disallowed_content($disallowed_content);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Reset vars
d6e8d8
	*/
d6e8d8
	function reset_vars()
d6e8d8
	{
d6e8d8
		$this->max_filesize = 0;
d6e8d8
		$this->min_width = $this->min_height = $this->max_width = $this->max_height = 0;
d6e8d8
		$this->error_prefix = '';
d6e8d8
		$this->allowed_extensions = array();
d6e8d8
		$this->disallowed_content = array();
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Set allowed extensions
d6e8d8
	*/
d6e8d8
	function set_allowed_extensions($allowed_extensions)
d6e8d8
	{
d6e8d8
		if ($allowed_extensions !== false && is_array($allowed_extensions))
d6e8d8
		{
d6e8d8
			$this->allowed_extensions = $allowed_extensions;
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Set allowed dimensions
d6e8d8
	*/
d6e8d8
	function set_allowed_dimensions($min_width, $min_height, $max_width, $max_height)
d6e8d8
	{
d6e8d8
		$this->min_width = (int) $min_width;
d6e8d8
		$this->min_height = (int) $min_height;
d6e8d8
		$this->max_width = (int) $max_width;
d6e8d8
		$this->max_height = (int) $max_height;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Set maximum allowed filesize
d6e8d8
	*/
d6e8d8
	function set_max_filesize($max_filesize)
d6e8d8
	{
d6e8d8
		if ($max_filesize !== false && (int) $max_filesize)
d6e8d8
		{
d6e8d8
			$this->max_filesize = (int) $max_filesize;
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Set disallowed strings
d6e8d8
	*/
d6e8d8
	function set_disallowed_content($disallowed_content)
d6e8d8
	{
d6e8d8
		if ($disallowed_content !== false && is_array($disallowed_content))
d6e8d8
		{
d6e8d8
			$this->disallowed_content = $disallowed_content;
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Set error prefix
d6e8d8
	*/
d6e8d8
	function set_error_prefix($error_prefix)
d6e8d8
	{
d6e8d8
		$this->error_prefix = $error_prefix;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Form upload method
d6e8d8
	* Upload file from users harddisk
d6e8d8
	*
d6e8d8
	* @param string $form_name Form name assigned to the file input field (if it is an array, the key has to be specified)
d6e8d8
	* @return object $file Object "filespec" is returned, all further operations can be done with this object
d6e8d8
	* @access public
d6e8d8
	*/
d6e8d8
	function form_upload($form_name)
d6e8d8
	{
d6e8d8
		global $user;
d6e8d8
d6e8d8
		unset($_FILES[$form_name]['local_mode']);
d6e8d8
		$file = new filespec($_FILES[$form_name], $this);
d6e8d8
d6e8d8
		if ($file->init_error)
d6e8d8
		{
d6e8d8
			$file->error[] = '';
d6e8d8
			return $file;
d6e8d8
		}
d6e8d8
d6e8d8
		// Error array filled?
d6e8d8
		if (isset($_FILES[$form_name]['error']))
d6e8d8
		{
d6e8d8
			$error = $this->assign_internal_error($_FILES[$form_name]['error']);
d6e8d8
d6e8d8
			if ($error !== false)
d6e8d8
			{
d6e8d8
				$file->error[] = $error;
d6e8d8
				return $file;
d6e8d8
			}
d6e8d8
		}
d6e8d8
d6e8d8
		// Check if empty file got uploaded (not catched by is_uploaded_file)
d6e8d8
		if (isset($_FILES[$form_name]['size']) && $_FILES[$form_name]['size'] == 0)
d6e8d8
		{
d6e8d8
			$file->error[] = $user->lang[$this->error_prefix . 'EMPTY_FILEUPLOAD'];
d6e8d8
			return $file;
d6e8d8
		}
d6e8d8
d6e8d8
		// PHP Upload filesize exceeded
d6e8d8
		if ($file->get('filename') == 'none')
d6e8d8
		{
d6e8d8
			$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'));
d6e8d8
			return $file;
d6e8d8
		}
d6e8d8
d6e8d8
		// Not correctly uploaded
d6e8d8
		if (!$file->is_uploaded())
d6e8d8
		{
d6e8d8
			$file->error[] = $user->lang[$this->error_prefix . 'NOT_UPLOADED'];
d6e8d8
			return $file;
d6e8d8
		}
d6e8d8
d6e8d8
		$this->common_checks($file);
d6e8d8
d6e8d8
		return $file;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Move file from another location to phpBB
d6e8d8
	*/
d6e8d8
	function local_upload($source_file, $filedata = false)
d6e8d8
	{
d6e8d8
		global $user;
d6e8d8
d6e8d8
		$form_name = 'local';
d6e8d8
d6e8d8
		$_FILES[$form_name]['local_mode'] = true;
d6e8d8
		$_FILES[$form_name]['tmp_name'] = $source_file;
d6e8d8
d6e8d8
		if ($filedata === false)
d6e8d8
		{
d6e8d8
			$_FILES[$form_name]['name'] = basename($source_file);
d6e8d8
			$_FILES[$form_name]['size'] = 0;
d6e8d8
			$mimetype = '';
d6e8d8
d6e8d8
			if (function_exists('mime_content_type'))
d6e8d8
			{
d6e8d8
				$mimetype = mime_content_type($source_file);
d6e8d8
			}
d6e8d8
d6e8d8
			// Some browsers choke on a mimetype of application/octet-stream
d6e8d8
			if (!$mimetype || $mimetype == 'application/octet-stream')
d6e8d8
			{
d6e8d8
				$mimetype = 'application/octetstream';
d6e8d8
			}
d6e8d8
d6e8d8
			$_FILES[$form_name]['type'] = $mimetype;
d6e8d8
		}
d6e8d8
		else
d6e8d8
		{
d6e8d8
			$_FILES[$form_name]['name'] = $filedata['realname'];
d6e8d8
			$_FILES[$form_name]['size'] = $filedata['size'];
d6e8d8
			$_FILES[$form_name]['type'] = $filedata['type'];
d6e8d8
		}
d6e8d8
d6e8d8
		$file = new filespec($_FILES[$form_name], $this);
d6e8d8
d6e8d8
		if ($file->init_error)
d6e8d8
		{
d6e8d8
			$file->error[] = '';
d6e8d8
			return $file;
d6e8d8
		}
d6e8d8
d6e8d8
		if (isset($_FILES[$form_name]['error']))
d6e8d8
		{
d6e8d8
			$error = $this->assign_internal_error($_FILES[$form_name]['error']);
d6e8d8
d6e8d8
			if ($error !== false)
d6e8d8
			{
d6e8d8
				$file->error[] = $error;
d6e8d8
				return $file;
d6e8d8
			}
d6e8d8
		}
d6e8d8
d6e8d8
		// PHP Upload filesize exceeded
d6e8d8
		if ($file->get('filename') == 'none')
d6e8d8
		{
d6e8d8
			$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'));
d6e8d8
			return $file;
d6e8d8
		}
d6e8d8
d6e8d8
		// Not correctly uploaded
d6e8d8
		if (!$file->is_uploaded())
d6e8d8
		{
d6e8d8
			$file->error[] = $user->lang[$this->error_prefix . 'NOT_UPLOADED'];
d6e8d8
			return $file;
d6e8d8
		}
d6e8d8
d6e8d8
		$this->common_checks($file);
d6e8d8
d6e8d8
		return $file;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Remote upload method
d6e8d8
	* Uploads file from given url
d6e8d8
	*
d6e8d8
	* @param string $upload_url URL pointing to file to upload, for example http://www.foobar.com/example.gif
d6e8d8
	* @return object $file Object "filespec" is returned, all further operations can be done with this object
d6e8d8
	* @access public
d6e8d8
	*/
d6e8d8
	function remote_upload($upload_url)
d6e8d8
	{
d6e8d8
		global $user, $phpbb_root_path;
d6e8d8
d6e8d8
		$upload_ary = array();
d6e8d8
		$upload_ary['local_mode'] = true;
d6e8d8
d6e8d8
		if (!preg_match('#^(https?://).*?\.(' . implode('|', $this->allowed_extensions) . ')$#i', $upload_url, $match))
d6e8d8
		{
d6e8d8
			$file = new fileerror($user->lang[$this->error_prefix . 'URL_INVALID']);
d6e8d8
			return $file;
d6e8d8
		}
d6e8d8
d6e8d8
		if (empty($match[2]))
d6e8d8
		{
d6e8d8
			$file = new fileerror($user->lang[$this->error_prefix . 'URL_INVALID']);
d6e8d8
			return $file;
d6e8d8
		}
d6e8d8
d6e8d8
		$url = parse_url($upload_url);
d6e8d8
d6e8d8
		$host = $url['host'];
d6e8d8
		$path = $url['path'];
d6e8d8
		$port = (!empty($url['port'])) ? (int) $url['port'] : 80;
d6e8d8
d6e8d8
		$upload_ary['type'] = 'application/octet-stream';
d6e8d8
d6e8d8
		$url['path'] = explode('.', $url['path']);
d6e8d8
		$ext = array_pop($url['path']);
d6e8d8
d6e8d8
		$url['path'] = implode('', $url['path']);
d6e8d8
		$upload_ary['name'] = basename($url['path']) . (($ext) ? '.' . $ext : '');
d6e8d8
		$filename = $url['path'];
d6e8d8
		$filesize = 0;
d6e8d8
d6e8d8
		$errno = 0;
d6e8d8
		$errstr = '';
d6e8d8
d6e8d8
		if (!($fsock = @fsockopen($host, $port, $errno, $errstr)))
d6e8d8
		{
d6e8d8
			$file = new fileerror($user->lang[$this->error_prefix . 'NOT_UPLOADED']);
d6e8d8
			return $file;
d6e8d8
		}
d6e8d8
d6e8d8
		// Make sure $path not beginning with /
d6e8d8
		if (strpos($path, '/') === 0)
d6e8d8
		{
d6e8d8
			$path = substr($path, 1);
d6e8d8
		}
d6e8d8
d6e8d8
		fputs($fsock, 'GET /' . $path . " HTTP/1.1\r\n");
d6e8d8
		fputs($fsock, "HOST: " . $host . "\r\n");
d6e8d8
		fputs($fsock, "Connection: close\r\n\r\n");
d6e8d8
d6e8d8
		$get_info = false;
d6e8d8
		$data = '';
d6e8d8
		while (!@feof($fsock))
d6e8d8
		{
d6e8d8
			if ($get_info)
d6e8d8
			{
d6e8d8
				$data .= @fread($fsock, 1024);
d6e8d8
			}
d6e8d8
			else
d6e8d8
			{
d6e8d8
				$line = @fgets($fsock, 1024);
d6e8d8
d6e8d8
				if ($line == "\r\n")
d6e8d8
				{
d6e8d8
					$get_info = true;
d6e8d8
				}
d6e8d8
				else
d6e8d8
				{
d6e8d8
					if (stripos($line, 'content-type: ') !== false)
d6e8d8
					{
d6e8d8
						$upload_ary['type'] = rtrim(str_replace('content-type: ', '', strtolower($line)));
d6e8d8
					}
d6e8d8
					else if (stripos($line, '404 not found') !== false)
d6e8d8
					{
d6e8d8
						$file = new fileerror($user->lang[$this->error_prefix . 'URL_NOT_FOUND']);
d6e8d8
						return $file;
d6e8d8
					}
d6e8d8
				}
d6e8d8
			}
d6e8d8
		}
d6e8d8
		@fclose($fsock);
d6e8d8
d6e8d8
		if (empty($data))
d6e8d8
		{
d6e8d8
			$file = new fileerror($user->lang[$this->error_prefix . 'EMPTY_REMOTE_DATA']);
d6e8d8
			return $file;
d6e8d8
		}
d6e8d8
d6e8d8
		$tmp_path = (!@ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'off') ? false : $phpbb_root_path . 'cache';
d6e8d8
		$filename = tempnam($tmp_path, unique_id() . '-');
d6e8d8
d6e8d8
		if (!($fp = @fopen($filename, 'wb')))
d6e8d8
		{
d6e8d8
			$file = new fileerror($user->lang[$this->error_prefix . 'NOT_UPLOADED']);
d6e8d8
			return $file;
d6e8d8
		}
d6e8d8
d6e8d8
		$upload_ary['size'] = fwrite($fp, $data);
d6e8d8
		fclose($fp);
d6e8d8
		unset($data);
d6e8d8
d6e8d8
		$upload_ary['tmp_name'] = $filename;
d6e8d8
d6e8d8
		$file = new filespec($upload_ary, $this);
d6e8d8
		$this->common_checks($file);
d6e8d8
d6e8d8
		return $file;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Assign internal error
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function assign_internal_error($errorcode)
d6e8d8
	{
d6e8d8
		global $user;
d6e8d8
d6e8d8
		switch ($errorcode)
d6e8d8
		{
d6e8d8
			case 1:
d6e8d8
				$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'));
d6e8d8
			break;
d6e8d8
d6e8d8
			case 2:
d6e8d8
				$size_lang = ($this->max_filesize >= 1048576) ? $user->lang['MIB'] : (($this->max_filesize >= 1024) ? $user->lang['KIB'] : $user->lang['BYTES']);
d6e8d8
				$max_filesize = get_formatted_filesize($this->max_filesize, false);
d6e8d8
d6e8d8
				$error = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize, $size_lang);
d6e8d8
			break;
d6e8d8
d6e8d8
			case 3:
d6e8d8
				$error = $user->lang[$this->error_prefix . 'PARTIAL_UPLOAD'];
d6e8d8
			break;
d6e8d8
d6e8d8
			case 4:
d6e8d8
				$error = $user->lang[$this->error_prefix . 'NOT_UPLOADED'];
d6e8d8
			break;
d6e8d8
d6e8d8
			case 6:
d6e8d8
				$error = 'Temporary folder could not be found. Please check your PHP installation.';
d6e8d8
			break;
d6e8d8
d6e8d8
			default:
d6e8d8
				$error = false;
d6e8d8
			break;
d6e8d8
		}
d6e8d8
d6e8d8
		return $error;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Perform common checks
d6e8d8
	*/
d6e8d8
	function common_checks(&$file)
d6e8d8
	{
d6e8d8
		global $user;
d6e8d8
d6e8d8
		// Filesize is too big or it's 0 if it was larger than the maxsize in the upload form
d6e8d8
		if ($this->max_filesize && ($file->get('filesize') > $this->max_filesize || $file->get('filesize') == 0))
d6e8d8
		{
d6e8d8
			$size_lang = ($this->max_filesize >= 1048576) ? $user->lang['MIB'] : (($this->max_filesize >= 1024) ? $user->lang['KIB'] : $user->lang['BYTES']);
d6e8d8
			$max_filesize = get_formatted_filesize($this->max_filesize, false);
d6e8d8
d6e8d8
			$file->error[] = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize, $size_lang);
d6e8d8
		}
d6e8d8
d6e8d8
		// check Filename
d6e8d8
		if (preg_match("#[\\/:*?\"<>|]#i", $file->get('realname')))
d6e8d8
		{
d6e8d8
			$file->error[] = sprintf($user->lang[$this->error_prefix . 'INVALID_FILENAME'], $file->get('realname'));
d6e8d8
		}
d6e8d8
d6e8d8
		// Invalid Extension
d6e8d8
		if (!$this->valid_extension($file))
d6e8d8
		{
d6e8d8
			$file->error[] = sprintf($user->lang[$this->error_prefix . 'DISALLOWED_EXTENSION'], $file->get('extension'));
d6e8d8
		}
d6e8d8
d6e8d8
		// MIME Sniffing
d6e8d8
		if (!$this->valid_content($file))
d6e8d8
		{
d6e8d8
			$file->error[] = sprintf($user->lang[$this->error_prefix . 'DISALLOWED_CONTENT']);
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Check for allowed extension
d6e8d8
	*/
d6e8d8
	function valid_extension(&$file)
d6e8d8
	{
d6e8d8
		return (in_array($file->get('extension'), $this->allowed_extensions)) ? true : false;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Check for allowed dimension
d6e8d8
	*/
d6e8d8
	function valid_dimensions(&$file)
d6e8d8
	{
d6e8d8
		if (!$this->max_width && !$this->max_height && !$this->min_width && !$this->min_height)
d6e8d8
		{
d6e8d8
			return true;
d6e8d8
		}
d6e8d8
d6e8d8
		if (($file->get('width') > $this->max_width && $this->max_width) ||
d6e8d8
			($file->get('height') > $this->max_height && $this->max_height) ||
d6e8d8
			($file->get('width') < $this->min_width && $this->min_width) ||
d6e8d8
			($file->get('height') < $this->min_height && $this->min_height))
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		return true;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Check if form upload is valid
d6e8d8
	*/
d6e8d8
	function is_valid($form_name)
d6e8d8
	{
d6e8d8
		return (isset($_FILES[$form_name]) && $_FILES[$form_name]['name'] != 'none') ? true : false;
d6e8d8
	}
d6e8d8
d6e8d8
d6e8d8
	/**
d6e8d8
	* Check for allowed extension
d6e8d8
	*/
d6e8d8
	function valid_content(&$file)
d6e8d8
	{
d6e8d8
		return ($file->check_content($this->disallowed_content));
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Return image type/extension mapping
d6e8d8
	*/
d6e8d8
	function image_types()
d6e8d8
	{
d6e8d8
		return array(
d6e8d8
			1 => array('gif'),
d6e8d8
			2 => array('jpg', 'jpeg'),
d6e8d8
			3 => array('png'),
d6e8d8
			4 => array('swf'),
d6e8d8
			5 => array('psd'),
d6e8d8
			6 => array('bmp'),
d6e8d8
			7 => array('tif', 'tiff'),
d6e8d8
			8 => array('tif', 'tiff'),
d6e8d8
			9 => array('jpg', 'jpeg'),
d6e8d8
			10 => array('jpg', 'jpeg'),
d6e8d8
			11 => array('jpg', 'jpeg'),
d6e8d8
			12 => array('jpg', 'jpeg'),
d6e8d8
			13 => array('swc'),
d6e8d8
			14 => array('iff'),
d6e8d8
			15 => array('wbmp'),
d6e8d8
			16 => array('xbm'),
d6e8d8
		);
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
?>