diff options
Diffstat (limited to 'phpBB/download/file.php')
-rw-r--r-- | phpBB/download/file.php | 446 |
1 files changed, 35 insertions, 411 deletions
diff --git a/phpBB/download/file.php b/phpBB/download/file.php index bf277c69fa..c01b0789de 100644 --- a/phpBB/download/file.php +++ b/phpBB/download/file.php @@ -2,9 +2,8 @@ /** * * @package phpBB3 -* @version $Id$ * @copyright (c) 2005 phpBB Group -* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 * */ @@ -39,14 +38,27 @@ if (isset($_GET['avatar'])) exit; } - require($phpbb_root_path . 'includes/acm/acm_' . $acm_type . '.' . $phpEx); - require($phpbb_root_path . 'includes/cache.' . $phpEx); + require($phpbb_root_path . 'includes/class_loader.' . $phpEx); require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx); require($phpbb_root_path . 'includes/constants.' . $phpEx); require($phpbb_root_path . 'includes/functions.' . $phpEx); - + require($phpbb_root_path . 'includes/functions_download' . '.' . $phpEx); + require($phpbb_root_path . 'includes/utf/utf_tools.' . $phpEx); + + $phpbb_class_loader_ext = new phpbb_class_loader('phpbb_ext_', $phpbb_root_path . 'ext/', ".$phpEx"); + $phpbb_class_loader_ext->register(); + $phpbb_class_loader = new phpbb_class_loader('phpbb_', $phpbb_root_path . 'includes/', ".$phpEx"); + $phpbb_class_loader->register(); + + // set up caching + $cache_factory = new phpbb_cache_factory($acm_type); + $cache = $cache_factory->get_service(); + $phpbb_class_loader_ext->set_cache($cache->get_driver()); + $phpbb_class_loader->set_cache($cache->get_driver()); + + $phpbb_dispatcher = new phpbb_event_dispatcher(); + $request = new phpbb_request(); $db = new $sql_db(); - $cache = new cache(); // Connect to DB if (!@$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, false)) @@ -55,10 +67,21 @@ if (isset($_GET['avatar'])) } unset($dbpasswd); + request_var('', 0, false, false, $request); + // worst-case default - $browser = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : 'msie 6.0'; + $browser = strtolower($request->header('User-Agent', 'msie 6.0')); + + $config = new phpbb_config_db($db, $cache->get_driver(), CONFIG_TABLE); + set_config(null, null, null, $config); + set_config_count(null, null, null, $config); + + // load extensions + $phpbb_extension_manager = new phpbb_extension_manager($db, EXT_TABLE, $phpbb_root_path, ".$phpEx", $cache->get_driver()); + + $phpbb_subscriber_loader = new phpbb_event_extension_subscriber_loader($phpbb_dispatcher, $phpbb_extension_manager); + $phpbb_subscriber_loader->load(); - $config = $cache->obtain_config(); $filename = request_var('avatar', ''); $avatar_group = false; $exit = false; @@ -108,6 +131,7 @@ if (isset($_GET['avatar'])) // implicit else: we are not in avatar mode include($phpbb_root_path . 'common.' . $phpEx); +require($phpbb_root_path . 'includes/functions_download' . '.' . $phpEx); $download_id = request_var('id', 0); $mode = request_var('mode', ''); @@ -178,8 +202,7 @@ else $row = $db->sql_fetchrow($result); $db->sql_freeresult($result); - // Global announcement? - $f_download = (!$row) ? $auth->acl_getf_global('f_download') : $auth->acl_get('f_download', $row['forum_id']); + $f_download = $auth->acl_get('f_download', $row['forum_id']); if ($auth->acl_get('u_download') && $f_download) { @@ -246,7 +269,7 @@ if (!download_allowed()) $download_mode = (int) $extensions[$attachment['extension']]['download_mode']; // Fetching filename here to prevent sniffing of filename -$sql = 'SELECT attach_id, is_orphan, in_message, post_msg_id, extension, physical_filename, real_filename, mimetype, filetime +$sql = 'SELECT attach_id, is_orphan, in_message, post_msg_id, extension, physical_filename, real_filename, mimetype, filesize, filetime FROM ' . ATTACHMENTS_TABLE . " WHERE attach_id = $download_id"; $result = $db->sql_query_limit($sql, 1); @@ -276,7 +299,7 @@ if ($thumbnail) { $attachment['physical_filename'] = 'thumb_' . $attachment['physical_filename']; } -else if (($display_cat == ATTACHMENT_CATEGORY_NONE/* || $display_cat == ATTACHMENT_CATEGORY_IMAGE*/) && !$attachment['is_orphan']) +else if (($display_cat == ATTACHMENT_CATEGORY_NONE/* || $display_cat == ATTACHMENT_CATEGORY_IMAGE*/) && !$attachment['is_orphan'] && !phpbb_http_byte_range($attachment['filesize'])) { // Update download count $sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' @@ -311,402 +334,3 @@ else file_gc(); } } - - -/** -* A simplified function to deliver avatars -* The argument needs to be checked before calling this function. -*/ -function send_avatar_to_browser($file, $browser) -{ - global $config, $phpbb_root_path; - - $prefix = $config['avatar_salt'] . '_'; - $image_dir = $config['avatar_path']; - - // Adjust image_dir path (no trailing slash) - if (substr($image_dir, -1, 1) == '/' || substr($image_dir, -1, 1) == '\\') - { - $image_dir = substr($image_dir, 0, -1) . '/'; - } - $image_dir = str_replace(array('../', '..\\', './', '.\\'), '', $image_dir); - - if ($image_dir && ($image_dir[0] == '/' || $image_dir[0] == '\\')) - { - $image_dir = ''; - } - $file_path = $phpbb_root_path . $image_dir . '/' . $prefix . $file; - - if ((@file_exists($file_path) && @is_readable($file_path)) && !headers_sent()) - { - header('Pragma: public'); - - $image_data = @getimagesize($file_path); - header('Content-Type: ' . image_type_to_mime_type($image_data[2])); - - if (strpos(strtolower($browser), 'msie') !== false && strpos(strtolower($browser), 'msie 8.0') === false) - { - header('Content-Disposition: attachment; ' . header_filename($file)); - - 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"); - } - - if (@readfile($file_path) == false) - { - $fp = @fopen($file_path, 'rb'); - - if ($fp !== false) - { - while (!feof($fp)) - { - echo fread($fp, 8192); - } - fclose($fp); - } - } - - flush(); - } - else - { - send_status_line(404, 'Not Found'); - } -} - -/** -* 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>'; -} - -/** -* Send file to browser -*/ -function send_file_to_browser($attachment, $upload_dir, $category) -{ - global $user, $db, $config, $phpbb_root_path; - - $filename = $phpbb_root_path . $upload_dir . '/' . $attachment['physical_filename']; - - if (!@file_exists($filename)) - { - send_status_line(404, 'Not Found'); - trigger_error('ERROR_NO_ATTACHMENT'); - } - - // 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 != ATTACHMENT_CATEGORY_IMAGE || strpos($attachment['mimetype'], 'image') !== 0) - { - $attachment['mimetype'] = (strpos(strtolower($user->browser), 'msie') !== false || strpos(strtolower($user->browser), 'opera') !== false) ? 'application/octetstream' : 'application/octet-stream'; - } - - if (@ob_get_length()) - { - @ob_end_clean(); - } - - // Now send the File Contents to the Browser - $size = @filesize($filename); - - // 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. - if (headers_sent() || !@file_exists($filename) || !@is_readable($filename)) - { - // PHP track_errors setting On? - if (!empty($php_errormsg)) - { - send_status_line(500, 'Internal Server Error'); - trigger_error($user->lang['UNABLE_TO_DELIVER_FILE'] . '<br />' . sprintf($user->lang['TRACKED_PHP_ERROR'], $php_errormsg)); - } - - send_status_line(500, 'Internal Server Error'); - trigger_error('UNABLE_TO_DELIVER_FILE'); - } - - // Now the tricky part... let's dance - header('Pragma: public'); - - /** - * 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) - * { - * header('X-Sendfile: ' . $filename); - * } - * </code> - */ - - // Send out the Headers. Do not set Content-Disposition to inline please, it is a security measure for users using the Internet Explorer. - $is_ie8 = (strpos(strtolower($user->browser), 'msie 8.0') !== false); - header('Content-Type: ' . $attachment['mimetype']); - - if ($is_ie8) - { - header('X-Content-Type-Options: nosniff'); - } - - if ($category == ATTACHMENT_CATEGORY_FLASH && request_var('view', 0) === 1) - { - // 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'); - } - else - { - if (empty($user->browser) || (!$is_ie8 && (strpos(strtolower($user->browser), 'msie') !== false))) - { - 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']))); - if ($is_ie8 && (strpos($attachment['mimetype'], 'image') !== 0)) - { - header('X-Download-Options: noopen'); - } - } - } - - if ($size) - { - header("Content-Length: $size"); - } - - // Close the db connection before sending the file - $db->sql_close(); - - if (!set_modified_headers($attachment['filetime'], $user->browser)) - { - // Try to deliver in chunks - @set_time_limit(0); - - $fp = @fopen($filename, 'rb'); - - if ($fp !== false) - { - while (!feof($fp)) - { - echo fread($fp, 8192); - } - fclose($fp); - } - else - { - @readfile($filename); - } - - flush(); - } - file_gc(); -} - -/** -* Get a browser friendly UTF-8 encoded filename -*/ -function header_filename($file) -{ - $user_agent = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : ''; - - // 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) - { - return "filename=" . rawurlencode($file); - } - - // follow the RFC for extended filename for the rest - return "filename*=UTF-8''" . rawurlencode($file); -} - -/** -* Check if downloading item is allowed -*/ -function download_allowed() -{ - global $config, $user, $db; - - if (!$config['secure_downloads']) - { - return true; - } - - $url = (!empty($_SERVER['HTTP_REFERER'])) ? trim($_SERVER['HTTP_REFERER']) : trim(getenv('HTTP_REFERER')); - - if (!$url) - { - return ($config['secure_allow_empty_referer']) ? true : false; - } - - // Split URL into domain and script part - $url = @parse_url($url); - - if ($url === false) - { - return ($config['secure_allow_empty_referer']) ? true : false; - } - - $hostname = $url['host']; - unset($url); - - $allowed = ($config['secure_allow_deny']) ? false : true; - $iplist = array(); - - if (($ip_ary = @gethostbynamel($hostname)) !== false) - { - foreach ($ip_ary as $ip) - { - if ($ip) - { - $iplist[] = $ip; - } - } - } - - // Check for own server... - $server_name = $user->host; - - // 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)) - { - $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)) - { - $site_ip = trim($row['site_ip']); - $site_hostname = trim($row['site_hostname']); - - if ($site_ip) - { - foreach ($iplist as $ip) - { - if (preg_match('#^' . str_replace('\*', '.*?', preg_quote($site_ip, '#')) . '$#i', $ip)) - { - if ($row['ip_exclude']) - { - $allowed = ($config['secure_allow_deny']) ? false : true; - break 2; - } - else - { - $allowed = ($config['secure_allow_deny']) ? true : false; - } - } - } - } - - if ($site_hostname) - { - if (preg_match('#^' . str_replace('\*', '.*?', preg_quote($site_hostname, '#')) . '$#i', $hostname)) - { - if ($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; -} - -/** -* 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; - if ((strpos(strtolower($browser), 'msie 6.0') === false) && (strpos(strtolower($browser), 'msie 8.0') === false)) - { - if ($last_load !== false && $last_load >= $stamp) - { - send_status_line(304, 'Not Modified'); - // 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; -} - -?>
\ No newline at end of file |