1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
<?php
/**
* The MIME_Viewer_richtext class renders out HTML text from text/richtext
* content tags, (RFC 1896 [7.1.3]).
*
* A minimal richtext implementation is one that simply converts "<lt>" to
* "<", converts CRLFs to SPACE, converts <nl> to a newline according to
* local newline convention, removes everything between a <comment> command
* and the next balancing </comment> command, and removes all other
* other 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>
*
* 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.
*
* The following tags are NOT implemented:
* <indentright>, <outdent>, <outdentright>, <samepage>, <iso-8859-X>,
* <us-ascii>,
*
* $Horde: framework/MIME/MIME/Viewer/richtext.php,v 1.4.10.7 2006/01/01 21:28:25 jan Exp $
*
* Copyright 2004-2006 Michael Slusarz <slusarz@bigworm.colorado.edu>
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
*
* @author Michael Slusarz <slusarz@bigworm.colorado.edu>
* @since Horde 3.0
* @package Horde_MIME_Viewer
*/
class MIME_Viewer_richtext extends MIME_Viewer {
/**
* Render out the currently set contents in HTML format.
*
* @param array $params Any parameters the Viewer may need.
*
* @return string The rendered contents.
*/
function render($params = array())
{
if (($text = $this->mime_part->getContents()) === false) {
return false;
}
if (trim($text) == '') {
return $text;
}
/* Use str_ireplace() if using PHP 5.0+. */
$has_str_ireplace = function_exists('str_ireplace');
/* We add space at the beginning and end of the string as it will
* make some regular expression checks later much easier (so we
* don't have to worry about start/end of line characters). */
$text = ' ' . $text . ' ';
/* Remove everything between <comment> tags. */
$text = preg_replace('/<comment.*>.*<\/comment>/Uis', '', $text);
/* Remove any unrecognized tags in the text. We don't need <no-op>
* in $tags since it doesn't do anything anyway. All <comment> tags
* have already been removed. */
$tags = '<bold><italic><fixed><smaller><bigger><underline><center><flushleft><flushright><indent><subscript><excerpt><paragraph><signature><lt><nl>';
$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);
}
/* We try to protect against bad stuff here. */
$text = @htmlspecialchars($text, ENT_QUOTES, $this->mime_part->getCharset());
/* <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);
}
/* 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('/(?<!<)<bold.*>(.*)<\/bold>/Uis', '<span style="font-weight: bold">\1</span>', $text);
$text = preg_replace('/(?<!<)<italic.*>(.*)<\/italic>/Uis', '<span style="font-style: italic">\1</span>', $text);
$text = preg_replace('/(?<!<)<fixed.*>(.*)<\/fixed>/Uis', '<font face="fixed">\1</font>', $text);
$text = preg_replace('/(?<!<)<smaller.*>/Uis', '<span style="font-size: smaller">', $text);
$text = preg_replace('/(?<!<)<\/smaller>/Uis', '</span>', $text);
$text = preg_replace('/(?<!<)<bigger.*>/Uis', '<span style="font-size: larger">', $text);
$text = preg_replace('/(?<!<)<\/bigger>/Uis', '</span>', $text);
$text = preg_replace('/(?<!<)<underline.*>(.*)<\/underline>/Uis', '<span style="text-decoration: underline">\1</span>', $text);
$text = preg_replace('/(?<!<)<center.*>(.*)<\/center>/Uis', '<div align="center">\1</div>', $text);
$text = preg_replace('/(?<!<)<flushleft.*>(.*)<\/flushleft>/Uis', '<div align="left">\1</div>', $text);
$text = preg_replace('/(?<!<)<flushright.*>(.*)<\/flushright>/Uis', '<div align="right">\1</div>', $text);
$text = preg_replace('/(?<!<)<indent.*>(.*)<\/indent>/Uis', '<blockquote>\1</blockquote>', $text);
$text = preg_replace('/(?<!<)<excerpt.*>(.*)<\/excerpt>/Uis', '<cite>\1</cite>', $text);
$text = preg_replace('/(?<!<)<subscript.*>(.*)<\/subscript>/Uis', '<sub>\1</sub>', $text);
$text = preg_replace('/(?<!<)<superscript.*>(.*)<\/superscript>/Uis', '<sup>\1</sup>', $text);
$text = preg_replace('/(?<!<)<heading.*>(.*)<\/heading>/Uis', '<br /><div align="center" style="font-weight: bold">\1</div><br />', $text);
$text = preg_replace('/(?<!<)<footing.*>(.*)<\/footing>/Uis', '<br /><div align="center" style="font-weight: bold">\1</div><br />', $text);
$text = preg_replace('/(?<!<)<paragraph.*>(.*)<\/paragraph>/Uis', '<p>\1</p>', $text);
$text = preg_replace('/(?<!<)<signature.*>(.*)<\/signature>/Uis', '<address>\1</address>', $text);
/* Now we remove the leading/trailing space we added at the start. */
$text = substr($text, 1, -1);
/* Wordwrap. */
$text = str_replace("\t", ' ', $text);
$text = str_replace(' ', ' ', $text);
$text = str_replace("\n ", "\n ", $text);
if ($text[0] == ' ') {
$text = ' ' . substr($text, 1);
}
$text = nl2br($text);
$text = '<p class="fixed">' . $text . '</p>';
return $text;
}
/**
* Return the MIME content type of the rendered content.
*
* @return string The content type of the output.
*/
function getType()
{
return 'text/html; charset=' . NLS::getCharset();
}
}
|