Adding ability to hook into ANY attachment type. Also, please review my comments...
[squirrelmail.git] / functions / mime.php
index 6210c57fc8d1db3cc97f5bef94b31d0c327222f3..35ce6e7c3e4771351162dc0c61b2b3c7c80386d4 100644 (file)
@@ -313,15 +313,9 @@ function translateText(&$body, $wrap_at, $charset) {
         }
 
         if ($quotes % 2) {
         }
 
         if ($quotes % 2) {
-            if (!isset($color[13])) {
-                $color[13] = '#800000';
-            }
-            $line = '<font color="' . $color[13] . '">' . $line . '</font>';
+            $line = '<span class="quote1">' . $line . '</span>';
         } elseif ($quotes) {
         } elseif ($quotes) {
-            if (!isset($color[14])) {
-                $color[14] = '#FF0000';
-            }
-            $line = '<font color="' . $color[14] . '">' . $line . '</font>';
+            $line = '<span class="quote2">' . $line . '</span>';
         }
 
         $body_ary[$i] = $line;
         }
 
         $body_ary[$i] = $line;
@@ -420,33 +414,11 @@ function formatBody($imap_stream, $message, $color, $wrap_at, $ent_num, $id, $ma
                     . '&amp;ent_id=' . $ent_num
                     . '&amp;view_unsafe_images=' . (int) $view_unsafe_images;
 
                     . '&amp;ent_id=' . $ent_num
                     . '&amp;view_unsafe_images=' . (int) $view_unsafe_images;
 
-                // adding warning message
-                $body = html_tag('div',_("Viewing HTML formatted email"),'center');
-
-                /**
-                 * height can't be set to 100%, because it does not work as expected when
-                 * iframe is inside the table. Browsers do not create full height objects
-                 * even when iframe is not nested. Maybe there is some way to get full size
-                 * with CSS. Tested in firefox 1.02 and opera 7.53
-                 *
-                 * width="100%" does not work as expected, when table width is not set (automatic)
-                 *
-                 * tokul: I think <iframe> are safer sandbox than <object>. Objects might
-                 * need special handling for IE and IE6SP2.
-                 */
-                $body.= "<div><iframe name=\"message_frame\" width=\"100%\" height=\"$iframe_height\" src=\"$iframeurl\""
-                    .' frameborder="1" marginwidth="0" marginheight="0" scrolling="auto">' . "\n";
-
-                // Message for browsers without iframe support
-                //$body.= _("Your browser does not support inline frames.
-                // You can view HTML formated message by following below link.");
-                //$body.= "<br /><a href=\"$iframeurl\">"._("View HTML Message")."</a>";
-
-                // if browser can't render iframe, it renders html message.
-                $body.= $html_body;
-
-                // close iframe
-                $body.="</iframe></div>\n";
+                global $oTemplate;
+                $oTemplate->assign('iframe_url', $iframeurl);
+                $oTemplate->assign('html_body', $html_body);
+                
+                $body = $oTemplate->fetch('read_html_iframe.tpl');
             } else {
                 // old way of html rendering
                 $body = magicHTML($body, $id, $message, $mailbox);
             } else {
                 // old way of html rendering
                 $body = magicHTML($body, $id, $message, $mailbox);
@@ -497,40 +469,34 @@ function formatBody($imap_stream, $message, $color, $wrap_at, $ent_num, $id, $ma
 }
 
 /**
 }
 
 /**
- * Displays attachment links and information
- *
- * Since 1.3.0 function is not included in formatBody() call.
- *
- * Since 1.0.2 uses attachment $type0/$type1 hook.
- * Since 1.2.5 uses attachment $type0/* hook.
- * Since 1.5.0 uses attachments_bottom hook.
- *
+ * Generate attachments array for passing to templates.  Separated from
+ * formatAttachments() below so that the same array can be given to the
+ * print-friendly version.
+ * 
+ * @since 1.5.2
  * @param object $message SquirrelMail message object
  * @param array $exclude_id message parts that are not attachments.
  * @param string $mailbox mailbox name
  * @param integer $id message id
  * @param object $message SquirrelMail message object
  * @param array $exclude_id message parts that are not attachments.
  * @param string $mailbox mailbox name
  * @param integer $id message id
- * @return string html formated attachment information.
  */
  */
-function formatAttachments($message, $exclude_id, $mailbox, $id) {
+function buildAttachmentArray($message, $exclude_id, $mailbox, $id) {
     global $where, $what, $startMessage, $color, $passed_ent_id, $base_uri;
 
     $att_ar = $message->getAttachments($exclude_id);
     global $where, $what, $startMessage, $color, $passed_ent_id, $base_uri;
 
     $att_ar = $message->getAttachments($exclude_id);
-
-    if (!count($att_ar)) return '';
-
-    $attachments = '';
-
     $urlMailbox = urlencode($mailbox);
 
     $urlMailbox = urlencode($mailbox);
 
+    $attachments = array();
     foreach ($att_ar as $att) {
         $ent = $att->entity_id;
         $header = $att->header;
         $type0 = strtolower($header->type0);
         $type1 = strtolower($header->type1);
         $name = '';
     foreach ($att_ar as $att) {
         $ent = $att->entity_id;
         $header = $att->header;
         $type0 = strtolower($header->type0);
         $type1 = strtolower($header->type1);
         $name = '';
+        $links = array();
         $links['download link']['text'] = _("Download");
         $links['download link']['href'] = $base_uri .
             "src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;ent_id=$ent";
         $links['download link']['text'] = _("Download");
         $links['download link']['href'] = $base_uri .
             "src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;ent_id=$ent";
+            
         if ($type0 =='message' && $type1 == 'rfc822') {
             $default_page = $base_uri  . 'src/read_body.php';
             $rfc822_header = $att->rfc822_header;
         if ($type0 =='message' && $type1 == 'rfc822') {
             $default_page = $base_uri  . 'src/read_body.php';
             $rfc822_header = $att->rfc822_header;
@@ -551,7 +517,7 @@ function formatAttachments($message, $exclude_id, $mailbox, $id) {
             } else {
                 $from_name = _("Unknown sender");
             }
             } else {
                 $from_name = _("Unknown sender");
             }
-            $description = $from_name;
+            $description = _("From").': '.$from_name;
         } else {
             $default_page = $base_uri  . 'src/download.php';
             $filename = $att->getFilename();
         } else {
             $default_page = $base_uri  . 'src/download.php';
             $filename = $att->getFilename();
@@ -577,50 +543,84 @@ function formatAttachments($message, $exclude_id, $mailbox, $id) {
 
         /* This executes the attachment hook with a specific MIME-type.
          * If that doesn't have results, it tries if there's a rule
 
         /* This executes the attachment hook with a specific MIME-type.
          * If that doesn't have results, it tries if there's a rule
-         * for a more generic type.
+         * for a more generic type. Finally, a hook for ALL attachment
+         * types is run as well.
          */
         $hookresults = do_hook("attachment $type0/$type1", $links,
                 $startMessage, $id, $urlMailbox, $ent, $defaultlink,
                 $display_filename, $where, $what);
          */
         $hookresults = do_hook("attachment $type0/$type1", $links,
                 $startMessage, $id, $urlMailbox, $ent, $defaultlink,
                 $display_filename, $where, $what);
+// FIXME: How does this code ever get executed?  $hookresults[1] is $links
+//        from above, and should always be returned by do_hook().  I will
+//        test this and fix if needed (but it has been working, right???)
+//        and get rid of these comments (Paul)
+// UPDATE: Preliminary testing shows that in fact I am correct that the
+//         following never gets executed (unless a strange plugin actually
+//         unsets the contents of the links)... how did this ever work???
+//         Can someonen please show a plugin that worked here?
         if(count($hookresults[1]) <= 1) {
             $hookresults = do_hook("attachment $type0/*", $links,
                     $startMessage, $id, $urlMailbox, $ent, $defaultlink,
                     $display_filename, $where, $what);
         }
         if(count($hookresults[1]) <= 1) {
             $hookresults = do_hook("attachment $type0/*", $links,
                     $startMessage, $id, $urlMailbox, $ent, $defaultlink,
                     $display_filename, $where, $what);
         }
+        $hookresults = do_hook("attachment */*", $hookresults[1],
+                $startMessage, $id, $urlMailbox, $ent, $hookresults[6], 
+                $display_filename, $where, $what);
 
         $links = $hookresults[1];
         $defaultlink = $hookresults[6];
 
 
         $links = $hookresults[1];
         $defaultlink = $hookresults[6];
 
-        $attachments .= '<tr><td>' .
-            '<a href="'.$defaultlink.'">'.decodeHeader($display_filename).'</a>&nbsp;</td>' .
-            '<td><small><b>' . show_readable_size($header->size) .
-            '</b>&nbsp;&nbsp;</small></td>' .
-            '<td><small>[ '.htmlspecialchars($type0).'/'.htmlspecialchars($type1).' ]&nbsp;</small></td>' .
-            '<td><small>';
-        $attachments .= '<b>' . $description . '</b>';
-        $attachments .= '</small></td><td><small>&nbsp;';
-
-        $skipspaces = 1;
+        $this_attachment = array();
+        $this_attachment['Name'] = decodeHeader($display_filename);
+        $this_attachment['Description'] = $description;
+        $this_attachment['DefaultHREF'] = $defaultlink;
+        $this_attachment['DownloadHREF'] = $links['download link']['href'];
+        $this_attachment['ViewHREF'] = isset($links['attachment_common']) ? $links['attachment_common']['href'] : '';
+        $this_attachment['Size'] = $header->size;
+        $this_attachment['ContentType'] = htmlspecialchars($type0 .'/'. $type1);
+        $this_attachment['OtherLinks'] = array();
         foreach ($links as $val) {
         foreach ($links as $val) {
-            if ($skipspaces) {
-                $skipspaces = 0;
-            } else {
-                $attachments .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
-            }
-            $attachments .= '<a href="' . $val['href'] . '">'
-                . (isset($val['text']) && !empty($val['text']) ? $val['text'] : '')
-                . (isset($val['extra']) && !empty($val['extra']) ? $val['extra'] : '')
-                . '</a>';
+            if ($val['text']==_("Download") || $val['text'] == _("View"))
+                continue;
+            if (empty($val['text']) && empty($val['extra']))
+                continue;
+                
+            $temp = array();
+            $temp['HREF'] = $val['href'];
+            $temp['Text'] = (empty($val['text']) ? '' : $val['text']) . (empty($val['extra']) ? '' : $val['extra']);
+            $this_attachment['OtherLinks'][] = $temp;
         }
         }
+        $attachments[] = $this_attachment;
+        
         unset($links);
         unset($links);
-        $attachments .= "</td></tr>\n";
     }
     }
-    $attachmentadd = do_hook_function('attachments_bottom',$attachments);
-    if ($attachmentadd != '')
-        $attachments = $attachmentadd;
+    
     return $attachments;
 }
 
     return $attachments;
 }
 
+/**
+ * Displays attachment links and information
+ *
+ * Since 1.3.0 function is not included in formatBody() call.
+ *
+ * Since 1.0.2 uses attachment $type0/$type1 hook.
+ * Since 1.2.5 uses attachment $type0/* hook.
+ * Since 1.5.0 uses attachments_bottom hook.
+ * Since 1.5.2 uses templates and does *not* return a value.
+ *
+ * @param object $message SquirrelMail message object
+ * @param array $exclude_id message parts that are not attachments.
+ * @param string $mailbox mailbox name
+ * @param integer $id message id
+ */
+function formatAttachments($message, $exclude_id, $mailbox, $id) {
+    global $oTemplate;
+    
+    $attach = buildAttachmentArray($message, $exclude_id, $mailbox, $id);
+
+    $oTemplate->assign('attachments', $attach);
+    $oTemplate->display('read_attachments.tpl');
+}
+
 function sqimap_base64_decode(&$string) {
 
     // Base64 encoded data goes in pairs of 4 bytes. To achieve on the
 function sqimap_base64_decode(&$string) {
 
     // Base64 encoded data goes in pairs of 4 bytes. To achieve on the
@@ -2102,6 +2102,8 @@ function magicHTML($body, $id, $message, $mailbox = 'INBOX', $take_mailto_links
            $has_unsafe_images;
     /**
      * Don't display attached images in HTML mode.
            $has_unsafe_images;
     /**
      * Don't display attached images in HTML mode.
+     * 
+     * SB: why?
      */
     $attachment_common_show_images = false;
     $tag_list = Array(
      */
     $attachment_common_show_images = false;
     $tag_list = Array(