Aleksander Machniak
2016-04-15 ead084693499934c467ca6a9c396367ac661dd61
Plugin API: Add html2text hook (backport from master)
6 files modified
67 ■■■■ changed files
CHANGELOG 1 ●●●● patch | view | raw | blame | history
program/include/rcmail.php 33 ●●●●● patch | view | raw | blame | history
program/steps/mail/compose.inc 11 ●●●● patch | view | raw | blame | history
program/steps/mail/func.inc 3 ●●●● patch | view | raw | blame | history
program/steps/mail/sendmail.inc 8 ●●●● patch | view | raw | blame | history
program/steps/utils/html2text.inc 11 ●●●● patch | view | raw | blame | history
CHANGELOG
@@ -1,6 +1,7 @@
CHANGELOG Roundcube Webmail
===========================
- Plugin API: Add html2text hook
- Fix missing emoticons on html-to-text conversion
- Fix random "access to this resource is secured against CSRF" message at logout (#4956)
- Fix missing language name in "Add to Dictionary" request in HTML mode (#4951)
program/include/rcmail.php
@@ -2322,6 +2322,39 @@
        return file_get_contents($name, false);
    }
    /**
     * Converts HTML content into plain text
     *
     * @param string $html    HTML content
     * @param array  $options Conversion parameters (width, links, charset)
     *
     * @return string Plain text
     */
    public function html2text($html, $options = array())
    {
        $default_options = array(
            'links'   => true,
            'width'   => 75,
            'body'    => $html,
            'charset' => RCUBE_CHARSET,
        );
        $options = array_merge($default_options, (array) $options);
        // Plugins may want to modify HTML in another/additional way
        $options = $this->plugins->exec_hook('html2text', $options);
        // Convert to text
        if (!$options['abort']) {
            $converter = new rcube_html2text($options['body'],
                false, $options['links'], $options['width'], $options['charset']);
            $options['body'] = rtrim($converter->get_text());
        }
        return $options['body'];
    }
    /************************************************************************
     *********          Deprecated methods (to be removed)          *********
program/steps/mail/compose.inc
@@ -643,8 +643,8 @@
                $text = $html = $sql_arr['signature'];
                if ($sql_arr['html_signature']) {
                    $h2t  = new rcube_html2text($html, false, true);
                    $text = trim($h2t->get_text());
                    $text = $RCMAIL->html2text($html, array('links' => false));
                    $text = trim($text);
                }
                else {
                    $t2h  = new rcube_text2html($text, false);
@@ -858,9 +858,8 @@
        if ($part->ctype_secondary == 'html') {
            // use html part if it has been used for message (pre)viewing
            // decrease line length for quoting
            $len = $compose_mode == RCUBE_COMPOSE_REPLY ? $LINE_LENGTH-2 : $LINE_LENGTH;
            $txt = new rcube_html2text($body, false, true, $len);
            $body = $txt->get_text();
            $len  = $compose_mode == RCUBE_COMPOSE_REPLY ? $LINE_LENGTH-2 : $LINE_LENGTH;
            $body = $RCMAIL->html2text($body, array('width' => $len));
        }
        else {
            if ($part->ctype_secondary == 'plain' && $part->ctype_parameters['format'] == 'flowed') {
@@ -1043,7 +1042,7 @@
            $suffix = '</blockquote>';
        }
        else {
            $suffix = '</blockquote><p></p>';
            $suffix = '</blockquote><p><br/></p>';
        }
    }
program/steps/mail/func.inc
@@ -884,8 +884,7 @@
            $data['body'] = rcube_enriched::to_html($data['body']);
        }
        $txt  = new rcube_html2text($data['body'], false, true);
        $body = $txt->get_text();
        $body = $RCMAIL->html2text($data['body']);
        $part->ctype_secondary = 'plain';
    }
    // text/html
program/steps/mail/sendmail.inc
@@ -359,12 +359,8 @@
    $MAIL_MIME->setHTMLBody($plugin['body']);
    // replace emoticons
    $plugin['body'] = $RCMAIL->replace_emoticons($plugin['body']);
    // add a plain text version of the e-mail as an alternative part.
    $h2t = new rcube_html2text($plugin['body'], false, true, 0, $message_charset);
    $plainTextPart = rcube_mime::wordwrap($h2t->get_text(), $LINE_LENGTH, "\r\n", false, $message_charset);
    $plainTextPart = $RCMAIL->html2text($plugin['body'], array('width' => 0, 'charset' => $message_charset));
    $plainTextPart = rcube_mime::wordwrap($plainTextPart, $LINE_LENGTH, "\r\n", false, $message_charset);
    $plainTextPart = wordwrap($plainTextPart, 998, "\r\n", true);
    // make sure all line endings are CRLF (#1486712)
program/steps/utils/html2text.inc
@@ -29,12 +29,11 @@
// Replace emoticon images with its text representation
$html = $RCMAIL->replace_emoticons($html);
$do_links = (bool) rcube_utils::get_input_value('_do_links', rcube_utils::INPUT_GET);
$width    = (int) rcube_utils::get_input_value('_width', rcube_utils::INPUT_GET);
$params['links'] = (bool) rcube_utils::get_input_value('_do_links', rcube_utils::INPUT_GET);
$params['width'] = (int) rcube_utils::get_input_value('_width', rcube_utils::INPUT_GET);
// Convert to text
$converter = new rcube_html2text($html, false, $do_links, $width);
$text = $RCMAIL->html2text($html, $params);
header('Content-Type: text/plain; charset=UTF-8');
print rtrim($converter->get_text());
header('Content-Type: text/plain; charset=' . RCUBE_CHARSET);
print $text;
exit;