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