download/file.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Fri, 09 Mar 2012 16:11:37 +0400
changeset 12 898199d067e7
parent 0 ceef6d64c21d
permissions -rw-r--r--
Ignoring cache
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package phpBB3
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2005 phpBB Group
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
* @ignore
indvdum@0
    13
*/
indvdum@0
    14
define('IN_PHPBB', true);
indvdum@0
    15
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../';
indvdum@0
    16
$phpEx = substr(strrchr(__FILE__, '.'), 1);
indvdum@0
    17
indvdum@0
    18
indvdum@0
    19
// Thank you sun.
indvdum@0
    20
if (isset($_SERVER['CONTENT_TYPE']))
indvdum@0
    21
{
indvdum@0
    22
	if ($_SERVER['CONTENT_TYPE'] === 'application/x-java-archive')
indvdum@0
    23
	{
indvdum@0
    24
		exit;
indvdum@0
    25
	}
indvdum@0
    26
}
indvdum@0
    27
else if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Java') !== false)
indvdum@0
    28
{
indvdum@0
    29
	exit;
indvdum@0
    30
}
indvdum@0
    31
indvdum@0
    32
if (isset($_GET['avatar']))
indvdum@0
    33
{
indvdum@0
    34
	require($phpbb_root_path . 'includes/startup.' . $phpEx);
indvdum@0
    35
	require($phpbb_root_path . 'config.' . $phpEx);
indvdum@0
    36
indvdum@0
    37
	if (!defined('PHPBB_INSTALLED') || empty($dbms) || empty($acm_type))
indvdum@0
    38
	{
indvdum@0
    39
		exit;
indvdum@0
    40
	}
indvdum@0
    41
indvdum@0
    42
	require($phpbb_root_path . 'includes/acm/acm_' . $acm_type . '.' . $phpEx);
indvdum@0
    43
	require($phpbb_root_path . 'includes/cache.' . $phpEx);
indvdum@0
    44
	require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
indvdum@0
    45
	require($phpbb_root_path . 'includes/constants.' . $phpEx);
indvdum@0
    46
	require($phpbb_root_path . 'includes/functions.' . $phpEx);
indvdum@0
    47
indvdum@0
    48
	$db = new $sql_db();
indvdum@0
    49
	$cache = new cache();
indvdum@0
    50
indvdum@0
    51
	// Connect to DB
indvdum@0
    52
	if (!@$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, false))
indvdum@0
    53
	{
indvdum@0
    54
		exit;
indvdum@0
    55
	}
indvdum@0
    56
	unset($dbpasswd);
indvdum@0
    57
indvdum@0
    58
	// worst-case default
indvdum@0
    59
	$browser = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : 'msie 6.0';
indvdum@0
    60
indvdum@0
    61
	$config = $cache->obtain_config();
indvdum@0
    62
	$filename = request_var('avatar', '');
indvdum@0
    63
	$avatar_group = false;
indvdum@0
    64
	$exit = false;
indvdum@0
    65
indvdum@0
    66
	if (isset($filename[0]) && $filename[0] === 'g')
indvdum@0
    67
	{
indvdum@0
    68
		$avatar_group = true;
indvdum@0
    69
		$filename = substr($filename, 1);
indvdum@0
    70
	}
indvdum@0
    71
indvdum@0
    72
	// '==' is not a bug - . as the first char is as bad as no dot at all
indvdum@0
    73
	if (strpos($filename, '.') == false)
indvdum@0
    74
	{
indvdum@0
    75
		send_status_line(403, 'Forbidden');
indvdum@0
    76
		$exit = true;
indvdum@0
    77
	}
indvdum@0
    78
indvdum@0
    79
	if (!$exit)
indvdum@0
    80
	{
indvdum@0
    81
		$ext		= substr(strrchr($filename, '.'), 1);
indvdum@0
    82
		$stamp		= (int) substr(stristr($filename, '_'), 1);
indvdum@0
    83
		$filename	= (int) $filename;
indvdum@0
    84
		$exit = set_modified_headers($stamp, $browser);
indvdum@0
    85
	}
indvdum@0
    86
	if (!$exit && !in_array($ext, array('png', 'gif', 'jpg', 'jpeg')))
indvdum@0
    87
	{
indvdum@0
    88
		// no way such an avatar could exist. They are not following the rules, stop the show.
indvdum@0
    89
		send_status_line(403, 'Forbidden');
indvdum@0
    90
		$exit = true;
indvdum@0
    91
	}
indvdum@0
    92
indvdum@0
    93
indvdum@0
    94
	if (!$exit)
