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