fix wrt IE.. sucky browser
[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) {
382606fb 17 require_once 'banana/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
73d5bf46 23/********************************************************************************
24 * HEADER STUFF
25 */
26
dd7d1c59 27function _headerdecode($charset, $c, $str) {
28 $s = ($c == 'Q') ? quoted_printable_decode($str) : base64_decode($str);
29 $s = iconv($charset, 'iso-8859-15', $s);
30 return str_replace('_', ' ', $s);
31}
32
33function headerDecode($value) {
34 $val = preg_replace('/(=\?[^?]*\?[BQ]\?[^?]*\?=) (=\?[^?]*\?[BQ]\?[^?]*\?=)/', '\1\2', $value);
35 return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val);
36}
37
e2cae7e3 38function header_translate($hdr) {
d4c19591 39 switch ($hdr) {
0a65ec9d 40 case 'from': return _b_('De');
41 case 'subject': return _b_('Sujet');
42 case 'newsgroups': return _b_('Forums');
d4c19591 43 case 'followup-to': return _b_('Suivi-à');
0a65ec9d 44 case 'date': return _b_('Date');
45 case 'organization': return _b_('Organisation');
46 case 'references': return _b_('Références');
d4c19591 47 case 'x-face': return _b_('Image');
e2cae7e3 48 default:
559be3d6 49 if (function_exists('hook_headerTranslate')
50 && $res = hook_headerTranslate($hdr)) {
51 return $res;
2dbc0167 52 }
e2cae7e3 53 return $hdr;
54 }
55}
56
2dbc0167 57function formatDisplayHeader($_header,$_text) {
58 global $banana;
59 switch ($_header) {
60 case "date":
61 return formatDate($_text);
62
63 case "followup-to":
64 case "newsgroups":
65 $res = "";
66 $groups = preg_split("/[\t ]*,[\t ]*/",$_text);
67 foreach ($groups as $g) {
8d99c683 68 $res.="<a href='?group=$g'>$g</a>, ";
2dbc0167 69 }
70 return substr($res,0, -2);
71
72 case "from":
73 return formatFrom($_text);
74
75 case "references":
76 $rsl = "";
77 $ndx = 1;
78 $text = str_replace("><","> <",$_text);
79 $text = preg_split("/[ \t]/",strtr($text,$banana->spool->ids));
80 $parents = preg_grep("/^\d+$/",$text);
81 $p = array_pop($parents);
58d1740e 82 $par_ok = Array();
2dbc0167 83
84 while ($p) {
58d1740e 85 $par_ok[]=$p;
2dbc0167 86 $p = $banana->spool->overview[$p]->parent;
87 }
58d1740e 88 foreach (array_reverse($par_ok) as $p) {
8d99c683 89 $rsl .= "<a href=\"?group={$banana->spool->group}&amp;artid=$p\">$ndx</a> ";
2dbc0167 90 $ndx++;
91 }
92 return $rsl;
93
94 case "x-face":
95 return '<img src="xface.php?face='.base64_encode($_text).'" alt="x-face" />';
96
97 default:
559be3d6 98 if (function_exists('hook_formatDisplayHeader')
99 && $res = hook_formatDisplayHeader($_header, $_text))
100 {
101 return $res;
2dbc0167 102 }
103 return htmlentities($_text);
104 }
105}
106
73d5bf46 107/********************************************************************************
108 * FORMATTING STUFF
109 */
110
e2cae7e3 111function formatDate($_text) {
112 return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text));
113}
114
115function fancyDate($stamp) {
116 $today = intval(time() / (24*3600));
117 $dday = intval($stamp / (24*3600));
118
119 if ($today == $dday) {
120 $format = "%H:%M";
121 } elseif ($today == 1 + $dday) {
0a65ec9d 122 $format = _b_('hier')." %H:%M";
e2cae7e3 123 } elseif ($today < 7 + $dday) {
1248ebac 124 $format = '%a %H:%M';
e2cae7e3 125 } else {
126 $format = '%a %e %b';
127 }
128 return strftime($format, $stamp);
129}
130
dd7d1c59 131function formatFrom($text) {
132# From: mark@cbosgd.ATT.COM
133# From: mark@cbosgd.ATT.COM (Mark Horton)
134# From: Mark Horton <mark@cbosgd.ATT.COM>
135 $mailto = '<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;';
136
137 $result = htmlentities($text);
138 if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) {
139 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[1]."&#64;".$regs[2])."</a>";
140 }
141 if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) {
142 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[3])."</a>";
143 }
144 if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) {
145 $result="$mailto{$regs[2]}&#64;{$regs[3]}\">".htmlentities($regs[1])."</a>";
146 }
147 return preg_replace("/\\\(\(|\))/","\\1",$result);
148}
149
f78f34d2 150function displayshortcuts($first = -1) {
41cf00eb 151 global $banana;
8d99c683 152 extract($banana->state);
153
559be3d6 154 $res = '<div class="banana_scuts">';
8d99c683 155 $res .= '[<a href="?">'._b_('Liste des forums').'</a>] ';
156 if (is_null($group)) {
4cc7f778 157 return $res.'[<a href="?subscribe=1">'._b_('Abonnements').'</a>]</div>';
8d99c683 158 }
159
160 $res .= "[<a href=\"?group=$group\">$group</a>] ";
161
162 if (is_null($artid)) {
163 $res .= "[<a href=\"?group=$group&amp;action=new\">"._b_('Nouveau message')."</a>] ";
164 if (sizeof($banana->spool->overview)>$banana->tmax) {
165 $res .= '<br />';
166 $n = intval(log(count($banana->spool->overview), 10))+1;
167 for ($ndx=1; $ndx <= sizeof($banana->spool->overview); $ndx += $banana->tmax) {
168 if ($first==$ndx) {
169 $fmt = "[%0{$n}u-%0{$n}u] ";
170 } else {
171 $fmt = "[<a href=\"?group=$group&amp;first=$ndx\">%0{$n}u-%0{$n}u</a>] ";
f78f34d2 172 }
8d99c683 173 $res .= sprintf($fmt, $ndx, min($ndx+$banana->tmax-1,sizeof($banana->spool->overview)));
f78f34d2 174 }
8d99c683 175 }
176 } else {
177 $res .= "[<a href=\"?group=$group&amp;artid=$artid&amp;action=new\">"
178 ._b_('Répondre')."</a>] ";
179 if ($banana->post->checkcancel()) {
180 $res .= "[<a href=\"?group=$group&amp;artid=$artid&amp;action=cancel\">"
181 ._b_('Annuler ce message')."</a>] ";
182 }
f78f34d2 183 }
8d99c683 184 return $res.'</div>';
f78f34d2 185}
186
187/********************************************************************************
188 * FORMATTING STUFF : BODY
189 */
190
2dbc0167 191function wrap($text, $_prefix="")
cced14b6 192{
dd7d1c59 193 $parts = preg_split("/\n-- ?\n/", $text);
276debfc 194 if (count($parts) >1) {
195 $sign = "\n-- \n" . array_pop($parts);
196 $text = join("\n-- \n", $parts);
3ee590a9 197 } else {
276debfc 198 $sign = '';
dd7d1c59 199 $text = $text;
3ee590a9 200 }
2dbc0167 201
202 global $banana;
203 $length = $banana->wrap;
204 $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length, all=>1 };'";
f8e23519 205 exec($cmd, $result);
3ee590a9 206
f8e23519 207 return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign);
3ee590a9 208}
209
dd7d1c59 210function formatbody($_text) {
987299b4 211 $res = "\n\n" . to_entities(wrap($_text, ""))."\n\n";
dd7d1c59 212 $res = preg_replace("/(&lt;|&gt;|&quot;)/", " \\1 ", $res);
213 $res = preg_replace('/(["\[])?((https?|ftp|news):\/\/[a-z@0-9.~%$£µ&i#\-+=_\/\?]*)(["\]])?/i', "\\1<a href=\"\\2\">\\2</a>\\4", $res);
214 $res = preg_replace("/ (&lt;|&gt;|&quot;) /", "\\1", $res);
215
216 $parts = preg_split("/\n-- ?\n/", $res);
217
218 if (count($parts) > 1) {
219 $sign = "</pre><hr style='width: 100%; margin: 1em 0em; ' /><pre>" . array_pop($parts);
220 return join("\n-- \n", $parts).$sign;
221 } else {
222 return $res;
223 }
cced14b6 224}
225
3ee590a9 226?>