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