Blame Identity/Webenv/App/phpBB/3.0.4/includes/functions_upload.php

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