Horde_Mime_Magic improvements.
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 9 Feb 2010 20:15:37 +0000 (13:15 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 9 Feb 2010 20:30:22 +0000 (13:30 -0700)
Add 'nostrip' option to prevent stripping MIME parameter information
from the return.
Improve phpdoc.
Remove unused variable.

framework/Mime/lib/Horde/Mime/Magic.php

index 7a03859..7ea0267 100644 (file)
@@ -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:
+     * <pre>
+     * 'nostrip' - (boolean) Don't strip parameter information from MIME
+     *             type string.
+     *             DEFAULT: false
+     * </pre>
      *
-     * @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:
+     * <pre>
+     * 'nostrip' - (boolean) Don't strip parameter information from MIME
+     *             type string.
+     *             DEFAULT: false
+     * </pre>
      *
-     * @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;
     }
+
 }