Blame Identity/Models/Html/phpBB/3.0.4/download/file.php

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

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