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 | |
65d96b1f |
14 | function mtime() |
15 | { |
16 | global $time; |
17 | list($usec, $sec) = explode(" ", microtime()); |
18 | $time[] = ((float)$usec + (float)$sec); |
19 | } |
20 | |
21 | mtime(); |
22 | |
0ca6e016 |
23 | function _b_($str) { return utf8_decode(dgettext('banana', utf8_encode($str))); } |
0a65ec9d |
24 | |
73d5bf46 |
25 | /******************************************************************************** |
26 | * HEADER STUFF |
27 | */ |
28 | |
dd7d1c59 |
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 | |
e2cae7e3 |
40 | function header_translate($hdr) { |
d4c19591 |
41 | switch ($hdr) { |
0a65ec9d |
42 | case 'from': return _b_('De'); |
43 | case 'subject': return _b_('Sujet'); |
44 | case 'newsgroups': return _b_('Forums'); |
d4c19591 |
45 | case 'followup-to': return _b_('Suivi-à'); |
0a65ec9d |
46 | case 'date': return _b_('Date'); |
47 | case 'organization': return _b_('Organisation'); |
48 | case 'references': return _b_('Références'); |
d4c19591 |
49 | case 'x-face': return _b_('Image'); |
e2cae7e3 |
50 | default: |
2dbc0167 |
51 | if (function_exists('hook_header_translate')) { |
52 | return hook_header_translate($hdr); |
53 | } |
e2cae7e3 |
54 | return $hdr; |
55 | } |
56 | } |
57 | |
2dbc0167 |
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); |
58d1740e |
83 | $par_ok = Array(); |
2dbc0167 |
84 | |
85 | while ($p) { |
58d1740e |
86 | $par_ok[]=$p; |
2dbc0167 |
87 | $p = $banana->spool->overview[$p]->parent; |
88 | } |
58d1740e |
89 | foreach (array_reverse($par_ok) as $p) { |
2dbc0167 |
90 | $rsl .= "<a href=\"article.php?group={$banana->spool->group}&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 | |
73d5bf46 |
106 | /******************************************************************************** |
107 | * FORMATTING STUFF |
108 | */ |
109 | |
e2cae7e3 |
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) { |
0a65ec9d |
121 | $format = _b_('hier')." %H:%M"; |
e2cae7e3 |
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 | |
dd7d1c59 |
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="mailto:'; |
135 | |
136 | $result = htmlentities($text); |
137 | if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) { |
138 | $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[1]."@".$regs[2])."</a>"; |
139 | } |
140 | if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) { |
141 | $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[3])."</a>"; |
142 | } |
143 | if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) { |
144 | $result="$mailto{$regs[2]}@{$regs[3]}\">".htmlentities($regs[1])."</a>"; |
145 | } |
146 | return preg_replace("/\\\(\(|\))/","\\1",$result); |
147 | } |
148 | |
f78f34d2 |
149 | function displayshortcuts($first = -1) { |
150 | global $banana, $css; |
151 | $sname = basename($_SERVER['SCRIPT_NAME']); |
152 | |
65d96b1f |
153 | $res = '<div class="banana_scuts">'; |
f78f34d2 |
154 | |
155 | if (function_exists('hook_displayshortcuts')) { |
65d96b1f |
156 | $res .= hook_displayshortcuts($sname, $first); |
f78f34d2 |
157 | } else { |
65d96b1f |
158 | $res .= '[<a href="disconnect.php">'._b_('Déconnexion').'</a>] '; |
f78f34d2 |
159 | } |
160 | |
161 | switch ($sname) { |
162 | case 'thread.php' : |
65d96b1f |
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>] "; |
f78f34d2 |
165 | if (sizeof($banana->spool->overview)>$banana->tmax) { |
166 | for ($ndx=1; $ndx<=sizeof($banana->spool->overview); $ndx += $banana->tmax) { |
167 | if ($first==$ndx) { |
65d96b1f |
168 | $res .= "[$ndx-".min($ndx+$banana->tmax-1,sizeof($banana->spool->overview))."] "; |
f78f34d2 |
169 | } else { |
65d96b1f |
170 | $res .= "[<a href=\"?group={$banana->spool->group}&first=" |
f78f34d2 |
171 | ."$ndx\">$ndx-".min($ndx+$banana->tmax-1,sizeof($banana->spool->overview)) |
172 | ."</a>] "; |
173 | } |
174 | } |
175 | } |
176 | break; |
177 | case 'article.php' : |
65d96b1f |
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}&id={$banana->post->id}&type=followup\">" |
f78f34d2 |
181 | ._b_('Répondre')."</a>] "; |
182 | if ($banana->post->checkcancel()) { |
65d96b1f |
183 | $res .= "[<a href=\"article.php?group={$banana->spool->group}&id={$banana->post->id}&type=cancel\">" |
f78f34d2 |
184 | ._b_('Annuler ce message')."</a>] "; |
185 | } |
186 | break; |
187 | case 'post.php' : |
65d96b1f |
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>]"; |
f78f34d2 |
190 | break; |
191 | } |
65d96b1f |
192 | $res .= '</div>'; |
193 | |
194 | return $res; |
f78f34d2 |
195 | } |
196 | |
197 | /******************************************************************************** |
198 | * FORMATTING STUFF : BODY |
199 | */ |
200 | |
2dbc0167 |
201 | function wrap($text, $_prefix="") |
cced14b6 |
202 | { |
dd7d1c59 |
203 | $parts = preg_split("/\n-- ?\n/", $text); |
276debfc |
204 | if (count($parts) >1) { |
205 | $sign = "\n-- \n" . array_pop($parts); |
206 | $text = join("\n-- \n", $parts); |
3ee590a9 |
207 | } else { |
276debfc |
208 | $sign = ''; |
dd7d1c59 |
209 | $text = $text; |
3ee590a9 |
210 | } |
2dbc0167 |
211 | |
212 | global $banana; |
213 | $length = $banana->wrap; |
214 | $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length, all=>1 };'"; |
f8e23519 |
215 | exec($cmd, $result); |
3ee590a9 |
216 | |
f8e23519 |
217 | return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign); |
3ee590a9 |
218 | } |
219 | |
dd7d1c59 |
220 | function formatbody($_text) { |
2dbc0167 |
221 | $res = "\n\n" . htmlentities(wrap($_text, ""))."\n\n"; |
dd7d1c59 |
222 | $res = preg_replace("/(<|>|")/", " \\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("/ (<|>|") /", "\\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 | } |
cced14b6 |
234 | } |
235 | |
3ee590a9 |
236 | ?> |