From cb3dfdfedef18381a30fd9b80c9f7120af1031a4 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Sat, 11 Sep 2010 09:21:34 -0400
Subject: [PATCH] - Fix messages background image handling in some cases (#1486990)

---
 CHANGELOG                   |    1 +
 program/include/main.inc    |    8 ++++----
 program/steps/mail/func.inc |   51 ++++++++++++++++++++++++++++++++-------------------
 3 files changed, 37 insertions(+), 23 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index cd3fcce..e0739b6 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -20,6 +20,7 @@
 - Added fieldsets in Identity form, added 'identity_form' hook
 - Re-added 'Close' button in upload form (#1486930, #1486823)
 - Fix handling of charsets with LATIN-* label
+- Fix messages background image handling in some cases (#1486990)
 
 RELEASE 0.4
 -----------
diff --git a/program/include/main.inc b/program/include/main.inc
index 501a80f..e3004d0 100644
--- a/program/include/main.inc
+++ b/program/include/main.inc
@@ -845,7 +845,7 @@
   {
   $last_pos = 0;
   $replacements = new rcube_string_replacer;
-  
+
   // ignore the whole block if evil styles are detected
   $stripped = preg_replace('/[^a-z\(:]/', '', rcmail_xss_entity_decode($source));
   if (preg_match('/expression|behavior|url\(|import/', $stripped))
@@ -868,15 +868,15 @@
     array(
       '/(^\s*<!--)|(-->\s*$)/',
       '/(^\s*|,\s*|\}\s*)([a-z0-9\._#\*][a-z0-9\.\-_]*)/im',
-      "/$container_id\s+body/i",
+      '/'.preg_quote($container_id, '/').'\s+body/i',
     ),
     array(
       '',
       "\\1#$container_id \\2",
-      "$container_id div.rcmBody",
+      $container_id,
     ),
     $source);
-  
+
   // put block contents back in
   $styles = $replacements->resolve($styles);
 
diff --git a/program/steps/mail/func.inc b/program/steps/mail/func.inc
index 73d675a..68b790c 100644
--- a/program/steps/mail/func.inc
+++ b/program/steps/mail/func.inc
@@ -994,12 +994,12 @@
           $div_attr = array('class' => 'message-htmlpart');
           $style = array();
 
-          if (!empty($attrs['color']))
-            $style[] = 'background-color: '.$attrs['color'];
-          if (!empty($attrs['image']))
-            $style[] = 'background-image: url('.$attrs['image'].')';
-          if (!empty($style))
-            $div_attr['style'] = implode('; ', $style);
+          if (!empty($attrs)) {
+            foreach ($attrs as $a_idx => $a_val)
+              $style[] = $a_idx . ': ' . $a_val;
+            if (!empty($style))
+              $div_attr['style'] = implode('; ', $style);
+          }
 
           $out .= html::div($div_attr, $plugin['prefix'] . $body);
         }
@@ -1071,30 +1071,31 @@
  * modify a HTML message that it can be displayed inside a HTML page
  */
 function rcmail_html4inline($body, $container_id, $body_id='', &$attributes=null)
-  {
+{
   $last_style_pos = 0;
   $body_lc = strtolower($body);
+  $cont_id = $container_id.($body_id ? ' div.'.$body_id : '');
 
   // find STYLE tags
   while (($pos = strpos($body_lc, '<style', $last_style_pos)) && ($pos2 = strpos($body_lc, '</style>', $pos)))
-    {
+  {
     $pos = strpos($body_lc, '>', $pos)+1;
 
     // replace all css definitions with #container [def]
-    $styles = rcmail_mod_css_styles(substr($body, $pos, $pos2-$pos),
-      $container_id.($body_id ? ' div.'.$body_id : ''));
+    $styles = rcmail_mod_css_styles(
+      substr($body, $pos, $pos2-$pos), $cont_id);
 
     $body = substr($body, 0, $pos) . $styles . substr($body, $pos2);
     $body_lc = strtolower($body);
     $last_style_pos = $pos2;
-    }
+  }
 
   // modify HTML links to open a new window if clicked
   $GLOBALS['rcmail_html_container_id'] = $container_id;
   $body = preg_replace_callback('/<(a|link)\s+([^>]+)>/Ui', 'rcmail_alter_html_link', $body);
   unset($GLOBALS['rcmail_html_container_id']);
 
-  $out = preg_replace(array(
+  $body = preg_replace(array(
       // add comments arround html and other tags
       '/(<!DOCTYPE[^>]*>)/i',
       '/(<\?xml[^>]*>)/i',
@@ -1126,28 +1127,40 @@
   $attributes = array();
 
   // Handle body attributes that doesn't play nicely with div elements
-  if (preg_match('/<div class="' . preg_quote($body_id, '/') . '" ([^>]+)/', $out, $m)) {
+  if (preg_match('/<div class="' . preg_quote($body_id, '/') . '" ([^>]+)/', $body, $m)) {
     $attrs = $m[0];
     // Get bgcolor, we'll set it as background-color of the message container
     if (preg_match('/bgcolor=["\']*([a-z0-9#]+)["\']*/', $attrs, $mb)) {
-      $attributes['color'] = $mb[1];
+      $attributes['background-color'] = $mb[1];
       $attrs = preg_replace('/bgcolor=["\']*([a-z0-9#]+)["\']*/', '', $attrs);
     }
     // Get background, we'll set it as background-image of the message container
     if (preg_match('/background=["\']*([^"\'>\s]+)["\']*/', $attrs, $mb)) {
-      $attributes['image'] = $mb[1];
+      $attributes['background-image'] = 'url('.$mb[1].')';
       $attrs = preg_replace('/background=["\']*([^"\'>\s]+)["\']*/', '', $attrs);
     }
     if (!empty($attributes))
-      $out = preg_replace('/<div class="' . preg_quote($body_id, '/') . '" [^>]+/', rtrim($attrs), $out, 1);
+      $body = preg_replace('/<div class="' . preg_quote($body_id, '/') . '" [^>]+/', rtrim($attrs), $body, 1);
+
+    // handle body styles related to background image
+    if ($attributes['background-image']) {
+      // get body style
+      if (preg_match('/#'.preg_quote($cont_id, '/').'\s+\{([^}]+)}/i', $body, $m)) {
+        // get background related style
+        if (preg_match_all('/(background-position|background-repeat)\s*:\s*([^;]+);/i', $m[1], $ma, PREG_SET_ORDER)) {
+          foreach ($ma as $style)
+            $attributes[$style[1]] = $style[2];
+        }
+      }
+    }
   }
   // make sure there's 'rcmBody' div, we need it for proper css modification
   // its name is hardcoded in rcmail_message_body() also
   else
-    $out = '<div class="' . $body_id . '">' . $out . '</div>';
+    $body = '<div class="' . $body_id . '">' . $body . '</div>';
 
-  return $out;
-  }
+  return $body;
+}
 
 
 /**

--
Gitblit v1.9.1