5
* @version $Id: file.php,v 1.5 2007/11/17 22:35:33 kellanved Exp $
6
* @copyright (c) 2005 phpBB Group
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
14
define('IN_PHPBB', true);
15
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../';
16
$phpEx = substr(strrchr(__FILE__, '.'), 1);
18
if (isset($_GET['avatar']))
20
require($phpbb_root_path . 'config.' . $phpEx);
21
require($phpbb_root_path . 'includes/acm/acm_' . $acm_type . '.' . $phpEx);
22
require($phpbb_root_path . 'includes/cache.' . $phpEx);
23
require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
24
require($phpbb_root_path . 'includes/constants.' . $phpEx);
30
if (!@$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, false))
37
$browser = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : 'msie 6.0';
39
$config = $cache->obtain_config();
40
$filename = $_GET['avatar'];
41
$avatar_group = false;
42
if ($filename[0] === 'g')
45
$filename = substr($filename, 1);
48
// '==' is not a bug - . as the first char is as bad as no dot at all
49
if (strpos($filename, '.') == false)
51
header('HTTP/1.0 403 forbidden');
60
$ext = substr(strrchr($filename, '.'), 1);
61
$stamp = (int) substr(stristr($filename, '_'), 1);
62
$filename = (int) $filename;
64
// let's see if we have to send the file at all
65
$last_load = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime(trim($_SERVER['HTTP_IF_MODIFIED_SINCE'])) : false;
66
if (strpos(strtolower($browser), 'msie 6.0') === false)
68
if ($last_load !== false && $last_load <= $stamp)
70
header('Not Modified', true, 304);
71
// seems that we need those too ... browsers
72
header('Pragma: public');
73
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 31536000));
78
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $stamp) . ' GMT');
82
if (!in_array($ext, array('png', 'gif', 'jpg', 'jpeg')))
84
// no way such an avatar could exist. They are not following the rules, stop the show.
85
header("HTTP/1.0 403 forbidden");
96
// no way such an avatar could exist. They are not following the rules, stop the show.
97
header("HTTP/1.0 403 forbidden");
106
send_avatar_to_browser(($avatar_group ? 'g' : '') . $filename . '.' . $ext, $browser);
116
// implicit else: we are not in avatar mode
117
include($phpbb_root_path . 'common.' . $phpEx);
119
$download_id = request_var('id', 0);
120
$mode = request_var('mode', '');
121
$thumbnail = request_var('t', false);
123
// Start session management, do not update session page.
124
$user->session_begin(false);
125
$auth->acl($user->data);
126
$user->setup('viewtopic');
130
trigger_error('NO_ATTACHMENT_SELECTED');
133
if (!$config['allow_attachments'] && !$config['allow_pm_attach'])
135
trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED');
138
$sql = 'SELECT attach_id, in_message, post_msg_id, extension, is_orphan, poster_id
139
FROM ' . ATTACHMENTS_TABLE . "
140
WHERE attach_id = $download_id";
141
$result = $db->sql_query_limit($sql, 1);
142
$attachment = $db->sql_fetchrow($result);
143
$db->sql_freeresult($result);
147
trigger_error('ERROR_NO_ATTACHMENT');
150
if ((!$attachment['in_message'] && !$config['allow_attachments']) || ($attachment['in_message'] && !$config['allow_pm_attach']))
152
trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED');
157
if ($attachment['is_orphan'])
159
// We allow admins having attachment permissions to see orphan attachments...
160
$own_attachment = ($auth->acl_get('a_attach') || $attachment['poster_id'] == $user->data['user_id']) ? true : false;
162
if (!$own_attachment || ($attachment['in_message'] && !$auth->acl_get('u_pm_download')) || (!$attachment['in_message'] && !$auth->acl_get('u_download')))
164
trigger_error('ERROR_NO_ATTACHMENT');
167
// Obtain all extensions...
168
$extensions = $cache->obtain_attach_extensions(true);
172
if (!$attachment['in_message'])
175
$sql = 'SELECT p.forum_id, f.forum_password, f.parent_id
176
FROM ' . POSTS_TABLE . ' p, ' . FORUMS_TABLE . ' f
177
WHERE p.post_id = ' . $attachment['post_msg_id'] . '
178
AND p.forum_id = f.forum_id';
179
$result = $db->sql_query_limit($sql, 1);
180
$row = $db->sql_fetchrow($result);
181
$db->sql_freeresult($result);
183
// Global announcement?
184
$f_download = (!$row) ? $auth->acl_getf_global('f_download') : $auth->acl_get('f_download', $row['forum_id']);
186
if ($auth->acl_get('u_download') && $f_download)
188
if ($row && $row['forum_password'])
190
// Do something else ... ?
191
login_forum_box($row);
196
trigger_error('SORRY_AUTH_VIEW_ATTACH');
201
$row['forum_id'] = false;
202
if (!$auth->acl_get('u_pm_download'))
204
trigger_error('SORRY_AUTH_VIEW_ATTACH');
209
$extensions = array();
210
if (!extension_allowed($row['forum_id'], $attachment['extension'], $extensions))
212
trigger_error(sprintf($user->lang['EXTENSION_DISABLED_AFTER_POSTING'], $attachment['extension']));
216
if (!download_allowed())
218
trigger_error($user->lang['LINKAGE_FORBIDDEN']);
221
$download_mode = (int) $extensions[$attachment['extension']]['download_mode'];
223
// Fetching filename here to prevent sniffing of filename
224
$sql = 'SELECT attach_id, is_orphan, in_message, post_msg_id, extension, physical_filename, real_filename, mimetype
225
FROM ' . ATTACHMENTS_TABLE . "
226
WHERE attach_id = $download_id";
227
$result = $db->sql_query_limit($sql, 1);
228
$attachment = $db->sql_fetchrow($result);
229
$db->sql_freeresult($result);
233
trigger_error('ERROR_NO_ATTACHMENT');
236
$attachment['physical_filename'] = basename($attachment['physical_filename']);
237
$display_cat = $extensions[$attachment['extension']]['display_cat'];
239
if (($display_cat == ATTACHMENT_CATEGORY_IMAGE || $display_cat == ATTACHMENT_CATEGORY_THUMB) && !$user->optionget('viewimg'))
241
$display_cat = ATTACHMENT_CATEGORY_NONE;
244
if ($display_cat == ATTACHMENT_CATEGORY_FLASH && !$user->optionget('viewflash'))
246
$display_cat = ATTACHMENT_CATEGORY_NONE;
251
$attachment['physical_filename'] = 'thumb_' . $attachment['physical_filename'];
253
else if (($display_cat == ATTACHMENT_CATEGORY_NONE || $display_cat == ATTACHMENT_CATEGORY_IMAGE) && !$attachment['is_orphan'])
255
// Update download count
256
$sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
257
SET download_count = download_count + 1
258
WHERE attach_id = ' . $attachment['attach_id'];
259
$db->sql_query($sql);
262
if ($display_cat == ATTACHMENT_CATEGORY_IMAGE && $mode === 'view' && (strpos($attachment['mimetype'], 'image') === 0) && strpos(strtolower($user->browser), 'msie') !== false)
264
wrap_img_in_html(append_sid($phpbb_root_path . 'download/file.' . $phpEx, 'id=' . $attachment['attach_id']), $attachment['real_filename']);
268
// Determine the 'presenting'-method
269
if ($download_mode == PHYSICAL_LINK)
271
// This presenting method should no longer be used
272
if (!@is_dir($phpbb_root_path . $config['upload_path']))
274
trigger_error($user->lang['PHYSICAL_DOWNLOAD_NOT_POSSIBLE']);
277
redirect($phpbb_root_path . $config['upload_path'] . '/' . $attachment['physical_filename']);
282
send_file_to_browser($attachment, $config['upload_path'], $display_cat);
289
* A simplified function to deliver avatars
290
* The argument needs to be checked before calling this function.
292
function send_avatar_to_browser($file, $browser)
294
global $config, $phpbb_root_path;
296
$prefix = $config['avatar_salt'] . '_';
297
$image_dir = $config['avatar_path'];
299
// Adjust image_dir path (no trailing slash)
300
if (substr($image_dir, -1, 1) == '/' || substr($image_dir, -1, 1) == '\\')
302
$image_dir = substr($image_dir, 0, -1) . '/';
304
$image_dir = str_replace(array('../', '..\\', './', '.\\'), '', $image_dir);
306
if ($image_dir && ($image_dir[0] == '/' || $image_dir[0] == '\\'))
310
$file_path = $phpbb_root_path . $image_dir . '/' . $prefix . $file;
312
if ((@file_exists($file_path) && @is_readable($file_path)) && !headers_sent())
314
header('Pragma: public');
316
$image_data = @getimagesize($file_path);
317
header('Content-Type: ' . image_type_to_mime_type($image_data[2]));
319
if (strpos(strtolower($browser), 'msie') !== false)
321
header('Content-Disposition: attachment; ' . header_filename($file));
323
if (strpos(strtolower($browser), 'msie 6.0') !== false)
325
header('Expires: -1');
329
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 31536000));
334
header('Content-Disposition: inline; ' . header_filename($file));
335
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 31536000));
338
$size = @filesize($file_path);
341
header("Content-Length: $size");
344
if (@readfile($file_path) === false)
346
$fp = @fopen($file_path, 'rb');
352
echo fread($fp, 8192);
362
header('HTTP/1.0 404 not found');
367
* Wraps an url into a simple html page. Used to display attachments in IE.
368
* this is a workaround for now; might be moved to template system later
369
* direct any complaints to 1 Microsoft Way, Redmond
371
function wrap_img_in_html($src, $title)
373
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Strict.dtd">';
376
echo '<meta http-equiv="content-type" content="text/html; charset=UTF-8" />';
377
echo '<title>' . $title . '</title>';
381
echo '<img src="' . $src . '" alt="' . $title . '" />';
388
* Send file to browser
390
function send_file_to_browser($attachment, $upload_dir, $category)
392
global $user, $db, $config, $phpbb_root_path;
394
$filename = $phpbb_root_path . $upload_dir . '/' . $attachment['physical_filename'];
396
if (!@file_exists($filename))
398
trigger_error($user->lang['ERROR_NO_ATTACHMENT'] . '<br /><br />' . sprintf($user->lang['FILE_NOT_FOUND_404'], $filename));
401
// Correct the mime type - we force application/octetstream for all files, except images
402
// Please do not change this, it is a security precaution
403
if ($category != ATTACHMENT_CATEGORY_IMAGE || strpos($attachment['mimetype'], 'image') !== 0)
405
$attachment['mimetype'] = (strpos(strtolower($user->browser), 'msie') !== false || strpos(strtolower($user->browser), 'opera') !== false) ? 'application/octetstream' : 'application/octet-stream';
408
if (@ob_get_length())
413
// Now send the File Contents to the Browser
414
$size = @filesize($filename);
416
// To correctly display further errors we need to make sure we are using the correct headers for both (unsetting content-length may not work)
418
// Check if headers already sent or not able to get the file contents.
419
if (headers_sent() || !@file_exists($filename) || !@is_readable($filename))
421
// PHP track_errors setting On?
422
if (!empty($php_errormsg))
424
trigger_error($user->lang['UNABLE_TO_DELIVER_FILE'] . '<br />' . sprintf($user->lang['TRACKED_PHP_ERROR'], $php_errormsg));
427
trigger_error('UNABLE_TO_DELIVER_FILE');
430
// Now the tricky part... let's dance
431
header('Pragma: public');
434
* 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.
436
* Try X-Sendfile since it is much more server friendly - only works if the path is *not* outside of the root path...
437
* lighttpd has core support for it. An apache2 module is available at http://celebnamer.celebworld.ws/stuff/mod_xsendfile/
439
* Not really ideal, but should work fine...
441
* if (strpos($upload_dir, '/') !== 0 && strpos($upload_dir, '../') === false)
443
* header('X-Sendfile: ' . $filename);
448
// Send out the Headers. Do not set Content-Disposition to inline please, it is a security measure for users using the Internet Explorer.
449
header('Content-Type: ' . $attachment['mimetype']);
451
if (empty($user->browser) || (strpos(strtolower($user->browser), 'msie') !== false))
453
header('Content-Disposition: attachment; ' . header_filename(htmlspecialchars_decode($attachment['real_filename'])));
454
if (empty($user->browser) || (strpos(strtolower($user->browser), 'msie 6.0') !== false))
456
header('expires: -1');
461
header('Content-Disposition: ' . ((strpos($attachment['mimetype'], 'image') === 0) ? 'inline' : 'attachment') . '; ' . header_filename(htmlspecialchars_decode($attachment['real_filename'])));
466
header("Content-Length: $size");
469
// Try to deliver in chunks
472
$fp = @fopen($filename, 'rb');
478
echo fread($fp, 8192);
484
@readfile($filename);
492
* Get a browser friendly UTF-8 encoded filename
494
function header_filename($file)
496
$user_agent = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : '';
498
// There be dragons here.
499
// Not many follows the RFC...
500
if (strpos($user_agent, 'MSIE') !== false || strpos($user_agent, 'Safari') !== false || strpos($user_agent, 'Konqueror') !== false)
502
return "filename=" . rawurlencode($file);
505
// follow the RFC for extended filename for the rest
506
return "filename*=UTF-8''" . rawurlencode($file);
510
* Check if downloading item is allowed
512
function download_allowed()
514
global $config, $user, $db;
516
if (!$config['secure_downloads'])
521
$url = (!empty($_SERVER['HTTP_REFERER'])) ? trim($_SERVER['HTTP_REFERER']) : trim(getenv('HTTP_REFERER'));
525
return ($config['secure_allow_empty_referer']) ? true : false;
528
// Split URL into domain and script part
529
$url = @parse_url($url);
533
return ($config['secure_allow_empty_referer']) ? true : false;
536
$hostname = $url['host'];
539
$allowed = ($config['secure_allow_deny']) ? false : true;
542
if (($ip_ary = @gethostbynamel($hostname)) !== false)
544
foreach ($ip_ary as $ip)
553
// Check for own server...
554
$server_name = (!empty($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : getenv('SERVER_NAME');
556
// Forcing server vars is the only way to specify/override the protocol
557
if ($config['force_server_vars'] || !$server_name)
559
$server_name = $config['server_name'];
562
if (preg_match('#^.*?' . preg_quote($server_name, '#') . '.*?$#i', $hostname))
567
// Get IP's and Hostnames
570
$sql = 'SELECT site_ip, site_hostname, ip_exclude
571
FROM ' . SITELIST_TABLE;
572
$result = $db->sql_query($sql);
574
while ($row = $db->sql_fetchrow($result))
576
$site_ip = trim($row['site_ip']);
577
$site_hostname = trim($row['site_hostname']);
581
foreach ($iplist as $ip)
583
if (preg_match('#^' . str_replace('\*', '.*?', preg_quote($site_ip, '#')) . '$#i', $ip))
585
if ($row['ip_exclude'])
587
$allowed = ($config['secure_allow_deny']) ? false : true;
592
$allowed = ($config['secure_allow_deny']) ? true : false;
600
if (preg_match('#^' . str_replace('\*', '.*?', preg_quote($site_hostname, '#')) . '$#i', $hostname))
602
if ($row['ip_exclude'])
604
$allowed = ($config['secure_allow_deny']) ? false : true;
609
$allowed = ($config['secure_allow_deny']) ? true : false;
614
$db->sql_freeresult($result);
b'\\ No newline at end of file'