|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
*
|
|
|
f2e824 |
* @package phpBB3
|
|
|
f2e824 |
* @version $Id: file.php 8792 2008-08-28 13:10:05Z Kellanved $
|
|
|
f2e824 |
* @copyright (c) 2005 phpBB Group
|
|
|
f2e824 |
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
|
f2e824 |
*
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* @ignore
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
define('IN_PHPBB', true);
|
|
|
f2e824 |
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../';
|
|
|
f2e824 |
$phpEx = substr(strrchr(__FILE__, '.'), 1);
|
|
|
f2e824 |
|
|
|
f2e824 |
|
|
|
f2e824 |
// Thank you sun.
|
|
|
f2e824 |
if (isset($_SERVER['CONTENT_TYPE']))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
if ($_SERVER['CONTENT_TYPE'] === 'application/x-java-archive')
|
|
|
f2e824 |
{
|
|
|
f2e824 |
exit;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Java') !== false)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
exit;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
if (isset($_GET['avatar']))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
require($phpbb_root_path . 'config.' . $phpEx);
|
|
|
f2e824 |
|
|
|
f2e824 |
if (!defined('PHPBB_INSTALLED') || empty($dbms) || empty($acm_type))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
exit;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
require($phpbb_root_path . 'includes/acm/acm_' . $acm_type . '.' . $phpEx);
|
|
|
f2e824 |
require($phpbb_root_path . 'includes/cache.' . $phpEx);
|
|
|
f2e824 |
require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
|
|
|
f2e824 |
require($phpbb_root_path . 'includes/constants.' . $phpEx);
|
|
|
f2e824 |
|
|
|
f2e824 |
$db = new $sql_db();
|
|
|
f2e824 |
$cache = new cache();
|
|
|
f2e824 |
|
|
|
f2e824 |
// Connect to DB
|
|
|
f2e824 |
if (!@$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, false))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
exit;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
unset($dbpasswd);
|
|
|
f2e824 |
|
|
|
f2e824 |
// worst-case default
|
|
|
f2e824 |
$browser = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : 'msie 6.0';
|
|
|
f2e824 |
|
|
|
f2e824 |
$config = $cache->obtain_config();
|
|
|
f2e824 |
$filename = $_GET['avatar'];
|
|
|
f2e824 |
$avatar_group = false;
|
|
|
f2e824 |
$exit = false;
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($filename[0] === 'g')
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$avatar_group = true;
|
|
|
f2e824 |
$filename = substr($filename, 1);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
// '==' is not a bug - . as the first char is as bad as no dot at all
|
|
|
f2e824 |
if (strpos($filename, '.') == false)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
header('HTTP/1.0 403 Forbidden');
|
|
|
f2e824 |
$exit = true;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
if (!$exit)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$ext = substr(strrchr($filename, '.'), 1);
|
|
|
f2e824 |
$stamp = (int) substr(stristr($filename, '_'), 1);
|
|
|
f2e824 |
$filename = (int) $filename;
|
|
|
f2e824 |
$exit = set_modified_headers($stamp, $browser);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
if (!$exit && !in_array($ext, array('png', 'gif', 'jpg', 'jpeg')))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
// no way such an avatar could exist. They are not following the rules, stop the show.
|
|
|
f2e824 |
header("HTTP/1.0 403 Forbidden");
|
|
|
f2e824 |
$exit = true;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
|
|
|
f2e824 |
if (!$exit)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
if (!$filename)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
// no way such an avatar could exist. They are not following the rules, stop the show.
|
|
|
f2e824 |
header("HTTP/1.0 403 Forbidden");
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else
|
|
|
f2e824 |
{
|
|
|
f2e824 |
send_avatar_to_browser(($avatar_group ? 'g' : '') . $filename . '.' . $ext, $browser);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
file_gc();
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
// implicit else: we are not in avatar mode
|
|
|
f2e824 |
include($phpbb_root_path . 'common.' . $phpEx);
|
|
|
f2e824 |
|
|
|
f2e824 |
$download_id = request_var('id', 0);
|
|
|
f2e824 |
$mode = request_var('mode', '');
|
|
|
f2e824 |
$thumbnail = request_var('t', false);
|
|
|
f2e824 |
|
|
|
f2e824 |
// Start session management, do not update session page.
|
|
|
f2e824 |
$user->session_begin(false);
|
|
|
f2e824 |
$auth->acl($user->data);
|
|
|
f2e824 |
$user->setup('viewtopic');
|
|
|
f2e824 |
|
|
|
f2e824 |
if (!$download_id)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
trigger_error('NO_ATTACHMENT_SELECTED');
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
if (!$config['allow_attachments'] && !$config['allow_pm_attach'])
|
|
|
f2e824 |
{
|
|
|
f2e824 |
trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED');
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
$sql = 'SELECT attach_id, in_message, post_msg_id, extension, is_orphan, poster_id, filetime
|
|
|
f2e824 |
FROM ' . ATTACHMENTS_TABLE . "
|
|
|
f2e824 |
WHERE attach_id = $download_id";
|
|
|
f2e824 |
$result = $db->sql_query_limit($sql, 1);
|
|
|
f2e824 |
$attachment = $db->sql_fetchrow($result);
|
|
|
f2e824 |
$db->sql_freeresult($result);
|
|
|
f2e824 |
|
|
|
f2e824 |
if (!$attachment)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
trigger_error('ERROR_NO_ATTACHMENT');
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
if ((!$attachment['in_message'] && !$config['allow_attachments']) || ($attachment['in_message'] && !$config['allow_pm_attach']))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED');
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
$row = array();
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($attachment['is_orphan'])
|
|
|
f2e824 |
{
|
|
|
f2e824 |
// We allow admins having attachment permissions to see orphan attachments...
|
|
|
f2e824 |
$own_attachment = ($auth->acl_get('a_attach') || $attachment['poster_id'] == $user->data['user_id']) ? true : false;
|
|
|
f2e824 |
|
|
|
f2e824 |
if (!$own_attachment || ($attachment['in_message'] && !$auth->acl_get('u_pm_download')) || (!$attachment['in_message'] && !$auth->acl_get('u_download')))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
trigger_error('ERROR_NO_ATTACHMENT');
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
// Obtain all extensions...
|
|
|
f2e824 |
$extensions = $cache->obtain_attach_extensions(true);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else
|
|
|
f2e824 |
{
|
|
|
f2e824 |
if (!$attachment['in_message'])
|
|
|
f2e824 |
{
|
|
|
f2e824 |
//
|
|
|
f2e824 |
$sql = 'SELECT p.forum_id, f.forum_password, f.parent_id
|
|
|
f2e824 |
FROM ' . POSTS_TABLE . ' p, ' . FORUMS_TABLE . ' f
|
|
|
f2e824 |
WHERE p.post_id = ' . $attachment['post_msg_id'] . '
|
|
|
f2e824 |
AND p.forum_id = f.forum_id';
|
|
|
f2e824 |
$result = $db->sql_query_limit($sql, 1);
|
|
|
f2e824 |
$row = $db->sql_fetchrow($result);
|
|
|
f2e824 |
$db->sql_freeresult($result);
|
|
|
f2e824 |
|
|
|
f2e824 |
// Global announcement?
|
|
|
f2e824 |
$f_download = (!$row) ? $auth->acl_getf_global('f_download') : $auth->acl_get('f_download', $row['forum_id']);
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($auth->acl_get('u_download') && $f_download)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
if ($row && $row['forum_password'])
|
|
|
f2e824 |
{
|
|
|
f2e824 |
// Do something else ... ?
|
|
|
f2e824 |
login_forum_box($row);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else
|
|
|
f2e824 |
{
|
|
|
f2e824 |
trigger_error('SORRY_AUTH_VIEW_ATTACH');
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$row['forum_id'] = false;
|
|
|
f2e824 |
if (!$auth->acl_get('u_pm_download'))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
header('HTTP/1.0 403 Forbidden');
|
|
|
f2e824 |
trigger_error('SORRY_AUTH_VIEW_ATTACH');
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
// Check if the attachment is within the users scope...
|
|
|
f2e824 |
$sql = 'SELECT user_id, author_id
|
|
|
f2e824 |
FROM ' . PRIVMSGS_TO_TABLE . '
|
|
|
f2e824 |
WHERE msg_id = ' . $attachment['post_msg_id'];
|
|
|
f2e824 |
$result = $db->sql_query($sql);
|
|
|
f2e824 |
|
|
|
f2e824 |
$allowed = false;
|
|
|
f2e824 |
while ($user_row = $db->sql_fetchrow($result))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
if ($user->data['user_id'] == $user_row['user_id'] || $user->data['user_id'] == $user_row['author_id'])
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$allowed = true;
|
|
|
f2e824 |
break;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
$db->sql_freeresult($result);
|
|
|
f2e824 |
|
|
|
f2e824 |
if (!$allowed)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
header('HTTP/1.0 403 Forbidden');
|
|
|
f2e824 |
trigger_error('ERROR_NO_ATTACHMENT');
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
// disallowed?
|
|
|
f2e824 |
$extensions = array();
|
|
|
f2e824 |
if (!extension_allowed($row['forum_id'], $attachment['extension'], $extensions))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
trigger_error(sprintf($user->lang['EXTENSION_DISABLED_AFTER_POSTING'], $attachment['extension']));
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
if (!download_allowed())
|
|
|
f2e824 |
{
|
|
|
f2e824 |
header('HTTP/1.0 403 Forbidden');
|
|
|
f2e824 |
trigger_error($user->lang['LINKAGE_FORBIDDEN']);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
$download_mode = (int) $extensions[$attachment['extension']]['download_mode'];
|
|
|
f2e824 |
|
|
|
f2e824 |
// Fetching filename here to prevent sniffing of filename
|
|
|
f2e824 |
$sql = 'SELECT attach_id, is_orphan, in_message, post_msg_id, extension, physical_filename, real_filename, mimetype, filetime
|
|
|
f2e824 |
FROM ' . ATTACHMENTS_TABLE . "
|
|
|
f2e824 |
WHERE attach_id = $download_id";
|
|
|
f2e824 |
$result = $db->sql_query_limit($sql, 1);
|
|
|
f2e824 |
$attachment = $db->sql_fetchrow($result);
|
|
|
f2e824 |
$db->sql_freeresult($result);
|
|
|
f2e824 |
|
|
|
f2e824 |
if (!$attachment)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
trigger_error('ERROR_NO_ATTACHMENT');
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
$attachment['physical_filename'] = basename($attachment['physical_filename']);
|
|
|
f2e824 |
$display_cat = $extensions[$attachment['extension']]['display_cat'];
|
|
|
f2e824 |
|
|
|
f2e824 |
if (($display_cat == ATTACHMENT_CATEGORY_IMAGE || $display_cat == ATTACHMENT_CATEGORY_THUMB) && !$user->optionget('viewimg'))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$display_cat = ATTACHMENT_CATEGORY_NONE;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($display_cat == ATTACHMENT_CATEGORY_FLASH && !$user->optionget('viewflash'))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$display_cat = ATTACHMENT_CATEGORY_NONE;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($thumbnail)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$attachment['physical_filename'] = 'thumb_' . $attachment['physical_filename'];
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else if (($display_cat == ATTACHMENT_CATEGORY_NONE || $display_cat == ATTACHMENT_CATEGORY_IMAGE) && !$attachment['is_orphan'])
|
|
|
f2e824 |
{
|
|
|
f2e824 |
// Update download count
|
|
|
f2e824 |
$sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
|
|
|
f2e824 |
SET download_count = download_count + 1
|
|
|
f2e824 |
WHERE attach_id = ' . $attachment['attach_id'];
|
|
|
f2e824 |
$db->sql_query($sql);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
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)))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
wrap_img_in_html(append_sid($phpbb_root_path . 'download/file.' . $phpEx, 'id=' . $attachment['attach_id']), $attachment['real_filename']);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else
|
|
|
f2e824 |
{
|
|
|
f2e824 |
// Determine the 'presenting'-method
|
|
|
f2e824 |
if ($download_mode == PHYSICAL_LINK)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
// This presenting method should no longer be used
|
|
|
f2e824 |
if (!@is_dir($phpbb_root_path . $config['upload_path']))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
trigger_error($user->lang['PHYSICAL_DOWNLOAD_NOT_POSSIBLE']);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
redirect($phpbb_root_path . $config['upload_path'] . '/' . $attachment['physical_filename']);
|
|
|
f2e824 |
file_gc();
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else
|
|
|
f2e824 |
{
|
|
|
f2e824 |
send_file_to_browser($attachment, $config['upload_path'], $display_cat);
|
|
|
f2e824 |
file_gc();
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* A simplified function to deliver avatars
|
|
|
f2e824 |
* The argument needs to be checked before calling this function.
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function send_avatar_to_browser($file, $browser)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
global $config, $phpbb_root_path;
|
|
|
f2e824 |
|
|
|
f2e824 |
$prefix = $config['avatar_salt'] . '_';
|
|
|
f2e824 |
$image_dir = $config['avatar_path'];
|
|
|
f2e824 |
|
|
|
f2e824 |
// Adjust image_dir path (no trailing slash)
|
|
|
f2e824 |
if (substr($image_dir, -1, 1) == '/' || substr($image_dir, -1, 1) == '\\')
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$image_dir = substr($image_dir, 0, -1) . '/';
|
|
|
f2e824 |
}
|
|
|
f2e824 |
$image_dir = str_replace(array('../', '..\\', './', '.\\'), '', $image_dir);
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($image_dir && ($image_dir[0] == '/' || $image_dir[0] == '\\'))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$image_dir = '';
|
|
|
f2e824 |
}
|
|
|
f2e824 |
$file_path = $phpbb_root_path . $image_dir . '/' . $prefix . $file;
|
|
|
f2e824 |
|
|
|
f2e824 |
if ((@file_exists($file_path) && @is_readable($file_path)) && !headers_sent())
|
|
|
f2e824 |
{
|
|
|
f2e824 |
header('Pragma: public');
|
|
|
f2e824 |
|
|
|
f2e824 |
$image_data = @getimagesize($file_path);
|
|
|
f2e824 |
header('Content-Type: ' . image_type_to_mime_type($image_data[2]));
|
|
|
f2e824 |
|
|
|
f2e824 |
if (strpos(strtolower($browser), 'msie') !== false && strpos(strtolower($browser), 'msie 8.0') === false)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
header('Content-Disposition: attachment; ' . header_filename($file));
|
|
|
f2e824 |
|
|
|
f2e824 |
if (strpos(strtolower($browser), 'msie 6.0') !== false)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
header('Expires: -1');
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else
|
|
|
f2e824 |
{
|
|
|
f2e824 |
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 31536000));
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else
|
|
|
f2e824 |
{
|
|
|
f2e824 |
header('Content-Disposition: inline; ' . header_filename($file));
|
|
|
f2e824 |
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 31536000));
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
$size = @filesize($file_path);
|
|
|
f2e824 |
if ($size)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
header("Content-Length: $size");
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
if (@readfile($file_path) == false)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$fp = @fopen($file_path, 'rb');
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($fp !== false)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
while (!feof($fp))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
echo fread($fp, 8192);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
fclose($fp);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
flush();
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else
|
|
|
f2e824 |
{
|
|
|
f2e824 |
header('HTTP/1.0 404 not found');
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* Wraps an url into a simple html page. Used to display attachments in IE.
|
|
|
f2e824 |
* this is a workaround for now; might be moved to template system later
|
|
|
f2e824 |
* direct any complaints to 1 Microsoft Way, Redmond
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function wrap_img_in_html($src, $title)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
echo '';
|
|
|
f2e824 |
echo '<html>';
|
|
|
f2e824 |
echo '<head>';
|
|
|
f2e824 |
echo '<meta http-equiv="content-type" content="text/html; charset=UTF-8" />';
|
|
|
f2e824 |
echo '<title>' . $title . '</title>';
|
|
|
f2e824 |
echo '</head>';
|
|
|
f2e824 |
echo '<body>';
|
|
|
f2e824 |
echo '';
|
|
|
f2e824 |
echo '';
|
|
|
f2e824 |
echo '';
|
|
|
f2e824 |
echo '</body>';
|
|
|
f2e824 |
echo '</html>';
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* Send file to browser
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function send_file_to_browser($attachment, $upload_dir, $category)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
global $user, $db, $config, $phpbb_root_path;
|
|
|
f2e824 |
|
|
|
f2e824 |
$filename = $phpbb_root_path . $upload_dir . '/' . $attachment['physical_filename'];
|
|
|
f2e824 |
|
|
|
f2e824 |
if (!@file_exists($filename))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
trigger_error($user->lang['ERROR_NO_ATTACHMENT'] . '
' . sprintf($user->lang['FILE_NOT_FOUND_404'], $filename));
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
// Correct the mime type - we force application/octetstream for all files, except images
|
|
|
f2e824 |
// Please do not change this, it is a security precaution
|
|
|
f2e824 |
if ($category != ATTACHMENT_CATEGORY_IMAGE || strpos($attachment['mimetype'], 'image') !== 0)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$attachment['mimetype'] = (strpos(strtolower($user->browser), 'msie') !== false || strpos(strtolower($user->browser), 'opera') !== false) ? 'application/octetstream' : 'application/octet-stream';
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
if (@ob_get_length())
|
|
|
f2e824 |
{
|
|
|
f2e824 |
@ob_end_clean();
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
// Now send the File Contents to the Browser
|
|
|
f2e824 |
$size = @filesize($filename);
|
|
|
f2e824 |
|
|
|
f2e824 |
// To correctly display further errors we need to make sure we are using the correct headers for both (unsetting content-length may not work)
|
|
|
f2e824 |
|
|
|
f2e824 |
// Check if headers already sent or not able to get the file contents.
|
|
|
f2e824 |
if (headers_sent() || !@file_exists($filename) || !@is_readable($filename))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
// PHP track_errors setting On?
|
|
|
f2e824 |
if (!empty($php_errormsg))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
trigger_error($user->lang['UNABLE_TO_DELIVER_FILE'] . ' ' . sprintf($user->lang['TRACKED_PHP_ERROR'], $php_errormsg));
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
trigger_error('UNABLE_TO_DELIVER_FILE');
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
// Now the tricky part... let's dance
|
|
|
f2e824 |
header('Pragma: public');
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* 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.
|
|
|
f2e824 |
*
|
|
|
f2e824 |
* Try X-Sendfile since it is much more server friendly - only works if the path is *not* outside of the root path...
|
|
|
f2e824 |
* lighttpd has core support for it. An apache2 module is available at http://celebnamer.celebworld.ws/stuff/mod_xsendfile/
|
|
|
f2e824 |
*
|
|
|
f2e824 |
* Not really ideal, but should work fine...
|
|
|
f2e824 |
*
|
|
|
f2e824 |
* if (strpos($upload_dir, '/') !== 0 && strpos($upload_dir, '../') === false)
|
|
|
f2e824 |
* {
|
|
|
f2e824 |
* header('X-Sendfile: ' . $filename);
|
|
|
f2e824 |
* }
|
|
|
f2e824 |
*
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
|
|
|
f2e824 |
// Send out the Headers. Do not set Content-Disposition to inline please, it is a security measure for users using the Internet Explorer.
|
|
|
f2e824 |
$is_ie8 = (strpos(strtolower($user->browser), 'msie 8.0') !== false);
|
|
|
f2e824 |
header('Content-Type: ' . $attachment['mimetype'] . (($is_ie8) ? '; authoritative=true;' : ''));
|
|
|
f2e824 |
|
|
|
f2e824 |
if (empty($user->browser) || (!$is_ie8 && (strpos(strtolower($user->browser), 'msie') !== false)))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
header('Content-Disposition: attachment; ' . header_filename(htmlspecialchars_decode($attachment['real_filename'])));
|
|
|
f2e824 |
if (empty($user->browser) || (strpos(strtolower($user->browser), 'msie 6.0') !== false))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
header('expires: -1');
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else
|
|
|
f2e824 |
{
|
|
|
f2e824 |
header('Content-Disposition: ' . ((strpos($attachment['mimetype'], 'image') === 0) ? 'inline' : 'attachment') . '; ' . header_filename(htmlspecialchars_decode($attachment['real_filename'])));
|
|
|
f2e824 |
if ($is_ie8 && (strpos($attachment['mimetype'], 'image') !== 0))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
header('X-Download-Options: noopen');
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($size)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
header("Content-Length: $size");
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
// Close the db connection before sending the file
|
|
|
f2e824 |
$db->sql_close();
|
|
|
f2e824 |
|
|
|
f2e824 |
if (!set_modified_headers($attachment['filetime'], $user->browser))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
// Try to deliver in chunks
|
|
|
f2e824 |
@set_time_limit(0);
|
|
|
f2e824 |
|
|
|
f2e824 |
$fp = @fopen($filename, 'rb');
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($fp !== false)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
while (!feof($fp))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
echo fread($fp, 8192);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
fclose($fp);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else
|
|
|
f2e824 |
{
|
|
|
f2e824 |
@readfile($filename);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
flush();
|
|
|
f2e824 |
}
|
|
|
f2e824 |
file_gc();
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* Get a browser friendly UTF-8 encoded filename
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function header_filename($file)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$user_agent = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : '';
|
|
|
f2e824 |
|
|
|
f2e824 |
// There be dragons here.
|
|
|
f2e824 |
// Not many follows the RFC...
|
|
|
f2e824 |
if (strpos($user_agent, 'MSIE') !== false || strpos($user_agent, 'Safari') !== false || strpos($user_agent, 'Konqueror') !== false)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
return "filename=" . rawurlencode($file);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
// follow the RFC for extended filename for the rest
|
|
|
f2e824 |
return "filename*=UTF-8''" . rawurlencode($file);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* Check if downloading item is allowed
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function download_allowed()
|
|
|
f2e824 |
{
|
|
|
f2e824 |
global $config, $user, $db;
|
|
|
f2e824 |
|
|
|
f2e824 |
if (!$config['secure_downloads'])
|
|
|
f2e824 |
{
|
|
|
f2e824 |
return true;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
$url = (!empty($_SERVER['HTTP_REFERER'])) ? trim($_SERVER['HTTP_REFERER']) : trim(getenv('HTTP_REFERER'));
|
|
|
f2e824 |
|
|
|
f2e824 |
if (!$url)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
return ($config['secure_allow_empty_referer']) ? true : false;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
// Split URL into domain and script part
|
|
|
f2e824 |
$url = @parse_url($url);
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($url === false)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
return ($config['secure_allow_empty_referer']) ? true : false;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
$hostname = $url['host'];
|
|
|
f2e824 |
unset($url);
|
|
|
f2e824 |
|
|
|
f2e824 |
$allowed = ($config['secure_allow_deny']) ? false : true;
|
|
|
f2e824 |
$iplist = array();
|
|
|
f2e824 |
|
|
|
f2e824 |
if (($ip_ary = @gethostbynamel($hostname)) !== false)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
foreach ($ip_ary as $ip)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
if ($ip)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$iplist[] = $ip;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
// Check for own server...
|
|
|
f2e824 |
$server_name = $user->host;
|
|
|
f2e824 |
|
|
|
f2e824 |
// Forcing server vars is the only way to specify/override the protocol
|
|
|
f2e824 |
if ($config['force_server_vars'] || !$server_name)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$server_name = $config['server_name'];
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
if (preg_match('#^.*?' . preg_quote($server_name, '#') . '.*?$#i', $hostname))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$allowed = true;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
// Get IP's and Hostnames
|
|
|
f2e824 |
if (!$allowed)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$sql = 'SELECT site_ip, site_hostname, ip_exclude
|
|
|
f2e824 |
FROM ' . SITELIST_TABLE;
|
|
|
f2e824 |
$result = $db->sql_query($sql);
|
|
|
f2e824 |
|
|
|
f2e824 |
while ($row = $db->sql_fetchrow($result))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$site_ip = trim($row['site_ip']);
|
|
|
f2e824 |
$site_hostname = trim($row['site_hostname']);
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($site_ip)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
foreach ($iplist as $ip)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
if (preg_match('#^' . str_replace('\*', '.*?', preg_quote($site_ip, '#')) . '$#i', $ip))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
if ($row['ip_exclude'])
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$allowed = ($config['secure_allow_deny']) ? false : true;
|
|
|
f2e824 |
break 2;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$allowed = ($config['secure_allow_deny']) ? true : false;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($site_hostname)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
if (preg_match('#^' . str_replace('\*', '.*?', preg_quote($site_hostname, '#')) . '$#i', $hostname))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
if ($row['ip_exclude'])
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$allowed = ($config['secure_allow_deny']) ? false : true;
|
|
|
f2e824 |
break;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$allowed = ($config['secure_allow_deny']) ? true : false;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
$db->sql_freeresult($result);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
return $allowed;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* Check if the browser has the file already and set the appropriate headers-
|
|
|
f2e824 |
* @returns false if a resend is in order.
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function set_modified_headers($stamp, $browser)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
// let's see if we have to send the file at all
|
|
|
f2e824 |
$last_load = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime(trim($_SERVER['HTTP_IF_MODIFIED_SINCE'])) : false;
|
|
|
f2e824 |
if ((strpos(strtolower($browser), 'msie 6.0') === false) && (strpos(strtolower($browser), 'msie 8.0') === false))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
if ($last_load !== false && $last_load <= $stamp)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
if (@php_sapi_name() === 'CGI')
|
|
|
f2e824 |
{
|
|
|
f2e824 |
header('Status: 304 Not Modified', true, 304);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else
|
|
|
f2e824 |
{
|
|
|
f2e824 |
header('HTTP/1.0 304 Not Modified', true, 304);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
// seems that we need those too ... browsers
|
|
|
f2e824 |
header('Pragma: public');
|
|
|
f2e824 |
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 31536000));
|
|
|
f2e824 |
return true;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else
|
|
|
f2e824 |
{
|
|
|
f2e824 |
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $stamp) . ' GMT');
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
return false;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
function file_gc()
|
|
|
f2e824 |
{
|
|
|
f2e824 |
global $cache, $db;
|
|
|
f2e824 |
if (!empty($cache))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$cache->unload();
|
|
|
f2e824 |
}
|
|
|
f2e824 |
$db->sql_close();
|
|
|
f2e824 |
exit;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
?>
|