get rid of session.inc.php
[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 $par_ok = Array();
82
83 while ($p) {
84 $par_ok[]=$p;
85 $p = $banana->spool->overview[$p]->parent;
86 }
87 foreach (array_reverse($par_ok) as $p) {
88 $rsl .= "<a href=\"article.php?group={$banana->spool->group}&amp;id=$p\">$ndx</a> ";
89 $ndx++;
90 }
91 return $rsl;
92
93 case "x-face":
94 return '<img src="xface.php?face='.base64_encode($_text).'" alt="x-face" />';
95
96 default:
97 if (function_exists('hook_formatDisplayHeader')) {
98 return hook_formatDisplayHeader($_header, $_text);
99 }
100 return htmlentities($_text);
101 }
102 }
103
104 /********************************************************************************
105 * FORMATTING STUFF
106 */
107
108 function formatDate($_text) {
109 return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text));
110 }
111
112 function fancyDate($stamp) {
113 $today = intval(time() / (24*3600));
114 $dday = intval($stamp / (24*3600));
115
116 if ($today == $dday) {
117 $format = "%H:%M";
118 } elseif ($today == 1 + $dday) {
119 $format = _b_('hier')." %H:%M";
120 } elseif ($today < 7 + $dday) {
121 $format = '%A %H:%M';
122 } else {
123 $format = '%a %e %b';
124 }
125 return strftime($format, $stamp);
126 }
127
128 function formatFrom($text) {
129 # From: mark@cbosgd.ATT.COM
130 # From: mark@cbosgd.ATT.COM (Mark Horton)
131 # From: Mark Horton <mark@cbosgd.ATT.COM>
132 $mailto = '<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;';
133
134 $result = htmlentities($text);
135 if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) {
136 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[1]."&#64;".$regs[2])."</a>";
137 }
138 if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) {
139 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[3])."</a>";
140 }
141 if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) {
142 $result="$mailto{$regs[2]}&#64;{$regs[3]}\">".htmlentities($regs[1])."</a>";
143 }
144 return preg_replace("/\\\(\(|\))/","\\1",$result);
145 }
146
147 function wrap($text, $_prefix="")
148 {
149 $parts = preg_split("/\n-- ?\n/", $text);
150 if (count($parts) >1) {
151 $sign = "\n-- \n" . array_pop($parts);
152 $text = join("\n-- \n", $parts);
153 } else {
154 $sign = '';
155 $text = $text;
156 }
157
158 global $banana;
159 $length = $banana->wrap;
160 $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length, all=>1 };'";
161 exec($cmd, $result);
162
163 return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign);
164 }
165
166 function formatbody($_text) {
167 $res = "\n\n" . htmlentities(wrap($_text, ""))."\n\n";
168 $res = preg_replace("/(&lt;|&gt;|&quot;)/", " \\1 ", $res);
169 $res = preg_replace('/(["\[])?((https?|ftp|news):\/\/[a-z@0-9.~%$£µ&i#\-+=_\/\?]*)(["\]])?/i', "\\1<a href=\"\\2\">\\2</a>\\4", $res);
170 $res = preg_replace("/ (&lt;|&gt;|&quot;) /", "\\1", $res);
171
172 $parts = preg_split("/\n-- ?\n/", $res);
173
174 if (count($parts) > 1) {
175 $sign = "</pre><hr style='width: 100%; margin: 1em 0em; ' /><pre>" . array_pop($parts);
176 return join("\n-- \n", $parts).$sign;
177 } else {
178 return $res;
179 }
180 }
181
182 ?>