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 (preg_match("@multipart/([^;]+);@", $this->headers
['content-type'], $mpart_type)) {
53 preg_match("/boundary=\"?([^ \"]+)\"?/", $this->headers
['content-type'], $mpart_boundary);
54 $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($type, $boundary)
90 $parts = preg_split("/\n--$boundary(--|\n)/", $this->body
);
91 foreach ($parts as $part) {
92 $part = $this->_get_part($part);
93 $local_header = $part['headers'];
94 $local_body = $part['body'];
95 if (isset($local_header['content-disposition']) && preg_match("/attachment/", $local_header['content-disposition'])) {
96 $this->_add_attachment($part);
97 } else if (isset($local_header['content-type']) && preg_match("@text/([^;]+);@", $local_header['content-type'], $format)) {
98 array_push($this->messages
, $part);
101 $this->set_body_to_part(0);
104 /** extract new headers from the part
105 * @param $part STRING part of a multipart message
107 function _get_part($part)
111 $lines = split("\n", $part);
112 while (count($lines)) {
113 $line = array_shift($lines);
115 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
116 $hdr = strtolower($hdr);
117 if (in_array($hdr, $banana->parse_hdr
)) {
118 $local_headers[$hdr] = $val;
124 return Array('headers' => $local_headers, 'body' => join("\n", $lines));
127 /** add an attachment
129 function _add_attachment($part)
131 $local_header = $part['headers'];
132 $local_body = $part['body'];
134 if (!isset($local_header['content-transfer-encoding'])) {
138 if (isset($local_header['content-disposition'])) {
139 if (preg_match("/attachment/", $local_header['content-disposition'])) {
140 preg_match("/filename=\"?([^\"]+)\"?/", $local_header['content-disposition'], $filename);
141 $filename = $filename[1];
144 if (!isset($filename)) {
145 $filename = "attachment".count($pj);
148 if (isset($local_header['content-type'])) {
149 if (preg_match("/^\\s*([^ ;]+);/", $local_header['content-type'], $mimetype)) {
150 $mimetype = $mimetype[1];
153 if (!isset($mimetype)) {
157 array_push($this->pj
, Array('MIME' => $mimetype,
158 'filename' => $filename,
159 'encoding' => strtolower($local_header['content-transfer-encoding']),
160 'data' => $local_body));
164 /** return body in plain text (useful for messages without a text/plain part)
168 preg_match("@text/([^;]+);@", $this->headers
['content-type'], $format);
169 if ($format[1] == 'plain') {
172 if ($format[1] == 'richtext') {
173 return htmlToPlainText(richtextToHtml($this->body
));
175 return htmlToPlainText($this->body
);
179 /** decode an attachment
180 * @param pjid INT id of the attachment to decode
181 * @param action BOOL action to execute : true=view, false=download
183 function get_attachment($pjid, $action = false
)
185 if ($pjid >= count($this->pj
)) {
188 $file = $this->pj
[$pjid];
189 header('Content-Type: '.$file['MIME'].'; name="'.$file['filename'].'"');
191 header('Content-Disposition: attachment; filename="'.$file['filename'].'"');
193 header('Content-Disposition: inline; filename="'.$file['filename'].'"');
195 if ($file['encoding'] == 'base64') {
196 echo base64_decode($file['data']);
197 } else if ($file['encoding'] == 'x-uuencode') {
198 passthru('echo '.escapeshellarg($file['data']).' | uudecode -o /dev/stdout');
200 header('Content-Transfer-Encoding: '.$file['encoding']);
207 /** set body to represent the given part
208 * @param partid INT index of the part in messages
210 function set_body_to_part($partid)
214 if (count($this->messages
) == 0) {
218 $local_header = $this->messages
[$partid]['headers'];
219 $this->body
= $this->messages
[$partid]['body'];
220 foreach ($banana->parse_hdr
as $hdr) {
221 if (isset($local_header[$hdr])) {
222 $this->headers
[$hdr] = $local_header[$hdr];
226 if (preg_match('!charset=([^;]*)\s*(;|$)!', $this->headers
['content-type'], $matches)) {
227 $this->body
= iconv($matches[1], 'utf-8', $this->body
);
229 $this->body
= utf8_encode($this->body
);
237 $hdrs = $banana->nntp
->head($this->id
);
244 foreach ($hdrs as $line) {
245 if (preg_match("/^[\t\r ]+/", $line)) {
246 $line = ($hdr=="X-Face"?
"":" ").ltrim($line);
247 if (in_array($hdr, $banana->parse_hdr
)) {
248 $this->headers
[$hdr] .= $line;
251 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
252 $hdr = strtolower($hdr);
253 if (in_array($hdr, $banana->parse_hdr
)) {
254 $this->headers
[$hdr] = $val;
259 foreach ($banana->hdecode
as $hdr) {
260 if (isset($this->headers
[$hdr])) {
261 $this->headers
[$hdr] = headerDecode($this->headers
[$hdr]);
265 $this->name
= $this->headers
['from'];
266 $this->name
= preg_replace('/<[^ ]*>/', '', $this->name
);
267 $this->name
= trim($this->name
);
270 function checkcancel()
272 if (function_exists('hook_checkcancel')) {
273 return hook_checkcancel($this->headers
);
275 return ($this->headers
['from'] == $_SESSION['name']." <".$_SESSION['mail'].">");
278 /** convert message to html
279 * @param partid INT id of the multipart message that must be displaid
281 function to_html($partid = -1)
285 if (count($this->messages
) > 1) {
287 $this->set_body_to_part($partid);
289 // Select prefered text-format
290 foreach ($banana->body_mime
as $mime) {
291 for ($id = 0 ; $id < count($this->messages
) ; $id++
) {
292 if (preg_match("@$mime@", $this->messages
[$id]['headers']['content-type'])) {
294 $this->set_body_to_part($partid);
310 $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">';
311 $res .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>';
313 foreach ($banana->show_hdr
as $hdr) {
314 if (isset($this->headers
[$hdr])) {
315 $res2 = formatdisplayheader($hdr, $this->headers
[$hdr]);
317 $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n";
322 $res .= '<tr><th colspan="2">'._b_('Corps');
323 if (count($this->messages
) > 1) {
324 for ($i = 0 ; $i < count($this->messages
) ; $i++
) {
330 preg_match("@text/([^;]+);@", $this->messages
[$i]['headers']['content-type'], $format);
331 $format = textFormat_translate($format[1]);
333 $res .= '<a href="?group='.$banana->state
['group'].'&artid='.$this->id
.'&part='.$i.'">'.$format.'</a>';
339 $res .= '</th></tr>';
341 preg_match("@text/([^;]+);@", $this->headers
['content-type'], $format);
342 $format = $format[1];
343 $res .= '<tr><td colspan="2"';
344 if ($format == 'html') {
345 if (preg_match('@<body[^>]*bgcolor="?([#0-9a-f]+)"?[^>]*>@i', $this->body
, $bgcolor)) {
346 $res .= ' bgcolor="'.$bgcolor[1].'"';
348 $res .= '>'.formatbody($this->body
, $format);
350 $res .= '><pre>'.formatbody($this->body
).'</pre>';
352 $res .= '</td></tr>';
354 if (count($this->pj
) > 0) {
355 $res .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>';
356 $res .= '<tr><td colspan="2">';
358 foreach ($this->pj
as $file) {
359 $res .= $file['filename'].' ('.$file['MIME'].') : ';
360 $res .= '<a href="pj.php?group='.$banana->state
['group'].'&artid='.$this->id
.'&pj='.$i.'">télécharger</a>';
361 $res .= ' . <a href="pj.php?group='.$banana->state
['group'].'&artid='.$this->id
.'&pj='.$i.'&action=view" target="_blank">aperçu</a>';
365 $res .= '</td></tr>';
368 $res .= '<tr><th colspan="2">'._b_('Apercu').'</th></tr>';
369 $ndx = $banana->spool
->getndx($this->id
);
370 $res .= '<tr><td class="thrd" colspan="2">'.$banana->spool
->to_html($ndx-$banana->tbefore
, $ndx+
$banana->tafter
, $ndx).'</td></tr>';
372 return $res.'</table>';