Update richtext Viewer.
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 12 Nov 2008 18:49:37 +0000 (11:49 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 12 Nov 2008 18:49:37 +0000 (11:49 -0700)
framework/Mime/lib/Horde/Mime/Viewer/richtext.php

index aae76b4..deb391c 100644 (file)
  * 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
@@ -69,64 +97,37 @@ class Horde_Mime_Viewer_richtext extends Horde_Mime_Viewer_Driver
         $text = strip_tags($text, $tags);
 
         /* <lt> becomes a '<'. CRLF becomes a SPACE. */
-        if ($has_str_ireplace) {
-            $text = str_ireplace(array('<lt>', "\r\n"), array('&lt;', ' '), $text);
-        } else {
-            $text = preg_replace(array('/<lt>/i', "/\r\n/"), array('&lt;', ' '), $text);
-        }
+        $text = str_ireplace(array('<lt>', "\r\n"), array('&lt;', ' '), $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('&lt;nl&gt;', '&lt;np&gt;'), array('<br />', '<p />'), $text);
-        } else {
-            $text = preg_replace(array('/(?<!&lt;)&lt;nl.*&gt;/Uis', '/(?<!&lt;)&lt;np.*&gt;/Uis'), array('<br />', '<p />'), $text);
-        }
+        $text = str_ireplace(array('&lt;nl&gt;', '&lt;np&gt;'), 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(
-            '/(?<!&lt;)&lt;bold.*&gt;(.*)&lt;\/bold&gt;/Uis',
-            '/(?<!&lt;)&lt;italic.*&gt;(.*)&lt;\/italic&gt;/Uis',
-            '/(?<!&lt;)&lt;fixed.*&gt;(.*)&lt;\/fixed&gt;/Uis',
-            '/(?<!&lt;)&lt;smaller.*&gt;(.*)&lt;\/smaller&gt;/Uis',
-            '/(?<!&lt;)&lt;bigger.*&gt;(.*)&lt;\/bigger&gt;/Uis',
-            '/(?<!&lt;)&lt;underline.*&gt;(.*)&lt;\/underline&gt;/Uis',
-            '/(?<!&lt;)&lt;center.*&gt;(.*)&lt;\/center&gt;/Uis',
-            '/(?<!&lt;)&lt;flushleft.*&gt;(.*)&lt;\/flushleft&gt;/Uis',
-            '/(?<!&lt;)&lt;flushright.*&gt;(.*)&lt;\/flushright&gt;/Uis',
-            '/(?<!&lt;)&lt;indent.*&gt;(.*)&lt;\/indent&gt;/Uis',
-            '/(?<!&lt;)&lt;excerpt.*&gt;(.*)&lt;\/excerpt&gt;/Uis',
-            '/(?<!&lt;)&lt;subscript.*&gt;(.*)&lt;\/subscript&gt;/Uis',
-            '/(?<!&lt;)&lt;superscript.*&gt;(.*)&lt;\/superscript&gt;/Uis',
-            '/(?<!&lt;)&lt;heading.*&gt;(.*)&lt;\/heading&gt;/Uis',
-            '/(?<!&lt;)&lt;footing.*&gt;(.*)&lt;\/footing&gt;/Uis',
-            '/(?<!&lt;)&lt;paragraph.*&gt;(.*)&lt;\/paragraph&gt;/Uis',
-            '/(?<!&lt;)&lt;signature.*&gt;(.*)&lt;\/signature&gt;/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>',
+            '/(?<!&lt;)&lt;bold.*&gt;(.*)&lt;\/bold&gt;/Uis' => '<span style="font-weight: bold">\1</span>',
+            '/(?<!&lt;)&lt;italic.*&gt;(.*)&lt;\/italic&gt;/Uis' => '<span style="font-style: italic">\1</span>',
+            '/(?<!&lt;)&lt;fixed.*&gt;(.*)&lt;\/fixed&gt;/Uis' => '<font face="fixed">\1</font>',
+            '/(?<!&lt;)&lt;smaller.*&gt;(.*)&lt;\/smaller&gt;/Uis' => '<span style="font-size: smaller">\1</span>',
+            '/(?<!&lt;)&lt;bigger.*&gt;(.*)&lt;\/bigger&gt;/Uis' => '<span style="font-size: larger">\1</span>',
+            '/(?<!&lt;)&lt;underline.*&gt;(.*)&lt;\/underline&gt;/Uis' => '<span style="text-decoration: underline">\1</span>',
+            '/(?<!&lt;)&lt;center.*&gt;(.*)&lt;\/center&gt;/Uis' => '<div align="center">\1</div>',
+            '/(?<!&lt;)&lt;flushleft.*&gt;(.*)&lt;\/flushleft&gt;/Uis' => '<div align="left">\1</div>',
+            '/(?<!&lt;)&lt;flushright.*&gt;(.*)&lt;\/flushright&gt;/Uis' => '<div align="right">\1</div>',
+            '/(?<!&lt;)&lt;indent.*&gt;(.*)&lt;\/indent&gt;/Uis' => '<blockquote>\1</blockquote>',
+            '/(?<!&lt;)&lt;excerpt.*&gt;(.*)&lt;\/excerpt&gt;/Uis' => '<cite>\1</cite>',
+            '/(?<!&lt;)&lt;subscript.*&gt;(.*)&lt;\/subscript&gt;/Uis' => '<sub>\1</sub>',
+            '/(?<!&lt;)&lt;superscript.*&gt;(.*)&lt;\/superscript&gt;/Uis' => '<sup>\1</sup>',
+            '/(?<!&lt;)&lt;heading.*&gt;(.*)&lt;\/heading&gt;/Uis' => '<br /><div align="center" style="font-weight: bold">\1</div><br />',
+            '/(?<!&lt;)&lt;footing.*&gt;(.*)&lt;\/footing&gt;/Uis' => '<br /><div align="center" style="font-weight: bold">\1</div><br />',
+            '/(?<!&lt;)&lt;paragraph.*&gt;(.*)&lt;\/paragraph&gt;/Uis' => '<p>\1</p>',
+            '/(?<!&lt;)&lt;signature.*&gt;(.*)&lt;\/signature&gt;/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);
@@ -137,16 +138,18 @@ class Horde_Mime_Viewer_richtext extends Horde_Mime_Viewer_Driver
             $text = '&nbsp;' . 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
+            );
+        }
     }
 }