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 /********************************************************************************
14 function _b_($str) { return utf8_decode(dgettext('banana', utf8_encode($str))); }
16 /********************************************************************************
20 function _headerdecode($charset, $c, $str) {
21 $s = ($c == 'Q') ?
quoted_printable_decode($str) : base64_decode($str);
22 $s = iconv($charset, 'iso-8859-15', $s);
23 return str_replace('_', ' ', $s);
26 function headerDecode($value) {
27 $val = preg_replace('/(=\?[^?]*\?[BQ]\?[^?]*\?=) (=\?[^?]*\?[BQ]\?[^?]*\?=)/', '\1\2', $value);
28 return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val);
31 function header_translate($hdr) {
33 case 'from': return _b_('De');
34 case 'subject': return _b_('Sujet');
35 case 'newsgroups': return _b_('Forums');
36 case 'followup-to': return _b_('Suivi-à');
37 case 'date': return _b_('Date');
38 case 'organization': return _b_('Organisation');
39 case 'references': return _b_('Références');
40 case 'x-face': return _b_('Image');
42 if (function_exists('hook_header_translate')) {
43 return hook_header_translate($hdr);
49 function formatDisplayHeader($_header,$_text) {
53 return formatDate($_text);
58 $groups = preg_split("/[\t ]*,[\t ]*/",$_text);
59 foreach ($groups as $g) {
60 $res.="<a href='?group=$g'>$g</a>, ";
62 return substr($res,0, -2);
65 return formatFrom($_text);
70 $text = str_replace("><","> <",$_text);
71 $text = preg_split("/[ \t]/",strtr($text,$banana->spool
->ids
));
72 $parents = preg_grep("/^\d+$/",$text);
73 $p = array_pop($parents);
78 $p = $banana->spool
->overview
[$p]->parent
;
80 foreach (array_reverse($par_ok) as $p) {
81 $rsl .= "<a href=\"?group={$banana->spool->group}&artid=$p\">$ndx</a> ";
87 return '<img src="xface.php?face='.base64_encode($_text).'" alt="x-face" />';
90 if (function_exists('hook_formatDisplayHeader')) {
91 return hook_formatDisplayHeader($_header, $_text);
93 return htmlentities($_text);
97 /********************************************************************************
101 function formatDate($_text) {
102 return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text));
105 function fancyDate($stamp) {
106 $today = intval(time() / (24*3600));
107 $dday = intval($stamp / (24*3600));
109 if ($today == $dday) {
111 } elseif ($today == 1 +
$dday) {
112 $format = _b_('hier')." %H:%M";
113 } elseif ($today < 7 +
$dday) {
114 $format = '%a %H:%M';
116 $format = '%a %e %b';
118 return strftime($format, $stamp);
121 function formatFrom($text) {
122 # From: mark@cbosgd.ATT.COM
123 # From: mark@cbosgd.ATT.COM (Mark Horton)
124 # From: Mark Horton <mark@cbosgd.ATT.COM>
125 $mailto = '<a href="mailto:';
127 $result = htmlentities($text);
128 if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) {
129 $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[1]."@".$regs[2])."</a>";
131 if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) {
132 $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[3])."</a>";
134 if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) {
135 $result="$mailto{$regs[2]}@{$regs[3]}\">".htmlentities($regs[1])."</a>";
137 return preg_replace("/\\\(\(|\))/","\\1",$result);
140 function displayshortcuts($first = -1) {
143 $res = '<div class="banana_scuts">';
145 if (function_exists('hook_displayshortcuts')) {
146 $res .= hook_displayshortcuts($sname, $first);
149 extract($banana->state
);
151 $res .= '[<a href="?">'._b_('Liste des forums').'</a>] ';
152 if (is_null($group)) {
153 return $res.'</div>';
156 $res .= "[<a href=\"?group=$group\">$group</a>] ";
158 if (is_null($artid)) {
159 $res .= "[<a href=\"?group=$group&action=new\">"._b_('Nouveau message')."</a>] ";
160 if (sizeof($banana->spool
->overview
)>$banana->tmax
) {
162 $n = intval(log(count($banana->spool
->overview
), 10))+
1;
163 for ($ndx=1; $ndx <= sizeof($banana->spool
->overview
); $ndx +
= $banana->tmax
) {
165 $fmt = "[%0{$n}u-%0{$n}u] ";
167 $fmt = "[<a href=\"?group=$group&first=$ndx\">%0{$n}u-%0{$n}u</a>] ";
169 $res .= sprintf($fmt, $ndx, min($ndx+
$banana->tmax
-1,sizeof($banana->spool
->overview
)));
173 $res .= "[<a href=\"?group=$group&artid=$artid&action=new\">"
174 ._b_('Répondre')."</a>] ";
175 if ($banana->post
->checkcancel()) {
176 $res .= "[<a href=\"?group=$group&artid=$artid&action=cancel\">"
177 ._b_('Annuler ce message')."</a>] ";
180 return $res.'</div>';
183 /********************************************************************************
184 * FORMATTING STUFF : BODY
187 function wrap($text, $_prefix="")
189 $parts = preg_split("/\n-- ?\n/", $text);
190 if (count($parts) >1) {
191 $sign = "\n-- \n" . array_pop($parts);
192 $text = join("\n-- \n", $parts);
199 $length = $banana->wrap
;
200 $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length, all=>1 };'";
203 return $_prefix.join("\n$_prefix", $result).($_prefix ?
'' : $sign);
206 function formatbody($_text) {
207 $res = "\n\n" . htmlentities(wrap($_text, ""))."\n\n";
208 $res = preg_replace("/(<|>|")/", " \\1 ", $res);
209 $res = preg_replace('/(["\[])?((https?|ftp|news):\/\/[a-z@0-9.~%$£µ&i#\-+=_\/\?]*)(["\]])?/i', "\\1<a href=\"\\2\">\\2</a>\\4", $res);
210 $res = preg_replace("/ (<|>|") /", "\\1", $res);
212 $parts = preg_split("/\n-- ?\n/", $res);
214 if (count($parts) > 1) {
215 $sign = "</pre><hr style='width: 100%; margin: 1em 0em; ' /><pre>" . array_pop($parts);
216 return join("\n-- \n", $parts).$sign;