1197c69bbeec7f94961a791f0ae56483d32c4b8c
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>';
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>')));
67 $res = preg_replace("@<br[^>]>@i", "\n", $res);
71 /********************************************************************************
75 /** Convert richtext to html
77 function richtextToHtml($source)
79 $tags = Array('bold' => 'b',
85 'superscript' => 'sup',
86 'excerpt' => 'blockquote',
91 // clean unsupported tags
92 $protectedTags = '<signature><lt><comment><'.join('><', array_keys($tags)).'>';
93 $source = strip_tags($source, $protectedTags);
95 // convert richtext tags to html
96 foreach (array_keys($tags) as $tag) {
97 $source = preg_replace('@(</?)'.$tag.'([^>]*>)@i', '\1'.$tags[$tag].'\2', $source);
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', '<', $source);
104 $source = preg_replace('@<comment[^>]*>((?:[^<]|<(?!/comment>))*)</comment>@i', '<!-- \1 -->', $source);
105 return removeEvilAttributes($source);
108 /********************************************************************************
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);
118 function headerDecode($value) {
119 $val = preg_replace('/(=\?[^?]*\?[BQ]\?[^?]*\?=) (=\?[^?]*\?[BQ]\?[^?]*\?=)/', '\1\2', $value);
120 return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val);
123 function headerEncode($value, $trim = 0) {
125 if (strlen($value) > $trim) {
126 $value = substr($value, 0, $trim) . "[...]";
129 return "=?UTF-8?B?".base64_encode($value)."?=";
132 function header_translate($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');
143 if (function_exists('hook_headerTranslate')
144 && $res = hook_headerTranslate($hdr)) {
151 function formatDisplayHeader($_header,$_text) {
155 return formatDate($_text);
160 $groups = preg_split("/[\t ]*,[\t ]*/",$_text);
161 foreach ($groups as $g) {
162 $res.="<a href='?group=$g'>$g</a>, ";
164 return substr($res,0, -2);
167 return formatFrom($_text);
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);
180 $p = $banana->spool
->overview
[$p]->parent
;
182 foreach (array_reverse($par_ok) as $p) {
183 $rsl .= "<a href=\"?group={$banana->spool->group}&artid=$p\">$ndx</a> ";
189 return '<img src="xface.php?face='.base64_encode($_text).'" alt="x-face" />';
192 if (function_exists('hook_formatDisplayHeader')
193 && $res = hook_formatDisplayHeader($_header, $_text))
197 return htmlentities($_text);
201 /********************************************************************************
205 function formatDate($_text) {
206 return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text));
209 function fancyDate($stamp) {
210 $today = intval(time() / (24*3600));
211 $dday = intval($stamp / (24*3600));
213 if ($today == $dday) {
215 } elseif ($today == 1 +
$dday) {
216 $format = _b_('hier')." %H:%M";
217 } elseif ($today < 7 +
$dday) {
218 $format = '%a %H:%M';
220 $format = '%a %e %b';
222 return strftime($format, $stamp);
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="mailto:';
231 $result = htmlentities($text);
232 if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) {
233 $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[1]."@".$regs[2])."</a>";
235 if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) {
236 $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[3])."</a>";
238 if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) {
239 $result="$mailto{$regs[2]}@{$regs[3]}\">".htmlentities($regs[1])."</a>";
241 return preg_replace("/\\\(\(|\))/","\\1",$result);
244 function displayshortcuts($first = -1) {
246 extract($banana->state
);
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>';
254 $res .= "[<a href=\"?group=$group\">$group</a>] ";
256 if (is_null($artid)) {
257 $res .= "[<a href=\"?group=$group&action=new\">"._b_('Nouveau message')."</a>] ";
258 if (sizeof($banana->spool
->overview
)>$banana->tmax
) {
260 $n = intval(log(count($banana->spool
->overview
), 10))+
1;
261 for ($ndx=1; $ndx <= sizeof($banana->spool
->overview
); $ndx +
= $banana->tmax
) {
263 $fmt = "[%0{$n}u-%0{$n}u] ";
265 $fmt = "[<a href=\"?group=$group&first=$ndx\">%0{$n}u-%0{$n}u</a>] ";
267 $res .= sprintf($fmt, $ndx, min($ndx+
$banana->tmax
-1,sizeof($banana->spool
->overview
)));
271 $res .= "[<a href=\"?group=$group&artid=$artid&action=new\">"
272 ._b_('Répondre')."</a>] ";
273 if ($banana->post
->checkcancel()) {
274 $res .= "[<a href=\"?group=$group&artid=$artid&action=cancel\">"
275 ._b_('Annuler ce message')."</a>] ";
278 return $res.'</div>';
281 /********************************************************************************
282 * FORMATTING STUFF : BODY
285 function wrap($text, $_prefix="")
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);
297 $length = $banana->wrap
;
298 $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length, all=>1 };'";
301 return $_prefix.join("\n$_prefix", $result).($_prefix ?
'' : $sign);
304 function formatbody($_text, $format='plain')
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/>';
312 $res = "\n\n" . to_entities(wrap($_text, ""))."\n\n";
314 $res = preg_replace("/(<|>|")/", " \\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("/ (<|>|") /", "\\1", $res);
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);
323 $parts = preg_split("/\n-- ?\n/", $res);
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;
332 $res = join('\n-- \n', $parts);
333 $sign = '</pre><hr style="width: 100%; margin: 1em 0em; " /><pre>'.$sign;