indvdum@0
    95
	{
indvdum@0
    96
		if (!$filename)
indvdum@0
    97
		{
indvdum@0
    98
			// no way such an avatar could exist. They are not following the rules, stop the show.
indvdum@0
    99
			send_status_line(403, 'Forbidden');
indvdum@0
   100
		}
indvdum@0
   101
		else
indvdum@0
   102
		{
indvdum@0
   103
			send_avatar_to_browser(($avatar_group ? 'g' : '') . $filename . '.' . $ext, $browser);
indvdum@0
   104
		}
indvdum@0
   105
	}
indvdum@0
   106
	file_gc();
indvdum@0
   107
}
indvdum@0
   108
indvdum@0
   109
// implicit else: we are not in avatar mode
indvdum@0
   110
include($phpbb_root_path . 'common.' . $phpEx);
indvdum@0
   111
indvdum@0
   112
$download_id = request_var('id', 0);
indvdum@0
   113
$mode = request_var('mode', '');
indvdum@0
   114
$thumbnail = request_var('t', false);
indvdum@0
   115
indvdum@0
   116
// Start session management, do not update session page.
indvdum@0
   117
$user->session_begin(false);
indvdum@0
   118
$auth->acl($user->data);
indvdum@0
   119
$user->setup('viewtopic');
indvdum@0
   120
indvdum@0
   121
if (!$download_id)
indvdum@0
   122
{
indvdum@0
   123
	send_status_line(404, 'Not Found');
indvdum@0
   124
	trigger_error('NO_ATTACHMENT_SELECTED');
indvdum@0
   125
}
indvdum@0
   126
indvdum@0
   127
if (!$config['allow_attachments'] && !$config['allow_pm_attach'])
indvdum@0
   128
{
indvdum@0
   129
	send_status_line(404, 'Not Found');
indvdum@0
   130
	trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED');
indvdum@0
   131
}
indvdum@0
   132
indvdum@0
   133
$sql = 'SELECT attach_id, in_message, post_msg_id, extension, is_orphan, poster_id, filetime
indvdum@0
   134
	FROM ' . ATTACHMENTS_TABLE . "
indvdum@0
   135
	WHERE attach_id = $download_id";
indvdum@0
   136
$result = $db->sql_query_limit($sql, 1);
indvdum@0
   137
$attachment = $db->sql_fetchrow($result);
indvdum@0
   138
$db->sql_freeresult($result);
indvdum@0
   139
indvdum@0
   140
if (!$attachment)
indvdum@0
   141
{
indvdum@0
   142
	send_status_line(404, 'Not Found');
indvdum@0
   143
	trigger_error('ERROR_NO_ATTACHMENT');
indvdum@0
   144
}
indvdum@0
   145
indvdum@0
   146
if ((!$attachment['in_message'] && !$config['allow_attachments']) || ($attachment['in_message'] && !$config['allow_pm_attach']))
indvdum@0
   147
{
indvdum@0
   148
	send_status_line(404, 'Not Found');
indvdum@0
   149
	trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED');
indvdum@0
   150
}
indvdum@0
   151
indvdum@0
   152
$row = array();
indvdum@0
   153
indvdum@0
   154
if ($attachment['is_orphan'])
indvdum@0
   155
{
indvdum@0
   156
	// We allow admins having attachment permissions to see orphan attachments...
indvdum@0
   157
	$own_attachment = ($auth->acl_get('a_attach') || $attachment['poster_id'] == $user->data['user_id']) ? true : false;
indvdum@0
   158
indvdum@0
   159
	if (!$own_attachment || ($attachment['in_message'] && !$auth->acl_get('u_pm_download')) || (!$attachment['in_message'] && !$auth->acl_get('u_download')))
indvdum@0
   160
	{
indvdum@0
   161
		send_status_line(404, 'Not Found');
indvdum@0
   162
		trigger_error('ERROR_NO_ATTACHMENT');
indvdum@0
   163
	}
indvdum@0
   164
indvdum@0
   165
	// Obtain all extensions...
indvdum@0
   166
	$extensions = $cache->obtain_attach_extensions(true);
indvdum@0
   167
}
indvdum@0
   168
