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