Rework compose attachment hook.
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 24 Aug 2010 18:21:18 +0000 (12:21 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 24 Aug 2010 18:22:11 +0000 (12:22 -0600)
Rename from 'attachmimetype' to 'compose_attach', since more than just
MIME type determination can be done in the hook.
Add virus scanning example to hook.

imp/config/hooks.php.dist
imp/lib/Compose.php

index 1029c8d..18a1979 100644 (file)
@@ -135,7 +135,7 @@ class IMP_Hooks
 
 
     /**
-     * Do MIME type determination for compose attachments.
+     * Perform actions on files uploaded for use as compose attachments.
      *
      * This hook is enabled by default.
      *
@@ -146,9 +146,12 @@ class IMP_Hooks
      * @param string $type      The MIME type as reported by the browser.
      *
      * @return string  The MIME type of the file.
+     * @throws IMP_Compose_Exception
      */
-     public function attachmimetype($filename, $tempfile, $type)
+     public function compose_attach($filename, $tempfile, $type)
      {
+        /* Do MIME type determination for compose attachments. */
+
         /* 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') {
@@ -163,6 +166,24 @@ class IMP_Hooks
             }
         }
 
+//        // Example: Do a virus scan on the attachment, and reject attachment
+//        // if a virus is found.
+//        // This example uses the open source ClamAV binary (tested with
+//        // v0.96). See: http://www.clamav.net/
+//        $clamscan = '/path/to/clamscan';
+//        exec($clamscan . ' --quiet ' . escapeshellarg($tempfile), null, $return_var);
+//        switch ($return_var) {
+//        case 1:
+//            // Virus found.
+//            Horde::logMessage('Virus found in uploaded attachment', 'INFO');
+//            throw new IMP_Compose_Exception('Virus found in uploaded attachment. Attachment will not be added to the compose message.');
+//
+//        case 2:
+//            // Error occurred.
+//            Horde::logMessage('Unknown error when scanning message for virus.', 'INFO');
+//            break;
+//        }
+
         return $type;
     }
 
index 5a95ea9..ec4702e 100644 (file)
@@ -1829,9 +1829,9 @@ class IMP_Compose
             ? 'application/octet-stream'
             : $_FILES[$name]['type'];
 
-        /* User hook to do MIME magic determinations. */
+        /* User hook to do file scanning/MIME magic determinations. */
         try {
-            $type = Horde::callHook('attachmimetype', array($filename, $tempfile, $type), 'imp');
+            $type = Horde::callHook('compose_attach', array($filename, $tempfile, $type), 'imp');
         } catch (Horde_Exception_HookNotSet $e) {}
 
         $part = new Horde_Mime_Part();