BUGFIX Ensure HTTP::get_mime_type() checks the full path in

file_exists() before using the finfo class
This commit is contained in:
Sean Harvey 2012-05-18 09:22:02 +12:00
parent c9bcfd49ec
commit 78423c1bd0

View File

@ -196,7 +196,10 @@ class HTTP {
} }
/** /**
* Get mime type based on file extension. * Get the MIME type based on a file's extension.
*
* If the finfo class exists in PHP, and the file actually exists, then use that
* extension, otherwise fallback to a list of commonly known MIME types.
* *
* @uses finfo * @uses finfo
* @param string $filename Relative path to filename from project root, e.g. "mysite/tests/file.csv" * @param string $filename Relative path to filename from project root, e.g. "mysite/tests/file.csv"
@ -204,9 +207,10 @@ class HTTP {
*/ */
public static function get_mime_type($filename) { public static function get_mime_type($filename) {
// If the finfo module is compiled into PHP, use it. // If the finfo module is compiled into PHP, use it.
if(class_exists('finfo') && file_exists($filename)) { $path = BASE_PATH . DIRECTORY_SEPARATOR . $filename;
if(class_exists('finfo') && file_exists($path)) {
$finfo = new finfo(FILEINFO_MIME_TYPE); $finfo = new finfo(FILEINFO_MIME_TYPE);
return $finfo->file(BASE_PATH . DIRECTORY_SEPARATOR . $filename); return $finfo->file($path);
} }
// Fallback to use the list from the HTTP.yml configuration and rely on the file extension // Fallback to use the list from the HTTP.yml configuration and rely on the file extension
@ -214,10 +218,12 @@ class HTTP {
$ext = File::get_file_extension($filename); $ext = File::get_file_extension($filename);
// Get the mime-types // Get the mime-types
$mimeTypes = Config::inst()->get('HTTP', 'MimeTypes'); $mimeTypes = Config::inst()->get('HTTP', 'MimeTypes');
// The mime type doesn't exist // The mime type doesn't exist
if(!isset($mimeTypes[$ext])) { if(!isset($mimeTypes[$ext])) {
return 'application/unknown'; return 'application/unknown';
} }
return $mimeTypes[$ext]; return $mimeTypes[$ext];
} }