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);
61 } elseif(isset($mpart_type)) {
62 $this->_split_multipart($mpart_type[1], $mpart_boundary[1]);
63 $this->_find_uuencode();
64 $this->_fix_charset();
68 /** find and add uuencoded attachments
70 function _find_uuencode()
72 if (preg_match_all('@\n(begin \d+ ([^\r\n]+)\r?(?:\n(?!end)[^\n]*)*\nend)@', $this->body
, $matches, PREG_SET_ORDER
)) {
73 foreach ($matches as $match) {
74 $mime = trim(exec('echo '.escapeshellarg($match[1]).' | uudecode -o /dev/stdout | file -bi -'));
75 if ($mime != 'application/x-empty') {
76 $this->body
= trim(str_replace($match[0], '', $this->body
));
78 $header['content-type'] = $mime.'; name="'.$match[2].'"';
79 $header['content-transfer-encoding'] = 'x-uuencode';
80 $header['content-disposition'] = 'attachment; filename="'.$match[2].'"';
81 $this->_add_attachment(Array('headers' => $header, 'body' => $body));
87 /** split multipart messages
88 * @param $type STRING multipart type description
89 * @param $boundary STRING multipart boundary identification string
91 function _split_multipart($headers, $body)
93 if (!isset($headers['content-type'])
94 ||
!preg_match("@multipart/([^;]+);@", $headers['content-type'], $type)) {
98 preg_match("/boundary=\"?([^ \"]+)\"?/", $headers['content-type'], $boundary);
99 $boundary = $boundary[1];
101 $parts = preg_split("@\n--$boundary(--|\n)@", $body);
102 foreach ($parts as $part) {
103 $part = $this->_get_part($part);
104 $local_header = $part['headers'];
105 $local_body = $part['body'];
106 if (!$this->_split_multipart($local_header, $local_body)) {
107 $is_text = isset($local_header['content-type'])
108 && preg_match("@text/([^;]+);@", $local_header['content-type'])
109 && (!isset($local_header['content-disposition'])
110 ||
!preg_match('@attachment@', $local_header['content-disposition']));
112 // alternative ==> multiple formats for messages
113 if ($type == 'alternative' && $is_text) {
114 array_push($this->messages
, $part);
116 // !alternative ==> une body, others are attachments
117 } else if ($is_text) {
118 if (count($this->messages
) == 0) {
119 $this->body
= $local_body;
120 foreach (array_keys($local_header) as $key) {
121 $this->header
[$key] = $local_header[$key];
123 array_push($this->messages
, $part);
125 $this->_add_attachment($part);
128 $this->_add_attachment($part);
135 /** extract new headers from the part
136 * @param $part STRING part of a multipart message
138 function _get_part($part)
142 $local_headers = Array();
143 $lines = split("\n", $part);
144 while (count($lines)) {
145 $line = array_shift($lines);
147 if (preg_match('@^[\t\r ]+@', $line) && isset($hdr)) {
148 $local_headers[$hdr] .= ' '.trim($line);
150 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
151 $hdr = strtolower($hdr);
152 if (in_array($hdr, $banana->parse_hdr
)) {
153 $local_headers[$hdr] = $val;
160 $local_body = join("\n", $lines);
161 if (isset($local_headers['content-transfer-encoding'])
162 && preg_match("/quoted-printable/", $local_headers['content-transfer-encoding'])) {
163 $local_body = quoted_printable_decode($local_body);
165 return Array('headers' => $local_headers, 'body' => $local_body);
168 /** add an attachment
170 function _add_attachment($part)
172 $local_header = $part['headers'];
173 $local_body = $part['body'];
175 if ((isset($local_header['content-disposition']) && preg_match('/filename="?([^"]+)"?/', $local_header['content-disposition'], $filename))
176 ||
(isset($local_header['content-type']) && preg_match('/name="?([^"]+)"?/', $local_header['content-type'], $filename))) {
177 $filename = $filename[1];
179 if (!isset($filename)) {
180 $filename = "attachment" . count($this->pj
);
183 if (isset($local_header['content-type'])
184 && preg_match('/^\s*([^ ;]+);/', $local_header['content-type'], $mimetype)) {
185 $mimetype = $mimetype[1];
190 if (isset($local_header['content-id'])
191 && preg_match('/^\s*<([^> ]+)>/', $local_header['content-id'], $cid)) {
197 array_push($this->pj
, Array('MIME' => $mimetype,
198 'filename' => $filename,
199 'encoding' => strtolower($local_header['content-transfer-encoding']),
200 'data' => $local_body,
205 /** Fix body charset (convert body to utf8)
206 * @return false if failed
208 function _fix_charset()
210 if (preg_match('!charset="?([^;"]*)"?\s*(;|$)?!', $this->headers
['content-type'], $matches)) {
211 $body = iconv($matches[1], 'utf-8', $this->body
);
212 if (strlen($body) == 0) {
217 $this->body
= utf8_encode($this->body
);
222 /** return body in plain text (useful for messages without a text/plain part)
226 preg_match("@text/([^;]+);@", $this->headers
['content-type'], $format);
227 if ($format[1] == 'plain') {
230 if ($format[1] == 'richtext') {
231 return htmlToPlainText(richtextToHtml($this->body
));
233 return htmlToPlainText($this->body
);
237 /** return local url for the given cid
240 function find_attachment($cid)
244 foreach ($this->pj
as $pj) {
245 if ($pj['cid'] == $cid) {
246 return htmlentities(makeLink(Array('group' => $banana->state
['group'],
247 'artid' => $this->id
,
249 'action' => 'view')));
253 return 'cid:' . $cid;;
256 /** decode an attachment
257 * @param pjid INT id of the attachment to decode
258 * @param action BOOL action to execute : true=view, false=download
260 function get_attachment($pjid, $action = false
)
262 if ($pjid >= count($this->pj
)) {
265 $file = $this->pj
[$pjid];
266 header('Content-Type: '.$file['MIME'].'; name="'.$file['filename'].'"');
268 header('Content-Disposition: attachment; filename="'.$file['filename'].'"');
270 header('Content-Disposition: inline; filename="'.$file['filename'].'"');
272 if ($file['encoding'] == 'base64') {
273 echo base64_decode($file['data']);
274 } else if ($file['encoding'] == 'x-uuencode') {
275 passthru('echo '.escapeshellarg($file['data']).' | uudecode -o /dev/stdout');
277 header('Content-Transfer-Encoding: '.$file['encoding']);
284 /** set body to represent the given part
285 * @param partid INT index of the part in messages
287 function set_body_to_part($partid)
291 if (count($this->messages
) == 0) {
295 $local_header = $this->messages
[$partid]['headers'];
296 $this->body
= $this->messages
[$partid]['body'];
297 foreach ($banana->parse_hdr
as $hdr) {
298 if (isset($local_header[$hdr])) {
299 $this->headers
[$hdr] = $local_header[$hdr];
303 $this->_fix_charset();
310 $hdrs = $banana->nntp
->head($this->id
);
316 foreach ($hdrs as $line) {
317 if (preg_match("/^[\t\r ]+/", $line)) {
318 $line = ($hdr=="X-Face"?
"":" ").ltrim($line);
319 if (in_array($hdr, $banana->parse_hdr
)) {
320 $this->headers
[$hdr] .= $line;
323 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
324 $hdr = strtolower($hdr);
325 if (in_array($hdr, $banana->parse_hdr
)) {
326 $this->headers
[$hdr] = $val;
331 foreach ($banana->hdecode
as $hdr) {
332 if (isset($this->headers
[$hdr])) {
333 $this->headers
[$hdr] = headerDecode($this->headers
[$hdr]);
337 $this->name
= $this->headers
['from'];
338 $this->name
= preg_replace('/<[^ ]*>/', '', $this->name
);
339 $this->name
= trim($this->name
);
343 function checkcancel()
345 if (function_exists('hook_checkcancel')) {
346 return hook_checkcancel($this->headers
);
348 if (!isset($_SESSION)) {
351 return ($this->headers
['from'] == $_SESSION['name'] . ' <' . $_SESSION['mail']. '>');
354 /** Make some links to browse the current newsgroup
359 $ret = '<div class="banana_menu">';
360 $actions = Array('prevThread' => Array('prev_thread', _b_('Discussion précédente')),
361 'prevPost' => Array('prev', _b_('Article précédent')),
362 'nextPost' => Array('next', _b_('Article suivant')),
363 'nextThread' => Array('next_thread', _b_('Discussion suivante')));
364 foreach ($actions as $method=>$params) {
365 $id = $banana->spool
->$method($this->id
);
367 $ret .= makeImgLink(Array('group' => $banana->state
['group'],
373 return $ret . '</div>';
376 /** convert message to html
377 * @param partid INT id of the multipart message that must be displaid
379 function to_html($partid = -1)
383 if (count($this->messages
) > 1) {
385 $this->set_body_to_part($partid);
387 // Select prefered text-format
388 foreach ($banana->body_mime
as $mime) {
389 for ($id = 0 ; $id < count($this->messages
) ; $id++
) {
390 if (preg_match("@$mime@", $this->messages
[$id]['headers']['content-type'])) {
392 $this->set_body_to_part($partid);
408 $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">';
409 $res .= '<tr><th colspan="2" class="subject">'
411 . '<div class="banana_action">'
412 . makeImgLink(Array('group' => $banana->state
['group'],
415 _b_('Nouveau message'))
416 . makeImgLink(Array('group' => $banana->state
['group'],
417 'artid' => $this->id
,
421 if ($this->checkCancel()) {
422 $res .= makeImgLink(Array('group' => $banana->state
['group'],
423 'artid' => $this->id
,
424 'action' => 'cancel'),
429 . formatDisplayHeader('subject', $this->headers
['subject'])
431 . '<tr class="pair"><td class="headers"><table cellpadding="0" cellspacing="0">';
434 foreach ($banana->show_hdr
as $hdr) {
435 if (isset($this->headers
[$hdr])) {
436 $res2 = formatdisplayheader($hdr, $this->headers
[$hdr]);
437 if ($res2 && ($hdr != 'x-face' ||
!$banana->formatxface
)) {
438 $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n";
444 $res .= '</table></td><td class="xface">';
449 $res .= '</td></tr>';
451 if (count($this->messages
) > 1) {
452 $res .= '<tr><th colspan="2">';
453 for ($i = 0 ; $i < count($this->messages
) ; $i++
) {
457 preg_match("@text/([^;]+);@", $this->messages
[$i]['headers']['content-type'], $format);
458 $format = textFormat_translate($format[1]);
460 $res .= makeHREF(Array('group' => $banana->state
['group'],
461 'artid' => $this->id
,
468 $res .= '</th></tr>';
471 preg_match("@text/([^;]+);@", $this->headers
['content-type'], $format);
472 $format = $format[1];
473 $res .= '<tr class="impair"><td colspan="2" class="body"';
474 if ($format == 'html') {
475 if (preg_match('@<body[^>]*bgcolor="?([#0-9a-f]+)"?[^>]*>@i', $this->body
, $bgcolor)) {
476 $res .= ' bgcolor="'.$bgcolor[1].'"';
478 $this->body
= preg_replace('/cid:([^\'" ]+)/e', "find_attachment('\\1')", $this->body
);
479 $res .= '>'.formatbody($this->body
, $format);
481 $res .= '><pre>'.formatbody($this->body
).'</pre>';
483 $res .= '</td></tr>';
485 if (count($this->pj
) > 0) {
486 $res .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>';
487 $res .= '<tr><td colspan="2">';
489 foreach ($this->pj
as $file) {
490 $res .= makeImgLink(Array('group' => $banana->state
['group'],
491 'artid' => $this->id
,
494 _b_('Télécharger')) . ' ';
495 $res .= makeImgLink(Array('group' => $banana->state
['group'],
496 'artid' => $this->id
,
501 $res .= ' ' . $file['filename'].' ('.$file['MIME'].')';
505 $res .= '</td></tr>';
508 $ndx = $banana->spool
->getndx($this->id
);
509 $res .= '<tr><td class="thrd" colspan="2">'
510 . $banana->spool
->to_html($ndx-$banana->tbefore
, $ndx+
$banana->tafter
, $ndx)
512 return $res.'</table>';
516 /** Wrapper for Post::find_attachment
518 function find_attachment($cid)
521 return $banana->post
->find_attachment($cid);
524 // vim:set et sw=4 sts=4 ts=4