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 | ||
9cac5c87 | 33 | function redirect($params) |
34 | { | |
35 | header('Location: ' . makeLink($params)); | |
36 | } | |
37 | ||
38 | function makeLink($params) | |
39 | { | |
40 | $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'http://'; | |
41 | $host = $_SERVER['HTTP_HOST']; | |
42 | $file = $_SERVER['PHP_SELF']; | |
43 | ||
44 | return $proto . $host . $file . '?' . $params; | |
45 | } | |
46 | ||
8f6f50fb | 47 | /******************************************************************************** |
48 | * HTML STUFF | |
49 | * Taken from php.net | |
50 | */ | |
51 | ||
5d133234 | 52 | /** |
8f6f50fb | 53 | * @return string |
54 | * @param string | |
55 | * @desc Strip forbidden tags and delegate tag-source check to removeEvilAttributes() | |
56 | */ | |
57 | function removeEvilTags($source) | |
58 | { | |
73ab762c | 59 | $allowedTags = '<h1><b><i><a><ul><li><pre><hr><blockquote><img><br><font><p><small><big><sup><sub><code><em>'; |
76032c26 | 60 | $source = preg_replace('|</div>|i', '<br />', $source); |
8f6f50fb | 61 | $source = strip_tags($source, $allowedTags); |
62 | return preg_replace('/<(.*?)>/ie', "'<'.removeEvilAttributes('\\1').'>'", $source); | |
63 | } | |
64 | ||
65 | /** | |
66 | * @return string | |
67 | * @param string | |
68 | * @desc Strip forbidden attributes from a tag | |
69 | */ | |
70 | function removeEvilAttributes($tagSource) | |
71 | { | |
72 | $stripAttrib = 'javascript:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|'. | |
73 | 'onmousemove|onmouseout|onkeypress|onkeydown|onkeyup'; | |
74 | return stripslashes(preg_replace("/$stripAttrib/i", '', $tagSource)); | |
75 | } | |
76 | ||
5d133234 | 77 | /** Convert html to plain text |
78 | */ | |
79 | function htmlToPlainText($res) | |
80 | { | |
76032c26 | 81 | $res = trim(html_entity_decode(strip_tags($res, '<div><br><p>'))); |
82 | $res = preg_replace("@</?(br|p|div)[^>]*>@i", "\n", $res); | |
e27ae1a3 | 83 | if (!is_utf8($res)) { |
84 | $res = utf8_encode($res); | |
85 | } | |
5d133234 | 86 | return $res; |
87 | } | |
88 | ||
89 | /******************************************************************************** | |
90 | * RICHTEXT STUFF | |
91 | */ | |
92 | ||
93 | /** Convert richtext to html | |
94 | */ | |
95 | function richtextToHtml($source) | |
96 | { | |
97 | $tags = Array('bold' => 'b', | |
98 | 'italic' => 'i', | |
99 | 'smaller' => 'small', | |
100 | 'bigger' => 'big', | |
101 | 'underline' => 'u', | |
102 | 'subscript' => 'sub', | |
103 | 'superscript' => 'sup', | |
104 | 'excerpt' => 'blockquote', | |
105 | 'paragraph' => 'p', | |
106 | 'nl' => 'br' | |
107 | ); | |
108 | ||
109 | // clean unsupported tags | |
110 | $protectedTags = '<signature><lt><comment><'.join('><', array_keys($tags)).'>'; | |
111 | $source = strip_tags($source, $protectedTags); | |
112 | ||
113 | // convert richtext tags to html | |
114 | foreach (array_keys($tags) as $tag) { | |
115 | $source = preg_replace('@(</?)'.$tag.'([^>]*>)@i', '\1'.$tags[$tag].'\2', $source); | |
116 | } | |
117 | ||
118 | // some special cases | |
119 | $source = preg_replace('@<signature>@i', '<br>-- <br>', $source); | |
120 | $source = preg_replace('@</signature>@i', '', $source); | |
121 | $source = preg_replace('@<lt>@i', '<', $source); | |
122 | $source = preg_replace('@<comment[^>]*>((?:[^<]|<(?!/comment>))*)</comment>@i', '<!-- \1 -->', $source); | |
123 | return removeEvilAttributes($source); | |
124 | } | |
125 | ||
73d5bf46 | 126 | /******************************************************************************** |
127 | * HEADER STUFF | |
128 | */ | |
129 | ||
dd7d1c59 | 130 | function _headerdecode($charset, $c, $str) { |
67d2dcfc | 131 | $s = ($c == 'Q' || $c == 'q') ? quoted_printable_decode($str) : base64_decode($str); |
dd7d1c59 | 132 | $s = iconv($charset, 'iso-8859-15', $s); |
133 | return str_replace('_', ' ', $s); | |
134 | } | |
135 | ||
136 | function headerDecode($value) { | |
67d2dcfc | 137 | $val = preg_replace('/(=\?[^?]*\?[BQbq]\?[^?]*\?=) (=\?[^?]*\?[BQbq]\?[^?]*\?=)/', '\1\2', $value); |
138 | return preg_replace('/=\?([^?]*)\?([BQbq])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val); | |
dd7d1c59 | 139 | } |
140 | ||
9cf3f056 PHM |
141 | function headerEncode($value, $trim = 0) { |
142 | if ($trim) { | |
d752880f PHM |
143 | if (strlen($value) > $trim) { |
144 | $value = substr($value, 0, $trim) . "[...]"; | |
145 | } | |
9cf3f056 PHM |
146 | } |
147 | return "=?UTF-8?B?".base64_encode($value)."?="; | |
148 | } | |
149 | ||
e2cae7e3 | 150 | function header_translate($hdr) { |
d4c19591 | 151 | switch ($hdr) { |
0a65ec9d | 152 | case 'from': return _b_('De'); |
153 | case 'subject': return _b_('Sujet'); | |
154 | case 'newsgroups': return _b_('Forums'); | |
d4c19591 | 155 | case 'followup-to': return _b_('Suivi-à'); |
0a65ec9d | 156 | case 'date': return _b_('Date'); |
157 | case 'organization': return _b_('Organisation'); | |
158 | case 'references': return _b_('Références'); | |
d4c19591 | 159 | case 'x-face': return _b_('Image'); |
e2cae7e3 | 160 | default: |
559be3d6 | 161 | if (function_exists('hook_headerTranslate') |
162 | && $res = hook_headerTranslate($hdr)) { | |
163 | return $res; | |
2dbc0167 | 164 | } |
e2cae7e3 | 165 | return $hdr; |
166 | } | |
167 | } | |
168 | ||
2dbc0167 | 169 | function formatDisplayHeader($_header,$_text) { |
170 | global $banana; | |
171 | switch ($_header) { | |
172 | case "date": | |
173 | return formatDate($_text); | |
174 | ||
175 | case "followup-to": | |
176 | case "newsgroups": | |
177 | $res = ""; | |
178 | $groups = preg_split("/[\t ]*,[\t ]*/",$_text); | |
179 | foreach ($groups as $g) { | |
8d99c683 | 180 | $res.="<a href='?group=$g'>$g</a>, "; |
2dbc0167 | 181 | } |
182 | return substr($res,0, -2); | |
183 | ||
184 | case "from": | |
185 | return formatFrom($_text); | |
186 | ||
187 | case "references": | |
188 | $rsl = ""; | |
189 | $ndx = 1; | |
190 | $text = str_replace("><","> <",$_text); | |
191 | $text = preg_split("/[ \t]/",strtr($text,$banana->spool->ids)); | |
192 | $parents = preg_grep("/^\d+$/",$text); | |
193 | $p = array_pop($parents); | |
58d1740e | 194 | $par_ok = Array(); |
2dbc0167 | 195 | |
196 | while ($p) { | |
58d1740e | 197 | $par_ok[]=$p; |
2dbc0167 | 198 | $p = $banana->spool->overview[$p]->parent; |
199 | } | |
58d1740e | 200 | foreach (array_reverse($par_ok) as $p) { |
8d99c683 | 201 | $rsl .= "<a href=\"?group={$banana->spool->group}&artid=$p\">$ndx</a> "; |
2dbc0167 | 202 | $ndx++; |
203 | } | |
204 | return $rsl; | |
205 | ||
206 | case "x-face": | |
09a8b2b0 | 207 | return '<img src="xface.php?face='.urlencode(base64_encode($_text)).'" alt="x-face" />'; |
2dbc0167 | 208 | |
209 | default: | |
559be3d6 | 210 | if (function_exists('hook_formatDisplayHeader') |
211 | && $res = hook_formatDisplayHeader($_header, $_text)) | |
212 | { | |
213 | return $res; | |
2dbc0167 | 214 | } |
215 | return htmlentities($_text); | |
216 | } | |
217 | } | |
218 | ||
73d5bf46 | 219 | /******************************************************************************** |
220 | * FORMATTING STUFF | |
221 | */ | |
222 | ||
e2cae7e3 | 223 | function formatDate($_text) { |
224 | return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text)); | |
225 | } | |
226 | ||
227 | function fancyDate($stamp) { | |
228 | $today = intval(time() / (24*3600)); | |
229 | $dday = intval($stamp / (24*3600)); | |
230 | ||
231 | if ($today == $dday) { | |
232 | $format = "%H:%M"; | |
233 | } elseif ($today == 1 + $dday) { | |
0a65ec9d | 234 | $format = _b_('hier')." %H:%M"; |
e2cae7e3 | 235 | } elseif ($today < 7 + $dday) { |
1248ebac | 236 | $format = '%a %H:%M'; |
e2cae7e3 | 237 | } else { |
238 | $format = '%a %e %b'; | |
239 | } | |
240 | return strftime($format, $stamp); | |
241 | } | |
242 | ||
dd7d1c59 | 243 | function formatFrom($text) { |
244 | # From: mark@cbosgd.ATT.COM | |
245 | # From: mark@cbosgd.ATT.COM (Mark Horton) | |
246 | # From: Mark Horton <mark@cbosgd.ATT.COM> | |
247 | $mailto = '<a href="mailto:'; | |
248 | ||
249 | $result = htmlentities($text); | |
250 | if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) { | |
251 | $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[1]."@".$regs[2])."</a>"; | |
252 | } | |
253 | if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) { | |
254 | $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[3])."</a>"; | |
255 | } | |
256 | if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) { | |
257 | $result="$mailto{$regs[2]}@{$regs[3]}\">".htmlentities($regs[1])."</a>"; | |
258 | } | |
259 | return preg_replace("/\\\(\(|\))/","\\1",$result); | |
260 | } | |
261 | ||
f78f34d2 | 262 | function displayshortcuts($first = -1) { |
41cf00eb | 263 | global $banana; |
8d99c683 | 264 | extract($banana->state); |
265 | ||
559be3d6 | 266 | $res = '<div class="banana_scuts">'; |
8d99c683 | 267 | $res .= '[<a href="?">'._b_('Liste des forums').'</a>] '; |
268 | if (is_null($group)) { | |
4cc7f778 | 269 | return $res.'[<a href="?subscribe=1">'._b_('Abonnements').'</a>]</div>'; |
8d99c683 | 270 | } |
271 | ||
272 | $res .= "[<a href=\"?group=$group\">$group</a>] "; | |
273 | ||
274 | if (is_null($artid)) { | |
275 | $res .= "[<a href=\"?group=$group&action=new\">"._b_('Nouveau message')."</a>] "; | |
276 | if (sizeof($banana->spool->overview)>$banana->tmax) { | |
277 | $res .= '<br />'; | |
278 | $n = intval(log(count($banana->spool->overview), 10))+1; | |
279 | for ($ndx=1; $ndx <= sizeof($banana->spool->overview); $ndx += $banana->tmax) { | |
280 | if ($first==$ndx) { | |
281 | $fmt = "[%0{$n}u-%0{$n}u] "; | |
282 | } else { | |
283 | $fmt = "[<a href=\"?group=$group&first=$ndx\">%0{$n}u-%0{$n}u</a>] "; | |
f78f34d2 | 284 | } |
8d99c683 | 285 | $res .= sprintf($fmt, $ndx, min($ndx+$banana->tmax-1,sizeof($banana->spool->overview))); |
f78f34d2 | 286 | } |
8d99c683 | 287 | } |
288 | } else { | |
289 | $res .= "[<a href=\"?group=$group&artid=$artid&action=new\">" | |
73afa785 | 290 | ._b_('Répondre')."</a>] "; |
940ae667 | 291 | if ($banana->post && $banana->post->checkcancel()) { |
8d99c683 | 292 | $res .= "[<a href=\"?group=$group&artid=$artid&action=cancel\">" |
73afa785 | 293 | ._b_('Annuler ce message')."</a>] "; |
8d99c683 | 294 | } |
f78f34d2 | 295 | } |
8d99c683 | 296 | return $res.'</div>'; |
f78f34d2 | 297 | } |
298 | ||
299 | /******************************************************************************** | |
300 | * FORMATTING STUFF : BODY | |
301 | */ | |
302 | ||
28fb1083 | 303 | function autoformat($text) |
304 | { | |
305 | global $banana; | |
306 | $length = $banana->wrap; | |
307 | ||
308 | $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length, all=>1 };'"; | |
309 | exec($cmd, $result, $ret); | |
310 | if ($ret != 0) { | |
9ee36ea4 | 311 | $result = split("\n", $text); |
28fb1083 | 312 | } |
313 | return $result; | |
314 | } | |
315 | ||
e04e6059 | 316 | function wrap($text, $_prefix="", $_force=false) |
cced14b6 | 317 | { |
dd7d1c59 | 318 | $parts = preg_split("/\n-- ?\n/", $text); |
276debfc | 319 | if (count($parts) >1) { |
320 | $sign = "\n-- \n" . array_pop($parts); | |
321 | $text = join("\n-- \n", $parts); | |
3ee590a9 | 322 | } else { |
276debfc | 323 | $sign = ''; |
3ee590a9 | 324 | } |
2dbc0167 | 325 | |
326 | global $banana; | |
e04e6059 | 327 | $url = $banana->url_regexp; |
2dbc0167 | 328 | $length = $banana->wrap; |
e04e6059 | 329 | $max = $length + ($length/10); |
330 | $splits = split("\n", $text); | |
28fb1083 | 331 | $result = array(); |
332 | $next = array(); | |
333 | $format = false; | |
e04e6059 | 334 | foreach ($splits as $line) { |
335 | if ($_force || strlen($line) > $max) { | |
28fb1083 | 336 | if (preg_match("!^(.*)($url)(.*)!i", $line, $matches) && strlen($matches[2]) > $length && strlen($matches) < 900) { |
337 | if (strlen($matches[1]) != 0) { | |
338 | array_push($next, rtrim($matches[1])); | |
339 | if (strlen($matches[1]) > $max) { | |
340 | $format = true; | |
341 | } | |
342 | } | |
343 | ||
344 | if ($format) { | |
345 | $result = array_merge($result, autoformat(join("\n", $next))); | |
346 | } else { | |
347 | $result = array_merge($result, $next); | |
348 | } | |
349 | $format = false; | |
350 | $next = array(); | |
351 | array_push($result, $matches[2]); | |
352 | ||
353 | if (strlen($matches[6]) != 0) { | |
354 | array_push($next, ltrim($matches[6])); | |
355 | if (strlen($matches[6]) > $max) { | |
356 | $format = true; | |
357 | } | |
358 | } | |
359 | } else { | |
360 | $format = true; | |
361 | array_push($next, $line); | |
e04e6059 | 362 | } |
28fb1083 | 363 | } else { |
364 | array_push($next, $line); | |
e04e6059 | 365 | } |
366 | } | |
28fb1083 | 367 | if ($format) { |
368 | $result = array_merge($result, autoformat(join("\n", $next))); | |
369 | } else { | |
370 | $result = array_merge($result, $next); | |
ee4709c7 | 371 | } |
3ee590a9 | 372 | |
f8e23519 | 373 | return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign); |
3ee590a9 | 374 | } |
375 | ||
e04e6059 | 376 | function cutlink($link) |
377 | { | |
378 | global $banana; | |
379 | ||
380 | if (strlen($link) > $banana->wrap) { | |
381 | $link = substr($link, 0, $banana->wrap - 3)."..."; | |
382 | } | |
383 | return $link; | |
384 | } | |
385 | ||
4c921acf | 386 | function cleanurl($url) |
387 | { | |
388 | $url = str_replace('@', '%40', $url); | |
389 | return '<a href="'.$url.'" title="'.$url.'">'.cutlink($url).'</a>'; | |
390 | } | |
391 | ||
e04e6059 | 392 | function formatbody($_text, $format='plain', $flowed=false) |
8f6f50fb | 393 | { |
394 | if ($format == 'html') { | |
76032c26 | 395 | $res = '<br/>'.html_entity_decode(to_entities(removeEvilTags($_text))).'<br/>'; |
5d133234 | 396 | } else if ($format == 'richtext') { |
09f71105 | 397 | $res = '<br/>'.html_entity_decode(to_entities(richtextToHtml($_text))).'<br/>'; |
8f6f50fb | 398 | } else { |
e04e6059 | 399 | $res = "\n\n" . to_entities(wrap($_text, "", $flowed))."\n\n"; |
8f6f50fb | 400 | } |
8f6f50fb | 401 | |
09f71105 | 402 | if ($format != 'html') { |
403 | global $banana; | |
404 | $url = $banana->url_regexp; | |
405 | $res = preg_replace("/(<|>|")/", " \\1 ", $res); | |
4c921acf | 406 | $res = preg_replace("!$url!ie", "'\\1'.cleanurl('\\2').'\\3'", $res); |
09f71105 | 407 | $res = preg_replace('/(["\[])?(?:mailto:)?([a-z0-9.\-+_]+@[a-z0-9.\-+_]+)(["\]])?/i', '\1<a href="mailto:\2">\2</a>\3', $res); |
408 | $res = preg_replace("/ (<|>|") /", "\\1", $res); | |
409 | ||
410 | if ($format == 'richtext') { | |
411 | $format = 'html'; | |
412 | } | |
413 | } | |
e04e6059 | 414 | |
8f6f50fb | 415 | if ($format == 'html') { |
9c192e03 | 416 | $res = preg_replace("@(</p>)\n?-- ?\n?(<p[^>]*>|<br[^>]*>)@", "\\1<br/>-- \\2", $res); |
417 | $res = preg_replace("@<br[^>]*>\n?-- ?\n?(<p[^>]*>)@", "<br/>-- <br/>\\2", $res); | |
418 | $res = preg_replace("@(<pre[^>]*>)\n?-- ?\n@", "<br/>-- <br/>\\1", $res); | |
419 | $parts = preg_split("@(:?<p[^>]*>\n?-- ?\n?</p>|<br[^>]*>\n?-- ?\n?<br[^>]*>)@", $res); | |
f5eb6c66 | 420 | } else { |
9ee36ea4 | 421 | while (preg_match("@(^|<pre>|\n)>@i", $res)) { |
b7a8ff33 | 422 | $res = preg_replace("@(^|<pre>|\n)((>[^\n]*\n)+)@ie", |
9ee36ea4 | 423 | "'\\1</pre><blockquote><pre>'" |
4c1df590 | 424 | .".stripslashes(preg_replace('@(^|<pre>|\n)>[ \\t\\r]*@i', '\\1', '\\2'))" |
425 | .".'</pre></blockquote><pre>'", | |
426 | $res); | |
73afa785 | 427 | } |
b7a8ff33 | 428 | $res = preg_replace("@<pre>-- ?\n@", "<pre>\n-- \n", $res); |
f5eb6c66 | 429 | $parts = preg_split("/\n-- ?\n/", $res); |
8f6f50fb | 430 | } |
431 | ||
dd7d1c59 | 432 | if (count($parts) > 1) { |
f5eb6c66 | 433 | $sign = array_pop($parts); |
434 | if ($format == 'html') { | |
435 | $res = join('<br/>-- <br/>', $parts); | |
73afa785 | 436 | $sign = '<hr style="width: 100%; margin: 1em 0em; " />'.$sign.'<br/>'; |
f5eb6c66 | 437 | } else { |
438 | $res = join('\n-- \n', $parts); | |
439 | $sign = '</pre><hr style="width: 100%; margin: 1em 0em; " /><pre>'.$sign; | |
440 | } | |
441 | return $res.$sign; | |
dd7d1c59 | 442 | } else { |
443 | return $res; | |
444 | } | |
cced14b6 | 445 | } |
446 | ||
3ee590a9 | 447 | ?> |