/
Plain.php
321 lines (283 loc) · 10.4 KB
/
Plain.php
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
/**
* Copyright 1999-2013 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @category Horde
* @copyright 1999-2013 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
/**
* Renderer for text/plain MIME parts with URLs made into hyperlinks.
*
* @author Anil Madhavapeddy <anil@recoil.org>
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 1999-2013 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
class IMP_Mime_Viewer_Plain extends Horde_Mime_Viewer_Plain
{
/**
* Cached data.
*
* @var array
*/
static protected $_cache = array();
/**
* Return the full rendered version of the Horde_Mime_Part object.
*
* @return array See parent::render().
*/
protected function _render()
{
$data = $this->_impRender(false);
$item = reset($data);
Horde::startBuffer();
$GLOBALS['page_output']->includeStylesheetFiles();
$item['data'] = '<html><head>' . Horde::endBuffer() . '</head><body>' . $item['data'] . '</body></html>';
$data[key($data)] = $item;
return $data;
}
/**
* Return the rendered inline version of the Horde_Mime_Part object.
*
* @return array See parent::render().
*/
protected function _renderInline()
{
return $this->_impRender(true);
}
/**
* Render the object.
*
* @param boolean $inline Viewing inline?
*
* @return array See parent::render().
*/
protected function _impRender($inline)
{
global $injector, $prefs, $registry;
$mime_id = $this->_mimepart->getMimeId();
if (isset(self::$_cache[$mime_id])) {
return array($mime_id => null);
}
// Trim extra whitespace in the text.
$charset = $this->_mimepart->getCharset();
$text = trim($this->_mimepart->getContents());
if ($text == '') {
return array(
$mime_id => array(
'data' => '',
'type' => 'text/html; charset=' . $charset
)
);
}
// Convert to the local charset.
if ($inline) {
$text = Horde_String::convertCharset($text, $charset, 'UTF-8');
$charset = $this->getConfigParam('charset');
}
$type = 'text/html; charset=' . $charset;
// Check for 'flowed' text data.
if ($this->_mimepart->getContentTypeParameter('format') == 'flowed') {
$text = $this->_formatFlowed($text, $this->_mimepart->getContentTypeParameter('delsp'));
} else {
/* A "From" located at the beginning of a line in the body text
* will be escaped with a '>' by the IMAP server. Remove this
* escape character or else the line will display as being
* quoted. Flowed conversion would have already taken care of this
* for us. */
$text = preg_replace('/(\n+)> ?From(\s+)/', "$1From$2", $text);
}
$text = IMP::filterText($text);
/* Done processing if in minimal mode. */
if ($registry->getView() == Horde_Registry::VIEW_MINIMAL) {
$filters = array(
'text2html' => array(
'charset' => $charset,
'parselevel' => Horde_Text_Filter_Text2html::NOHTML_NOBREAK
)
);
$text = $this->_textFilter($text, array_keys($filters), array_values($filters));
return array(
$mime_id => array(
'data' => $text,
'type' => $type
)
);
}
// Build filter stack. Starts with HTML markup and tab expansion.
$filters = array(
'text2html' => array(
'charset' => $charset,
'parselevel' => $inline ? Horde_Text_Filter_Text2html::MICRO : Horde_Text_Filter_Text2html::MICRO_LINKURL
),
'tabs2spaces' => array(),
);
// Highlight quoted parts of an email.
if ($prefs->getValue('highlight_text')) {
if ($registry->getView() == $registry::VIEW_SMARTMOBILE) {
$hideBlocks = $js_blocks = false;
} else {
$js_blocks = $inline;
$show = $prefs->getValue('show_quoteblocks');
$hideBlocks = $inline &&
(($show == 'hidden') ||
(($show == 'thread') && ($injector->getInstance('Horde_Variables')->page == 'thread')));
if (!$hideBlocks &&
in_array($show, array('list', 'listthread'))) {
$header = $this->getConfigParam('imp_contents')->getHeader();
$list_info = $injector->getInstance('IMP_Message_Ui')->getListInformation($header);
$hideBlocks = $list_info['exists'];
}
}
if ($js_blocks) {
$filters['highlightquotes'] = array(
'hideBlocks' => $hideBlocks,
'noJS' => ($registry->getView() == Horde_Registry::VIEW_DYNAMIC)
);
} else {
$filters['Horde_Text_Filter_Highlightquotes'] = array(
'hideBlocks' => $hideBlocks
);
}
}
// Highlight simple markup of an email.
if ($prefs->getValue('highlight_simple_markup')) {
$filters['simplemarkup'] = array();
}
// Dim signatures.
if ($prefs->getValue('dim_signature')) {
$filters['dimsignature'] = array();
}
if ($prefs->getValue('emoticons')) {
$filters['emoticons'] = array('entities' => true);
}
// Run filters.
$status = array();
$text = $this->_textFilter($text, array_keys($filters), array_values($filters));
if (strlen($text)) {
// Wordwrap.
$text = str_replace(array(' ', "\n "), array(' ', "\n "), $text);
if (!strncmp($text, ' ', 1)) {
$text = ' ' . substr($text, 1);
}
} else {
$error = new IMP_Mime_Status(array(
_("Cannot display message text."),
_("The message part may contain incorrect character set information preventing correct display.")
));
$error->action(IMP_Mime_Status::ERROR);
$status[] = $error;
}
return array(
$mime_id => array(
'data' => "<div class=\"fixed leftAlign\">\n" . $text . '</div>',
'status' => $status,
'type' => $type
)
);
}
/**
* Does this MIME part possibly contain embedded MIME parts?
*
* @return boolean True if this driver supports parsing embedded MIME
* parts.
*/
public function embeddedMimeParts()
{
return ($this->getConfigParam('pgp_inline') ||
$this->getConfigParam('uudecode'));
}
/**
* If this MIME part can contain embedded MIME part(s), and those part(s)
* exist, return a representation of that data.
*
* @return mixed A Horde_Mime_Part object representing the embedded data.
* Returns null if no embedded MIME part(s) exist.
*/
protected function _getEmbeddedMimeParts()
{
$ret = $this->getConfigParam('pgp_inline')
? $this->_parsePGP()
: null;
return (is_null($ret) && $this->getConfigParam('uudecode'))
? $this->_parseUUencode()
: $ret;
}
/**
* Scan text for inline, armored PGP blocks and, if they exist, convert
* the part to the embedded MIME representation.
*
* @return mixed See self::_getEmbeddedMimeParts().
*/
protected function _parsePGP()
{
$part = $GLOBALS['injector']->getInstance('IMP_Crypt_Pgp')->armoredTextToPart(
new Horde_Stream_Existing(array(
'stream' => $this->_mimepart->getContents(array('stream' => true))
)),
$this->_mimepart->getCharset()
);
if (!is_null($part)) {
self::$_cache[$this->_mimepart->getMimeId()] = true;
}
return $part;
}
/**
* Scan text for UUencode data an, if it exists, convert the part to the
* embedded MIME representation.
*
* @return mixed See self::_getEmbeddedMimeParts().
*/
protected function _parseUUencode()
{
$text = Horde_String::convertCharset($this->_mimepart->getContents(), $this->_mimepart->getCharset(), 'UTF-8');
$files = Horde_Mime::uudecode($text);
if (empty($files)) {
return null;
}
$new_part = new Horde_Mime_Part();
$new_part->setType('multipart/mixed');
$text_part = new Horde_Mime_Part();
$text_part->setType('text/plain');
$text_part->setCharset($this->getConfigParam('charset'));
$text_part->setContents(preg_replace("/begin [0-7]{3} .+\r?\n.+\r?\nend/Us", "\n", $text));
$new_part->addPart($text_part);
reset($files);
while (list(,$file) = each($files)) {
$uupart = new Horde_Mime_Part();
$uupart->setType('application/octet-stream');
$uupart->setContents($file['data']);
$uupart->setName(strip_tags($file['name']));
$new_part->addPart($uupart);
}
return $new_part;
}
/**
* Output to use if text size is over the limit.
* See IMP_Contents::renderMIMEPart().
*
* @return string The text to display inline.
*/
public function overLimitText()
{
$stream = $this->_mimepart->getContents(array('stream' => true));
rewind($stream);
// Escape text
$filters = array(
'text2html' => array(
'parselevel' => Horde_Text_Filter_Text2html::MICRO
),
'tabs2spaces' => array(),
);
return '<div class="fixed">' .
$this->_textFilter(Horde_String::convertCharset(fread($stream, 1024), $this->_mimepart->getCharset(), 'UTF-8'), array_keys($filters), array_values($filters)) .
' [...]</div>';
}
}