Skip to content

Commit

Permalink
Allow content types to be overriden when downloading files
Browse files Browse the repository at this point in the history
Fixes #13439: Wrong Content-type for various MSOffice documents
  • Loading branch information
rombert committed Dec 6, 2011
1 parent bfc04a4 commit fbac390
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
16 changes: 16 additions & 0 deletions config_defaults_inc.php
Expand Up @@ -3486,6 +3486,22 @@
'zip' => 'zip.gif',
'?' => 'generic.gif' );

/**
*
* Content types which will be overriden when downloading files
*
* @global array $g_file_download_content_type_overrides
*/
$g_file_download_content_type_overrides = array (
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template'
);

/**
* Icon associative arrays
* Status to icon mapping
Expand Down
15 changes: 15 additions & 0 deletions core/file_api.php
Expand Up @@ -1063,3 +1063,18 @@ function file_copy_attachments( $p_source_bug_id, $p_dest_bug_id ) {
db_query_bound( $query, Array( $p_dest_bug_id, $t_bug_file['title'], $t_bug_file['description'], $t_new_diskfile_name, $t_new_file_name, $t_bug_file['folder'], $t_bug_file['filesize'], $t_bug_file['file_type'], $t_bug_file['date_added'], $t_bug_file['content'] ) );
}
}

/**
* Returns a possibly override content type for a file name
*
* @param string $p_filename the filename of the file which will be downloaded
* @return string the content type, or empty if it should not be overriden
*/
function file_get_content_type_override( $p_filename ) {

global $g_file_download_content_type_overrides;

$t_extension = pathinfo( $p_filename, PATHINFO_EXTENSION );

return $g_file_download_content_type_overrides[$t_extension];
}
11 changes: 11 additions & 0 deletions file_download.php
Expand Up @@ -167,6 +167,8 @@

$t_content_type = $v_file_type;

$t_content_type_override = file_get_content_type_override ( $t_filename );

# dump file content to the connection.
switch ( config_get( 'file_upload_method' ) ) {
case DISK:
Expand All @@ -180,6 +182,9 @@
$t_content_type = $t_file_info_type;
}
}

if ( $t_content_type_override )
$t_content_type = $t_content_type_override;

header( 'Content-Type: ' . $t_content_type );
if ( config_get( 'file_download_xsendfile_enabled' ) ) {
Expand All @@ -206,6 +211,9 @@
$t_content_type = $t_file_info_type;
}
}

if ( $t_content_type_override )
$t_content_type = $t_content_type_override;

header( 'Content-Type: ' . $t_content_type );
readfile( $t_local_disk_file );
Expand All @@ -218,6 +226,9 @@
$t_content_type = $t_file_info_type;
}
}

if ( $t_content_type_override )
$t_content_type = $t_content_type_override;

header( 'Content-Type: ' . $t_content_type );
echo $v_content;
Expand Down

0 comments on commit fbac390

Please sign in to comment.