send_status_line(403, 'Forbidden'); trigger_error(sprintf($user->lang['EXTENSION_DISABLED_AFTER_POSTING'], $attachment['extension'])); } } $download_mode = (int) $extensions[$attachment['extension']]['download_mode']; $display_cat = $extensions[$attachment['extension']]['display_cat']; 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; } if ($thumbnail) { $attachment['physical_filename'] = 'thumb_' . $attachment['physical_filename']; } else { if ($display_cat == ATTACHMENT_CATEGORY_NONE && !$attachment['is_orphan'] && !src_http_byte_range($attachment['filesize'])) { // Update download count src_increment_downloads($db, $attachment['attach_id']); } } if ($display_cat == ATTACHMENT_CATEGORY_IMAGE && $mode === 'view' && strpos($attachment['mimetype'], 'image') === 0 && strpos(strtolower($user->browser), 'msie') !== false && !src_is_greater_ie_version($user->browser, 7)) { wrap_img_in_html(append_sid($src_root_path . 'download/file.' . $phpEx, 'id=' . $attachment['attach_id']), $attachment['real_filename']); file_gc(); } else { // Determine the 'presenting'-method if ($download_mode == PHYSICAL_LINK) { // This presenting method should no longer be used if (!@is_dir($src_root_path . $config['upload_path'])) { send_status_line(500, 'Internal Server Error'); trigger_error($user->lang['PHYSICAL_DOWNLOAD_NOT_POSSIBLE']); }
/** * Send file to browser */ function send_file_to_browser($attachment, $upload_dir, $category) { global $user, $db, $config, $src_root_path; $filename = $src_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'); } // Make sure the database record for the filesize is correct if ($size > 0 && $size != $attachment['filesize']) { // Update database record $sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' SET filesize = ' . (int) $size . ' WHERE attach_id = ' . (int) $attachment['attach_id']; $db->sql_query($sql); } // Now the tricky part... let's dance header('Cache-Control: public'); // Send out the Headers. Do not set Content-Disposition to inline please, it is a security measure for users using the Internet Explorer. header('Content-Type: ' . $attachment['mimetype']); if (src_is_greater_ie_version($user->browser, 7)) { 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) || strpos(strtolower($user->browser), 'msie') !== false && !src_is_greater_ie_version($user->browser, 7)) { 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: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT'); } } else { header('Content-Disposition: ' . (strpos($attachment['mimetype'], 'image') === 0 ? 'inline' : 'attachment') . '; ' . header_filename(htmlspecialchars_decode($attachment['real_filename']))); if (src_is_greater_ie_version($user->browser, 7) && strpos($attachment['mimetype'], 'image') !== 0) { header('X-Download-Options: noopen'); } } } // Close the db connection before sending the file etc. file_gc(false); if (!set_modified_headers($attachment['filetime'], $user->browser)) { // We make sure those have to be enabled manually by defining a constant // because of the potential disclosure of full attachment path // in case support for features is absent in the webserver software. if (defined('src_ENABLE_X_ACCEL_REDIRECT') && src_ENABLE_X_ACCEL_REDIRECT) { // X-Accel-Redirect - http://wiki.nginx.org/XSendfile header('X-Accel-Redirect: ' . $user->page['root_script_path'] . $upload_dir . '/' . $attachment['physical_filename']); exit; } else { if (defined('src_ENABLE_X_SENDFILE') && src_ENABLE_X_SENDFILE && !src_http_byte_range($size)) { // X-Sendfile - http://blog.lighttpd.net/articles/2006/07/02/x-sendfile // Lighttpd's X-Sendfile does not support range requests as of 1.4.26 // and always requires an absolute path. header('X-Sendfile: ' . dirname(__FILE__) . "/../{$upload_dir}/{$attachment['physical_filename']}"); exit; } } if ($size) { header("Content-Length: {$size}"); } // Try to deliver in chunks @set_time_limit(0); $fp = @fopen($filename, 'rb'); if ($fp !== false) { // Deliver file partially if requested if ($range = src_http_byte_range($size)) { fseek($fp, $range['byte_pos_start']); send_status_line(206, 'Partial Content'); header('Content-Range: bytes ' . $range['byte_pos_start'] . '-' . $range['byte_pos_end'] . '/' . $range['bytes_total']); header('Content-Length: ' . $range['bytes_requested']); } while (!feof($fp)) { echo fread($fp, 8192); } fclose($fp); } else { @readfile($filename); } flush(); } exit; }