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