dafc16351a633e849dab5e77a2e9a241c942876a
[banana.git] / include / 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 mtime()
15 {
16 global $time;
17 list($usec, $sec) = explode(" ", microtime());
18 $time[] = ((float)$usec + (float)$sec);
19 }
20
21 mtime();
22
23 function _b_($str) { return utf8_decode(dgettext('banana', utf8_encode($str))); }
24
25 /********************************************************************************
26 * HEADER STUFF
27 */
28
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);
33 }
34
35 function headerDecode($value) {
36 $val = preg_replace('/(=\?[^?]*\?[BQ]\?[^?]*\?=) (=\?[^?]*\?[BQ]\?[^?]*\?=)/', '\1\2', $value);
37 return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val);
38 }
39
40 function header_translate($hdr) {
41 switch ($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');
50 default:
51 if (function_exists('hook_header_translate')) {
52 return hook_header_translate($hdr);
53 }
54 return $hdr;
55 }
56 }
57
58 function formatDisplayHeader($_header,$_text) {
59 global $banana;
60 switch ($_header) {
61 case "date":
62 return formatDate($_text);
63
64 case "followup-to":
65 case "newsgroups":
66 $res = "";
67 $groups = preg_split("/[\t ]*,[\t ]*/",$_text);
68 foreach ($groups as $g) {
69 $res.="<a href='thread.php?group=$g'>$g</a>, ";
70 }
71 return substr($res,0, -2);
72
73 case "from":
74 return formatFrom($_text);
75
76 case "references":
77 $rsl = "";
78 $ndx = 1;
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);
83 $par_ok = Array();
84
85 while ($p) {
86 $par_ok[]=$p;
87 $p = $banana->spool->overview[$p]->parent;
88 }
89 foreach (array_reverse($par_ok) as $p) {
90 $rsl .= "<a href=\"article.php?group={$banana->spool->group}&amp;id=$p\">$ndx</a> ";
91 $ndx++;
92 }
93 return $rsl;
94
95 case "x-face":
96 return '<img src="xface.php?face='.base64_encode($_text).'" alt="x-face" />';
97
98 default:
99 if (function_exists('hook_formatDisplayHeader')) {
100 return hook_formatDisplayHeader($_header, $_text);
101 }
102 return htmlentities($_text);
103 }
104 }
105
106 /********************************************************************************
107 * FORMATTING STUFF
108 */
109
110 function formatDate($_text) {
111 return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text));
112 }
113
114 function fancyDate($stamp) {
115 $today = intval(time() / (24*3600));
116 $dday = intval($stamp / (24*3600));
117
118 if ($today == $dday) {
119 $format = "%H:%M";
120 } elseif ($today == 1 + $dday) {
121 $format = _b_('hier')." %H:%M";
122 } elseif ($today < 7 + $dday) {
123 $format = '%A %H:%M';
124 } else {
125 $format = '%a %e %b';
126 }
127 return strftime($format, $stamp);
128 }
129
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="&#109;&#97;&#105;&#108;&#116;&#111;&#58;';
135
136 $result = htmlentities($text);
137 if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) {
138 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[1]."&#64;".$regs[2])."</a>";
139 }
140 if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) {
141 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[3])."</a>";
142 }
143 if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) {
144 $result="$mailto{$regs[2]}&#64;{$regs[3]}\">".htmlentities($regs[1])."</a>";
145 }
146 return preg_replace("/\\\(\(|\))/","\\1",$result);
147 }
148
149 function displayshortcuts($first = -1) {
150 global $banana;
151 $sname = basename($_SERVER['SCRIPT_NAME']);
152
153 $res = '<div class="banana_scuts">';
154
155 if (function_exists('hook_displayshortcuts')) {
156 $res .= hook_displayshortcuts($sname, $first);
157 } else {
158 $res .= '[<a href="disconnect.php">'._b_('Déconnexion').'</a>] ';
159 }
160
161 switch ($sname) {
162 case 'thread.php' :
163 $res .= '[<a href="index.php">'._b_('Liste des forums').'</a>] ';
164 $res .= "[<a href=\"post.php?group={$banana->spool->group}\">"._b_('Nouveau message')."</a>] ";
165 if (sizeof($banana->spool->overview)>$banana->tmax) {
166 for ($ndx=1; $ndx<=sizeof($banana->spool->overview); $ndx += $banana->tmax) {
167 if ($first==$ndx) {
168 $res .= "[$ndx-".min($ndx+$banana->tmax-1,sizeof($banana->spool->overview))."] ";
169 } else {
170 $res .= "[<a href=\"?group={$banana->spool->group}&amp;first="
171 ."$ndx\">$ndx-".min($ndx+$banana->tmax-1,sizeof($banana->spool->overview))
172 ."</a>] ";
173 }
174 }
175 }
176 break;
177 case 'article.php' :
178 $res .= '[<a href="index.php">'._b_('Liste des forums').'</a>] ';
179 $res .= "[<a href=\"thread.php?group={$banana->spool->group}\">{$banana->spool->group}</a>] ";
180 $res .= "[<a href=\"post.php?group={$banana->spool->group}&amp;id={$banana->post->id}&amp;type=followup\">"
181 ._b_('Répondre')."</a>] ";
182 if ($banana->post->checkcancel()) {
183 $res .= "[<a href=\"article.php?group={$banana->spool->group}&amp;id={$banana->post->id}&amp;type=cancel\">"
184 ._b_('Annuler ce message')."</a>] ";
185 }
186 break;
187 case 'post.php' :
188 $res .= '[<a href="index.php">'._b_('Liste des forums').'</a>] ';
189 $res .= "[<a href=\"thread.php?group={$banana->spool->group}\">{$banana->spool->group}</a>]";
190 break;
191 }
192 $res .= '</div>';
193
194 return $res;
195 }
196
197 /********************************************************************************
198 * FORMATTING STUFF : BODY
199 */
200
201 function wrap($text, $_prefix="")
202 {
203 $parts = preg_split("/\n-- ?\n/", $text);
204 if (count($parts) >1) {
205 $sign = "\n-- \n" . array_pop($parts);
206 $text = join("\n-- \n", $parts);
207 } else {
208 $sign = '';
209 $text = $text;
210 }
211
212 global $banana;
213 $length = $banana->wrap;
214 $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length, all=>1 };'";
215 exec($cmd, $result);
216
217 return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign);
218 }
219
220 function formatbody($_text) {
221 $res = "\n\n" . htmlentities(wrap($_text, ""))."\n\n";
222 $res = preg_replace("/(&lt;|&gt;|&quot;)/", " \\1 ", $res);
223 $res = preg_replace('/(["\[])?((https?|ftp|news):\/\/[a-z@0-9.~%$£µ&i#\-+=_\/\?]*)(["\]])?/i', "\\1<a href=\"\\2\">\\2</a>\\4", $res);
224 $res = preg_replace("/ (&lt;|&gt;|&quot;) /", "\\1", $res);
225
226 $parts = preg_split("/\n-- ?\n/", $res);
227
228 if (count($parts) > 1) {
229 $sign = "</pre><hr style='width: 100%; margin: 1em 0em; ' /><pre>" . array_pop($parts);
230 return join("\n-- \n", $parts).$sign;
231 } else {
232 return $res;
233 }
234 }
235
236 ?>