* Supprime du code inutile
[banana.git] / banana / misc.inc.php
CommitLineData
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
14function _b_($str) { return utf8_decode(dgettext('banana', utf8_encode($str))); }
15
16function to_entities($str) {
39816f8b 17 require_once dirname(__FILE__).'/utf8.php';
78cd27b3 18 return utf8entities(htmlentities($str, ENT_NOQUOTES, 'UTF-8'));
19}
20
21function is_utf8($s) { return iconv('utf-8', 'utf-8', $s) == $s; }
22
1f75b135 23function 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 */
43function removeEvilTags($source)
44{
aef14768 45 $allowedTags = '<h1><b><i><a><ul><li><pre><hr><blockquote><img><br><font><p><small><big><sup><sub><code>';
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 */
55function 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 */
64function htmlToPlainText($res)
65{
66 $res = trim(html_entity_decode(strip_tags($res, '<br>')));
67 $res = preg_replace("@<br[^>]>@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 */
80function 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', '&lt;', $source);
107 $source = preg_replace('@<comment[^>]*>((?:[^<]|<(?!/comment>))*)</comment>@i', '<!-- \1 -->', $source);
108 return removeEvilAttributes($source);
109}
110
78cd27b3 111/********************************************************************************
112 * HEADER STUFF
113 */
114
115function _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
121function headerDecode($value) {
122 $val = preg_replace('/(=\?[^?]*\?[BQ]\?[^?]*\?=) (=\?[^?]*\?[BQ]\?[^?]*\?=)/', '\1\2', $value);
123 return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val);
124}
125
126function 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
135function 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
154function 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}&amp;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
208function formatDate($_text) {
209 return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text));
210}
211
212function 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
228function 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="&#109;&#97;&#105;&#108;&#116;&#111;&#58;';
233
234 $result = htmlentities($text);
235 if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) {
236 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[1]."&#64;".$regs[2])."</a>";
237 }
238 if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) {
239 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[3])."</a>";
240 }
241 if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) {
242 $result="$mailto{$regs[2]}&#64;{$regs[3]}\">".htmlentities($regs[1])."</a>";
243 }
244 return preg_replace("/\\\(\(|\))/","\\1",$result);
245}
246
247function 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&amp;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&amp;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&amp;artid=$artid&amp;action=new\">"
275 ._b_('Répondre')."</a>] ";
276 if ($banana->post->checkcancel()) {
277 $res .= "[<a href=\"?group=$group&amp;artid=$artid&amp;action=cancel\">"
278 ._b_('Annuler ce message')."</a>] ";
279 }
280 }
281 return $res.'</div>';
282}
283
284/********************************************************************************
285 * FORMATTING STUFF : BODY
286 */
287
288function 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 307function 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("/(&lt;|&gt;|&quot;)/", " \\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("/ (&lt;|&gt;|&quot;) /", "\\1", $res);
1f75b135 320
321 if ($format == 'html') {
11102461 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);
325 } else {
326 $parts = preg_split("/\n-- ?\n/", $res);
1f75b135 327 }
328
78cd27b3 329 if (count($parts) > 1) {
11102461 330 $sign = array_pop($parts);
331 if ($format == 'html') {
332 $res = join('<br/>-- <br/>', $parts);
333 $sign = '<hr style="width: 100%; margin: 1em 0em; " />'.$sign;
334 } else {
335 $res = join('\n-- \n', $parts);
336 $sign = '</pre><hr style="width: 100%; margin: 1em 0em; " /><pre>'.$sign;
337 }
338 return $res.$sign;
78cd27b3 339 } else {
340 return $res;
341 }
342}
343
344?>