diff options
author | Aleksander Machniak <alec@alec.pl> | 2013-05-28 20:59:44 +0200 |
---|---|---|
committer | Aleksander Machniak <alec@alec.pl> | 2013-05-28 20:59:44 +0200 |
commit | fe0f1d589b2320905fa3dd73737d5b7b1cbc402a (patch) | |
tree | f350019cd9446e2f1651047f2d120f12a939e9f3 | |
parent | b91bc101a0a3c8227b225ce557954f6a1e654ed3 (diff) |
Improve rcube_utils::file2class() to not return duplicates
-rw-r--r-- | program/lib/Roundcube/rcube_utils.php | 13 | ||||
-rw-r--r-- | tests/Framework/Utils.php | 16 |
2 files changed, 26 insertions, 3 deletions
diff --git a/program/lib/Roundcube/rcube_utils.php b/program/lib/Roundcube/rcube_utils.php index 8467107fe..23f24a46a 100644 --- a/program/lib/Roundcube/rcube_utils.php +++ b/program/lib/Roundcube/rcube_utils.php @@ -510,17 +510,24 @@ class rcube_utils */ public static function file2class($mimetype, $filename) { + $mimetype = strtolower($mimetype); + $filename = strtolower($filename); + list($primary, $secondary) = explode('/', $mimetype); $classes = array($primary ? $primary : 'unknown'); + if ($secondary) { $classes[] = $secondary; } - if (preg_match('/\.([a-z0-9]+)$/i', $filename, $m)) { - $classes[] = $m[1]; + + if (preg_match('/\.([a-z0-9]+)$/', $filename, $m)) { + if (!in_array($m[1], $classes)) { + $classes[] = $m[1]; + } } - return strtolower(join(" ", $classes)); + return join(" ", $classes); } diff --git a/tests/Framework/Utils.php b/tests/Framework/Utils.php index 7c1e92ac8..65efeecff 100644 --- a/tests/Framework/Utils.php +++ b/tests/Framework/Utils.php @@ -229,4 +229,20 @@ class Framework_Utils extends PHPUnit_Framework_TestCase } } + /** + * rcube:utils::file2class() + */ + function test_file2class() + { + $test = array( + array('', '', 'unknown'), + array('text', 'text', 'text'), + array('image/png', 'image.png', 'image png'), + ); + + foreach ($test as $v) { + $result = rcube_utils::file2class($v[0], $v[1]); + $this->assertSame($v[2], $result); + } + } } |