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