Thomas Bruederli
2012-11-19 c72a96144de1e5674159f4010ec0e44c9d946a5b
Improve line wrapping behavior where message charset is changed by plugins (including html2plaintext conversion)
5 files modified
51 ■■■■■ changed files
program/include/rcube_bc.inc 4 ●●●● patch | view | raw | blame | history
program/include/rcube_mime.php 18 ●●●● patch | view | raw | blame | history
program/lib/html2text.php 17 ●●●● patch | view | raw | blame | history
program/steps/mail/sendmail.inc 8 ●●●● patch | view | raw | blame | history
program/steps/utils/html2text.inc 4 ●●●● patch | view | raw | blame | history
program/include/rcube_bc.inc
@@ -338,9 +338,9 @@
    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)
program/include/rcube_mime.php
@@ -529,10 +529,11 @@
     *
     * @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 @@
                    $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 @@
     * @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 @@
            }
        }
        if ($charset)
            mb_internal_encoding(RCMAIL_CHARSET);
        return $string;
    }
program/lib/html2text.php
@@ -135,6 +135,14 @@
    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 @@
     *  @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 @@
        $this->set_base_url();
        $this->_do_links = $do_links;
        $this->width = $width;
        $this->charset = $charset;
    }
    /**
@@ -517,7 +526,7 @@
        $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 _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;
    }
program/steps/mail/sendmail.inc
@@ -559,8 +559,8 @@
  $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 @@
  // 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);
program/steps/utils/html2text.inc
@@ -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 @@
// 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());