2 /********************************************************************************
3 * * banana/message.func.inc.php : function to display messages
4 * * ------------------------
6 * * This file is part of the banana distribution
7 * * Copyright: See COPYING files that comes with this distribution
8 * ********************************************************************************/
10 require_once dirname(__FILE__
) . '/mimepart.inc.php';
11 require_once dirname(__FILE__
) . '/banana.inc.php';
13 // {{{ Plain Text Functions
15 function banana_isFlowed($line)
17 return ctype_space(substr($line, -1)) && $line != '-- ';
20 function banana_removeQuotes($line, &$quote_level, $strict = true
)
26 while ($line{0} == '>') {
27 $line = substr($line, 1);
28 if (!$strict && ctype_space($line{0})) {
29 $line = substr($line, 1);
33 if (ctype_space($line{0})) {
34 $line = substr($line, 1);
39 function banana_quote($line, $level, $mark = '>')
41 $lines = explode("\n", $line);
42 $quote = str_repeat($mark, $level);
43 foreach ($lines as &$line) {
44 $line = $quote . $line;
46 return implode("\n", $lines);
49 function banana_unflowed($text)
51 $lines = explode("\n", $text);
53 while (!is_null($line = array_shift($lines))) {
55 $line = banana_removeQuotes($line, $level);
56 while (banana_isFlowed($line)) {
62 $nl = banana_removeQuotes($nl, $lvl);
70 $text .= banana_quote($line, $level) . "\n";
75 function banana_wordwrap($text, $quote_level = 0)
77 if ($quote_level > 0) {
78 $length = Banana
::$msgshow_wrap - $quote_level - 1;
79 return banana_quote(wordwrap($text, $length), $quote_level);
81 return wordwrap($text, Banana
::$msgshow_wrap);
84 function banana_catchFormats($text)
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);
97 return preg_replace('/&&&urls&&&/e', 'array_shift($urls[0])', $text);
100 /** Build a flowed text from plain text
102 function banana_flow($text)
104 $lines = explode("\n", $text);
106 while (!is_null($line = array_shift($lines))) {
107 if ($line != '-- ') {
109 $line = banana_removeQuotes($line, $level);
110 $text .= rtrim(str_replace("\n", " \n", banana_wordwrap($line, $level))) . "\n";
112 $text .= $line . "\n";
118 // {{{ URL Catcher tools
120 function banana__cutlink($link)
122 $link = banana_html_entity_decode($link, ENT_QUOTES
);
123 if (strlen($link) > Banana
::$msgshow_wrap) {
124 $link = substr($link, 0, Banana
::$msgshow_wrap - 3) . "...";
126 return banana_htmlentities($link, ENT_QUOTES
);
129 function banana__cleanURL($url)
131 $url = str_replace('@', '%40', $url);
132 if (strpos($url, '://') === false
) {
133 $url = 'http://' . $url;
135 return '<a href="'.$url.'" title="'.$url.'">' . banana__cutlink($url) . '</a>';
138 function banana__catchMailLink($email)
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],
145 } elseif (strpos($email, '$') !== false
) {
148 return '<a href="mailto:' . $email . '">' . $email . '</a>';
153 function banana_catchURLs($text)
155 $url = Banana
::$msgshow_url;
157 $res = preg_replace("/&(lt|gt|quot);/", " &\\1; ", $text);
158 $res = preg_replace("/$url/uie", "'\\1'.banana__cleanurl('\\2').'\\3'", $res);
159 $res = preg_replace('/(["\[])?(?:mailto:|news:)?([a-z0-9.\-+_\$]+@([\-.+_]?[a-z0-9])+)(["\]])?/ie',
160 "'\\1' . banana__catchMailLink('\\2') . '\\4'",
162 $res = preg_replace("/ &(lt|gt|quot); /", "&\\1;", $res);
166 // {{{ Quotes catcher functions
168 function banana__replaceQuotes($text, $regexp)
170 return stripslashes(preg_replace("@(^|<pre>|\n)$regexp@i", '\1', $text));
175 function banana_catchQuotes($res, $strict = true
)
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>'",
192 function banana_catchSignature($res)
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);
200 function banana_plainTextToHtml($text, $strict = true
)
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);
207 return '<pre>' . $text . '</pre>';
210 function banana_wrap($text, $base_level = 0, $strict = true
)
212 $lines = explode("\n", $text);
216 while (!is_null($line = array_shift($lines))) {
218 $line = banana_removeQuotes($line, $lvl, $strict);
220 if (!empty($buffer)) {
221 $text .= banana_wordwrap(implode("\n", $buffer), $level +
$base_level) . "\n";
228 if (!empty($buffer)) {
229 $text .= banana_wordwrap(implode("\n", $buffer), $level +
$base_level);
234 function banana_formatPlainText(BananaMimePart
&$part, $base_level = 0)
236 $text = $part->getText();
237 if ($part->isFlowed()) {
238 $text = banana_unflowed($text);
240 $text = banana_wrap($text, $base_level, $part->isFlowed());
241 return banana_plainTextToHtml($text, $part->isFlowed());
244 function banana_quotePlainText(BananaMimePart
&$part)
246 $text = $part->getText();
247 if ($part->isFlowed()) {
248 $text = banana_unflowed($text);
250 return banana_quote($text, 1);
254 // {{{ HTML Functions
256 function banana_htmlentities($text, $quote = ENT_COMPAT
)
258 return htmlentities($text, $quote, 'UTF-8');
261 function banana_html_entity_decode($text, $quote = ENT_COMPAT
)
263 return html_entity_decode($text, $quote, 'UTF-8');
266 function banana_removeEvilAttributes($tagSource)
268 $stripAttrib = 'javascript:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|'.
269 'onmousemove|onmouseout|onkeypress|onkeydown|onkeyup';
270 return stripslashes(preg_replace("/$stripAttrib/i", '', $tagSource));
273 function banana_cleanStyles($tag, $attributes)
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');
287 if ($tag == 'table') {
288 array_unshift($td_style, '');
290 if ($tag == '/table') {
291 array_shift($td_style);
293 if ($tag{0} == '/') {
297 $style = $td_style[0];
301 $attributes = str_replace(array("\n", "\r"), ' ', stripslashes($attributes));
302 $attributes = str_replace(array('= "', '= \''), array('="', '=\''), $attributes);
303 foreach ($conv as $att=>$stl) {
304 $pattern = '/\b' . preg_quote($att, '/') . '=([\'"])?(.+?)(?(1)\1|(?:$| ))/i';
305 if (preg_match($pattern, $attributes, $matches)) {
306 $attributes = preg_replace($pattern, '', $attributes);
308 if ($att == 'cellspacing' && strpos($style, 'border-collapse') === false
) {
309 $style = "border-collapse: separate; border-spacing: $val $val; " . $style;
310 } elseif ($att == 'cellpadding' && $tag == 'table') {
311 $td_style[0] = "$stl: {$val}px; ";
312 } elseif ($att == 'style') {
313 $val = rtrim($val, ' ;');
315 } elseif ($att == 'size') {
316 $val = $size_conv[$val];
317 $style = "$stl: $val; " . $style;
318 } elseif (is_numeric($val)) {
319 $style = "$stl: {$val}px; " . $style;
321 $style = "$stl: $val; " . $style;
325 if (!empty($style)) {
326 $style = 'style="' . $style . '" ';
328 return ' ' . $style . trim($attributes);
331 function banana__filterCss($text)
333 $text = preg_replace("/(,[\s\n\r]*)/s", '\1 .banana .message .body .html ', $text);
334 return '.banana .message .body .html ' . $text;
337 function banana_filterCss($css)
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);
341 $css = preg_replace('/ body\b/i', '', $css);
342 if (!Banana
::$msgshow_externalimages) {
343 if (preg_match('!url\([^:\)]+:(//|\\\).*?\)!i', $css)) {
344 $css = preg_replace('!url\([^:\)]+:(//|\\\).*?\)!i', 'url(invalid-image.png)', $css);
345 Banana
::$msgshow_hasextimages = true
;
354 * @desc Strip forbidden tags and delegate tag-source check to removeEvilAttributes()
356 function banana_cleanHtml($source, $to_xhtml = false
)
358 if (function_exists('tidy_repair_string')) {
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
,
367 'show-body-only' => false
,
368 'alt-text' => '[ inserted by TIDY ]',
370 if (function_exists('tidy_setopt')) { // Tidy 1.0
371 foreach ($tidy_config as $field=>$value) {
372 tidy_setopt($field, $value);
374 tidy_set_encoding('utf8');
375 $source = tidy_repair_string($source);
378 $source = tidy_repair_string($source, $tidy_config, 'utf8');
386 if (preg_match('/<head.*?>(.*?)<\/head>/is', $source, $matches)) {
387 $source = preg_replace('/<head.*?>.*?<\/head>/is', '', $source);
388 preg_match_all('/<style(?:.*?type="text\/css".*?)?>(.*?)<\/style>/is', $matches[1], $matches);
389 foreach ($matches[1] as &$match) {
392 $css = banana_filterCss($css);
393 Banana
::$page->addCssInline($css);
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);
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);
407 // Use inlined style instead of old html attributes
409 $source = preg_replace('/<(\/?\w+)(.*?)(\/?>)/muise', "'<\\1' . banana_cleanStyles('\\1', '\\2') . '\\3'", $source);
411 return preg_replace('/<(.*?)>/ie', "'<'.banana_removeEvilAttributes('\\1').'>'", $source);
414 function banana_catchHtmlSignature($res)
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);
424 // {{{ Link to part catcher tools
426 function banana__linkAttachment($cid)
428 return banana_htmlentities(
429 Banana
::$page->makeUrl(Array('group' => Banana
::$group,
430 'artid' => Banana
::$artid,
436 function banana_hideExternalImages($text)
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>',
447 function banana_catchPartLinks($text)
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);
456 // {{{ HTML to Plain Text tools
458 function banana__convertFormats($res)
460 $table = array('em|i' => '/',
463 foreach ($table as $tags=>$format) {
464 $res = preg_replace("!</?($tags)( .*?)?>!is", $format, $res);
469 function banana__convertQuotes($res)
471 return preg_replace('!<blockquote.*?>([^<]*)</blockquote>!ies',
472 "\"\n\" . banana_quote(banana__convertQuotes('\\1' . \"\n\"), 1, '>')",
478 function banana_htmlToPlainText($res)
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);
488 function banana_formatHtml(BananaMimePart
&$part)
490 $text = $part->getText();
491 $text = banana_catchHtmlSignature($text);
492 if (!Banana
::$msgshow_externalimages) {
493 $text = banana_hideExternalImages($text);
495 $text = banana_catchPartLinks($text);
496 return banana_cleanHtml($text, true
);
499 function banana_quoteHtml(BananaMimePart
&$part)
501 $text = $part->getText();
502 $text = banana_htmlToPlainText($text);
503 return banana_quote($text, 1);
507 // {{{ Richtext Functions
509 /** Convert richtext to html
511 function banana_richtextToHtml($source)
513 $tags = Array('bold' => 'b',
515 'smaller' => 'small',
518 'subscript' => 'sub',
519 'superscript' => 'sup',
520 'excerpt' => 'blockquote',
525 // clean unsupported tags
526 $protectedTags = '<signature><lt><comment><'.join('><', array_keys($tags)).'>';
527 $source = strip_tags($source, $protectedTags);
529 // convert richtext tags to html
530 foreach (array_keys($tags) as $tag) {
531 $source = preg_replace('@(</?)'.$tag.'([^>]*>)@i', '\1'.$tags[$tag].'\2', $source);
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', '<', $source);
538 $source = preg_replace('@<comment[^>]*>((?:[^<]|<(?!/comment>))*)</comment>@i', '<!-- \1 -->', $source);
539 return banana_cleanHtml($source);
542 function banana_formatRichText(BananaMimePart
&$part)
544 $text = $part->getText();
545 $text = banana_richtextToHtml($text);
546 $text = banana_catchHtmlSignature($text);
547 return banana_cleanHtml($text);
550 function banana_quoteRichtText(BananaMimePart
&$part)
552 $text = $part->getText();
553 $text = banana_richtextToHtml($text);
554 $text = banana_htmlToPlainText($text);
555 return banana_quote($text, 1);
560 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: