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 $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 (!preg_match("@multipart/([^;]+);@", $headers['content-type'], $type)) {
97 preg_match("/boundary=\"?([^ \"]+)\"?/", $headers['content-type'], $boundary);
98 $boundary = $boundary[1];
100 $parts = preg_split("@\n--$boundary(--|\n)@", $body);
101 foreach ($parts as $part) {
102 $part = $this->_get_part($part);
103 $local_header = $part['headers'];
104 $local_body = $part['body'];
105 if (!$this->_split_multipart($local_header, $local_body)) {
106 $is_text = isset($local_header['content-type'])
107 && preg_match("@text/([^;]+);@", $local_header['content-type'])
108 && (!isset($local_header['content-disposition'])
109 ||
!preg_match('@attachment@', $local_header['content-disposition']));
111 // alternative ==> multiple formats for messages
112 if ($type == 'alternative' && $is_text) {
113 array_push($this->messages
, $part);
115 // !alternative ==> une body, others are attachments
116 } else if ($is_text) {
117 if (count($this->messages
) == 0) {
118 $this->body
= $local_body;
119 foreach (array_keys($local_header) as $key) {
120 $this->header
[$key] = $local_header[$key];
122 array_push($this->messages
, $part);
124 $this->_add_attachment($part);
127 $this->_add_attachment($part);
134 /** extract new headers from the part
135 * @param $part STRING part of a multipart message
137 function _get_part($part)
141 $lines = split("\n", $part);
142 while (count($lines)) {
143 $line = array_shift($lines);
145 if (preg_match('@^[\t\r ]+@', $line) && isset($hdr)) {
146 $local_headers[$hdr] .= ' '.trim($line);
148 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
149 $hdr = strtolower($hdr);
150 if (in_array($hdr, $banana->parse_hdr
)) {
151 $local_headers[$hdr] = $val;
158 $local_body = join("\n", $lines);
159 if (preg_match("/quoted-printable/", $local_headers['content-transfer-encoding'])) {
160 $local_body = quoted_printable_decode($local_body);
162 return Array('headers' => $local_headers, 'body' => $local_body);
165 /** add an attachment
167 function _add_attachment($part)
169 $local_header = $part['headers'];
170 $local_body = $part['body'];
172 if ((isset($local_header['content-disposition']) && preg_match('/filename="?([^"]+)"?/', $local_header['content-disposition'], $filename))
173 ||
(isset($local_header['content-type']) && preg_match('/name="?([^"]+)"?/', $local_header['content-type'], $filename))) {
174 $filename = $filename[1];
176 if (!isset($filename)) {
177 $filename = "attachment".count($pj);
180 if (isset($local_header['content-type'])
181 && preg_match('/^\s*([^ ;]+);/', $local_header['content-type'], $mimetype)) {
182 $mimetype = $mimetype[1];
187 if (isset($local_header['content-id'])
188 && preg_match('/^\s*<([^> ]+)>/', $local_header['content-id'], $cid)) {
194 array_push($this->pj
, Array('MIME' => $mimetype,
195 'filename' => $filename,
196 'encoding' => strtolower($local_header['content-transfer-encoding']),
197 'data' => $local_body,
202 /** Fix body charset (convert body to utf8)
203 * @return false if failed
205 function _fix_charset()
207 if (preg_match('!charset="?([^;"]*)"?\s*(;|$)?!', $this->headers
['content-type'], $matches)) {
208 $body = iconv($matches[1], 'utf-8', $this->body
);
209 if (strlen($body) == 0) {
214 $this->body
= utf8_encode($this->body
);
219 /** return body in plain text (useful for messages without a text/plain part)
223 preg_match("@text/([^;]+);@", $this->headers
['content-type'], $format);
224 if ($format[1] == 'plain') {
227 if ($format[1] == 'richtext') {
228 return htmlToPlainText(richtextToHtml($this->body
));
230 return htmlToPlainText($this->body
);
234 /** return local url for the given cid
237 function find_attachment($cid)
241 foreach ($this->pj
as $pj) {
242 if ($pj['cid'] == $cid) {
243 return htmlentities(makeLink(Array('group' => $banana->state
['group'],
244 'artid' => $this->id
,
246 'action' => 'view')));
250 return 'cid:' . $cid;;
253 /** decode an attachment
254 * @param pjid INT id of the attachment to decode
255 * @param action BOOL action to execute : true=view, false=download
257 function get_attachment($pjid, $action = false
)
259 if ($pjid >= count($this->pj
)) {
262 $file = $this->pj
[$pjid];
263 header('Content-Type: '.$file['MIME'].'; name="'.$file['filename'].'"');
265 header('Content-Disposition: attachment; filename="'.$file['filename'].'"');
267 header('Content-Disposition: inline; filename="'.$file['filename'].'"');
269 if ($file['encoding'] == 'base64') {
270 echo base64_decode($file['data']);
271 } else if ($file['encoding'] == 'x-uuencode') {
272 passthru('echo '.escapeshellarg($file['data']).' | uudecode -o /dev/stdout');
274 header('Content-Transfer-Encoding: '.$file['encoding']);
281 /** set body to represent the given part
282 * @param partid INT index of the part in messages
284 function set_body_to_part($partid)
288 if (count($this->messages
) == 0) {
292 $local_header = $this->messages
[$partid]['headers'];
293 $this->body
= $this->messages
[$partid]['body'];
294 foreach ($banana->parse_hdr
as $hdr) {
295 if (isset($local_header[$hdr])) {
296 $this->headers
[$hdr] = $local_header[$hdr];
300 $this->_fix_charset();
307 $hdrs = $banana->nntp
->head($this->id
);
313 foreach ($hdrs as $line) {
314 if (preg_match("/^[\t\r ]+/", $line)) {
315 $line = ($hdr=="X-Face"?
"":" ").ltrim($line);
316 if (in_array($hdr, $banana->parse_hdr
)) {
317 $this->headers
[$hdr] .= $line;
320 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
321 $hdr = strtolower($hdr);
322 if (in_array($hdr, $banana->parse_hdr
)) {
323 $this->headers
[$hdr] = $val;
328 foreach ($banana->hdecode
as $hdr) {
329 if (isset($this->headers
[$hdr])) {
330 $this->headers
[$hdr] = headerDecode($this->headers
[$hdr]);
334 $this->name
= $this->headers
['from'];
335 $this->name
= preg_replace('/<[^ ]*>/', '', $this->name
);
336 $this->name
= trim($this->name
);
340 function checkcancel()
342 if (function_exists('hook_checkcancel')) {
343 return hook_checkcancel($this->headers
);
345 if (!isset($_SESSION)) {
348 return ($this->headers
['from'] == $_SESSION['name'] . ' <' . $_SESSION['mail']. '>');
351 /** Make some links to browse the current newsgroup
356 $ret = '<div class="banana_menu">';
357 $actions = Array('prevThread' => Array('prev_thread', _b_('Discussion précédente')),
358 'prevPost' => Array('prev', _b_('Article précédent')),
359 'nextPost' => Array('next', _b_('Article suivant')),
360 'nextThread' => Array('next_thread', _b_('Discussion suivante')));
361 foreach ($actions as $method=>$params) {
362 $id = $banana->spool
->$method($this->id
);
364 $ret .= makeImgLink(Array('group' => $banana->state
['group'],
370 return $ret . '</div>';
373 /** convert message to html
374 * @param partid INT id of the multipart message that must be displaid
376 function to_html($partid = -1)
380 if (count($this->messages
) > 1) {
382 $this->set_body_to_part($partid);
384 // Select prefered text-format
385 foreach ($banana->body_mime
as $mime) {
386 for ($id = 0 ; $id < count($this->messages
) ; $id++
) {
387 if (preg_match("@$mime@", $this->messages
[$id]['headers']['content-type'])) {
389 $this->set_body_to_part($partid);
405 $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">';
406 $res .= '<tr><th colspan="2" class="subject">'
408 . '<div class="banana_action">'
409 . makeImgLink(Array('group' => $banana->state
['group'],
412 _b_('Nouveau message'))
413 . makeImgLink(Array('group' => $banana->state
['group'],
414 'artid' => $this->id
,
418 if ($this->checkCancel()) {
419 $res .= makeImgLink(Array('group' => $banana->state
['group'],
420 'artid' => $this->id
,
421 'action' => 'cancel'),
426 . formatDisplayHeader('subject', $this->headers
['subject'])
428 . '<tr class="pair"><td class="headers"><table cellpadding="0" cellspacing="0">';
430 foreach ($banana->show_hdr
as $hdr) {
431 if (isset($this->headers
[$hdr])) {
432 $res2 = formatdisplayheader($hdr, $this->headers
[$hdr]);
433 if ($res2 && ($hdr != 'x-face' ||
!$banana->formatxface
)) {
434 $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n";
440 $res .= '</table></td><td class="xface">';
445 $res .= '</td></tr>';
447 if (count($this->messages
) > 1) {
448 $res .= '<tr><th colspan="2">';
449 for ($i = 0 ; $i < count($this->messages
) ; $i++
) {
453 preg_match("@text/([^;]+);@", $this->messages
[$i]['headers']['content-type'], $format);
454 $format = textFormat_translate($format[1]);
456 $res .= makeHREF(Array('group' => $banana->state
['group'],
457 'artid' => $this->id
,
464 $res .= '</th></tr>';
467 preg_match("@text/([^;]+);@", $this->headers
['content-type'], $format);
468 $format = $format[1];
469 $res .= '<tr class="impair"><td colspan="2" class="body"';
470 if ($format == 'html') {
471 if (preg_match('@<body[^>]*bgcolor="?([#0-9a-f]+)"?[^>]*>@i', $this->body
, $bgcolor)) {
472 $res .= ' bgcolor="'.$bgcolor[1].'"';
474 $this->body
= preg_replace('/cid:([^\'" ]+)/e', "find_attachment('\\1')", $this->body
);
475 $res .= '>'.formatbody($this->body
, $format);
477 $res .= '><pre>'.formatbody($this->body
).'</pre>';
479 $res .= '</td></tr>';
481 if (count($this->pj
) > 0) {
482 $res .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>';
483 $res .= '<tr><td colspan="2">';
485 foreach ($this->pj
as $file) {
486 $res .= makeImgLink(Array('group' => $banana->state
['group'],
487 'artid' => $this->id
,
490 _b_('Télécharger')) . ' ';
491 $res .= makeImgLink(Array('group' => $banana->state
['group'],
492 'artid' => $this->id
,
497 $res .= ' ' . $file['filename'].' ('.$file['MIME'].')';
501 $res .= '</td></tr>';
504 $ndx = $banana->spool
->getndx($this->id
);
505 $res .= '<tr><td class="thrd" colspan="2">'
506 . $banana->spool
->to_html($ndx-$banana->tbefore
, $ndx+
$banana->tafter
, $ndx)
508 return $res.'</table>';
512 /** Wrapper for Post::find_attachment
514 function find_attachment($cid)
517 return $banana->post
->find_attachment($cid);
520 // vim:set et sw=4 sts=4 ts=4