1: <?php
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: class Horde_Mime_Viewer_Enriched extends Horde_Mime_Viewer_Base
29: {
30: 31: 32: 33: 34:
35: protected $_capability = array(
36: 'full' => true,
37: 'info' => false,
38: 'inline' => true,
39: 'raw' => false
40: );
41:
42: 43: 44: 45: 46:
47: protected function _render()
48: {
49: return $this->_renderReturn(
50: '<html><body>' . $this->_toHTML(false) . '</body></html>',
51: 'text/html; charset=' . $this->_mimepart->getCharset()
52: );
53: }
54:
55: 56: 57: 58: 59:
60: protected function _renderInline()
61: {
62: return $this->_renderReturn(
63: Horde_String::convertCharset($this->_toHTML(true), $this->_mimepart->getCharset(), 'UTF-8'),
64: 'text/html; charset=UTF-8'
65: );
66: }
67:
68: 69: 70: 71: 72: 73: 74:
75: protected function _toHTML($inline)
76: {
77: $text = trim($this->_mimepart->getContents());
78: if (!strlen($text)) {
79: return array();
80: }
81:
82:
83:
84:
85: $text = ' ' . $text . ' ';
86:
87:
88:
89:
90:
91:
92: $text = str_replace('<<', chr(1), $text);
93:
94:
95:
96:
97: $implementedTags = '<param><bold><italic><underline><fixed><excerpt>' .
98: '<smaller><bigger><center><color><fontfamily>' .
99: '<flushleft><flushright><flushboth><paraindent>';
100:
101: $text = strip_tags($text, $implementedTags);
102:
103:
104: $text = str_replace(chr(1), '<<', $text);
105:
106:
107: $replace = array(
108:
109: '/<color><param>([\da-fA-F]+),([\da-fA-F]+),([\da-fA-F]+)<\/param>/Uis' => '<color r=\1 g=\2 b=\3>',
110: '/<color><param>(red|blue|green|yellow|cyan|magenta|black|white)<\/param>/Uis' => '<color n=\1>',
111:
112:
113: '/<fontfamily><param>(\w+)<\/param>/Uis' => '<fontfamily f=\1>',
114:
115: 116: 117: 118:
119: '/<param>.*<\/param>/Uis' => '',
120:
121: 122: 123: 124: 125:
126: '/([^\n])\r\n([^\r])/' => '\1 \2',
127: '/(\r\n)\r\n/' => '\1'
128: );
129: $text = preg_replace(array_keys($replace), array_values($replace), $text);
130:
131:
132: $text = @htmlspecialchars($text, ENT_QUOTES, $this->_mimepart->getCharset());
133:
134:
135:
136: $replace = array(
137: '/(?<!<)<bold.*>(.*)<\/bold>/Uis' => '<span style="font-weight: bold">\1</span>',
138: '/(?<!<)<italic.*>(.*)<\/italic>/Uis' => '<span style="font-style: italic">\1</span>',
139: '/(?<!<)<underline.*>(.*)<\/underline>/Uis' => '<span style="text-decoration: underline">\1</span>'
140: );
141: $text = preg_replace(array_keys($replace), array_values($replace), $text);
142:
143: $text = preg_replace_callback('/(?<!<)<color r=([\da-fA-F]+) g=([\da-fA-F]+) b=([\da-fA-F]+)>(.*)<\/color>/Uis', array($this, 'colorize'), $text);
144:
145: $replace = array(
146: '/(?<!<)<color n=(red|blue|green|yellow|cyan|magenta|black|white)>(.*)<\/color>/Uis' => '<span style="color: \1">\2</span>',
147: '/(?<!<)<fontfamily>(.*)<\/fontfamily>/Uis' => '\1',
148: '/(?<!<)<fontfamily f=(\w+)>(.*)<\/fontfamily>/Uis' => '<span style="font-family: \1">\2</span>',
149: '/(?<!<)<smaller.*>/Uis' => '<span style="font-size: smaller">',
150: '/(?<!<)<\/smaller>/Uis' => '</span>',
151: '/(?<!<)<bigger.*>/Uis' => '<span style="font-size: larger">',
152: '/(?<!<)<\/bigger>/Uis' => '</span>',
153: '/(?<!<)<fixed.*>(.*)<\/fixed>/Uis' => '<font face="fixed">\1</font>',
154: '/(?<!<)<center.*>(.*)<\/center>/Uis' => '<div align="center">\1</div>',
155: '/(?<!<)<flushleft.*>(.*)<\/flushleft>/Uis' => '<div align="left">\1</div>',
156: '/(?<!<)<flushright.*>(.*)<\/flushright>/Uis' => '<div align="right">\1</div>',
157: '/(?<!<)<flushboth.*>(.*)<\/flushboth>/Uis' => '<div align="justify">\1</div>',
158: '/(?<!<)<paraindent.*>(.*)<\/paraindent>/Uis' => '<blockquote>\1</blockquote>',
159: '/(?<!<)<excerpt.*>(.*)<\/excerpt>/Uis' => '<blockquote>\1</blockquote>'
160: );
161: $text = preg_replace(array_keys($replace), array_values($replace), $text);
162:
163:
164: $text = str_replace('<<', '<', $text);
165:
166:
167:
168: $text = preg_replace('/^ (.*) $/s', '\1', $text);
169:
170:
171: $text = $this->_textFilter($text, 'linkurls');
172:
173: 174:
175: $text = str_replace(array("\t", ' ', "\n "), array(' ', ' ', "\n "), $text);
176:
177: if ($text[0] == ' ') {
178: $text = ' ' . substr($text, 1);
179: }
180:
181: return '<p class="fixed">' . nl2br($text) . '</p>';
182: }
183:
184: 185: 186:
187: public function colorize($colors)
188: {
189: for ($i = 1; $i < 4; $i++) {
190: $colors[$i] = sprintf('%02X', round(hexdec($colors[$i]) / 255));
191: }
192: return '<span style="color: #' . $colors[1] . $colors[2] . $colors[3] . '">' . $colors[4] . '</span>';
193: }
194:
195: }
196: