Skip to content

Commit

Permalink
NOBUG mimetypes - improve a bit mimeinfo_from_icon() so, before using…
Browse files Browse the repository at this point in the history
… the

"return last" strategy (leading to all images returning image/tiff always)
it will try one "return by extension match" if possible.
  • Loading branch information
stronk7 committed May 4, 2010
1 parent c329e37 commit 637a60d
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/filelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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'!
}
Expand All @@ -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)
}

Expand Down

0 comments on commit 637a60d

Please sign in to comment.