Blame Extras/phpBB/3.0.4/includes/functions_upload.php

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