download/file.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/download/file.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,712 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package phpBB3
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +define('IN_PHPBB', true);
    1.18 +$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../';
    1.19 +$phpEx = substr(strrchr(__FILE__, '.'), 1);
    1.20 +
    1.21 +
    1.22 +// Thank you sun.
    1.23 +if (isset($_SERVER['CONTENT_TYPE']))
    1.24 +{
    1.25 +	if ($_SERVER['CONTENT_TYPE'] === 'application/x-java-archive')
    1.26 +	{
    1.27 +		exit;
    1.28 +	}
    1.29 +}
    1.30 +else if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Java') !== false)
    1.31 +{
    1.32 +	exit;
    1.33 +}
    1.34 +
    1.35 +if (isset($_GET['avatar']))
    1.36 +{
    1.37 +	require($phpbb_root_path . 'includes/startup.' . $phpEx);
    1.38 +	require($phpbb_root_path . 'config.' . $phpEx);
    1.39 +
    1.40 +	if (!defined('PHPBB_INSTALLED') || empty($dbms) || empty($acm_type))
    1.41 +	{
    1.42 +		exit;
    1.43 +	}
    1.44 +
    1.45 +	require($phpbb_root_path . 'includes/acm/acm_' . $acm_type . '.' . $phpEx);
    1.46 +	require($phpbb_root_path . 'includes/cache.' . $phpEx);
    1.47 +	require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
    1.48 +	require($phpbb_root_path . 'includes/constants.' . $phpEx);
    1.49 +	require($phpbb_root_path . 'includes/functions.' . $phpEx);
    1.50 +
    1.51 +	$db = new $sql_db();
    1.52 +	$cache = new cache();
    1.53 +
    1.54 +	// Connect to DB
    1.55 +	if (!@$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, false))
    1.56 +	{
    1.57 +		exit;
    1.58 +	}
    1.59 +	unset($dbpasswd);
    1.60 +
    1.61 +	// worst-case default
    1.62 +	$browser = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : 'msie 6.0';
    1.63 +
    1.64 +	$config = $cache->obtain_config();
    1.65 +	$filename = request_var('avatar', '');
    1.66 +	$avatar_group = false;
    1.67 +	$exit = false;
    1.68 +
    1.69 +	if (isset($filename[0]) && $filename[0] === 'g')
    1.70 +	{
    1.71 +		$avatar_group = true;
    1.72 +		$filename = substr($filename, 1);
    1.73 +	}
    1.74 +
    1.75 +	// '==' is not a bug - . as the first char is as bad as no dot at all
    1.76 +	if (strpos($filename, '.') == false)
    1.77 +	{
    1.78 +		send_status_line(403, 'Forbidden');
    1.79 +		$exit = true;
    1.80 +	}
    1.81 +
    1.82 +	if (!$exit)
    1.83 +	{
    1.84 +		$ext		= substr(strrchr($filename, '.'), 1);
    1.85 +		$stamp		= (int) substr(stristr($filename, '_'), 1);
    1.86 +		$filename	= (int) $filename;
    1.87 +		$exit = set_modified_headers($stamp, $browser);
    1.88 +	}
    1.89 +	if (!$exit && !in_array($ext, array('png', 'gif', 'jpg', 'jpeg')))
    1.90 +	{
    1.91 +		// no way such an avatar could exist. They are not following the rules, stop the show.
    1.92 +		send_status_line(403, 'Forbidden');
    1.93 +		$exit = true;
    1.94 +	}
    1.95 +
    1.96 +
    1.97 +	if (!$exit)
    1.98 +	{
    1.99 +		if (!$filename)
   1.100 +		{
   1.101 +			// no way such an avatar could exist. They are not following the rules, stop the show.
   1.102 +			send_status_line(403, 'Forbidden');
   1.103 +		}
   1.104 +		else
   1.105 +		{
   1.106 +			send_avatar_to_browser(($avatar_group ? 'g' : '') . $filename . '.' . $ext, $browser);
   1.107 +		}
   1.108 +	}
   1.109 +	file_gc();
   1.110 +}
   1.111 +
   1.112 +// implicit else: we are not in avatar mode
   1.113 +include($phpbb_root_path . 'common.' . $phpEx);
   1.114 +
   1.115 +$download_id = request_var('id', 0);
   1.116 +$mode = request_var('mode', '');
   1.117 +$thumbnail = request_var('t', false);
   1.118 +
   1.119 +// Start session management, do not update session page.
   1.120 +$user->session_begin(false);
   1.121 +$auth->acl($user->data);
   1.122 +$user->setup('viewtopic');
   1.123 +
   1.124 +if (!$download_id)
   1.125 +{
   1.126 +	send_status_line(404, 'Not Found');
   1.127 +	trigger_error('NO_ATTACHMENT_SELECTED');
   1.128 +}
   1.129 +
   1.130 +if (!$config['allow_attachments'] && !$config['allow_pm_attach'])
   1.131 +{
   1.132 +	send_status_line(404, 'Not Found');
   1.133 +	trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED');
   1.134 +}
   1.135 +
   1.136 +$sql = 'SELECT attach_id, in_message, post_msg_id, extension, is_orphan, poster_id, filetime
   1.137 +	FROM ' . ATTACHMENTS_TABLE . "
   1.138 +	WHERE attach_id = $download_id";
   1.139 +$result = $db->sql_query_limit($sql, 1);
   1.140 +$attachment = $db->sql_fetchrow($result);
   1.141 +$db->sql_freeresult($result);
   1.142 +
   1.143 +if (!$attachment)
   1.144 +{
   1.145 +	send_status_line(404, 'Not Found');
   1.146 +	trigger_error('ERROR_NO_ATTACHMENT');
   1.147 +}
   1.148 +
   1.149 +if ((!$attachment['in_message'] && !$config['allow_attachments']) || ($attachment['in_message'] && !$config['allow_pm_attach']))
   1.150 +{
   1.151 +	send_status_line(404, 'Not Found');
   1.152 +	trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED');
   1.153 +}
   1.154 +
   1.155 +$row = array();
   1.156 +
   1.157 +if ($attachment['is_orphan'])
   1.158 +{
   1.159 +	// We allow admins having attachment permissions to see orphan attachments...
   1.160 +	$own_attachment = ($auth->acl_get('a_attach') || $attachment['poster_id'] == $user->data['user_id']) ? true : false;
   1.161 +
   1.162 +	if (!$own_attachment || ($attachment['in_message'] && !$auth->acl_get('u_pm_download')) || (!$attachment['in_message'] && !$auth->acl_get('u_download')))
   1.163 +	{
   1.164 +		send_status_line(404, 'Not Found');
   1.165 +		trigger_error('ERROR_NO_ATTACHMENT');
   1.166 +	}
   1.167 +
   1.168 +	// Obtain all extensions...
   1.169 +	$extensions = $cache->obtain_attach_extensions(true);
   1.170 +}
   1.171 +else
   1.172 +{
   1.173 +	if (!$attachment['in_message'])
   1.174 +	{
   1.175 +		//
   1.176 +		$sql = 'SELECT p.forum_id, f.forum_password, f.parent_id
   1.177 +			FROM ' . POSTS_TABLE . ' p, ' . FORUMS_TABLE . ' f
   1.178 +			WHERE p.post_id = ' . $attachment['post_msg_id'] . '
   1.179 +				AND p.forum_id = f.forum_id';
   1.180 +		$result = $db->sql_query_limit($sql, 1);
   1.181 +		$row = $db->sql_fetchrow($result);
   1.182 +		$db->sql_freeresult($result);
   1.183 +
   1.184 +		// Global announcement?
   1.185 +		$f_download = (!$row) ? $auth->acl_getf_global('f_download') : $auth->acl_get('f_download', $row['forum_id']);
   1.186 +
   1.187 +		if ($auth->acl_get('u_download') && $f_download)
   1.188 +		{
   1.189 +			if ($row && $row['forum_password'])
   1.190 +			{
   1.191 +				// Do something else ... ?
   1.192 +				login_forum_box($row);
   1.193 +			}
   1.194 +		}
   1.195 +		else
   1.196 +		{
   1.197 +			send_status_line(403, 'Forbidden');
   1.198 +			trigger_error('SORRY_AUTH_VIEW_ATTACH');
   1.199 +		}
   1.200 +	}
   1.201 +	else
   1.202 +	{
   1.203 +		$row['forum_id'] = false;
   1.204 +		if (!$auth->acl_get('u_pm_download'))
   1.205 +		{
   1.206 +			send_status_line(403, 'Forbidden');
   1.207 +			trigger_error('SORRY_AUTH_VIEW_ATTACH');
   1.208 +		}
   1.209 +
   1.210 +		// Check if the attachment is within the users scope...
   1.211 +		$sql = 'SELECT user_id, author_id
   1.212 +			FROM ' . PRIVMSGS_TO_TABLE . '
   1.213 +			WHERE msg_id = ' . $attachment['post_msg_id'];
   1.214 +		$result = $db->sql_query($sql);
   1.215 +
   1.216 +		$allowed = false;
   1.217 +		while ($user_row = $db->sql_fetchrow($result))
   1.218 +		{
   1.219 +			if ($user->data['user_id'] == $user_row['user_id'] || $user->data['user_id'] == $user_row['author_id'])
   1.220 +			{
   1.221 +				$allowed = true;
   1.222 +				break;
   1.223 +			}
   1.224 +		}
   1.225 +		$db->sql_freeresult($result);
   1.226 +
   1.227 +		if (!$allowed)
   1.228 +		{
   1.229 +			send_status_line(403, 'Forbidden');
   1.230 +			trigger_error('ERROR_NO_ATTACHMENT');
   1.231 +		}
   1.232 +	}
   1.233 +
   1.234 +	// disallowed?
   1.235 +	$extensions = array();
   1.236 +	if (!extension_allowed($row['forum_id'], $attachment['extension'], $extensions))
   1.237 +	{
   1.238 +		send_status_line(404, 'Forbidden');
   1.239 +		trigger_error(sprintf($user->lang['EXTENSION_DISABLED_AFTER_POSTING'], $attachment['extension']));
   1.240 +	}
   1.241 +}
   1.242 +
   1.243 +if (!download_allowed())
   1.244 +{
   1.245 +	send_status_line(403, 'Forbidden');
   1.246 +	trigger_error($user->lang['LINKAGE_FORBIDDEN']);
   1.247 +}
   1.248 +
   1.249 +$download_mode = (int) $extensions[$attachment['extension']]['download_mode'];
   1.250 +
   1.251 +// Fetching filename here to prevent sniffing of filename
   1.252 +$sql = 'SELECT attach_id, is_orphan, in_message, post_msg_id, extension, physical_filename, real_filename, mimetype, filetime
   1.253 +	FROM ' . ATTACHMENTS_TABLE . "
   1.254 +	WHERE attach_id = $download_id";
   1.255 +$result = $db->sql_query_limit($sql, 1);
   1.256 +$attachment = $db->sql_fetchrow($result);
   1.257 +$db->sql_freeresult($result);
   1.258 +
   1.259 +if (!$attachment)
   1.260 +{
   1.261 +	send_status_line(404, 'Not Found');
   1.262 +	trigger_error('ERROR_NO_ATTACHMENT');
   1.263 +}
   1.264 +
   1.265 +$attachment['physical_filename'] = utf8_basename($attachment['physical_filename']);
   1.266 +$display_cat = $extensions[$attachment['extension']]['display_cat'];
   1.267 +
   1.268 +if (($display_cat == ATTACHMENT_CATEGORY_IMAGE || $display_cat == ATTACHMENT_CATEGORY_THUMB) && !$user->optionget('viewimg'))
   1.269 +{
   1.270 +	$display_cat = ATTACHMENT_CATEGORY_NONE;
   1.271 +}
   1.272 +
   1.273 +if ($display_cat == ATTACHMENT_CATEGORY_FLASH && !$user->optionget('viewflash'))
   1.274 +{
   1.275 +	$display_cat = ATTACHMENT_CATEGORY_NONE;
   1.276 +}
   1.277 +
   1.278 +if ($thumbnail)
   1.279 +{
   1.280 +	$attachment['physical_filename'] = 'thumb_' . $attachment['physical_filename'];
   1.281 +}
   1.282 +else if (($display_cat == ATTACHMENT_CATEGORY_NONE/* || $display_cat == ATTACHMENT_CATEGORY_IMAGE*/) && !$attachment['is_orphan'])
   1.283 +{
   1.284 +	// Update download count
   1.285 +	$sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
   1.286 +		SET download_count = download_count + 1
   1.287 +		WHERE attach_id = ' . $attachment['attach_id'];
   1.288 +	$db->sql_query($sql);
   1.289 +}
   1.290 +
   1.291 +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)))
   1.292 +{
   1.293 +	wrap_img_in_html(append_sid($phpbb_root_path . 'download/file.' . $phpEx, 'id=' . $attachment['attach_id']), $attachment['real_filename']);
   1.294 +	file_gc();
   1.295 +}
   1.296 +else
   1.297 +{
   1.298 +	// Determine the 'presenting'-method
   1.299 +	if ($download_mode == PHYSICAL_LINK)
   1.300 +	{
   1.301 +		// This presenting method should no longer be used
   1.302 +		if (!@is_dir($phpbb_root_path . $config['upload_path']))
   1.303 +		{
   1.304 +			send_status_line(500, 'Internal Server Error');
   1.305 +			trigger_error($user->lang['PHYSICAL_DOWNLOAD_NOT_POSSIBLE']);
   1.306 +		}
   1.307 +
   1.308 +		redirect($phpbb_root_path . $config['upload_path'] . '/' . $attachment['physical_filename']);
   1.309 +		file_gc();
   1.310 +	}
   1.311 +	else
   1.312 +	{
   1.313 +		send_file_to_browser($attachment, $config['upload_path'], $display_cat);
   1.314 +		file_gc();
   1.315 +	}
   1.316 +}
   1.317 +
   1.318 +
   1.319 +/**
   1.320 +* A simplified function to deliver avatars
   1.321 +* The argument needs to be checked before calling this function.
   1.322 +*/
   1.323 +function send_avatar_to_browser($file, $browser)
   1.324 +{
   1.325 +	global $config, $phpbb_root_path;
   1.326 +
   1.327 +	$prefix = $config['avatar_salt'] . '_';
   1.328 +	$image_dir = $config['avatar_path'];
   1.329 +
   1.330 +	// Adjust image_dir path (no trailing slash)
   1.331 +	if (substr($image_dir, -1, 1) == '/' || substr($image_dir, -1, 1) == '\\')
   1.332 +	{
   1.333 +		$image_dir = substr($image_dir, 0, -1) . '/';
   1.334 +	}
   1.335 +	$image_dir = str_replace(array('../', '..\\', './', '.\\'), '', $image_dir);
   1.336 +
   1.337 +	if ($image_dir && ($image_dir[0] == '/' || $image_dir[0] == '\\'))
   1.338 +	{
   1.339 +		$image_dir = '';
   1.340 +	}
   1.341 +	$file_path = $phpbb_root_path . $image_dir . '/' . $prefix . $file;
   1.342 +
   1.343 +	if ((@file_exists($file_path) && @is_readable($file_path)) && !headers_sent())
   1.344 +	{
   1.345 +		header('Pragma: public');
   1.346 +
   1.347 +		$image_data = @getimagesize($file_path);
   1.348 +		header('Content-Type: ' . image_type_to_mime_type($image_data[2]));
   1.349 +
   1.350 +		if (strpos(strtolower($browser), 'msie') !== false && strpos(strtolower($browser), 'msie 8.0') === false)
   1.351 +		{
   1.352 +			header('Content-Disposition: attachment; ' . header_filename($file));
   1.353 +
   1.354 +			if (strpos(strtolower($browser), 'msie 6.0') !== false)
   1.355 +			{
   1.356 +				header('Expires: -1');
   1.357 +			}
   1.358 +			else
   1.359 +			{
   1.360 +				header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 31536000));
   1.361 +			}
   1.362 +		}
   1.363 +		else
   1.364 +		{
   1.365 +			header('Content-Disposition: inline; ' . header_filename($file));
   1.366 +			header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 31536000));
   1.367 +		}
   1.368 +
   1.369 +		$size = @filesize($file_path);
   1.370 +		if ($size)
   1.371 +		{
   1.372 +			header("Content-Length: $size");
   1.373 +		}
   1.374 +
   1.375 +		if (@readfile($file_path) == false)
   1.376 +		{
   1.377 +			$fp = @fopen($file_path, 'rb');
   1.378 +
   1.379 +			if ($fp !== false)
   1.380 +			{
   1.381 +				while (!feof($fp))
   1.382 +				{
   1.383 +					echo fread($fp, 8192);
   1.384 +				}
   1.385 +				fclose($fp);
   1.386 +			}
   1.387 +		}
   1.388 +
   1.389 +		flush();
   1.390 +	}
   1.391 +	else
   1.392 +	{
   1.393 +		send_status_line(404, 'Not Found');
   1.394 +	}
   1.395 +}
   1.396 +
   1.397 +/**
   1.398 +* Wraps an url into a simple html page. Used to display attachments in IE.
   1.399 +* this is a workaround for now; might be moved to template system later
   1.400 +* direct any complaints to 1 Microsoft Way, Redmond
   1.401 +*/
   1.402 +function wrap_img_in_html($src, $title)
   1.403 +{
   1.404 +	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Strict.dtd">';
   1.405 +	echo '<html>';
   1.406 +	echo '<head>';
   1.407 +	echo '<meta http-equiv="content-type" content="text/html; charset=UTF-8" />';
   1.408 +	echo '<title>' . $title . '</title>';
   1.409 +	echo '</head>';
   1.410 +	echo '<body>';
   1.411 +	echo '<div>';
   1.412 +	echo '<img src="' . $src . '" alt="' . $title . '" />';
   1.413 +	echo '</div>';
   1.414 +	echo '</body>';
   1.415 +	echo '</html>';
   1.416 +}
   1.417 +
   1.418 +/**
   1.419 +* Send file to browser
   1.420 +*/
   1.421 +function send_file_to_browser($attachment, $upload_dir, $category)
   1.422 +{
   1.423 +	global $user, $db, $config, $phpbb_root_path;
   1.424 +
   1.425 +	$filename = $phpbb_root_path . $upload_dir . '/' . $attachment['physical_filename'];
   1.426 +
   1.427 +	if (!@file_exists($filename))
   1.428 +	{
   1.429 +		send_status_line(404, 'Not Found');
   1.430 +		trigger_error($user->lang['ERROR_NO_ATTACHMENT'] . '<br /><br />' . sprintf($user->lang['FILE_NOT_FOUND_404'], $filename));
   1.431 +	}
   1.432 +
   1.433 +	// Correct the mime type - we force application/octetstream for all files, except images
   1.434 +	// Please do not change this, it is a security precaution
   1.435 +	if ($category != ATTACHMENT_CATEGORY_IMAGE || strpos($attachment['mimetype'], 'image') !== 0)
   1.436 +	{
   1.437 +		$attachment['mimetype'] = (strpos(strtolower($user->browser), 'msie') !== false || strpos(strtolower($user->browser), 'opera') !== false) ? 'application/octetstream' : 'application/octet-stream';
   1.438 +	}
   1.439 +
   1.440 +	if (@ob_get_length())
   1.441 +	{
   1.442 +		@ob_end_clean();
   1.443 +	}
   1.444 +
   1.445 +	// Now send the File Contents to the Browser
   1.446 +	$size = @filesize($filename);
   1.447 +
   1.448 +	// To correctly display further errors we need to make sure we are using the correct headers for both (unsetting content-length may not work)
   1.449 +
   1.450 +	// Check if headers already sent or not able to get the file contents.
   1.451 +	if (headers_sent() || !@file_exists($filename) || !@is_readable($filename))
   1.452 +	{
   1.453 +		// PHP track_errors setting On?
   1.454 +		if (!empty($php_errormsg))
   1.455 +		{
   1.456 +			send_status_line(500, 'Internal Server Error');
   1.457 +			trigger_error($user->lang['UNABLE_TO_DELIVER_FILE'] . '<br />' . sprintf($user->lang['TRACKED_PHP_ERROR'], $php_errormsg));
   1.458 +		}
   1.459 +
   1.460 +		send_status_line(500, 'Internal Server Error');
   1.461 +		trigger_error('UNABLE_TO_DELIVER_FILE');
   1.462 +	}
   1.463 +
   1.464 +	// Now the tricky part... let's dance
   1.465 +	header('Pragma: public');
   1.466 +
   1.467 +	/**
   1.468 +	* 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.
   1.469 +	*
   1.470 +	* Try X-Sendfile since it is much more server friendly - only works if the path is *not* outside of the root path...
   1.471 +	* lighttpd has core support for it. An apache2 module is available at http://celebnamer.celebworld.ws/stuff/mod_xsendfile/
   1.472 +	*
   1.473 +	* Not really ideal, but should work fine...
   1.474 +	* <code>
   1.475 +	*	if (strpos($upload_dir, '/') !== 0 && strpos($upload_dir, '../') === false)
   1.476 +	*	{
   1.477 +	*		header('X-Sendfile: ' . $filename);
   1.478 +	*	}
   1.479 +	* </code>
   1.480 +	*/
   1.481 +
   1.482 +	// Send out the Headers. Do not set Content-Disposition to inline please, it is a security measure for users using the Internet Explorer.
   1.483 +	$is_ie8 = (strpos(strtolower($user->browser), 'msie 8.0') !== false);
   1.484 +	header('Content-Type: ' . $attachment['mimetype']);
   1.485 +
   1.486 +	if ($is_ie8)
   1.487 +	{
   1.488 +		header('X-Content-Type-Options: nosniff');
   1.489 +	}
   1.490 +
   1.491 +	if ($category == ATTACHMENT_CATEGORY_FLASH && request_var('view', 0) === 1)
   1.492 +	{
   1.493 +		// We use content-disposition: inline for flash files and view=1 to let it correctly play with flash player 10 - any other disposition will fail to play inline
   1.494 +		header('Content-Disposition: inline');
   1.495 +	}
   1.496 +	else
   1.497 +	{
   1.498 +		if (empty($user->browser) || (!$is_ie8 && (strpos(strtolower($user->browser), 'msie') !== false)))
   1.499 +		{
   1.500 +			header('Content-Disposition: attachment; ' . header_filename(htmlspecialchars_decode($attachment['real_filename'])));
   1.501 +			if (empty($user->browser) || (strpos(strtolower($user->browser), 'msie 6.0') !== false))
   1.502 +			{
   1.503 +				header('expires: -1');
   1.504 +			}
   1.505 +		}
   1.506 +		else
   1.507 +		{
   1.508 +			header('Content-Disposition: ' . ((strpos($attachment['mimetype'], 'image') === 0) ? 'inline' : 'attachment') . '; ' . header_filename(htmlspecialchars_decode($attachment['real_filename'])));
   1.509 +			if ($is_ie8 && (strpos($attachment['mimetype'], 'image') !== 0))
   1.510 +			{
   1.511 +				header('X-Download-Options: noopen');
   1.512 +			}
   1.513 +		}
   1.514 +	}
   1.515 +
   1.516 +	if ($size)
   1.517 +	{
   1.518 +		header("Content-Length: $size");
   1.519 +	}
   1.520 +
   1.521 +	// Close the db connection before sending the file
   1.522 +	$db->sql_close();
   1.523 +
   1.524 +	if (!set_modified_headers($attachment['filetime'], $user->browser))
   1.525 +	{
   1.526 +		// Try to deliver in chunks
   1.527 +		@set_time_limit(0);
   1.528 +
   1.529 +		$fp = @fopen($filename, 'rb');
   1.530 +
   1.531 +		if ($fp !== false)
   1.532 +		{
   1.533 +			while (!feof($fp))
   1.534 +			{
   1.535 +				echo fread($fp, 8192);
   1.536 +			}
   1.537 +			fclose($fp);
   1.538 +		}
   1.539 +		else
   1.540 +		{
   1.541 +			@readfile($filename);
   1.542 +		}
   1.543 +
   1.544 +		flush();
   1.545 +	}
   1.546 +	file_gc();
   1.547 +}
   1.548 +
   1.549 +/**
   1.550 +* Get a browser friendly UTF-8 encoded filename
   1.551 +*/
   1.552 +function header_filename($file)
   1.553 +{
   1.554 +	$user_agent = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : '';
   1.555 +
   1.556 +	// There be dragons here.
   1.557 +	// Not many follows the RFC...
   1.558 +	if (strpos($user_agent, 'MSIE') !== false || strpos($user_agent, 'Safari') !== false || strpos($user_agent, 'Konqueror') !== false)
   1.559 +	{
   1.560 +		return "filename=" . rawurlencode($file);
   1.561 +	}
   1.562 +
   1.563 +	// follow the RFC for extended filename for the rest
   1.564 +	return "filename*=UTF-8''" . rawurlencode($file);
   1.565 +}
   1.566 +
   1.567 +/**
   1.568 +* Check if downloading item is allowed
   1.569 +*/
   1.570 +function download_allowed()
   1.571 +{
   1.572 +	global $config, $user, $db;
   1.573 +
   1.574 +	if (!$config['secure_downloads'])
   1.575 +	{
   1.576 +		return true;
   1.577 +	}
   1.578 +
   1.579 +	$url = (!empty($_SERVER['HTTP_REFERER'])) ? trim($_SERVER['HTTP_REFERER']) : trim(getenv('HTTP_REFERER'));
   1.580 +
   1.581 +	if (!$url)
   1.582 +	{
   1.583 +		return ($config['secure_allow_empty_referer']) ? true : false;
   1.584 +	}
   1.585 +
   1.586 +	// Split URL into domain and script part
   1.587 +	$url = @parse_url($url);
   1.588 +
   1.589 +	if ($url === false)
   1.590 +	{
   1.591 +		return ($config['secure_allow_empty_referer']) ? true : false;
   1.592 +	}
   1.593 +
   1.594 +	$hostname = $url['host'];
   1.595 +	unset($url);
   1.596 +
   1.597 +	$allowed = ($config['secure_allow_deny']) ? false : true;
   1.598 +	$iplist = array();
   1.599 +
   1.600 +	if (($ip_ary = @gethostbynamel($hostname)) !== false)
   1.601 +	{
   1.602 +		foreach ($ip_ary as $ip)
   1.603 +		{
   1.604 +			if ($ip)
   1.605 +			{
   1.606 +				$iplist[] = $ip;
   1.607 +			}
   1.608 +		}
   1.609 +	}
   1.610 +
   1.611 +	// Check for own server...
   1.612 +	$server_name = $user->host;
   1.613 +
   1.614 +	// Forcing server vars is the only way to specify/override the protocol
   1.615 +	if ($config['force_server_vars'] || !$server_name)
   1.616 +	{
   1.617 +		$server_name = $config['server_name'];
   1.618 +	}
   1.619 +
   1.620 +	if (preg_match('#^.*?' . preg_quote($server_name, '#') . '.*?$#i', $hostname))
   1.621 +	{
   1.622 +		$allowed = true;
   1.623 +	}
   1.624 +
   1.625 +	// Get IP's and Hostnames
   1.626 +	if (!$allowed)
   1.627 +	{
   1.628 +		$sql = 'SELECT site_ip, site_hostname, ip_exclude
   1.629 +			FROM ' . SITELIST_TABLE;
   1.630 +		$result = $db->sql_query($sql);
   1.631 +
   1.632 +		while ($row = $db->sql_fetchrow($result))
   1.633 +		{
   1.634 +			$site_ip = trim($row['site_ip']);
   1.635 +			$site_hostname = trim($row['site_hostname']);
   1.636 +
   1.637 +			if ($site_ip)
   1.638 +			{
   1.639 +				foreach ($iplist as $ip)
   1.640 +				{
   1.641 +					if (preg_match('#^' . str_replace('\*', '.*?', preg_quote($site_ip, '#')) . '$#i', $ip))
   1.642 +					{
   1.643 +						if ($row['ip_exclude'])
   1.644 +						{
   1.645 +							$allowed = ($config['secure_allow_deny']) ? false : true;
   1.646 +							break 2;
   1.647 +						}
   1.648 +						else
   1.649 +						{
   1.650 +							$allowed = ($config['secure_allow_deny']) ? true : false;
   1.651 +						}
   1.652 +					}
   1.653 +				}
   1.654 +			}
   1.655 +
   1.656 +			if ($site_hostname)
   1.657 +			{
   1.658 +				if (preg_match('#^' . str_replace('\*', '.*?', preg_quote($site_hostname, '#')) . '$#i', $hostname))
   1.659 +				{
   1.660 +					if ($row['ip_exclude'])
   1.661 +					{
   1.662 +						$allowed = ($config['secure_allow_deny']) ? false : true;
   1.663 +						break;
   1.664 +					}
   1.665 +					else
   1.666 +					{
   1.667 +						$allowed = ($config['secure_allow_deny']) ? true : false;
   1.668 +					}
   1.669 +				}
   1.670 +			}
   1.671 +		}
   1.672 +		$db->sql_freeresult($result);
   1.673 +	}
   1.674 +
   1.675 +	return $allowed;
   1.676 +}
   1.677 +
   1.678 +/**
   1.679 +* Check if the browser has the file already and set the appropriate headers-
   1.680 +* @returns false if a resend is in order.
   1.681 +*/
   1.682 +function set_modified_headers($stamp, $browser)
   1.683 +{
   1.684 +	// let's see if we have to send the file at all
   1.685 +	$last_load 	=  isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime(trim($_SERVER['HTTP_IF_MODIFIED_SINCE'])) : false;
   1.686 +	if ((strpos(strtolower($browser), 'msie 6.0') === false) && (strpos(strtolower($browser), 'msie 8.0') === false))
   1.687 +	{
   1.688 +		if ($last_load !== false && $last_load >= $stamp)
   1.689 +		{
   1.690 +			send_status_line(304, 'Not Modified');
   1.691 +			// seems that we need those too ... browsers
   1.692 +			header('Pragma: public');
   1.693 +			header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 31536000));
   1.694 +			return true;
   1.695 +		}
   1.696 +		else
   1.697 +		{
   1.698 +			header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $stamp) . ' GMT');
   1.699 +		}
   1.700 +	}
   1.701 +	return false;
   1.702 +}
   1.703 +
   1.704 +function file_gc()
   1.705 +{
   1.706 +	global $cache, $db;
   1.707 +	if (!empty($cache))
   1.708 +	{
   1.709 +		$cache->unload();
   1.710 +	}
   1.711 +	$db->sql_close();
   1.712 +	exit;
   1.713 +}
   1.714 +
   1.715 +?>
   1.716 \ No newline at end of file