78cd27b3 |
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 | function to_entities($str) { |
39816f8b |
17 | require_once dirname(__FILE__).'/utf8.php'; |
78cd27b3 |
18 | return utf8entities(htmlentities($str, ENT_NOQUOTES, 'UTF-8')); |
19 | } |
20 | |
21 | function is_utf8($s) { return iconv('utf-8', 'utf-8', $s) == $s; } |
22 | |
1f75b135 |
23 | function textFormat_translate($format) |
24 | { |
25 | switch (strtolower($format)) { |
26 | case 'plain': return _b_('Texte brut'); |
27 | case 'richtext': return _b_('Texte enrichi'); |
28 | case 'html': return _b_('HTML'); |
29 | default: return $format; |
30 | } |
31 | } |
32 | |
33 | /******************************************************************************** |
34 | * HTML STUFF |
35 | * Taken from php.net |
36 | */ |
37 | |
aef14768 |
38 | /** |
1f75b135 |
39 | * @return string |
40 | * @param string |
41 | * @desc Strip forbidden tags and delegate tag-source check to removeEvilAttributes() |
42 | */ |
43 | function removeEvilTags($source) |
44 | { |
f12fdb59 |
45 | $allowedTags = '<h1><b><i><a><ul><li><pre><hr><blockquote><img><br><font><p><small><big><sup><sub><code><em>'; |
1f75b135 |
46 | $source = strip_tags($source, $allowedTags); |
47 | return preg_replace('/<(.*?)>/ie', "'<'.removeEvilAttributes('\\1').'>'", $source); |
48 | } |
49 | |
50 | /** |
51 | * @return string |
52 | * @param string |
53 | * @desc Strip forbidden attributes from a tag |
54 | */ |
55 | function removeEvilAttributes($tagSource) |
56 | { |
57 | $stripAttrib = 'javascript:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|'. |
58 | 'onmousemove|onmouseout|onkeypress|onkeydown|onkeyup'; |
59 | return stripslashes(preg_replace("/$stripAttrib/i", '', $tagSource)); |
60 | } |
61 | |
aef14768 |
62 | /** Convert html to plain text |
63 | */ |
64 | function htmlToPlainText($res) |
65 | { |
f12fdb59 |
66 | $res = trim(html_entity_decode(strip_tags($res, '<br><p>'))); |
67 | $res = preg_replace("@</?(br|p)[^>]*>@i", "\n", $res); |
4cc80c9a |
68 | if (!is_utf8($res)) { |
69 | $res = utf8_encode($res); |
70 | } |
aef14768 |
71 | return $res; |
72 | } |
73 | |
74 | /******************************************************************************** |
75 | * RICHTEXT STUFF |
76 | */ |
77 | |
78 | /** Convert richtext to html |
79 | */ |
80 | function richtextToHtml($source) |
81 | { |
82 | $tags = Array('bold' => 'b', |
83 | 'italic' => 'i', |
84 | 'smaller' => 'small', |
85 | 'bigger' => 'big', |
86 | 'underline' => 'u', |
87 | 'subscript' => 'sub', |
88 | 'superscript' => 'sup', |
89 | 'excerpt' => 'blockquote', |
90 | 'paragraph' => 'p', |
91 | 'nl' => 'br' |
92 | ); |
93 | |
94 | // clean unsupported tags |
95 | $protectedTags = '<signature><lt><comment><'.join('><', array_keys($tags)).'>'; |
96 | $source = strip_tags($source, $protectedTags); |
97 | |
98 | // convert richtext tags to html |
99 | foreach (array_keys($tags) as $tag) { |
100 | $source = preg_replace('@(</?)'.$tag.'([^>]*>)@i', '\1'.$tags[$tag].'\2', $source); |
101 | } |
102 | |
103 | // some special cases |
104 | $source = preg_replace('@<signature>@i', '<br>-- <br>', $source); |
105 | $source = preg_replace('@</signature>@i', '', $source); |
106 | $source = preg_replace('@<lt>@i', '<', $source); |
107 | $source = preg_replace('@<comment[^>]*>((?:[^<]|<(?!/comment>))*)</comment>@i', '<!-- \1 -->', $source); |
108 | return removeEvilAttributes($source); |
109 | } |
110 | |
78cd27b3 |
111 | /******************************************************************************** |
112 | * HEADER STUFF |
113 | */ |
114 | |
115 | function _headerdecode($charset, $c, $str) { |
116 | $s = ($c == 'Q') ? quoted_printable_decode($str) : base64_decode($str); |
117 | $s = iconv($charset, 'iso-8859-15', $s); |
118 | return str_replace('_', ' ', $s); |
119 | } |
120 | |
121 | function headerDecode($value) { |
122 | $val = preg_replace('/(=\?[^?]*\?[BQ]\?[^?]*\?=) (=\?[^?]*\?[BQ]\?[^?]*\?=)/', '\1\2', $value); |
123 | return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val); |
124 | } |
125 | |
126 | function headerEncode($value, $trim = 0) { |
127 | if ($trim) { |
128 | if (strlen($value) > $trim) { |
129 | $value = substr($value, 0, $trim) . "[...]"; |
130 | } |
131 | } |
132 | return "=?UTF-8?B?".base64_encode($value)."?="; |
133 | } |
134 | |
135 | function header_translate($hdr) { |
136 | switch ($hdr) { |
137 | case 'from': return _b_('De'); |
138 | case 'subject': return _b_('Sujet'); |
139 | case 'newsgroups': return _b_('Forums'); |
140 | case 'followup-to': return _b_('Suivi-à'); |
141 | case 'date': return _b_('Date'); |
142 | case 'organization': return _b_('Organisation'); |
143 | case 'references': return _b_('Références'); |
144 | case 'x-face': return _b_('Image'); |
145 | default: |
146 | if (function_exists('hook_headerTranslate') |
147 | && $res = hook_headerTranslate($hdr)) { |
148 | return $res; |
149 | } |
150 | return $hdr; |
151 | } |
152 | } |
153 | |
154 | function formatDisplayHeader($_header,$_text) { |
155 | global $banana; |
156 | switch ($_header) { |
157 | case "date": |
158 | return formatDate($_text); |
159 | |
160 | case "followup-to": |
161 | case "newsgroups": |
162 | $res = ""; |
163 | $groups = preg_split("/[\t ]*,[\t ]*/",$_text); |
164 | foreach ($groups as $g) { |
165 | $res.="<a href='?group=$g'>$g</a>, "; |
166 | } |
167 | return substr($res,0, -2); |
168 | |
169 | case "from": |
170 | return formatFrom($_text); |
171 | |
172 | case "references": |
173 | $rsl = ""; |
174 | $ndx = 1; |
175 | $text = str_replace("><","> <",$_text); |
176 | $text = preg_split("/[ \t]/",strtr($text,$banana->spool->ids)); |
177 | $parents = preg_grep("/^\d+$/",$text); |
178 | $p = array_pop($parents); |
179 | $par_ok = Array(); |
180 | |
181 | while ($p) { |
182 | $par_ok[]=$p; |
183 | $p = $banana->spool->overview[$p]->parent; |
184 | } |
185 | foreach (array_reverse($par_ok) as $p) { |
186 | $rsl .= "<a href=\"?group={$banana->spool->group}&artid=$p\">$ndx</a> "; |
187 | $ndx++; |
188 | } |
189 | return $rsl; |
190 | |
191 | case "x-face": |
192 | return '<img src="xface.php?face='.base64_encode($_text).'" alt="x-face" />'; |
193 | |
194 | default: |
195 | if (function_exists('hook_formatDisplayHeader') |
196 | && $res = hook_formatDisplayHeader($_header, $_text)) |
197 | { |
198 | return $res; |
199 | } |
200 | return htmlentities($_text); |
201 | } |
202 | } |
203 | |
204 | /******************************************************************************** |
205 | * FORMATTING STUFF |
206 | */ |
207 | |
208 | function formatDate($_text) { |
209 | return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text)); |
210 | } |
211 | |
212 | function fancyDate($stamp) { |
213 | $today = intval(time() / (24*3600)); |
214 | $dday = intval($stamp / (24*3600)); |
215 | |
216 | if ($today == $dday) { |
217 | $format = "%H:%M"; |
218 | } elseif ($today == 1 + $dday) { |
219 | $format = _b_('hier')." %H:%M"; |
220 | } elseif ($today < 7 + $dday) { |
221 | $format = '%a %H:%M'; |
222 | } else { |
223 | $format = '%a %e %b'; |
224 | } |
225 | return strftime($format, $stamp); |
226 | } |
227 | |
228 | function formatFrom($text) { |
229 | # From: mark@cbosgd.ATT.COM |
230 | # From: mark@cbosgd.ATT.COM (Mark Horton) |
231 | # From: Mark Horton <mark@cbosgd.ATT.COM> |
232 | $mailto = '<a href="mailto:'; |
233 | |
234 | $result = htmlentities($text); |
235 | if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) { |
236 | $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[1]."@".$regs[2])."</a>"; |
237 | } |
238 | if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) { |
239 | $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[3])."</a>"; |
240 | } |
241 | if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) { |
242 | $result="$mailto{$regs[2]}@{$regs[3]}\">".htmlentities($regs[1])."</a>"; |
243 | } |
244 | return preg_replace("/\\\(\(|\))/","\\1",$result); |
245 | } |
246 | |
247 | function displayshortcuts($first = -1) { |
248 | global $banana; |
249 | extract($banana->state); |
250 | |
251 | $res = '<div class="banana_scuts">'; |
252 | $res .= '[<a href="?">'._b_('Liste des forums').'</a>] '; |
253 | if (is_null($group)) { |
254 | return $res.'[<a href="?subscribe=1">'._b_('Abonnements').'</a>]</div>'; |
255 | } |
256 | |
257 | $res .= "[<a href=\"?group=$group\">$group</a>] "; |
258 | |
259 | if (is_null($artid)) { |
260 | $res .= "[<a href=\"?group=$group&action=new\">"._b_('Nouveau message')."</a>] "; |
261 | if (sizeof($banana->spool->overview)>$banana->tmax) { |
262 | $res .= '<br />'; |
263 | $n = intval(log(count($banana->spool->overview), 10))+1; |
264 | for ($ndx=1; $ndx <= sizeof($banana->spool->overview); $ndx += $banana->tmax) { |
265 | if ($first==$ndx) { |
266 | $fmt = "[%0{$n}u-%0{$n}u] "; |
267 | } else { |
268 | $fmt = "[<a href=\"?group=$group&first=$ndx\">%0{$n}u-%0{$n}u</a>] "; |
269 | } |
270 | $res .= sprintf($fmt, $ndx, min($ndx+$banana->tmax-1,sizeof($banana->spool->overview))); |
271 | } |
272 | } |
273 | } else { |
274 | $res .= "[<a href=\"?group=$group&artid=$artid&action=new\">" |
cfaab21d |
275 | ._b_('Répondre')."</a>] "; |
f2dab64a |
276 | if ($banana->post && $banana->post->checkcancel()) { |
78cd27b3 |
277 | $res .= "[<a href=\"?group=$group&artid=$artid&action=cancel\">" |
cfaab21d |
278 | ._b_('Annuler ce message')."</a>] "; |
78cd27b3 |
279 | } |
280 | } |
281 | return $res.'</div>'; |
282 | } |
283 | |
284 | /******************************************************************************** |
285 | * FORMATTING STUFF : BODY |
286 | */ |
287 | |
288 | function wrap($text, $_prefix="") |
289 | { |
290 | $parts = preg_split("/\n-- ?\n/", $text); |
291 | if (count($parts) >1) { |
292 | $sign = "\n-- \n" . array_pop($parts); |
293 | $text = join("\n-- \n", $parts); |
294 | } else { |
295 | $sign = ''; |
296 | $text = $text; |
297 | } |
298 | |
299 | global $banana; |
300 | $length = $banana->wrap; |
301 | $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length, all=>1 };'"; |
302 | exec($cmd, $result); |
303 | |
304 | return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign); |
305 | } |
306 | |
1f75b135 |
307 | function formatbody($_text, $format='plain') |
308 | { |
309 | if ($format == 'html') { |
4cc80c9a |
310 | $res = '<br/>'.removeEvilTags($_text).'<br/>'; |
aef14768 |
311 | } else if ($format == 'richtext') { |
4cc80c9a |
312 | $res = '<br/>'.richtextToHtml($_text).'<br/>'; |
aef14768 |
313 | $format = 'html'; |
1f75b135 |
314 | } else { |
315 | $res = "\n\n" . to_entities(wrap($_text, ""))."\n\n"; |
316 | } |
78cd27b3 |
317 | $res = preg_replace("/(<|>|")/", " \\1 ", $res); |
318 | $res = preg_replace('/(["\[])?((https?|ftp|news):\/\/[a-z@0-9.~%$£µ&i#\-+=_\/\?]*)(["\]])?/i', "\\1<a href=\"\\2\">\\2</a>\\4", $res); |
319 | $res = preg_replace("/ (<|>|") /", "\\1", $res); |
1f75b135 |
320 | |
321 | if ($format == 'html') { |
cfaab21d |
322 | $res = preg_replace("@(</p>)\n?-- \n?(<p[^>]*>|<br[^>]*>)@", "\\1<br/>-- \\2", $res); |
323 | $res = preg_replace("@<br[^>]*>\n?-- \n?(<p[^>]*>)@", "<br/>-- <br/>\\2", $res); |
324 | $parts = preg_split("@(:?<p[^>]*>\n?-- \n?</p>|<br[^>]*>\n?-- \n?<br[^>]*>)@", $res); |
11102461 |
325 | } else { |
cfaab21d |
326 | while (preg_match("@(^|<blockquote[^>]*>|\n)>@i", $res)) { |
327 | $res = preg_replace("@(^|<blockquote[^>]*>|\n)((>[^\n]*\n)+)@ie", |
328 | "'\\1<blockquote type=\'cite\'>'.preg_replace('@(^|<blockquote[^>]*>|\n)>[ \\t\\r]*@i', '\\1', '\\2').'</blockquote>'", |
329 | $res); |
330 | } |
331 | $res = preg_replace("@(<blockquote[^>]*>)\n@", '\1', $res); |
11102461 |
332 | $parts = preg_split("/\n-- ?\n/", $res); |
1f75b135 |
333 | } |
334 | |
78cd27b3 |
335 | if (count($parts) > 1) { |
11102461 |
336 | $sign = array_pop($parts); |
337 | if ($format == 'html') { |
338 | $res = join('<br/>-- <br/>', $parts); |
cfaab21d |
339 | $sign = '<hr style="width: 100%; margin: 1em 0em; " />'.$sign.'<br/>'; |
11102461 |
340 | } else { |
341 | $res = join('\n-- \n', $parts); |
342 | $sign = '</pre><hr style="width: 100%; margin: 1em 0em; " /><pre>'.$sign; |
343 | } |
344 | return $res.$sign; |
78cd27b3 |
345 | } else { |
346 | return $res; |
347 | } |
348 | } |
349 | |
350 | ?> |