_(...) -> _b_(...)
[banana.git] / include / 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 function _b_($str) { return dgettext('banana', $str); }
11
12 function _headerdecode($charset, $c, $str) {
13 $s = ($c == 'Q') ? quoted_printable_decode($str) : base64_decode($str);
14 $s = iconv($charset, 'iso-8859-15', $s);
15 return str_replace('_', ' ', $s);
16 }
17
18 function headerDecode($value) {
19 $val = preg_replace('/(=\?[^?]*\?[BQ]\?[^?]*\?=) (=\?[^?]*\?[BQ]\?[^?]*\?=)/', '\1\2', $value);
20 return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val);
21 }
22
23 function header_translate($hdr) {
24 switch (strtolower($hdr)) {
25 case 'from': return _b_('De');
26 case 'subject': return _b_('Sujet');
27 case 'newsgroups': return _b_('Forums');
28 case 'followup': return _b_('Suivi-à');
29 case 'date': return _b_('Date');
30 case 'organization': return _b_('Organisation');
31 case 'references': return _b_('Références');
32 case 'xface': return _b_('Image');
33 default:
34 return $hdr;
35 }
36 }
37
38 function formatDate($_text) {
39 return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text));
40 }
41
42 function fancyDate($stamp) {
43 $today = intval(time() / (24*3600));
44 $dday = intval($stamp / (24*3600));
45
46 if ($today == $dday) {
47 $format = "%H:%M";
48 } elseif ($today == 1 + $dday) {
49 $format = _b_('hier')." %H:%M";
50 } elseif ($today < 7 + $dday) {
51 $format = '%A %H:%M';
52 } else {
53 $format = '%a %e %b';
54 }
55 return strftime($format, $stamp);
56 }
57
58 function formatFrom($text) {
59 # From: mark@cbosgd.ATT.COM
60 # From: mark@cbosgd.ATT.COM (Mark Horton)
61 # From: Mark Horton <mark@cbosgd.ATT.COM>
62 $mailto = '<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;';
63
64 $result = htmlentities($text);
65 if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) {
66 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[1]."&#64;".$regs[2])."</a>";
67 }
68 if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) {
69 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[3])."</a>";
70 }
71 if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) {
72 $result="$mailto{$regs[2]}&#64;{$regs[3]}\">".htmlentities($regs[1])."</a>";
73 }
74 return preg_replace("/\\\(\(|\))/","\\1",$result);
75 }
76
77 function wrap($text, $_prefix="", $_length=72)
78 {
79 $parts = preg_split("/\n-- ?\n/", $text);
80 if (count($parts) >1) {
81 $sign = "\n-- \n" . array_pop($parts);
82 $text = join("\n-- \n", $parts);
83 } else {
84 $sign = '';
85 $text = $text;
86 }
87
88 $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$_length, all=>1 };'";
89 exec($cmd, $result);
90
91 return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign);
92 }
93
94 function formatbody($_text) {
95 global $news;
96 $res = "\n\n" . htmlentities(wrap($_text, "", $news['wrap']))."\n\n";
97 $res = preg_replace("/(&lt;|&gt;|&quot;)/", " \\1 ", $res);
98 $res = preg_replace('/(["\[])?((https?|ftp|news):\/\/[a-z@0-9.~%$£µ&i#\-+=_\/\?]*)(["\]])?/i', "\\1<a href=\"\\2\">\\2</a>\\4", $res);
99 $res = preg_replace("/ (&lt;|&gt;|&quot;) /", "\\1", $res);
100
101 $parts = preg_split("/\n-- ?\n/", $res);
102
103 if (count($parts) > 1) {
104 $sign = "</pre><hr style='width: 100%; margin: 1em 0em; ' /><pre>" . array_pop($parts);
105 return join("\n-- \n", $parts).$sign;
106 } else {
107 return $res;
108 }
109 }
110
111 ?>