From 8749e94b4bed36500e4f45c65cc16cfd5633ef34 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Fri, 08 Jun 2012 05:14:53 -0400
Subject: [PATCH] Fix attachment sizes in message print page and attachment preview page (#1488515)   - Use size parameter from Content-Disposition header if specified,   - Calculate size of base64 encoded message parts

---
 program/steps/mail/func.inc |   78 +++++++++++++++++++++++++++------------
 1 files changed, 54 insertions(+), 24 deletions(-)

diff --git a/program/steps/mail/func.inc b/program/steps/mail/func.inc
index 829f159..5a18ded 100644
--- a/program/steps/mail/func.inc
+++ b/program/steps/mail/func.inc
@@ -947,7 +947,7 @@
 
   // single header value is requested
   if (!empty($attrib['valueof']))
-    return Q($plugin['output'][$attrib['valueof']]['value'], ($hkey == 'subject' ? 'strict' : 'show'));
+    return Q($plugin['output'][$attrib['valueof']]['value'], ($attrib['valueof'] == 'subject' ? 'strict' : 'show'));
 
   // compose html table
   $table = new html_table(array('cols' => 2));
@@ -1097,12 +1097,6 @@
     }
   }
 
-  // Skip TIFF images if browser doesn't support this format
-  // @TODO: we could convert TIFF to JPEG and display it
-  $tiff_support = !empty($_SESSION['browser_caps']) && !empty($_SESSION['browser_caps']['tif']);
-  $mime_regex   = $tiff_support ? '/^image\//i' : '/^image\/(?!tif)/i';
-  $ext_regex    = '/\.(jpg|jpeg|png|gif|bmp' . ($tiff_support ? '|tif|tiff' : '') .')$/i';
-
   // list images after mail body
   if ($CONFIG['inline_images'] && !empty($MESSAGE->attachments)) {
     foreach ($MESSAGE->attachments as $attach_prop) {
@@ -1112,12 +1106,7 @@
       }
 
       // Content-Type: image/*...
-      if (preg_match($mime_regex, $attach_prop->mimetype) ||
-        // ...or known file extension: many clients are using application/octet-stream
-        ($attach_prop->filename &&
-          preg_match('/^application\/octet-stream$/i', $attach_prop->mimetype) &&
-          preg_match($ext_regex, $attach_prop->filename))
-      ) {
+      if (rcmail_part_image_type($attach_prop)) {
         $out .= html::tag('hr') . html::p(array('align' => "center"),
           html::img(array(
             'src' => $MESSAGE->get_part_url($attach_prop->mime_id, true),
@@ -1135,6 +1124,48 @@
   return html::div($attrib, $out);
 }
 
+function rcmail_part_image_type($part)
+{
+  $rcmail = rcmail::get_instance();
+
+  // Skip TIFF images if browser doesn't support this format...
+  $tiff_support = !empty($_SESSION['browser_caps']) && !empty($_SESSION['browser_caps']['tif']);
+  // until we can convert them to JPEG
+  $tiff_support = $tiff_support || $rcmail->config->get('im_convert_path');
+
+  // Content-type regexp
+  $mime_regex = $tiff_support ? '/^image\//i' : '/^image\/(?!tif)/i';
+
+  // Content-Type: image/*...
+  if (preg_match($mime_regex, $part->mimetype)) {
+    return $part->mimetype;
+  }
+
+  // Many clients use application/octet-stream, we'll detect mimetype
+  // by checking filename extension
+
+  // Supported image filename extensions to image type map
+  $types = array(
+    'jpg'  => 'image/jpeg',
+    'jpeg' => 'image/jpeg',
+    'png'  => 'image/png',
+    'gif'  => 'image/gif',
+    'bmp'  => 'image/bmp',
+  );
+  if ($tiff_support) {
+    $types['tif']  = 'image/tiff';
+    $types['tiff'] = 'image/tiff';
+  }
+
+  if ($part->filename
+    && preg_match('/^application\/octet-stream$/i', $part->mimetype)
+    && preg_match('/\.([^.])$/i', $part->filename, $m)
+    && ($extension = strtolower($m[1]))
+    && isset($types[$extension])
+  ) {
+    return $types[$extension];
+  }
+}
 
 /**
  * Convert all relative URLs according to a <base> in HTML
@@ -1162,14 +1193,15 @@
   // find STYLE tags
   while (($pos = stripos($body, '<style', $last_style_pos)) && ($pos2 = stripos($body, '</style>', $pos)))
   {
-    $pos = strpos($body, '>', $pos)+1;
+    $pos = strpos($body, '>', $pos) + 1;
+    $len = $pos2 - $pos;
 
     // replace all css definitions with #container [def]
-    $styles = rcmail_mod_css_styles(
-      substr($body, $pos, $pos2-$pos), $cont_id, $allow_remote);
+    $styles = substr($body, $pos, $len);
+    $styles = rcmail_mod_css_styles($styles, $cont_id, $allow_remote);
 
-    $body = substr_replace($body, $styles, $pos, $pos2-$pos);
-    $last_style_pos = $pos2;
+    $body = substr_replace($body, $styles, $pos, $len);
+    $last_style_pos = $pos2 + strlen($styles) - $len;
   }
 
   // modify HTML links to open a new window if clicked
@@ -1445,13 +1477,13 @@
 
 function rcmail_message_part_controls($attrib)
 {
-  global $MESSAGE;
+  global $MESSAGE, $RCMAIL;
 
   $part = asciiwords(get_input_value('_part', RCUBE_INPUT_GPC));
   if (!is_object($MESSAGE) || !is_array($MESSAGE->parts) || !($_GET['_uid'] && $_GET['_part']) || !$MESSAGE->mime_parts[$part])
     return '';
 
-  $part = $MESSAGE->mime_parts[$part];
+  $part  = $MESSAGE->mime_parts[$part];
   $table = new html_table(array('cols' => 3));
 
   $filename = $part->filename;
@@ -1465,10 +1497,8 @@
     $table->add('download-link', html::a(array('href' => './?'.str_replace('_frame=', '_download=', $_SERVER['QUERY_STRING'])), Q(rcube_label('download'))));
   }
 
-  if (!empty($part->size)) {
-    $table->add('title', Q(rcube_label('filesize')));
-    $table->add('header', Q(show_bytes($part->size)));
-  }
+  $table->add('title', Q(rcube_label('filesize')));
+  $table->add('header', Q($RCMAIL->message_part_size($part)));
 
   return $table->show($attrib);
 }

--
Gitblit v1.9.1