else
indvdum@0
   169
{
indvdum@0
   170
	if (!$attachment['in_message'])
indvdum@0
   171
	{
indvdum@0
   172
		//
indvdum@0
   173
		$sql = 'SELECT p.forum_id, f.forum_password, f.parent_id
indvdum@0
   174
			FROM ' . POSTS_TABLE . ' p, ' . FORUMS_TABLE . ' f
indvdum@0
   175
			WHERE p.post_id = ' . $attachment['post_msg_id'] . '
indvdum@0
   176
				AND p.forum_id = f.forum_id';
indvdum@0
   177
		$result = $db->sql_query_limit($sql, 1);
indvdum@0
   178
		$row = $db->sql_fetchrow($result);
indvdum@0
   179
		$db->sql_freeresult($result);
indvdum@0
   180
indvdum@0
   181
		// Global announcement?
indvdum@0
   182
		$f_download = (!$row) ? $auth->acl_getf_global('f_download') : $auth->acl_get('f_download', $row['forum_id']);
indvdum@0
   183
indvdum@0
   184
		if ($auth->acl_get('u_download') && $f_download)
indvdum@0
   185
		{
indvdum@0
   186
			if ($row && $row['forum_password'])
indvdum@0
   187
			{
indvdum@0
   188
				// Do something else ... ?
indvdum@0
   189
				login_forum_box($row);
indvdum@0
   190
			}
indvdum@0
   191
		}
indvdum@0
   192
		else
indvdum@0
   193
		{
indvdum@0
   194
			send_status_line(403, 'Forbidden');
indvdum@0
   195
			trigger_error('SORRY_AUTH_VIEW_ATTACH');
indvdum@0
   196
		}
indvdum@0
   197
	}
indvdum@0
   198
	else
indvdum@0
   199
	{
indvdum@0
   200
		$row['forum_id'] = false;
indvdum@0
   201
		if (!$auth->acl_get('u_pm_download'))
indvdum@0
   202
		{
indvdum@0
   203
			send_status_line(403, 'Forbidden');
indvdum@0
   204
			trigger_error('SORRY_AUTH_VIEW_ATTACH');
indvdum@0
   205
		}
indvdum@0
   206
indvdum@0
   207
		// Check if the attachment is within the users scope...
indvdum@0
   208
		$sql = 'SELECT user_id, author_id
indvdum@0
   209
			FROM ' . PRIVMSGS_TO_TABLE . '
indvdum@0
   210
			WHERE msg_id = ' . $attachment['post_msg_id'];
indvdum@0
   211
		$result = $db->sql_query($sql);
indvdum@0
   212
indvdum@0
   213
		$allowed = false;
indvdum@0
   214
		while ($user_row = $db->sql_fetchrow($result))
indvdum@0
   215
		{
indvdum@0
   216
			if ($user->data['user_id'] == $user_row['user_id'] || $user->data['user_id'] == $user_row['author_id'])
indvdum@0
   217
			{
indvdum@0
   218
				$allowed = true;
indvdum@0
   219
				break;
indvdum@0
   220
			}
indvdum@0
   221
		}
indvdum@0
   222
		$db->sql_freeresult($result);
indvdum@0
   223
indvdum@0
   224
		if (!$allowed)
indvdum@0
   225
		{
indvdum@0
   226
			send_status_line(403, 'Forbidden');
indvdum@0
   227
			trigger_error('ERROR_NO_ATTACHMENT');
indvdum@0
   228
		}
indvdum@0
   229
	}
indvdum@0
   230
indvdum@0
   231
	// disallowed?
indvdum@0
   232
	$extensions = array();
indvdum@0
   233
	if (!extension_allowed($row['forum_id'], $attachment['extension'], $extensions))
indvdum@0
   234
	{
indvdum@0
   235
		send_status_line(404, 'Forbidden');
indvdum@0
   236
		trigger_error(sprintf($user->lang['EXTENSION_DISABLED_AFTER_POSTING'], $attachment['extension']));
indvdum@0
   237
	}
indvdum@0
   238
}
indvdum@0
   239
indvdum@0
   240
if (!download_allowed())
indvdum@0
   241
{
indvdum@0
   242
	send_status_line(403, 'Forbidden');
indvdum@0
   243
	trigger_error($user->lang['LINKAGE_FORBIDDEN']);
indvdum@0
   244
}
indvdum@0
   245
indvdum@0
   246
$download_mode = (int) $extensions[$attachment['extension']]['download_mode'];
indvdum@0
   247
indvdum@0
   248
// Fetching filename here to prevent sniffing of filename
indvdum@0
   249
$sql = 'SELECT attach_id, is_orphan, in_message, post_msg_id, extension, physical_filename, real_filename, mimetype, filetime
indvdum@0
   250
	FROM ' . ATTACHMENTS_TABLE . "
indvdum@0
   251
	WHERE attach_id = $download_id";
indvdum@0
   252
$result = $db->sql_query_limit($sql, 1);
indvdum@0
   253
$attachment = $db->sql_fetchrow($result);
indvdum@0
   254
$db->sql_freeresult($result);
indvdum@0
   255
indvdum@0
   256
if (!$attachment)
indvdum@0
   257
{
indvdum@0
   258
	send_status_line(404, 'Not Found');
indvdum@0
   259
	trigger_error('ERROR_NO_ATTACHMENT');
indvdum@0
   260
}
indvdum@0
   261
indvdum@0
   262
$attachment['physical_filename'] = utf8_basename($attachment['physical_filename']);
indvdum@0
   263
$display_cat = $extensions[$attachment['extension']]['display_cat'];
indvdum@0
   264
indvdum@0
   265
