From 5a6ad209837a8bcca14d4f74541d8ac3ea760341 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Tue, 13 Mar 2007 20:39:51 -0400
Subject: [PATCH] Fixed message headers encoding; improved recipient splitting; applied patch for attachment download (#1484198)

---
 program/lib/Mail/mime.php       |    4 +-
 CHANGELOG                       |   13 ++++++
 program/steps/mail/get.inc      |   16 ++++---
 program/steps/mail/sendmail.inc |   22 ++++++----
 program/include/rcube_imap.inc  |   25 +++++++-----
 5 files changed, 51 insertions(+), 29 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 7b2597e..3d1796d 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,9 +1,22 @@
 CHANGELOG RoundCube Webmail
 ---------------------------
 
+2007/03/13 (thomasb)
+----------
+- Applied patch for attachment download by crichardson (closes #1484198)
+- Fixed bug in Postgres DB handling (closes #1484068)
+- Fixed bug of invalid calls to fetchRow() in rcube_db.inc (closes #1484280)
+- Fixed array_merge bug (closes #1484281)
+- Fixed flag for deletion in list view (closes #1484264)
+- Finally support semicolons as recipient separator (closes ##1484251)
+- Fixed message headers (subject) encoding
+
+
 2007/03/04 (tomekp)
+----------
 - check if safe mode is on or not (closes #1484269)
 
+
 2007/03/02 (thomasb)
 ----------
 - Show "no subject" in message list if subject is missing (closes #1484243)
diff --git a/program/include/rcube_imap.inc b/program/include/rcube_imap.inc
index 9f249d5..7ac51fc 100644
--- a/program/include/rcube_imap.inc
+++ b/program/include/rcube_imap.inc
@@ -2454,7 +2454,7 @@
   function _parse_address_list($str)
     {
     // remove any newlines and carriage returns before
-    $a = $this->_explode_quoted_string(',', preg_replace( "/[\r\n]/", " ", $str));
+    $a = $this->_explode_quoted_string('[,;]', preg_replace( "/[\r\n]/", " ", $str));
     $result = array();
     
     foreach ($a as $key => $val)
@@ -2481,17 +2481,20 @@
 
   function _explode_quoted_string($delimiter, $string)
     {
-    $quotes = explode("\"", $string);
-    foreach ($quotes as $key => $val)
-      if (($key % 2) == 1)
-        $quotes[$key] = str_replace($delimiter, "_!@!_", $quotes[$key]);
-        
-    $string = implode("\"", $quotes);
-
-    $result = explode($delimiter, $string);
-    foreach ($result as $key => $val) 
-      $result[$key] = str_replace("_!@!_", $delimiter, $result[$key]);
+    $result = array();
+    $strlen = strlen($string);
+    for ($q=$p=$i=0; $i < $strlen; $i++)
+    {
+      if ($string{$i} == "\"" && $string{$i-1} != "\\")
+        $q = $q ? false : true;
+      else if (!$q && preg_match("/$delimiter/", $string{$i}))
+      {
+        $result[] = substr($string, $p, $i - $p);
+        $p = $i + 1;
+      }
+    }
     
+    $result[] = substr($string, $p);
     return $result;
     }
   }
diff --git a/program/lib/Mail/mime.php b/program/lib/Mail/mime.php
index a9f798e..e7129db 100644
--- a/program/lib/Mail/mime.php
+++ b/program/lib/Mail/mime.php
@@ -836,9 +836,9 @@
                     default:
                         // quoted-printable encoding has been selected
                         $mode = 'Q';
-                        $encoded = preg_replace('/([\x20-\x25\x2C\x80-\xFF])/e', "'='.sprintf('%02X', ord('\\1'))", $value);
+                        $encoded = preg_replace('/([\x2C\x3F\x80-\xFF])/e', "'='.sprintf('%02X', ord('\\1'))", $value);
                         // replace spaces with _
-                        $encoded = str_replace('=20', '_', $encoded);
+                        $encoded = str_replace(' ', '_', $encoded);
                     }
 
                 $value = '=?' . $this->_build_params['head_charset'] . '?' . $mode . '?' . $encoded . '?=' . $suffix;
diff --git a/program/steps/mail/get.inc b/program/steps/mail/get.inc
index 995a485..521f477 100644
--- a/program/steps/mail/get.inc
+++ b/program/steps/mail/get.inc
@@ -63,19 +63,21 @@
     $mimetype = sprintf('%s/%s', $ctype_primary, $ctype_secondary);
     $filename = $part->d_parameters['filename'] ? $part->d_parameters['filename'] : $part->ctype_parameters['name'];
 
-    // send correct headers for content type and length
+    header("Expires: 0");
+    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
+    header("Cache-Control: private", false);
+    header("Content-Transfer-Encoding: binary");
+    header(sprintf('Content-Disposition: attachment; filename="%s";',
+                   $filename ? rcube_imap::decode_mime_string($filename) : "roundcube.$ctype_secondary"));
+
+    // send download headers
     if ($_GET['_download'])
       {
-      // send download headers
+      header("Cache-Control: private", false);
       header("Content-Type: application/octet-stream");
-      header(sprintf('Content-Disposition: attachment; filename="%s"',
-                     $filename ? rcube_imap::decode_mime_string($filename) : "roundcube.$ctype_secondary"));
       }
     else
-      {
       header("Content-Type: $mimetype");
-      header(sprintf('Content-Disposition: inline; filename="%s"', rcube_imap::decode_mime_string($filename)));
-      }
 
     // We need to set the following headers to make downloads work using IE in HTTPS mode.
     if (isset($_SERVER['HTTPS']))
diff --git a/program/steps/mail/sendmail.inc b/program/steps/mail/sendmail.inc
index 7c7727b..ea8264f 100644
--- a/program/steps/mail/sendmail.inc
+++ b/program/steps/mail/sendmail.inc
@@ -151,8 +151,8 @@
 $input_charset = $OUTPUT->get_charset();
 $message_charset = isset($_POST['_charset']) ? $_POST['_charset'] : $input_charset;
 
-$mailto_regexp = array('/[,;]\s*[\r\n]+/', '/[\r\n]+/', '/[,;]\s*$/m');
-$mailto_replace = array(', ', ', ', '');
+$mailto_regexp = array('/[,;]\s*[\r\n]+/', '/[\r\n]+/', '/[,;]\s*$/m', '/;/');
+$mailto_replace = array(', ', ', ', '', ',');
 
 // replace new lines and strip ending ', '
 $mailto = preg_replace($mailto_regexp, $mailto_replace, get_input_value('_to', RCUBE_INPUT_POST, TRUE, $message_charset));
@@ -170,7 +170,7 @@
 // compose headers array
 $headers = array('Date' => date('D, j M Y H:i:s O'),
                  'From' => rcube_charset_convert($identity_arr['string'], $CHARSET, $message_charset),
-                 'To'   => rcube_charset_convert($mailto, $input_charset, $message_charset));
+                 'To'   => $mailto);
 
 // additional recipients
 if (!empty($_POST['_cc']))
@@ -301,10 +301,11 @@
 // unset to save memory.
 unset($MAIL_MIME->_parts);
 
+// encoding subject header with mb_encode provides better results with asian characters
 if ($MBSTRING && function_exists("mb_encode_mimeheader"))
 {
   mb_internal_encoding($message_charset);
-  $headers['Subject'] = mb_encode_mimeheader($headers['Subject'], $message_charset, 'Q');
+  $mb_subject = mb_encode_mimeheader($headers['Subject'], $message_charset, 'Q');
   mb_internal_encoding($CHARSET);
 }
 
@@ -326,12 +327,12 @@
     $send_headers = $headers;
     unset($send_headers['Bcc']);
   
-    // generate message headers
-    $header_str = $MAIL_MIME->txtHeaders($send_headers);
+    if (!empty($mb_subject))
+      $send_headers['Subject'] = $mb_subject;
   
     // send message
     $smtp_response = array();
-    $sent = smtp_mail($from, $a_recipients, $header_str, $msg_body, $smtp_response);
+    $sent = smtp_mail($from, $a_recipients, $MAIL_MIME->txtHeaders($send_headers), $msg_body, $smtp_response);
   
     // log error
     if (!$sent)
@@ -352,14 +353,17 @@
     $headers_php = $MAIL_MIME->_headers;
     unset($headers_php['To'], $headers_php['Subject']);
     
+    if (!empty($mb_subject))
+      $headers_enc['Subject'] = $mb_subject;
+    
     // reset stored headers and overwrite
     $MAIL_MIME->_headers = array();
     $header_str = $MAIL_MIME->txtHeaders($headers_php);
   
     if (ini_get('safe_mode'))
-      $sent = mail($headers_enc['To'], $headers['Subject'], $msg_body, $header_str);
+      $sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str);
     else
-      $sent = mail($headers_enc['To'], $headers['Subject'], $msg_body, $header_str, "-f$from");
+      $sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str, "-f$from");
     }
   
   

--
Gitblit v1.9.1