From c72a96144de1e5674159f4010ec0e44c9d946a5b Mon Sep 17 00:00:00 2001
From: Thomas Bruederli <thomas@roundcube.net>
Date: Mon, 19 Nov 2012 11:02:13 +0100
Subject: Improve line wrapping behavior where message charset is changed by
 plugins (including html2plaintext conversion)

---
 program/include/rcube_mime.php | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

(limited to 'program/include/rcube_mime.php')

diff --git a/program/include/rcube_mime.php b/program/include/rcube_mime.php
index 2dbeaf67a..ac3ef9e8a 100644
--- a/program/include/rcube_mime.php
+++ b/program/include/rcube_mime.php
@@ -529,10 +529,11 @@ class rcube_mime
      *
      * @param string $text Text to wrap
      * @param int $length Length
+     * @param string $charset Character encoding of $text
      *
      * @return string Wrapped text
      */
-    public static function format_flowed($text, $length = 72)
+    public static function format_flowed($text, $length = 72, $charset=null)
     {
         $text = preg_split('/\r?\n/', $text);
 
@@ -542,10 +543,10 @@ class rcube_mime
                     $prefix = $regs[0];
                     $level = strlen($prefix);
                     $line  = rtrim(substr($line, $level));
-                    $line  = $prefix . self::wordwrap($line, $length - $level - 2, " \r\n$prefix ");
+                    $line  = $prefix . self::wordwrap($line, $length - $level - 2, " \r\n$prefix ", false, $charset);
                 }
                 else if ($line) {
-                    $line = self::wordwrap(rtrim($line), $length - 2, " \r\n");
+                    $line = self::wordwrap(rtrim($line), $length - 2, " \r\n", false, $charset);
                     // space-stuffing
                     $line = preg_replace('/(^|\r\n)(From| |>)/', '\\1 \\2', $line);
                 }
@@ -565,12 +566,16 @@ class rcube_mime
      * @param int    $width   Line width
      * @param string $break   Line separator
      * @param bool   $cut     Enable to cut word
+     * @param string $charset Charset of $string
      *
      * @return string Text
      */
-    public static function wordwrap($string, $width=75, $break="\n", $cut=false)
+    public static function wordwrap($string, $width=75, $break="\n", $cut=false, $charset=null)
     {
-        $para   = explode($break, $string);
+        if ($charset)
+            mb_internal_encoding($charset);
+
+        $para   = preg_split('/\r?\n/', $string);
         $string = '';
 
         while (count($para)) {
@@ -624,6 +629,9 @@ class rcube_mime
             }
         }
 
+        if ($charset)
+            mb_internal_encoding(RCMAIL_CHARSET);
+
         return $string;
     }
 
-- 
cgit v1.2.3