read changelog
[banana.git] / banana / misc.inc.php
1 <?php
2 /********************************************************************************
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 /********************************************************************************
11 * MISC
12 */
13
14 function _b_($str) { return utf8_decode(dgettext('banana', utf8_encode($str))); }
15
16 /********************************************************************************
17 * HEADER STUFF
18 */
19
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);
24 }
25
26 function headerDecode($value) {
27 $val = preg_replace('/(=\?[^?]*\?[BQ]\?[^?]*\?=) (=\?[^?]*\?[BQ]\?[^?]*\?=)/', '\1\2', $value);
28 return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val);
29 }
30
31 function header_translate($hdr) {
32 switch ($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');
41 default:
42 if (function_exists('hook_headerTranslate')
43 && $res = hook_headerTranslate($hdr)) {
44 return $res;
45 }
46 return $hdr;
47 }
48 }
49
50 function 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) {
61 $res.="<a href='?group=$g'>$g</a>, ";
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);
75 $par_ok = Array();
76
77 while ($p) {
78 $par_ok[]=$p;
79 $p = $banana->spool->overview[$p]->parent;
80 }
81 foreach (array_reverse($par_ok) as $p) {
82 $rsl .= "<a href=\"?group={$banana->spool->group}&amp;artid=$p\">$ndx</a> ";
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:
91 if (function_exists('hook_formatDisplayHeader')
92 && $res = hook_formatDisplayHeader($_header, $_text))
93 {
94 return $res;
95 }
96 return htmlentities($_text);
97 }
98 }
99
100 /********************************************************************************
101 * FORMATTING STUFF
102 */
103
104 function formatDate($_text) {
105 return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text));
106 }
107
108 function 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) {
115 $format = _b_('hier')." %H:%M";
116 } elseif ($today < 7 + $dday) {
117 $format = '%a %H:%M';
118 } else {
119 $format = '%a %e %b';
120 }
121 return strftime($format, $stamp);
122 }
123
124 function 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
143 function displayshortcuts($first = -1) {
144 global $banana;
145 extract($banana->state);
146
147 $res = '<div class="banana_scuts">';
148 $res .= '[<a href="?">'._b_('Liste des forums').'</a>] ';
149 if (is_null($group)) {
150 return $res.'[<a href="?subscribe=1">'._b_('Abonnements').'</a>]</div>';
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>] ";
165 }
166 $res .= sprintf($fmt, $ndx, min($ndx+$banana->tmax-1,sizeof($banana->spool->overview)));
167 }
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 }
176 }
177 return $res.'</div>';
178 }
179
180 /********************************************************************************
181 * FORMATTING STUFF : BODY
182 */
183
184 function wrap($text, $_prefix="")
185 {
186 $parts = preg_split("/\n-- ?\n/", $text);
187 if (count($parts) >1) {
188 $sign = "\n-- \n" . array_pop($parts);
189 $text = join("\n-- \n", $parts);
190 } else {
191 $sign = '';
192 $text = $text;
193 }
194
195 global $banana;
196 $length = $banana->wrap;
197 $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length, all=>1 };'";
198 exec($cmd, $result);
199
200 return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign);
201 }
202
203 function formatbody($_text) {
204 $res = "\n\n" . htmlentities(wrap($_text, ""))."\n\n";
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 }
217 }
218
219 ?>