7027794f |
1 | <?php |
2 | /******************************************************************************** |
3 | * banana/message.inc.php : class for messages |
4 | * ------------------------ |
5 | * |
6 | * This file is part of the banana distribution |
7 | * Copyright: See COPYING files that comes with this distribution |
8 | ********************************************************************************/ |
9 | |
10 | require_once dirname(__FILE__) . '/mimepart.inc.php'; |
11 | require_once dirname(__FILE__) . '/message.func.inc.php'; |
12 | require_once dirname(__FILE__) . '/banana.inc.php'; |
13 | |
14 | final class BananaMessage extends BananaMimePart |
15 | { |
16 | private $msg_headers = array(); |
17 | |
18 | public function __construct($data = null) |
19 | { |
20 | parent::__construct($data); |
21 | if (!is_null($data)) { |
22 | if (isset($this->headers['in-reply-to']) && isset($this->headers['references'])) { |
23 | unset($this->headers['in-reply-to']); |
24 | } |
e9360b11 |
25 | Banana::$msgshow_headers = array_intersect(Banana::$msgshow_headers, array_keys($this->headers)); |
7027794f |
26 | Banana::$message =& $this; |
27 | } |
28 | } |
29 | |
30 | public function hasHeader($hdr) |
31 | { |
32 | return isset($this->headers[$hdr]); |
33 | } |
34 | |
35 | static public function newMessage(array $headers, $body, array $file = null) |
36 | { |
37 | $msg = new BananaMessage(); |
38 | $msg->msg_headers = $headers; |
168e9acb |
39 | $msg->makeTextPart($body, 'text/plain', '8bits', 'UTF-8', 'flowed'); |
7027794f |
40 | if (!is_null($file)) { |
41 | $msg->addAttachment($file); |
42 | } |
43 | return $msg; |
44 | } |
45 | |
46 | static public function translateHeaderName($hdr) |
47 | { |
48 | switch (strtolower($hdr)) { |
49 | case 'from': return _b_('De'); |
50 | case 'subject': return _b_('Sujet'); |
51 | case 'newsgroups': return _b_('Forums'); |
598a1c53 |
52 | case 'followup-to': return _b_('Suivi à'); |
53 | case 'to': return _b_('À'); |
54 | case 'cc': return _b_('Copie à'); |
55 | case 'bcc': return _b_('Copie cachée à'); |
56 | case 'reply-to': return _b_('Répondre à'); |
7027794f |
57 | case 'date': return _b_('Date'); |
58 | case 'organization': return _b_('Organisation'); |
59 | case 'in-reply-to': |
598a1c53 |
60 | case 'references': return _b_('Références'); |
7027794f |
61 | case 'x-face': return _b_('Image'); |
62 | } |
63 | return $hdr; |
64 | } |
65 | |
66 | public function translateHeaderValue($hdr) |
67 | { |
68 | if (!isset($this->headers[$hdr])) { |
69 | return null; |
70 | } |
71 | $text = $this->headers[$hdr]; |
72 | |
73 | if (function_exists('hook_formatDisplayHeader') |
74 | && $res = hook_formatDisplayHeader($hdr, $text)) { |
75 | return $res; |
76 | } |
77 | switch ($hdr) { |
78 | case "date": |
79 | return BananaMessage::formatDate($text); |
80 | |
81 | case "followup-to": case "newsgroups": |
82 | $groups = preg_split("/[\t ]*,[\t ]*/", $text); |
83 | $res = ''; |
84 | foreach ($groups as $g) { |
85 | $res .= Banana::$page->makeLink(Array('group' => $g, 'text' => $g)) . ', '; |
86 | } |
87 | return substr($res,0, -2); |
88 | |
89 | case "from": |
f8391ee4 |
90 | return BananaMessage::formatFrom($text, $this->headers['subject']); |
7027794f |
91 | |
92 | case "references": case "in-reply-to": |
93 | $rsl = ""; |
2cd25d86 |
94 | $parents = Banana::$spool->getReferences($this->headers); |
7027794f |
95 | $ndx = 1; |
2cd25d86 |
96 | while (!empty($parents)) { |
97 | $p = array_shift($parents); |
7027794f |
98 | $rsl .= Banana::$page->makeLink(Array('group' => Banana::$spool->group, |
2cd25d86 |
99 | 'artid' => $p->id, 'text' => $ndx++)) . ' '; |
7027794f |
100 | } |
101 | return $rsl; |
102 | |
103 | case "subject": |
7027794f |
104 | $text = stripslashes($text); |
ba77e884 |
105 | $text = banana_htmlentities($text); |
b87c9103 |
106 | return banana_catchFormats($text); |
7027794f |
107 | |
108 | default: |
109 | return $text; |
110 | } |
111 | } |
112 | |
113 | public function getSender() |
114 | { |
115 | $from = $this->headers['from']; |
66e81236 |
116 | $name = trim(strip_tags($from)); |
7027794f |
117 | if (empty($name)) { |
118 | return $from; |
119 | } |
120 | return $name; |
121 | } |
122 | |
123 | public function getHeaderValue($hdr) |
124 | { |
125 | $hdr = strtolower($hdr); |
126 | if (!isset($this->headers[$hdr])) { |
127 | return null; |
128 | } |
129 | if ($hdr == 'date') { |
130 | return strtotime($this->headers['date']); |
3172a611 |
131 | } else if ($hdr == 'references' || $hdr == 'reply-to') { |
132 | return str_replace('><', '> <', $this->headers[$hdr]); |
7027794f |
133 | } else { |
134 | return $this->headers[$hdr]; |
135 | } |
136 | } |
137 | |
138 | public function getHeaders() |
139 | { |
e9360b11 |
140 | $this->msg_headers = array_merge($this->msg_headers, Banana::$msgedit_headers, Banana::$profile['headers']); |
7027794f |
141 | $headers = array_map(array($this, 'encodeHeader'), $this->msg_headers); |
142 | return array_merge($headers, parent::getHeaders()); |
143 | } |
144 | |
f8391ee4 |
145 | static public function formatFrom($text, $subject = '') |
7027794f |
146 | { |
147 | # From: mark@cbosgd.ATT.COM |
148 | # From: <mark@cbosgd.ATT.COM> |
149 | # From: mark@cbosgd.ATT.COM (Mark Horton) |
150 | # From: Mark Horton <mark@cbosgd.ATT.COM> |
151 | $mailto = '<a href="mailto:'; |
f8391ee4 |
152 | |
7027794f |
153 | $result = banana_htmlentities($text); |
f8391ee4 |
154 | if ($subject) { |
155 | $subject = '?subject=' . banana_htmlentities(_b_('Re: ') . $subject, ENT_QUOTES); |
156 | } |
e02edbfe |
157 | if (preg_match("/^<?([^< ]+@[^> ]+)>?$/", $text, $regs)) { |
f8391ee4 |
158 | $result = $mailto . $regs[1] . $subject . '">' . banana_htmlentities($regs[1]) . '</a>'; |
7027794f |
159 | } |
160 | if (preg_match("/^([^ ]+@[^ ]+) \((.*)\)$/", $text, $regs)) { |
f8391ee4 |
161 | $result = $mailto . $regs[1] . $subject . '">' . banana_htmlentities($regs[2]) . '</a>'; |
66e81236 |
162 | } |
7027794f |
163 | if (preg_match("/^\"?([^<>\"]+)\"? +<(.+@.+)>$/", $text, $regs)) { |
164 | $nom = preg_replace("/^'(.*)'$/", '\1', $regs[1]); |
165 | $nom = stripslashes($nom); |
f8391ee4 |
166 | $result = $mailto . $regs[2] . $subject . '">' . banana_htmlentities($nom) . '</a>'; |
7027794f |
167 | } |
168 | return preg_replace("/\\\(\(|\))/","\\1",$result); |
169 | } |
170 | |
e02edbfe |
171 | public function getAuthorName() |
172 | { |
173 | $text = $this->getHeaderValue('From'); |
174 | $name = null; |
175 | if (preg_match("/^([^ ]+@[^ ]+) \((.*)\)$/", $text, $regs)) { |
176 | $name = $regs[2]; |
66e81236 |
177 | } |
e02edbfe |
178 | if (preg_match("/^\"?([^<>\"]+)\"? +<(.+@.+)>$/", $text, $regs)) { |
179 | $name = preg_replace("/^'(.*)'$/", '\1', $regs[1]); |
180 | $name = stripslashes($name); |
181 | } |
182 | if ($name) { |
183 | return preg_replace("/\\\(\(|\))/","\\1", $name); |
184 | } |
185 | |
186 | if (function_exists('hook_getAuthorName') && $name = hook_getAuthorName($this)) { |
187 | return $name; |
188 | } |
189 | |
190 | if (preg_match("/([^< ]+)@([^> ]+)/", $text, $regs)) { |
191 | return $regs[1]; |
192 | } |
193 | return 'Anonymous'; |
194 | } |
195 | |
7027794f |
196 | static public function formatDate($text) |
197 | { |
22b95309 |
198 | return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($text)); |
7027794f |
199 | } |
200 | |
201 | public function translateHeaders() |
202 | { |
203 | $result = array(); |
204 | foreach (array_keys($this->headers) as $name) { |
205 | $value = $this->translateHeaderValue($name); |
206 | if (!is_null($value)) { |
207 | $result[$this->translateHeaderName($name)] = $value; |
208 | } |
209 | } |
210 | return $result; |
211 | } |
212 | |
213 | public function getReferences() |
214 | { |
215 | $text = $this->headers['references']; |
216 | $text = str_replace("><","> <", $text); |
217 | return preg_split('/\s/', $text); |
218 | } |
219 | |
7027794f |
220 | public function hasXFace() |
221 | { |
52d7843e |
222 | return Banana::$msgshow_xface && |
27934b36 |
223 | ((function_exists('hook_hasxface') && hook_hasXFace($this->headers)) |
224 | || isset($this->headers['x-face'])); |
7027794f |
225 | } |
226 | |
227 | public function getXFace() |
228 | { |
27934b36 |
229 | if (function_exists('hook_getxface') && hook_getXFace($this->headers)) { |
230 | return; |
231 | } |
7027794f |
232 | header('Content-Type: image/gif'); |
233 | $xface = $this->headers['x-face']; |
234 | passthru('echo ' . escapeshellarg($xface) |
235 | . '| uncompface -X ' |
236 | . '| convert -transparent white xbm:- gif:-'); |
237 | exit; |
238 | } |
239 | |
a9defc17 |
240 | public function getFormattedBody(&$reqtype = null) |
7027794f |
241 | { |
e9360b11 |
242 | $types = Banana::$msgshow_mimeparts; |
a9defc17 |
243 | if (!is_null($reqtype)) { |
244 | array_unshift($types, $reqtype); |
7027794f |
245 | } |
246 | foreach ($types as $type) { |
247 | @list($type, $subtype) = explode('/', $type); |
248 | $parts = $this->getParts($type, $subtype); |
249 | if (empty($parts)) { |
250 | continue; |
251 | } |
a9defc17 |
252 | $reqtype = implode('/', $parts[0]->getType()); |
3c3a3ce3 |
253 | return $parts[0]->toHtml(); |
7027794f |
254 | } |
255 | return null; |
256 | } |
257 | |
258 | public function quote() |
259 | { |
3c3a3ce3 |
260 | foreach (Banana::$msgedit_mimeparts as $type) { |
261 | @list($type, $subtype) = explode('/', $type); |
262 | $parts = $this->getParts($type, $subtype); |
263 | if (empty($parts)) { |
264 | continue; |
265 | } |
266 | if ($parts[0] === $this) { |
267 | return parent::quote(); |
268 | } |
269 | return $parts[0]->quote(); |
7027794f |
270 | } |
3c3a3ce3 |
271 | return null; |
7027794f |
272 | } |
273 | |
274 | public function canCancel() |
275 | { |
276 | if (!Banana::$protocole->canCancel()) { |
277 | return false; |
278 | } |
279 | if (function_exists('hook_checkcancel')) { |
280 | return hook_checkcancel($this->headers); |
281 | } |
1ffc081d |
282 | return Banana::$profile['headers']['From'] == $this->headers['from']; |
7027794f |
283 | } |
284 | |
285 | public function canSend() |
286 | { |
287 | return Banana::$protocole->canSend(); |
288 | } |
a3c90095 |
289 | |
290 | public function getSignature() |
291 | { |
292 | $email = $this->getHeaderValue('from'); |
293 | if (preg_match('/<?([^ <]+@[^ >]+)>?/', $email, $matches)) { |
294 | $email = $matches[1]; |
295 | } |
296 | $signature = BananaMimePart::getSignature(); |
297 | if (empty($signature)) { |
298 | return $signature; |
299 | } else { |
300 | foreach ($signature['identity'] as $ident) { |
301 | if (strpos($ident, "<$email>") !== false) { |
302 | return $signature; |
303 | } |
304 | } |
305 | $signature['certified'] = false; |
306 | $signature['certification_error'] = 'mauvaise identité'; |
307 | } |
308 | return $signature; |
309 | } |
7027794f |
310 | } |
311 | |
598a1c53 |
312 | // vim:set et sw=4 sts=4 ts=4 enc=utf-8: |
7027794f |
313 | ?> |