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 | ||
8f6f50fb | 23 | function textFormat_translate($format) |
24 | { | |
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; | |
30 | } | |
31 | } | |
32 | ||
33 | /******************************************************************************** | |
34 | * HTML STUFF | |
35 | * Taken from php.net | |
36 | */ | |
37 | ||
38 | /** | |
39 | * @return string | |
40 | * @param string | |
41 | * @desc Strip forbidden tags and delegate tag-source check to removeEvilAttributes() | |
42 | */ | |
43 | function removeEvilTags($source) | |
44 | { | |
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); | |
48 | } | |
49 | ||
50 | /** | |
51 | * @return string | |
52 | * @param string | |
53 | * @desc Strip forbidden attributes from a tag | |
54 | */ | |
55 | function removeEvilAttributes($tagSource) | |
56 | { | |
57 | $stripAttrib = 'javascript:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|'. | |
58 | 'onmousemove|onmouseout|onkeypress|onkeydown|onkeyup'; | |
59 | return stripslashes(preg_replace("/$stripAttrib/i", '', $tagSource)); | |
60 | } | |
61 | ||
73d5bf46 | 62 | /******************************************************************************** |
63 | * HEADER STUFF | |
64 | */ | |
65 | ||
dd7d1c59 | 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); | |
70 | } | |
71 | ||
72 | function headerDecode($value) { | |
73 | $val = preg_replace('/(=\?[^?]*\?[BQ]\?[^?]*\?=) (=\?[^?]*\?[BQ]\?[^?]*\?=)/', '\1\2', $value); | |
74 | return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val); | |
75 | } | |
76 | ||
9cf3f056 PHM |
77 | function headerEncode($value, $trim = 0) { |
78 | if ($trim) { | |
d752880f PHM |
79 | if (strlen($value) > $trim) { |
80 | $value = substr($value, 0, $trim) . "[...]"; | |
81 | } | |
9cf3f056 PHM |
82 | } |
83 | return "=?UTF-8?B?".base64_encode($value)."?="; | |
84 | } | |
85 | ||
e2cae7e3 | 86 | function header_translate($hdr) { |
d4c19591 | 87 | switch ($hdr) { |
0a65ec9d | 88 | case 'from': return _b_('De'); |
89 | case 'subject': return _b_('Sujet'); | |
90 | case 'newsgroups': return _b_('Forums'); | |
d4c19591 | 91 | case 'followup-to': return _b_('Suivi-à'); |
0a65ec9d | 92 | case 'date': return _b_('Date'); |
93 | case 'organization': return _b_('Organisation'); | |
94 | case 'references': return _b_('Références'); | |
d4c19591 | 95 | case 'x-face': return _b_('Image'); |
e2cae7e3 | 96 | default: |
559be3d6 | 97 | if (function_exists('hook_headerTranslate') |
98 | && $res = hook_headerTranslate($hdr)) { | |
99 | return $res; | |
2dbc0167 | 100 | } |
e2cae7e3 | 101 | return $hdr; |
102 | } | |
103 | } | |
104 | ||
2dbc0167 | 105 | function formatDisplayHeader($_header,$_text) { |
106 | global $banana; | |
107 | switch ($_header) { | |
108 | case "date": | |
109 | return formatDate($_text); | |
110 | ||
111 | case "followup-to": | |
112 | case "newsgroups": | |
113 | $res = ""; | |
114 | $groups = preg_split("/[\t ]*,[\t ]*/",$_text); | |
115 | foreach ($groups as $g) { | |
8d99c683 | 116 | $res.="<a href='?group=$g'>$g</a>, "; |
2dbc0167 | 117 | } |
118 | return substr($res,0, -2); | |
119 | ||
120 | case "from": | |
121 | return formatFrom($_text); | |
122 | ||
123 | case "references": | |
124 | $rsl = ""; | |
125 | $ndx = 1; | |
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); | |
58d1740e | 130 | $par_ok = Array(); |
2dbc0167 | 131 | |
132 | while ($p) { | |
58d1740e | 133 | $par_ok[]=$p; |
2dbc0167 | 134 | $p = $banana->spool->overview[$p]->parent; |
135 | } | |
58d1740e | 136 | foreach (array_reverse($par_ok) as $p) { |
8d99c683 | 137 | $rsl .= "<a href=\"?group={$banana->spool->group}&artid=$p\">$ndx</a> "; |
2dbc0167 | 138 | $ndx++; |
139 | } | |
140 | return $rsl; | |
141 | ||
142 | case "x-face": | |
143 | return '<img src="xface.php?face='.base64_encode($_text).'" alt="x-face" />'; | |
144 | ||
145 | default: | |
559be3d6 | 146 | if (function_exists('hook_formatDisplayHeader') |
147 | && $res = hook_formatDisplayHeader($_header, $_text)) | |
148 | { | |
149 | return $res; | |
2dbc0167 | 150 | } |
151 | return htmlentities($_text); | |
152 | } | |
153 | } | |
154 | ||
73d5bf46 | 155 | /******************************************************************************** |
156 | * FORMATTING STUFF | |
157 | */ | |
158 | ||
e2cae7e3 | 159 | function formatDate($_text) { |
160 | return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text)); | |
161 | } | |
162 | ||
163 | function fancyDate($stamp) { | |
164 | $today = intval(time() / (24*3600)); | |
165 | $dday = intval($stamp / (24*3600)); | |
166 | ||
167 | if ($today == $dday) { | |
168 | $format = "%H:%M"; | |
169 | } elseif ($today == 1 + $dday) { | |
0a65ec9d | 170 | $format = _b_('hier')." %H:%M"; |
e2cae7e3 | 171 | } elseif ($today < 7 + $dday) { |
1248ebac | 172 | $format = '%a %H:%M'; |
e2cae7e3 | 173 | } else { |
174 | $format = '%a %e %b'; | |
175 | } | |
176 | return strftime($format, $stamp); | |
177 | } | |
178 | ||
dd7d1c59 | 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:'; | |
184 | ||
185 | $result = htmlentities($text); | |
186 | if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) { | |
187 | $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[1]."@".$regs[2])."</a>"; | |
188 | } | |
189 | if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) { | |
190 | $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[3])."</a>"; | |
191 | } | |
192 | if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) { | |
193 | $result="$mailto{$regs[2]}@{$regs[3]}\">".htmlentities($regs[1])."</a>"; | |
194 | } | |
195 | return preg_replace("/\\\(\(|\))/","\\1",$result); | |
196 | } | |
197 | ||
f78f34d2 | 198 | function displayshortcuts($first = -1) { |
41cf00eb | 199 | global $banana; |
8d99c683 | 200 | extract($banana->state); |
201 | ||
559be3d6 | 202 | $res = '<div class="banana_scuts">'; |
8d99c683 | 203 | $res .= '[<a href="?">'._b_('Liste des forums').'</a>] '; |
204 | if (is_null($group)) { | |
4cc7f778 | 205 | return $res.'[<a href="?subscribe=1">'._b_('Abonnements').'</a>]</div>'; |
8d99c683 | 206 | } |
207 | ||
208 | $res .= "[<a href=\"?group=$group\">$group</a>] "; | |
209 | ||
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) { | |
213 | $res .= '<br />'; | |
214 | $n = intval(log(count($banana->spool->overview), 10))+1; | |
215 | for ($ndx=1; $ndx <= sizeof($banana->spool->overview); $ndx += $banana->tmax) { | |
216 | if ($first==$ndx) { | |
217 | $fmt = "[%0{$n}u-%0{$n}u] "; | |
218 | } else { | |
219 | $fmt = "[<a href=\"?group=$group&first=$ndx\">%0{$n}u-%0{$n}u</a>] "; | |
f78f34d2 | 220 | } |
8d99c683 | 221 | $res .= sprintf($fmt, $ndx, min($ndx+$banana->tmax-1,sizeof($banana->spool->overview))); |
f78f34d2 | 222 | } |
8d99c683 | 223 | } |
224 | } else { | |
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>] "; | |
230 | } | |
f78f34d2 | 231 | } |
8d99c683 | 232 | return $res.'</div>'; |
f78f34d2 | 233 | } |
234 | ||
235 | /******************************************************************************** | |
236 | * FORMATTING STUFF : BODY | |
237 | */ | |
238 | ||
2dbc0167 | 239 | function wrap($text, $_prefix="") |
cced14b6 | 240 | { |
dd7d1c59 | 241 | $parts = preg_split("/\n-- ?\n/", $text); |
276debfc | 242 | if (count($parts) >1) { |
243 | $sign = "\n-- \n" . array_pop($parts); | |
244 | $text = join("\n-- \n", $parts); | |
3ee590a9 | 245 | } else { |
276debfc | 246 | $sign = ''; |
dd7d1c59 | 247 | $text = $text; |
3ee590a9 | 248 | } |
2dbc0167 | 249 | |
250 | global $banana; | |
251 | $length = $banana->wrap; | |
252 | $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length, all=>1 };'"; | |
f8e23519 | 253 | exec($cmd, $result); |
3ee590a9 | 254 | |
f8e23519 | 255 | return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign); |
3ee590a9 | 256 | } |
257 | ||
8f6f50fb | 258 | function formatbody($_text, $format='plain') |
259 | { | |
260 | if ($format == 'html') { | |
261 | $res = '<br/>'.removeEvilTags(html_entity_decode(to_entities($_text))).'<br/>'; | |
262 | } else { | |
263 | $res = "\n\n" . to_entities(wrap($_text, ""))."\n\n"; | |
264 | } | |
dd7d1c59 | 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); | |
8f6f50fb | 268 | |
269 | if ($format == 'html') { | |
f5eb6c66 | 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); | |
273 | } else { | |
274 | $parts = preg_split("/\n-- ?\n/", $res); | |
8f6f50fb | 275 | } |
276 | ||
dd7d1c59 | 277 | if (count($parts) > 1) { |
f5eb6c66 | 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; | |
282 | } else { | |
283 | $res = join('\n-- \n', $parts); | |
284 | $sign = '</pre><hr style="width: 100%; margin: 1em 0em; " /><pre>'.$sign; | |
285 | } | |
286 | return $res.$sign; | |
dd7d1c59 | 287 | } else { |
288 | return $res; | |
289 | } | |
cced14b6 | 290 | } |
291 | ||
3ee590a9 | 292 | ?> |