da5b05c7fd2f4550272152644e59458223ae9d4f
[banana.git] / banana / message.inc.php
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 }
25 Banana::$msgshow_headers = array_intersect(Banana::$msgshow_headers, array_keys($this->headers));
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;
39 $msg->makeTextPart($body, 'text/plain', '8bits', 'UTF-8', 'flowed');
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');
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 à');
57 case 'date': return _b_('Date');
58 case 'organization': return _b_('Organisation');
59 case 'in-reply-to':
60 case 'references': return _b_('Références');
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":
90 return BananaMessage::formatFrom($text, $this->headers['subject']);
91
92 case "references": case "in-reply-to":
93 $rsl = "";
94 $parents = Banana::$spool->getReferences($this->headers);
95 $ndx = 1;
96 while (!empty($parents)) {
97 $p = array_shift($parents);
98 $rsl .= Banana::$page->makeLink(Array('group' => Banana::$spool->group,
99 'artid' => $p->id, 'text' => $ndx++)) . ' ';
100 }
101 return $rsl;
102
103 case "subject":
104 $text = stripslashes($text);
105 $text = banana_htmlentities($text);
106 return banana_catchFormats($text);
107
108 default:
109 return $text;
110 }
111 }
112
113 public function getSender()
114 {
115 $from = $this->headers['from'];
116 $name = trim(strip_tags($from));
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']);
131 } else if ($hdr == 'references' || $hdr == 'reply-to') {
132 return str_replace('><', '> <', $this->headers[$hdr]);
133 } else {
134 return $this->headers[$hdr];
135 }
136 }
137
138 public function getHeaders()
139 {
140 $this->msg_headers = array_merge($this->msg_headers, Banana::$msgedit_headers, Banana::$profile['headers']);
141 $headers = array_map(array($this, 'encodeHeader'), $this->msg_headers);
142 return array_merge($headers, parent::getHeaders());
143 }
144
145 static public function formatFrom($text, $subject = '')
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:';
152
153 $result = banana_htmlentities($text);
154 if ($subject) {
155 $subject = '?subject=' . banana_htmlentities(_b_('Re: ') . $subject, ENT_QUOTES);
156 }
157 if (preg_match("/^<?([^< ]+@[^> ]+)>?$/", $text, $regs)) {
158 $result = $mailto . $regs[1] . $subject . '">' . banana_htmlentities($regs[1]) . '</a>';
159 }
160 if (preg_match("/^([^ ]+@[^ ]+) \((.*)\)$/", $text, $regs)) {
161 $result = $mailto . $regs[1] . $subject . '">' . banana_htmlentities($regs[2]) . '</a>';
162 }
163 if (preg_match("/^\"?([^<>\"]+)\"? +<(.+@.+)>$/", $text, $regs)) {
164 $nom = preg_replace("/^'(.*)'$/", '\1', $regs[1]);
165 $nom = stripslashes($nom);
166 $result = $mailto . $regs[2] . $subject . '">' . banana_htmlentities($nom) . '</a>';
167 }
168 return preg_replace("/\\\(\(|\))/","\\1",$result);
169 }
170
171 public function getAuthorName()
172 {
173 $text = $this->getHeaderValue('From');
174 $name = null;
175 if (preg_match("/^([^ ]+@[^ ]+) \((.*)\)$/", $text, $regs)) {
176 $name = $regs[2];
177 }
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
196 static public function formatDate($text)
197 {
198 return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($text));
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
220 public function hasXFace()
221 {
222 return Banana::$msgshow_xface &&
223 ((function_exists('hook_hasxface') && hook_hasXFace($this->headers))
224 || isset($this->headers['x-face']));
225 }
226
227 public function getXFace()
228 {
229 if (function_exists('hook_getxface') && hook_getXFace($this->headers)) {
230 return;
231 }
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
240 public function getFormattedBody(&$reqtype = null)
241 {
242 $types = Banana::$msgshow_mimeparts;
243 if (!is_null($reqtype)) {
244 array_unshift($types, $reqtype);
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 }
252 $reqtype = implode('/', $parts[0]->getType());
253 return $parts[0]->toHtml();
254 }
255 return null;
256 }
257
258 public function quote()
259 {
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();
270 }
271 return null;
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 }
282 return Banana::$profile['headers']['From'] == $this->headers['from'];
283 }
284
285 public function canSend()
286 {
287 return Banana::$protocole->canSend();
288 }
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 }
310 }
311
312 // vim:set et sw=4 sts=4 ts=4 enc=utf-8:
313 ?>