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 != '-- ') {
108 $text .= rtrim(str_replace("\n", " \n", banana_wordwrap($line))) . "\n";
110 $text .= $line . "\n";
116 // {{{ URL Catcher tools
118 function banana__cutlink($link)
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) . "...";
124 return banana_htmlentities($link, ENT_QUOTES
);
127 function banana__cleanURL($url)
129 $url = str_replace('@', '%40', $url);
130 if (strpos($url, '://') === false
) {
131 $url = 'http://' . $url;
133 return '<a href="'.$url.'" title="'.$url.'">' . banana__cutlink($url) . '</a>';
136 function banana__catchMailLink($email)
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],
143 } elseif (strpos($email, '$') !== false
) {
146 return '<a href="mailto:' . $email . '">' . $email . '</a>';
151 function banana_catchURLs($text)
153 $url = Banana
::$msgshow_url;
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'",
160 $res = preg_replace("/ &(lt|gt|quot); /", "&\\1;", $res);
164 // {{{ Quotes catcher functions
166 function banana__replaceQuotes($text, $regexp)
168 return stripslashes(preg_replace("@(^|<pre>|\n)$regexp@i", '\1', $text));
173 function banana_catchQuotes($res, $strict = true
)
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>'",
190 function banana_catchSignature($res)
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);
198 function banana_plainTextToHtml($text, $strict = true
)
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>');
208 function banana_wrap($text, $base_level = 0, $strict = true
)
210 $lines = explode("\n", $text);
214 while (!is_null($line = array_shift($lines))) {
216 $line = banana_removeQuotes($line, $lvl, $strict);
218 if (!empty($buffer)) {
219 $text .= banana_wordwrap(implode("\n", $buffer), $level +
$base_level) . "\n";
226 if (!empty($buffer)) {
227 $text .= banana_wordwrap(implode("\n", $buffer), $level +
$base_level);
232 function banana_formatPlainText(BananaMimePart
&$part, $base_level = 0)
234 $text = $part->getText();
235 if ($part->isFlowed()) {
236 $text = banana_unflowed($text);
238 $text = banana_wrap($text, $base_level, $part->isFlowed());
239 return banana_plainTextToHtml($text, $part->isFlowed());
242 function banana_quotePlainText(BananaMimePart
&$part)
244 $text = $part->getText();
245 if ($part->isFlowed()) {
246 $text = banana_unflowed($text);
248 return banana_wrap($text, 1);
252 // {{{ HTML Functions
254 function banana_htmlentities($text, $quote = ENT_COMPAT
)
256 return htmlentities($text, $quote, 'UTF-8');
259 function banana_html_entity_decode($text, $quote = ENT_COMPAT
)
261 return html_entity_decode($text, $quote, 'UTF-8');
264 function banana_removeEvilAttributes($tagSource)
266 $stripAttrib = 'javascript:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|'.
267 'onmousemove|onmouseout|onkeypress|onkeydown|onkeyup';
268 return stripslashes(preg_replace("/$stripAttrib/i", '', $tagSource));
271 function banana_cleanStyles($tag, $attributes)
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');
285 if ($tag == 'table') {
286 array_unshift($td_style, '');
288 if ($tag == '/table') {
289 array_shift($td_style);
291 if ($tag{0} == '/') {
295 $style = $td_style[0];
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);
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, ' ;');
313 } elseif ($att == 'size') {
314 $val = $size_conv[$val];
315 $style .= "$stl: $val; ";
316 } elseif (is_numeric($val)) {
317 $style .= "$stl: {$val}px; ";
319 $style .= "$stl: $val; ";
323 if (!empty($style)) {
324 $style = 'style="' . $style . '" ';
326 return ' ' . $style . trim($attributes);
329 function banana__filterCss($text)
331 $text = preg_replace("/(,[\s\n\r]*)/s", '\1 .banana .message .body .html ', $text);
332 return '.banana .message .body .html ' . $text;
335 function banana_filterCss($css)
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
;
352 * @desc Strip forbidden tags and delegate tag-source check to removeEvilAttributes()
354 function banana_cleanHtml($source, $to_xhtml = false
)
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
,
365 'show-body-only' => false
,
366 'alt-text' => '[ inserted by TIDY ]',
368 if (function_exists('tidy_setopt')) { // Tidy 1.0
369 foreach ($tidy_config as $field=>$value) {
370 tidy_setopt($field, $value);
372 tidy_set_encoding('utf8');
373 $source = tidy_repair_string($source);
375 $source = tidy_repair_string($source, $tidy_config, 'utf8');
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) {
389 $css = banana_filterCss($css);
390 Banana
::$page->addCssInline($css);
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);
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);
404 // Use inlined style instead of old html attributes
406 $source = preg_replace('/<(\/?\w+)(.*?)(\/?>)/uise', "'<\\1' . banana_cleanStyles('\\1', '\\2') . '\\3'", $source);
408 return preg_replace('/<(.*?)>/ie', "'<'.banana_removeEvilAttributes('\\1').'>'", $source);
411 function banana_catchHtmlSignature($res)
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);
421 // {{{ Link to part catcher tools
423 function banana__linkAttachment($cid)
425 return banana_htmlentities(
426 Banana
::$page->makeUrl(Array('group' => Banana
::$group,
427 'artid' => Banana
::$artid,
433 function banana_hideExternalImages($text)
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>',
444 function banana_catchPartLinks($text)
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);
453 // {{{ HTML to Plain Text tools
455 function banana__convertFormats($res)
457 $table = array('em|i' => '/',
460 foreach ($table as $tags=>$format) {
461 $res = preg_replace("!</?($tags)( .*?)?>!is", $format, $res);
466 function banana__convertQuotes($res)
468 return preg_replace('!<blockquote.*?>([^<]*)</blockquote>!ies',
469 "\"\n\" . banana_quote(banana__convertQuotes('\\1' . \"\n\"), 1, '>')",
475 function banana_htmlToPlainText($res)
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);
485 function banana_formatHtml(BananaMimePart
&$part)
487 $text = $part->getText();
488 $text = banana_catchHtmlSignature($text);
489 if (!Banana
::$msgshow_externalimages) {
490 $text = banana_hideExternalImages($text);
492 $text = banana_catchPartLinks($text);
493 return banana_cleanHtml($text, true
);
496 function banana_quoteHtml(BananaMimePart
&$part)
498 $text = $part->getText();
499 $text = banana_htmlToPlainText($text);
500 return banana_wrap($text, 1);
504 // {{{ Richtext Functions
506 /** Convert richtext to html
508 function banana_richtextToHtml($source)
510 $tags = Array('bold' => 'b',
512 'smaller' => 'small',
515 'subscript' => 'sub',
516 'superscript' => 'sup',
517 'excerpt' => 'blockquote',
522 // clean unsupported tags
523 $protectedTags = '<signature><lt><comment><'.join('><', array_keys($tags)).'>';
524 $source = strip_tags($source, $protectedTags);
526 // convert richtext tags to html
527 foreach (array_keys($tags) as $tag) {
528 $source = preg_replace('@(</?)'.$tag.'([^>]*>)@i', '\1'.$tags[$tag].'\2', $source);
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', '<', $source);
535 $source = preg_replace('@<comment[^>]*>((?:[^<]|<(?!/comment>))*)</comment>@i', '<!-- \1 -->', $source);
536 return banana_cleanHtml($source);
539 function banana_formatRichText(BananaMimePart
&$part)
541 $text = $part->getText();
542 $text = banana_richtextToHtml($text);
543 $text = banana_catchHtmlSignature($text);
544 return banana_cleanHtml($text);
547 function banana_quoteRichtText(BananaMimePart
&$part)
549 $text = $part->getText();
550 $text = banana_richtextToHtml($text);
551 $text = banana_htmlToPlainText($text);
552 return banana_wrap($text, 1);
557 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: