From 39aff3ca3991f8a07ef053200cb3e9666d1b7dfd Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Mon, 8 Sep 2008 08:48:21 +0000
Subject: [PATCH] Module: Update list of MIME types, and add a fallback for
 mime_content_type().

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@859 44740490-163a-0410-bde0-09ae8108e29a
---
 www/module.php | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/www/module.php b/www/module.php
index 73c36b21d..e07a87110 100644
--- a/www/module.php
+++ b/www/module.php
@@ -19,12 +19,13 @@ $indexFiles = array('index.php', 'index.html', 'index.htm', 'index.txt');
 
 /* MIME types - key is file extension, value is MIME type. */
 $mimeTypes = array(
-	'bml' => 'image/x-ms-bmp',
+	'bmp' => 'image/x-ms-bmp',
 	'css' => 'text/css',
 	'gif' => 'image/gif',
 	'htm' => 'text/html',
 	'html' => 'text/html',
 	'shtml' => 'text/html',
+	'ico' => 'image/vnd.microsoft.icon',
 	'jpe' => 'image/jpeg',
 	'jpeg' => 'image/jpeg',
 	'jpg' => 'image/jpeg',
@@ -114,15 +115,25 @@ try {
 	/* Some other file type - attempt to serve it. */
 
 	/* Find MIME type for file, based on extension. */
+	$contentType = NULL;
 	if (preg_match('#\.([^/]+)$#', $path, $type)) {
 		$type = strtolower($type[1]);
 		if (array_key_exists($type, $mimeTypes)) {
 			$contentType = $mimeTypes[$type];
-		} else {
+		}
+	}
+
+	if ($contentType === NULL) {
+		/* We were unable to determine the MIME type from the file extension. Fall back
+		 * to mime_content_type (if it exists).
+		 */
+		if (function_exists('mime_content_type')) {
 			$contentType = mime_content_type($path);
+		} else {
+			/* mime_content_type doesn't exist. Return a default MIME type. */
+			SimpleSAML_Logger::warning('Unable to determine mime content type of file: ' . $path);
+			$contentType = 'application/octet-stream';
 		}
-	} else {
-		$contentType = mime_content_type($path);
 	}
 
 	$contentLength = sprintf('%u', filesize($path)); /* Force filesize to an unsigned number. */
-- 
GitLab