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