From 15d5dd63f233ac8c5032bc88705502bec87c613e Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Fri, 11 Oct 2013 22:50:19 +0000
Subject: [PATCH] Consistently handle emails

Whether HTML ticket thread is enabled or not, consistently handle the
processing of the email bodies
---
 include/class.mailfetch.php | 33 +++++++++++++++++----------------
 include/class.mailparse.php | 24 ++++++++++++++++--------
 2 files changed, 33 insertions(+), 24 deletions(-)

diff --git a/include/class.mailfetch.php b/include/class.mailfetch.php
index 07a3aa27f..fababa054 100644
--- a/include/class.mailfetch.php
+++ b/include/class.mailfetch.php
@@ -418,23 +418,24 @@ class MailFetcher {
     function getBody($mid) {
         global $cfg;
 
-        if ($body = $this->getPart($mid,'TEXT/HTML', $this->charset)) {
-            //Convert tags of interest before we striptags
-            //$body=str_replace("</DIV><DIV>", "\n", $body);
-            //$body=str_replace(array("<br>", "<br />", "<BR>", "<BR />"), "\n", $body);
-            $body=Format::safe_html($body); //Balance html tags & neutralize unsafe tags.
-            if (!$cfg->isHtmlThreadEnabled())
-                $body = convert_html_to_text($body);
+        if ($cfg->isHtmlThreadEnabled()) {
+            if ($body=$this->getPart($mid, 'text/html', $this->charset)) {
+                //Cleanup the html.
+                $body=Format::safe_html($body); //Balance html tags & neutralize unsafe tags.
+            }
+            elseif ($body=$this->getPart($mid, 'text/plain', $this->charset)) {
+                $body = Format::htmlchars($body);
+                $body = "<div style=\"white-space:pre-wrap\">$body</div>";
+            }
         }
-        elseif ($body = $this->getPart($mid,'TEXT/PLAIN', $this->charset)) {
-            // Escape anything that looks like HTML chars since what's in
-            // the database will be considered HTML
-            // TODO: Consider the reverse of the above edits (replace \n
-            //       <br/>
-            $body=Format::htmlchars($body);
-            if ($cfg->isHtmlThreadEnabled()) {
-                $body = wordwrap($body, 90);
-                $body = "<div style=\"white-space:pre\">$body</div>";
+        else {
+            if ($body=$this->getPart($mid, 'text/plain', $this->charset)) {
+                //Cleanup the html.
+                $body = Format::htmlchars($body);
+                $body = "<div style=\"white-space:pre-wrap\">$body</div>";
+            }
+            elseif ($body=$this->getPart($mid, 'text/html', $this->charset)) {
+                $body = convert_html_to_text($body, 100);
             }
         }
         return $body;
diff --git a/include/class.mailparse.php b/include/class.mailparse.php
index 145d10bf7..4a3c5e6c5 100644
--- a/include/class.mailparse.php
+++ b/include/class.mailparse.php
@@ -158,17 +158,25 @@ class Mail_Parse {
 
     function getBody(){
 
-        if ($body=$this->getPart($this->struct,'text/html')) {
-            //Cleanup the html.
-            $body=Format::safe_html($body); //Balance html tags & neutralize unsafe tags.
-            if (!$cfg->isHtmlThreadEnabled()) {
-                $body = convert_html_to_text($body, 120);
+        if ($cfg->isHtmlThreadEnabled()) {
+            if ($body=$this->getPart($this->struct,'text/html')) {
+                //Cleanup the html.
+                $body=Format::safe_html($body); //Balance html tags & neutralize unsafe tags.
+            }
+            elseif ($body=$this->getPart($this->struct,'text/plain')) {
+                $body = Format::htmlchars($body);
                 $body = "<div style=\"white-space:pre-wrap\">$body</div>";
             }
         }
-        elseif ($body=$this->getPart($this->struct,'text/plain')) {
-            $body = Format::htmlchars($body);
-            $body = "<div style=\"white-space:pre-wrap\">$body</div>";
+        else {
+            if ($body=$this->getPart($this->struct,'text/plain')) {
+                //Cleanup the html.
+                $body = Format::htmlchars($body);
+                $body = "<div style=\"white-space:pre-wrap\">$body</div>";
+            }
+            elseif ($body=$this->getPart($this->struct,'text/html')) {
+                $body = convert_html_to_text($body, 100);
+            }
         }
         return $body;
     }
-- 
GitLab