Permalink
Browse files

Improve line wrapping behavior where message charset is changed by pl…

…ugins (including html2plaintext conversion)
  • Loading branch information...
1 parent 6fa6175 commit c72a96144de1e5674159f4010ec0e44c9d946a5b @thomascube thomascube committed Nov 19, 2012
@@ -338,9 +338,9 @@ function show_bytes($bytes)
return rcmail::get_instance()->show_bytes($bytes);
}
-function rc_wordwrap($string, $width=75, $break="\n", $cut=false)
+function rc_wordwrap($string, $width=75, $break="\n", $cut=false, $charset=null)
{
- return rcube_mime::wordwrap($string, $width, $break, $cut);
+ return rcube_mime::wordwrap($string, $width, $break, $cut, $charset);
}
function rc_request_header($name)
@@ -529,10 +529,11 @@ public static function unfold_flowed($text)
*
* @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 @@ public static function format_flowed($text, $length = 72)
$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 @@ public static function format_flowed($text, $length = 72)
* @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 @@ public static function wordwrap($string, $width=75, $break="\n", $cut=false)
}
}
+ if ($charset)
+ mb_internal_encoding(RCMAIL_CHARSET);
+
return $string;
}
View
@@ -135,6 +135,14 @@ class html2text
var $width = 70;
/**
+ * Target character encoding for output text
+ *
+ * @var string $charset
+ * @access public
+ */
+ var $charset = 'UTF-8';
+
+ /**
* List of preg* regular expression patterns to search for,
* used in conjunction with $replace.
*
@@ -347,7 +355,7 @@ class html2text
* @access public
* @return void
*/
- function html2text( $source = '', $from_file = false, $do_links = true, $width = 75 )
+ function html2text( $source = '', $from_file = false, $do_links = true, $width = 75, $charset = 'UTF-8' )
{
if ( !empty($source) ) {
$this->set_html($source, $from_file);
@@ -356,6 +364,7 @@ function html2text( $source = '', $from_file = false, $do_links = true, $width =
$this->set_base_url();
$this->_do_links = $do_links;
$this->width = $width;
+ $this->charset = $charset;
}
/**
@@ -517,7 +526,7 @@ function _converter(&$text)
$text = preg_replace($this->ent_search, $this->ent_replace, $text);
// Replace known html entities
- $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
+ $text = html_entity_decode($text, ENT_QUOTES, $this->charset);
// Remove unknown/unhandled entities (this cannot be done in search-and-replace block)
$text = preg_replace('/&([a-zA-Z0-9]{2,6}|#[0-9]{2,4});/', '', $text);
@@ -732,14 +741,14 @@ private function _toupper($str)
*/
private function _strtoupper($str)
{
- $str = html_entity_decode($str, ENT_COMPAT, RCMAIL_CHARSET);
+ $str = html_entity_decode($str, ENT_COMPAT, $this->charset);
if (function_exists('mb_strtoupper'))
$str = mb_strtoupper($str);
else
$str = strtoupper($str);
- $str = htmlspecialchars($str, ENT_COMPAT, RCMAIL_CHARSET);
+ $str = htmlspecialchars($str, ENT_COMPAT, $this->charset);
return $str;
}
@@ -559,8 +559,8 @@ if ($isHtml) {
$plugin['body'] = rcmail_replace_emoticons($plugin['body']);
// add a plain text version of the e-mail as an alternative part.
- $h2t = new html2text($plugin['body'], false, true, 0);
- $plainTextPart = rc_wordwrap($h2t->get_text(), $LINE_LENGTH, "\r\n");
+ $h2t = new html2text($plugin['body'], false, true, 0, $message_charset);
+ $plainTextPart = rc_wordwrap($h2t->get_text(), $LINE_LENGTH, "\r\n", false, $message_charset);
$plainTextPart = wordwrap($plainTextPart, 998, "\r\n", true);
// make sure all line endings are CRLF (#1486712)
@@ -586,9 +586,9 @@ else {
// compose format=flowed content if enabled
if ($flowed = $RCMAIL->config->get('send_format_flowed', true))
- $message_body = rcube_mime::format_flowed($message_body, min($LINE_LENGTH+2, 79));
+ $message_body = rcube_mime::format_flowed($message_body, min($LINE_LENGTH+2, 79), $message_charset);
else
- $message_body = rc_wordwrap($message_body, $LINE_LENGTH, "\r\n");
+ $message_body = rc_wordwrap($message_body, $LINE_LENGTH, "\r\n", false, $message_charset);
$message_body = wordwrap($message_body, 998, "\r\n", true);
@@ -5,7 +5,7 @@
| program/steps/utils/html2text.inc |
| |
| This file is part of the Roundcube Webmail client |
- | Copyright (C) 2005-2010, The Roundcube Dev Team |
+ | Copyright (C) 2005-2012, The Roundcube Dev Team |
| |
| Licensed under the GNU General Public License version 3 or |
| any later version with exceptions for skins & plugins. |
@@ -24,7 +24,7 @@ $html = $HTTP_RAW_POST_DATA;
// Replace emoticon images with its text representation
$html = rcmail_replace_emoticons($html);
-$converter = new html2text($html);
+$converter = new html2text($html, false, true, 0);
header('Content-Type: text/plain; charset=UTF-8');
print rtrim($converter->get_text());

0 comments on commit c72a961

Please sign in to comment.