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

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