2 /********************************************************************************
3 * banana/message.inc.php : class for messages
4 * ------------------------
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
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';
14 final class BananaMessage
extends BananaMimePart
16 private $msg_headers = array();
18 public function __construct($data = null
)
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']);
25 Banana
::$msgshow_headers = array_intersect(Banana
::$msgshow_headers, array_keys($this->headers
));
26 Banana
::$message =& $this;
30 public function hasHeader($hdr)
32 return isset($this->headers
[$hdr]);
35 static public function newMessage(array $headers, $body, array $file = null
)
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);
46 static public function translateHeaderName($hdr)
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');
60 case 'references': return _b_('Références');
61 case 'x-face': return _b_('Image');
66 public function translateHeaderValue($hdr)
68 if (!isset($this->headers
[$hdr])) {
71 $text = $this->headers
[$hdr];
73 if (function_exists('hook_formatDisplayHeader')
74 && $res = hook_formatDisplayHeader($hdr, $text)) {
79 return BananaMessage
::formatDate($text);
81 case "followup-to": case "newsgroups":
82 $groups = preg_split("/[\t ]*,[\t ]*/", $text);
84 foreach ($groups as $g) {
85 $res .= Banana
::$page->makeLink(Array('group' => $g, 'text' => $g)) . ', ';
87 return substr($res,0, -2);
90 return BananaMessage
::formatFrom($text, $this->headers
['subject']);
92 case "references": case "in-reply-to":
94 $parents = Banana
::$spool->getReferences($this->headers
);
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++
)) . ' ';
104 $text = stripslashes($text);
105 $text = banana_htmlentities($text);
106 return banana_catchFormats($text);
113 public function getSender()
115 $from = $this->headers
['from'];
116 $name = trim(strip_tags($from));
123 public function getHeaderValue($hdr)
125 $hdr = strtolower($hdr);
126 if (!isset($this->headers
[$hdr])) {
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]);
134 return $this->headers
[$hdr];
138 public function getHeaders()
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());
145 static public function formatFrom($text, $subject = '')
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:';
153 $result = banana_htmlentities($text);
155 $subject = '?subject=' . banana_htmlentities(_b_('Re: ') . $subject, ENT_QUOTES
);
157 if (preg_match("/^<?([^< ]+@[^> ]+)>?$/", $text, $regs)) {
158 $result = $mailto . $regs[1] . $subject . '">' . banana_htmlentities($regs[1]) . '</a>';
160 if (preg_match("/^([^ ]+@[^ ]+) \((.*)\)$/", $text, $regs)) {
161 $result = $mailto . $regs[1] . $subject . '">' . banana_htmlentities($regs[2]) . '</a>';
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>';
168 return preg_replace("/\\\(\(|\))/","\\1",$result);
171 public function getAuthorName()
173 $text = $this->getHeaderValue('From');
175 if (preg_match("/^([^ ]+@[^ ]+) \((.*)\)$/", $text, $regs)) {
178 if (preg_match("/^\"?([^<>\"]+)\"? +<(.+@.+)>$/", $text, $regs)) {
179 $name = preg_replace("/^'(.*)'$/", '\1', $regs[1]);
180 $name = stripslashes($name);
183 return preg_replace("/\\\(\(|\))/","\\1", $name);
186 if (function_exists('hook_getAuthorName') && $name = hook_getAuthorName($this)) {
190 if (preg_match("/([^< ]+)@([^> ]+)/", $text, $regs)) {
196 static public function formatDate($text)
198 return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($text));
201 public function translateHeaders()
204 foreach (array_keys($this->headers
) as $name) {
205 $value = $this->translateHeaderValue($name);
206 if (!is_null($value)) {
207 $result[$this->translateHeaderName($name)] = $value;
213 public function getReferences()
215 $text = $this->headers
['references'];
216 $text = str_replace("><","> <", $text);
217 return preg_split('/\s/', $text);
220 public function hasXFace()
222 return Banana
::$msgshow_xface &&
223 ((function_exists('hook_hasxface') && hook_hasXFace($this->headers
))
224 ||
isset($this->headers
['x-face']));
227 public function getXFace()
229 if (function_exists('hook_getxface') && hook_getXFace($this->headers
)) {
232 header('Content-Type: image/gif');
233 $xface = $this->headers
['x-face'];
234 passthru('echo ' . escapeshellarg($xface)
236 . '| convert -transparent white xbm:- gif:-');
240 public function getFormattedBody(&$reqtype = null
)
242 $types = Banana
::$msgshow_mimeparts;
243 if (!is_null($reqtype)) {
244 array_unshift($types, $reqtype);
246 foreach ($types as $type) {
247 @list
($type, $subtype) = explode('/', $type);
248 $parts = $this->getParts($type, $subtype);
252 $reqtype = implode('/', $parts[0]->getType());
253 return $parts[0]->toHtml();
258 public function quote()
260 foreach (Banana
::$msgedit_mimeparts as $type) {
261 @list
($type, $subtype) = explode('/', $type);
262 $parts = $this->getParts($type, $subtype);
266 if ($parts[0] === $this) {
267 return parent
::quote();
269 return $parts[0]->quote();
274 public function canCancel()
276 if (!Banana
::$protocole->canCancel()) {
279 if (function_exists('hook_checkcancel')) {
280 return hook_checkcancel($this->headers
);
282 return Banana
::$profile['headers']['From'] == $this->headers
['from'];
285 public function canSend()
287 return Banana
::$protocole->canSend();
290 public function getSignature()
292 $email = $this->getHeaderValue('from');
293 if (preg_match('/<?([^ <]+@[^ >]+)>?/', $email, $matches)) {
294 $email = $matches[1];
296 $signature = BananaMimePart
::getSignature();
297 if (empty($signature)) {
300 foreach ($signature['identity'] as $ident) {
301 if (strpos($ident, "<$email>") !== false
) {
305 $signature['certified'] = false
;
306 $signature['certification_error'] = 'mauvaise identité';
312 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: