From 30fc86fd904af822bd92a4e1a7d19defef2484c3 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Tue, 9 Feb 2010 13:15:37 -0700 Subject: [PATCH] Horde_Mime_Magic improvements. Add 'nostrip' option to prevent stripping MIME parameter information from the return. Improve phpdoc. Remove unused variable. --- framework/Mime/lib/Horde/Mime/Magic.php | 74 +++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 31 deletions(-) diff --git a/framework/Mime/lib/Horde/Mime/Magic.php b/framework/Mime/lib/Horde/Mime/Magic.php index 7a03859c6..7ea026739 100644 --- a/framework/Mime/lib/Horde/Mime/Magic.php +++ b/framework/Mime/lib/Horde/Mime/Magic.php @@ -22,13 +22,6 @@ class Horde_Mime_Magic static protected $_map = null; /** - * The MIME magic file. - * - * @var array - */ - static protected $_magic = null; - - /** * Returns a copy of the MIME extension map. * * @return array The MIME extension map. @@ -140,34 +133,44 @@ class Horde_Mime_Magic } /** - * Uses variants of the UNIX "file" command to attempt to determine the - * MIME type of an unknown file. + * Attempt to determine the MIME type of an unknown file. * * @param string $path The path to the file to analyze. * @param string $magic_db Path to the mime magic database. + * @param array $opts Additional options: + *
+     * 'nostrip' - (boolean) Don't strip parameter information from MIME
+     *             type string.
+     *             DEFAULT: false
+     * 
* - * @return string The MIME type of the file. Returns false if the file - * type isn't recognized or an error happened. + * @return mixed The MIME type of the file. Returns false if the file + * type can not be determined. */ - static public function analyzeFile($path, $magic_db = null) + static public function analyzeFile($path, $magic_db = null, + $opts = array()) { if (Horde_Util::extensionExists('fileinfo')) { $res = empty($magic_db) - ? @finfo_open(FILEINFO_MIME) - : @finfo_open(FILEINFO_MIME, $magic_db); + ? finfo_open(FILEINFO_MIME) + : finfo_open(FILEINFO_MIME, $magic_db); if ($res) { - $type = finfo_file($res, $path); + $type = trim(finfo_file($res, $path)); finfo_close($res); /* Remove any additional information. */ - foreach (array(';', ',', '\\0') as $separator) { - if (($pos = strpos($type, $separator)) !== false) { - $type = rtrim(substr($type, 0, $pos)); + if (empty($opts['nostrip'])) { + foreach (array(';', ',', '\\0') as $separator) { + if (($pos = strpos($type, $separator)) !== false) { + $type = rtrim(substr($type, 0, $pos)); + } } - } - if (preg_match('|^[a-z0-9]+/[.-a-z0-9]+$|i', $type)) { + if (preg_match('|^[a-z0-9]+/[.-a-z0-9]+$|i', $type)) { + return $type; + } + } else { return $type; } } @@ -177,16 +180,22 @@ class Horde_Mime_Magic } /** - * Uses variants of the UNIX "file" command to attempt to determine the - * MIME type of an unknown byte stream. + * Attempt to determine the MIME type of an unknown byte stream. * * @param string $data The file data to analyze. * @param string $magic_db Path to the mime magic database. + * @param array $opts Additional options: + *
+     * 'nostrip' - (boolean) Don't strip parameter information from MIME
+     *             type string.
+     *             DEFAULT: false
+     * 
* - * @return string The MIME type of the file. Returns false if the file - * type isn't recognized or an error happened. + * @return mixed The MIME type of the file. Returns false if the file + * type can not be determined. */ - static public function analyzeData($data, $magic_db = null) + static public function analyzeData($data, $magic_db = null, + $opts = array()) { /* If the PHP Mimetype extension is available, use that. */ if (Horde_Util::extensionExists('fileinfo')) { @@ -198,16 +207,18 @@ class Horde_Mime_Magic return false; } - $type = finfo_buffer($res, $data); + $type = trim(finfo_buffer($res, $data)); finfo_close($res); /* Remove any additional information. */ - if (($pos = strpos($type, ';')) !== false) { - $type = rtrim(substr($type, 0, $pos)); - } + if (empty($opts['nostrip'])) { + if (($pos = strpos($type, ';')) !== false) { + $type = rtrim(substr($type, 0, $pos)); + } - if (($pos = strpos($type, ',')) !== false) { - $type = rtrim(substr($type, 0, $pos)); + if (($pos = strpos($type, ',')) !== false) { + $type = rtrim(substr($type, 0, $pos)); + } } return $type; @@ -215,4 +226,5 @@ class Horde_Mime_Magic return false; } + } -- 2.11.0