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 ********************************************************************************/
28 * @param $_id STRING MSGNUM or MSGID (a group should be selected in this case)
30 function BananaPost($_id)
35 $this->messages
= array();
38 if ($body = $banana->nntp
->body($_id)) {
39 $this->body
= join("\n", $body);
41 return ($this = null
);
44 if (isset($this->headers
['content-transfer-encoding'])) {
45 if (preg_match("/base64/", $this->headers
['content-transfer-encoding'])) {
46 $this->body
= base64_decode($this->body
);
47 } elseif (preg_match("/quoted-printable/", $this->headers
['content-transfer-encoding'])) {
48 $this->body
= quoted_printable_decode($this->body
);
52 if ($this->_split_multipart($this->headers
, $this->body
)) {
53 $this->set_body_to_part(0);
55 $this->_split_multipart($mpart_type[1], $mpart_boundary[1]);
56 $this->_find_uuencode();
57 if (preg_match('!charset=([^;]*)\s*(;|$)!', $this->headers
['content-type'], $matches)) {
58 $this->body
= iconv($matches[1], 'utf-8', $this->body
);
60 $this->body
= utf8_encode($this->body
);
65 /** find and add uuencoded attachments
67 function _find_uuencode()
69 if (preg_match_all('@\n(begin \d+ ([^\r\n]+)\r?(?:\n(?!end)[^\n]*)*\nend)@', $this->body
, $matches, PREG_SET_ORDER
)) {
70 foreach ($matches as $match) {
71 $mime = trim(exec('echo '.escapeshellarg($match[1]).' | uudecode -o /dev/stdout | file -bi -'));
72 if ($mime != 'application/x-empty') {
73 $this->body
= trim(str_replace($match[0], '', $this->body
));
75 $header['content-type'] = $mime.'; name="'.$match[2].'"';
76 $header['content-transfer-encoding'] = 'x-uuencode';
77 $header['content-disposition'] = 'attachment; filename="'.$match[2].'"';
78 $this->_add_attachment(Array('headers' => $header, 'body' => $body));
84 /** split multipart messages
85 * @param $type STRING multipart type description
86 * @param $boundary STRING multipart boundary identification string
88 function _split_multipart($headers, $body)
90 if (!preg_match("@multipart/([^;]+);@", $headers['content-type'], $type)) {
94 preg_match("/boundary=\"?([^ \"]+)\"?/", $headers['content-type'], $boundary);
95 $boundary = $boundary[1];
97 $parts = preg_split("@\n--$boundary(--|\n)@", $body);
98 foreach ($parts as $part) {
99 $part = $this->_get_part($part);
100 $local_header = $part['headers'];
101 $local_body = $part['body'];
102 if (!$this->_split_multipart($local_header, $local_body)) {
103 $is_text = isset($local_header['content-type']) && preg_match("@text/([^;]+);@", $local_header['content-type'])
104 && (!isset($local_header['content-disposition']) ||
!preg_match('@attachment@', $local_header['content-disposition']));
106 // alternative ==> multiple formats for messages
107 if ($type == 'alternative' && $is_text) {
108 array_push($this->messages
, $part);
110 // !alternative ==> une body, others are attachments
111 } else if ($is_text) {
112 if (count($this->messages
) == 0) {
113 $this->body
= $local_body;
114 foreach (array_keys($local_header) as $key) {
115 $this->header
[$key] = $local_header[$key];
117 array_push($this->messages
, $part);
119 $this->_add_attachment($part);
122 $this->_add_attachment($part);
129 /** extract new headers from the part
130 * @param $part STRING part of a multipart message
132 function _get_part($part)
136 $lines = split("\n", $part);
137 while (count($lines)) {
138 $line = array_shift($lines);
140 if (preg_match('@^[\t\r ]+@', $line) && isset($hdr)) {
141 $local_headers[$hdr] .= ' '.trim($line);
143 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
144 $hdr = strtolower($hdr);
145 if (in_array($hdr, $banana->parse_hdr
)) {
146 $local_headers[$hdr] = $val;
153 $local_body = join("\n", $lines);
154 if (preg_match("/quoted-printable/", $local_headers['content-transfer-encoding'])) {
155 $local_body = quoted_printable_decode($local_body);
157 return Array('headers' => $local_headers, 'body' => $local_body);
160 /** add an attachment
162 function _add_attachment($part)
164 $local_header = $part['headers'];
165 $local_body = $part['body'];
167 if ((isset($local_header['content-disposition']) && preg_match("/filename=\"?([^\"]+)\"?/", $local_header['content-disposition'], $filename))
168 ||
(isset($local_header['content-type']) && preg_match("/name=\"?([^\"]+)\"?/", $local_header['content-type'], $filename))) {
169 $filename = $filename[1];
171 if (!isset($filename)) {
172 $filename = "attachment".count($pj);
175 if (isset($local_header['content-type'])) {
176 if (preg_match("/^\\s*([^ ;]+);/", $local_header['content-type'], $mimetype)) {
177 $mimetype = $mimetype[1];
180 if (!isset($mimetype)) {
184 array_push($this->pj
, Array('MIME' => $mimetype,
185 'filename' => $filename,
186 'encoding' => strtolower($local_header['content-transfer-encoding']),
187 'data' => $local_body));
191 /** return body in plain text (useful for messages without a text/plain part)
195 preg_match("@text/([^;]+);@", $this->headers
['content-type'], $format);
196 if ($format[1] == 'plain') {
199 if ($format[1] == 'richtext') {
200 return htmlToPlainText(richtextToHtml($this->body
));
202 return htmlToPlainText($this->body
);
206 /** decode an attachment
207 * @param pjid INT id of the attachment to decode
208 * @param action BOOL action to execute : true=view, false=download
210 function get_attachment($pjid, $action = false
)
212 if ($pjid >= count($this->pj
)) {
215 $file = $this->pj
[$pjid];
216 header('Content-Type: '.$file['MIME'].'; name="'.$file['filename'].'"');
218 header('Content-Disposition: attachment; filename="'.$file['filename'].'"');
220 header('Content-Disposition: inline; filename="'.$file['filename'].'"');
222 if ($file['encoding'] == 'base64') {
223 echo base64_decode($file['data']);
224 } else if ($file['encoding'] == 'x-uuencode') {
225 passthru('echo '.escapeshellarg($file['data']).' | uudecode -o /dev/stdout');
227 header('Content-Transfer-Encoding: '.$file['encoding']);
234 /** set body to represent the given part
235 * @param partid INT index of the part in messages
237 function set_body_to_part($partid)
241 if (count($this->messages
) == 0) {
245 $local_header = $this->messages
[$partid]['headers'];
246 $this->body
= $this->messages
[$partid]['body'];
247 foreach ($banana->parse_hdr
as $hdr) {
248 if (isset($local_header[$hdr])) {
249 $this->headers
[$hdr] = $local_header[$hdr];
253 if (preg_match('!charset=([^;]*)\s*(;|$)!', $this->headers
['content-type'], $matches)) {
254 $this->body
= iconv($matches[1], 'utf-8', $this->body
);
256 $this->body
= utf8_encode($this->body
);
264 $hdrs = $banana->nntp
->head($this->id
);
271 foreach ($hdrs as $line) {
272 if (preg_match("/^[\t\r ]+/", $line)) {
273 $line = ($hdr=="X-Face"?
"":" ").ltrim($line);
274 if (in_array($hdr, $banana->parse_hdr
)) {
275 $this->headers
[$hdr] .= $line;
278 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
279 $hdr = strtolower($hdr);
280 if (in_array($hdr, $banana->parse_hdr
)) {
281 $this->headers
[$hdr] = $val;
286 foreach ($banana->hdecode
as $hdr) {
287 if (isset($this->headers
[$hdr])) {
288 $this->headers
[$hdr] = headerDecode($this->headers
[$hdr]);
292 $this->name
= $this->headers
['from'];
293 $this->name
= preg_replace('/<[^ ]*>/', '', $this->name
);
294 $this->name
= trim($this->name
);
297 function checkcancel()
299 if (function_exists('hook_checkcancel')) {
300 return hook_checkcancel($this->headers
);
302 return ($this->headers
['from'] == $_SESSION['name']." <".$_SESSION['mail'].">");
305 /** convert message to html
306 * @param partid INT id of the multipart message that must be displaid
308 function to_html($partid = -1)
312 if (count($this->messages
) > 1) {
314 $this->set_body_to_part($partid);
316 // Select prefered text-format
317 foreach ($banana->body_mime
as $mime) {
318 for ($id = 0 ; $id < count($this->messages
) ; $id++
) {
319 if (preg_match("@$mime@", $this->messages
[$id]['headers']['content-type'])) {
321 $this->set_body_to_part($partid);
337 $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">';
338 $res .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>';
339 $res .= '<tr><td class="headers"><table cellpadding="0" cellspacing="0">';
341 foreach ($banana->show_hdr
as $hdr) {
342 if (isset($this->headers
[$hdr])) {
343 $res2 = formatdisplayheader($hdr, $this->headers
[$hdr]);
344 if ($res2 && ($hdr != 'x-face' ||
!$banana->formatxface
)) {
345 $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n";
351 $res .= '</table></td><td class="xface">';
356 $res .= '</td></tr>';
358 $res .= '<tr><th colspan="2">'._b_('Corps');
359 if (count($this->messages
) > 1) {
360 for ($i = 0 ; $i < count($this->messages
) ; $i++
) {
366 preg_match("@text/([^;]+);@", $this->messages
[$i]['headers']['content-type'], $format);
367 $format = textFormat_translate($format[1]);
369 $res .= '<a href="?group='.$banana->state
['group'].'&artid='.$this->id
.'&part='.$i.'">'.$format.'</a>';
375 $res .= '</th></tr>';
377 preg_match("@text/([^;]+);@", $this->headers
['content-type'], $format);
378 $format = $format[1];
379 $res .= '<tr><td colspan="2"';
380 if ($format == 'html') {
381 if (preg_match('@<body[^>]*bgcolor="?([#0-9a-f]+)"?[^>]*>@i', $this->body
, $bgcolor)) {
382 $res .= ' bgcolor="'.$bgcolor[1].'"';
384 $res .= '>'.formatbody($this->body
, $format);
386 $res .= '><pre>'.formatbody($this->body
).'</pre>';
388 $res .= '</td></tr>';
390 if (count($this->pj
) > 0) {
391 $res .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>';
392 $res .= '<tr><td colspan="2">';
394 foreach ($this->pj
as $file) {
395 $res .= $file['filename'].' ('.$file['MIME'].') : ';
396 $res .= '<a href="?group='.$banana->state
['group'].'&artid='.$this->id
.'&pj='.$i.'">télécharger</a>';
397 $res .= ' . <a href="?group='.$banana->state
['group'].'&artid='.$this->id
.'&pj='.$i.'&action=view" target="_blank">aperçu</a>';
401 $res .= '</td></tr>';
404 $res .= '<tr><th colspan="2">'._b_('Apercu').'</th></tr>';
405 $ndx = $banana->spool
->getndx($this->id
);
406 $res .= '<tr><td class="thrd" colspan="2">'.$banana->spool
->to_html($ndx-$banana->tbefore
, $ndx+
$banana->tafter
, $ndx).'</td></tr>';
408 return $res.'</table>';