Commit | Line | Data |
---|---|---|
3ee590a9 | 1 | <?php |
2 | /******************************************************************************** | |
73d5bf46 | 3 | * include/misc.inc.php : Misc functions |
4 | * ------------------------- | |
5 | * | |
6 | * This file is part of the banana distribution | |
7 | * Copyright: See COPYING files that comes with this distribution | |
8 | ********************************************************************************/ | |
9 | ||
10 | /******************************************************************************** | |
2dbc0167 | 11 | * MISC |
73d5bf46 | 12 | */ |
13 | ||
0ca6e016 | 14 | function _b_($str) { return utf8_decode(dgettext('banana', utf8_encode($str))); } |
0a65ec9d | 15 | |
987299b4 | 16 | function to_entities($str) { |
6918d66a | 17 | require_once dirname(__FILE__).'/utf8.php'; |
987299b4 | 18 | return utf8entities(htmlentities($str, ENT_NOQUOTES, 'UTF-8')); |
382606fb PHM |
19 | } |
20 | ||
c42efe2f PHM |
21 | function is_utf8($s) { return iconv('utf-8', 'utf-8', $s) == $s; } |
22 | ||
8f6f50fb | 23 | function textFormat_translate($format) |
24 | { | |
25 | switch (strtolower($format)) { | |
26 | case 'plain': return _b_('Texte brut'); | |
27 | case 'richtext': return _b_('Texte enrichi'); | |
28 | case 'html': return _b_('HTML'); | |
29 | default: return $format; | |
30 | } | |
31 | } | |
32 | ||
0eb1e7ef | 33 | /** Redirect to the page with the given parameter |
34 | * @ref makeLink | |
35 | */ | |
e8de34ae | 36 | function redirectInBanana($params) |
9cac5c87 | 37 | { |
d5588318 | 38 | header('Location: ' . makeLink($params)); |
9cac5c87 | 39 | } |
40 | ||
0eb1e7ef | 41 | /** Make a link using the given parameters |
42 | * @param ARRAY params, the parameters with | |
43 | * key => value | |
44 | * Known key are: | |
45 | * - group = group name | |
46 | * - artid/first = article id the the group | |
47 | * - subscribe = to show the subscription page | |
48 | * - action = action to do (new, cancel, view) | |
49 | * - part = to show the given MIME part of the article | |
50 | * - pj = to get the given attachment | |
261210bc | 51 | * - xface = to make a link to an xface |
0eb1e7ef | 52 | * |
53 | * Can be overloaded by defining a hook_makeLink function | |
54 | */ | |
9cac5c87 | 55 | function makeLink($params) |
56 | { | |
d5588318 | 57 | if (function_exists('hook_makeLink') |
58 | && $res = hook_makeLink($params)) { | |
59 | return $res; | |
60 | } | |
0eb1e7ef | 61 | $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://'; |
9cac5c87 | 62 | $host = $_SERVER['HTTP_HOST']; |
63 | $file = $_SERVER['PHP_SELF']; | |
64 | ||
261210bc | 65 | if (isset($params['xface'])) { |
66 | $file = dirname($file) . '/xface.php'; | |
8948d347 | 67 | $get = 'face=' . urlencode(base64_encode($params['xface'])); |
261210bc | 68 | } else if (count($params) != 0) { |
d5588318 | 69 | $get = '?'; |
70 | foreach ($params as $key=>$value) { | |
71 | if (strlen($get) != 1) { | |
72 | $get .= '&'; | |
73 | } | |
74 | $get .= $key . '=' . $value; | |
75 | } | |
76 | } else { | |
77 | $get = ''; | |
78 | } | |
79 | ||
80 | return $proto . $host . $file . $get; | |
0eb1e7ef | 81 | } |
82 | ||
83 | /** Format a link to be use in a link | |
84 | * @ref makeLink | |
85 | */ | |
d8e2470c | 86 | function makeHREF($params, $text = null, $popup = null, $class = null) |
0eb1e7ef | 87 | { |
d5588318 | 88 | $link = makeLink($params); |
89 | if (is_null($text)) { | |
90 | $text = $link; | |
91 | } | |
937a36cf | 92 | if (!is_null($popup)) { |
93 | $popup = ' title="' . $popup . '"'; | |
94 | } | |
d8e2470c | 95 | if (!is_null($class)) { |
96 | $class = ' class="' . $class . '"'; | |
97 | } | |
d5588318 | 98 | $target = null; |
99 | if (isset($params['action']) && $params['action'] == 'view') { | |
100 | $target = ' target="_blank"'; | |
101 | } | |
ec39d93e | 102 | return '<a href="' . htmlentities($link) .'"' . $target . $popup . $class . '>' . $text . '</a>'; |
9cac5c87 | 103 | } |
104 | ||
d7632010 | 105 | /** Format tree images links |
106 | * @param img STRING Image name (without extension) | |
107 | * @param alt STRING alternative string | |
108 | * @param width INT to force width of the image (null if not defined) | |
109 | * | |
110 | * This function can be overloaded by defining hook_makeImg() | |
111 | */ | |
4ccaaeb7 | 112 | function makeImg($img, $alt, $height = null, $width = null) |
d7632010 | 113 | { |
114 | if (function_exists('hook_makeImg') | |
9419816d | 115 | && $res = hook_makeImg($img, $alt, $height, $width)) { |
d7632010 | 116 | return $res; |
117 | } | |
118 | ||
119 | if (!is_null($width)) { | |
120 | $width = ' width="' . $width . '"'; | |
121 | } | |
4ccaaeb7 | 122 | if (!is_null($height)) { |
123 | $height = ' height="' . $height . '"'; | |
124 | } | |
125 | ||
d7632010 | 126 | $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://'; |
127 | $host = $_SERVER['HTTP_HOST']; | |
d8e2470c | 128 | $file = dirname($_SERVER['PHP_SELF']) . '/img/' . $img; |
d7632010 | 129 | $url = $proto . $host . $file; |
130 | ||
4ccaaeb7 | 131 | return '<img src="' . $url . '"' . $height . $width . ' alt="' . $alt . '" />'; |
d7632010 | 132 | } |
133 | ||
d8e2470c | 134 | /** Make a link using an image |
135 | */ | |
136 | function makeImgLink($params, $img, $alt, $height = null, $width = null, $class = null) | |
137 | { | |
138 | return makeHREF($params, | |
139 | makeImg($img, ' [' . $alt . ']', $height, $width), | |
140 | $alt, | |
141 | $class); | |
142 | } | |
143 | ||
8f6f50fb | 144 | /******************************************************************************** |
145 | * HTML STUFF | |
146 | * Taken from php.net | |
147 | */ | |
148 | ||
5d133234 | 149 | /** |
8f6f50fb | 150 | * @return string |
151 | * @param string | |
152 | * @desc Strip forbidden tags and delegate tag-source check to removeEvilAttributes() | |
153 | */ | |
154 | function removeEvilTags($source) | |
155 | { | |
73ab762c | 156 | $allowedTags = '<h1><b><i><a><ul><li><pre><hr><blockquote><img><br><font><p><small><big><sup><sub><code><em>'; |
76032c26 | 157 | $source = preg_replace('|</div>|i', '<br />', $source); |
8f6f50fb | 158 | $source = strip_tags($source, $allowedTags); |
159 | return preg_replace('/<(.*?)>/ie', "'<'.removeEvilAttributes('\\1').'>'", $source); | |
160 | } | |
161 | ||
162 | /** | |
163 | * @return string | |
164 | * @param string | |
165 | * @desc Strip forbidden attributes from a tag | |
166 | */ | |
167 | function removeEvilAttributes($tagSource) | |
168 | { | |
169 | $stripAttrib = 'javascript:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|'. | |
170 | 'onmousemove|onmouseout|onkeypress|onkeydown|onkeyup'; | |
171 | return stripslashes(preg_replace("/$stripAttrib/i", '', $tagSource)); | |
172 | } | |
173 | ||
5d133234 | 174 | /** Convert html to plain text |
175 | */ | |
176 | function htmlToPlainText($res) | |
177 | { | |
76032c26 | 178 | $res = trim(html_entity_decode(strip_tags($res, '<div><br><p>'))); |
179 | $res = preg_replace("@</?(br|p|div)[^>]*>@i", "\n", $res); | |
e27ae1a3 | 180 | if (!is_utf8($res)) { |
181 | $res = utf8_encode($res); | |
182 | } | |
5d133234 | 183 | return $res; |
184 | } | |
185 | ||
1fb11000 | 186 | /** Match **, // and __ to format plain text |
187 | */ | |
188 | function formatPlainText($text) | |
189 | { | |
190 | $formatting = Array('\*' => 'strong', | |
191 | '_' => 'u', | |
192 | '/' => 'em'); | |
193 | foreach ($formatting as $limit=>$mark) { | |
194 | $text = preg_replace('@(^|\W)' . $limit . '(\w+)' . $limit . '(\W|$)@' | |
195 | ,'\1<' . $mark . '>\2</' . $mark . '>\3' | |
196 | , $text); | |
197 | } | |
198 | return $text; | |
199 | } | |
200 | ||
5d133234 | 201 | /******************************************************************************** |
202 | * RICHTEXT STUFF | |
203 | */ | |
204 | ||
205 | /** Convert richtext to html | |
206 | */ | |
207 | function richtextToHtml($source) | |
208 | { | |
209 | $tags = Array('bold' => 'b', | |
210 | 'italic' => 'i', | |
211 | 'smaller' => 'small', | |
212 | 'bigger' => 'big', | |
213 | 'underline' => 'u', | |
214 | 'subscript' => 'sub', | |
215 | 'superscript' => 'sup', | |
216 | 'excerpt' => 'blockquote', | |
217 | 'paragraph' => 'p', | |
218 | 'nl' => 'br' | |
219 | ); | |
220 | ||
221 | // clean unsupported tags | |
222 | $protectedTags = '<signature><lt><comment><'.join('><', array_keys($tags)).'>'; | |
223 | $source = strip_tags($source, $protectedTags); | |
224 | ||
225 | // convert richtext tags to html | |
226 | foreach (array_keys($tags) as $tag) { | |
227 | $source = preg_replace('@(</?)'.$tag.'([^>]*>)@i', '\1'.$tags[$tag].'\2', $source); | |
228 | } | |
229 | ||
230 | // some special cases | |
231 | $source = preg_replace('@<signature>@i', '<br>-- <br>', $source); | |
232 | $source = preg_replace('@</signature>@i', '', $source); | |
233 | $source = preg_replace('@<lt>@i', '<', $source); | |
234 | $source = preg_replace('@<comment[^>]*>((?:[^<]|<(?!/comment>))*)</comment>@i', '<!-- \1 -->', $source); | |
235 | return removeEvilAttributes($source); | |
236 | } | |
237 | ||
73d5bf46 | 238 | /******************************************************************************** |
239 | * HEADER STUFF | |
240 | */ | |
241 | ||
dd7d1c59 | 242 | function _headerdecode($charset, $c, $str) { |
67d2dcfc | 243 | $s = ($c == 'Q' || $c == 'q') ? quoted_printable_decode($str) : base64_decode($str); |
dd7d1c59 | 244 | $s = iconv($charset, 'iso-8859-15', $s); |
245 | return str_replace('_', ' ', $s); | |
246 | } | |
247 | ||
248 | function headerDecode($value) { | |
67d2dcfc | 249 | $val = preg_replace('/(=\?[^?]*\?[BQbq]\?[^?]*\?=) (=\?[^?]*\?[BQbq]\?[^?]*\?=)/', '\1\2', $value); |
250 | return preg_replace('/=\?([^?]*)\?([BQbq])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val); | |
dd7d1c59 | 251 | } |
252 | ||
9cf3f056 PHM |
253 | function headerEncode($value, $trim = 0) { |
254 | if ($trim) { | |
d752880f PHM |
255 | if (strlen($value) > $trim) { |
256 | $value = substr($value, 0, $trim) . "[...]"; | |
257 | } | |
9cf3f056 PHM |
258 | } |
259 | return "=?UTF-8?B?".base64_encode($value)."?="; | |
260 | } | |
261 | ||
e2cae7e3 | 262 | function header_translate($hdr) { |
d4c19591 | 263 | switch ($hdr) { |
0a65ec9d | 264 | case 'from': return _b_('De'); |
265 | case 'subject': return _b_('Sujet'); | |
266 | case 'newsgroups': return _b_('Forums'); | |
d4c19591 | 267 | case 'followup-to': return _b_('Suivi-à'); |
0a65ec9d | 268 | case 'date': return _b_('Date'); |
269 | case 'organization': return _b_('Organisation'); | |
270 | case 'references': return _b_('Références'); | |
d4c19591 | 271 | case 'x-face': return _b_('Image'); |
e2cae7e3 | 272 | default: |
559be3d6 | 273 | if (function_exists('hook_headerTranslate') |
274 | && $res = hook_headerTranslate($hdr)) { | |
275 | return $res; | |
2dbc0167 | 276 | } |
e2cae7e3 | 277 | return $hdr; |
278 | } | |
279 | } | |
280 | ||
2dbc0167 | 281 | function formatDisplayHeader($_header,$_text) { |
282 | global $banana; | |
1fb11000 | 283 | if (function_exists('hook_formatDisplayHeader') |
284 | && $res = hook_formatDisplayHeader($_header, $_text)) { | |
285 | return $res; | |
286 | } | |
287 | ||
2dbc0167 | 288 | switch ($_header) { |
289 | case "date": | |
290 | return formatDate($_text); | |
291 | ||
292 | case "followup-to": | |
293 | case "newsgroups": | |
294 | $res = ""; | |
295 | $groups = preg_split("/[\t ]*,[\t ]*/",$_text); | |
296 | foreach ($groups as $g) { | |
0eb1e7ef | 297 | $res .= makeHREF(Array('group' => $g), $g) . ', '; |
2dbc0167 | 298 | } |
299 | return substr($res,0, -2); | |
300 | ||
301 | case "from": | |
302 | return formatFrom($_text); | |
303 | ||
304 | case "references": | |
305 | $rsl = ""; | |
306 | $ndx = 1; | |
307 | $text = str_replace("><","> <",$_text); | |
308 | $text = preg_split("/[ \t]/",strtr($text,$banana->spool->ids)); | |
309 | $parents = preg_grep("/^\d+$/",$text); | |
310 | $p = array_pop($parents); | |
58d1740e | 311 | $par_ok = Array(); |
2dbc0167 | 312 | |
313 | while ($p) { | |
58d1740e | 314 | $par_ok[]=$p; |
2dbc0167 | 315 | $p = $banana->spool->overview[$p]->parent; |
316 | } | |
58d1740e | 317 | foreach (array_reverse($par_ok) as $p) { |
98647290 | 318 | $rsl .= makeHREF(Array('group' => $banana->spool->group, |
319 | 'artid' => $p), | |
320 | $ndx) . ' '; | |
2dbc0167 | 321 | $ndx++; |
322 | } | |
323 | return $rsl; | |
324 | ||
325 | case "x-face": | |
8948d347 | 326 | return '<img src="' . makeLink(Array('xface' => headerDecode($_text))) .'" alt="x-face" />'; |
1fb11000 | 327 | |
328 | case "subject": | |
72afc3ec | 329 | $link = null; |
330 | if (function_exists('hook_getSubject')) { | |
331 | $link = hook_getSubject($_text); | |
332 | } | |
333 | return formatPlainText($_text) . $link; | |
1fb11000 | 334 | |
2dbc0167 | 335 | default: |
2dbc0167 | 336 | return htmlentities($_text); |
337 | } | |
338 | } | |
339 | ||
73d5bf46 | 340 | /******************************************************************************** |
341 | * FORMATTING STUFF | |
342 | */ | |
343 | ||
e2cae7e3 | 344 | function formatDate($_text) { |
345 | return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text)); | |
346 | } | |
347 | ||
348 | function fancyDate($stamp) { | |
349 | $today = intval(time() / (24*3600)); | |
350 | $dday = intval($stamp / (24*3600)); | |
351 | ||
352 | if ($today == $dday) { | |
353 | $format = "%H:%M"; | |
354 | } elseif ($today == 1 + $dday) { | |
0a65ec9d | 355 | $format = _b_('hier')." %H:%M"; |
e2cae7e3 | 356 | } elseif ($today < 7 + $dday) { |
1248ebac | 357 | $format = '%a %H:%M'; |
e2cae7e3 | 358 | } else { |
359 | $format = '%a %e %b'; | |
360 | } | |
361 | return strftime($format, $stamp); | |
362 | } | |
363 | ||
dd7d1c59 | 364 | function formatFrom($text) { |
365 | # From: mark@cbosgd.ATT.COM | |
366 | # From: mark@cbosgd.ATT.COM (Mark Horton) | |
367 | # From: Mark Horton <mark@cbosgd.ATT.COM> | |
368 | $mailto = '<a href="mailto:'; | |
369 | ||
370 | $result = htmlentities($text); | |
371 | if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) { | |
372 | $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[1]."@".$regs[2])."</a>"; | |
373 | } | |
374 | if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) { | |
375 | $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[3])."</a>"; | |
376 | } | |
377 | if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) { | |
378 | $result="$mailto{$regs[2]}@{$regs[3]}\">".htmlentities($regs[1])."</a>"; | |
379 | } | |
380 | return preg_replace("/\\\(\(|\))/","\\1",$result); | |
381 | } | |
382 | ||
12ed25e1 | 383 | function makeTab($link, $text) |
384 | { | |
385 | return Array(makeHREF($link, $text), | |
386 | $text); | |
387 | } | |
388 | ||
8003ba55 | 389 | function displayTabs() |
d8e2470c | 390 | { |
41cf00eb | 391 | global $banana; |
8d99c683 | 392 | extract($banana->state); |
d8e2470c | 393 | if (function_exists('hook_shortcuts') && $cstm = hook_shortcuts()) { |
8003ba55 | 394 | $res = $cstm; |
8d99c683 | 395 | } |
8003ba55 | 396 | |
12ed25e1 | 397 | $res['subscribe'] = makeTab(Array('subscribe' => 1), _b_('Abonnements')); |
398 | $res['forums'] = makeTab(Array(), _b_('Les forums')); | |
d8e2470c | 399 | |
400 | if (!is_null($group)) { | |
12ed25e1 | 401 | $res['group'] = makeTab(Array('group' => $group), $group); |
d8e2470c | 402 | if (is_null($artid)) { |
8003ba55 | 403 | if (@$action == 'new') { |
12ed25e1 | 404 | $res['action'] = makeTab(Array('group' => $group, |
405 | 'action' => 'new'), | |
406 | _b_('Nouveau Message')); | |
d83e052b | 407 | } |
d8e2470c | 408 | } else { |
12ed25e1 | 409 | $res['message'] = makeTab(Array('group' => $group, |
410 | 'artid' => $artid), | |
411 | _b_('Message')); | |
d83e052b | 412 | if (!is_null($action)) { |
d83e052b | 413 | if ($action == 'new') { |
12ed25e1 | 414 | $res['action'] = makeTab(Array('group' => $group, |
415 | 'artid' => $artid, | |
416 | 'action' => 'new'), | |
417 | _b_('Réponse')); | |
d83e052b | 418 | } elseif ($action == 'cancel') { |
12ed25e1 | 419 | $res['action'] = makeTab(Array('group' => $group, |
420 | 'artid' => $artid, | |
421 | 'action' => 'cancel'), | |
422 | _b_('Annuler')); | |
d83e052b | 423 | } |
d83e052b | 424 | } |
8d99c683 | 425 | } |
f78f34d2 | 426 | } |
8003ba55 | 427 | $ret = '<ul id="onglet">'; |
12ed25e1 | 428 | foreach ($res as $name=>$onglet) { |
eb31725e | 429 | if ($name != @$page) { |
12ed25e1 | 430 | $ret .= '<li>' . $onglet[0] . '</li>'; |
8003ba55 | 431 | } else { |
12ed25e1 | 432 | $ret .= '<li class="actif">' . $onglet[1] . '</li>'; |
8003ba55 | 433 | } |
434 | } | |
435 | $ret .= '</ul>'; | |
436 | return $ret; | |
f78f34d2 | 437 | } |
438 | ||
8003ba55 | 439 | function displayPages($first = -1) |
440 | { | |
441 | global $banana; | |
442 | extract($banana->state); | |
443 | $res = null; | |
444 | if (!is_null($group) && is_null($artid) | |
445 | && sizeof($banana->spool->overview)>$banana->tmax) { | |
446 | $res .= '<div class="pages">'; | |
447 | $n = intval(log(count($banana->spool->overview), 10))+1; | |
448 | $i = 1; | |
449 | for ($ndx = 1 ; $ndx <= sizeof($banana->spool->overview) ; $ndx += $banana->tmax) { | |
450 | if ($first==$ndx) { | |
451 | $res .= '<strong>' . $i . '</strong> '; | |
452 | } else { | |
453 | $res .= makeHREF(Array('group' => $group, | |
454 | 'first' => $ndx), | |
455 | $i, | |
456 | $ndx . '-' . min($ndx+$banana->tmax-1,sizeof($banana->spool->overview))) | |
457 | . ' '; | |
458 | } | |
459 | $i++; | |
460 | } | |
461 | $res .= '</div>'; | |
462 | } | |
463 | return $res; | |
464 | } | |
465 | ||
466 | function makeTable($text) | |
467 | { | |
12ed25e1 | 468 | return '<table class="cadre_a_onglet" cellpadding="0" cellspacing="0" width="100%">' |
8003ba55 | 469 | . '<tr><td>' |
470 | . displayTabs() | |
471 | . '</td></tr>' | |
472 | . '<tr><td class="conteneur_tab">' | |
473 | . $text | |
474 | . '</td></tr>' | |
475 | . '</table>'; | |
476 | } | |
477 | ||
f78f34d2 | 478 | /******************************************************************************** |
479 | * FORMATTING STUFF : BODY | |
480 | */ | |
481 | ||
a205f5e3 | 482 | function autoformat($text, $part = false) |
28fb1083 | 483 | { |
484 | global $banana; | |
485 | $length = $banana->wrap; | |
a205f5e3 | 486 | $all = null; |
487 | if (!$part) { | |
488 | $all = ', all=1'; | |
489 | } | |
fc71753b | 490 | |
a205f5e3 | 491 | $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length$all };'"; |
28fb1083 | 492 | exec($cmd, $result, $ret); |
493 | if ($ret != 0) { | |
9ee36ea4 | 494 | $result = split("\n", $text); |
28fb1083 | 495 | } |
496 | return $result; | |
497 | } | |
498 | ||
e04e6059 | 499 | function wrap($text, $_prefix="", $_force=false) |
cced14b6 | 500 | { |
dd7d1c59 | 501 | $parts = preg_split("/\n-- ?\n/", $text); |
276debfc | 502 | if (count($parts) >1) { |
503 | $sign = "\n-- \n" . array_pop($parts); | |
504 | $text = join("\n-- \n", $parts); | |
3ee590a9 | 505 | } else { |
276debfc | 506 | $sign = ''; |
3ee590a9 | 507 | } |
2dbc0167 | 508 | |
509 | global $banana; | |
e04e6059 | 510 | $url = $banana->url_regexp; |
2dbc0167 | 511 | $length = $banana->wrap; |
e04e6059 | 512 | $max = $length + ($length/10); |
513 | $splits = split("\n", $text); | |
28fb1083 | 514 | $result = array(); |
515 | $next = array(); | |
516 | $format = false; | |
e04e6059 | 517 | foreach ($splits as $line) { |
518 | if ($_force || strlen($line) > $max) { | |
fc71753b | 519 | if (preg_match("!^(.*)($url)(.*)!i", $line, $matches) |
520 | && strlen($matches[2]) > $length && strlen($matches) < 900) { | |
28fb1083 | 521 | if (strlen($matches[1]) != 0) { |
522 | array_push($next, rtrim($matches[1])); | |
523 | if (strlen($matches[1]) > $max) { | |
524 | $format = true; | |
525 | } | |
526 | } | |
527 | ||
528 | if ($format) { | |
529 | $result = array_merge($result, autoformat(join("\n", $next))); | |
530 | } else { | |
531 | $result = array_merge($result, $next); | |
532 | } | |
533 | $format = false; | |
534 | $next = array(); | |
535 | array_push($result, $matches[2]); | |
536 | ||
537 | if (strlen($matches[6]) != 0) { | |
538 | array_push($next, ltrim($matches[6])); | |
539 | if (strlen($matches[6]) > $max) { | |
540 | $format = true; | |
541 | } | |
542 | } | |
543 | } else { | |
fc71753b | 544 | if (strlen($line) > 2 * $max) { |
a205f5e3 | 545 | $next = array_merge($next, autoformat($line, true)); |
fc71753b | 546 | } else { |
547 | $format = true; | |
548 | array_push($next, $line); | |
549 | } | |
e04e6059 | 550 | } |
28fb1083 | 551 | } else { |
552 | array_push($next, $line); | |
e04e6059 | 553 | } |
554 | } | |
28fb1083 | 555 | if ($format) { |
556 | $result = array_merge($result, autoformat(join("\n", $next))); | |
557 | } else { | |
558 | $result = array_merge($result, $next); | |
ee4709c7 | 559 | } |
3ee590a9 | 560 | |
f8e23519 | 561 | return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign); |
3ee590a9 | 562 | } |
563 | ||
e04e6059 | 564 | function cutlink($link) |
565 | { | |
566 | global $banana; | |
567 | ||
568 | if (strlen($link) > $banana->wrap) { | |
569 | $link = substr($link, 0, $banana->wrap - 3)."..."; | |
570 | } | |
571 | return $link; | |
572 | } | |
573 | ||
4c921acf | 574 | function cleanurl($url) |
575 | { | |
576 | $url = str_replace('@', '%40', $url); | |
577 | return '<a href="'.$url.'" title="'.$url.'">'.cutlink($url).'</a>'; | |
578 | } | |
579 | ||
e04e6059 | 580 | function formatbody($_text, $format='plain', $flowed=false) |
8f6f50fb | 581 | { |
582 | if ($format == 'html') { | |
8452d698 | 583 | $res = html_entity_decode(to_entities(removeEvilTags($_text))); |
5d133234 | 584 | } else if ($format == 'richtext') { |
8452d698 | 585 | $res = html_entity_decode(to_entities(richtextToHtml($_text))); |
8f6f50fb | 586 | } else { |
8452d698 | 587 | $res = to_entities(wrap($_text, "", $flowed)); |
1fb11000 | 588 | $res = formatPlainText($res); |
d5588318 | 589 | } |
8f6f50fb | 590 | |
09f71105 | 591 | if ($format != 'html') { |
592 | global $banana; | |
593 | $url = $banana->url_regexp; | |
594 | $res = preg_replace("/(<|>|")/", " \\1 ", $res); | |
4c921acf | 595 | $res = preg_replace("!$url!ie", "'\\1'.cleanurl('\\2').'\\3'", $res); |
09f71105 | 596 | $res = preg_replace('/(["\[])?(?:mailto:)?([a-z0-9.\-+_]+@[a-z0-9.\-+_]+)(["\]])?/i', '\1<a href="mailto:\2">\2</a>\3', $res); |
597 | $res = preg_replace("/ (<|>|") /", "\\1", $res); | |
598 | ||
599 | if ($format == 'richtext') { | |
600 | $format = 'html'; | |
601 | } | |
602 | } | |
e04e6059 | 603 | |
8f6f50fb | 604 | if ($format == 'html') { |
9c192e03 | 605 | $res = preg_replace("@(</p>)\n?-- ?\n?(<p[^>]*>|<br[^>]*>)@", "\\1<br/>-- \\2", $res); |
606 | $res = preg_replace("@<br[^>]*>\n?-- ?\n?(<p[^>]*>)@", "<br/>-- <br/>\\2", $res); | |
607 | $res = preg_replace("@(<pre[^>]*>)\n?-- ?\n@", "<br/>-- <br/>\\1", $res); | |
608 | $parts = preg_split("@(:?<p[^>]*>\n?-- ?\n?</p>|<br[^>]*>\n?-- ?\n?<br[^>]*>)@", $res); | |
ce4c7bf6 | 609 | $sign = '<hr style="width: 100%; margin: 1em 0em; " />'; |
f5eb6c66 | 610 | } else { |
9ee36ea4 | 611 | while (preg_match("@(^|<pre>|\n)>@i", $res)) { |
b7a8ff33 | 612 | $res = preg_replace("@(^|<pre>|\n)((>[^\n]*\n)+)@ie", |
9ee36ea4 | 613 | "'\\1</pre><blockquote><pre>'" |
d5588318 | 614 | .".stripslashes(preg_replace('@(^|<pre>|\n)>[ \\t\\r]*@i', '\\1', '\\2'))" |
615 | .".'</pre></blockquote><pre>'", | |
616 | $res); | |
73afa785 | 617 | } |
d5588318 | 618 | $res = preg_replace("@<pre>-- ?\n@", "<pre>\n-- \n", $res); |
f5eb6c66 | 619 | $parts = preg_split("/\n-- ?\n/", $res); |
ce4c7bf6 | 620 | $sign = '</pre><hr style="width: 100%; margin: 1em 0em; " /><pre>'; |
8f6f50fb | 621 | } |
622 | ||
8452d698 | 623 | return join($sign, $parts); |
cced14b6 | 624 | } |
625 | ||
d5588318 | 626 | // vim:set et sw=4 sts=4 ts=4 |
3ee590a9 | 627 | ?> |