Add attachmimetype hook
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 21 Jan 2010 19:03:27 +0000 (12:03 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 21 Jan 2010 19:03:27 +0000 (12:03 -0700)
imp/config/hooks.php.dist
imp/docs/CHANGES
imp/lib/Compose.php

index fb9e90c..c6c6544 100644 (file)
@@ -105,6 +105,7 @@ class IMP_Hooks
 //        }
 //    }
 
+
     /**
      * AJAX HOOK: Add user-defined AJAX action handlers.
      *
@@ -124,10 +125,42 @@ class IMP_Hooks
 //        throw new Horde_Ajax_Exception('Unknown action');
 //     }
 
+
+    /**
+     * Do MIME type determination for compose attachments.
+     *
+     * @param string $filename  The filename.
+     * @param string $tempfile  The location on the local server of the
+     *                          temporary file that contains the attachment
+     *                          data.
+     * @param string $type      The MIME type as reported by the browser.
+     *
+     * @return string  The MIME type of the file.
+     /
+     function attachmimetype($filename, $tempfile, $type)
+     {
+        /* By default, we trust the MIME type reported by the browser if it is
+         * something other than the generic octet-stream type. */
+        if ($type == 'application/octet-stream') {
+            /* Try to determine the MIME type from 1) analysis of the file
+             * (if available) and, if that fails, 2) from the extension. We
+             * do it in this order here because, most likely, if a browser
+             * can't identify the type of a file, it is because the file
+             * extension isn't available and/or recognized. */
+            $type = Horde_Mime_Magic::analyzeFile($tempfile, !empty($GLOBALS['conf']['mime']['magic_db']) ? $GLOBALS['conf']['mime']['magic_db'] : null);
+            if (!$type) {
+                $type = Horde_Mime_Magic::filenameToMIME($filename, false);
+            }
+        }
+
+        return $type;
+    }
+
+
     /**
      * Perform an action after a message has been sent successfully.
      *
-     * @param Horde_Mime_Part $message  The message content object.
+     * @param Horde_Mime_Part $message     The message content object.
      * @param Horde_Mime_Headers $message  The message headers object.
      */
 //    function postsent($message, $headers)
index bb0aef3..b6e36c7 100644 (file)
@@ -2,6 +2,7 @@
 v5.0-git
 --------
 
+[mms] Add hook to allow determination of compose attachments MIME type.
 [mms] Move AJAX processing framework to Horde (Request #4561).
 [mms] If selected message(s) disappear from mailbox, gracefully handle in the
       user interface (DIMP).
index f72073b..41e2536 100644 (file)
@@ -1686,21 +1686,16 @@ class IMP_Compose
             throw new IMP_Compose_Exception(sprintf(_("Attached file \"%s\" exceeds the attachment size limits. File NOT attached."), $filename), 'horde.error');
         }
 
-        /* Store the data in a Horde_Mime_Part. Some browsers do not send the
-         * MIME type so try an educated guess. */
-        if (!empty($_FILES[$name]['type']) &&
-            ($_FILES[$name]['type'] != 'application/octet-stream')) {
-            $type = $_FILES[$name]['type'];
-        } else {
-            /* Try to determine the MIME type from 1) analysis of the file
-             * (if available) and, if that fails, 2) from the extension. We
-             * do it in this order here because, most likely, if a browser
-             * can't identify the type of a file, it is because the file
-             * extension isn't available and/or recognized. */
-            if (!($type = Horde_Mime_Magic::analyzeFile($tempfile, !empty($conf['mime']['magic_db']) ? $conf['mime']['magic_db'] : null))) {
-                $type = Horde_Mime_Magic::filenameToMIME($filename, false);
-            }
-        }
+        /* Determine the MIME type of the data. */
+        $type = empty($_FILES[$name]['type'])
+            ? 'application/octet-stream'
+            : $_FILES[$name]['type'];
+
+        /* User hook to do MIME magic determinations. */
+        try {
+            $type = Horde::callHook('attachmimetype', array($filename, $tempfile, $type), 'imp');
+        } catch (Horde_Exception_HookNotSet $e) {}
+
         $part = new Horde_Mime_Part();
         $part->setType($type);
         $part->setCharset(Horde_Nls::getCharset());