Improve line wrapping behavior where message charset is changed by plugins (including html2plaintext conversion)
| | |
| | | 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) |
| | |
| | | * |
| | | * @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); |
| | | |
| | |
| | | $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); |
| | | } |
| | |
| | | * @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)) { |
| | |
| | | } |
| | | } |
| | | |
| | | if ($charset) |
| | | mb_internal_encoding(RCMAIL_CHARSET); |
| | | |
| | | return $string; |
| | | } |
| | | |
| | |
| | | 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. |
| | | * |
| | |
| | | * @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); |
| | |
| | | $this->set_base_url(); |
| | | $this->_do_links = $do_links; |
| | | $this->width = $width; |
| | | $this->charset = $charset; |
| | | } |
| | | |
| | | /** |
| | |
| | | $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); |
| | |
| | | */ |
| | | 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; |
| | | } |
| | |
| | | $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) |
| | |
| | | |
| | | // 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 | |
| | | | | |
| | | | 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. | |
| | |
| | | // 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()); |