Blame Extras/phpBB/3.0.4/download/file.php

4c79b5
4c79b5
/**
4c79b5
*
4c79b5
* @package phpBB3
4c79b5
* @version $Id: file.php 8792 2008-08-28 13:10:05Z Kellanved $
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
define('IN_PHPBB', true);
4c79b5
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../';
4c79b5
$phpEx = substr(strrchr(__FILE__, '.'), 1);
4c79b5
4c79b5
4c79b5
// Thank you sun. 
4c79b5
if (isset($_SERVER['CONTENT_TYPE']))
4c79b5
{
4c79b5
	if ($_SERVER['CONTENT_TYPE'] === 'application/x-java-archive')
4c79b5
	{
4c79b5
		exit;
4c79b5
	}
4c79b5
}
4c79b5
else if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Java') !== false)
4c79b5
{
4c79b5
	exit;
4c79b5
}
4c79b5
4c79b5
if (isset($_GET['avatar']))
4c79b5
{
4c79b5
	require($phpbb_root_path . 'config.' . $phpEx);
4c79b5
4c79b5
	if (!defined('PHPBB_INSTALLED') || empty($dbms) || empty($acm_type))
4c79b5
	{
4c79b5
		exit;
4c79b5
	}
4c79b5
4c79b5
	require($phpbb_root_path . 'includes/acm/acm_' . $acm_type . '.' . $phpEx);
4c79b5
	require($phpbb_root_path . 'includes/cache.' . $phpEx);
4c79b5
	require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
4c79b5
	require($phpbb_root_path . 'includes/constants.' . $phpEx);
4c79b5
4c79b5
	$db = new $sql_db();
4c79b5
	$cache = new cache();
4c79b5
4c79b5
	// Connect to DB
4c79b5
	if (!@$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, false))
4c79b5
	{
4c79b5
		exit;
4c79b5
	}
4c79b5
	unset($dbpasswd);
4c79b5
4c79b5
	// worst-case default
4c79b5
	$browser = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : 'msie 6.0';
4c79b5
4c79b5
	$config = $cache->obtain_config();
4c79b5
	$filename = $_GET['avatar'];
4c79b5
	$avatar_group = false;
4c79b5
	$exit = false;
4c79b5
	
4c79b5
	if ($filename[0] === 'g')
4c79b5
	{
4c79b5
		$avatar_group = true;
4c79b5
		$filename = substr($filename, 1);
4c79b5
	}
4c79b5
4c79b5
	// '==' is not a bug - . as the first char is as bad as no dot at all
4c79b5
	if (strpos($filename, '.') == false)
4c79b5
	{
4c79b5
		header('HTTP/1.0 403 Forbidden');
4c79b5
		$exit = true;
4c79b5
	}
4c79b5
4c79b5
	if (!$exit)
4c79b5
	{
4c79b5
		$ext		= substr(strrchr($filename, '.'), 1);
4c79b5
		$stamp		= (int) substr(stristr($filename, '_'), 1);
4c79b5
		$filename	= (int) $filename;
4c79b5
		$exit = set_modified_headers($stamp, $browser);
4c79b5
	}
4c79b5
	if (!$exit && !in_array($ext, array('png', 'gif', 'jpg', 'jpeg')))
4c79b5
	{
4c79b5
		// no way such an avatar could exist. They are not following the rules, stop the show.
4c79b5
		header("HTTP/1.0 403 Forbidden");
4c79b5
		$exit = true;
4c79b5
	}
4c79b5
	
4c79b5
	
4c79b5
	if (!$exit)
4c79b5
	{
4c79b5
		if (!$filename)
4c79b5
		{
4c79b5
			// no way such an avatar could exist. They are not following the rules, stop the show.
4c79b5
			header("HTTP/1.0 403 Forbidden");
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			send_avatar_to_browser(($avatar_group ? 'g' : '') . $filename . '.' . $ext, $browser);
4c79b5
		}
4c79b5
	}
4c79b5
	file_gc();
4c79b5
}
4c79b5
4c79b5
// implicit else: we are not in avatar mode
4c79b5
include($phpbb_root_path . 'common.' . $phpEx);
4c79b5
4c79b5
$download_id = request_var('id', 0);
4c79b5
$mode = request_var('mode', '');
4c79b5
$thumbnail = request_var('t', false);
4c79b5
4c79b5
// Start session management, do not update session page.
4c79b5
$user->session_begin(false);
4c79b5
$auth->acl($user->data);
4c79b5
$user->setup('viewtopic');
4c79b5
4c79b5
if (!$download_id)
4c79b5
{
4c79b5
	trigger_error('NO_ATTACHMENT_SELECTED');
4c79b5
}
4c79b5
4c79b5
if (!$config['allow_attachments'] && !$config['allow_pm_attach'])
4c79b5
{
4c79b5
	trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED');
4c79b5
}
4c79b5
4c79b5
$sql = 'SELECT attach_id, in_message, post_msg_id, extension, is_orphan, poster_id, filetime
4c79b5
	FROM ' . ATTACHMENTS_TABLE . "
4c79b5
	WHERE attach_id = $download_id";
4c79b5
$result = $db->sql_query_limit($sql, 1);
4c79b5
$attachment = $db->sql_fetchrow($result);
4c79b5
$db->sql_freeresult($result);
4c79b5
4c79b5
if (!$attachment)
4c79b5
{
4c79b5
	trigger_error('ERROR_NO_ATTACHMENT');
4c79b5
}
4c79b5
4c79b5
if ((!$attachment['in_message'] && !$config['allow_attachments']) || ($attachment['in_message'] && !$config['allow_pm_attach']))
4c79b5
{
4c79b5
	trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED');
4c79b5
}
4c79b5
4c79b5
$row = array();
4c79b5
4c79b5
if ($attachment['is_orphan'])
4c79b5
{
4c79b5
	// We allow admins having attachment permissions to see orphan attachments...
4c79b5
	$own_attachment = ($auth->acl_get('a_attach') || $attachment['poster_id'] == $user->data['user_id']) ? true : false;
4c79b5
4c79b5
	if (!$own_attachment || ($attachment['in_message'] && !$auth->acl_get('u_pm_download')) || (!$attachment['in_message'] && !$auth->acl_get('u_download')))
4c79b5
	{
4c79b5
		trigger_error('ERROR_NO_ATTACHMENT');
4c79b5
	}
4c79b5
4c79b5
	// Obtain all extensions...
4c79b5
	$extensions = $cache->obtain_attach_extensions(true);
4c79b5
}
4c79b5
else
4c79b5
{
4c79b5
	if (!$attachment['in_message'])
4c79b5
	{
4c79b5
		//
4c79b5
		$sql = 'SELECT p.forum_id, f.forum_password, f.parent_id
4c79b5
			FROM ' . POSTS_TABLE . ' p, ' . FORUMS_TABLE . ' f
4c79b5
			WHERE p.post_id = ' . $attachment['post_msg_id'] . '
4c79b5
				AND p.forum_id = f.forum_id';
4c79b5
		$result = $db->sql_query_limit($sql, 1);
4c79b5
		$row = $db->sql_fetchrow($result);
4c79b5
		$db->sql_freeresult($result);
4c79b5
4c79b5
		// Global announcement?
4c79b5
		$f_download = (!$row) ? $auth->acl_getf_global('f_download') : $auth->acl_get('f_download', $row['forum_id']);
4c79b5
4c79b5
		if ($auth->acl_get('u_download') && $f_download)
4c79b5
		{
4c79b5
			if ($row && $row['forum_password'])
4c79b5
			{
4c79b5
				// Do something else ... ?
4c79b5
				login_forum_box($row);
4c79b5
			}
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			trigger_error('SORRY_AUTH_VIEW_ATTACH');
4c79b5
		}
4c79b5
	}
4c79b5
	else
4c79b5
	{
4c79b5
		$row['forum_id'] = false;
4c79b5
		if (!$auth->acl_get('u_pm_download'))
4c79b5
		{
4c79b5
			header('HTTP/1.0 403 Forbidden');
4c79b5
			trigger_error('SORRY_AUTH_VIEW_ATTACH');
4c79b5
		}
4c79b5
4c79b5
		// Check if the attachment is within the users scope...
4c79b5
		$sql = 'SELECT user_id, author_id
4c79b5
			FROM ' . PRIVMSGS_TO_TABLE . '
4c79b5
			WHERE msg_id = ' . $attachment['post_msg_id'];
4c79b5
		$result = $db->sql_query($sql);
4c79b5
4c79b5
		$allowed = false;
4c79b5
		while ($user_row = $db->sql_fetchrow($result))
4c79b5
		{
4c79b5
			if ($user->data['user_id'] == $user_row['user_id'] || $user->data['user_id'] == $user_row['author_id'])
4c79b5
			{
4c79b5
				$allowed = true;
4c79b5
				break;
4c79b5
			}
4c79b5
		}
4c79b5
		$db->sql_freeresult($result);
4c79b5
4c79b5
		if (!$allowed)
4c79b5
		{
4c79b5
			header('HTTP/1.0 403 Forbidden');
4c79b5
			trigger_error('ERROR_NO_ATTACHMENT');
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	// disallowed?
4c79b5
	$extensions = array();
4c79b5
	if (!extension_allowed($row['forum_id'], $attachment['extension'], $extensions))
4c79b5
	{
4c79b5
		trigger_error(sprintf($user->lang['EXTENSION_DISABLED_AFTER_POSTING'], $attachment['extension']));
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
if (!download_allowed())
4c79b5
{
4c79b5
	header('HTTP/1.0 403 Forbidden');
4c79b5
	trigger_error($user->lang['LINKAGE_FORBIDDEN']);
4c79b5
}
4c79b5
4c79b5
$download_mode = (int) $extensions[$attachment['extension']]['download_mode'];
4c79b5
4c79b5
// Fetching filename here to prevent sniffing of filename
4c79b5
$sql = 'SELECT attach_id, is_orphan, in_message, post_msg_id, extension, physical_filename, real_filename, mimetype, filetime
4c79b5
	FROM ' . ATTACHMENTS_TABLE . "
4c79b5
	WHERE attach_id = $download_id";
4c79b5
$result = $db->sql_query_limit($sql, 1);
4c79b5
$attachment = $db->sql_fetchrow($result);
4c79b5
$db->sql_freeresult($result);
4c79b5
4c79b5
if (!$attachment)
4c79b5
{
4c79b5
	trigger_error('ERROR_NO_ATTACHMENT');
4c79b5
}
4c79b5
4c79b5
$attachment['physical_filename'] = basename($attachment['physical_filename']);
4c79b5
$display_cat = $extensions[$attachment['extension']]['display_cat'];
4c79b5
4c79b5
if (($display_cat == ATTACHMENT_CATEGORY_IMAGE || $display_cat == ATTACHMENT_CATEGORY_THUMB) && !$user->optionget('viewimg'))
4c79b5
{
4c79b5
	$display_cat = ATTACHMENT_CATEGORY_NONE;
4c79b5
}
4c79b5
4c79b5
if ($display_cat == ATTACHMENT_CATEGORY_FLASH && !$user->optionget('viewflash'))
4c79b5
{
4c79b5
	$display_cat = ATTACHMENT_CATEGORY_NONE;
4c79b5
}
4c79b5
4c79b5
if ($thumbnail)
4c79b5
{
4c79b5
	$attachment['physical_filename'] = 'thumb_' . $attachment['physical_filename'];
4c79b5
}
4c79b5
else if (($display_cat == ATTACHMENT_CATEGORY_NONE || $display_cat == ATTACHMENT_CATEGORY_IMAGE) && !$attachment['is_orphan'])
4c79b5
{
4c79b5
	// Update download count
4c79b5
	$sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
4c79b5
		SET download_count = download_count + 1
4c79b5
		WHERE attach_id = ' . $attachment['attach_id'];
4c79b5
	$db->sql_query($sql);
4c79b5
}
4c79b5
4c79b5
if ($display_cat == ATTACHMENT_CATEGORY_IMAGE && $mode === 'view' && (strpos($attachment['mimetype'], 'image') === 0) && ((strpos(strtolower($user->browser), 'msie') !== false) && (strpos(strtolower($user->browser), 'msie 8.0') === false)))
4c79b5
{
4c79b5
	wrap_img_in_html(append_sid($phpbb_root_path . 'download/file.' . $phpEx, 'id=' . $attachment['attach_id']), $attachment['real_filename']);
4c79b5
}
4c79b5
else
4c79b5
{
4c79b5
	// Determine the 'presenting'-method
4c79b5
	if ($download_mode == PHYSICAL_LINK)
4c79b5
	{
4c79b5
		// This presenting method should no longer be used
4c79b5
		if (!@is_dir($phpbb_root_path . $config['upload_path']))
4c79b5
		{
4c79b5
			trigger_error($user->lang['PHYSICAL_DOWNLOAD_NOT_POSSIBLE']);
4c79b5
		}
4c79b5
4c79b5
		redirect($phpbb_root_path . $config['upload_path'] . '/' . $attachment['physical_filename']);
4c79b5
		file_gc();
4c79b5
	}
4c79b5
	else
4c79b5
	{
4c79b5
		send_file_to_browser($attachment, $config['upload_path'], $display_cat);
4c79b5
		file_gc();
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
4c79b5
/**
4c79b5
* A simplified function to deliver avatars
4c79b5
* The argument needs to be checked before calling this function.
4c79b5
*/
4c79b5
function send_avatar_to_browser($file, $browser)
4c79b5
{
4c79b5
	global $config, $phpbb_root_path;
4c79b5
4c79b5
	$prefix = $config['avatar_salt'] . '_';
4c79b5
	$image_dir = $config['avatar_path'];
4c79b5
4c79b5
	// Adjust image_dir path (no trailing slash)
4c79b5
	if (substr($image_dir, -1, 1) == '/' || substr($image_dir, -1, 1) == '\\')
4c79b5
	{
4c79b5
		$image_dir = substr($image_dir, 0, -1) . '/';
4c79b5
	}
4c79b5
	$image_dir = str_replace(array('../', '..\\', './', '.\\'), '', $image_dir);
4c79b5
4c79b5
	if ($image_dir && ($image_dir[0] == '/' || $image_dir[0] == '\\'))
4c79b5
	{
4c79b5
		$image_dir = '';
4c79b5
	}
4c79b5
	$file_path = $phpbb_root_path . $image_dir . '/' . $prefix . $file;
4c79b5
4c79b5
	if ((@file_exists($file_path) && @is_readable($file_path)) && !headers_sent())
4c79b5
	{
4c79b5
		header('Pragma: public');
4c79b5
4c79b5
		$image_data = @getimagesize($file_path);
4c79b5
		header('Content-Type: ' . image_type_to_mime_type($image_data[2]));
4c79b5
4c79b5
		if (strpos(strtolower($browser), 'msie') !== false && strpos(strtolower($browser), 'msie 8.0') === false)
4c79b5
		{
4c79b5
			header('Content-Disposition: attachment; ' . header_filename($file));
4c79b5
4c79b5
			if (strpos(strtolower($browser), 'msie 6.0') !== false)
4c79b5
			{
4c79b5
				header('Expires: -1');
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 31536000));
4c79b5
			}
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			header('Content-Disposition: inline; ' . header_filename($file));
4c79b5
			header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 31536000));
