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 'banana/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>';
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 /********************************************************************************
66 function _headerdecode($charset, $c, $str) {
67 $s = ($c == 'Q') ?
quoted_printable_decode($str) : base64_decode($str);
68 $s = iconv($charset, 'iso-8859-15', $s);
69 return str_replace('_', ' ', $s);
72 function headerDecode($value) {
73 $val = preg_replace('/(=\?[^?]*\?[BQ]\?[^?]*\?=) (=\?[^?]*\?[BQ]\?[^?]*\?=)/', '\1\2', $value);
74 return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val);
77 function headerEncode($value, $trim = 0) {
79 if (strlen($value) > $trim) {
80 $value = substr($value, 0, $trim) . "[...]";
83 return "=?UTF-8?B?".base64_encode($value)."?=";
86 function header_translate($hdr) {
88 case 'from': return _b_('De');
89 case 'subject': return _b_('Sujet');
90 case 'newsgroups': return _b_('Forums');
91 case 'followup-to': return _b_('Suivi-à');
92 case 'date': return _b_('Date');
93 case 'organization': return _b_('Organisation');
94 case 'references': return _b_('Références');
95 case 'x-face': return _b_('Image');
97 if (function_exists('hook_headerTranslate')
98 && $res = hook_headerTranslate($hdr)) {
105 function formatDisplayHeader($_header,$_text) {
109 return formatDate($_text);
114 $groups = preg_split("/[\t ]*,[\t ]*/",$_text);
115 foreach ($groups as $g) {
116 $res.="<a href='?group=$g'>$g</a>, ";
118 return substr($res,0, -2);
121 return formatFrom($_text);
126 $text = str_replace("><","> <",$_text);
127 $text = preg_split("/[ \t]/",strtr($text,$banana->spool
->ids
));
128 $parents = preg_grep("/^\d+$/",$text);
129 $p = array_pop($parents);
134 $p = $banana->spool
->overview
[$p]->parent
;
136 foreach (array_reverse($par_ok) as $p) {
137 $rsl .= "<a href=\"?group={$banana->spool->group}&artid=$p\">$ndx</a> ";
143 return '<img src="xface.php?face='.base64_encode($_text).'" alt="x-face" />';
146 if (function_exists('hook_formatDisplayHeader')
147 && $res = hook_formatDisplayHeader($_header, $_text))
151 return htmlentities($_text);
155 /********************************************************************************
159 function formatDate($_text) {
160 return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text));
163 function fancyDate($stamp) {
164 $today = intval(time() / (24*3600));
165 $dday = intval($stamp / (24*3600));
167 if ($today == $dday) {
169 } elseif ($today == 1 +
$dday) {
170 $format = _b_('hier')." %H:%M";
171 } elseif ($today < 7 +
$dday) {
172 $format = '%a %H:%M';
174 $format = '%a %e %b';
176 return strftime($format, $stamp);
179 function formatFrom($text) {
180 # From: mark@cbosgd.ATT.COM
181 # From: mark@cbosgd.ATT.COM (Mark Horton)
182 # From: Mark Horton <mark@cbosgd.ATT.COM>
183 $mailto = '<a href="mailto:';
185 $result = htmlentities($text);
186 if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) {
187 $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[1]."@".$regs[2])."</a>";
189 if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) {
190 $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[3])."</a>";
192 if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) {
193 $result="$mailto{$regs[2]}@{$regs[3]}\">".htmlentities($regs[1])."</a>";
195 return preg_replace("/\\\(\(|\))/","\\1",$result);
198 function displayshortcuts($first = -1) {
200 extract($banana->state
);
202 $res = '<div class="banana_scuts">';
203 $res .= '[<a href="?">'._b_('Liste des forums').'</a>] ';
204 if (is_null($group)) {
205 return $res.'[<a href="?subscribe=1">'._b_('Abonnements').'</a>]</div>';
208 $res .= "[<a href=\"?group=$group\">$group</a>] ";
210 if (is_null($artid)) {
211 $res .= "[<a href=\"?group=$group&action=new\">"._b_('Nouveau message')."</a>] ";
212 if (sizeof($banana->spool
->overview
)>$banana->tmax
) {
214 $n = intval(log(count($banana->spool
->overview
), 10))+
1;
215 for ($ndx=1; $ndx <= sizeof($banana->spool
->overview
); $ndx +
= $banana->tmax
) {
217 $fmt = "[%0{$n}u-%0{$n}u] ";
219 $fmt = "[<a href=\"?group=$group&first=$ndx\">%0{$n}u-%0{$n}u</a>] ";
221 $res .= sprintf($fmt, $ndx, min($ndx+
$banana->tmax
-1,sizeof($banana->spool
->overview
)));
225 $res .= "[<a href=\"?group=$group&artid=$artid&action=new\">"
226 ._b_('Répondre')."</a>] ";
227 if ($banana->post
->checkcancel()) {
228 $res .= "[<a href=\"?group=$group&artid=$artid&action=cancel\">"
229 ._b_('Annuler ce message')."</a>] ";
232 return $res.'</div>';
235 /********************************************************************************
236 * FORMATTING STUFF : BODY
239 function wrap($text, $_prefix="")
241 $parts = preg_split("/\n-- ?\n/", $text);
242 if (count($parts) >1) {
243 $sign = "\n-- \n" . array_pop($parts);
244 $text = join("\n-- \n", $parts);
251 $length = $banana->wrap
;
252 $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length, all=>1 };'";
255 return $_prefix.join("\n$_prefix", $result).($_prefix ?
'' : $sign);
258 function formatbody($_text, $format='plain')
260 if ($format == 'html') {
261 $res = '<br/>'.removeEvilTags(html_entity_decode(to_entities($_text))).'<br/>';
263 $res = "\n\n" . to_entities(wrap($_text, ""))."\n\n";
265 $res = preg_replace("/(<|>|")/", " \\1 ", $res);
266 $res = preg_replace('/(["\[])?((https?|ftp|news):\/\/[a-z@0-9.~%$£µ&i#\-+=_\/\?]*)(["\]])?/i', "\\1<a href=\"\\2\">\\2</a>\\4", $res);
267 $res = preg_replace("/ (<|>|") /", "\\1", $res);
269 if ($format == 'html') {
270 $res = preg_replace("@(</p>)\n?-- \n?(<p[^>]*>|<br>)@", "\\1<br>-- \\2", $res);
271 $res = preg_replace("@<br>\n?-- \n?(<p[^>]*>)@", "<br>-- <br>\\2", $res);
272 $parts = preg_split("@(:?<p[^>]*>\n?-- \n?</p>|<br[^>]*>\n?-- \n?<br>)@", $res);
274 $parts = preg_split("/\n-- ?\n/", $res);
277 if (count($parts) > 1) {
278 $sign = array_pop($parts);
279 if ($format == 'html') {
280 $res = join('<br/>-- <br/>', $parts);
281 $sign = '<hr style="width: 100%; margin: 1em 0em; " />'.$sign;
283 $res = join('\n-- \n', $parts);
284 $sign = '</pre><hr style="width: 100%; margin: 1em 0em; " /><pre>'.$sign;