Add support for custom message formatter.
[banana.git] / banana / message.func.inc.php
1 <?php
2 /********************************************************************************
3 * * banana/message.func.inc.php : function to display messages
4 * * ------------------------
5 * *
6 * * This file is part of the banana distribution
7 * * Copyright: See COPYING files that comes with this distribution
8 * ********************************************************************************/
9
10 require_once dirname(__FILE__) . '/mimepart.inc.php';
11 require_once dirname(__FILE__) . '/banana.inc.php';
12
13 // {{{ Plain Text Functions
14
15 function banana_isFlowed($line)
16 {
17 return ctype_space(substr($line, -1)) && $line != '-- ';
18 }
19
20 function banana_removeQuotes($line, &$quote_level, $strict = true)
21 {
22 $quote_level = 0;
23 if (empty($line)) {
24 return '';
25 }
26 while ($line{0} == '>') {
27 $line = substr($line, 1);
28 if (!$strict && ctype_space($line{0})) {
29 $line = substr($line, 1);
30 }
31 $quote_level++;
32 }
33 if (ctype_space($line{0})) {
34 $line = substr($line, 1);
35 }
36 return $line;
37 }
38
39 function banana_quote($line, $level, $mark = '>')
40 {
41 $lines = explode("\n", $line);
42 $quote = str_repeat($mark, $level);
43 foreach ($lines as &$line) {
44 $line = $quote . $line;
45 }
46 return implode("\n", $lines);
47 }
48
49 function banana_unflowed($text)
50 {
51 $lines = explode("\n", $text);
52 $text = '';
53 while (!is_null($line = array_shift($lines))) {
54 $level = 0;
55 $line = banana_removeQuotes($line, $level);
56 while (banana_isFlowed($line)) {
57 $lvl = 0;
58 if (empty($lines)) {
59 break;
60 }
61 $nl = $lines[0];
62 $nl = banana_removeQuotes($nl, $lvl);
63 if ($lvl == $level) {
64 $line .= $nl;
65 array_shift($lines);
66 } else {
67 break;
68 }
69 }
70 $text .= banana_quote($line, $level) . "\n";
71 }
72 return $text;
73 }
74
75 function banana_wordwrap($text, $quote_level = 0)
76 {
77 if ($quote_level > 0) {
78 $length = Banana::$msgshow_wrap - $quote_level - 1;
79 return banana_quote(wordwrap($text, $length), $quote_level);
80 }
81 return wordwrap($text, Banana::$msgshow_wrap);
82 }
83
84 function banana_catchFormats($text)
85 {
86 $formatting = Array('em' => array('\B\/\b', '\b\/\B'),
87 'u' => array('\b_', '_\b'),
88 'strong' => array('\B\*\b', '\b\*\B'));
89 $url = Banana::$msgshow_url;
90 preg_match_all("/$url/ui", $text, $urls);
91 $urls = $urls[0];
92 $replace = $urls;
93 rsort($replace);
94 $text = str_replace($replace, "&&&urls&&&", $text);
95 foreach ($formatting as $mark=>$limit) {
96 list($ll, $lr) = $limit;
97 $text = preg_replace('/' . $ll . '(\w+?)' . $lr . '/us',
98 "<$mark>\\1</$mark>", $text);
99 }
100 return preg_replace('/&&&urls&&&/e', 'array_shift($urls)', $text);
101 }
102
103 /** Build a flowed text from plain text
104 */
105 function banana_flow($text)
106 {
107 $lines = explode("\n", $text);
108 $text = '';
109 while (!is_null($line = array_shift($lines))) {
110 if ($line != '-- ') {
111 $level = 0;
112 $line = banana_removeQuotes($line, $level);
113 $text .= rtrim(str_replace("\n", " \n", banana_wordwrap($line, $level))) . "\n";
114 } else {
115 $text .= $line . "\n";
116 }
117 }
118 return $text;
119 }
120
121 // {{{ URL Catcher tools
122
123 function banana__cutlink($link)
124 {
125 $link = banana_html_entity_decode($link, ENT_QUOTES);
126 if (strlen($link) > Banana::$msgshow_wrap) {
127 $link = substr($link, 0, Banana::$msgshow_wrap - 3) . "...";
128 }
129 return banana_htmlentities($link, ENT_QUOTES);
130 }
131
132 function banana__cleanURL($url)
133 {
134 $url = str_replace('@', '%40', $url);
135 if (strpos($url, '://') === false) {
136 $url = 'http://' . $url;
137 }
138 return '<a href="'.$url.'" title="'.$url.'">' . banana__cutlink($url) . '</a>';
139 }
140
141 function banana__catchMailLink($email)
142 {
143 $mid = '<' . $email . '>';
144 if (isset(Banana::$spool->ids[$mid])) {
145 return Banana::$page->makeLink(Array('group' => Banana::$group,
146 'artid' => Banana::$spool->ids[$mid]->id,
147 'text' => $email));
148 } elseif (strpos($email, '$') !== false) {
149 return $email;
150 }
151 return '<a href="mailto:' . $email . '">' . $email . '</a>';
152 }
153
154 // }}}
155
156 function banana_catchURLs($text)
157 {
158 $url = Banana::$msgshow_url;
159
160 $res = preg_replace("/&(lt|gt|quot);/", " &\\1; ", $text);
161 $res = preg_replace("/$url/uie", "'\\1'.banana__cleanurl('\\2').'\\3'", $res);
162 $res = preg_replace('/(["\[])?(?:mailto:|news:)?([a-z0-9.\-+_\$]+@([\-.+_]?[a-z0-9])+)(["\]])?/ie',
163 "'\\1' . banana__catchMailLink('\\2') . '\\4'",
164 $res);
165 $res = preg_replace("/ &(lt|gt|quot); /", "&\\1;", $res);
166 return $res;
167 }
168
169 // {{{ Quotes catcher functions
170
171 function banana__replaceQuotes($text, $regexp)
172 {
173 return stripslashes(preg_replace("@(^|<pre>|\n)$regexp@i", '\1', $text));
174 }
175
176 // }}}
177
178 function banana_catchQuotes($res, $strict = true)
179 {
180 if ($strict) {
181 $regexp = "&gt;";
182 } else {
183 $regexp = "&gt; *";
184 }
185 while (preg_match("/(^|<pre>|\n)$regexp/i", $res)) {
186 $res = preg_replace("/(^|<pre>|\n)(($regexp.*(?:\n|$))+)/ie",
187 "'\\1</pre><blockquote><pre>'"
188 ." . banana__replaceQuotes('\\2', '$regexp')"
189 ." . '</pre></blockquote><pre>'",
190 $res);
191 }
192 return $res;
193 }
194
195 function banana_catchSignature($res)
196 {
197 $res = preg_replace("@<pre>-- ?\n@", "<pre>\n-- \n", $res);
198 $parts = preg_split("/\n-- ?\n/", $res);
199 $sign = '</pre><hr style="width: 100%; margin: 1em 0em; " /><pre>';
200 return join($sign, $parts);
201 }
202
203 function banana_plainTextToHtml($text, $strict = true)
204 {
205 $text = banana_htmlentities($text);
206 $text = banana_catchFormats($text);
207 $text = banana_catchURLs($text);
208 $text = banana_catchQuotes($text, $strict);
209 $text = banana_catchSignature($text);
210 return '<pre>' . $text . '</pre>';
211 }
212
213 function banana_wrap($text, $base_level = 0, $strict = true)
214 {
215 $lines = explode("\n", $text);
216 $text = '';
217 $buffer = array();
218 $level = 0;
219 while (!is_null($line = array_shift($lines))) {
220 $lvl = 0;
221 $line = banana_removeQuotes($line, $lvl, $strict);
222 if($lvl != $level) {
223 if (!empty($buffer)) {
224 $text .= banana_wordwrap(implode("\n", $buffer), $level + $base_level) . "\n";
225 $buffer = array();
226 }
227 $level = $lvl;
228 }
229 $buffer[] = $line;
230 }
231 if (!empty($buffer)) {
232 $text .= banana_wordwrap(implode("\n", $buffer), $level + $base_level);
233 }
234 return $text;
235 }
236
237 function banana_formatPlainText(BananaMimePart $part, $base_level = 0)
238 {
239 $text = $part->getText();
240 if ($part->isFlowed()) {
241 $text = banana_unflowed($text);
242 }
243 if (function_exists('hook_formatPart') && ($ret = hook_formatPart($text, $part, $base_level))) {
244 return $ret;
245 } else {
246 $text = banana_wrap($text, $base_level, $part->isFlowed());
247 return banana_plainTextToHtml($text, $part->isFlowed());
248 }
249 }
250
251 function banana_quotePlainText(BananaMimePart $part)
252 {
253 $text = $part->getText();
254 if ($part->isFlowed()) {
255 $text = banana_unflowed($text);
256 }
257 return banana_quote($text, 1);
258 }
259
260 // }}}
261 // {{{ HTML Functions
262
263 function banana_htmlentities($text, $quote = ENT_COMPAT)
264 {
265 return htmlentities($text, $quote, 'UTF-8');
266 }
267
268 function banana_html_entity_decode($text, $quote = ENT_COMPAT)
269 {
270 return html_entity_decode($text, $quote, 'UTF-8');
271 }
272
273 function banana_removeEvilAttributes($tagSource)
274 {
275 $stripAttrib = 'javascript:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|'.
276 'onmousemove|onmouseout|onkeypress|onkeydown|onkeyup';
277 return stripslashes(preg_replace("/$stripAttrib/i", '', $tagSource));
278 }
279
280 function banana_cleanStyles($tag, $attributes)
281 {
282 static $td_style, $conv, $size_conv;
283 if (!isset($td_style)) {
284 $conv = array('style' => 'style', 'width' => 'width', 'height' => 'height', 'border' => 'border-size',
285 'size' => 'font-size', 'align' => 'text-align', 'valign' => 'vertical-align', 'face' => 'font',
286 'bgcolor' => 'background-color', 'color' => 'color', 'style' => 'style',
287 'cellpadding' => 'padding', 'cellspacing' => 'border-spacing');
288 $size_conv = array(1 => 'xx-small', 2 => 'x-small', 3 => 'small', 4 => 'medium', 5 => 'large',
289 6 => 'x-large', 7 => 'xx-large',
290 '-2' => 'xx-small', '-1' => 'x-small', '+1' => 'medium', '+2' => 'large',
291 '+3' => 'x-large', '+4' => 'xx-large');
292 $td_style = array();
293 }
294 if ($tag == 'table') {
295 array_unshift($td_style, '');
296 }
297 if ($tag == '/table') {
298 array_shift($td_style);
299 }
300 if ($tag{0} == '/') {
301 return '';
302 }
303 if ($tag == 'td') {
304 $style = $td_style[0];
305 } else {
306 $style = '';
307 }
308 $attributes = str_replace(array("\n", "\r"), ' ', stripslashes($attributes));
309 $attributes = str_replace(array('= "', '= \''), array('="', '=\''), $attributes);
310 foreach ($conv as $att=>$stl) {
311 $pattern = '/\b' . preg_quote($att, '/') . '=([\'"])?(.+?)(?(1)\1|(?:$| ))/i';
312 if (preg_match($pattern, $attributes, $matches)) {
313 $attributes = preg_replace($pattern, '', $attributes);
314 $val = $matches[2];
315 if ($att == 'cellspacing' && strpos($style, 'border-collapse') === false) {
316 $style = "border-collapse: separate; border-spacing: $val $val; " . $style;
317 } elseif ($att == 'cellpadding' && $tag == 'table') {
318 $td_style[0] = "$stl: {$val}px; ";
319 } elseif ($att == 'style') {
320 $val = rtrim($val, ' ;');
321 $style .= "$val; ";
322 } elseif ($att == 'size') {
323 $val = $size_conv[$val];
324 $style = "$stl: $val; " . $style;
325 } elseif (is_numeric($val)) {
326 $style = "$stl: {$val}px; " . $style;
327 } else {
328 $style = "$stl: $val; " . $style;
329 }
330 }
331 }
332 if (!empty($style)) {
333 $style = 'style="' . $style . '" ';
334 }
335 return ' ' . $style . trim($attributes);
336 }
337
338 function banana__filterCss($text)
339 {
340 $text = preg_replace("/(,[\s\n\r]*)/s", '\1 .banana .message .body .html ', $text);
341 return '.banana .message .body .html ' . $text;
342 }
343
344 function banana_filterCss($css)
345 {
346 preg_match_all("/(^|\n|,\s*)\s*([\#\.@\w][^;\{\}\<]*?[\{])/s", $css, $matches);
347 $css = preg_replace("/(^|\n)\s*([\#\.@\w][^;\{\}\<]*?)([\{])/se", '"\1" . banana__filterCss("\2") . "\3"', $css);
348 $css = preg_replace('/ body\b/i', '', $css);
349 if (!Banana::$msgshow_externalimages) {
350 if (preg_match('!url\([^:\)]+:(//|\\\).*?\)!i', $css)) {
351 $css = preg_replace('!url\([^:\)]+:(//|\\\).*?\)!i', 'url(invalid-image.png)', $css);
352 Banana::$msgshow_hasextimages = true;
353 }
354 }
355 return $css;
356 }
357
358 /**
359 * @return string
360 * @param string
361 * @desc Strip forbidden tags and delegate tag-source check to removeEvilAttributes()
362 */
363 function banana_cleanHtml($source, $to_xhtml = false)
364 {
365 if (function_exists('tidy_repair_string')) {
366 $tidy_config = array('drop-empty-paras' => true,
367 'drop-proprietary-attributes' => true,
368 'hide-comments' => true,
369 'logical-emphasis' => true,
370 'output-xhtml' => true,
371 'replace-color' => true,
372 'join-classes' => false,
373 'clean' => false,
374 'show-body-only' => false,
375 'alt-text' => '[ inserted by TIDY ]',
376 'wrap' => 120);
377 if (function_exists('tidy_setopt')) { // Tidy 1.0
378 foreach ($tidy_config as $field=>$value) {
379 tidy_setopt($field, $value);
380 }
381 tidy_set_encoding('utf8');
382 $source = tidy_repair_string($source);
383
384 } else { // Tidy 2.0
385 $source = tidy_repair_string($source, $tidy_config, 'utf8');
386 }
387 }
388
389 // To XHTML
390 if ($to_xhtml) {
391 // catch inline CSS
392 $css = null;
393 if (preg_match('/<head.*?>(.*?)<\/head>/is', $source, $matches)) {
394 $source = preg_replace('/<head.*?>.*?<\/head>/is', '', $source);
395 preg_match_all('/<style(?:.*?type="text\/css".*?)?>(.*?)<\/style>/is', $matches[1], $matches);
396 foreach ($matches[1] as &$match) {
397 $css .= $match;
398 }
399 $css = banana_filterCss($css);
400 Banana::$page->addCssInline($css);
401 }
402
403 // clean DTD
404 $source = str_replace('<font', '<span', $source);
405 $source = preg_replace('/<u\b/', '<span style="text-decoration: underline"', $source);
406 $source = preg_replace('/<\/(font|u)>/', '</span>', $source);
407 $source = str_replace('<body', $css ? '<div class="html"' : '<div class="html default"', $source);
408 $source = str_replace('</body>', '</div>', $source);
409 }
410 $allowedTags = '<h1><h2><h3><b><i><a><ul><li><pre><hr><blockquote><img><br><div><span>'
411 . '<p><small><big><sup><sub><code><em><strong><table><tr><td><th>';
412 $source = strip_tags($source, $allowedTags);
413
414 // Use inlined style instead of old html attributes
415 if ($to_xhtml) {
416 $source = preg_replace('/<(\/?\w+)(.*?)(\/?>)/muise', "'<\\1' . banana_cleanStyles('\\1', '\\2') . '\\3'", $source);
417 }
418 return preg_replace('/<(.*?)>/ie', "'<'.banana_removeEvilAttributes('\\1').'>'", $source);
419 }
420
421 function banana_catchHtmlSignature($res)
422 {
423 $res = preg_replace("@(</p>)\n?-- ?\n?(<p[^>]*>|<br[^>]*>)@", "\\1<br/>-- \\2", $res);
424 $res = preg_replace("@<br[^>]*>\n?-- ?\n?(<p[^>]*>)@", "<br/>-- <br/>\\2", $res);
425 $res = preg_replace("@(<pre[^>]*>)\n?-- ?\n@", "<br/>-- <br/>\\1", $res);
426 $parts = preg_split("@(:?<p[^>]*>\n?-- ?\n?</p>|<br[^>]*>\n?-- ?\n?<br[^>]*>)@", $res);
427 $sign = '<hr style="width: 100%; margin: 1em 0em; " />';
428 return join($sign, $parts);
429 }
430
431 // {{{ Link to part catcher tools
432
433 function banana__linkAttachment($cid)
434 {
435 return banana_htmlentities(
436 Banana::$page->makeUrl(Array('group' => Banana::$group,
437 'artid' => Banana::$artid,
438 'part' => $cid)));
439 }
440
441 // }}}
442
443 function banana_hideExternalImages($text)
444 {
445 if (preg_match("/<img([^>]*?)src=['\"](?!cid).*?['\"](.*?)>/i", $text)) {
446 Banana::$msgshow_hasextimages = true;
447 return preg_replace("/<img([^>]*?)src=['\"](?!cid).*?['\"](.*?)>/i",
448 '<img\1src="invalid"\2>',
449 $text);
450 }
451 return $text;
452 }
453
454 function banana_catchPartLinks($text)
455 {
456 $article = Banana::$page->makeURL(array('group' => Banana::$group, 'artid' => Banana::$artid, 'part' => Banana::$part));
457 $article = banana_htmlentities($article);
458 $text = preg_replace('/cid:([^\'" ]+)/e', "banana__linkAttachment('\\1')", $text);
459 $text = preg_replace('/href="(#.*?)"/i', 'href="' . $article . '\1"', $text);
460 return $text;
461 }
462
463 // {{{ HTML to Plain Text tools
464
465 function banana__convertFormats($res)
466 {
467 $table = array('em|i' => '/',
468 'strong|b' => '*',
469 'u' => '_');
470 foreach ($table as $tags=>$format) {
471 $res = preg_replace("!</?($tags)( .*?)?>!is", $format, $res);
472 }
473 return $res;
474 }
475
476 function banana__convertQuotes($res)
477 {
478 return preg_replace('!<blockquote.*?>([^<]*)</blockquote>!ies',
479 "\"\n\" . banana_quote(banana__convertQuotes('\\1' . \"\n\"), 1, '&gt;')",
480 $res);
481 }
482
483 // }}}
484
485 function banana_htmlToPlainText($res)
486 {
487 $res = str_replace("\n", '', $res);
488 $res = banana__convertFormats($res);
489 $res = trim(strip_tags($res, '<div><br><p><blockquote>'));
490 $res = preg_replace("@</?(br|p|div).*?>@si", "\n", $res);
491 $res = banana__convertQuotes($res);
492 return banana_html_entity_decode($res);
493 }
494
495 function banana_formatHtml(BananaMimePart $part)
496 {
497 $text = $part->getText();
498 $text = banana_catchHtmlSignature($text);
499 if (!Banana::$msgshow_externalimages) {
500 $text = banana_hideExternalImages($text);
501 }
502 $text = banana_catchPartLinks($text);
503 return banana_cleanHtml($text, true);
504 }
505
506 function banana_quoteHtml(BananaMimePart $part)
507 {
508 $text = $part->getText();
509 $text = banana_htmlToPlainText($text);
510 return banana_quote($text, 1);
511 }
512
513 // }}}
514 // {{{ Richtext Functions
515
516 /** Convert richtext to html
517 */
518 function banana_richtextToHtml($source)
519 {
520 $tags = Array('bold' => 'b',
521 'italic' => 'i',
522 'smaller' => 'small',
523 'bigger' => 'big',
524 'underline' => 'u',
525 'subscript' => 'sub',
526 'superscript' => 'sup',
527 'excerpt' => 'blockquote',
528 'paragraph' => 'p',
529 'nl' => 'br'
530 );
531
532 // clean unsupported tags
533 $protectedTags = '<signature><lt><comment><'.join('><', array_keys($tags)).'>';
534 $source = strip_tags($source, $protectedTags);
535
536 // convert richtext tags to html
537 foreach (array_keys($tags) as $tag) {
538 $source = preg_replace('@(</?)'.$tag.'([^>]*>)@i', '\1'.$tags[$tag].'\2', $source);
539 }
540
541 // some special cases
542 $source = preg_replace('@<signature>@i', '<br>-- <br>', $source);
543 $source = preg_replace('@</signature>@i', '', $source);
544 $source = preg_replace('@<lt>@i', '&lt;', $source);
545 $source = preg_replace('@<comment[^>]*>((?:[^<]|<(?!/comment>))*)</comment>@i', '<!-- \1 -->', $source);
546 return banana_cleanHtml($source);
547 }
548
549 function banana_formatRichText(BananaMimePart $part)
550 {
551 $text = $part->getText();
552 $text = banana_richtextToHtml($text);
553 $text = banana_catchHtmlSignature($text);
554 return banana_cleanHtml($text);
555 }
556
557 function banana_quoteRichtText(BananaMimePart $part)
558 {
559 $text = $part->getText();
560 $text = banana_richtextToHtml($text);
561 $text = banana_htmlToPlainText($text);
562 return banana_quote($text, 1);
563 }
564
565 // }}}
566
567 // vim:set et sw=4 sts=4 ts=4 enc=utf-8:
568 ?>