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 foreach ($lines as &$line) {
43 if ($level > 0 && substr($line, 0, strlen($mark)) != $mark) {
46 for ($i = 0 ; $i < $level ; $i++
) {
47 $line = $mark . $line;
50 return implode("\n", $lines);
53 function banana_unflowed($text)
55 $lines = explode("\n", $text);
57 while (!is_null($line = array_shift($lines))) {
59 $line = banana_removeQuotes($line, $level);
60 while (banana_isFlowed($line)) {
62 if (is_null($nl = array_shift($lines))) {
65 $nl = banana_removeQuotes($nl, $lvl);
68 $text .= banana_quote($line, $level) . "\n";
73 function banana_wordwrap($text, $quote_level)
75 if ($quote_level > 0) {
76 $length = Banana
::$wrap - $quote_level - 1;
77 return banana_quote(wordwrap($text, $length), $quote_level);
80 return wordwrap($text, Banana
::$wrap);
83 function banana_catchFormats($text)
85 $formatting = Array('/' => 'em', // match / first in order not to match closing markups </...> <> </>
88 $url = Banana
::$url_regexp;
89 preg_match_all("/$url/i", $text, $urls);
90 $text = str_replace($urls[0], "&&&urls&&&", $text);
91 foreach ($formatting as $limit=>$mark) {
92 $limit = preg_quote($limit, '/');
93 $text = preg_replace("/$limit\\b(.*?)\\b$limit/s",
94 "<$mark>\\1</$mark>", $text);
96 return preg_replace('/&&&urls&&&/e', 'array_shift($urls[0])', $text);
99 // {{{ URL Catcher tools
101 function banana__cutlink($link)
103 $link = banana_html_entity_decode($link, ENT_QUOTES
);
104 if (strlen($link) > Banana
::$wrap) {
105 $link = substr($link, 0, Banana
::$wrap - 3) . "...";
107 return banana_htmlentities($link, ENT_QUOTES
);
110 function banana__cleanURL($url)
112 $url = str_replace('@', '%40', $url);
113 if (strpos($url, '://') === false
) {
114 $url = 'http://' . $url;
116 return '<a href="'.$url.'" title="'.$url.'">' . banana__cutlink($url) . '</a>';
119 function banana__catchMailLink($email)
121 $mid = '<' . $email . '>';
122 if (isset(Banana
::$spool->ids
[$mid])) {
123 return Banana
::$page->makeLink(Array('group' => Banana
::$group,
124 'artid' => Banana
::$spool->ids
[$mid],
126 } elseif (strpos($email, '$') !== false
) {
129 return '<a href="mailto:' . $email . '">' . $email . '</a>';
134 function banana_catchURLs($text)
136 $url = Banana
::$url_regexp;
138 $res = preg_replace("/&(lt|gt|quot);/", " &\\1; ", $text);
139 $res = preg_replace("/$url/ie", "'\\1'.banana__cleanurl('\\2').'\\3'", $res);
140 $res = preg_replace('/(["\[])?(?:mailto:|news:)?([a-z0-9.\-+_\$]+@([\-.+_]?[a-z0-9])+)(["\]])?/ie',
141 "'\\1' . banana__catchMailLink('\\2') . '\\4'",
143 $res = preg_replace("/ &(lt|gt|quot); /", "&\\1;", $res);
147 // {{{ Quotes catcher functions
149 function banana__replaceQuotes($text, $regexp)
151 return stripslashes(preg_replace("@(^|<pre>|\n)$regexp@i", '\1', $text));
156 function banana_catchQuotes($res, $strict = true
)
163 while (preg_match("/(^|<pre>|\n)$regexp/i", $res)) {
164 $res = preg_replace("/(^|<pre>|\n)(($regexp.*(?:\n|$))+)/ie",
165 "'\\1</pre><blockquote><pre>'"
166 ." . banana__replaceQuotes('\\2', '$regexp')"
167 ." . '</pre></blockquote><pre>'",
173 function banana_catchSignature($res)
175 $res = preg_replace("@<pre>-- ?\n@", "<pre>\n-- \n", $res);
176 $parts = preg_split("/\n-- ?\n/", $res);
177 $sign = '</pre><hr style="width: 100%; margin: 1em 0em; " /><pre>';
178 return join($sign, $parts);
181 function banana_plainTextToHtml($text, $strict = true
)
183 $text = banana_htmlentities($text);
184 $text = banana_catchFormats($text);
185 $text = banana_catchURLs($text);
186 $text = banana_catchQuotes($text, $strict);
187 $text = banana_catchSignature($text);
188 return banana_cleanHtml('<pre>' . $text . '</pre>');
191 function banana_wrap($text, $base_level = 0, $strict = true
)
193 $lines = explode("\n", $text);
197 while (!is_null($line = array_shift($lines))) {
199 $line = banana_removeQuotes($line, $lvl, $strict);
200 if($lvl != $level && !empty($buffer)) {
201 $text .= banana_wordwrap(implode("\n", $buffer), $level +
$base_level) . "\n";
207 if (!empty($buffer)) {
208 $text .= banana_wordwrap(implode("\n", $buffer), $level +
$base_level);
213 function banana_formatPlainText(BananaMimePart
&$part, $base_level = 0)
215 $text = $part->getText();
216 if ($part->isFlowed()) {
217 $text = banana_unflowed($text);
219 $text = banana_wrap($text, $base_level, $part->isFlowed());
220 return banana_plainTextToHtml($text, $part->isFlowed());
223 function banana_quotePlainText(BananaMimePart
&$part)
225 $text = $part->getText();
226 if ($part->isFlowed()) {
227 $text = banana_unflowed($text);
229 return banana_wrap($text, 1);
233 // {{{ HTML Functions
235 function banana_htmlentities($text, $quote = ENT_COMPAT
)
237 return htmlentities($text, $quote, 'UTF-8');
240 function banana_html_entity_decode($text, $quote = ENT_COMPAT
)
242 return html_entity_decode($text, $quote, 'UTF-8');
245 function banana_removeEvilAttributes($tagSource)
247 $stripAttrib = 'javascript:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|'.
248 'onmousemove|onmouseout|onkeypress|onkeydown|onkeyup';
249 return stripslashes(preg_replace("/$stripAttrib/i", '', $tagSource));
255 * @desc Strip forbidden tags and delegate tag-source check to removeEvilAttributes()
257 function banana_cleanHtml($source)
259 $allowedTags = '<h1><b><i><a><ul><li><pre><hr><blockquote><img><br><font><div>'
260 . '<p><small><big><sup><sub><code><em><strong><table><tr><td><th>';
261 $source = strip_tags($source, $allowedTags);
262 $source = preg_replace('/<(.*?)>/ie', "'<'.banana_removeEvilAttributes('\\1').'>'", $source);
264 if (function_exists('tidy_repair_string')) {
266 'drop-empty-paras', 'drop-proprietary-attributes',
267 'hide-comments', 'logical-emphasis', 'output-xhtml',
268 'replace-color', 'show-body-only'
270 $tidy_off = Array('join-classes', 'clean'); // 'clean' may be a good idea, but it is too aggressive
272 foreach($tidy_on as $opt) {
273 tidy_setopt($opt, true
);
275 foreach($tidy_off as $opt) {
276 tidy_setopt($opt, false
);
278 tidy_setopt('alt-text', '[ inserted by TIDY ]');
279 tidy_setopt('wrap', '120');
280 tidy_set_encoding('utf8');
281 return tidy_repair_string($source);
286 function banana_catchHtmlSignature($res)
288 $res = preg_replace("@(</p>)\n?-- ?\n?(<p[^>]*>|<br[^>]*>)@", "\\1<br/>-- \\2", $res);
289 $res = preg_replace("@<br[^>]*>\n?-- ?\n?(<p[^>]*>)@", "<br/>-- <br/>\\2", $res);
290 $res = preg_replace("@(<pre[^>]*>)\n?-- ?\n@", "<br/>-- <br/>\\1", $res);
291 $parts = preg_split("@(:?<p[^>]*>\n?-- ?\n?</p>|<br[^>]*>\n?-- ?\n?<br[^>]*>)@", $res);
292 $sign = '<hr style="width: 100%; margin: 1em 0em; " />';
293 return join($sign, $parts);
296 // {{{ Link to part catcher tools
298 function banana__linkAttachment($cid)
300 return banana_htmlentities(
301 Banana
::$page->makeUrl(Array('group' => Banana
::$group,
302 'artid' => Banana
::$artid,
308 function banana_hideExternalImages($text)
310 return preg_replace("/<img[^>]*?src=['\"](?!cid).*?>/i",
311 Banana
::$page->makeImg(array('img' => 'invalid')),
315 function banana_catchPartLinks($text)
317 return preg_replace('/cid:([^\'" ]+)/e', "banana__linkAttachment('\\1')", $text);
320 // {{{ HTML to Plain Text tools
322 function banana__convertFormats($res)
324 $table = array('em|i' => '/',
327 foreach ($table as $tags=>$format) {
328 $res = preg_replace("!</?($tags)( .*?)?>!is", $format, $res);
333 function banana__convertQuotes($res)
335 return preg_replace('!<blockquote.*?>([^<]*)</blockquote>!ies',
336 "\"\n\" . banana_quote(banana__convertQuotes('\\1' . \"\n\"), 1, '>')",
342 function banana_htmlToPlainText($res)
344 $res = str_replace("\n", '', $res);
345 $res = banana__convertFormats($res);
346 $res = trim(strip_tags($res, '<div><br><p><blockquote>'));
347 $res = preg_replace("@</?(br|p|div).*?>@si", "\n", $res);
348 $res = banana__convertQuotes($res);
349 return banana_html_entity_decode($res);
352 function banana_formatHtml(BananaMimePart
&$part)
354 $text = $part->getText();
355 $text = banana_catchHtmlSignature($text);
356 $text = banana_hideExternalImages($text);
357 $text = banana_catchPartLinks($text);
358 return banana_cleanHtml($text);
361 function banana_quoteHtml(BananaMimePart
&$part)
363 $text = $part->getText();
364 $text = banana_htmlToPlainText($text);
365 return banana_wrap($text, 1);
369 // {{{ Richtext Functions
371 /** Convert richtext to html
373 function banana_richtextToHtml($source)
375 $tags = Array('bold' => 'b',
377 'smaller' => 'small',
380 'subscript' => 'sub',
381 'superscript' => 'sup',
382 'excerpt' => 'blockquote',
387 // clean unsupported tags
388 $protectedTags = '<signature><lt><comment><'.join('><', array_keys($tags)).'>';
389 $source = strip_tags($source, $protectedTags);
391 // convert richtext tags to html
392 foreach (array_keys($tags) as $tag) {
393 $source = preg_replace('@(</?)'.$tag.'([^>]*>)@i', '\1'.$tags[$tag].'\2', $source);
396 // some special cases
397 $source = preg_replace('@<signature>@i', '<br>-- <br>', $source);
398 $source = preg_replace('@</signature>@i', '', $source);
399 $source = preg_replace('@<lt>@i', '<', $source);
400 $source = preg_replace('@<comment[^>]*>((?:[^<]|<(?!/comment>))*)</comment>@i', '<!-- \1 -->', $source);
401 return banana_cleanHtml($source);
404 function banana_formatRichText(BananaMimePart
&$part)
406 $text = $part->getText();
407 $text = banana_richtextToHtml($text);
408 $text = banana_catchHtmlSignature($text);
409 return banana_cleanHtml($text);
414 // vim:set et sw=4 sts=4 ts=4: