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