|
|
@ -6,26 +6,27 @@ |
|
|
|
* @file |
|
|
|
* @file |
|
|
|
* This class inspired by Chris Jean's work, here: |
|
|
|
* This class inspired by Chris Jean's work, here: |
|
|
|
* http://chrisjean.com/2009/02/14/generating-mime-type-in-php-is-not-magic/ |
|
|
|
* http://chrisjean.com/2009/02/14/generating-mime-type-in-php-is-not-magic/ |
|
|
|
* |
|
|
|
* |
|
|
|
* It does some MIME trickery, inspired by the need to to deal with Openoffice |
|
|
|
* It does some MIME trickery, inspired by the need to to deal with Openoffice |
|
|
|
* and MS Office 2007 file formats -- which are often mis-interpreted by |
|
|
|
* and MS Office 2007 file formats -- which are often mis-interpreted by |
|
|
|
* mime-magic, fileinfo, and the *nix `file` command. |
|
|
|
* mime-magic, fileinfo, and the *nix `file` command. |
|
|
|
* |
|
|
|
* |
|
|
|
* In Drupal 6, we also make use of file_get_mimetype. See: |
|
|
|
* In Drupal 6, we also make use of file_get_mimetype. See: |
|
|
|
* http://api.drupal.org/api/function/file_get_mimetype/6 |
|
|
|
* http://api.drupal.org/api/function/file_get_mimetype/6 |
|
|
|
* ... however this only provides a uni-directional lookup (ext->mime). |
|
|
|
* ... however this only provides a uni-directional lookup (ext->mime). |
|
|
|
* While I don't have a specific use case for a mime->extension lookup, I think |
|
|
|
* While I don't have a specific use case for a mime->extension lookup, I think |
|
|
|
* it's good to have in here. |
|
|
|
* it's good to have in here. |
|
|
|
* |
|
|
|
* |
|
|
|
* Drupal 7 will have better mime handlers. See: |
|
|
|
* Drupal 7 will have better mime handlers. See: |
|
|
|
* http://api.drupal.org/api/function/file_default_mimetype_mapping/7 |
|
|
|
* http://api.drupal.org/api/function/file_default_mimetype_mapping/7 |
|
|
|
* |
|
|
|
* |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
class MimeClass { |
|
|
|
class MimeClass { |
|
|
|
|
|
|
|
|
|
|
|
private $private_mime_types = array( |
|
|
|
private $private_mime_types = array( |
|
|
|
/** |
|
|
|
/* |
|
|
|
* This is a shortlist of mimetypes which should catch most |
|
|
|
* This is a shortlist of mimetypes which should catch most |
|
|
|
* mimetype<-->extension lookups in the context of Islandora collections. |
|
|
|
* mimetype<-->extension lookups in the context of Islandora collections. |
|
|
|
* |
|
|
|
* |
|
|
|
* It has been cut from a much longer list. |
|
|
|
* It has been cut from a much longer list. |
|
|
@ -33,26 +34,26 @@ class MimeClass { |
|
|
|
* Two types of mimetypes should be put in this list: |
|
|
|
* Two types of mimetypes should be put in this list: |
|
|
|
* 1) Special emerging formats which may not yet be expressed in the system |
|
|
|
* 1) Special emerging formats which may not yet be expressed in the system |
|
|
|
* mime.types file. |
|
|
|
* mime.types file. |
|
|
|
* 2) Heavily used mimetypes of particular importance to the Islandora |
|
|
|
* 2) Heavily used mimetypes of particular importance to the Islandora |
|
|
|
* project, as lookups against this list will be quicker and less |
|
|
|
* project, as lookups against this list will be quicker and less |
|
|
|
* resource intensive than other methods. |
|
|
|
* resource intensive than other methods. |
|
|
|
* |
|
|
|
* |
|
|
|
* Lookups are first checked against this short list. If no results are found, |
|
|
|
* Lookups are first checked against this short list. |
|
|
|
* then the lookup function may move on to check other sources, namely the |
|
|
|
* If no results are found, then the lookup function may move |
|
|
|
* system's mime.types file. |
|
|
|
* on to check other sources, namely the system's mime.types file. |
|
|
|
* |
|
|
|
* |
|
|
|
* In most cases though, this short list should suffice. |
|
|
|
* In most cases though, this short list should suffice. |
|
|
|
* |
|
|
|
* |
|
|
|
* If modifying this list, please note that for promiscuous mimetypes |
|
|
|
* If modifying this list, please note that for promiscuous mimetypes |
|
|
|
* (those which map to multiple extensions, such as text/plain) |
|
|
|
* (those which map to multiple extensions, such as text/plain) |
|
|
|
* The function get_extension will always return the *LAST* extension in this list, |
|
|
|
* The function get_extension will always return the *LAST* extension |
|
|
|
* so you should put your preferred extension *LAST*. |
|
|
|
* in this list, so you should put your preferred extension *LAST*. |
|
|
|
* |
|
|
|
* |
|
|
|
* e.g... |
|
|
|
* e.g... |
|
|
|
* "jpeg" => "image/jpeg", |
|
|
|
* "jpeg" => "image/jpeg", |
|
|
|
* "jpe" => "image/jpeg", |
|
|
|
* "jpe" => "image/jpeg", |
|
|
|
* "jpg" => "image/jpeg", |
|
|
|
* "jpg" => "image/jpeg", |
|
|
|
* |
|
|
|
* |
|
|
|
* $this->get_extension('image/jpeg') will always return 'jpg'. |
|
|
|
* $this->get_extension('image/jpeg') will always return 'jpg'. |
|
|
|
* |
|
|
|
* |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -89,7 +90,7 @@ class MimeClass { |
|
|
|
'ksp' => 'application/x-kspread', |
|
|
|
'ksp' => 'application/x-kspread', |
|
|
|
'kwt' => 'application/x-kword', |
|
|
|
'kwt' => 'application/x-kword', |
|
|
|
'kwd' => 'application/x-kword', |
|
|
|
'kwd' => 'application/x-kword', |
|
|
|
// ms office 97: |
|
|
|
// MS office 97: |
|
|
|
'doc' => 'application/msword', |
|
|
|
'doc' => 'application/msword', |
|
|
|
'xls' => 'application/vnd.ms-excel', |
|
|
|
'xls' => 'application/vnd.ms-excel', |
|
|
|
'ppt' => 'application/vnd.ms-powerpoint', |
|
|
|
'ppt' => 'application/vnd.ms-powerpoint', |
|
|
@ -113,9 +114,9 @@ class MimeClass { |
|
|
|
'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12', |
|
|
|
'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12', |
|
|
|
'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide', |
|
|
|
'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide', |
|
|
|
'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12', |
|
|
|
'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12', |
|
|
|
// wordperfect (who cares?): |
|
|
|
// Wordperfect (who cares?): |
|
|
|
'wpd' => 'application/wordperfect', |
|
|
|
'wpd' => 'application/wordperfect', |
|
|
|
// common and generic containers: |
|
|
|
// Common and generic containers: |
|
|
|
'pdf' => 'application/pdf', |
|
|
|
'pdf' => 'application/pdf', |
|
|
|
'eps' => 'application/postscript', |
|
|
|
'eps' => 'application/postscript', |
|
|
|
'ps' => 'application/postscript', |
|
|
|
'ps' => 'application/postscript', |
|
|
@ -185,13 +186,14 @@ class MimeClass { |
|
|
|
"ogg" => "audio/ogg", |
|
|
|
"ogg" => "audio/ogg", |
|
|
|
"flac" => "audio/x-flac", |
|
|
|
"flac" => "audio/x-flac", |
|
|
|
"wav" => "audio/vnd.wave", |
|
|
|
"wav" => "audio/vnd.wave", |
|
|
|
// compressed formats: (note: http://svn.cleancode.org/svn/email/trunk/mime.types) |
|
|
|
/* Compressed formats: |
|
|
|
|
|
|
|
(note: http://svn.cleancode.org/svn/email/trunk/mime.types).*/ |
|
|
|
"tgz" => "application/x-gzip", |
|
|
|
"tgz" => "application/x-gzip", |
|
|
|
"gz" => "application/x-gzip", |
|
|
|
"gz" => "application/x-gzip", |
|
|
|
"tar" => "application/x-tar", |
|
|
|
"tar" => "application/x-tar", |
|
|
|
"gtar" => "application/x-gtar", |
|
|
|
"gtar" => "application/x-gtar", |
|
|
|
"zip" => "application/x-zip", |
|
|
|
"zip" => "application/x-zip", |
|
|
|
// others: |
|
|
|
// others: |
|
|
|
'bin' => 'application/octet-stream', |
|
|
|
'bin' => 'application/octet-stream', |
|
|
|
); |
|
|
|
); |
|
|
|
private $private_file_extensions; |
|
|
|
private $private_file_extensions; |
|
|
@ -204,10 +206,10 @@ class MimeClass { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function __construct() { |
|
|
|
public function __construct() { |
|
|
|
|
|
|
|
|
|
|
|
// populate the reverse shortlist: |
|
|
|
// Populate the reverse shortlist: |
|
|
|
$this->private_file_extensions = array_flip($this->private_mime_types); |
|
|
|
$this->private_file_extensions = array_flip($this->private_mime_types); |
|
|
|
|
|
|
|
|
|
|
|
// pick up a local mime.types file if it is available |
|
|
|
// Pick up a local mime.types file if it is available. |
|
|
|
if (is_readable('mime.types')) { |
|
|
|
if (is_readable('mime.types')) { |
|
|
|
$this->etc_mime_types = 'mime.types'; |
|
|
|
$this->etc_mime_types = 'mime.types'; |
|
|
|
} |
|
|
|
} |
|
|
@ -219,7 +221,7 @@ class MimeClass { |
|
|
|
* for backwards-compatibility with our old mimetype class. |
|
|
|
* for backwards-compatibility with our old mimetype class. |
|
|
|
* |
|
|
|
* |
|
|
|
* @param type $filename |
|
|
|
* @param type $filename |
|
|
|
* @return type |
|
|
|
* @return type |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function getType($filename) { |
|
|
|
public function getType($filename) { |
|
|
|
return $this->get_mimetype($filename); |
|
|
|
return $this->get_mimetype($filename); |
|
|
@ -227,40 +229,47 @@ class MimeClass { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* function: get_mimetype |
|
|
|
* function: get_mimetype |
|
|
|
* description: returns a mimetype associated with the file extension of $filename |
|
|
|
* description: returns a mimetype associated with the |
|
|
|
|
|
|
|
* file extension of $filename |
|
|
|
* |
|
|
|
* |
|
|
|
* @param type $filename |
|
|
|
* @param type $filename |
|
|
|
* @param type $debug |
|
|
|
* @param type $debug |
|
|
|
* @return type |
|
|
|
* @return string |
|
|
|
|
|
|
|
* mimetype associated with the file extension of $filename |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function get_mimetype($filename, $debug = FALSE) { |
|
|
|
public function get_mimetype($filename, $debug = FALSE) { |
|
|
|
$ext = strtolower(substr($filename, strrpos($filename, '.') + 1)); |
|
|
|
$ext = strtolower(substr($filename, strrpos($filename, '.') + 1)); |
|
|
|
|
|
|
|
|
|
|
|
if (!empty($this->private_mime_types[$ext])) { |
|
|
|
if (!empty($this->private_mime_types[$ext])) { |
|
|
|
if (TRUE === $debug) |
|
|
|
if (TRUE === $debug) { |
|
|
|
return array('mime_type' => $this->private_mime_types[$ext], 'method' => 'from_array'); |
|
|
|
return array('mime_type' => $this->private_mime_types[$ext], 'method' => 'from_array'); |
|
|
|
|
|
|
|
} |
|
|
|
return $this->private_mime_types[$ext]; |
|
|
|
return $this->private_mime_types[$ext]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (function_exists('file_get_mimetype')) { |
|
|
|
if (function_exists('file_get_mimetype')) { |
|
|
|
$drupal_mimetype = file_get_mimetype($filename); |
|
|
|
$drupal_mimetype = file_get_mimetype($filename); |
|
|
|
if ('application/octet-stream' != $drupal_mimetype) { |
|
|
|
if ('application/octet-stream' != $drupal_mimetype) { |
|
|
|
if (TRUE == $debug) |
|
|
|
if (TRUE == $debug) { |
|
|
|
return array('mime_type' => $drupal_mimetype, 'method' => 'file_get_mimetype'); |
|
|
|
return array('mime_type' => $drupal_mimetype, 'method' => 'file_get_mimetype'); |
|
|
|
|
|
|
|
} |
|
|
|
return $drupal_mimetype; |
|
|
|
return $drupal_mimetype; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!isset($this->system_types)) |
|
|
|
if (!isset($this->system_types)) { |
|
|
|
$this->system_types = $this->system_extension_mime_types(); |
|
|
|
$this->system_types = $this->system_extension_mime_types(); |
|
|
|
|
|
|
|
} |
|
|
|
if (isset($this->system_types[$ext])) { |
|
|
|
if (isset($this->system_types[$ext])) { |
|
|
|
if (TRUE == $debug) |
|
|
|
if (TRUE == $debug) { |
|
|
|
return array('mime_type' => $this->system_types[$ext], 'method' => 'mime.types'); |
|
|
|
return array('mime_type' => $this->system_types[$ext], 'method' => 'mime.types'); |
|
|
|
|
|
|
|
} |
|
|
|
return $this->system_types[$ext]; |
|
|
|
return $this->system_types[$ext]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (TRUE === $debug) |
|
|
|
if (TRUE === $debug) { |
|
|
|
return array('mime_type' => 'application/octet-stream', 'method' => 'last_resort'); |
|
|
|
return array('mime_type' => 'application/octet-stream', 'method' => 'last_resort'); |
|
|
|
|
|
|
|
} |
|
|
|
return 'application/octet-stream'; |
|
|
|
return 'application/octet-stream'; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -270,34 +279,38 @@ class MimeClass { |
|
|
|
* |
|
|
|
* |
|
|
|
* @param type $mime_type |
|
|
|
* @param type $mime_type |
|
|
|
* @param type $debug |
|
|
|
* @param type $debug |
|
|
|
* @return type |
|
|
|
* @return type |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function get_extension($mime_type, $debug = FALSE) { |
|
|
|
public function get_extension($mime_type, $debug = FALSE) { |
|
|
|
|
|
|
|
|
|
|
|
if (!empty($this->private_file_extensions[$mime_type])) { |
|
|
|
if (!empty($this->private_file_extensions[$mime_type])) { |
|
|
|
if (TRUE == $debug) |
|
|
|
if (TRUE == $debug) { |
|
|
|
return array('extension' => $this->private_file_extensions[$mime_type], 'method' => 'from_array'); |
|
|
|
return array('extension' => $this->private_file_extensions[$mime_type], 'method' => 'from_array'); |
|
|
|
|
|
|
|
} |
|
|
|
return $this->private_file_extensions[$mime_type]; |
|
|
|
return $this->private_file_extensions[$mime_type]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!isset($this->system_exts)) |
|
|
|
if (!isset($this->system_exts)) { |
|
|
|
$this->system_exts = $this->system_mime_type_extensions(); |
|
|
|
$this->system_exts = $this->system_mime_type_extensions(); |
|
|
|
|
|
|
|
} |
|
|
|
if (isset($this->system_exts[$mime_type])) { |
|
|
|
if (isset($this->system_exts[$mime_type])) { |
|
|
|
if (TRUE == $debug) |
|
|
|
if (TRUE == $debug) { |
|
|
|
return array('extension' => $this->system_exts[$mime_type], 'method' => 'mime.types'); |
|
|
|
return array('extension' => $this->system_exts[$mime_type], 'method' => 'mime.types'); |
|
|
|
|
|
|
|
} |
|
|
|
return $this->system_exts[$mime_type]; |
|
|
|
return $this->system_exts[$mime_type]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (TRUE == $debug) |
|
|
|
if (TRUE == $debug) { |
|
|
|
return array('extension' => 'bin', 'method' => 'last_resort'); |
|
|
|
return array('extension' => 'bin', 'method' => 'last_resort'); |
|
|
|
|
|
|
|
} |
|
|
|
return 'bin'; |
|
|
|
return 'bin'; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* function: system_mime_type_extensions |
|
|
|
* function: system_mime_type_extensions |
|
|
|
* description: populates an internal array of mimetype/extension associations |
|
|
|
* description: populates an internal array of mimetype/extension associations |
|
|
|
* from the system mime.types file, or a local mime.types if one is found (see |
|
|
|
* from the system mime.types file, or a local mime.types if one is found (see |
|
|
|
* __constuctor). |
|
|
|
* __constuctor). |
|
|
|
* return: array of mimetype => extension |
|
|
|
* return: array of mimetype => extension |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private function system_mime_type_extensions() { |
|
|
|
private function system_mime_type_extensions() { |
|
|
@ -306,15 +319,18 @@ class MimeClass { |
|
|
|
$file = fopen($this->etc_mime_types, 'r'); |
|
|
|
$file = fopen($this->etc_mime_types, 'r'); |
|
|
|
while (($line = fgets($file)) !== FALSE) { |
|
|
|
while (($line = fgets($file)) !== FALSE) { |
|
|
|
$line = trim(preg_replace('/#.*/', '', $line)); |
|
|
|
$line = trim(preg_replace('/#.*/', '', $line)); |
|
|
|
if (!$line) |
|
|
|
if (!$line) { |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
$parts = preg_split('/\s+/', $line); |
|
|
|
$parts = preg_split('/\s+/', $line); |
|
|
|
if (count($parts) == 1) |
|
|
|
if (count($parts) == 1) { |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
// A single part means a mimetype without extensions, which we ignore. |
|
|
|
// A single part means a mimetype without extensions, which we ignore. |
|
|
|
$type = array_shift($parts); |
|
|
|
$type = array_shift($parts); |
|
|
|
if (!isset($out[$type])) |
|
|
|
if (!isset($out[$type])) { |
|
|
|
$out[$type] = array_shift($parts); |
|
|
|
$out[$type] = array_shift($parts); |
|
|
|
|
|
|
|
} |
|
|
|
// We take the first ext from the line if many are present. |
|
|
|
// We take the first ext from the line if many are present. |
|
|
|
} |
|
|
|
} |
|
|
|
fclose($file); |
|
|
|
fclose($file); |
|
|
@ -325,8 +341,8 @@ class MimeClass { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* function: system_mime_type_extensions |
|
|
|
* function: system_mime_type_extensions |
|
|
|
* description: populates an internal array of mimetype/extension associations |
|
|
|
* description: populates an internal array of mimetype/extension associations |
|
|
|
* from the system mime.types file, or a local mime.types if one is found (see |
|
|
|
* from the system mime.types file, or a local mime.types if one is found (see |
|
|
|
* __constuctor). |
|
|
|
* __constuctor). |
|
|
|
* return: array of extension => mimetype |
|
|
|
* return: array of extension => mimetype |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private function system_extension_mime_types() { |
|
|
|
private function system_extension_mime_types() { |
|
|
@ -335,15 +351,18 @@ class MimeClass { |
|
|
|
$file = fopen($this->etc_mime_types, 'r'); |
|
|
|
$file = fopen($this->etc_mime_types, 'r'); |
|
|
|
while (($line = fgets($file)) !== FALSE) { |
|
|
|
while (($line = fgets($file)) !== FALSE) { |
|
|
|
$line = trim(preg_replace('/#.*/', '', $line)); |
|
|
|
$line = trim(preg_replace('/#.*/', '', $line)); |
|
|
|
if (!$line) |
|
|
|
if (!$line) { |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
$parts = preg_split('/\s+/', $line); |
|
|
|
$parts = preg_split('/\s+/', $line); |
|
|
|
if (count($parts) == 1) |
|
|
|
if (count($parts) == 1) { |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
// A single part means a mimetype without extensions, which we ignore. |
|
|
|
// A single part means a mimetype without extensions, which we ignore. |
|
|
|
$type = array_shift($parts); |
|
|
|
$type = array_shift($parts); |
|
|
|
foreach ($parts as $part) |
|
|
|
foreach ($parts as $part) { |
|
|
|
$out[$part] = $type; |
|
|
|
$out[$part] = $type; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
fclose($file); |
|
|
|
fclose($file); |
|
|
|
} |
|
|
|
} |
|
|
@ -351,4 +370,3 @@ class MimeClass { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|