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