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