2 /********************************************************************************
3 * include/misc.inc.php : Misc functions
4 * -------------------------
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
10 /********************************************************************************
17 list($usec, $sec) = explode(" ", microtime());
18 $time[] = ((float)$usec +
(float)$sec);
23 function _b_($str) { return utf8_decode(dgettext('banana', utf8_encode($str))); }
25 /********************************************************************************
29 function _headerdecode($charset, $c, $str) {
30 $s = ($c == 'Q') ?
quoted_printable_decode($str) : base64_decode($str);
31 $s = iconv($charset, 'iso-8859-15', $s);
32 return str_replace('_', ' ', $s);
35 function headerDecode($value) {
36 $val = preg_replace('/(=\?[^?]*\?[BQ]\?[^?]*\?=) (=\?[^?]*\?[BQ]\?[^?]*\?=)/', '\1\2', $value);
37 return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val);
40 function header_translate($hdr) {
42 case 'from': return _b_('De');
43 case 'subject': return _b_('Sujet');
44 case 'newsgroups': return _b_('Forums');
45 case 'followup-to': return _b_('Suivi-à');
46 case 'date': return _b_('Date');
47 case 'organization': return _b_('Organisation');
48 case 'references': return _b_('Références');
49 case 'x-face': return _b_('Image');
51 if (function_exists('hook_header_translate')) {
52 return hook_header_translate($hdr);
58 function formatDisplayHeader($_header,$_text) {
62 return formatDate($_text);
67 $groups = preg_split("/[\t ]*,[\t ]*/",$_text);
68 foreach ($groups as $g) {
69 $res.="<a href='thread.php?group=$g'>$g</a>, ";
71 return substr($res,0, -2);
74 return formatFrom($_text);
79 $text = str_replace("><","> <",$_text);
80 $text = preg_split("/[ \t]/",strtr($text,$banana->spool
->ids
));
81 $parents = preg_grep("/^\d+$/",$text);
82 $p = array_pop($parents);
87 $p = $banana->spool
->overview
[$p]->parent
;
89 foreach (array_reverse($par_ok) as $p) {
90 $rsl .= "<a href=\"article.php?group={$banana->spool->group}&id=$p\">$ndx</a> ";
96 return '<img src="xface.php?face='.base64_encode($_text).'" alt="x-face" />';
99 if (function_exists('hook_formatDisplayHeader')) {
100 return hook_formatDisplayHeader($_header, $_text);
102 return htmlentities($_text);
106 /********************************************************************************
110 function formatDate($_text) {
111 return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text));
114 function fancyDate($stamp) {
115 $today = intval(time() / (24*3600));
116 $dday = intval($stamp / (24*3600));
118 if ($today == $dday) {
120 } elseif ($today == 1 +
$dday) {
121 $format = _b_('hier')." %H:%M";
122 } elseif ($today < 7 +
$dday) {
123 $format = '%a %H:%M';
125 $format = '%a %e %b';
127 return strftime($format, $stamp);
130 function formatFrom($text) {
131 # From: mark@cbosgd.ATT.COM
132 # From: mark@cbosgd.ATT.COM (Mark Horton)
133 # From: Mark Horton <mark@cbosgd.ATT.COM>
134 $mailto = '<a href="mailto:';
136 $result = htmlentities($text);
137 if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) {
138 $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[1]."@".$regs[2])."</a>";
140 if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) {
141 $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[3])."</a>";
143 if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) {
144 $result="$mailto{$regs[2]}@{$regs[3]}\">".htmlentities($regs[1])."</a>";
146 return preg_replace("/\\\(\(|\))/","\\1",$result);
149 function displayshortcuts($first = -1) {
151 $sname = basename($_SERVER['SCRIPT_NAME']);
153 $res = '<div class="banana_scuts">';
155 if (function_exists('hook_displayshortcuts')) {
156 $res .= hook_displayshortcuts($sname, $first);
161 $res .= '[<a href="index.php">'._b_('Liste des forums').'</a>] ';
162 $res .= "[<a href=\"post.php?group={$banana->spool->group}\">"._b_('Nouveau message')."</a>] ";
163 if (sizeof($banana->spool
->overview
)>$banana->tmax
) {
164 for ($ndx=1; $ndx<=sizeof($banana->spool
->overview
); $ndx +
= $banana->tmax
) {
166 $res .= "[$ndx-".min($ndx+
$banana->tmax
-1,sizeof($banana->spool
->overview
))."] ";
168 $res .= "[<a href=\"?group={$banana->spool->group}&first="
169 ."$ndx\">$ndx-".min($ndx+
$banana->tmax
-1,sizeof($banana->spool
->overview
))
176 $res .= '[<a href="index.php">'._b_('Liste des forums').'</a>] ';
177 $res .= "[<a href=\"thread.php?group={$banana->spool->group}\">{$banana->spool->group}</a>] ";
178 $res .= "[<a href=\"post.php?group={$banana->spool->group}&id={$banana->post->id}&type=followup\">"
179 ._b_('Répondre')."</a>] ";
180 if ($banana->post
->checkcancel()) {
181 $res .= "[<a href=\"article.php?group={$banana->spool->group}&id={$banana->post->id}&type=cancel\">"
182 ._b_('Annuler ce message')."</a>] ";
186 $res .= '[<a href="index.php">'._b_('Liste des forums').'</a>] ';
187 $res .= "[<a href=\"thread.php?group={$banana->spool->group}\">{$banana->spool->group}</a>]";
195 /********************************************************************************
196 * FORMATTING STUFF : BODY
199 function wrap($text, $_prefix="")
201 $parts = preg_split("/\n-- ?\n/", $text);
202 if (count($parts) >1) {
203 $sign = "\n-- \n" . array_pop($parts);
204 $text = join("\n-- \n", $parts);
211 $length = $banana->wrap
;
212 $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length, all=>1 };'";
215 return $_prefix.join("\n$_prefix", $result).($_prefix ?
'' : $sign);
218 function formatbody($_text) {
219 $res = "\n\n" . htmlentities(wrap($_text, ""))."\n\n";
220 $res = preg_replace("/(<|>|")/", " \\1 ", $res);
221 $res = preg_replace('/(["\[])?((https?|ftp|news):\/\/[a-z@0-9.~%$£µ&i#\-+=_\/\?]*)(["\]])?/i', "\\1<a href=\"\\2\">\\2</a>\\4", $res);
222 $res = preg_replace("/ (<|>|") /", "\\1", $res);
224 $parts = preg_split("/\n-- ?\n/", $res);
226 if (count($parts) > 1) {
227 $sign = "</pre><hr style='width: 100%; margin: 1em 0em; ' /><pre>" . array_pop($parts);
228 return join("\n-- \n", $parts).$sign;