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 if (preg_match('!charset=([^;]*)\s*(;|$)!', $this->headers
['content-type'], $matches)) {
65 $this->body
= iconv($matches[1], 'utf-8', $this->body
);
67 $this->body
= utf8_encode($this->body
);
72 /** find and add uuencoded attachments
74 function _find_uuencode()
76 if (preg_match_all('@\n(begin \d+ ([^\r\n]+)\r?(?:\n(?!end)[^\n]*)*\nend)@', $this->body
, $matches, PREG_SET_ORDER
)) {
77 foreach ($matches as $match) {
78 $mime = trim(exec('echo '.escapeshellarg($match[1]).' | uudecode -o /dev/stdout | file -bi -'));
79 if ($mime != 'application/x-empty') {
80 $this->body
= trim(str_replace($match[0], '', $this->body
));
82 $header['content-type'] = $mime.'; name="'.$match[2].'"';
83 $header['content-transfer-encoding'] = 'x-uuencode';
84 $header['content-disposition'] = 'attachment; filename="'.$match[2].'"';
85 $this->_add_attachment(Array('headers' => $header, 'body' => $body));
91 /** split multipart messages
92 * @param $type STRING multipart type description
93 * @param $boundary STRING multipart boundary identification string
95 function _split_multipart($headers, $body)
97 if (!preg_match("@multipart/([^;]+);@", $headers['content-type'], $type)) {
101 preg_match("/boundary=\"?([^ \"]+)\"?/", $headers['content-type'], $boundary);
102 $boundary = $boundary[1];
104 $parts = preg_split("@\n--$boundary(--|\n)@", $body);
105 foreach ($parts as $part) {
106 $part = $this->_get_part($part);
107 $local_header = $part['headers'];
108 $local_body = $part['body'];
109 if (!$this->_split_multipart($local_header, $local_body)) {
110 $is_text = isset($local_header['content-type']) && preg_match("@text/([^;]+);@", $local_header['content-type'])
111 && (!isset($local_header['content-disposition']) ||
!preg_match('@attachment@', $local_header['content-disposition']));
113 // alternative ==> multiple formats for messages
114 if ($type == 'alternative' && $is_text) {
115 array_push($this->messages
, $part);
117 // !alternative ==> une body, others are attachments
118 } else if ($is_text) {
119 if (count($this->messages
) == 0) {
120 $this->body
= $local_body;
121 foreach (array_keys($local_header) as $key) {
122 $this->header
[$key] = $local_header[$key];
124 array_push($this->messages
, $part);
126 $this->_add_attachment($part);
129 $this->_add_attachment($part);
136 /** extract new headers from the part
137 * @param $part STRING part of a multipart message
139 function _get_part($part)
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 (preg_match("/quoted-printable/", $local_headers['content-transfer-encoding'])) {
162 $local_body = quoted_printable_decode($local_body);
164 return Array('headers' => $local_headers, 'body' => $local_body);
167 /** add an attachment
169 function _add_attachment($part)
171 $local_header = $part['headers'];
172 $local_body = $part['body'];
174 if ((isset($local_header['content-disposition']) && preg_match("/filename=\"?([^\"]+)\"?/", $local_header['content-disposition'], $filename))
175 ||
(isset($local_header['content-type']) && preg_match("/name=\"?([^\"]+)\"?/", $local_header['content-type'], $filename))) {
176 $filename = $filename[1];
178 if (!isset($filename)) {
179 $filename = "attachment".count($pj);
182 if (isset($local_header['content-type'])) {
183 if (preg_match("/^\\s*([^ ;]+);/", $local_header['content-type'], $mimetype)) {
184 $mimetype = $mimetype[1];
187 if (!isset($mimetype)) {
191 array_push($this->pj
, Array('MIME' => $mimetype,
192 'filename' => $filename,
193 'encoding' => strtolower($local_header['content-transfer-encoding']),
194 'data' => $local_body));
198 /** return body in plain text (useful for messages without a text/plain part)
202 preg_match("@text/([^;]+);@", $this->headers
['content-type'], $format);
203 if ($format[1] == 'plain') {
206 if ($format[1] == 'richtext') {
207 return htmlToPlainText(richtextToHtml($this->body
));
209 return htmlToPlainText($this->body
);
213 /** decode an attachment
214 * @param pjid INT id of the attachment to decode
215 * @param action BOOL action to execute : true=view, false=download
217 function get_attachment($pjid, $action = false
)
219 if ($pjid >= count($this->pj
)) {
222 $file = $this->pj
[$pjid];
223 header('Content-Type: '.$file['MIME'].'; name="'.$file['filename'].'"');
225 header('Content-Disposition: attachment; filename="'.$file['filename'].'"');
227 header('Content-Disposition: inline; filename="'.$file['filename'].'"');
229 if ($file['encoding'] == 'base64') {
230 echo base64_decode($file['data']);
231 } else if ($file['encoding'] == 'x-uuencode') {
232 passthru('echo '.escapeshellarg($file['data']).' | uudecode -o /dev/stdout');
234 header('Content-Transfer-Encoding: '.$file['encoding']);
241 /** set body to represent the given part
242 * @param partid INT index of the part in messages
244 function set_body_to_part($partid)
248 if (count($this->messages
) == 0) {
252 $local_header = $this->messages
[$partid]['headers'];
253 $this->body
= $this->messages
[$partid]['body'];
254 foreach ($banana->parse_hdr
as $hdr) {
255 if (isset($local_header[$hdr])) {
256 $this->headers
[$hdr] = $local_header[$hdr];
260 if (preg_match('!charset=([^;]*)\s*(;|$)!', $this->headers
['content-type'], $matches)) {
261 $this->body
= iconv($matches[1], 'utf-8', $this->body
);
263 $this->body
= utf8_encode($this->body
);
271 $hdrs = $banana->nntp
->head($this->id
);
277 foreach ($hdrs as $line) {
278 if (preg_match("/^[\t\r ]+/", $line)) {
279 $line = ($hdr=="X-Face"?
"":" ").ltrim($line);
280 if (in_array($hdr, $banana->parse_hdr
)) {
281 $this->headers
[$hdr] .= $line;
284 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
285 $hdr = strtolower($hdr);
286 if (in_array($hdr, $banana->parse_hdr
)) {
287 $this->headers
[$hdr] = $val;
292 foreach ($banana->hdecode
as $hdr) {
293 if (isset($this->headers
[$hdr])) {
294 $this->headers
[$hdr] = headerDecode($this->headers
[$hdr]);
298 $this->name
= $this->headers
['from'];
299 $this->name
= preg_replace('/<[^ ]*>/', '', $this->name
);
300 $this->name
= trim($this->name
);
304 function checkcancel()
306 if (function_exists('hook_checkcancel')) {
307 return hook_checkcancel($this->headers
);
309 return ($this->headers
['from'] == $_SESSION['name']." <".$_SESSION['mail'].">");
312 /** convert message to html
313 * @param partid INT id of the multipart message that must be displaid
315 function to_html($partid = -1)
319 if (count($this->messages
) > 1) {
321 $this->set_body_to_part($partid);
323 // Select prefered text-format
324 foreach ($banana->body_mime
as $mime) {
325 for ($id = 0 ; $id < count($this->messages
) ; $id++
) {
326 if (preg_match("@$mime@", $this->messages
[$id]['headers']['content-type'])) {
328 $this->set_body_to_part($partid);
344 $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">';
345 $res .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>';
346 $res .= '<tr><td class="headers"><table cellpadding="0" cellspacing="0">';
348 foreach ($banana->show_hdr
as $hdr) {
349 if (isset($this->headers
[$hdr])) {
350 $res2 = formatdisplayheader($hdr, $this->headers
[$hdr]);
351 if ($res2 && ($hdr != 'x-face' ||
!$banana->formatxface
)) {
352 $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n";
358 $res .= '</table></td><td class="xface">';
363 $res .= '</td></tr>';
365 $res .= '<tr><th colspan="2">'._b_('Corps');
366 if (count($this->messages
) > 1) {
367 for ($i = 0 ; $i < count($this->messages
) ; $i++
) {
373 preg_match("@text/([^;]+);@", $this->messages
[$i]['headers']['content-type'], $format);
374 $format = textFormat_translate($format[1]);
376 $res .= makeHREF(Array('group' => $banana->state
['group'],
377 'artid' => $this->id
,
385 $res .= '</th></tr>';
387 preg_match("@text/([^;]+);@", $this->headers
['content-type'], $format);
388 $format = $format[1];
389 $res .= '<tr><td colspan="2"';
390 if ($format == 'html') {
391 if (preg_match('@<body[^>]*bgcolor="?([#0-9a-f]+)"?[^>]*>@i', $this->body
, $bgcolor)) {
392 $res .= ' bgcolor="'.$bgcolor[1].'"';
394 $res .= '>'.formatbody($this->body
, $format);
396 $res .= '><pre>'.formatbody($this->body
).'</pre>';
398 $res .= '</td></tr>';
400 if (count($this->pj
) > 0) {
401 $res .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>';
402 $res .= '<tr><td colspan="2">';
404 foreach ($this->pj
as $file) {
405 $res .= $file['filename'].' ('.$file['MIME'].') : ';
406 $res .= makeHREF(Array('group' => $banana->state
['group'],
407 'artid' => $this->id
,
411 $res .= makeHREF(Array('group' => $banana->state
['group'],
412 'artid' => $this->id
,
419 $res .= '</td></tr>';
422 $res .= '<tr><th colspan="2">'._b_('Apercu').'</th></tr>';
423 $ndx = $banana->spool
->getndx($this->id
);
424 $res .= '<tr><td class="thrd" colspan="2">'.$banana->spool
->to_html($ndx-$banana->tbefore
, $ndx+
$banana->tafter
, $ndx).'</td></tr>';
426 return $res.'</table>';
430 // vim:set et sw=4 sts=4 ts=4