if (($display_cat == ATTACHMENT_CATEGORY_IMAGE || $display_cat == ATTACHMENT_CATEGORY_THUMB) && !$user->optionget('viewimg'))
indvdum@0
   266
{
indvdum@0
   267
	$display_cat = ATTACHMENT_CATEGORY_NONE;
indvdum@0
   268
}
indvdum@0
   269
indvdum@0
   270
if ($display_cat == ATTACHMENT_CATEGORY_FLASH && !$user->optionget('viewflash'))
indvdum@0
   271
{
indvdum@0
   272
	$display_cat = ATTACHMENT_CATEGORY_NONE;
indvdum@0
   273
}
indvdum@0
   274
indvdum@0
   275
if ($thumbnail)
indvdum@0
   276
{
indvdum@0
   277
	$attachment['physical_filename'] = 'thumb_' . $attachment['physical_filename'];
indvdum@0
   278
}
indvdum@0
   279
else if (($display_cat == ATTACHMENT_CATEGORY_NONE/* || $display_cat == ATTACHMENT_CATEGORY_IMAGE*/) && !$attachment['is_orphan'])
indvdum@0
   280
{
indvdum@0
   281
	// Update download count
indvdum@0
   282
	$sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
indvdum@0
   283
		SET download_count = download_count + 1
indvdum@0
   284
		WHERE attach_id = ' . $attachment['attach_id'];
indvdum@0
   285
	$db->sql_query($sql);
indvdum@0
   286
}
indvdum@0
   287
indvdum@0
   288
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)))
indvdum@0
   289
{
indvdum@0
   290
	wrap_img_in_html(append_sid($phpbb_root_path . 'download/file.' . $phpEx, 'id=' . $attachment['attach_id']), $attachment['real_filename']);
indvdum@0
   291
	file_gc();
indvdum@0
   292
}
indvdum@0
   293
else
indvdum@0
   294
{
indvdum@0
   295
	// Determine the 'presenting'-method
indvdum@0
   296
	if ($download_mode == PHYSICAL_LINK)
indvdum@0
   297
	{
indvdum@0
   298
		// This presenting method should no longer be used
indvdum@0
   299
		if (!@is_dir($phpbb_root_path . $config['upload_path']))
indvdum@0
   300
		{
indvdum@0
   301
			send_status_line(500, 'Internal Server Error');
indvdum@0
   302
			trigger_error($user->lang['PHYSICAL_DOWNLOAD_NOT_POSSIBLE']);
indvdum@0
   303
		}
indvdum@0
   304
indvdum@0
   305
		redirect($phpbb_root_path . $config['upload_path'] . '/' . $attachment['physical_filename']);
indvdum@0
   306
		file_gc();
indvdum@0
   307
	}
indvdum@0
   308
	else
indvdum@0
   309
	{
indvdum@0
   310
		send_file_to_browser($attachment, $config['upload_path'], $display_cat);
indvdum@0
   311
		file_gc();
indvdum@0
   312
	}
indvdum@0
   313
}
indvdum@0
   314
indvdum@0
   315
indvdum@0
   316
/**
indvdum@0
   317
* A simplified function to deliver avatars
indvdum@0
   318
* The argument needs to be checked before calling this function.
indvdum@0
   319
*/
indvdum@0
   320
function send_avatar_to_browser($file, $browser)
indvdum@0
   321
{
indvdum@0
   322
	global $config, $phpbb_root_path;
indvdum@0
   323
indvdum@0
   324
	$prefix = $config['avatar_salt'] . '_';
indvdum@0
   325
	$image_dir = $config['avatar_path'];
indvdum@0
   326
indvdum@0
   327
	// Adjust image_dir path (no trailing slash)
indvdum@0
   328
	if (substr($image_dir, -1, 1) == '/' || substr($image_dir, -1, 1) == '\\')
indvdum@0
   329
	{
indvdum@0
   330
		$image_dir = substr($image_dir, 0, -1) . '/';
indvdum@0
   331
	}
indvdum@0
   332
	$image_dir = str_replace(array('../', '..\\', './', '.\\'), '', $image_dir);
indvdum@0
   333
indvdum@0
   334
	if ($image_dir && ($image_dir[0] == '/' || $image_dir[0] == '\\'))
indvdum@0
   335
	{
indvdum@0
   336
		$image_dir = '';
indvdum@0
   337
	}
indvdum@0
   338
	$file_path = $phpbb_root_path . $image_dir . '/' . $prefix . $file;
indvdum@0
   339
indvdum@0
   340
	if ((@file_exists($file_path) && @is_readable($file_path)) && !headers_sent())
indvdum@0
   341
	{
indvdum@0
   342
		header('Pragma: public');
indvdum@0
   343
indvdum@0
   344
		$image_data = @getimagesize($file_path);
indvdum@0
   345
		header('Content-Type: ' . image_type_to_mime_type($image_data[2]));
indvdum@0
   346
indvdum@0
   347
		if (strpos(strtolower($browser), 'msie') !== false && strpos(strtolower($browser), 'msie 8.0') === false)
indvdum@0
   348
		{
indvdum@0
   349
			header('Content-Disposition: attachment; ' . header_filename($file));
indvdum@0
   350
indvdum@0
   351
			if (strpos(strtolower($browser), 'msie 6.0') !== false)
indvdum@0
   352
			{
indvdum@0
   353
				header('Expires: -1');
indvdum@0
   354
			}
indvdum@0
   355
			else
indvdum@0
   356
			{
indvdum@0
   357
				header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 31536000));
indvdum@0
   358
			}
indvdum@0
   359
		}
