From b534ca44abe98c48af90eb2a791d010904d038e9 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Wed, 12 Nov 2008 11:49:37 -0700 Subject: [PATCH] Update richtext Viewer. --- framework/Mime/lib/Horde/Mime/Viewer/richtext.php | 155 +++++++++++----------- 1 file changed, 79 insertions(+), 76 deletions(-) diff --git a/framework/Mime/lib/Horde/Mime/Viewer/richtext.php b/framework/Mime/lib/Horde/Mime/Viewer/richtext.php index aae76b428..deb391c74 100644 --- a/framework/Mime/lib/Horde/Mime/Viewer/richtext.php +++ b/framework/Mime/lib/Horde/Mime/Viewer/richtext.php @@ -10,19 +10,19 @@ * formatting commands (all text enclosed in angle brackets). * * We implement the following tags: - * , , , , , ,
, - * , , , , , , - * , , , , + * , , , , , ,
, + * , , , , , , + * , , , , * * The following tags are implemented differently than described in the RFC * (due to limitations in HTML output): - * - Output as centered, bold text. - * - Output as centered, bold text. - * - Output as paragraph break. + * - Output as centered, bold text. + * - Output as centered, bold text. + * - Output as paragraph break. * * The following tags are NOT implemented: - * , , , , , - * , + * , , , , , + * , * * Copyright 2004-2008 The Horde Project (http://www.horde.org/) * @@ -35,24 +35,52 @@ class Horde_Mime_Viewer_richtext extends Horde_Mime_Viewer_Driver { /** - * Render out the currently set contents in HTML format. + * Can this driver render various views? * - * @param array $params Any parameters the Viewer may need. + * @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 string The rendered contents. + * @return array See Horde_Mime_Viewer_Driver::render(). */ - public function render($params = array()) + protected function _render() { - if (($text = $this->mime_part->getContents()) === false) { - return false; - } + return $this->_toHTML(false); + } + + /** + * Return the rendered inline version of the Horde_Mime_Part object. + * + * @return array See Horde_Mime_Viewer_Driver::render(). + */ + protected function _renderInline() + { + return $this->_toHTML(true); + } - if (trim($text) == '') { + /** + * Convert richtext to HTML. + * + * @param boolean View inline? + * + * @return string The converted HTML string. + */ + protected function _toHTML($inline) + { + $text = trim($this->_mimepart->getContents()); + if ($text == '') { return $text; } - /* Use str_ireplace() if using PHP 5.0+. */ - $has_str_ireplace = function_exists('str_ireplace'); + $charset = $this->_mimepart->_getCharset(); /* We add space at the beginning and end of the string as it will * make some regular expression checks later much easier (so we @@ -69,64 +97,37 @@ class Horde_Mime_Viewer_richtext extends Horde_Mime_Viewer_Driver $text = strip_tags($text, $tags); /* becomes a '<'. CRLF becomes a SPACE. */ - if ($has_str_ireplace) { - $text = str_ireplace(array('', "\r\n"), array('<', ' '), $text); - } else { - $text = preg_replace(array('//i', "/\r\n/"), array('<', ' '), $text); - } + $text = str_ireplace(array('', "\r\n"), array('<', ' '), $text); /* We try to protect against bad stuff here. */ - $text = @htmlspecialchars($text, ENT_QUOTES, $this->mime_part->getCharset()); + $text = @htmlspecialchars($text, ENT_QUOTES, $charset); /* becomes a newline (
); * becomes a paragraph break (

). */ - if ($has_str_ireplace) { - $text = str_ireplace(array('<nl>', '<np>'), array('
', '

'), $text); - } else { - $text = preg_replace(array('/(?', '

'), $text); - } + $text = str_ireplace(array('<nl>', '<np>'), array('
', '

'), $text); /* Now convert the known tags to html. Try to remove any tag * parameters to stop people from trying to pull a fast one. */ - $pattern = array( - '/(?\1', - '\1', - '\1', - '\1', - '\1', - '\1', - '

\1
', - '
\1
', - '
\1
', - '
\1
', - '\1', - '\1', - '\1', - '
\1

', - '
\1

', - '

\1

', - '
\1
', + '/(? '\1', + '/(? '\1', + '/(? '\1', + '/(? '\1', + '/(? '\1', + '/(? '\1', + '/(? '
\1
', + '/(? '
\1
', + '/(? '
\1
', + '/(? '
\1
', + '/(? '\1', + '/(? '\1', + '/(? '\1', + '/(? '
\1

', + '/(? '
\1

', + '/(? '

\1

', + '/(? '
\1
' ); - $text = preg_replace($pattern, $replace, $text); + $text = preg_replace(array_keys($replace), array_values($replace), $text); /* Now we remove the leading/trailing space we added at the start. */ $text = substr($text, 1, -1); @@ -137,16 +138,18 @@ class Horde_Mime_Viewer_richtext extends Horde_Mime_Viewer_Driver $text = ' ' . substr($text, 1); } - return '

' . nl2br($text) . '

'; - } + $text = '

' . nl2br($text) . '

'; - /** - * 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(); + if ($inline) { + return array( + 'data' => String::convertCharset($text, $charset), + 'type' => 'text/html; charset=' . NLS::getCharset() + ); + } else { + return array( + 'data' => $text, + 'type' => 'text/html; charset=' . $charset + ); + } } } -- 2.11.0