2003-04-10 21:35:31 +00:00
|
|
|
|
<?php
|
2003-08-29 18:06:56 +00:00
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// $Id$
|
|
|
|
|
//
|
|
|
|
|
// FILENAME : download.php
|
|
|
|
|
// STARTED : Thu Apr 10, 2003
|
|
|
|
|
// COPYRIGHT : <20> 2001, 2003 phpBB Group
|
|
|
|
|
// WWW : http://www.phpbb.com/
|
|
|
|
|
// LICENCE : GPL vs2.0 [ see /docs/COPYING ]
|
|
|
|
|
//
|
|
|
|
|
// -------------------------------------------------------------
|
2003-04-10 21:35:31 +00:00
|
|
|
|
|
|
|
|
|
define('IN_PHPBB', true);
|
|
|
|
|
$phpbb_root_path = './';
|
2003-09-07 13:46:51 +00:00
|
|
|
|
$phpEx = substr(strrchr(__FILE__, '.'), 1);
|
2003-04-10 21:35:31 +00:00
|
|
|
|
include($phpbb_root_path . 'common.'.$phpEx);
|
|
|
|
|
|
2003-10-19 15:17:35 +00:00
|
|
|
|
$download_id = request_var('id', 0);
|
|
|
|
|
// Thumbnails are not called from this file by default
|
|
|
|
|
$thumbnail = request_var('thumb', false);
|
2003-04-10 21:35:31 +00:00
|
|
|
|
|
|
|
|
|
// Start session management
|
|
|
|
|
$user->start();
|
|
|
|
|
$auth->acl($user->data);
|
2003-05-25 13:07:19 +00:00
|
|
|
|
$user->setup();
|
2003-04-10 21:35:31 +00:00
|
|
|
|
|
2003-05-25 13:07:19 +00:00
|
|
|
|
if (!$download_id)
|
2003-04-10 21:35:31 +00:00
|
|
|
|
{
|
|
|
|
|
trigger_error('NO_ATTACHMENT_SELECTED');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$config['allow_attachments'])
|
|
|
|
|
{
|
|
|
|
|
trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED');
|
|
|
|
|
}
|
2003-10-19 15:17:35 +00:00
|
|
|
|
|
2003-04-10 21:35:31 +00:00
|
|
|
|
$sql = 'SELECT *
|
2003-11-04 22:05:38 +00:00
|
|
|
|
FROM ' . ATTACHMENTS_TABLE . "
|
2003-06-18 17:57:44 +00:00
|
|
|
|
WHERE attach_id = $download_id";
|
2003-10-19 15:17:35 +00:00
|
|
|
|
$result = $db->sql_query_limit($sql, 1);
|
2003-04-10 21:35:31 +00:00
|
|
|
|
|
2003-05-03 23:58:45 +00:00
|
|
|
|
if (!($attachment = $db->sql_fetchrow($result)))
|
2003-04-10 21:35:31 +00:00
|
|
|
|
{
|
|
|
|
|
trigger_error('ERROR_NO_ATTACHMENT');
|
|
|
|
|
}
|
2003-10-19 15:17:35 +00:00
|
|
|
|
$db->sql_freeresult($result);
|
2003-04-10 21:35:31 +00:00
|
|
|
|
|
|
|
|
|
// Additional query, because of more than one attachment assigned to posts and private messages
|
2003-11-04 22:05:38 +00:00
|
|
|
|
$sql = 'SELECT p.forum_id, f.forum_password, f.parent_id
|
|
|
|
|
FROM ' . POSTS_TABLE . ' p, ' . FORUMS_TABLE . ' f
|
|
|
|
|
WHERE p.post_id = ' . $attachment['post_id'] . '
|
|
|
|
|
AND p.forum_id = f.forum_id';
|
|
|
|
|
$result = $db->sql_query_limit($sql, 1);
|
|
|
|
|
$row = $db->sql_fetchrow($result);
|
|
|
|
|
$db->sql_freeresult($result);
|
2003-04-10 21:35:31 +00:00
|
|
|
|
|
2003-11-04 22:05:38 +00:00
|
|
|
|
if ($auth->acl_gets('f_download', 'u_download', $row['forum_id']))
|
2003-04-10 21:35:31 +00:00
|
|
|
|
{
|
2003-11-04 22:05:38 +00:00
|
|
|
|
if ($row['forum_password'])
|
2003-04-10 21:35:31 +00:00
|
|
|
|
{
|
2003-11-04 22:05:38 +00:00
|
|
|
|
// Do something else ... ?
|
|
|
|
|
login_forum_box($row);
|
2003-04-10 21:35:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2003-11-04 22:05:38 +00:00
|
|
|
|
else
|
2003-04-10 21:35:31 +00:00
|
|
|
|
{
|
|
|
|
|
trigger_error('SORRY_AUTH_VIEW_ATTACH');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$extensions = array();
|
|
|
|
|
obtain_attach_extensions($extensions);
|
|
|
|
|
|
|
|
|
|
// disallowed ?
|
2003-06-18 17:57:44 +00:00
|
|
|
|
if (!in_array($attachment['extension'], $extensions['_allowed_']))
|
2003-04-10 21:35:31 +00:00
|
|
|
|
{
|
2003-10-19 15:17:35 +00:00
|
|
|
|
trigger_error(sprintf($user->lang['EXTENSION_DISABLED_AFTER_POSTING'], $attachment['extension']));
|
2003-04-10 21:35:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-11-23 22:25:46 +00:00
|
|
|
|
if (!download_allowed())
|
|
|
|
|
{
|
|
|
|
|
trigger_error($user->lang['LINKAGE_FORBIDDEN']);
|
|
|
|
|
}
|
|
|
|
|
|
2003-10-19 15:17:35 +00:00
|
|
|
|
$download_mode = (int) $extensions[$attachment['extension']]['download_mode'];
|
2003-04-10 21:35:31 +00:00
|
|
|
|
|
|
|
|
|
if ($thumbnail)
|
|
|
|
|
{
|
2003-11-16 21:53:56 +00:00
|
|
|
|
$attachment['physical_filename'] = 'thumb_' . $attachment['physical_filename'];
|
2003-04-10 21:35:31 +00:00
|
|
|
|
}
|
2003-10-19 15:17:35 +00:00
|
|
|
|
else
|
2003-04-10 21:35:31 +00:00
|
|
|
|
{
|
2003-10-19 15:17:35 +00:00
|
|
|
|
// Update download count
|
2003-11-04 22:05:38 +00:00
|
|
|
|
$sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
|
2003-04-10 21:35:31 +00:00
|
|
|
|
SET download_count = download_count + 1
|
|
|
|
|
WHERE attach_id = ' . $attachment['attach_id'];
|
|
|
|
|
$db->sql_query($sql);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Determine the 'presenting'-method
|
|
|
|
|
if ($download_mode == PHYSICAL_LINK)
|
|
|
|
|
{
|
2003-11-16 21:53:56 +00:00
|
|
|
|
if (!@is_dir($config['upload_dir']))
|
2003-04-10 21:35:31 +00:00
|
|
|
|
{
|
2003-05-25 13:07:19 +00:00
|
|
|
|
trigger_error($user->lang['PHYSICAL_DOWNLOAD_NOT_POSSIBLE']);
|
2003-04-10 21:35:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-11-16 21:53:56 +00:00
|
|
|
|
redirect($config['upload_dir'] . '/' . $attachment['physical_filename']);
|
2003-04-10 21:35:31 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2003-11-16 21:53:56 +00:00
|
|
|
|
send_file_to_browser($attachment, $config['upload_dir'], $extensions[$attachment['extension']]['display_cat']);
|
2003-07-13 14:12:22 +00:00
|
|
|
|
exit;
|
2003-04-10 21:35:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-09-07 13:46:51 +00:00
|
|
|
|
|
|
|
|
|
// ---------
|
|
|
|
|
// FUNCTIONS
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
function send_file_to_browser($attachment, $upload_dir, $category)
|
|
|
|
|
{
|
|
|
|
|
global $_SERVER, $HTTP_USER_AGENT, $HTTP_SERVER_VARS, $user, $db, $config;
|
|
|
|
|
|
2003-10-19 15:17:35 +00:00
|
|
|
|
$filename = $upload_dir . '/' . $attachment['physical_filename'];
|
2003-09-07 13:46:51 +00:00
|
|
|
|
|
2003-10-19 15:17:35 +00:00
|
|
|
|
if (!@file_exists($filename))
|
2003-09-07 13:46:51 +00:00
|
|
|
|
{
|
|
|
|
|
trigger_error($user->lang['ERROR_NO_ATTACHMENT'] . '<br /><br />' . sprintf($user->lang['FILE_NOT_FOUND_404'], $filename));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Determine the Browser the User is using, because of some nasty incompatibilities.
|
|
|
|
|
// borrowed from phpMyAdmin. :)
|
2003-10-19 15:17:35 +00:00
|
|
|
|
$user_agent = (!empty($_SERVER['HTTP_USER_AGENT'])) ? $_SERVER['HTTP_USER_AGENT'] : ((!empty($HTTP_SERVER_VARS['HTTP_USER_AGENT'])) ? $HTTP_SERVER_VARS['HTTP_USER_AGENT'] : '');
|
2003-09-07 13:46:51 +00:00
|
|
|
|
|
2003-10-19 15:17:35 +00:00
|
|
|
|
if (ereg('Opera(/| )([0-9].[0-9]{1,2})', $user_agent, $log_version))
|
2003-09-07 13:46:51 +00:00
|
|
|
|
{
|
|
|
|
|
$browser_version = $log_version[2];
|
|
|
|
|
$browser_agent = 'opera';
|
2003-10-19 15:17:35 +00:00
|
|
|
|
}
|
|
|
|
|
else if (ereg('MSIE ([0-9].[0-9]{1,2})', $user_agent, $log_version))
|
2003-09-07 13:46:51 +00:00
|
|
|
|
{
|
|
|
|
|
$browser_version = $log_version[1];
|
|
|
|
|
$browser_agent = 'ie';
|
2003-10-19 15:17:35 +00:00
|
|
|
|
}
|
|
|
|
|
else if (ereg('OmniWeb/([0-9].[0-9]{1,2})', $user_agent, $log_version))
|
2003-09-07 13:46:51 +00:00
|
|
|
|
{
|
|
|
|
|
$browser_version = $log_version[1];
|
|
|
|
|
$browser_agent = 'omniweb';
|
2003-10-19 15:17:35 +00:00
|
|
|
|
}
|
|
|
|
|
else if (ereg('(Konqueror/)(.*)(;)', $user_agent, $log_version))
|
2003-09-07 13:46:51 +00:00
|
|
|
|
{
|
2003-10-19 15:17:35 +00:00
|
|
|
|
$browser_version = $log_version[2];
|
|
|
|
|
$browser_agent = 'konqueror';
|
|
|
|
|
}
|
|
|
|
|
else if (ereg('Mozilla/([0-9].[0-9]{1,2})', $user_agent, $log_version) && ereg('Safari/([0-9]*)', $user_agent, $log_version2))
|
2003-09-07 13:46:51 +00:00
|
|
|
|
{
|
2003-10-19 15:17:35 +00:00
|
|
|
|
$browser_version = $log_version[1] . '.' . $log_version2[1];
|
|
|
|
|
$browser_agent = 'safari';
|
|
|
|
|
}
|
|
|
|
|
else if (ereg('Mozilla/([0-9].[0-9]{1,2})', $user_agent, $log_version))
|
2003-09-07 13:46:51 +00:00
|
|
|
|
{
|
|
|
|
|
$browser_version = $log_version[1];
|
2003-10-19 15:17:35 +00:00
|
|
|
|
$browser_agent = 'mozilla';
|
|
|
|
|
}
|
|
|
|
|
else
|
2003-09-07 13:46:51 +00:00
|
|
|
|
{
|
|
|
|
|
$browser_version = 0;
|
|
|
|
|
$browser_agent = 'other';
|
2003-10-19 15:17:35 +00:00
|
|
|
|
}
|
2003-09-07 13:46:51 +00:00
|
|
|
|
|
|
|
|
|
// Correct the mime type - we force application/octetstream for all files, except images
|
|
|
|
|
// Please do not change this, it is a security precaution
|
|
|
|
|
if ($category == NONE_CAT && !strstr($attachment['mimetype'], 'image'))
|
|
|
|
|
{
|
|
|
|
|
$attachment['mimetype'] = ($browser_agent == 'ie' || $browser_agent == 'opera') ? 'application/octetstream' : 'application/octet-stream';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now the tricky part... let's dance
|
|
|
|
|
@ob_end_clean();
|
|
|
|
|
@ini_set('zlib.output_compression', 'Off');
|
|
|
|
|
header('Pragma: public');
|
|
|
|
|
header('Content-Transfer-Encoding: none');
|
|
|
|
|
|
|
|
|
|
// Send out the Headers
|
|
|
|
|
header('Content-Type: ' . $attachment['mimetype'] . '; name="' . $attachment['real_filename'] . '"');
|
|
|
|
|
header('Content-Disposition: inline; filename="' . $attachment['real_filename'] . '"');
|
|
|
|
|
|
|
|
|
|
// Now send the File Contents to the Browser
|
|
|
|
|
$size = @filesize($filename);
|
|
|
|
|
if ($size)
|
|
|
|
|
{
|
|
|
|
|
header("Content-length: $size");
|
|
|
|
|
}
|
2003-10-19 15:17:35 +00:00
|
|
|
|
$result = @readfile($filename);
|
|
|
|
|
|
|
|
|
|
if (!$result)
|
|
|
|
|
{
|
|
|
|
|
trigger_error('Unable to deliver file.<br />Error was: ' . $php_errormsg, E_USER_WARNING);
|
|
|
|
|
}
|
2003-09-07 13:46:51 +00:00
|
|
|
|
|
2003-10-19 15:17:35 +00:00
|
|
|
|
flush();
|
2003-09-07 13:46:51 +00:00
|
|
|
|
exit;
|
|
|
|
|
}
|
2003-11-23 22:25:46 +00:00
|
|
|
|
|
|
|
|
|
function download_allowed()
|
|
|
|
|
{
|
|
|
|
|
global $config, $user, $db;
|
|
|
|
|
|
|
|
|
|
if (!$config['secure_downloads'])
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$url = trim(getenv('HTTP_REFERER'));
|
|
|
|
|
if ($url == '')
|
|
|
|
|
{
|
|
|
|
|
$url = trim($_SERVER['HTTP_REFERER']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($url == '')
|
|
|
|
|
{
|
|
|
|
|
return ($config['secure_allow_empty_referer']) ? true : false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Split URL into domain and script part
|
|
|
|
|
$url = explode('?', str_replace(array('http://', 'https://'), array('', ''), $url));
|
|
|
|
|
$hostname = trim($url[0]);
|
|
|
|
|
unset($url);
|
|
|
|
|
|
|
|
|
|
$allowed = ($config['secure_allow_deny']) ? FALSE : TRUE;
|
|
|
|
|
$iplist = array();
|
|
|
|
|
|
|
|
|
|
$ip_ary = gethostbynamel($hostname);
|
|
|
|
|
|
|
|
|
|
foreach ($ip_ary as $ip)
|
|
|
|
|
{
|
|
|
|
|
if (!empty($ip))
|
|
|
|
|
{
|
|
|
|
|
$iplist[] = $ip;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for own server...
|
|
|
|
|
if (preg_match('#^.*?' . $config['server_name'] . '.*?$#i', $hostname))
|
|
|
|
|
{
|
|
|
|
|
$allowed = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get IP's and Hostnames
|
|
|
|
|
if (!$allowed)
|
|
|
|
|
{
|
|
|
|
|
$sql = 'SELECT site_ip, site_hostname, ip_exclude
|
|
|
|
|
FROM ' . SITELIST_TABLE;
|
|
|
|
|
$result = $db->sql_query($sql);
|
|
|
|
|
|
|
|
|
|
while ($row = $db->sql_fetchrow($result))
|
|
|
|
|
{
|
|
|
|
|
if (!empty($row['site_ip']))
|
|
|
|
|
{
|
|
|
|
|
foreach ($iplist as $ip)
|
|
|
|
|
{
|
|
|
|
|
if (preg_match('#^' . str_replace('*', '.*?', $row['site_ip']) . '$#i', $ip))
|
|
|
|
|
{
|
|
|
|
|
if (!empty($row['ip_exclude']))
|
|
|
|
|
{
|
|
|
|
|
$allowed = ($config['secure_allow_deny']) ? false : true;
|
|
|
|
|
break 2;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$allowed = ($config['secure_allow_deny']) ? true : false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!empty($row['site_hostname']))
|
|
|
|
|
{
|
|
|
|
|
if (preg_match('#^' . str_replace('*', '.*?', $row['site_hostname']) . '$#i', $hostname))
|
|
|
|
|
{
|
|
|
|
|
if (!empty($row['ip_exclude']))
|
|
|
|
|
{
|
|
|
|
|
$allowed = ($config['secure_allow_deny']) ? false : true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$allowed = ($config['secure_allow_deny']) ? true : false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$db->sql_freeresult($result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $allowed;
|
|
|
|
|
}
|
|
|
|
|
|
2003-09-07 13:46:51 +00:00
|
|
|
|
//
|
|
|
|
|
// FUNCTIONS
|
|
|
|
|
// ---------
|
|
|
|
|
|
2003-04-10 21:35:31 +00:00
|
|
|
|
?>
|