2 /********************************************************************************
3 * include/misc.inc.php : Misc functions
4 * -------------------------
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
10 /********************************************************************************
14 function _b_($str) { return utf8_decode(dgettext('banana', utf8_encode($str))); }
16 function to_entities($str) {
17 require_once dirname(__FILE__
).'/utf8.php';
18 return utf8entities(htmlentities($str, ENT_NOQUOTES
, 'UTF-8'));
21 function is_utf8($s) { return iconv('utf-8', 'utf-8', $s) == $s; }
23 function textFormat_translate($format)
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;
33 /********************************************************************************
41 * @desc Strip forbidden tags and delegate tag-source check to removeEvilAttributes()
43 function removeEvilTags($source)
45 $allowedTags = '<h1><b><i><a><ul><li><pre><hr><blockquote><img><br><font><p><small><big><sup><sub><code><em>';
46 $source = strip_tags($source, $allowedTags);
47 return preg_replace('/<(.*?)>/ie', "'<'.removeEvilAttributes('\\1').'>'", $source);
53 * @desc Strip forbidden attributes from a tag
55 function removeEvilAttributes($tagSource)
57 $stripAttrib = 'javascript:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|'.
58 'onmousemove|onmouseout|onkeypress|onkeydown|onkeyup';
59 return stripslashes(preg_replace("/$stripAttrib/i", '', $tagSource));
62 /** Convert html to plain text
64 function htmlToPlainText($res)
66 $res = trim(html_entity_decode(strip_tags($res, '<br><p>')));
67 $res = preg_replace("@</?(br|p)[^>]*>@i", "\n", $res);
69 $res = utf8_encode($res);
74 /********************************************************************************
78 /** Convert richtext to html
80 function richtextToHtml($source)
82 $tags = Array('bold' => 'b',
88 'superscript' => 'sup',
89 'excerpt' => 'blockquote',
94 // clean unsupported tags
95 $protectedTags = '<signature><lt><comment><'.join('><', array_keys($tags)).'>';
96 $source = strip_tags($source, $protectedTags);
98 // convert richtext tags to html
99 foreach (array_keys($tags) as $tag) {
100 $source = preg_replace('@(</?)'.$tag.'([^>]*>)@i', '\1'.$tags[$tag].'\2', $source);
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);
111 /********************************************************************************
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);
121 function headerDecode($value) {
122 $val = preg_replace('/(=\?[^?]*\?[BQ]\?[^?]*\?=) (=\?[^?]*\?[BQ]\?[^?]*\?=)/', '\1\2', $value);
123 return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val);
126 function headerEncode($value, $trim = 0) {
128 if (strlen($value) > $trim) {
129 $value = substr($value, 0, $trim) . "[...]";
132 return "=?UTF-8?B?".base64_encode($value)."?=";
135 function header_translate($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');
146 if (function_exists('hook_headerTranslate')
147 && $res = hook_headerTranslate($hdr)) {
154 function formatDisplayHeader($_header,$_text) {
158 return formatDate($_text);
163 $groups = preg_split("/[\t ]*,[\t ]*/",$_text);
164 foreach ($groups as $g) {
165 $res.="<a href='?group=$g'>$g</a>, ";
167 return substr($res,0, -2);
170 return formatFrom($_text);
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);
183 $p = $banana->spool
->overview
[$p]->parent
;
185 foreach (array_reverse($par_ok) as $p) {
186 $rsl .= "<a href=\"?group={$banana->spool->group}&artid=$p\">$ndx</a> ";
192 return '<img src="xface.php?face='.base64_encode($_text).'" alt="x-face" />';
195 if (function_exists('hook_formatDisplayHeader')
196 && $res = hook_formatDisplayHeader($_header, $_text))
200 return htmlentities($_text);
204 /********************************************************************************
208 function formatDate($_text) {
209 return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text));
212 function fancyDate($stamp) {
213 $today = intval(time() / (24*3600));
214 $dday = intval($stamp / (24*3600));
216 if ($today == $dday) {
218 } elseif ($today == 1 +
$dday) {
219 $format = _b_('hier')." %H:%M";
220 } elseif ($today < 7 +
$dday) {
221 $format = '%a %H:%M';
223 $format = '%a %e %b';
225 return strftime($format, $stamp);
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:';
234 $result = htmlentities($text);
235 if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) {
236 $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[1]."@".$regs[2])."</a>";
238 if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) {
239 $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[3])."</a>";
241 if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) {
242 $result="$mailto{$regs[2]}@{$regs[3]}\">".htmlentities($regs[1])."</a>";
244 return preg_replace("/\\\(\(|\))/","\\1",$result);
247 function displayshortcuts($first = -1) {
249 extract($banana->state
);
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>';
257 $res .= "[<a href=\"?group=$group\">$group</a>] ";
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
) {
263 $n = intval(log(count($banana->spool
->overview
), 10))+
1;
264 for ($ndx=1; $ndx <= sizeof($banana->spool
->overview
); $ndx +
= $banana->tmax
) {
266 $fmt = "[%0{$n}u-%0{$n}u] ";
268 $fmt = "[<a href=\"?group=$group&first=$ndx\">%0{$n}u-%0{$n}u</a>] ";
270 $res .= sprintf($fmt, $ndx, min($ndx+
$banana->tmax
-1,sizeof($banana->spool
->overview
)));
274 $res .= "[<a href=\"?group=$group&artid=$artid&action=new\">"
275 ._b_('Répondre')."</a>] ";
276 if ($banana->post
&& $banana->post
->checkcancel()) {
277 $res .= "[<a href=\"?group=$group&artid=$artid&action=cancel\">"
278 ._b_('Annuler ce message')."</a>] ";
281 return $res.'</div>';
284 /********************************************************************************
285 * FORMATTING STUFF : BODY
288 function wrap($text, $_prefix="")
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);
300 $length = $banana->wrap
;
301 $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length, all=>1 };'";
304 return $_prefix.join("\n$_prefix", $result).($_prefix ?
'' : $sign);
307 function formatbody($_text, $format='plain')
309 if ($format == 'html') {
310 $res = '<br/>'.removeEvilTags($_text).'<br/>';
311 } else if ($format == 'richtext') {
312 $res = '<br/>'.richtextToHtml($_text).'<br/>';
315 $res = "\n\n" . to_entities(wrap($_text, ""))."\n\n";
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);
321 if ($format == 'html') {
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);
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>'",
331 $res = preg_replace("@(<blockquote[^>]*>)\n@", '\1', $res);
332 $parts = preg_split("/\n-- ?\n/", $res);
335 if (count($parts) > 1) {
336 $sign = array_pop($parts);
337 if ($format == 'html') {
338 $res = join('<br/>-- <br/>', $parts);
339 $sign = '<hr style="width: 100%; margin: 1em 0em; " />'.$sign.'<br/>';
341 $res = join('\n-- \n', $parts);
342 $sign = '</pre><hr style="width: 100%; margin: 1em 0em; " /><pre>'.$sign;