diff --git a/include/class.mailfetch.php b/include/class.mailfetch.php index b9c9602fd9aa741bb9d955621ae5286f59523b36..d270f01408e249c4615cb0dc75beac7278f094d0 100644 --- a/include/class.mailfetch.php +++ b/include/class.mailfetch.php @@ -422,24 +422,31 @@ 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 = (trim($body, " <>br/\t\n\r")) + ? Format::safe_html($body) + : '--'; + } + elseif ($body=$this->getPart($mid, 'text/plain', $this->charset)) { + $body = trim($body) + ? sprintf('<div style="white-space:pre-wrap">%s</div>', + Format::htmlchars($body)) + : '--'; + } } - 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)) { + $body = Format::htmlchars($body); + } + elseif ($body=$this->getPart($mid, 'text/html', $this->charset)) { + $body = convert_html_to_text(Format::safe_html($body), 100); } + $body = trim($body) + ? sprintf('<div style="white-space:pre-wrap">%s</div>', + $body) + : '--'; } return $body; } diff --git a/include/class.mailparse.php b/include/class.mailparse.php index fa88c393d342136af381dc9f39a4a5b372fd90e5..2438d4f4d2b4461067a18f30f39fc626e7508938 100644 --- a/include/class.mailparse.php +++ b/include/class.mailparse.php @@ -172,17 +172,31 @@ class Mail_Parse { function getBody(){ global $cfg; - 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); - $body = "<div style=\"white-space:pre-wrap\">$body</div>"; + if ($cfg->isHtmlThreadEnabled()) { + if ($body=$this->getPart($this->struct,'text/html')) { + // Cleanup the html -- Balance html tags & neutralize unsafe tags. + $body = (trim($body, " <>br/\t\n\r")) + ? Format::safe_html($body) + : '--'; + } + elseif ($body=$this->getPart($this->struct,'text/plain')) { + $body = trim($body) + ? sprintf('<div style="white-space:pre-wrap">%s</div>', + Format::htmlchars($body)) + : '--'; } } - 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')) { + $body = Format::htmlchars($body); + } + elseif ($body=$this->getPart($this->struct,'text/html')) { + $body = convert_html_to_text($body, 100); + } + $body = trim($body) + ? sprintf('<div style="white-space:pre-wrap">%s</div>', + $body) + : '--'; } return $body; } diff --git a/include/html2text.php b/include/html2text.php index a4997d1b2b978131b5ad2d155035abf981c40898..72ca01f217471480e9c15358a893d9e33ba3e181 100644 --- a/include/html2text.php +++ b/include/html2text.php @@ -27,6 +27,14 @@ function convert_html_to_text($html, $width=74) { $html = fix_newlines($html); + if (!extension_loaded('xml')) { + $html = preg_replace( + array(':<br ?/?>|</div>:i', ':</p>:i'), + array("\n", "\n\n"), + $html); + return Format::striptags($html); + } + $doc = new DOMDocument('1.0', 'utf-8'); if (!@$doc->loadHTML($html)) return $html;