2 /********************************************************************************
3 * include/posts.inc.php : class for posts
4 * -----------------------
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
30 * @param $_id STRING MSGNUM or MSGID (a group should be selected in this case)
32 function BananaPost($_id)
37 $this->messages
= array();
38 if (!$this->_header()) {
44 if ($body = $banana->nntp
->body($_id)) {
45 $this->body
= join("\n", $body);
51 if (isset($this->headers
['content-transfer-encoding'])) {
52 if (preg_match("/base64/", $this->headers
['content-transfer-encoding'])) {
53 $this->body
= base64_decode($this->body
);
54 } elseif (preg_match("/quoted-printable/", $this->headers
['content-transfer-encoding'])) {
55 $this->body
= quoted_printable_decode($this->body
);
59 if ($this->_split_multipart($this->headers
, $this->body
)) {
60 $this->set_body_to_part(0);
62 if(isset($mpart_type)) {
63 $this->_split_multipart($mpart_type[1], $mpart_boundary[1]);
65 $this->_find_uuencode();
66 $this->_fix_charset();
70 /** find and add uuencoded attachments
72 function _find_uuencode()
74 if (preg_match_all('@\n(begin \d+ ([^\r\n]+)\r?(?:\n(?!end)[^\n]*)*\nend)@', $this->body
, $matches, PREG_SET_ORDER
)) {
75 foreach ($matches as $match) {
76 $mime = trim(exec('echo '.escapeshellarg($match[1]).' | uudecode -o /dev/stdout | file -bi -'));
77 if ($mime != 'application/x-empty') {
78 $this->body
= trim(str_replace($match[0], '', $this->body
));
80 $header['content-type'] = $mime.'; name="'.$match[2].'"';
81 $header['content-transfer-encoding'] = 'x-uuencode';
82 $header['content-disposition'] = 'attachment; filename="'.$match[2].'"';
83 $this->_add_attachment(Array('headers' => $header, 'body' => $body));
89 /** split multipart messages
90 * @param $type STRING multipart type description
91 * @param $boundary STRING multipart boundary identification string
93 function _split_multipart($headers, $body)
95 if (!isset($headers['content-type'])
96 ||
!preg_match("@multipart/([^;]+);@", $headers['content-type'], $type)) {
100 preg_match("/boundary=\"?([^ \"]+)\"?/", $headers['content-type'], $boundary);
101 $boundary = $boundary[1];
103 $parts = preg_split("@\n--$boundary(--|\n)@", $body);
104 foreach ($parts as $part) {
105 $part = $this->_get_part($part);
106 $local_header = $part['headers'];
107 $local_body = $part['body'];
108 if (!$this->_split_multipart($local_header, $local_body)) {
109 $is_text = isset($local_header['content-type'])
110 && preg_match("@text/([^;]+);@", $local_header['content-type'])
111 && (!isset($local_header['content-disposition'])
112 ||
!preg_match('@attachment@', $local_header['content-disposition']));
114 // alternative ==> multiple formats for messages
115 if ($type == 'alternative' && $is_text) {
116 array_push($this->messages
, $part);
118 // !alternative ==> une body, others are attachments
119 } else if ($is_text) {
120 if (count($this->messages
) == 0) {
121 $this->body
= $local_body;
122 foreach (array_keys($local_header) as $key) {
123 $this->header
[$key] = $local_header[$key];
125 array_push($this->messages
, $part);
127 $this->_add_attachment($part);
130 $this->_add_attachment($part);
137 /** extract new headers from the part
138 * @param $part STRING part of a multipart message
140 function _get_part($part)
144 $local_headers = Array();
145 $lines = split("\n", $part);
146 while (count($lines)) {
147 $line = array_shift($lines);
149 if (preg_match('@^[\t\r ]+@', $line) && isset($hdr)) {
150 $local_headers[$hdr] .= ' '.trim($line);
152 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
153 $hdr = strtolower($hdr);
154 if (in_array($hdr, $banana->parse_hdr
)) {
155 $local_headers[$hdr] = $val;
162 $local_body = join("\n", $lines);
163 if (isset($local_headers['content-transfer-encoding'])
164 && preg_match("/quoted-printable/", $local_headers['content-transfer-encoding'])) {
165 $local_body = quoted_printable_decode($local_body);
167 return Array('headers' => $local_headers, 'body' => $local_body);
170 /** add an attachment
172 function _add_attachment($part)
174 $local_header = $part['headers'];
175 $local_body = $part['body'];
177 if ((isset($local_header['content-disposition']) && preg_match('/filename="?([^"]+)"?/', $local_header['content-disposition'], $filename))
178 ||
(isset($local_header['content-type']) && preg_match('/name="?([^"]+)"?/', $local_header['content-type'], $filename))) {
179 $filename = $filename[1];
181 if (!isset($filename)) {
182 $filename = "attachment" . count($this->pj
);
185 if (isset($local_header['content-type'])
186 && preg_match('/^\s*([^ ;]+);/', $local_header['content-type'], $mimetype)) {
187 $mimetype = $mimetype[1];
192 if (isset($local_header['content-id'])
193 && preg_match('/^\s*<([^> ]+)>/', $local_header['content-id'], $cid)) {
199 array_push($this->pj
, Array('MIME' => $mimetype,
200 'filename' => $filename,
201 'encoding' => strtolower($local_header['content-transfer-encoding']),
202 'data' => $local_body,
207 /** Fix body charset (convert body to utf8)
208 * @return false if failed
210 function _fix_charset()
212 if (isset($this->headers
['content-type'])
213 && preg_match('!charset="?([^;"]*)"?\s*(;|$)?!', $this->headers
['content-type'], $matches)) {
214 $body = iconv($matches[1], 'utf-8', $this->body
);
215 if (strlen($body) == 0) {
220 $this->body
= utf8_encode($this->body
);
225 /** return body in plain text (useful for messages without a text/plain part)
229 preg_match("@text/([^;]+);@", $this->headers
['content-type'], $format);
230 if ($format[1] == 'plain') {
233 if ($format[1] == 'richtext') {
234 return htmlToPlainText(richtextToHtml($this->body
));
236 return htmlToPlainText($this->body
);
240 /** return local url for the given cid
243 function find_attachment($cid)
247 foreach ($this->pj
as $pj) {
248 if ($pj['cid'] == $cid) {
249 return htmlentities(makeLink(Array('group' => $banana->state
['group'],
250 'artid' => $this->id
,
252 'action' => 'view')));
256 return 'cid:' . $cid;;
259 /** decode an attachment
260 * @param pjid INT id of the attachment to decode
261 * @param action BOOL action to execute : true=view, false=download
263 function get_attachment($pjid, $action = false
)
265 if ($pjid >= count($this->pj
)) {
268 $file = $this->pj
[$pjid];
269 header('Content-Type: '.$file['MIME'].'; name="'.$file['filename'].'"');
271 header('Content-Disposition: attachment; filename="'.$file['filename'].'"');
273 header('Content-Disposition: inline; filename="'.$file['filename'].'"');
275 if ($file['encoding'] == 'base64') {
276 echo base64_decode($file['data']);
277 } else if ($file['encoding'] == 'x-uuencode') {
278 passthru('echo '.escapeshellarg($file['data']).' | uudecode -o /dev/stdout');
280 header('Content-Transfer-Encoding: '.$file['encoding']);
287 /** set body to represent the given part
288 * @param partid INT index of the part in messages
290 function set_body_to_part($partid)
294 if (count($this->messages
) == 0) {
298 $local_header = $this->messages
[$partid]['headers'];
299 $this->body
= $this->messages
[$partid]['body'];
300 foreach ($banana->parse_hdr
as $hdr) {
301 if (isset($local_header[$hdr])) {
302 $this->headers
[$hdr] = $local_header[$hdr];
306 $this->_fix_charset();
313 $hdrs = $banana->nntp
->head($this->id
);
319 foreach ($hdrs as $line) {
320 if (preg_match("/^[\t\r ]+/", $line)) {
321 $line = ($hdr=="X-Face"?
"":" ").ltrim($line);
322 if (in_array($hdr, $banana->parse_hdr
)) {
323 $this->headers
[$hdr] .= $line;
326 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
327 $hdr = strtolower($hdr);
328 if (in_array($hdr, $banana->parse_hdr
)) {
329 $this->headers
[$hdr] = $val;
334 foreach ($banana->hdecode
as $hdr) {
335 if (isset($this->headers
[$hdr])) {
336 $this->headers
[$hdr] = headerDecode($this->headers
[$hdr]);
340 $this->name
= $this->headers
['from'];
341 $this->name
= preg_replace('/<[^ ]*>/', '', $this->name
);
342 $this->name
= trim($this->name
);
346 function checkcancel()
348 if (function_exists('hook_checkcancel')) {
349 return hook_checkcancel($this->headers
);
351 if (!isset($_SESSION)) {
354 return ($this->headers
['from'] == $_SESSION['name'] . ' <' . $_SESSION['mail']. '>');
357 /** Make some links to browse the current newsgroup
362 $ret = '<div class="banana_menu">';
363 $actions = Array('nextUnread' => Array('next_unread', _b_('Message non-lu suivant'), 'u'),
364 'prevPost' => Array('prev', _b_('Article précédent'), 'a'),
365 'nextPost' => Array('next', _b_('Article suivant'), 'z'),
366 'prevThread' => Array('prev_thread', _b_('Discussion précédente'), 'q'),
367 'nextThread' => Array('next_thread', _b_('Discussion suivante'), 's'));
368 foreach ($actions as $method=>$params) {
369 $id = $banana->spool
->$method($this->id
);
371 $ret .= makeImgLink(Array('group' => $banana->state
['group'],
379 return $ret . '</div>';
382 /** convert message to html
383 * @param partid INT id of the multipart message that must be displaid
385 function to_html($partid = -1)
389 if (count($this->messages
) > 1) {
391 $this->set_body_to_part($partid);
393 // Select prefered text-format
394 foreach ($banana->body_mime
as $mime) {
395 for ($id = 0 ; $id < count($this->messages
) ; $id++
) {
396 if (preg_match("@$mime@", $this->messages
[$id]['headers']['content-type'])) {
398 $this->set_body_to_part($partid);
414 $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">';
415 $res .= '<tr><th colspan="2" class="subject">'
417 . '<div class="banana_action">'
418 . makeImgLink(Array('group' => $banana->state
['group'],
421 _b_('Nouveau message'), null
, null
, null
, 'p') . ' '
422 . makeImgLink(Array('group' => $banana->state
['group'],
423 'artid' => $this->id
,
426 _b_('Répondre'), null
, null
, null
, 'r');
427 if ($this->checkCancel()) {
429 . makeImgLink(Array('group' => $banana->state
['group'],
430 'artid' => $this->id
,
431 'action' => 'cancel'),
433 _b_('Annuler'), null
, null
, null
, 'c');
436 . formatDisplayHeader('subject', $this->headers
['subject'])
438 . '<tr class="pair"><td class="headers"><table cellpadding="0" cellspacing="0">';
441 foreach ($banana->show_hdr
as $hdr) {
442 if (isset($this->headers
[$hdr])) {
443 $res2 = formatdisplayheader($hdr, $this->headers
[$hdr]);
444 if ($res2 && ($hdr != 'x-face' ||
!$banana->formatxface
)) {
445 $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n";
451 $res .= '</table></td><td class="xface">';
456 $res .= '</td></tr>';
458 if (count($this->messages
) > 1) {
459 $res .= '<tr><th colspan="2">';
460 for ($i = 0 ; $i < count($this->messages
) ; $i++
) {
464 preg_match("@text/([^;]+);@", $this->messages
[$i]['headers']['content-type'], $format);
465 $format = textFormat_translate($format[1]);
467 $res .= makeHREF(Array('group' => $banana->state
['group'],
468 'artid' => $this->id
,
475 $res .= '</th></tr>';
478 if (isset($this->headers
['content-type'])
479 && preg_match("@text/([^;]+);@", $this->headers
['content-type'], $format)) {
480 $format = $format[1];
484 $res .= '<tr class="impair"><td colspan="2" class="body"';
485 if ($format == 'html') {
486 if (preg_match('@<body[^>]*bgcolor="?([#0-9a-f]+)"?[^>]*>@i', $this->body
, $bgcolor)) {
487 $res .= ' bgcolor="'.$bgcolor[1].'"';
489 $this->body
= preg_replace('/cid:([^\'" ]+)/e', "find_attachment('\\1')", $this->body
);
490 $res .= '>'.formatbody($this->body
, $format);
492 $res .= '><pre>'.formatbody($this->body
).'</pre>';
494 $res .= '</td></tr>';
496 if (count($this->pj
) > 0) {
497 $res .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>';
498 $res .= '<tr><td colspan="2">';
500 foreach ($this->pj
as $file) {
501 $res .= makeImgLink(Array('group' => $banana->state
['group'],
502 'artid' => $this->id
,
505 _b_('Télécharger')) . ' ';
506 $res .= makeImgLink(Array('group' => $banana->state
['group'],
507 'artid' => $this->id
,
512 $res .= ' ' . $file['filename'].' ('.$file['MIME'].')';
516 $res .= '</td></tr>';
519 $ndx = $banana->spool
->getndx($this->id
);
520 $res .= '<tr><td class="thrd" colspan="2">'
521 . $banana->spool
->to_html($ndx-$banana->tbefore
, $ndx+
$banana->tafter
, $ndx)
523 return $res.'</table>';
527 /** Wrapper for Post::find_attachment
529 function find_attachment($cid)
532 return $banana->post
->find_attachment($cid);
535 // vim:set et sw=4 sts=4 ts=4