read changelog
[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
73d5bf46 16/********************************************************************************
17 * HEADER STUFF
18 */
19
dd7d1c59 20function _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);
24}
25
26function headerDecode($value) {
27 $val = preg_replace('/(=\?[^?]*\?[BQ]\?[^?]*\?=) (=\?[^?]*\?[BQ]\?[^?]*\?=)/', '\1\2', $value);
28 return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val);
29}
30
e2cae7e3 31function header_translate($hdr) {
d4c19591 32 switch ($hdr) {
0a65ec9d 33 case 'from': return _b_('De');
34 case 'subject': return _b_('Sujet');
35 case 'newsgroups': return _b_('Forums');
d4c19591 36 case 'followup-to': return _b_('Suivi-à');
0a65ec9d 37 case 'date': return _b_('Date');
38 case 'organization': return _b_('Organisation');
39 case 'references': return _b_('Références');
d4c19591 40 case 'x-face': return _b_('Image');
e2cae7e3 41 default:
559be3d6 42 if (function_exists('hook_headerTranslate')
43 && $res = hook_headerTranslate($hdr)) {
44 return $res;
2dbc0167 45 }
e2cae7e3 46 return $hdr;
47 }
48}
49
2dbc0167 50function formatDisplayHeader($_header,$_text) {
51 global $banana;
52 switch ($_header) {
53 case "date":
54 return formatDate($_text);
55
56 case "followup-to":
57 case "newsgroups":
58 $res = "";
59 $groups = preg_split("/[\t ]*,[\t ]*/",$_text);
60 foreach ($groups as $g) {
8d99c683 61 $res.="<a href='?group=$g'>$g</a>, ";
2dbc0167 62 }
63 return substr($res,0, -2);
64
65 case "from":
66 return formatFrom($_text);
67
68 case "references":
69 $rsl = "";
70 $ndx = 1;
71 $text = str_replace("><","> <",$_text);
72 $text = preg_split("/[ \t]/",strtr($text,$banana->spool->ids));
73 $parents = preg_grep("/^\d+$/",$text);
74 $p = array_pop($parents);
58d1740e 75 $par_ok = Array();
2dbc0167 76
77 while ($p) {
58d1740e 78 $par_ok[]=$p;
2dbc0167 79 $p = $banana->spool->overview[$p]->parent;
80 }
58d1740e 81 foreach (array_reverse($par_ok) as $p) {
8d99c683 82 $rsl .= "<a href=\"?group={$banana->spool->group}&amp;artid=$p\">$ndx</a> ";
2dbc0167 83 $ndx++;
84 }
85 return $rsl;
86
87 case "x-face":
88 return '<img src="xface.php?face='.base64_encode($_text).'" alt="x-face" />';
89
90 default:
559be3d6 91 if (function_exists('hook_formatDisplayHeader')
92 && $res = hook_formatDisplayHeader($_header, $_text))
93 {
94 return $res;
2dbc0167 95 }
96 return htmlentities($_text);
97 }
98}
99
73d5bf46 100/********************************************************************************
101 * FORMATTING STUFF
102 */
103
e2cae7e3 104function formatDate($_text) {
105 return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text));
106}
107
108function fancyDate($stamp) {
109 $today = intval(time() / (24*3600));
110 $dday = intval($stamp / (24*3600));
111
112 if ($today == $dday) {
113 $format = "%H:%M";
114 } elseif ($today == 1 + $dday) {
0a65ec9d 115 $format = _b_('hier')." %H:%M";
e2cae7e3 116 } elseif ($today < 7 + $dday) {
1248ebac 117 $format = '%a %H:%M';
e2cae7e3 118 } else {
119 $format = '%a %e %b';
120 }
121 return strftime($format, $stamp);
122}
123
dd7d1c59 124function formatFrom($text) {
125# From: mark@cbosgd.ATT.COM
126# From: mark@cbosgd.ATT.COM (Mark Horton)
127# From: Mark Horton <mark@cbosgd.ATT.COM>
128 $mailto = '<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;';
129
130 $result = htmlentities($text);
131 if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) {
132 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[1]."&#64;".$regs[2])."</a>";
133 }
134 if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) {
135 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[3])."</a>";
136 }
137 if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) {
138 $result="$mailto{$regs[2]}&#64;{$regs[3]}\">".htmlentities($regs[1])."</a>";
139 }
140 return preg_replace("/\\\(\(|\))/","\\1",$result);
141}
142
f78f34d2 143function displayshortcuts($first = -1) {
41cf00eb 144 global $banana;
8d99c683 145 extract($banana->state);
146
559be3d6 147 $res = '<div class="banana_scuts">';
8d99c683 148 $res .= '[<a href="?">'._b_('Liste des forums').'</a>] ';
149 if (is_null($group)) {
4cc7f778 150 return $res.'[<a href="?subscribe=1">'._b_('Abonnements').'</a>]</div>';
8d99c683 151 }
152
153 $res .= "[<a href=\"?group=$group\">$group</a>] ";
154
155 if (is_null($artid)) {
156 $res .= "[<a href=\"?group=$group&amp;action=new\">"._b_('Nouveau message')."</a>] ";
157 if (sizeof($banana->spool->overview)>$banana->tmax) {
158 $res .= '<br />';
159 $n = intval(log(count($banana->spool->overview), 10))+1;
160 for ($ndx=1; $ndx <= sizeof($banana->spool->overview); $ndx += $banana->tmax) {
161 if ($first==$ndx) {
162 $fmt = "[%0{$n}u-%0{$n}u] ";
163 } else {
164 $fmt = "[<a href=\"?group=$group&amp;first=$ndx\">%0{$n}u-%0{$n}u</a>] ";
f78f34d2 165 }
8d99c683 166 $res .= sprintf($fmt, $ndx, min($ndx+$banana->tmax-1,sizeof($banana->spool->overview)));
f78f34d2 167 }
8d99c683 168 }
169 } else {
170 $res .= "[<a href=\"?group=$group&amp;artid=$artid&amp;action=new\">"
171 ._b_('Répondre')."</a>] ";
172 if ($banana->post->checkcancel()) {
173 $res .= "[<a href=\"?group=$group&amp;artid=$artid&amp;action=cancel\">"
174 ._b_('Annuler ce message')."</a>] ";
175 }
f78f34d2 176 }
8d99c683 177 return $res.'</div>';
f78f34d2 178}
179
180/********************************************************************************
181 * FORMATTING STUFF : BODY
182 */
183
2dbc0167 184function wrap($text, $_prefix="")
cced14b6 185{
dd7d1c59 186 $parts = preg_split("/\n-- ?\n/", $text);
276debfc 187 if (count($parts) >1) {
188 $sign = "\n-- \n" . array_pop($parts);
189 $text = join("\n-- \n", $parts);
3ee590a9 190 } else {
276debfc 191 $sign = '';
dd7d1c59 192 $text = $text;
3ee590a9 193 }
2dbc0167 194
195 global $banana;
196 $length = $banana->wrap;
197 $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length, all=>1 };'";
f8e23519 198 exec($cmd, $result);
3ee590a9 199
f8e23519 200 return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign);
3ee590a9 201}
202
dd7d1c59 203function formatbody($_text) {
2dbc0167 204 $res = "\n\n" . htmlentities(wrap($_text, ""))."\n\n";
dd7d1c59 205 $res = preg_replace("/(&lt;|&gt;|&quot;)/", " \\1 ", $res);
206 $res = preg_replace('/(["\[])?((https?|ftp|news):\/\/[a-z@0-9.~%$£µ&i#\-+=_\/\?]*)(["\]])?/i', "\\1<a href=\"\\2\">\\2</a>\\4", $res);
207 $res = preg_replace("/ (&lt;|&gt;|&quot;) /", "\\1", $res);
208
209 $parts = preg_split("/\n-- ?\n/", $res);
210
211 if (count($parts) > 1) {
212 $sign = "</pre><hr style='width: 100%; margin: 1em 0em; ' /><pre>" . array_pop($parts);
213 return join("\n-- \n", $parts).$sign;
214 } else {
215 return $res;
216 }
cced14b6 217}
218
3ee590a9 219?>