Add message subject in a tooltip in spool view
[banana.git] / banana / misc.inc.php
CommitLineData
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 14function _b_($str) { return utf8_decode(dgettext('banana', utf8_encode($str))); }
0a65ec9d 15
987299b4 16function 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
21function is_utf8($s) { return iconv('utf-8', 'utf-8', $s) == $s; }
22
8f6f50fb 23function 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 36function 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 55function 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';
67 $get = 'face=' . $params['xface'];
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 */
937a36cf 86function makeHREF($params, $text = null, $popup = 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 }
d5588318 95 $target = null;
96 if (isset($params['action']) && $params['action'] == 'view') {
97 $target = ' target="_blank"';
98 }
937a36cf 99 return '<a href="' . htmlentities($link) . $target . '"' . $popup . '>' . $text . '</a>';
9cac5c87 100}
101
d7632010 102/** Format tree images links
103 * @param img STRING Image name (without extension)
104 * @param alt STRING alternative string
105 * @param width INT to force width of the image (null if not defined)
106 *
107 * This function can be overloaded by defining hook_makeImg()
108 */
4ccaaeb7 109function makeImg($img, $alt, $height = null, $width = null)
d7632010 110{
111 if (function_exists('hook_makeImg')
9419816d 112 && $res = hook_makeImg($img, $alt, $height, $width)) {
d7632010 113 return $res;
114 }
115
116 if (!is_null($width)) {
117 $width = ' width="' . $width . '"';
118 }
4ccaaeb7 119 if (!is_null($height)) {
120 $height = ' height="' . $height . '"';
121 }
122
d7632010 123 $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
124 $host = $_SERVER['HTTP_HOST'];
125 $file = dirname($_SERVER['PHP_SELF']) . '/img/' . $img . '.gif';
126 $url = $proto . $host . $file;
127
4ccaaeb7 128 return '<img src="' . $url . '"' . $height . $width . ' alt="' . $alt . '" />';
d7632010 129}
130
8f6f50fb 131/********************************************************************************
132 * HTML STUFF
133 * Taken from php.net
134 */
135
5d133234 136/**
8f6f50fb 137 * @return string
138 * @param string
139 * @desc Strip forbidden tags and delegate tag-source check to removeEvilAttributes()
140 */
141function removeEvilTags($source)
142{
73ab762c 143 $allowedTags = '<h1><b><i><a><ul><li><pre><hr><blockquote><img><br><font><p><small><big><sup><sub><code><em>';
76032c26 144 $source = preg_replace('|</div>|i', '<br />', $source);
8f6f50fb 145 $source = strip_tags($source, $allowedTags);
146 return preg_replace('/<(.*?)>/ie', "'<'.removeEvilAttributes('\\1').'>'", $source);
147}
148
149/**
150 * @return string
151 * @param string
152 * @desc Strip forbidden attributes from a tag
153 */
154function removeEvilAttributes($tagSource)
155{
156 $stripAttrib = 'javascript:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|'.
157 'onmousemove|onmouseout|onkeypress|onkeydown|onkeyup';
158 return stripslashes(preg_replace("/$stripAttrib/i", '', $tagSource));
159}
160
5d133234 161/** Convert html to plain text
162 */
163function htmlToPlainText($res)
164{
76032c26 165 $res = trim(html_entity_decode(strip_tags($res, '<div><br><p>')));
166 $res = preg_replace("@</?(br|p|div)[^>]*>@i", "\n", $res);
e27ae1a3 167 if (!is_utf8($res)) {
168 $res = utf8_encode($res);
169 }
5d133234 170 return $res;
171}
172
173/********************************************************************************
174 * RICHTEXT STUFF
175 */
176
177/** Convert richtext to html
178 */
179function richtextToHtml($source)
180{
181 $tags = Array('bold' => 'b',
182 'italic' => 'i',
183 'smaller' => 'small',
184 'bigger' => 'big',
185 'underline' => 'u',
186 'subscript' => 'sub',
187 'superscript' => 'sup',
188 'excerpt' => 'blockquote',
189 'paragraph' => 'p',
190 'nl' => 'br'
191 );
192
193 // clean unsupported tags
194 $protectedTags = '<signature><lt><comment><'.join('><', array_keys($tags)).'>';
195 $source = strip_tags($source, $protectedTags);
196
197 // convert richtext tags to html
198 foreach (array_keys($tags) as $tag) {
199 $source = preg_replace('@(</?)'.$tag.'([^>]*>)@i', '\1'.$tags[$tag].'\2', $source);
200 }
201
202 // some special cases
203 $source = preg_replace('@<signature>@i', '<br>-- <br>', $source);
204 $source = preg_replace('@</signature>@i', '', $source);
205 $source = preg_replace('@<lt>@i', '&lt;', $source);
206 $source = preg_replace('@<comment[^>]*>((?:[^<]|<(?!/comment>))*)</comment>@i', '<!-- \1 -->', $source);
207 return removeEvilAttributes($source);
208}
209
73d5bf46 210/********************************************************************************
211 * HEADER STUFF
212 */
213
dd7d1c59 214function _headerdecode($charset, $c, $str) {
67d2dcfc 215 $s = ($c == 'Q' || $c == 'q') ? quoted_printable_decode($str) : base64_decode($str);
dd7d1c59 216 $s = iconv($charset, 'iso-8859-15', $s);
217 return str_replace('_', ' ', $s);
218}
219
220function headerDecode($value) {
67d2dcfc 221 $val = preg_replace('/(=\?[^?]*\?[BQbq]\?[^?]*\?=) (=\?[^?]*\?[BQbq]\?[^?]*\?=)/', '\1\2', $value);
222 return preg_replace('/=\?([^?]*)\?([BQbq])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val);
dd7d1c59 223}
224
9cf3f056
PHM
225function headerEncode($value, $trim = 0) {
226 if ($trim) {
d752880f
PHM
227 if (strlen($value) > $trim) {
228 $value = substr($value, 0, $trim) . "[...]";
229 }
9cf3f056
PHM
230 }
231 return "=?UTF-8?B?".base64_encode($value)."?=";
232}
233
e2cae7e3 234function header_translate($hdr) {
d4c19591 235 switch ($hdr) {
0a65ec9d 236 case 'from': return _b_('De');
237 case 'subject': return _b_('Sujet');
238 case 'newsgroups': return _b_('Forums');
d4c19591 239 case 'followup-to': return _b_('Suivi-ŕ');
0a65ec9d 240 case 'date': return _b_('Date');
241 case 'organization': return _b_('Organisation');
242 case 'references': return _b_('Références');
d4c19591 243 case 'x-face': return _b_('Image');
e2cae7e3 244 default:
559be3d6 245 if (function_exists('hook_headerTranslate')
246 && $res = hook_headerTranslate($hdr)) {
247 return $res;
2dbc0167 248 }
e2cae7e3 249 return $hdr;
250 }
251}
252
2dbc0167 253function formatDisplayHeader($_header,$_text) {
254 global $banana;
255 switch ($_header) {
256 case "date":
257 return formatDate($_text);
258
259 case "followup-to":
260 case "newsgroups":
261 $res = "";
262 $groups = preg_split("/[\t ]*,[\t ]*/",$_text);
263 foreach ($groups as $g) {
0eb1e7ef 264 $res .= makeHREF(Array('group' => $g), $g) . ', ';
2dbc0167 265 }
266 return substr($res,0, -2);
267
268 case "from":
269 return formatFrom($_text);
270
271 case "references":
272 $rsl = "";
273 $ndx = 1;
274 $text = str_replace("><","> <",$_text);
275 $text = preg_split("/[ \t]/",strtr($text,$banana->spool->ids));
276 $parents = preg_grep("/^\d+$/",$text);
277 $p = array_pop($parents);
58d1740e 278 $par_ok = Array();
2dbc0167 279
280 while ($p) {
58d1740e 281 $par_ok[]=$p;
2dbc0167 282 $p = $banana->spool->overview[$p]->parent;
283 }
58d1740e 284 foreach (array_reverse($par_ok) as $p) {
4840f22b 285 $rsl .= makeHREF(Array('group' => $banana->spool->group), $ndx) . ' ';
2dbc0167 286 $ndx++;
287 }
288 return $rsl;
289
290 case "x-face":
261210bc 291 return '<img src="' . makeLink(Array('xface' => urlencode(base64_encode($_text)))) .'" alt="x-face" />';
2dbc0167 292
293 default:
559be3d6 294 if (function_exists('hook_formatDisplayHeader')
295 && $res = hook_formatDisplayHeader($_header, $_text))
296 {
297 return $res;
2dbc0167 298 }
299 return htmlentities($_text);
300 }
301}
302
73d5bf46 303/********************************************************************************
304 * FORMATTING STUFF
305 */
306
e2cae7e3 307function formatDate($_text) {
308 return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text));
309}
310
311function fancyDate($stamp) {
312 $today = intval(time() / (24*3600));
313 $dday = intval($stamp / (24*3600));
314
315 if ($today == $dday) {
316 $format = "%H:%M";
317 } elseif ($today == 1 + $dday) {
0a65ec9d 318 $format = _b_('hier')." %H:%M";
e2cae7e3 319 } elseif ($today < 7 + $dday) {
1248ebac 320 $format = '%a %H:%M';
e2cae7e3 321 } else {
322 $format = '%a %e %b';
323 }
324 return strftime($format, $stamp);
325}
326
dd7d1c59 327function formatFrom($text) {
328# From: mark@cbosgd.ATT.COM
329# From: mark@cbosgd.ATT.COM (Mark Horton)
330# From: Mark Horton <mark@cbosgd.ATT.COM>
331 $mailto = '<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;';
332
333 $result = htmlentities($text);
334 if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) {
335 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[1]."&#64;".$regs[2])."</a>";
336 }
337 if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) {
338 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[3])."</a>";
339 }
340 if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) {
341 $result="$mailto{$regs[2]}&#64;{$regs[3]}\">".htmlentities($regs[1])."</a>";
342 }
343 return preg_replace("/\\\(\(|\))/","\\1",$result);
344}
345
f78f34d2 346function displayshortcuts($first = -1) {
41cf00eb 347 global $banana;
8d99c683 348 extract($banana->state);
349
559be3d6 350 $res = '<div class="banana_scuts">';
0eb1e7ef 351 $res .= '[' . makeHREF(Array(), _b_('Liste des forums')) . '] ';
8d99c683 352 if (is_null($group)) {
0eb1e7ef 353 return $res.'[' . makeHREF(Array('subscribe' => 1), _b_('Abonnements')) . ']</div>';
8d99c683 354 }
355
0eb1e7ef 356 $res .= '[' . makeHREF(Array('group' => $group), $group) . '] ';
8d99c683 357
358 if (is_null($artid)) {
0eb1e7ef 359 $res .= '[' . makeHREF(Array('group' => $group,
d5588318 360 'action' => 'new'),
361 _b_('Nouveau message'))
362 . '] ';
8d99c683 363 if (sizeof($banana->spool->overview)>$banana->tmax) {
937a36cf 364 $res .= '<br />Page : ';
8d99c683 365 $n = intval(log(count($banana->spool->overview), 10))+1;
937a36cf 366 $i = 1;
367 for ($ndx = 1 ; $ndx <= sizeof($banana->spool->overview) ; $ndx += $banana->tmax) {
8d99c683 368 if ($first==$ndx) {
937a36cf 369 $fmt = $i . ' ';
8d99c683 370 } else {
937a36cf 371 $fmt = makeHREF(Array('group' => $group,
372 'first' => $ndx),
373 $i,
374 '%0' . $n . 'u-%0' . $n . 'u')
375 . ' ';
f78f34d2 376 }
937a36cf 377 $i++;
8d99c683 378 $res .= sprintf($fmt, $ndx, min($ndx+$banana->tmax-1,sizeof($banana->spool->overview)));
f78f34d2 379 }
8d99c683 380 }
381 } else {
0eb1e7ef 382 $res .= '[' . makeHREF(Array('group' => $group,
d5588318 383 'artid' => $artid,
384 'action' => 'new'),
385 _b_('Répondre'))
386 . '] ';
940ae667 387 if ($banana->post && $banana->post->checkcancel()) {
0eb1e7ef 388 $res .= '[' . makeHREF(Array('group' => $group,
d5588318 389 'artid' => $artid,
390 'action' => 'cancel'),
391 _b_('Annuler ce message'))
392 . '] ';
8d99c683 393 }
f78f34d2 394 }
8d99c683 395 return $res.'</div>';
f78f34d2 396}
397
398/********************************************************************************
399 * FORMATTING STUFF : BODY
400 */
401
28fb1083 402function autoformat($text)
403{
404 global $banana;
405 $length = $banana->wrap;
406
407 $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length, all=>1 };'";
408 exec($cmd, $result, $ret);
409 if ($ret != 0) {
9ee36ea4 410 $result = split("\n", $text);
28fb1083 411 }
412 return $result;
413}
414
e04e6059 415function wrap($text, $_prefix="", $_force=false)
cced14b6 416{
dd7d1c59 417 $parts = preg_split("/\n-- ?\n/", $text);
276debfc 418 if (count($parts) >1) {
419 $sign = "\n-- \n" . array_pop($parts);
420 $text = join("\n-- \n", $parts);
3ee590a9 421 } else {
276debfc 422 $sign = '';
3ee590a9 423 }
2dbc0167 424
425 global $banana;
e04e6059 426 $url = $banana->url_regexp;
2dbc0167 427 $length = $banana->wrap;
e04e6059 428 $max = $length + ($length/10);
429 $splits = split("\n", $text);
28fb1083 430 $result = array();
431 $next = array();
432 $format = false;
e04e6059 433 foreach ($splits as $line) {
434 if ($_force || strlen($line) > $max) {
28fb1083 435 if (preg_match("!^(.*)($url)(.*)!i", $line, $matches) && strlen($matches[2]) > $length && strlen($matches) < 900) {
436 if (strlen($matches[1]) != 0) {
437 array_push($next, rtrim($matches[1]));
438 if (strlen($matches[1]) > $max) {
439 $format = true;
440 }
441 }
442
443 if ($format) {
444 $result = array_merge($result, autoformat(join("\n", $next)));
445 } else {
446 $result = array_merge($result, $next);
447 }
448 $format = false;
449 $next = array();
450 array_push($result, $matches[2]);
451
452 if (strlen($matches[6]) != 0) {
453 array_push($next, ltrim($matches[6]));
454 if (strlen($matches[6]) > $max) {
455 $format = true;
456 }
457 }
458 } else {
459 $format = true;
460 array_push($next, $line);
e04e6059 461 }
28fb1083 462 } else {
463 array_push($next, $line);
e04e6059 464 }
465 }
28fb1083 466 if ($format) {
467 $result = array_merge($result, autoformat(join("\n", $next)));
468 } else {
469 $result = array_merge($result, $next);
ee4709c7 470 }
3ee590a9 471
f8e23519 472 return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign);
3ee590a9 473}
474
e04e6059 475function cutlink($link)
476{
477 global $banana;
478
479 if (strlen($link) > $banana->wrap) {
480 $link = substr($link, 0, $banana->wrap - 3)."...";
481 }
482 return $link;
483}
484
4c921acf 485function cleanurl($url)
486{
487 $url = str_replace('@', '%40', $url);
488 return '<a href="'.$url.'" title="'.$url.'">'.cutlink($url).'</a>';
489}
490
e04e6059 491function formatbody($_text, $format='plain', $flowed=false)
8f6f50fb 492{
493 if ($format == 'html') {
76032c26 494 $res = '<br/>'.html_entity_decode(to_entities(removeEvilTags($_text))).'<br/>';
5d133234 495 } else if ($format == 'richtext') {
09f71105 496 $res = '<br/>'.html_entity_decode(to_entities(richtextToHtml($_text))).'<br/>';
8f6f50fb 497 } else {
e04e6059 498 $res = "\n\n" . to_entities(wrap($_text, "", $flowed))."\n\n";
03851c08 499 $formatting = Array('\*' => 'strong',
500 '_' => 'u',
501 '/' => 'em');
502 foreach ($formatting as $limit=>$mark) {
0e76aefc 503 $res = preg_replace('@(\W)' . $limit . '(\w+)' . $limit . '(\W)@'
d7d22d97 504 ,'\1<' . $mark . '>\2</' . $mark . '>\3'
505 , $res);
03851c08 506 }
d5588318 507 }
8f6f50fb 508
09f71105 509 if ($format != 'html') {
510 global $banana;
511 $url = $banana->url_regexp;
512 $res = preg_replace("/(&lt;|&gt;|&quot;)/", " \\1 ", $res);
4c921acf 513 $res = preg_replace("!$url!ie", "'\\1'.cleanurl('\\2').'\\3'", $res);
09f71105 514 $res = preg_replace('/(["\[])?(?:mailto:)?([a-z0-9.\-+_]+@[a-z0-9.\-+_]+)(["\]])?/i', '\1<a href="mailto:\2">\2</a>\3', $res);
515 $res = preg_replace("/ (&lt;|&gt;|&quot;) /", "\\1", $res);
516
517 if ($format == 'richtext') {
518 $format = 'html';
519 }
520 }
e04e6059 521
8f6f50fb 522 if ($format == 'html') {
9c192e03 523 $res = preg_replace("@(</p>)\n?-- ?\n?(<p[^>]*>|<br[^>]*>)@", "\\1<br/>-- \\2", $res);
524 $res = preg_replace("@<br[^>]*>\n?-- ?\n?(<p[^>]*>)@", "<br/>-- <br/>\\2", $res);
525 $res = preg_replace("@(<pre[^>]*>)\n?-- ?\n@", "<br/>-- <br/>\\1", $res);
526 $parts = preg_split("@(:?<p[^>]*>\n?-- ?\n?</p>|<br[^>]*>\n?-- ?\n?<br[^>]*>)@", $res);
f5eb6c66 527 } else {
9ee36ea4 528 while (preg_match("@(^|<pre>|\n)&gt;@i", $res)) {
b7a8ff33 529 $res = preg_replace("@(^|<pre>|\n)((&gt;[^\n]*\n)+)@ie",
9ee36ea4 530 "'\\1</pre><blockquote><pre>'"
d5588318 531 .".stripslashes(preg_replace('@(^|<pre>|\n)&gt;[ \\t\\r]*@i', '\\1', '\\2'))"
532 .".'</pre></blockquote><pre>'",
533 $res);
73afa785 534 }
d5588318 535 $res = preg_replace("@<pre>-- ?\n@", "<pre>\n-- \n", $res);
f5eb6c66 536 $parts = preg_split("/\n-- ?\n/", $res);
8f6f50fb 537 }
538
dd7d1c59 539 if (count($parts) > 1) {
f5eb6c66 540 $sign = array_pop($parts);
541 if ($format == 'html') {
542 $res = join('<br/>-- <br/>', $parts);
73afa785 543 $sign = '<hr style="width: 100%; margin: 1em 0em; " />'.$sign.'<br/>';
f5eb6c66 544 } else {
545 $res = join('\n-- \n', $parts);
546 $sign = '</pre><hr style="width: 100%; margin: 1em 0em; " /><pre>'.$sign;
547 }
548 return $res.$sign;
dd7d1c59 549 } else {
550 return $res;
551 }
cced14b6 552}
553
d5588318 554// vim:set et sw=4 sts=4 ts=4
3ee590a9 555?>