indvdum@0
   360
		else
indvdum@0
   361
		{
indvdum@0
   362
			header('Content-Disposition: inline; ' . header_filename($file));
indvdum@0
   363
			header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 31536000));
indvdum@0
   364
		}
indvdum@0
   365
indvdum@0
   366
		$size = @filesize($file_path);
indvdum@0
   367
		if ($size)
indvdum@0
   368
		{
indvdum@0
   369
			header("Content-Length: $size");
indvdum@0
   370
		}
indvdum@0
   371
indvdum@0
   372
		if (@readfile($file_path) == false)
indvdum@0
   373
		{
indvdum@0
   374
			$fp = @fopen($file_path, 'rb');
indvdum@0
   375
indvdum@0
   376
			if ($fp !== false)
indvdum@0
   377
			{
indvdum@0
   378
				while (!feof($fp))
indvdum@0
   379
				{
indvdum@0
   380
					echo fread($fp, 8192);
indvdum@0
   381
				}
indvdum@0
   382
				fclose($fp);
indvdum@0
   383
			}
indvdum@0
   384
		}
indvdum@0
   385
indvdum@0
   386
		flush();
indvdum@0
   387
	}
indvdum@0
   388
	else
indvdum@0
   389
	{
indvdum@0
   390
		send_status_line(404, 'Not Found');
indvdum@0
   391
	}
indvdum@0
   392
}
indvdum@0
   393
indvdum@0
   394
/**
indvdum@0
   395
* Wraps an url into a simple html page. Used to display attachments in IE.
indvdum@0
   396
* this is a workaround for now; might be moved to template system later
indvdum@0
   397
* direct any complaints to 1 Microsoft Way, Redmond
indvdum@0
   398
*/
indvdum@0
   399
function wrap_img_in_html($src, $title)
indvdum@0
   400
{
indvdum@0
   401
	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Strict.dtd">';
indvdum@0
   402
	echo '<html>';
indvdum@0
   403
	echo '<head>';
indvdum@0
   404
	echo '<meta http-equiv="content-type" content="text/html; charset=UTF-8" />';
indvdum@0
   405
	echo '<title>' . $title . '</title>';
indvdum@0
   406
	echo '</head>';
indvdum@0
   407
	echo '<body>';
indvdum@0
   408
	echo '<div>';
indvdum@0
   409
	echo '<img src="' . $src . '" alt="' . $title . '" />';
indvdum@0
   410
	echo '</div>';
indvdum@0
   411
	echo '</body>';
indvdum@0
   412
	echo '</html>';
indvdum@0
   413
}
indvdum@0
   414
indvdum@0
   415
/**
indvdum@0
   416
* Send file to browser
indvdum@0
   417
*/
indvdum@0
   418
