Skip to content

Commit bfc04a4

Browse files
committedDec 6, 2011
Allow overriding content-type for text/ and image/ files served by plugins
Fixes #13193: Files served by plugins do not have a Content-Type header set Conflicts: file_download.php
1 parent 8ddb68c commit bfc04a4

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed
 

‎core/plugin_api.php

+20-3
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,27 @@ function plugin_file_include( $p_filename, $p_basename = null ) {
152152
trigger_error( ERROR_GENERIC, ERROR );
153153
}
154154

155-
$t_extension = pathinfo( $t_file_path, PATHINFO_EXTENSION );
156-
if ( $t_extension && array_key_exists( $t_extension , $g_plugin_mime_types ) ) {
157-
header('Content-Type: ' . $g_plugin_mime_types [ $t_extension ] );
155+
$t_content_type = '';
156+
$finfo = finfo_get_if_available();
157+
158+
if ( $finfo ) {
159+
$t_file_info_type = $finfo->file( $t_file_path );
160+
if ( $t_file_info_type !== false ) {
161+
$t_content_type = $t_file_info_type;
162+
}
163+
}
164+
165+
// allow overriding the content type for specific text and image extensions
166+
// see bug #13193 for details
167+
if ( strpos($t_content_type, 'text/') === 0 || strpos( $t_content_type, 'image/') === 0 ) {
168+
$t_extension = pathinfo( $t_file_path, PATHINFO_EXTENSION );
169+
if ( $t_extension && array_key_exists( $t_extension , $g_plugin_mime_types ) ) {
170+
$t_content_type = $g_plugin_mime_types [ $t_extension ];
171+
}
158172
}
173+
174+
if ( $t_content_type )
175+
header('Content-Type: ' . $t_content_type );
159176

160177
readfile( $t_file_path );
161178
}

‎core/utility_api.php

+19
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,22 @@ function get_font_path() {
264264
}
265265
return $t_font_path;
266266
}
267+
268+
function finfo_get_if_available() {
269+
270+
if ( class_exists( 'finfo' ) ) {
271+
$t_info_file = config_get( 'fileinfo_magic_db_file' );
272+
273+
if ( is_blank( $t_info_file ) ) {
274+
$finfo = new finfo( FILEINFO_MIME );
275+
} else {
276+
$finfo = new finfo( FILEINFO_MIME, $t_info_file );
277+
}
278+
279+
if ( $finfo ) {
280+
return $finfo;
281+
}
282+
}
283+
284+
return null;
285+
}

‎file_download.php

+4-17
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,7 @@
163163
header( 'Content-Length: ' . $v_filesize );
164164

165165
# If finfo is available (always true for PHP >= 5.3.0) we can use it to determine the MIME type of files
166-
$finfo_available = false;
167-
if ( class_exists( 'finfo' ) ) {
168-
$t_info_file = config_get( 'fileinfo_magic_db_file' );
169-
170-
if ( is_blank( $t_info_file ) ) {
171-
$finfo = new finfo( FILEINFO_MIME );
172-
} else {
173-
$finfo = new finfo( FILEINFO_MIME, $t_info_file );
174-
}
175-
176-
if ( $finfo ) {
177-
$finfo_available = true;
178-
}
179-
}
166+
$finfo = finfo_get_if_available();;
180167

181168
$t_content_type = $v_file_type;
182169

@@ -186,7 +173,7 @@
186173
$t_local_disk_file = file_normalize_attachment_path( $v_diskfile, $t_project_id );
187174

188175
if ( file_exists( $t_local_disk_file ) ) {
189-
if ( $finfo_available ) {
176+
if ( $finfo ) {
190177
$t_file_info_type = $finfo->file( $t_local_disk_file );
191178

192179
if ( $t_file_info_type !== false ) {
@@ -212,7 +199,7 @@
212199
file_ftp_disconnect( $ftp );
213200
}
214201

215-
if ( $finfo_available ) {
202+
if ( $finfo ) {
216203
$t_file_info_type = $finfo->file( $t_local_disk_file );
217204

218205
if ( $t_file_info_type !== false ) {
@@ -224,7 +211,7 @@
224211
readfile( $t_local_disk_file );
225212
break;
226213
default:
227-
if ( $finfo_available ) {
214+
if ( $finfo ) {
228215
$t_file_info_type = $finfo->buffer( $v_content );
229216

230217
if ( $t_file_info_type !== false ) {

0 commit comments

Comments
 (0)
Please sign in to comment.