From b143ea6f0fd389df0aac067f66cf89b2238760ae Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Mon, 2 Mar 2015 15:03:27 -0600
Subject: [PATCH] oops: Fix corruption of thread body posted by collabs
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

If the system receives an email by a collaborator which has not yet been
added to the ticket (a friend of a friend — that is, a collaborator forwards
an email to a third-party), a header is added to the thread body something
like:

Received From: afriendofafriend@mycompany.tld

However, if the thread body is text and the HTML ticket thread is enabled,
then the text formatting hint will be lost and the body will be assumed as
HTML deeper inside the thread entry creation process. Therefore, the
whitespace inside the resulting thread entry will be collapsed.

This patch addresses the issue by maintaining the original format hint with
the thread body.
---
 include/class.thread.php | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/include/class.thread.php b/include/class.thread.php
index dc8a559e1..ce03a4a26 100644
--- a/include/class.thread.php
+++ b/include/class.thread.php
@@ -742,8 +742,14 @@ Class ThreadEntry {
         else {
             //XXX: Are we potentially leaking the email address to
             // collaborators?
-            $vars['message'] = sprintf("Received From: %s\n\n%s",
-                $mailinfo['email'], $body);
+            $header = sprintf("Received From: %s\n\n", $mailinfo['email']);
+            if ($body instanceof HtmlThreadBody)
+                $header = nl2br($header);
+            // Add the banner to the top of the message
+            if ($body instanceof ThreadBody)
+                $body->prepend($header);
+
+            $vars['message'] = $body;
             $vars['userId'] = 0; //Unknown user! //XXX: Assume ticket owner?
             return $ticket->postMessage($vars, 'Email');
         }
@@ -1385,6 +1391,14 @@ class ThreadBody /* extends SplString */ {
         return $this->display('html');
     }
 
+    function prepend($what) {
+        $this->body = $what . $this->body;
+    }
+
+    function append($what) {
+        $this->body .= $what;
+    }
+
     function asVar() {
         // Email template, assume HTML
         return $this->display('email');
-- 
GitLab