function send_file_to_browser($attachment, $upload_dir, $category)
indvdum@0
   419
{
indvdum@0
   420
	global $user, $db, $config, $phpbb_root_path;
indvdum@0
   421
indvdum@0
   422
	$filename = $phpbb_root_path . $upload_dir . '/' . $attachment['physical_filename'];
indvdum@0
   423
indvdum@0
   424
	if (!@file_exists($filename))
indvdum@0
   425
	{
indvdum@0
   426
		send_status_line(404, 'Not Found');
indvdum@0
   427
		trigger_error($user->lang['ERROR_NO_ATTACHMENT'] . '<br /><br />' . sprintf($user->lang['FILE_NOT_FOUND_404'], $filename));
indvdum@0
   428
	}
indvdum@0
   429
indvdum@0
   430
	// Correct the mime type - we force application/octetstream for all files, except images
indvdum@0
   431
	// Please do not change this, it is a security precaution
indvdum@0
   432
	if ($category != ATTACHMENT_CATEGORY_IMAGE || strpos($attachment['mimetype'], 'image') !== 0)
indvdum@0
   433
	{
indvdum@0
   434
		$attachment['mimetype'] = (strpos(strtolower($user->browser), 'msie') !== false || strpos(strtolower($user->browser), 'opera') !== false) ? 'application/octetstream' : 'application/octet-stream';
indvdum@0
   435
	}
indvdum@0
   436
indvdum@0
   437
	if (@ob_get_length())
indvdum@0
   438
	{
indvdum@0
   439
		@ob_end_clean();
indvdum@0
   440
	}
indvdum@0
   441
indvdum@0
   442
	// Now send the File Contents to the Browser
indvdum@0
   443
	$size = @filesize($filename);
indvdum@0
   444
indvdum@0
   445
	// To correctly display further errors we need to make sure we are using the correct headers for both (unsetting content-length may not work)
indvdum@0
   446
indvdum@0
   447
	// Check if headers already sent or not able to get the file contents.
indvdum@0
   448
	if (headers_sent() || !@file_exists($filename) || !@is_readable($filename))
indvdum@0
   449
	{
indvdum@0
   450
		// PHP track_errors setting On?
indvdum@0
   451
		if (!empty($php_errormsg))
indvdum@0
   452
		{
indvdum@0
   453
			send_status_line(500, 'Internal Server Error');
indvdum@0
   454
			trigger_error($user->lang['UNABLE_TO_DELIVER_FILE'] . '<br />' . sprintf($user->lang['TRACKED_PHP_ERROR'], $php_errormsg));
indvdum@0
   455
		}
indvdum@0
   456
indvdum@0
   457
		send_status_line(500, 'Internal Server Error');
indvdum@0
   458
		trigger_error('UNABLE_TO_DELIVER_FILE');
indvdum@0
   459
	}
indvdum@0
   460
indvdum@0
   461
	// Now the tricky part... let's dance
indvdum@0
   462
	header('Pragma: public');
indvdum@0
   463
indvdum@0
   464
	/**
indvdum@0
   465
	* 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.
indvdum@0
   466
	*
indvdum@0
   467
	* Try X-Sendfile since it is much more server friendly - only works if the path is *not* outside of the root path...
indvdum@0
   468
	* lighttpd has core support for it. An apache2 module is available at http://celebnamer.celebworld.ws/stuff/mod_xsendfile/
indvdum@0
   469
	*
indvdum@0
   470
	* Not really ideal, but should work fine...
indvdum@0
   471
	* <code>
indvdum@0
   472
	*	if (strpos($upload_dir, '/') !== 0 && strpos($upload_dir, '../') === false)
indvdum@0
   473
	*	{
indvdum@0
   474
	*		header('X-Sendfile: ' . $filename);
indvdum@0
   475
	*	}
indvdum@0
   476
	* </code>
indvdum@0
   477
	*/
indvdum@0
   478
indvdum@0
   479
	// Send out the Headers. Do not set Content-Disposition to inline please, it is a security measure for users using the Internet Explorer.
indvdum@0
   480
	$is_ie8 = (strpos(strtolower($user->browser), 'msie 8.0') !== false);
indvdum@0
   481
	header('Content-Type: ' . $attachment['mimetype']);
indvdum@0
   482
indvdum@0
   483
	if ($is_ie8)
indvdum@0
   484
	{
indvdum@0
   485
		header('X-Content-Type-Options: nosniff');
indvdum@0
   486
	}
indvdum@0
   487
indvdum@0
   488
	if ($category == ATTACHMENT_CATEGORY_FLASH && request_var('view', 0) === 1)
indvdum@0
   489
	{
indvdum@0
   490
		// 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
indvdum@0
   491
		header('Content-Disposition: inline');
indvdum@0
   492
	}
indvdum@0
   493
	else
indvdum@0
   494
	{
indvdum@0
   495
		if (empty($user->browser) || (!$is_ie8 && (strpos(strtolower($user->browser), 'msie') !== false)))
indvdum@0
   496
		{
indvdum@0
   497
			header('Content-Disposition: attachment; ' . header_filename(htmlspecialchars_decode($attachment['real_filename'])));
indvdum@0
   498
			if (empty($user->browser) || (strpos(strtolower($user->browser), 'msie 6.0') !== false))
indvdum@0
   499
			{
indvdum@0
   500
				header('expires: -1');
indvdum@0
   501
			}
indvdum@0
   502
		}
indvdum@0
   503
		else
indvdum@0
   504
		{
indvdum@0
   505
			header('Content-Disposition: ' . ((strpos($attachment['mimetype'], 'image') === 0) ? 'inline' : 'attachment') . '; ' . header_filename(htmlspecialchars_decode($attachment['real_filename'])));
indvdum@0
   506
			if ($is_ie8 && (strpos($attachment['mimetype'], 'image') !== 0))
indvdum@0
   507
			{
indvdum@0
   508
				header('X-Download-Options: noopen');
indvdum@0
   509
			}
indvdum@0
   510
		}
indvdum@0
   511
	}
indvdum@0
   512
indvdum@0
   513
	if ($size)
indvdum@0
   514
	{
indvdum@0
   515
		header("Content-Length: $size");
indvdum@0
   516
	}
indvdum@0
   517
indvdum@0
   518
	// Close the db connection before sending the file
indvdum@0
   519
	$db->sql_close();
indvdum@0
   520
indvdum@0
   521
	if (!set_modified_headers($attachment['filetime'], $user->browser))
indvdum@0
   522
	{
indvdum@0
   523
		// Try to deliver in chunks
indvdum@0
   524
		@set_time_limit(0);
indvdum@0
   525
indvdum@0
   526
		$fp = @fopen($filename, 'rb');
indvdum@0
   527
indvdum@0
   528
		if ($fp !== false)
indvdum@0
   529
		{
indvdum@0
   530
			while (!feof($fp))
indvdum@0
   531
			{
indvdum@0
   532
				echo fread($fp, 8192);
indvdum@0
   533
			}
indvdum@0
   534
			fclose($fp);
indvdum@0
   535
		}
indvdum@0
   536
		else
indvdum@0
   537
		{
indvdum@0
   538
			@readfile($filename);
indvdum@0
   539
		}
indvdum@0
   540
indvdum@0
   541
		flush();
indvdum@0
   542
	}
indvdum@0
   543
	file_gc();
indvdum@0
   544
}
indvdum@0
   545
