From ead084693499934c467ca6a9c396367ac661dd61 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Fri, 15 Apr 2016 06:28:05 -0400
Subject: [PATCH] Plugin API: Add html2text hook (backport from master)

---
 CHANGELOG                         |    1 
 program/steps/mail/compose.inc    |   11 ++---
 program/steps/utils/html2text.inc |   11 ++---
 program/include/rcmail.php        |   33 ++++++++++++++++
 program/steps/mail/func.inc       |    3 -
 program/steps/mail/sendmail.inc   |    8 +---
 6 files changed, 47 insertions(+), 20 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 2e6a1c1..17450ba 100644
--- a/CHANGELOG
+++ b/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)
diff --git a/program/include/rcmail.php b/program/include/rcmail.php
index 53aeb90..a6a61a3 100644
--- a/program/include/rcmail.php
+++ b/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)          *********
diff --git a/program/steps/mail/compose.inc b/program/steps/mail/compose.inc
index f158029..7c89c9b 100644
--- a/program/steps/mail/compose.inc
+++ b/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>';
         }
     }
 
diff --git a/program/steps/mail/func.inc b/program/steps/mail/func.inc
index 12acaf8..a767cdd 100644
--- a/program/steps/mail/func.inc
+++ b/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
diff --git a/program/steps/mail/sendmail.inc b/program/steps/mail/sendmail.inc
index 3152184..5f3e9dc 100644
--- a/program/steps/mail/sendmail.inc
+++ b/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)
diff --git a/program/steps/utils/html2text.inc b/program/steps/utils/html2text.inc
index a33c5f5..1115b3e 100644
--- a/program/steps/utils/html2text.inc
+++ b/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;

--
Gitblit v1.9.1