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