indvdum@0
   546
/**
indvdum@0
   547
* Get a browser friendly UTF-8 encoded filename
indvdum@0
   548
*/
indvdum@0
   549
function header_filename($file)
indvdum@0
   550
{
indvdum@0
   551
	$user_agent = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : '';
indvdum@0
   552
indvdum@0
   553
	// There be dragons here.
indvdum@0
   554
	// Not many follows the RFC...
indvdum@0
   555
	if (strpos($user_agent, 'MSIE') !== false || strpos($user_agent, 'Safari') !== false || strpos($user_agent, 'Konqueror') !== false)
indvdum@0
   556
	{
indvdum@0
   557
		return "filename=" . rawurlencode($file);
indvdum@0
   558
	}
indvdum@0
   559
indvdum@0
   560
	// follow the RFC for extended filename for the rest
indvdum@0
   561
	return "filename*=UTF-8''" . rawurlencode($file);
indvdum@0
   562
}
indvdum@0
   563
indvdum@0
   564
/**
indvdum@0
   565
* Check if downloading item is allowed
indvdum@0
   566
*/
indvdum@0
   567
function download_allowed()
indvdum@0
   568
{
indvdum@0
   569
	global $config, $user, $db;
indvdum@0
   570
indvdum@0
   571
	if (!$config['secure_downloads'])
indvdum@0
   572
	{
indvdum@0
   573
		return true;
indvdum@0
   574
	}
indvdum@0
   575
indvdum@0
   576
	$url = (!empty($_SERVER['HTTP_REFERER'])) ? trim($_SERVER['HTTP_REFERER']) : trim(getenv('HTTP_REFERER'));
indvdum@0
   577
indvdum@0
   578
	if (!$url)
indvdum@0
   579
	{
indvdum@0
   580
		return ($config['secure_allow_empty_referer']) ? true : false;
indvdum@0
   581
	}
indvdum@0
   582
indvdum@0
   583
	// Split URL into domain and script part
indvdum@0
   584
	$url = @parse_url($url);
indvdum@0
   585
indvdum@0
   586
	if ($url === false)
indvdum@0
   587
	{
indvdum@0
   588
		return ($config['secure_allow_empty_referer']) ? true : false;
indvdum@0
   589
	}
indvdum@0
   590
indvdum@0
   591
	$hostname = $url['host'];
indvdum@0
   592
	unset($url);
indvdum@0
   593
indvdum@0
   594
	$allowed = ($config['secure_allow_deny']) ? false : true;
indvdum@0
   595
	$iplist = array();
indvdum@0
   596
indvdum@0
   597
	if (($ip_ary = @gethostbynamel($hostname)) !== false)
indvdum@0
   598
	{
indvdum@0
   599
		foreach ($ip_ary as $ip)
indvdum@0
   600
		{
indvdum@0
   601
			if ($ip)
indvdum@0
   602
			{
indvdum@0
   603
				$iplist[] = $ip;
indvdum@0
   604
			}
indvdum@0
   605
		}
indvdum@0
   606
	}
indvdum@0
   607
indvdum@0
   608
	// Check for own server...
indvdum@0
   609
	$server_name = $user->host;
indvdum@0
   610
indvdum@0
   611
	// Forcing server vars is the only way to specify/override the protocol
indvdum@0
   612
	if ($config['force_server_vars'] || !$server_name)
indvdum@0
   613
	{
indvdum@0
   614
		$server_name = $config['server_name'];
indvdum@0
   615
	}
indvdum@0
   616
indvdum@0
   617
	if (preg_match('#^.*?' . preg_quote($server_name, '#') . '.*?$#i', $hostname))
indvdum@0
   618
	{
indvdum@0
   619
		$allowed = true;
indvdum@0
   620
	}
indvdum@0
   621
indvdum@0
   622
	// Get IP's and Hostnames
indvdum@0
   623
	if (!$allowed)
indvdum@0
   624
	{
indvdum@0
   625
		$sql = 'SELECT site_ip, site_hostname, ip_exclude
indvdum@0
   626
			FROM ' . SITELIST_TABLE;
indvdum@0
   627
		$result = $db->sql_query($sql);
indvdum@0
   628
indvdum@0
   629
		while ($row = $db->sql_fetchrow($result))
indvdum@0
   630
		{
indvdum@0
   631
			$site_ip = trim($row['site_ip']);
indvdum@0
   632
			$site_hostname = trim($row['site_hostname']);
indvdum@0
   633
indvdum@0
   634
			if ($site_ip)
indvdum@0
   635
			{
indvdum@0
   636
				foreach ($iplist as $ip)
indvdum@0
   637
				{
indvdum@0
   638
					if (preg_match('#^' . str_replace('\*', '.*?', preg_quote($site_ip, '#')) . '$#i', $ip))
indvdum@0
   639
					{
indvdum@0
   640
						if ($row['ip_exclude'])
indvdum@0
   641
						{
indvdum@0
   642
							$allowed = ($config['secure_allow_deny']) ? false : true;
indvdum@0
   643
							break 2;
indvdum@0
   644
						}
indvdum@0
   645
						else
indvdum@0
   646
						{
indvdum@0
   647
							$allowed = ($config['secure_allow_deny']) ? true : false;
indvdum@0
   648
						}
indvdum@0
   649
					}
indvdum@0
   650
				}
indvdum@0
   651
			}
indvdum@0
   652
indvdum@0
   653
			if ($site_hostname)
indvdum@0
   654
			{
indvdum@0
   655
				if (preg_match('#^' . str_replace('\*', '.*?', preg_quote($site_hostname, '#')) . '$#i', $hostname))
indvdum@0
   656
				{
indvdum@0
   657
					if ($row['ip_exclude'])
indvdum@0
   658
					{
indvdum@0
   659
						$allowed = ($config['secure_allow_deny']) ? false : true;
indvdum@0
   660
						break;
indvdum@0
   661
					}
indvdum@0
   662
					else
indvdum@0
   663
					{
indvdum@0
   664
						$allowed = ($config['secure_allow_deny']) ? true : false;
indvdum@0
   665
					}
indvdum@0
   666
				}
indvdum@0
   667
			}
indvdum@0
   668
		}
indvdum@0
   669
		$db->sql_freeresult($result);
indvdum@0
   670
	}
indvdum@0
   671
indvdum@0
   672
	return $allowed;
indvdum@0
   673
}
indvdum@0
   674
