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