* formatting commands (all text enclosed in angle brackets).
*
* We implement the following tags:
- * <bold>, <italic>, <fixed>, <smaller>, <bigger>, <underline>, <center>,
- * <flushleft>, <flushright>, <indent>, <subscript>, <excerpt>, <paragraph>,
- * <signature>, <comment>, <no-op>, <lt>, <nl>
+ * <bold>, <italic>, <fixed>, <smaller>, <bigger>, <underline>, <center>,
+ * <flushleft>, <flushright>, <indent>, <subscript>, <excerpt>, <paragraph>,
+ * <signature>, <comment>, <no-op>, <lt>, <nl>
*
* The following tags are implemented differently than described in the RFC
* (due to limitations in HTML output):
- * <heading> - Output as centered, bold text.
- * <footing> - Output as centered, bold text.
- * <np> - Output as paragraph break.
+ * <heading> - Output as centered, bold text.
+ * <footing> - Output as centered, bold text.
+ * <np> - Output as paragraph break.
*
* The following tags are NOT implemented:
- * <indentright>, <outdent>, <outdentright>, <samepage>, <iso-8859-X>,
- * <us-ascii>,
+ * <indentright>, <outdent>, <outdentright>, <samepage>, <iso-8859-X>,
+ * <us-ascii>,
*
* Copyright 2004-2008 The Horde Project (http://www.horde.org/)
*
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
$text = strip_tags($text, $tags);
/* <lt> becomes a '<'. CRLF becomes a SPACE. */
- if ($has_str_ireplace) {
- $text = str_ireplace(array('<lt>', "\r\n"), array('<', ' '), $text);
- } else {
- $text = preg_replace(array('/<lt>/i', "/\r\n/"), array('<', ' '), $text);
- }
+ $text = str_ireplace(array('<lt>', "\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);
/* <nl> becomes a newline (<br />);
* <np> becomes a paragraph break (<p />). */
- if ($has_str_ireplace) {
- $text = str_ireplace(array('<nl>', '<np>'), array('<br />', '<p />'), $text);
- } else {
- $text = preg_replace(array('/(?<!<)<nl.*>/Uis', '/(?<!<)<np.*>/Uis'), array('<br />', '<p />'), $text);
- }
+ $text = str_ireplace(array('<nl>', '<np>'), array('<br />', '<p />'), $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(
- '/(?<!<)<bold.*>(.*)<\/bold>/Uis',
- '/(?<!<)<italic.*>(.*)<\/italic>/Uis',
- '/(?<!<)<fixed.*>(.*)<\/fixed>/Uis',
- '/(?<!<)<smaller.*>(.*)<\/smaller>/Uis',
- '/(?<!<)<bigger.*>(.*)<\/bigger>/Uis',
- '/(?<!<)<underline.*>(.*)<\/underline>/Uis',
- '/(?<!<)<center.*>(.*)<\/center>/Uis',
- '/(?<!<)<flushleft.*>(.*)<\/flushleft>/Uis',
- '/(?<!<)<flushright.*>(.*)<\/flushright>/Uis',
- '/(?<!<)<indent.*>(.*)<\/indent>/Uis',
- '/(?<!<)<excerpt.*>(.*)<\/excerpt>/Uis',
- '/(?<!<)<subscript.*>(.*)<\/subscript>/Uis',
- '/(?<!<)<superscript.*>(.*)<\/superscript>/Uis',
- '/(?<!<)<heading.*>(.*)<\/heading>/Uis',
- '/(?<!<)<footing.*>(.*)<\/footing>/Uis',
- '/(?<!<)<paragraph.*>(.*)<\/paragraph>/Uis',
- '/(?<!<)<signature.*>(.*)<\/signature>/Uis',
- );
$replace = array(
- '<span style="font-weight: bold">\1</span>',
- '<span style="font-style: italic">\1</span>',
- '<font face="fixed">\1</font>',
- '<span style="font-size: smaller">\1</span>',
- '<span style="font-size: larger">\1</span>',
- '<span style="text-decoration: underline">\1</span>',
- '<div align="center">\1</div>',
- '<div align="left">\1</div>',
- '<div align="right">\1</div>',
- '<blockquote>\1</blockquote>',
- '<cite>\1</cite>',
- '<sub>\1</sub>',
- '<sup>\1</sup>',
- '<br /><div align="center" style="font-weight: bold">\1</div><br />',
- '<br /><div align="center" style="font-weight: bold">\1</div><br />',
- '<p>\1</p>',
- '<address>\1</address>',
+ '/(?<!<)<bold.*>(.*)<\/bold>/Uis' => '<span style="font-weight: bold">\1</span>',
+ '/(?<!<)<italic.*>(.*)<\/italic>/Uis' => '<span style="font-style: italic">\1</span>',
+ '/(?<!<)<fixed.*>(.*)<\/fixed>/Uis' => '<font face="fixed">\1</font>',
+ '/(?<!<)<smaller.*>(.*)<\/smaller>/Uis' => '<span style="font-size: smaller">\1</span>',
+ '/(?<!<)<bigger.*>(.*)<\/bigger>/Uis' => '<span style="font-size: larger">\1</span>',
+ '/(?<!<)<underline.*>(.*)<\/underline>/Uis' => '<span style="text-decoration: underline">\1</span>',
+ '/(?<!<)<center.*>(.*)<\/center>/Uis' => '<div align="center">\1</div>',
+ '/(?<!<)<flushleft.*>(.*)<\/flushleft>/Uis' => '<div align="left">\1</div>',
+ '/(?<!<)<flushright.*>(.*)<\/flushright>/Uis' => '<div align="right">\1</div>',
+ '/(?<!<)<indent.*>(.*)<\/indent>/Uis' => '<blockquote>\1</blockquote>',
+ '/(?<!<)<excerpt.*>(.*)<\/excerpt>/Uis' => '<cite>\1</cite>',
+ '/(?<!<)<subscript.*>(.*)<\/subscript>/Uis' => '<sub>\1</sub>',
+ '/(?<!<)<superscript.*>(.*)<\/superscript>/Uis' => '<sup>\1</sup>',
+ '/(?<!<)<heading.*>(.*)<\/heading>/Uis' => '<br /><div align="center" style="font-weight: bold">\1</div><br />',
+ '/(?<!<)<footing.*>(.*)<\/footing>/Uis' => '<br /><div align="center" style="font-weight: bold">\1</div><br />',
+ '/(?<!<)<paragraph.*>(.*)<\/paragraph>/Uis' => '<p>\1</p>',
+ '/(?<!<)<signature.*>(.*)<\/signature>/Uis' => '<address>\1</address>'
);
- $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);
$text = ' ' . substr($text, 1);
}
- return '<p class="fixed">' . nl2br($text) . '</p>';
- }
+ $text = '<p style="font-size:100%;font-family:Lucida Console,Courier,Courier New;">' . nl2br($text) . '</p>';
- /**
- * 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
+ );
+ }
}
}