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