From 8297ea1dcfbad6390cdaac6c941b72be59abf300 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Fri, 28 Aug 2015 03:02:39 -0400
Subject: [PATCH] Fix handling of plus character in mailto: links (#1490510)

---
 program/steps/mail/compose.inc |   70 ++++++++++++++++++++++++++--------
 1 files changed, 53 insertions(+), 17 deletions(-)

diff --git a/program/steps/mail/compose.inc b/program/steps/mail/compose.inc
index 9bb8713..402b32c 100644
--- a/program/steps/mail/compose.inc
+++ b/program/steps/mail/compose.inc
@@ -47,9 +47,11 @@
 if (!is_array($COMPOSE)) {
     // Infinite redirect prevention in case of broken session (#1487028)
     if ($COMPOSE_ID) {
-        rcube::raise_error(array('code' => 500, 'type' => 'php',
-            'file' => __FILE__, 'line' => __LINE__,
-            'message' => "Invalid compose ID"), true, true);
+        // if we know the message with specified ID was already sent
+        // we can ignore the error and compose a new message (#1490009)
+        if ($COMPOSE_ID != $_SESSION['last_compose_session']) {
+            rcube::raise_error(array('code' => 450), false, true);
+        }
     }
 
     $COMPOSE_ID = uniqid(mt_rand());
@@ -315,7 +317,8 @@
         // #1486037: remove "mailto:" prefix
         $COMPOSE['param']['to'] = preg_replace('/^mailto:/i', '', $mailto[0]);
         // #1490346: decode the recipient address
-        $COMPOSE['param']['to'] = urldecode($COMPOSE['param']['to']);
+        // #1490510: use raw encoding for correct "+" character handling as specified in RFC6068
+        $COMPOSE['param']['to'] = rawurldecode($COMPOSE['param']['to']);
 
         // Supported case-insensitive tokens in mailto URL
         $url_tokens = array('to', 'cc', 'bcc', 'reply-to', 'in-reply-to', 'references', 'subject', 'body');
@@ -620,8 +623,11 @@
                 $fvalue .= (!empty($fvalue) ? $separator : '') . $v;
             }
             // Use Sender header (#1489011)
-            if (($v = $MESSAGE->headers->get('Sender', false)) && strpos($v, '-bounces@') === false) {
-                $fvalue .= (!empty($fvalue) ? $separator : '') . $v;
+            if ($v = $MESSAGE->headers->get('Sender', false)) {
+                // Skip common mailing lists addresses: *-bounces@ and *-request@ (#1490452)
+                if (empty($MESSAGE->headers->others['list-post']) || !preg_match('/-(bounces|request)@/', $v)) {
+                    $fvalue .= (!empty($fvalue) ? $separator : '') . $v;
+                }
             }
 
             // When To: and Reply-To: are the same we add From: address to the list (#1489037)
@@ -694,7 +700,7 @@
 
 function rcmail_compose_editor_mode()
 {
-    global $RCMAIL;
+    global $RCMAIL, $COMPOSE;
     static $useHtml;
 
     if ($useHtml !== null) {
@@ -1140,8 +1146,24 @@
     if ($bodyIsHtml) {
         $body = rcmail_wash_html($body, array('safe' => 1), $cid_map);
 
-        // remove comments (produced by washtml)
-        $body = preg_replace('/<!--[^>]+-->/', '', $body);
+        // cleanup
+        $body = preg_replace(array(
+                // remove comments (produced by washtml)
+                '/<!--[^>]+-->/',
+                // remove <body> tags
+                '/<body([^>]*)>/i',
+                '/<\/body>/i',
+                // convert TinyMCE's empty-line sequence (#1490463)
+                '/<p>\xC2\xA0<\/p>/',
+            ),
+            array(
+                '',
+                '',
+                '',
+                '<p><br /></p>',
+            ),
+            $body
+        );
 
         // replace cid with href in inline images links
         if (!empty($cid_map)) {
@@ -1478,6 +1500,27 @@
     }
 }
 
+/**
+ * Creates reply subject by removing common subject
+ * prefixes/suffixes from the original message subject
+ */
+function rcmail_reply_subject($subject)
+{
+    $subject = trim($subject);
+
+    // replace Re:, Re[x]:, Re-x (#1490497)
+    $prefix = '/^(re:|re\[\d\]:|re-\d:)\s*/i';
+    do {
+        $subject = preg_replace($prefix, '', $subject, -1, $count);
+    }
+    while ($count);
+
+    // replace (was: ...) (#1489375)
+    $subject = preg_replace('/\s*\([wW]as:[^\)]+\)\s*$/', '', $subject);
+
+    return 'Re: ' . $subject;
+}
+
 function rcmail_compose_subject($attrib)
 {
     global $MESSAGE, $COMPOSE;
@@ -1500,13 +1543,7 @@
     }
     // create a reply-subject
     else if ($COMPOSE['mode'] == RCUBE_COMPOSE_REPLY) {
-        if (preg_match('/^re:/i', $MESSAGE->subject))
-            $subject = $MESSAGE->subject;
-        else
-            $subject = 'Re: '.$MESSAGE->subject;
-
-        // replace (was: ...) (#1489375)
-        $subject = preg_replace('/\s*\([wW]as:[^\)]+\)\s*$/', '', $subject);
+        $subject = rcmail_reply_subject($MESSAGE->subject);
     }
     // create a forward-subject
     else if ($COMPOSE['mode'] == RCUBE_COMPOSE_FORWARD) {
@@ -1526,7 +1563,6 @@
 
     return $out;
 }
-
 
 function rcmail_compose_attachment_list($attrib)
 {

--
Gitblit v1.9.1