4c79b5
		}
4c79b5
4c79b5
		$size = @filesize($file_path);
4c79b5
		if ($size)
4c79b5
		{
4c79b5
			header("Content-Length: $size");
4c79b5
		}
4c79b5
4c79b5
		if (@readfile($file_path) == false)
4c79b5
		{
4c79b5
			$fp = @fopen($file_path, 'rb');
4c79b5
4c79b5
			if ($fp !== false)
4c79b5
			{
4c79b5
				while (!feof($fp))
4c79b5
				{
4c79b5
					echo fread($fp, 8192);
4c79b5
				}
4c79b5
				fclose($fp);
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		flush();
4c79b5
	}
4c79b5
	else
4c79b5
	{
4c79b5
		header('HTTP/1.0 404 not found');
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Wraps an url into a simple html page. Used to display attachments in IE.
4c79b5
* this is a workaround for now; might be moved to template system later
4c79b5
* direct any complaints to 1 Microsoft Way, Redmond
4c79b5
*/
4c79b5
function wrap_img_in_html($src, $title)
4c79b5
{
4c79b5
	echo '';
4c79b5
	echo '<html>';
4c79b5
	echo '<head>';
4c79b5
	echo '<meta http-equiv="content-type" content="text/html; charset=UTF-8" />';
4c79b5
	echo '<title>' . $title . '</title>';
4c79b5
	echo '</head>';
4c79b5
	echo '<body>';
4c79b5
	echo '
';
4c79b5
	echo '' . $title . '';
4c79b5
	echo '';
4c79b5
	echo '</body>';
4c79b5
	echo '</html>';
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Send file to browser
4c79b5
*/
4c79b5
function send_file_to_browser($attachment, $upload_dir, $category)
4c79b5
{
4c79b5
	global $user, $db, $config, $phpbb_root_path;
4c79b5
4c79b5
	$filename = $phpbb_root_path . $upload_dir . '/' . $attachment['physical_filename'];
4c79b5
4c79b5
	if (!@file_exists($filename))
4c79b5
	{
4c79b5
		trigger_error($user->lang['ERROR_NO_ATTACHMENT'] . '

' . sprintf($user->lang['FILE_NOT_FOUND_404'], $filename));
4c79b5
	}
4c79b5
4c79b5
	// Correct the mime type - we force application/octetstream for all files, except images
4c79b5
	// Please do not change this, it is a security precaution
4c79b5
	if ($category != ATTACHMENT_CATEGORY_IMAGE || strpos($attachment['mimetype'], 'image') !== 0)
4c79b5
	{
4c79b5
		$attachment['mimetype'] = (strpos(strtolower($user->browser), 'msie') !== false || strpos(strtolower($user->browser), 'opera') !== false) ? 'application/octetstream' : 'application/octet-stream';
4c79b5
	}
4c79b5
4c79b5
	if (@ob_get_length())
4c79b5
	{
4c79b5
		@ob_end_clean();
4c79b5
	}
4c79b5
4c79b5
	// Now send the File Contents to the Browser
4c79b5
	$size = @filesize($filename);
4c79b5
4c79b5
	// To correctly display further errors we need to make sure we are using the correct headers for both (unsetting content-length may not work)
4c79b5
4c79b5
	// Check if headers already sent or not able to get the file contents.
4c79b5
	if (headers_sent() || !@file_exists($filename) || !@is_readable($filename))
4c79b5
	{
4c79b5
		// PHP track_errors setting On?
4c79b5
		if (!empty($php_errormsg))
4c79b5
		{
4c79b5
			trigger_error($user->lang['UNABLE_TO_DELIVER_FILE'] . '
' . sprintf($user->lang['TRACKED_PHP_ERROR'], $php_errormsg));
4c79b5
		}
4c79b5
4c79b5
		trigger_error('UNABLE_TO_DELIVER_FILE');
4c79b5
	}
4c79b5
4c79b5
	// Now the tricky part... let's dance
4c79b5
	header('Pragma: public');
4c79b5
4c79b5
	/**
4c79b5
	* Commented out X-Sendfile support. To not expose the physical filename within the header if xsendfile is absent we need to look into methods of checking it's status.
4c79b5
	*
4c79b5
	* Try X-Sendfile since it is much more server friendly - only works if the path is *not* outside of the root path...
4c79b5
	* lighttpd has core support for it. An apache2 module is available at http://celebnamer.celebworld.ws/stuff/mod_xsendfile/
4c79b5
	*
4c79b5
	* Not really ideal, but should work fine...
4c79b5
	* 
4c79b5
	*	if (strpos($upload_dir, '/') !== 0 && strpos($upload_dir, '../') === false)
4c79b5
	*	{
4c79b5
	*		header('X-Sendfile: ' . $filename);
4c79b5
	*	}
4c79b5
	* 
4c79b5
	*/
4c79b5
4c79b5
	// Send out the Headers. Do not set Content-Disposition to inline please, it is a security measure for users using the Internet Explorer.
4c79b5
	$is_ie8 = (strpos(strtolower($user->browser), 'msie 8.0') !== false);
4c79b5
	header('Content-Type: ' . $attachment['mimetype'] . (($is_ie8) ? '; authoritative=true;' : ''));
4c79b5
4c79b5
	if (empty($user->browser) || (!$is_ie8 && (strpos(strtolower($user->browser), 'msie') !== false)))
4c79b5
	{
4c79b5
		header('Content-Disposition: attachment; ' . header_filename(htmlspecialchars_decode($attachment['real_filename'])));
4c79b5
		if (empty($user->browser) || (strpos(strtolower($user->browser), 'msie 6.0') !== false))
4c79b5
		{
4c79b5
			header('expires: -1');
4c79b5
		}
4c79b5
	}
4c79b5
	else
4c79b5
	{
4c79b5
		header('Content-Disposition: ' . ((strpos($attachment['mimetype'], 'image') === 0) ? 'inline' : 'attachment') . '; ' . header_filename(htmlspecialchars_decode($attachment['real_filename'])));
4c79b5
		if ($is_ie8 && (strpos($attachment['mimetype'], 'image') !== 0))
4c79b5
		{
4c79b5
			header('X-Download-Options: noopen');
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	if ($size)
4c79b5
	{
4c79b5
		header("Content-Length: $size");
4c79b5
	}
4c79b5
4c79b5
	// Close the db connection before sending the file
4c79b5
	$db->sql_close();
4c79b5
4c79b5
	if (!set_modified_headers($attachment['filetime'], $user->browser))
4c79b5
	{
4c79b5
		// Try to deliver in chunks
4c79b5
		@set_time_limit(0);
4c79b5
4c79b5
		$fp = @fopen($filename, 'rb');
4c79b5
4c79b5
		if ($fp !== false)
4c79b5
		{
4c79b5
			while (!feof($fp))
4c79b5
			{
4c79b5
				echo fread($fp, 8192);
4c79b5
			}
4c79b5
			fclose($fp);
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			@readfile($filename);
4c79b5
		}
4c79b5
4c79b5
		flush();
4c79b5
	}
4c79b5
	file_gc();
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Get a browser friendly UTF-8 encoded filename
4c79b5
*/
4c79b5
function header_filename($file)
4c79b5
{
4c79b5
	$user_agent = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : '';
4c79b5
4c79b5
	// There be dragons here.
4c79b5
	// Not many follows the RFC...
4c79b5
	if (strpos($user_agent, 'MSIE') !== false || strpos($user_agent, 'Safari') !== false || strpos($user_agent, 'Konqueror') !== false)
4c79b5
	{
4c79b5
		return "filename=" . rawurlencode($file);
4c79b5
	}
4c79b5
4c79b5
	// follow the RFC for extended filename for the rest
4c79b5
	return "filename*=UTF-8''" . rawurlencode($file);
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Check if downloading item is allowed
4c79b5
*/
4c79b5
function download_allowed()
4c79b5
{
4c79b5
	global $config, $user, $db;
4c79b5
4c79b5
	if (!$config['secure_downloads'])
4c79b5
	{
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	$url = (!empty($_SERVER['HTTP_REFERER'])) ? trim($_SERVER['HTTP_REFERER']) : trim(getenv('HTTP_REFERER'));
4c79b5
4c79b5
	if (!$url)
4c79b5
	{
4c79b5
		return ($config['secure_allow_empty_referer']) ? true : false;
4c79b5
	}
4c79b5
4c79b5
	// Split URL into domain and script part
4c79b5
	$url = @parse_url($url);
4c79b5
4c79b5
	if ($url === false)
4c79b5
	{
4c79b5
		return ($config['secure_allow_empty_referer']) ? true : false;
4c79b5
	}
4c79b5
4c79b5
	$hostname = $url['host'];
4c79b5
	unset($url);
4c79b5
4c79b5
	$allowed = ($config['secure_allow_deny']) ? false : true;
4c79b5
	$iplist = array();
4c79b5
4c79b5
	if (($ip_ary = @gethostbynamel($hostname)) !== false)
4c79b5
	{
4c79b5
		foreach ($ip_ary as $ip)
4c79b5
		{
4c79b5
			if ($ip)
4c79b5
			{
4c79b5
				$iplist[] = $ip;
4c79b5
			}
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	// Check for own server...
4c79b5
	$server_name = $user->host;
4c79b5
4c79b5
	// Forcing server vars is the only way to specify/override the protocol
4c79b5
	if ($config['force_server_vars'] || !$server_name)
4c79b5
	{
4c79b5
		$server_name = $config['server_name'];
4c79b5
	}
4c79b5
4c79b5
	if (preg_match('#^.*?' . preg_quote($server_name, '#') . '.*?$#i', $hostname))
4c79b5
	{
4c79b5
		$allowed = true;
4c79b5
	}
4c79b5
4c79b5
	// Get IP's and Hostnames
4c79b5
	if (!$allowed)
4c79b5
	{
4c79b5
		$sql = 'SELECT site_ip, site_hostname, ip_exclude
4c79b5
			FROM ' . SITELIST_TABLE;
4c79b5
		$result = $db->sql_query($sql);
4c79b5
4c79b5
		while ($row = $db->sql_fetchrow($result))
4c79b5
		{
4c79b5
			$site_ip = trim($row['site_ip']);
4c79b5
			$site_hostname = trim($row['site_hostname']);
4c79b5
4c79b5
			if ($site_ip)
4c79b5
			{
4c79b5
				foreach ($iplist as $ip)
4c79b5
				{
4c79b5
					if (preg_match('#^' . str_replace('\*', '.*?', preg_quote($site_ip, '#')) . '$#i', $ip))
4c79b5
					{
4c79b5
						if ($row['ip_exclude'])
4c79b5
						{
4c79b5
							$allowed = ($config['secure_allow_deny']) ? false : true;
4c79b5
							break 2;
4c79b5
						}
4c79b5
						else
4c79b5
						{
4c79b5
							$allowed = ($config['secure_allow_deny']) ? true : false;
4c79b5
						}
4c79b5
					}
4c79b5
				}
4c79b5
			}
4c79b5
4c79b5
			if ($site_hostname)
4c79b5
			{
4c79b5
				if (preg_match('#^' . str_replace('\*', '.*?', preg_quote($site_hostname, '#')) . '$#i', $hostname))
4c79b5
				{
4c79b5
					if ($row['ip_exclude'])
4c79b5
					{
4c79b5
						$allowed = ($config['secure_allow_deny']) ? false : true;
4c79b5
						break;
4c79b5
					}
4c79b5
					else
4c79b5
					{
4c79b5
						$allowed = ($config['secure_allow_deny']) ? true : false;
4c79b5
					}
4c79b5
				}
4c79b5
			}
4c79b5
		}
4c79b5
		$db->sql_freeresult($result);
4c79b5
	}
4c79b5
4c79b5
	return $allowed;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Check if the browser has the file already and set the appropriate headers-
4c79b5
* @returns false if a resend is in order.
4c79b5
*/
4c79b5
function set_modified_headers($stamp, $browser)
4c79b5
{
4c79b5
	// let's see if we have to send the file at all
4c79b5
	$last_load 	=  isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime(trim($_SERVER['HTTP_IF_MODIFIED_SINCE'])) : false;
4c79b5
	if ((strpos(strtolower($browser), 'msie 6.0') === false) && (strpos(strtolower($browser), 'msie 8.0') === false))
4c79b5
	{
4c79b5
		if ($last_load !== false && $last_load <= $stamp)
4c79b5
		{
4c79b5
			if (@php_sapi_name() === 'CGI')
4c79b5
			{
4c79b5
				header('Status: 304 Not Modified', true, 304);
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				header('HTTP/1.0 304 Not Modified', true, 304);
4c79b5
			}
4c79b5
			// seems that we need those too ... browsers
4c79b5
			header('Pragma: public');
4c79b5
			header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 31536000));
4c79b5
			return true;
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $stamp) . ' GMT');
4c79b5
		}
4c79b5
	}
4c79b5
	return false;
4c79b5
}
4c79b5
4c79b5
function file_gc()
4c79b5
{
4c79b5
	global $cache, $db;
4c79b5
	if (!empty($cache))
4c79b5
	{
4c79b5
		$cache->unload();
4c79b5
	}
4c79b5
	$db->sql_close();
4c79b5
	exit;
4c79b5
}
4c79b5
4c79b5
?>