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 function to_entities($str) {
17 require_once 'banana/utf8.php';
18 return utf8entities(htmlentities($str, ENT_NOQUOTES
, 'UTF-8'));
21 function is_utf8($s) { return iconv('utf-8', 'utf-8', $s) == $s; }
23 /********************************************************************************
27 function _headerdecode($charset, $c, $str) {
28 $s = ($c == 'Q') ?
quoted_printable_decode($str) : base64_decode($str);
29 $s = iconv($charset, 'iso-8859-15', $s);
30 return str_replace('_', ' ', $s);
33 function headerDecode($value) {
34 $val = preg_replace('/(=\?[^?]*\?[BQ]\?[^?]*\?=) (=\?[^?]*\?[BQ]\?[^?]*\?=)/', '\1\2', $value);
35 return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val);
38 function headerEncode($value, $trim = 0) {
40 if (strlen($value) > $trim) {
41 $value = substr($value, 0, $trim) . "[...]";
44 return "=?UTF-8?B?".base64_encode($value)."?=";
47 function header_translate($hdr) {
49 case 'from': return _b_('De');
50 case 'subject': return _b_('Sujet');
51 case 'newsgroups': return _b_('Forums');
52 case 'followup-to': return _b_('Suivi-à');
53 case 'date': return _b_('Date');
54 case 'organization': return _b_('Organisation');
55 case 'references': return _b_('Références');
56 case 'x-face': return _b_('Image');
58 if (function_exists('hook_headerTranslate')
59 && $res = hook_headerTranslate($hdr)) {
66 function formatDisplayHeader($_header,$_text) {
70 return formatDate($_text);
75 $groups = preg_split("/[\t ]*,[\t ]*/",$_text);
76 foreach ($groups as $g) {
77 $res.="<a href='?group=$g'>$g</a>, ";
79 return substr($res,0, -2);
82 return formatFrom($_text);
87 $text = str_replace("><","> <",$_text);
88 $text = preg_split("/[ \t]/",strtr($text,$banana->spool
->ids
));
89 $parents = preg_grep("/^\d+$/",$text);
90 $p = array_pop($parents);
95 $p = $banana->spool
->overview
[$p]->parent
;
97 foreach (array_reverse($par_ok) as $p) {
98 $rsl .= "<a href=\"?group={$banana->spool->group}&artid=$p\">$ndx</a> ";
104 return '<img src="xface.php?face='.base64_encode($_text).'" alt="x-face" />';
107 if (function_exists('hook_formatDisplayHeader')
108 && $res = hook_formatDisplayHeader($_header, $_text))
112 return htmlentities($_text);
116 /********************************************************************************
120 function formatDate($_text) {
121 return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text));
124 function fancyDate($stamp) {
125 $today = intval(time() / (24*3600));
126 $dday = intval($stamp / (24*3600));
128 if ($today == $dday) {
130 } elseif ($today == 1 +
$dday) {
131 $format = _b_('hier')." %H:%M";
132 } elseif ($today < 7 +
$dday) {
133 $format = '%a %H:%M';
135 $format = '%a %e %b';
137 return strftime($format, $stamp);
140 function formatFrom($text) {
141 # From: mark@cbosgd.ATT.COM
142 # From: mark@cbosgd.ATT.COM (Mark Horton)
143 # From: Mark Horton <mark@cbosgd.ATT.COM>
144 $mailto = '<a href="mailto:';
146 $result = htmlentities($text);
147 if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) {
148 $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[1]."@".$regs[2])."</a>";
150 if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) {
151 $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[3])."</a>";
153 if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) {
154 $result="$mailto{$regs[2]}@{$regs[3]}\">".htmlentities($regs[1])."</a>";
156 return preg_replace("/\\\(\(|\))/","\\1",$result);
159 function displayshortcuts($first = -1) {
161 extract($banana->state
);
163 $res = '<div class="banana_scuts">';
164 $res .= '[<a href="?">'._b_('Liste des forums').'</a>] ';
165 if (is_null($group)) {
166 return $res.'[<a href="?subscribe=1">'._b_('Abonnements').'</a>]</div>';
169 $res .= "[<a href=\"?group=$group\">$group</a>] ";
171 if (is_null($artid)) {
172 $res .= "[<a href=\"?group=$group&action=new\">"._b_('Nouveau message')."</a>] ";
173 if (sizeof($banana->spool
->overview
)>$banana->tmax
) {
175 $n = intval(log(count($banana->spool
->overview
), 10))+
1;
176 for ($ndx=1; $ndx <= sizeof($banana->spool
->overview
); $ndx +
= $banana->tmax
) {
178 $fmt = "[%0{$n}u-%0{$n}u] ";
180 $fmt = "[<a href=\"?group=$group&first=$ndx\">%0{$n}u-%0{$n}u</a>] ";
182 $res .= sprintf($fmt, $ndx, min($ndx+
$banana->tmax
-1,sizeof($banana->spool
->overview
)));
186 $res .= "[<a href=\"?group=$group&artid=$artid&action=new\">"
187 ._b_('Répondre')."</a>] ";
188 if ($banana->post
->checkcancel()) {
189 $res .= "[<a href=\"?group=$group&artid=$artid&action=cancel\">"
190 ._b_('Annuler ce message')."</a>] ";
193 return $res.'</div>';
196 /********************************************************************************
197 * FORMATTING STUFF : BODY
200 function wrap($text, $_prefix="")
202 $parts = preg_split("/\n-- ?\n/", $text);
203 if (count($parts) >1) {
204 $sign = "\n-- \n" . array_pop($parts);
205 $text = join("\n-- \n", $parts);
212 $length = $banana->wrap
;
213 $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length, all=>1 };'";
216 return $_prefix.join("\n$_prefix", $result).($_prefix ?
'' : $sign);
219 function formatbody($_text) {
220 $res = "\n\n" . to_entities(wrap($_text, ""))."\n\n";
221 $res = preg_replace("/(<|>|")/", " \\1 ", $res);
222 $res = preg_replace('/(["\[])?((https?|ftp|news):\/\/[a-z@0-9.~%$£µ&i#\-+=_\/\?]*)(["\]])?/i', "\\1<a href=\"\\2\">\\2</a>\\4", $res);
223 $res = preg_replace("/ (<|>|") /", "\\1", $res);
225 $parts = preg_split("/\n-- ?\n/", $res);
227 if (count($parts) > 1) {
228 $sign = "</pre><hr style='width: 100%; margin: 1em 0em; ' /><pre>" . array_pop($parts);
229 return join("\n-- \n", $parts).$sign;