indvdum@0
   675
/**
indvdum@0
   676
* Check if the browser has the file already and set the appropriate headers-
indvdum@0
   677
* @returns false if a resend is in order.
indvdum@0
   678
*/
indvdum@0
   679
function set_modified_headers($stamp, $browser)
indvdum@0
   680
{
indvdum@0
   681
	// let's see if we have to send the file at all
indvdum@0
   682
	$last_load 	=  isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime(trim($_SERVER['HTTP_IF_MODIFIED_SINCE'])) : false;
indvdum@0
   683
	if ((strpos(strtolower($browser), 'msie 6.0') === false) && (strpos(strtolower($browser), 'msie 8.0') === false))
indvdum@0
   684
	{
indvdum@0
   685
		if ($last_load !== false && $last_load >= $stamp)
indvdum@0
   686
		{
indvdum@0
   687
			send_status_line(304, 'Not Modified');
indvdum@0
   688
			// seems that we need those too ... browsers
indvdum@0
   689
			header('Pragma: public');
indvdum@0
   690
			header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 31536000));
indvdum@0
   691
			return true;
indvdum@0
   692
		}
indvdum@0
   693
		else
indvdum@0
   694
		{
indvdum@0
   695
			header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $stamp) . ' GMT');
indvdum@0
   696
		}
indvdum@0
   697
	}
indvdum@0
   698
	return false;
indvdum@0
   699
}
indvdum@0
   700
indvdum@0
   701
function file_gc()
indvdum@0
   702
{
indvdum@0
   703
	global $cache, $db;
indvdum@0
   704
	if (!empty($cache))
indvdum@0
   705
	{
indvdum@0
   706
		$cache->unload();
indvdum@0
   707
	}
indvdum@0
   708
	$db->sql_close();
indvdum@0
   709
	exit;
indvdum@0
   710
}
indvdum@0
   711
indvdum@0
   712
?>