Overview

Packages

  • Mime
    • Viewer

Classes

  • Horde_Mime_Viewer
  • Horde_Mime_Viewer_Audio
  • Horde_Mime_Viewer_Base
  • Horde_Mime_Viewer_Deb
  • Horde_Mime_Viewer_Default
  • Horde_Mime_Viewer_Enriched
  • Horde_Mime_Viewer_Exception
  • Horde_Mime_Viewer_Html
  • Horde_Mime_Viewer_Images
  • Horde_Mime_Viewer_Msexcel
  • Horde_Mime_Viewer_Mspowerpoint
  • Horde_Mime_Viewer_Msword
  • Horde_Mime_Viewer_Ooo
  • Horde_Mime_Viewer_Pdf
  • Horde_Mime_Viewer_Plain
  • Horde_Mime_Viewer_Rar
  • Horde_Mime_Viewer_Report
  • Horde_Mime_Viewer_Rfc822
  • Horde_Mime_Viewer_Richtext
  • Horde_Mime_Viewer_Rpm
  • Horde_Mime_Viewer_Rtf
  • Horde_Mime_Viewer_Security
  • Horde_Mime_Viewer_Simple
  • Horde_Mime_Viewer_Smil
  • Horde_Mime_Viewer_Syntaxhighlighter
  • Horde_Mime_Viewer_Tgz
  • Horde_Mime_Viewer_Tnef
  • Horde_Mime_Viewer_Translation
  • Horde_Mime_Viewer_Wordperfect
  • Horde_Mime_Viewer_Zip
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * The Horde_Mime_Viewer_Richtext class renders out HTML text from
  4:  * text/richtext content tags, (RFC 1896 [7.1.3]).
  5:  *
  6:  * A minimal richtext implementation is one that simply converts "<lt>" to
  7:  * "<", converts CRLFs to SPACE, converts <nl> to a newline according to
  8:  * local newline convention, removes everything between a <comment> command
  9:  * and the next balancing </comment> command, and removes all other
 10:  * formatting commands (all text enclosed in angle brackets).
 11:  *
 12:  * We implement the following tags:
 13:  *   <bold>, <italic>, <fixed>, <smaller>, <bigger>, <underline>, <center>,
 14:  *   <flushleft>, <flushright>, <indent>, <subscript>, <excerpt>, <paragraph>,
 15:  *   <signature>, <comment>, <no-op>, <lt>, <nl>
 16:  *
 17:  * The following tags are implemented differently than described in the RFC
 18:  * (due to limitations in HTML output):
 19:  *   <heading> - Output as centered, bold text.
 20:  *   <footing> - Output as centered, bold text.
 21:  *   <np>      - Output as paragraph break.
 22:  *
 23:  * The following tags are NOT implemented:
 24:  *   <indentright>, <outdent>, <outdentright>, <samepage>, <iso-8859-X>,
 25:  *   <us-ascii>,
 26:  *
 27:  * Copyright 2004-2012 Horde LLC (http://www.horde.org/)
 28:  *
 29:  * See the enclosed file COPYING for license information (LGPL). If you
 30:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 31:  *
 32:  * @author   Michael Slusarz <slusarz@horde.org>
 33:  * @category Horde
 34:  * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 35:  * @package  Mime_Viewer
 36:  */
 37: class Horde_Mime_Viewer_Richtext extends Horde_Mime_Viewer_Base
 38: {
 39:     /**
 40:      * This driver's display capabilities.
 41:      *
 42:      * @var array
 43:      */
 44:     protected $_capability = array(
 45:         'full' => true,
 46:         'info' => false,
 47:         'inline' => true,
 48:         'raw' => false
 49:     );
 50: 
 51:     /**
 52:      * Return the full rendered version of the Horde_Mime_Part object.
 53:      *
 54:      * @return array  See parent::render().
 55:      */
 56:     protected function _render()
 57:     {
 58:         return $this->_renderFullReturn($this->_renderReturn(
 59:             $this->_toHTML(),
 60:             'text/html; charset=' . $this->_mimepart->getCharset()
 61: 
 62:         ));
 63:     }
 64: 
 65:     /**
 66:      * Return the rendered inline version of the Horde_Mime_Part object.
 67:      *
 68:      * @return array  See parent::render().
 69:      */
 70:     protected function _renderInline()
 71:     {
 72:         return $this->_renderReturn(
 73:             Horde_String::convertCharset($this->_toHTML(), $this->_mimepart->getCharset(), 'UTF-8'),
 74:             'text/html; charset=UTF-8'
 75:         );
 76:     }
 77: 
 78:     /**
 79:      * Convert richtext to HTML.
 80:      *
 81:      * @return string  The converted HTML string.
 82:      */
 83:     protected function _toHTML()
 84:     {
 85:         $text = trim($this->_mimepart->getContents());
 86:         if ($text == '') {
 87:             return array();
 88:         }
 89: 
 90:         /* We add space at the beginning and end of the string as it will
 91:          * make some regular expression checks later much easier (so we
 92:          * don't have to worry about start/end of line characters). */
 93:         $text = ' ' . $text . ' ';
 94: 
 95:         /* Remove everything between <comment> tags. */
 96:         $text = preg_replace('/<comment.*>.*<\/comment>/Uis', '', $text);
 97: 
 98:         /* Remove any unrecognized tags in the text.  We don't need <no-op>
 99:          * in $tags since it doesn't do anything anyway.  All <comment> tags
100:          * have already been removed. */
101:         $tags = '<bold><italic><fixed><smaller><bigger><underline><center><flushleft><flushright><indent><subscript><excerpt><paragraph><signature><lt><nl>';
102:         $text = strip_tags($text, $tags);
103: 
104:         /* <lt> becomes a '<'. CRLF becomes a SPACE. */
105:         $text = str_ireplace(array('<lt>', "\r\n"), array('&lt;', ' '), $text);
106: 
107:         /* We try to protect against bad stuff here. */
108:         $text = @htmlspecialchars($text, ENT_QUOTES, $this->_mimepart->getCharset());
109: 
110:         /* <nl> becomes a newline (<br />);
111:          * <np> becomes a paragraph break (<p />). */
112:         $text = str_ireplace(array('&lt;nl&gt;', '&lt;np&gt;'), array('<br />', '<p />'), $text);
113: 
114:         /* Now convert the known tags to html. Try to remove any tag
115:          * parameters to stop people from trying to pull a fast one. */
116:         $replace = array(
117:             '/(?<!&lt;)&lt;bold.*&gt;(.*)&lt;\/bold&gt;/Uis' => '<span style="font-weight: bold">\1</span>',
118:             '/(?<!&lt;)&lt;italic.*&gt;(.*)&lt;\/italic&gt;/Uis' => '<span style="font-style: italic">\1</span>',
119:             '/(?<!&lt;)&lt;fixed.*&gt;(.*)&lt;\/fixed&gt;/Uis' => '<font face="fixed">\1</font>',
120:             '/(?<!&lt;)&lt;smaller.*&gt;(.*)&lt;\/smaller&gt;/Uis' => '<span style="font-size: smaller">\1</span>',
121:             '/(?<!&lt;)&lt;bigger.*&gt;(.*)&lt;\/bigger&gt;/Uis' => '<span style="font-size: larger">\1</span>',
122:             '/(?<!&lt;)&lt;underline.*&gt;(.*)&lt;\/underline&gt;/Uis' => '<span style="text-decoration: underline">\1</span>',
123:             '/(?<!&lt;)&lt;center.*&gt;(.*)&lt;\/center&gt;/Uis' => '<div align="center">\1</div>',
124:             '/(?<!&lt;)&lt;flushleft.*&gt;(.*)&lt;\/flushleft&gt;/Uis' => '<div align="left">\1</div>',
125:             '/(?<!&lt;)&lt;flushright.*&gt;(.*)&lt;\/flushright&gt;/Uis' => '<div align="right">\1</div>',
126:             '/(?<!&lt;)&lt;indent.*&gt;(.*)&lt;\/indent&gt;/Uis' => '<blockquote>\1</blockquote>',
127:             '/(?<!&lt;)&lt;excerpt.*&gt;(.*)&lt;\/excerpt&gt;/Uis' => '<cite>\1</cite>',
128:             '/(?<!&lt;)&lt;subscript.*&gt;(.*)&lt;\/subscript&gt;/Uis' => '<sub>\1</sub>',
129:             '/(?<!&lt;)&lt;superscript.*&gt;(.*)&lt;\/superscript&gt;/Uis' => '<sup>\1</sup>',
130:             '/(?<!&lt;)&lt;heading.*&gt;(.*)&lt;\/heading&gt;/Uis' => '<br /><div align="center" style="font-weight: bold">\1</div><br />',
131:             '/(?<!&lt;)&lt;footing.*&gt;(.*)&lt;\/footing&gt;/Uis' => '<br /><div align="center" style="font-weight: bold">\1</div><br />',
132:             '/(?<!&lt;)&lt;paragraph.*&gt;(.*)&lt;\/paragraph&gt;/Uis' => '<p>\1</p>',
133:             '/(?<!&lt;)&lt;signature.*&gt;(.*)&lt;\/signature&gt;/Uis' => '<address>\1</address>'
134:         );
135:         $text = preg_replace(array_keys($replace), array_values($replace), $text);
136: 
137:         /* Now we remove the leading/trailing space we added at the start. */
138:         $text = substr($text, 1, -1);
139: 
140:         /* Wordwrap. */
141:         $text = str_replace(array("\t", '  ', "\n "), array('        ', ' &nbsp;', "\n&nbsp;"), $text);
142:         if ($text[0] == ' ') {
143:             $text = '&nbsp;' . substr($text, 1);
144:         }
145: 
146:         return '<p style="font-size:100%;font-family:Lucida Console,Courier,Courier New;">' . nl2br($text) . '</p>';
147:     }
148: 
149: }
150: 
API documentation generated by ApiGen