From 697b1abd3671d762aa412ef8185bef76840f9bff Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Tue, 11 Nov 2008 15:46:09 -0700 Subject: [PATCH] Update enriched Viewer. --- framework/Mime/lib/Horde/Mime/Viewer/enriched.php | 165 +++++++++++++--------- 1 file changed, 99 insertions(+), 66 deletions(-) diff --git a/framework/Mime/lib/Horde/Mime/Viewer/enriched.php b/framework/Mime/lib/Horde/Mime/Viewer/enriched.php index aff1ef42b..1165c2eb0 100644 --- a/framework/Mime/lib/Horde/Mime/Viewer/enriched.php +++ b/framework/Mime/lib/Horde/Mime/Viewer/enriched.php @@ -26,18 +26,53 @@ class Horde_Mime_Viewer_enriched extends Horde_Mime_Viewer_Driver { /** - * Render out the currently set contents in HTML format. The - * $mime_part class variable has the information to render out, - * encapsulated in a MIME_Part object. + * This driver's capabilities. + * + * @var boolean + */ + protected $_capability = array( + 'embedded' => false, + 'full' => true, + 'info' => false, + 'inline' => true + ); + + /** + * Return the full rendered version of the Horde_Mime_Part object. + * + * @return array See Horde_Mime_Viewer_Driver::render(). */ - public function render() + protected function _render() { - if (($text = $this->mime_part->getContents()) === false) { - return false; - } + return array( + 'data' => '' . $this->_toHTML() . '', + 'type' => 'text/html; charset=' . $this->_mimepart->getCharset() + ); + } - if (trim($text) == '') { - return $text; + /** + * Return the rendered inline version of the Horde_Mime_Part object. + * + * @return array See Horde_Mime_Viewer_Driver::render(). + */ + protected function _renderInline() + { + return array( + 'data' => String::convertCharset($this->_toHTML(), $this->_mimepart->getCharset()), + 'type' => 'text/html; charset=' . NLS::getCharset() + ); + } + + /** + * Convert the enriched text to HTML. + * + * @return string The HTML-ified version of the MIME part contents. + */ + protected function _toHTML() + { + $text = trim($this->_mimepart->getContents()); + if (!strlen($text)) { + return array(); } // We add space at the beginning and end of the string as it will @@ -65,50 +100,61 @@ class Horde_Mime_Viewer_enriched extends Horde_Mime_Viewer_Driver $text = str_replace(chr(1), '<<', $text); // $text = str_replace(chr(255), '<', $text); - // Get color parameters into a more useable format. - $text = preg_replace('/([\da-fA-F]+),([\da-fA-F]+),([\da-fA-F]+)<\/param>/Uis', '', $text); - $text = preg_replace('/(red|blue|green|yellow|cyan|magenta|black|white)<\/param>/Uis', '', $text); - - // Get font family parameters into a more useable format. - $text = preg_replace('/(\w+)<\/param>/Uis', '', $text); - - // Just remove any remaining parameters -- we won't use - // them. Any tags with parameters that we want to implement - // will have to come before this Someday we hope to use these - // tags (e.g. for tags) - $text = preg_replace('/.*<\/param>/Uis', '', $text); - - // Single line breaks become spaces, double line breaks are a - // real break. This needs to do tracking to be - // compliant but we don't want to deal with state at this - // time, so we fake it some day we should rewrite this to - // handle correctly. - $text = preg_replace('/([^\n])\r\n([^\r])/', '\1 \2', $text); - $text = preg_replace('/(\r\n)\r\n/', '\1', $text); + $replace = array( + // Get color parameters into a more useable format. + '/([\da-fA-F]+),([\da-fA-F]+),([\da-fA-F]+)<\/param>/Uis' => '', + '/(red|blue|green|yellow|cyan|magenta|black|white)<\/param>/Uis' => '', + + // Get font family parameters into a more useable format. + '/(\w+)<\/param>/Uis' => '', + + /* Just remove any remaining parameters -- we won't use them. + * Any tags with parameters that we want to implement will have + * come before this. Someday we hope to use these tags (e.g. for + * tags). */ + '/.*<\/param>/Uis' => '', + + /* Single line breaks become spaces, double line breaks are a + * real break. This needs to do tracking to be compliant + * but we don't want to deal with state at this time, so we fake + * it some day we should rewrite this to handle + * correctly. */ + '/([^\n])\r\n([^\r])/' => '\1 \2', + '/(\r\n)\r\n/' => '\1' + ); + $text = preg_replace(array_keys($replace), array_values($replace), $text); // We try to protect against bad stuff here. - $text = @htmlspecialchars($text, ENT_QUOTES, $this->mime_part->getCharset()); + $text = @htmlspecialchars($text, ENT_QUOTES, $this->_mimepart->getCharset()); // Now convert the known tags to html. Try to remove any tag // parameters to stop people from trying to pull a fast one - $text = preg_replace('/(?\1', $text); - $text = preg_replace('/(?\1', $text); - $text = preg_replace('/(?\1', $text); + $replace = array( + '/(? '\1', + '/(? '\1', + '/(? '\1' + ); + $text = preg_replace(array_keys($replace), array_values($replace), $text); + $text = preg_replace_callback('/(?\2', $text); - $text = preg_replace('/(?\2', $text); - $text = preg_replace('/(?', $text); - $text = preg_replace('/(?', $text); - $text = preg_replace('/(?', $text); - $text = preg_replace('/(?', $text); - $text = preg_replace('/(?\1', $text); - $text = preg_replace('/(?\1', $text); - $text = preg_replace('/(?\1', $text); - $text = preg_replace('/(?\1', $text); - $text = preg_replace('/(?\1', $text); - $text = preg_replace('/(?\1', $text); - $text = preg_replace('/(?\1', $text); + + $replace = array( + '/(? '\2', + '/(? '\1', + '/(? '\2', + '/(? '', + '/(? '', + '/(? '', + '/(? '', + '/(? '\1', + '/(? '
\1
', + '/(? '
\1
', + '/(? '
\1
', + '/(? '
\1
', + '/(? '
\1
', + '/(? '
\1
' + ); + $text = preg_replace(array_keys($replace), array_values($replace), $text); // Replace << with < now (from translated HTML form). $text = str_replace('<<', '<', $text); @@ -121,22 +167,19 @@ class Horde_Mime_Viewer_enriched extends Horde_Mime_Viewer_Driver require_once 'Horde/Text/Filter.php'; $text = Text_Filter::filter($text, 'linkurls', array('callback' => 'Horde::externalUrl')); - // Wordwrap -- note this could impact on our above RFC - // compliance *IF* we honored nofill tags (which we don't - // yet). - $text = str_replace("\t", ' ', $text); - $text = str_replace(' ', '  ', $text); - $text = str_replace("\n ", "\n ", $text); + /* Wordwrap -- note this could impact on our above RFC compliance *IF* + * we honored nofill tags (which we don't yet). */ + $text = str_replace(array("\t", ' ', "\n "), array(' ', '  ', "\n "), $text); + if ($text[0] == ' ') { $text = ' ' . substr($text, 1); } - $text = nl2br($text); - $text = '

' . $text . '

'; - return $text; + return '

' . nl2br($text) . '

'; } /** + * TODO */ public function colorize($colors) { @@ -145,14 +188,4 @@ class Horde_Mime_Viewer_enriched extends Horde_Mime_Viewer_Driver } return '' . $colors[4] . ''; } - - /** - * Return the MIME content type of the rendered content. - * - * @return string The content type of the output. - */ - public function getType() - { - return 'text/html; charset=' . NLS::getCharset(); - } } -- 2.11.0