From 637a60dc5fde253587f53b097bfd45652dadec53 Mon Sep 17 00:00:00 2001 From: Eloy Lafuente Date: Tue, 4 May 2010 14:19:00 +0000 Subject: [PATCH] NOBUG mimetypes - improve a bit mimeinfo_from_icon() so, before using the "return last" strategy (leading to all images returning image/tiff always) it will try one "return by extension match" if possible. --- lib/filelib.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/filelib.php b/lib/filelib.php index 2572199e05f..e4ee837a11f 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -1290,7 +1290,7 @@ function mimeinfo_from_type($element, $mimetype) { * * @param string $element Desired information (usually 'icon') * @param string $icon Icon file path. - * @param boolean $all return all matching entries (defaults to false - last match) + * @param boolean $all return all matching entries (defaults to false - best (by ext)/last match) * @return string Requested piece of information from array */ function mimeinfo_from_icon($element, $icon, $all=false) { @@ -1299,11 +1299,16 @@ function mimeinfo_from_icon($element, $icon, $all=false) { if (preg_match("/\/(.*)/", $icon, $matches)) { $icon = $matches[1]; } + // Try to get the extension + $extension = ''; + if (($cutat = strrpos($icon, '.')) !== false && $cutat < strlen($icon)-1) { + $extension = substr($icon, $cutat + 1); + } $info = array($mimeinfo['xxx'][$element]); // Default - foreach($mimeinfo as $values) { + foreach($mimeinfo as $key => $values) { if($values['icon']==$icon) { if(isset($values[$element])) { - $info[] = $values[$element]; + $info[$key] = $values[$element]; } //No break, for example for 'excel.gif' we don't want 'csv'! } @@ -1312,8 +1317,14 @@ function mimeinfo_from_icon($element, $icon, $all=false) { if (count($info) > 1) { array_shift($info); // take off document/unknown if we have better options } - return $info; + return array_values($info); // Keep keys out when requesting all } + + // Requested only one, try to get the best by extension coincidence, else return the last + if ($extension && isset($info[$extension])) { + return $info[$extension]; + } + return array_pop($info); // Return last match (mimicking behaviour/comment inside foreach loop) }