Skip to content
Snippets Groups Projects
Commit 0fc28101 authored by Jared Hancock's avatar Jared Hancock
Browse files

Merge pull request #34 from protich/feature/html2text


html2text revisited.

Reviewed-By: default avatarJared Hancock <jared@osticket.com>
parents 154b4a52 f76ced57
Branches
Tags
No related merge requests found
...@@ -42,7 +42,7 @@ class KbaseAjaxAPI extends AjaxController { ...@@ -42,7 +42,7 @@ class KbaseAjaxAPI extends AjaxController {
$resp['files'] = $canned->attachments->getSeparates(); $resp['files'] = $canned->attachments->getSeparates();
if (!$cfg->isHtmlThreadEnabled()) { if (!$cfg->isHtmlThreadEnabled()) {
$resp['response'] = convert_html_to_text($resp['response'], 90); $resp['response'] = Format::html2text($resp['response'], 90);
$resp['files'] += $canned->attachments->getInlines(); $resp['files'] += $canned->attachments->getInlines();
} }
...@@ -54,7 +54,7 @@ class KbaseAjaxAPI extends AjaxController { ...@@ -54,7 +54,7 @@ class KbaseAjaxAPI extends AjaxController {
$response =$ticket?$ticket->replaceVars($canned->getResponse()):$canned->getResponse(); $response =$ticket?$ticket->replaceVars($canned->getResponse()):$canned->getResponse();
if (!$cfg->isHtmlThreadEnabled()) if (!$cfg->isHtmlThreadEnabled())
$response = convert_html_to_text($response, 90); $response = Format::html2text($response, 90);
} }
return $response; return $response;
......
...@@ -128,8 +128,8 @@ class Format { ...@@ -128,8 +128,8 @@ class Format {
return is_array($var)?array_map(array('Format','strip_slashes'),$var):stripslashes($var); return is_array($var)?array_map(array('Format','strip_slashes'),$var):stripslashes($var);
} }
function wrap($text,$len=75) { function wrap($text, $len=75) {
return wordwrap($text,$len,"\n",true); return $len ? wordwrap($text, $len, "\n", true) : $text;
} }
function html($html, $config=array('balance'=>1)) { function html($html, $config=array('balance'=>1)) {
...@@ -137,6 +137,30 @@ class Format { ...@@ -137,6 +137,30 @@ class Format {
return htmLawed($html, $config); return htmLawed($html, $config);
} }
function html2text($html, $width=74, $tidy=true) {
# Tidy html: decode, balance, sanitize tags
if($tidy)
$html = Format::html(Format::htmldecode($html), array('balance' => 1));
# See if advanced html2text is available (requires xml extension)
if (function_exists('convert_html_to_text')
&& extension_loaded('xml'))
return convert_html_to_text($html, $width);
# Try simple html2text - insert line breaks after new line tags.
$html = preg_replace(
array(':<br ?/?\>:i', ':(</div>)\s*:i', ':(</p>)\s*:i')
array("\n", "$1\n", "$1\n\n"),
$html);
# Strip tags, decode html chars and wrap resulting text.
return Format::wrap(
Format::htmldecode( Format::striptags($html, false)),
$width);
}
function safe_html($html) { function safe_html($html) {
// Remove HEAD and STYLE sections // Remove HEAD and STYLE sections
$html = preg_replace(':<(head|style).+</\1>:is','', $html); $html = preg_replace(':<(head|style).+</\1>:is','', $html);
......
...@@ -143,8 +143,8 @@ class Mailer { ...@@ -143,8 +143,8 @@ class Mailer {
if (!(isset($options['text']) && $options['text']) if (!(isset($options['text']) && $options['text'])
&& preg_match('/^\s*</', $message)) { && preg_match('/^\s*</', $message)) {
// Make sure nothing unsafe has creeped into the message // Make sure nothing unsafe has creeped into the message
$message = Format::safe_html($message); $message = Format::safe_html($message); //XXX??
$mime->setTXTBody(convert_html_to_text($message)); $mime->setTXTBody(Format::html2text($message), 90, false);
} }
else { else {
$mime->setTXTBody($message); $mime->setTXTBody($message);
......
...@@ -441,7 +441,7 @@ class MailFetcher { ...@@ -441,7 +441,7 @@ class MailFetcher {
$body = Format::htmlchars($body); $body = Format::htmlchars($body);
} }
elseif ($body=$this->getPart($mid, 'text/html', $this->charset)) { elseif ($body=$this->getPart($mid, 'text/html', $this->charset)) {
$body = convert_html_to_text(Format::safe_html($body), 100); $body = Format::html2text(Format::safe_html($body), 100, false);
} }
$body = trim($body) $body = trim($body)
? sprintf('<div style="white-space:pre-wrap">%s</div>', ? sprintf('<div style="white-space:pre-wrap">%s</div>',
......
...@@ -191,7 +191,7 @@ class Mail_Parse { ...@@ -191,7 +191,7 @@ class Mail_Parse {
$body = Format::htmlchars($body); $body = Format::htmlchars($body);
} }
elseif ($body=$this->getPart($this->struct,'text/html')) { elseif ($body=$this->getPart($this->struct,'text/html')) {
$body = convert_html_to_text($body, 100); $body = Format::html2text(Format::safe_html($body), 100, false);
} }
$body = trim($body) $body = trim($body)
? sprintf('<div style="white-space:pre-wrap">%s</div>', ? sprintf('<div style="white-space:pre-wrap">%s</div>',
......
...@@ -25,16 +25,8 @@ ...@@ -25,16 +25,8 @@
* @return the HTML converted, as best as possible, to text * @return the HTML converted, as best as possible, to text
*/ */
function convert_html_to_text($html, $width=74) { 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);
}
$html = fix_newlines($html);
$doc = new DOMDocument('1.0', 'utf-8'); $doc = new DOMDocument('1.0', 'utf-8');
if (!@$doc->loadHTML($html)) if (!@$doc->loadHTML($html))
return $html; return $html;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment