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 return Array('headers' => $local_headers, 'body' => join("\n", $lines));
156 /** add an attachment
158 function _add_attachment($part)
160 $local_header = $part['headers'];
161 $local_body = $part['body'];
163 if ((isset($local_header['content-disposition']) && preg_match("/filename=\"?([^\"]+)\"?/", $local_header['content-disposition'], $filename))
164 ||
(isset($local_header['content-type']) && preg_match("/name=\"?([^\"]+)\"?/", $local_header['content-type'], $filename))) {
165 $filename = $filename[1];
167 if (!isset($filename)) {
168 $filename = "attachment".count($pj);
171 if (isset($local_header['content-type'])) {
172 if (preg_match("/^\\s*([^ ;]+);/", $local_header['content-type'], $mimetype)) {
173 $mimetype = $mimetype[1];
176 if (!isset($mimetype)) {
180 array_push($this->pj
, Array('MIME' => $mimetype,
181 'filename' => $filename,
182 'encoding' => strtolower($local_header['content-transfer-encoding']),
183 'data' => $local_body));
187 /** return body in plain text (useful for messages without a text/plain part)
191 preg_match("@text/([^;]+);@", $this->headers
['content-type'], $format);
192 if ($format[1] == 'plain') {
195 if ($format[1] == 'richtext') {
196 return htmlToPlainText(richtextToHtml($this->body
));
198 return htmlToPlainText($this->body
);
202 /** decode an attachment
203 * @param pjid INT id of the attachment to decode
204 * @param action BOOL action to execute : true=view, false=download
206 function get_attachment($pjid, $action = false
)
208 if ($pjid >= count($this->pj
)) {
211 $file = $this->pj
[$pjid];
212 header('Content-Type: '.$file['MIME'].'; name="'.$file['filename'].'"');
214 header('Content-Disposition: attachment; filename="'.$file['filename'].'"');
216 header('Content-Disposition: inline; filename="'.$file['filename'].'"');
218 if ($file['encoding'] == 'base64') {
219 echo base64_decode($file['data']);
220 } else if ($file['encoding'] == 'x-uuencode') {
221 passthru('echo '.escapeshellarg($file['data']).' | uudecode -o /dev/stdout');
223 header('Content-Transfer-Encoding: '.$file['encoding']);
230 /** set body to represent the given part
231 * @param partid INT index of the part in messages
233 function set_body_to_part($partid)
237 if (count($this->messages
) == 0) {
241 $local_header = $this->messages
[$partid]['headers'];
242 $this->body
= $this->messages
[$partid]['body'];
243 foreach ($banana->parse_hdr
as $hdr) {
244 if (isset($local_header[$hdr])) {
245 $this->headers
[$hdr] = $local_header[$hdr];
249 if (preg_match('!charset=([^;]*)\s*(;|$)!', $this->headers
['content-type'], $matches)) {
250 $this->body
= iconv($matches[1], 'utf-8', $this->body
);
252 $this->body
= utf8_encode($this->body
);
260 $hdrs = $banana->nntp
->head($this->id
);
267 foreach ($hdrs as $line) {
268 if (preg_match("/^[\t\r ]+/", $line)) {
269 $line = ($hdr=="X-Face"?
"":" ").ltrim($line);
270 if (in_array($hdr, $banana->parse_hdr
)) {
271 $this->headers
[$hdr] .= $line;
274 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
275 $hdr = strtolower($hdr);
276 if (in_array($hdr, $banana->parse_hdr
)) {
277 $this->headers
[$hdr] = $val;
282 foreach ($banana->hdecode
as $hdr) {
283 if (isset($this->headers
[$hdr])) {
284 $this->headers
[$hdr] = headerDecode($this->headers
[$hdr]);
288 $this->name
= $this->headers
['from'];
289 $this->name
= preg_replace('/<[^ ]*>/', '', $this->name
);
290 $this->name
= trim($this->name
);
293 function checkcancel()
295 if (function_exists('hook_checkcancel')) {
296 return hook_checkcancel($this->headers
);
298 return ($this->headers
['from'] == $_SESSION['name']." <".$_SESSION['mail'].">");
301 /** convert message to html
302 * @param partid INT id of the multipart message that must be displaid
304 function to_html($partid = -1)
308 if (count($this->messages
) > 1) {
310 $this->set_body_to_part($partid);
312 // Select prefered text-format
313 foreach ($banana->body_mime
as $mime) {
314 for ($id = 0 ; $id < count($this->messages
) ; $id++
) {
315 if (preg_match("@$mime@", $this->messages
[$id]['headers']['content-type'])) {
317 $this->set_body_to_part($partid);
333 $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">';
334 $res .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>';
336 foreach ($banana->show_hdr
as $hdr) {
337 if (isset($this->headers
[$hdr])) {
338 $res2 = formatdisplayheader($hdr, $this->headers
[$hdr]);
340 $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n";
345 $res .= '<tr><th colspan="2">'._b_('Corps');
346 if (count($this->messages
) > 1) {
347 for ($i = 0 ; $i < count($this->messages
) ; $i++
) {
353 preg_match("@text/([^;]+);@", $this->messages
[$i]['headers']['content-type'], $format);
354 $format = textFormat_translate($format[1]);
356 $res .= '<a href="?group='.$banana->state
['group'].'&artid='.$this->id
.'&part='.$i.'">'.$format.'</a>';
362 $res .= '</th></tr>';
364 preg_match("@text/([^;]+);@", $this->headers
['content-type'], $format);
365 $format = $format[1];
366 $res .= '<tr><td colspan="2"';
367 if ($format == 'html') {
368 if (preg_match('@<body[^>]*bgcolor="?([#0-9a-f]+)"?[^>]*>@i', $this->body
, $bgcolor)) {
369 $res .= ' bgcolor="'.$bgcolor[1].'"';
371 $res .= '>'.formatbody($this->body
, $format);
373 $res .= '><pre>'.formatbody($this->body
).'</pre>';
375 $res .= '</td></tr>';
377 if (count($this->pj
) > 0) {
378 $res .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>';
379 $res .= '<tr><td colspan="2">';
381 foreach ($this->pj
as $file) {
382 $res .= $file['filename'].' ('.$file['MIME'].') : ';
383 $res .= '<a href="pj.php?group='.$banana->state
['group'].'&artid='.$this->id
.'&pj='.$i.'">télécharger</a>';
384 $res .= ' . <a href="pj.php?group='.$banana->state
['group'].'&artid='.$this->id
.'&pj='.$i.'&action=view" target="_blank">aperçu</a>';
388 $res .= '</td></tr>';
391 $res .= '<tr><th colspan="2">'._b_('Apercu').'</th></tr>';
392 $ndx = $banana->spool
->getndx($this->id
);
393 $res .= '<tr><td class="thrd" colspan="2">'.$banana->spool
->to_html($ndx-$banana->tbefore
, $ndx+
$banana->tafter
, $ndx).'</td></tr>';
395 return $res.'</table>';