8d905e386fe6b15fe0ed24c57a6a4d3ed282279f
[banana.git] / banana / misc.inc.php
1 <?php
2 /********************************************************************************
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 /********************************************************************************
11 * MISC
12 */
13
14 function _b_($str) { return utf8_decode(dgettext('banana', utf8_encode($str))); }
15
16 function to_entities($str) {
17 require_once dirname(__FILE__).'/utf8.php';
18 return utf8entities(htmlentities($str, ENT_NOQUOTES, 'UTF-8'));
19 }
20
21 function is_utf8($s) { return iconv('utf-8', 'utf-8', $s) == $s; }
22
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 /** Redirect to the page with the given parameter
34 * @ref makeLink
35 */
36 function redirect($params)
37 {
38 header('Location: ' . makeLink($params));
39 }
40
41 /** Make a link using the given parameters
42 * @param ARRAY params, the parameters with
43 * key => value
44 * Known key are:
45 * - group = group name
46 * - artid/first = article id the the group
47 * - subscribe = to show the subscription page
48 * - action = action to do (new, cancel, view)
49 * - part = to show the given MIME part of the article
50 * - pj = to get the given attachment
51 *
52 * Can be overloaded by defining a hook_makeLink function
53 */
54 function makeLink($params)
55 {
56 if (function_exists('hook_makeLink')
57 && $res = hook_makeLink($params)) {
58 return $res;
59 }
60 $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
61 $host = $_SERVER['HTTP_HOST'];
62 $file = $_SERVER['PHP_SELF'];
63
64 if (count($params) != 0) {
65 $get = '?';
66 foreach ($params as $key=>$value) {
67 if (strlen($get) != 1) {
68 $get .= '&';
69 }
70 $get .= $key . '=' . $value;
71 }
72 } else {
73 $get = '';
74 }
75
76 return $proto . $host . $file . $get;
77 }
78
79 /** Format a link to be use in a link
80 * @ref makeLink
81 */
82 function makeHREF($params, $text = null)
83 {
84 $link = makeLink($params);
85 if (is_null($text)) {
86 $text = $link;
87 }
88 if ($params['action'] == 'view') {
89 $target = ' target="_blank"';
90 }
91 return '<a href="' . htmlentities($link) . $target . '">' . $text . '</a>';
92 }
93
94 /********************************************************************************
95 * HTML STUFF
96 * Taken from php.net
97 */
98
99 /**
100 * @return string
101 * @param string
102 * @desc Strip forbidden tags and delegate tag-source check to removeEvilAttributes()
103 */
104 function removeEvilTags($source)
105 {
106 $allowedTags = '<h1><b><i><a><ul><li><pre><hr><blockquote><img><br><font><p><small><big><sup><sub><code><em>';
107 $source = preg_replace('|</div>|i', '<br />', $source);
108 $source = strip_tags($source, $allowedTags);
109 return preg_replace('/<(.*?)>/ie', "'<'.removeEvilAttributes('\\1').'>'", $source);
110 }
111
112 /**
113 * @return string
114 * @param string
115 * @desc Strip forbidden attributes from a tag
116 */
117 function removeEvilAttributes($tagSource)
118 {
119 $stripAttrib = 'javascript:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|'.
120 'onmousemove|onmouseout|onkeypress|onkeydown|onkeyup';
121 return stripslashes(preg_replace("/$stripAttrib/i", '', $tagSource));
122 }
123
124 /** Convert html to plain text
125 */
126 function htmlToPlainText($res)
127 {
128 $res = trim(html_entity_decode(strip_tags($res, '<div><br><p>')));
129 $res = preg_replace("@</?(br|p|div)[^>]*>@i", "\n", $res);
130 if (!is_utf8($res)) {
131 $res = utf8_encode($res);
132 }
133 return $res;
134 }
135
136 /********************************************************************************
137 * RICHTEXT STUFF
138 */
139
140 /** Convert richtext to html
141 */
142 function richtextToHtml($source)
143 {
144 $tags = Array('bold' => 'b',
145 'italic' => 'i',
146 'smaller' => 'small',
147 'bigger' => 'big',
148 'underline' => 'u',
149 'subscript' => 'sub',
150 'superscript' => 'sup',
151 'excerpt' => 'blockquote',
152 'paragraph' => 'p',
153 'nl' => 'br'
154 );
155
156 // clean unsupported tags
157 $protectedTags = '<signature><lt><comment><'.join('><', array_keys($tags)).'>';
158 $source = strip_tags($source, $protectedTags);
159
160 // convert richtext tags to html
161 foreach (array_keys($tags) as $tag) {
162 $source = preg_replace('@(</?)'.$tag.'([^>]*>)@i', '\1'.$tags[$tag].'\2', $source);
163 }
164
165 // some special cases
166 $source = preg_replace('@<signature>@i', '<br>-- <br>', $source);
167 $source = preg_replace('@</signature>@i', '', $source);
168 $source = preg_replace('@<lt>@i', '&lt;', $source);
169 $source = preg_replace('@<comment[^>]*>((?:[^<]|<(?!/comment>))*)</comment>@i', '<!-- \1 -->', $source);
170 return removeEvilAttributes($source);
171 }
172
173 /********************************************************************************
174 * HEADER STUFF
175 */
176
177 function _headerdecode($charset, $c, $str) {
178 $s = ($c == 'Q' || $c == 'q') ? quoted_printable_decode($str) : base64_decode($str);
179 $s = iconv($charset, 'iso-8859-15', $s);
180 return str_replace('_', ' ', $s);
181 }
182
183 function headerDecode($value) {
184 $val = preg_replace('/(=\?[^?]*\?[BQbq]\?[^?]*\?=) (=\?[^?]*\?[BQbq]\?[^?]*\?=)/', '\1\2', $value);
185 return preg_replace('/=\?([^?]*)\?([BQbq])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val);
186 }
187
188 function headerEncode($value, $trim = 0) {
189 if ($trim) {
190 if (strlen($value) > $trim) {
191 $value = substr($value, 0, $trim) . "[...]";
192 }
193 }
194 return "=?UTF-8?B?".base64_encode($value)."?=";
195 }
196
197 function header_translate($hdr) {
198 switch ($hdr) {
199 case 'from': return _b_('De');
200 case 'subject': return _b_('Sujet');
201 case 'newsgroups': return _b_('Forums');
202 case 'followup-to': return _b_('Suivi-à');
203 case 'date': return _b_('Date');
204 case 'organization': return _b_('Organisation');
205 case 'references': return _b_('Références');
206 case 'x-face': return _b_('Image');
207 default:
208 if (function_exists('hook_headerTranslate')
209 && $res = hook_headerTranslate($hdr)) {
210 return $res;
211 }
212 return $hdr;
213 }
214 }
215
216 function formatDisplayHeader($_header,$_text) {
217 global $banana;
218 switch ($_header) {
219 case "date":
220 return formatDate($_text);
221
222 case "followup-to":
223 case "newsgroups":
224 $res = "";
225 $groups = preg_split("/[\t ]*,[\t ]*/",$_text);
226 foreach ($groups as $g) {
227 $res .= makeHREF(Array('group' => $g), $g) . ', ';
228 }
229 return substr($res,0, -2);
230
231 case "from":
232 return formatFrom($_text);
233
234 case "references":
235 $rsl = "";
236 $ndx = 1;
237 $text = str_replace("><","> <",$_text);
238 $text = preg_split("/[ \t]/",strtr($text,$banana->spool->ids));
239 $parents = preg_grep("/^\d+$/",$text);
240 $p = array_pop($parents);
241 $par_ok = Array();
242
243 while ($p) {
244 $par_ok[]=$p;
245 $p = $banana->spool->overview[$p]->parent;
246 }
247 foreach (array_reverse($par_ok) as $p) {
248 $rsl .= makeHREF(Array('group' => $banana->spool->group), $ndx);
249 $ndx++;
250 }
251 return $rsl;
252
253 case "x-face":
254 return '<img src="xface.php?face='.urlencode(base64_encode($_text)).'" alt="x-face" />';
255
256 default:
257 if (function_exists('hook_formatDisplayHeader')
258 && $res = hook_formatDisplayHeader($_header, $_text))
259 {
260 return $res;
261 }
262 return htmlentities($_text);
263 }
264 }
265
266 /********************************************************************************
267 * FORMATTING STUFF
268 */
269
270 function formatDate($_text) {
271 return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text));
272 }
273
274 function fancyDate($stamp) {
275 $today = intval(time() / (24*3600));
276 $dday = intval($stamp / (24*3600));
277
278 if ($today == $dday) {
279 $format = "%H:%M";
280 } elseif ($today == 1 + $dday) {
281 $format = _b_('hier')." %H:%M";
282 } elseif ($today < 7 + $dday) {
283 $format = '%a %H:%M';
284 } else {
285 $format = '%a %e %b';
286 }
287 return strftime($format, $stamp);
288 }
289
290 function formatFrom($text) {
291 # From: mark@cbosgd.ATT.COM
292 # From: mark@cbosgd.ATT.COM (Mark Horton)
293 # From: Mark Horton <mark@cbosgd.ATT.COM>
294 $mailto = '<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;';
295
296 $result = htmlentities($text);
297 if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) {
298 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[1]."&#64;".$regs[2])."</a>";
299 }
300 if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) {
301 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[3])."</a>";
302 }
303 if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) {
304 $result="$mailto{$regs[2]}&#64;{$regs[3]}\">".htmlentities($regs[1])."</a>";
305 }
306 return preg_replace("/\\\(\(|\))/","\\1",$result);
307 }
308
309 function displayshortcuts($first = -1) {
310 global $banana;
311 extract($banana->state);
312
313 $res = '<div class="banana_scuts">';
314 $res .= '[' . makeHREF(Array(), _b_('Liste des forums')) . '] ';
315 if (is_null($group)) {
316 return $res.'[' . makeHREF(Array('subscribe' => 1), _b_('Abonnements')) . ']</div>';
317 }
318
319 $res .= '[' . makeHREF(Array('group' => $group), $group) . '] ';
320
321 if (is_null($artid)) {
322 $res .= '[' . makeHREF(Array('group' => $group,
323 'action' => 'new'),
324 _b_('Nouveau message'))
325 . '] ';
326 if (sizeof($banana->spool->overview)>$banana->tmax) {
327 $res .= '<br />';
328 $n = intval(log(count($banana->spool->overview), 10))+1;
329 for ($ndx=1; $ndx <= sizeof($banana->spool->overview); $ndx += $banana->tmax) {
330 if ($first==$ndx) {
331 $fmt = "[%0{$n}u-%0{$n}u] ";
332 } else {
333 $fmt = '[' . makeHREF(Array('group' => $group,
334 'first' => $ndx),
335 '%0' . $n . 'u-%0' . $n . 'u')
336 . '] ';
337 }
338 $res .= sprintf($fmt, $ndx, min($ndx+$banana->tmax-1,sizeof($banana->spool->overview)));
339 }
340 }
341 } else {
342 $res .= '[' . makeHREF(Array('group' => $group,
343 'artid' => $artid,
344 'action' => 'new'),
345 _b_('Répondre'))
346 . '] ';
347 if ($banana->post && $banana->post->checkcancel()) {
348 $res .= '[' . makeHREF(Array('group' => $group,
349 'artid' => $artid,
350 'action' => 'cancel'),
351 _b_('Annuler ce message'))
352 . '] ';
353 }
354 }
355 return $res.'</div>';
356 }
357
358 /********************************************************************************
359 * FORMATTING STUFF : BODY
360 */
361
362 function autoformat($text)
363 {
364 global $banana;
365 $length = $banana->wrap;
366
367 $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length, all=>1 };'";
368 exec($cmd, $result, $ret);
369 if ($ret != 0) {
370 $result = split("\n", $text);
371 }
372 return $result;
373 }
374
375 function wrap($text, $_prefix="", $_force=false)
376 {
377 $parts = preg_split("/\n-- ?\n/", $text);
378 if (count($parts) >1) {
379 $sign = "\n-- \n" . array_pop($parts);
380 $text = join("\n-- \n", $parts);
381 } else {
382 $sign = '';
383 }
384
385 global $banana;
386 $url = $banana->url_regexp;
387 $length = $banana->wrap;
388 $max = $length + ($length/10);
389 $splits = split("\n", $text);
390 $result = array();
391 $next = array();
392 $format = false;
393 foreach ($splits as $line) {
394 if ($_force || strlen($line) > $max) {
395 if (preg_match("!^(.*)($url)(.*)!i", $line, $matches) && strlen($matches[2]) > $length && strlen($matches) < 900) {
396 if (strlen($matches[1]) != 0) {
397 array_push($next, rtrim($matches[1]));
398 if (strlen($matches[1]) > $max) {
399 $format = true;
400 }
401 }
402
403 if ($format) {
404 $result = array_merge($result, autoformat(join("\n", $next)));
405 } else {
406 $result = array_merge($result, $next);
407 }
408 $format = false;
409 $next = array();
410 array_push($result, $matches[2]);
411
412 if (strlen($matches[6]) != 0) {
413 array_push($next, ltrim($matches[6]));
414 if (strlen($matches[6]) > $max) {
415 $format = true;
416 }
417 }
418 } else {
419 $format = true;
420 array_push($next, $line);
421 }
422 } else {
423 array_push($next, $line);
424 }
425 }
426 if ($format) {
427 $result = array_merge($result, autoformat(join("\n", $next)));
428 } else {
429 $result = array_merge($result, $next);
430 }
431
432 return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign);
433 }
434
435 function cutlink($link)
436 {
437 global $banana;
438
439 if (strlen($link) > $banana->wrap) {
440 $link = substr($link, 0, $banana->wrap - 3)."...";
441 }
442 return $link;
443 }
444
445 function cleanurl($url)
446 {
447 $url = str_replace('@', '%40', $url);
448 return '<a href="'.$url.'" title="'.$url.'">'.cutlink($url).'</a>';
449 }
450
451 function formatbody($_text, $format='plain', $flowed=false)
452 {
453 if ($format == 'html') {
454 $res = '<br/>'.html_entity_decode(to_entities(removeEvilTags($_text))).'<br/>';
455 } else if ($format == 'richtext') {
456 $res = '<br/>'.html_entity_decode(to_entities(richtextToHtml($_text))).'<br/>';
457 } else {
458 $res = "\n\n" . to_entities(wrap($_text, "", $flowed))."\n\n";
459 }
460
461 if ($format != 'html') {
462 global $banana;
463 $url = $banana->url_regexp;
464 $res = preg_replace("/(&lt;|&gt;|&quot;)/", " \\1 ", $res);
465 $res = preg_replace("!$url!ie", "'\\1'.cleanurl('\\2').'\\3'", $res);
466 $res = preg_replace('/(["\[])?(?:mailto:)?([a-z0-9.\-+_]+@[a-z0-9.\-+_]+)(["\]])?/i', '\1<a href="mailto:\2">\2</a>\3', $res);
467 $res = preg_replace("/ (&lt;|&gt;|&quot;) /", "\\1", $res);
468
469 if ($format == 'richtext') {
470 $format = 'html';
471 }
472 }
473
474 if ($format == 'html') {
475 $res = preg_replace("@(</p>)\n?-- ?\n?(<p[^>]*>|<br[^>]*>)@", "\\1<br/>-- \\2", $res);
476 $res = preg_replace("@<br[^>]*>\n?-- ?\n?(<p[^>]*>)@", "<br/>-- <br/>\\2", $res);
477 $res = preg_replace("@(<pre[^>]*>)\n?-- ?\n@", "<br/>-- <br/>\\1", $res);
478 $parts = preg_split("@(:?<p[^>]*>\n?-- ?\n?</p>|<br[^>]*>\n?-- ?\n?<br[^>]*>)@", $res);
479 } else {
480 while (preg_match("@(^|<pre>|\n)&gt;@i", $res)) {
481 $res = preg_replace("@(^|<pre>|\n)((&gt;[^\n]*\n)+)@ie",
482 "'\\1</pre><blockquote><pre>'"
483 .".stripslashes(preg_replace('@(^|<pre>|\n)&gt;[ \\t\\r]*@i', '\\1', '\\2'))"
484 .".'</pre></blockquote><pre>'",
485 $res);
486 }
487 $res = preg_replace("@<pre>-- ?\n@", "<pre>\n-- \n", $res);
488 $parts = preg_split("/\n-- ?\n/", $res);
489 }
490
491 if (count($parts) > 1) {
492 $sign = array_pop($parts);
493 if ($format == 'html') {
494 $res = join('<br/>-- <br/>', $parts);
495 $sign = '<hr style="width: 100%; margin: 1em 0em; " />'.$sign.'<br/>';
496 } else {
497 $res = join('\n-- \n', $parts);
498 $sign = '</pre><hr style="width: 100%; margin: 1em 0em; " /><pre>'.$sign;
499 }
500 return $res.$sign;
501 } else {
502 return $res;
503 }
504 }
505
506 ?>