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