3 tons or rewrite
[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 /********************************************************************************
11 * MISC
12 */
13
14 function _b_($str) { return utf8_decode(dgettext('banana', utf8_encode($str))); }
15
16 function checkcancel($_headers) {
17 if (function_exists('hook_checkcancel')) {
18 return hook_checkcancel($_headers);
19 }
20 return ($_headers['from'] == $_SESSION['name']." <".$_SESSION['mail'].">");
21 }
22
23 /********************************************************************************
24 * HEADER STUFF
25 */
26
27 function _headerdecode($charset, $c, $str) {
28 $s = ($c == 'Q') ? quoted_printable_decode($str) : base64_decode($str);
29 $s = iconv($charset, 'iso-8859-15', $s);
30 return str_replace('_', ' ', $s);
31 }
32
33 function headerDecode($value) {
34 $val = preg_replace('/(=\?[^?]*\?[BQ]\?[^?]*\?=) (=\?[^?]*\?[BQ]\?[^?]*\?=)/', '\1\2', $value);
35 return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val);
36 }
37
38 function header_translate($hdr) {
39 switch ($hdr) {
40 case 'from': return _b_('De');
41 case 'subject': return _b_('Sujet');
42 case 'newsgroups': return _b_('Forums');
43 case 'followup-to': return _b_('Suivi-à');
44 case 'date': return _b_('Date');
45 case 'organization': return _b_('Organisation');
46 case 'references': return _b_('Références');
47 case 'x-face': return _b_('Image');
48 default:
49 if (function_exists('hook_header_translate')) {
50 return hook_header_translate($hdr);
51 }
52 return $hdr;
53 }
54 }
55
56 function formatDisplayHeader($_header,$_text) {
57 global $banana;
58 switch ($_header) {
59 case "date":
60 return formatDate($_text);
61
62 case "followup-to":
63 case "newsgroups":
64 $res = "";
65 $groups = preg_split("/[\t ]*,[\t ]*/",$_text);
66 foreach ($groups as $g) {
67 $res.="<a href='thread.php?group=$g'>$g</a>, ";
68 }
69 return substr($res,0, -2);
70
71 case "from":
72 return formatFrom($_text);
73
74 case "references":
75 $rsl = "";
76 $ndx = 1;
77 $text = str_replace("><","> <",$_text);
78 $text = preg_split("/[ \t]/",strtr($text,$banana->spool->ids));
79 $parents = preg_grep("/^\d+$/",$text);
80 $p = array_pop($parents);
81
82 while ($p) {
83 $valid_parents[]=$p;
84 $p = $banana->spool->overview[$p]->parent;
85 }
86 foreach (array_reverse($valid_parents) as $p) {
87 $rsl .= "<a href=\"article.php?group={$banana->spool->group}&amp;id=$p\">$ndx</a> ";
88 $ndx++;
89 }
90 return $rsl;
91
92 case "x-face":
93 return '<img src="xface.php?face='.base64_encode($_text).'" alt="x-face" />';
94
95 default:
96 if (function_exists('hook_formatDisplayHeader')) {
97 return hook_formatDisplayHeader($_header, $_text);
98 }
99 return htmlentities($_text);
100 }
101 }
102
103 /********************************************************************************
104 * FORMATTING STUFF
105 */
106
107 function formatDate($_text) {
108 return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text));
109 }
110
111 function fancyDate($stamp) {
112 $today = intval(time() / (24*3600));
113 $dday = intval($stamp / (24*3600));
114
115 if ($today == $dday) {
116 $format = "%H:%M";
117 } elseif ($today == 1 + $dday) {
118 $format = _b_('hier')." %H:%M";
119 } elseif ($today < 7 + $dday) {
120 $format = '%A %H:%M';
121 } else {
122 $format = '%a %e %b';
123 }
124 return strftime($format, $stamp);
125 }
126
127 function formatFrom($text) {
128 # From: mark@cbosgd.ATT.COM
129 # From: mark@cbosgd.ATT.COM (Mark Horton)
130 # From: Mark Horton <mark@cbosgd.ATT.COM>
131 $mailto = '<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;';
132
133 $result = htmlentities($text);
134 if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) {
135 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[1]."&#64;".$regs[2])."</a>";
136 }
137 if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) {
138 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[3])."</a>";
139 }
140 if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) {
141 $result="$mailto{$regs[2]}&#64;{$regs[3]}\">".htmlentities($regs[1])."</a>";
142 }
143 return preg_replace("/\\\(\(|\))/","\\1",$result);
144 }
145
146 function wrap($text, $_prefix="")
147 {
148 $parts = preg_split("/\n-- ?\n/", $text);
149 if (count($parts) >1) {
150 $sign = "\n-- \n" . array_pop($parts);
151 $text = join("\n-- \n", $parts);
152 } else {
153 $sign = '';
154 $text = $text;
155 }
156
157 global $banana;
158 $length = $banana->wrap;
159 $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length, all=>1 };'";
160 exec($cmd, $result);
161
162 return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign);
163 }
164
165 function formatbody($_text) {
166 $res = "\n\n" . htmlentities(wrap($_text, ""))."\n\n";
167 $res = preg_replace("/(&lt;|&gt;|&quot;)/", " \\1 ", $res);
168 $res = preg_replace('/(["\[])?((https?|ftp|news):\/\/[a-z@0-9.~%$£µ&i#\-+=_\/\?]*)(["\]])?/i', "\\1<a href=\"\\2\">\\2</a>\\4", $res);
169 $res = preg_replace("/ (&lt;|&gt;|&quot;) /", "\\1", $res);
170
171 $parts = preg_split("/\n-- ?\n/", $res);
172
173 if (count($parts) > 1) {
174 $sign = "</pre><hr style='width: 100%; margin: 1em 0em; ' /><pre>" . array_pop($parts);
175 return join("\n-- \n", $parts).$sign;
176 } else {
177 return $res;
178 }
179 }
180
181 ?>