=?utf-8?q?Bon=20en=20fait=20skinnage=20des=20blockquote=20par=20la=20CSS
[banana.git] / banana / misc.inc.php
CommitLineData
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 14function _b_($str) { return utf8_decode(dgettext('banana', utf8_encode($str))); }
0a65ec9d 15
987299b4 16function to_entities($str) {
6918d66a 17 require_once dirname(__FILE__).'/utf8.php';
987299b4 18 return utf8entities(htmlentities($str, ENT_NOQUOTES, 'UTF-8'));
382606fb
PHM
19}
20
c42efe2f
PHM
21function is_utf8($s) { return iconv('utf-8', 'utf-8', $s) == $s; }
22
8f6f50fb 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
5d133234 38/**
8f6f50fb 39 * @return string
40 * @param string
41 * @desc Strip forbidden tags and delegate tag-source check to removeEvilAttributes()
42 */
43function removeEvilTags($source)
44{
73ab762c 45 $allowedTags = '<h1><b><i><a><ul><li><pre><hr><blockquote><img><br><font><p><small><big><sup><sub><code><em>';
8f6f50fb 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
5d133234 62/** Convert html to plain text
63 */
64function htmlToPlainText($res)
65{
73ab762c 66 $res = trim(html_entity_decode(strip_tags($res, '<br><p>')));
67 $res = preg_replace("@</?(br|p)[^>]*>@i", "\n", $res);
e27ae1a3 68 if (!is_utf8($res)) {
69 $res = utf8_encode($res);
70 }
5d133234 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
73d5bf46 111/********************************************************************************
112 * HEADER STUFF
113 */
114
dd7d1c59 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
9cf3f056
PHM
126function headerEncode($value, $trim = 0) {
127 if ($trim) {
d752880f
PHM
128 if (strlen($value) > $trim) {
129 $value = substr($value, 0, $trim) . "[...]";
130 }
9cf3f056
PHM
131 }
132 return "=?UTF-8?B?".base64_encode($value)."?=";
133}
134
e2cae7e3 135function header_translate($hdr) {
d4c19591 136 switch ($hdr) {
0a65ec9d 137 case 'from': return _b_('De');
138 case 'subject': return _b_('Sujet');
139 case 'newsgroups': return _b_('Forums');
d4c19591 140 case 'followup-to': return _b_('Suivi-à');
0a65ec9d 141 case 'date': return _b_('Date');
142 case 'organization': return _b_('Organisation');
143 case 'references': return _b_('Références');
d4c19591 144 case 'x-face': return _b_('Image');
e2cae7e3 145 default:
559be3d6 146 if (function_exists('hook_headerTranslate')
147 && $res = hook_headerTranslate($hdr)) {
148 return $res;
2dbc0167 149 }
e2cae7e3 150 return $hdr;
151 }
152}
153
2dbc0167 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) {
8d99c683 165 $res.="<a href='?group=$g'>$g</a>, ";
2dbc0167 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);
58d1740e 179 $par_ok = Array();
2dbc0167 180
181 while ($p) {
58d1740e 182 $par_ok[]=$p;
2dbc0167 183 $p = $banana->spool->overview[$p]->parent;
184 }
58d1740e 185 foreach (array_reverse($par_ok) as $p) {
8d99c683 186 $rsl .= "<a href=\"?group={$banana->spool->group}&amp;artid=$p\">$ndx</a> ";
2dbc0167 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:
559be3d6 195 if (function_exists('hook_formatDisplayHeader')
196 && $res = hook_formatDisplayHeader($_header, $_text))
197 {
198 return $res;
2dbc0167 199 }
200 return htmlentities($_text);
201 }
202}
203
73d5bf46 204/********************************************************************************
205 * FORMATTING STUFF
206 */
207
e2cae7e3 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) {
0a65ec9d 219 $format = _b_('hier')." %H:%M";
e2cae7e3 220 } elseif ($today < 7 + $dday) {
1248ebac 221 $format = '%a %H:%M';
e2cae7e3 222 } else {
223 $format = '%a %e %b';
224 }
225 return strftime($format, $stamp);
226}
227
dd7d1c59 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
f78f34d2 247function displayshortcuts($first = -1) {
41cf00eb 248 global $banana;
8d99c683 249 extract($banana->state);
250
559be3d6 251 $res = '<div class="banana_scuts">';
8d99c683 252 $res .= '[<a href="?">'._b_('Liste des forums').'</a>] ';
253 if (is_null($group)) {
4cc7f778 254 return $res.'[<a href="?subscribe=1">'._b_('Abonnements').'</a>]</div>';
8d99c683 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>] ";
f78f34d2 269 }
8d99c683 270 $res .= sprintf($fmt, $ndx, min($ndx+$banana->tmax-1,sizeof($banana->spool->overview)));
f78f34d2 271 }
8d99c683 272 }
273 } else {
274 $res .= "[<a href=\"?group=$group&amp;artid=$artid&amp;action=new\">"
73afa785 275 ._b_('Répondre')."</a>] ";
940ae667 276 if ($banana->post && $banana->post->checkcancel()) {
8d99c683 277 $res .= "[<a href=\"?group=$group&amp;artid=$artid&amp;action=cancel\">"
73afa785 278 ._b_('Annuler ce message')."</a>] ";
8d99c683 279 }
f78f34d2 280 }
8d99c683 281 return $res.'</div>';
f78f34d2 282}
283
284/********************************************************************************
285 * FORMATTING STUFF : BODY
286 */
287
2dbc0167 288function wrap($text, $_prefix="")
cced14b6 289{
dd7d1c59 290 $parts = preg_split("/\n-- ?\n/", $text);
276debfc 291 if (count($parts) >1) {
292 $sign = "\n-- \n" . array_pop($parts);
293 $text = join("\n-- \n", $parts);
3ee590a9 294 } else {
276debfc 295 $sign = '';
dd7d1c59 296 $text = $text;
3ee590a9 297 }
2dbc0167 298
299 global $banana;
300 $length = $banana->wrap;
301 $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length, all=>1 };'";
f8e23519 302 exec($cmd, $result);
3ee590a9 303
f8e23519 304 return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign);
3ee590a9 305}
306
8f6f50fb 307function formatbody($_text, $format='plain')
308{
309 if ($format == 'html') {
e27ae1a3 310 $res = '<br/>'.removeEvilTags($_text).'<br/>';
5d133234 311 } else if ($format == 'richtext') {
e27ae1a3 312 $res = '<br/>'.richtextToHtml($_text).'<br/>';
5d133234 313 $format = 'html';
8f6f50fb 314 } else {
315 $res = "\n\n" . to_entities(wrap($_text, ""))."\n\n";
316 }
dd7d1c59 317 $res = preg_replace("/(&lt;|&gt;|&quot;)/", " \\1 ", $res);
b7a8ff33 318 $res = preg_replace('/(["\[])?((https?|ftp|news):\/\/[a-z@0-9.~%$£µ&i#\-+=_\/\?]*)(["\]])?/i', '\1<a href="\2">\2</a>\4', $res);
dd7d1c59 319 $res = preg_replace("/ (&lt;|&gt;|&quot;) /", "\\1", $res);
8f6f50fb 320
321 if ($format == 'html') {
73afa785 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);
f5eb6c66 325 } else {
4c1df590 326 for ($i = 1 ; preg_match("@(^|<pre>|\n)&gt;@i", $res) ; $i++) {
b7a8ff33 327 $res = preg_replace("@(^|<pre>|\n)((&gt;[^\n]*\n)+)@ie",
4c1df590 328 "'\\1</pre><blockquote class=\'level$i\'><pre>'"
329 .".stripslashes(preg_replace('@(^|<pre>|\n)&gt;[ \\t\\r]*@i', '\\1', '\\2'))"
330 .".'</pre></blockquote><pre>'",
331 $res);
73afa785 332 }
b7a8ff33 333 $res = preg_replace("@<pre>-- ?\n@", "<pre>\n-- \n", $res);
f5eb6c66 334 $parts = preg_split("/\n-- ?\n/", $res);
8f6f50fb 335 }
336
dd7d1c59 337 if (count($parts) > 1) {
f5eb6c66 338 $sign = array_pop($parts);
339 if ($format == 'html') {
340 $res = join('<br/>-- <br/>', $parts);
73afa785 341 $sign = '<hr style="width: 100%; margin: 1em 0em; " />'.$sign.'<br/>';
f5eb6c66 342 } else {
343 $res = join('\n-- \n', $parts);
344 $sign = '</pre><hr style="width: 100%; margin: 1em 0em; " /><pre>'.$sign;
345 }
346 return $res.$sign;
dd7d1c59 347 } else {
348 return $res;
349 }
cced14b6 350}
351
3ee590a9 352?>