Rework all sources :
[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 foreach ($lines as &$line) {
43 if ($level > 0 && substr($line, 0, strlen($mark)) != $mark) {
44 $line = ' ' . $line;
45 }
46 for ($i = 0 ; $i < $level ; $i++) {
47 $line = $mark . $line;
48 }
49 }
50 return implode("\n", $lines);
51 }
52
53 function banana_unflowed($text)
54 {
55 $lines = explode("\n", $text);
56 $text = '';
57 while (!is_null($line = array_shift($lines))) {
58 $level = 0;
59 $line = banana_removeQuotes($line, $level);
60 while (banana_isFlowed($line)) {
61 $lvl = 0;
62 if (is_null($nl = array_shift($lines))) {
63 break;
64 }
65 $nl = banana_removeQuotes($nl, $lvl);
66 $line .= $nl;
67 }
68 $text .= banana_quote($line, $level) . "\n";
69 }
70 return $text;
71 }
72
73 function banana_wordwrap($text, $quote_level)
74 {
75 if ($quote_level > 0) {
76 $length = Banana::$wrap - $quote_level - 1;
77 return banana_quote(wordwrap($text, $length), $quote_level);
78
79 }
80 return wordwrap($text, Banana::$wrap);
81 }
82
83 function banana_catchFormats($text)
84 {
85 $formatting = Array('/' => 'em', // match / first in order not to match closing markups </...> <> </>
86 '_' => 'u',
87 '*' => 'strong');
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);
95 }
96 return preg_replace('/&&&urls&&&/e', 'array_shift($urls[0])', $text);
97 }
98
99 // {{{ URL Catcher tools
100
101 function banana__cutlink($link)
102 {
103 $link = banana_html_entity_decode($link, ENT_QUOTES);
104 if (strlen($link) > Banana::$wrap) {
105 $link = substr($link, 0, Banana::$wrap - 3) . "...";
106 }
107 return banana_htmlentities($link, ENT_QUOTES);
108 }
109
110 function banana__cleanURL($url)
111 {
112 $url = str_replace('@', '%40', $url);
113 if (strpos($url, '://') === false) {
114 $url = 'http://' . $url;
115 }
116 return '<a href="'.$url.'" title="'.$url.'">' . banana__cutlink($url) . '</a>';
117 }
118
119 function banana__catchMailLink($email)
120 {
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],
125 'text' => $email));
126 } elseif (strpos($email, '$') !== false) {
127 return $email;
128 }
129 return '<a href="mailto:' . $email . '">' . $email . '</a>';
130 }
131
132 // }}}
133
134 function banana_catchURLs($text)
135 {
136 $url = Banana::$url_regexp;
137
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'",
142 $res);
143 $res = preg_replace("/ &(lt|gt|quot); /", "&\\1;", $res);
144 return $res;
145 }
146
147 // {{{ Quotes catcher functions
148
149 function banana__replaceQuotes($text, $regexp)
150 {
151 return stripslashes(preg_replace("@(^|<pre>|\n)$regexp@i", '\1', $text));
152 }
153
154 // }}}
155
156 function banana_catchQuotes($res, $strict = true)
157 {
158 if ($strict) {
159 $regexp = "&gt;";
160 } else {
161 $regexp = "&gt; *";
162 }
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>'",
168 $res);
169 }
170 return $res;
171 }
172
173 function banana_catchSignature($res)
174 {
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);
179 }
180
181 function banana_plainTextToHtml($text, $strict = true)
182 {
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>');
189 }
190
191 function banana_wrap($text, $base_level = 0, $strict = true)
192 {
193 $lines = explode("\n", $text);
194 $text = '';
195 $buffer = array();
196 $level = 0;
197 while (!is_null($line = array_shift($lines))) {
198 $lvl = 0;
199 $line = banana_removeQuotes($line, $lvl, $strict);
200 if($lvl != $level && !empty($buffer)) {
201 $text .= banana_wordwrap(implode("\n", $buffer), $level + $base_level) . "\n";
202 $level = $lvl;
203 $buffer = array();
204 }
205 $buffer[] = $line;
206 }
207 if (!empty($buffer)) {
208 $text .= banana_wordwrap(implode("\n", $buffer), $level + $base_level);
209 }
210 return $text;
211 }
212
213 function banana_formatPlainText(BananaMimePart &$part, $base_level = 0)
214 {
215 $text = $part->getText();
216 if ($part->isFlowed()) {
217 $text = banana_unflowed($text);
218 }
219 $text = banana_wrap($text, $base_level, $part->isFlowed());
220 return banana_plainTextToHtml($text, $part->isFlowed());
221 }
222
223 function banana_quotePlainText(BananaMimePart &$part)
224 {
225 $text = $part->getText();
226 if ($part->isFlowed()) {
227 $text = banana_unflowed($text);
228 }
229 return banana_wrap($text, 1);
230 }
231
232 // }}}
233 // {{{ HTML Functions
234
235 function banana_htmlentities($text, $quote = ENT_COMPAT)
236 {
237 return htmlentities($text, $quote, 'UTF-8');
238 }
239
240 function banana_html_entity_decode($text, $quote = ENT_COMPAT)
241 {
242 return html_entity_decode($text, $quote, 'UTF-8');
243 }
244
245 function banana_removeEvilAttributes($tagSource)
246 {
247 $stripAttrib = 'javascript:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|'.
248 'onmousemove|onmouseout|onkeypress|onkeydown|onkeyup';
249 return stripslashes(preg_replace("/$stripAttrib/i", '', $tagSource));
250 }
251
252 /**
253 * @return string
254 * @param string
255 * @desc Strip forbidden tags and delegate tag-source check to removeEvilAttributes()
256 */
257 function banana_cleanHtml($source)
258 {
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);
263
264 if (function_exists('tidy_repair_string')) {
265 $tidy_on = Array(
266 'drop-empty-paras', 'drop-proprietary-attributes',
267 'hide-comments', 'logical-emphasis', 'output-xhtml',
268 'replace-color', 'show-body-only'
269 );
270 $tidy_off = Array('join-classes', 'clean'); // 'clean' may be a good idea, but it is too aggressive
271
272 foreach($tidy_on as $opt) {
273 tidy_setopt($opt, true);
274 }
275 foreach($tidy_off as $opt) {
276 tidy_setopt($opt, false);
277 }
278 tidy_setopt('alt-text', '[ inserted by TIDY ]');
279 tidy_setopt('wrap', '120');
280 tidy_set_encoding('utf8');
281 return tidy_repair_string($source);
282 }
283 return $source;
284 }
285
286 function banana_catchHtmlSignature($res)
287 {
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);
294 }
295
296 // {{{ Link to part catcher tools
297
298 function banana__linkAttachment($cid)
299 {
300 return banana_htmlentities(
301 Banana::$page->makeUrl(Array('group' => Banana::$group,
302 'artid' => Banana::$artid,
303 'part' => $cid)));
304 }
305
306 // }}}
307
308 function banana_hideExternalImages($text)
309 {
310 return preg_replace("/<img[^>]*?src=['\"](?!cid).*?>/i",
311 Banana::$page->makeImg(array('img' => 'invalid')),
312 $text);
313 }
314
315 function banana_catchPartLinks($text)
316 {
317 return preg_replace('/cid:([^\'" ]+)/e', "banana__linkAttachment('\\1')", $text);
318 }
319
320 // {{{ HTML to Plain Text tools
321
322 function banana__convertFormats($res)
323 {
324 $table = array('em|i' => '/',
325 'strong|b' => '*',
326 'u' => '_');
327 foreach ($table as $tags=>$format) {
328 $res = preg_replace("!</?($tags)( .*?)?>!is", $format, $res);
329 }
330 return $res;
331 }
332
333 function banana__convertQuotes($res)
334 {
335 return preg_replace('!<blockquote.*?>([^<]*)</blockquote>!ies',
336 "\"\n\" . banana_quote(banana__convertQuotes('\\1' . \"\n\"), 1, '&gt;')",
337 $res);
338 }
339
340 // }}}
341
342 function banana_htmlToPlainText($res)
343 {
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);
350 }
351
352 function banana_formatHtml(BananaMimePart &$part)
353 {
354 $text = $part->getText();
355 $text = banana_catchHtmlSignature($text);
356 $text = banana_hideExternalImages($text);
357 $text = banana_catchPartLinks($text);
358 return banana_cleanHtml($text);
359 }
360
361 function banana_quoteHtml(BananaMimePart &$part)
362 {
363 $text = $part->getText();
364 $text = banana_htmlToPlainText($text);
365 return banana_wrap($text, 1);
366 }
367
368 // }}}
369 // {{{ Richtext Functions
370
371 /** Convert richtext to html
372 */
373 function banana_richtextToHtml($source)
374 {
375 $tags = Array('bold' => 'b',
376 'italic' => 'i',
377 'smaller' => 'small',
378 'bigger' => 'big',
379 'underline' => 'u',
380 'subscript' => 'sub',
381 'superscript' => 'sup',
382 'excerpt' => 'blockquote',
383 'paragraph' => 'p',
384 'nl' => 'br'
385 );
386
387 // clean unsupported tags
388 $protectedTags = '<signature><lt><comment><'.join('><', array_keys($tags)).'>';
389 $source = strip_tags($source, $protectedTags);
390
391 // convert richtext tags to html
392 foreach (array_keys($tags) as $tag) {
393 $source = preg_replace('@(</?)'.$tag.'([^>]*>)@i', '\1'.$tags[$tag].'\2', $source);
394 }
395
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', '&lt;', $source);
400 $source = preg_replace('@<comment[^>]*>((?:[^<]|<(?!/comment>))*)</comment>@i', '<!-- \1 -->', $source);
401 return banana_cleanHtml($source);
402 }
403
404 function banana_formatRichText(BananaMimePart &$part)
405 {
406 $text = $part->getText();
407 $text = banana_richtextToHtml($text);
408 $text = banana_catchHtmlSignature($text);
409 return banana_cleanHtml($text);
410 }
411
412 // }}}
413
414 // vim:set et sw=4 sts=4 ts=4:
415 ?>