df64f2715200fa46fff1e5f1fafbe2d4531a802c
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 if (preg_match('!charset=([^;]*)\s*(;|$)!', $this->headers
['content-type'], $matches)) {
57 $this->body
= iconv($matches[1], 'utf-8', $this->body
);
59 $this->body
= utf8_encode($this->body
);
64 /** split multipart messages
65 * @param $type STRING multipart type description
66 * @param $boundary STRING multipart boundary identification string
68 function _split_multipart($type, $boundary)
70 $parts = preg_split("/\n--$boundary(--|\n)/", $this->body
);
71 foreach ($parts as $part) {
72 $part = $this->_get_part($part);
73 $local_header = $part['headers'];
74 $local_body = $part['body'];
75 if (isset($local_header['content-disposition']) && preg_match("/attachment/", $local_header['content-disposition'])) {
76 $this->_add_attachment($part);
77 } else if (isset($local_header['content-type']) && preg_match("@text/([^;]+);@", $local_header['content-type'], $format)) {
78 array_push($this->messages
, $part);
81 $this->set_body_to_part(0);
84 /** extract new headers from the part
85 * @param $part STRING part of a multipart message
87 function _get_part($part)
91 $lines = split("\n", $part);
92 while (count($lines)) {
93 $line = array_shift($lines);
95 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
96 $hdr = strtolower($hdr);
97 if (in_array($hdr, $banana->parse_hdr
)) {
98 $local_headers[$hdr] = $val;
104 return Array('headers' => $local_headers, 'body' => join("\n", $lines));
107 /** add an attachment
109 function _add_attachment($part)
111 $local_header = $part['headers'];
112 $local_body = $part['body'];
114 if (!isset($local_header['content-transfer-encoding'])) {
118 if (isset($local_header['content-disposition'])) {
119 if (preg_match("/attachment/", $local_header['content-disposition'])) {
120 preg_match("/filename=\"?([^\"]+)\"?/", $local_header['content-disposition'], $filename);
121 $filename = $filename[1];
124 if (!isset($filename)) {
125 $filename = "attachment".count($pj);
128 if (isset($local_header['content-type'])) {
129 if (preg_match("/^\\s*([^ ;]+);/", $local_header['content-type'], $mimetype)) {
130 $mimetype = $mimetype[1];
133 if (!isset($mimetype)) {
137 array_push($this->pj
, Array('MIME' => $mimetype,
138 'filename' => $filename,
139 'encoding' => strtolower($local_header['content-transfer-encoding']),
140 'data' => $local_body));
144 /** return body in plain text (useful for messages without a text/plain part)
148 preg_match("@text/([^;]+);@", $this->headers
['content-type'], $format);
149 if ($format[1] == 'plain') {
152 $res = preg_replace("@<br[^>]>@", "@@@#@", $this->body
);
153 $res = trim(html_entity_decode(strip_tags($res)));
154 $res = str_replace("@@@#@", "\n", $res);
155 if (!is_utf8($res)) {
156 $res = utf8_encode($res);
161 /** decode an attachment
162 * @param pjid INT id of the attachment to decode
163 * @param action BOOL action to execute : true=view, false=download
165 function get_attachment($pjid, $action = false
)
167 if ($pjid >= count($this->pj
)) {
170 $file = $this->pj
[$pjid];
171 header('Content-Type: '.$file['MIME'].'; name="'.$file['filename'].'"');
173 header('Content-Disposition: attachment; filename="'.$file['filename'].'"');
175 header('Content-Disposition: inline; filename="'.$file['filename'].'"');
177 if ($file['encoding'] == 'base64') {
178 echo base64_decode($file['data']);
180 header('Content-Transfer-Encoding: '.$file['encoding']);
187 /** set body to represent the given part
188 * @param partid INT index of the part in messages
190 function set_body_to_part($partid)
194 if (count($this->messages
) == 0) {
198 $local_header = $this->messages
[$partid]['headers'];
199 $this->body
= $this->messages
[$partid]['body'];
200 foreach ($banana->parse_hdr
as $hdr) {
201 if (isset($local_header[$hdr])) {
202 $this->headers
[$hdr] = $local_header[$hdr];
206 if (preg_match('!charset=([^;]*)\s*(;|$)!', $this->headers
['content-type'], $matches)) {
207 $this->body
= iconv($matches[1], 'utf-8', $this->body
);
209 $this->body
= utf8_encode($this->body
);
217 $hdrs = $banana->nntp
->head($this->id
);
224 foreach ($hdrs as $line) {
225 if (preg_match("/^[\t\r ]+/", $line)) {
226 $line = ($hdr=="X-Face"?
"":" ").ltrim($line);
227 if (in_array($hdr, $banana->parse_hdr
)) {
228 $this->headers
[$hdr] .= $line;
231 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
232 $hdr = strtolower($hdr);
233 if (in_array($hdr, $banana->parse_hdr
)) {
234 $this->headers
[$hdr] = $val;
239 foreach ($banana->hdecode
as $hdr) {
240 if (isset($this->headers
[$hdr])) {
241 $this->headers
[$hdr] = headerDecode($this->headers
[$hdr]);
245 $this->name
= $this->headers
['from'];
246 $this->name
= preg_replace('/<[^ ]*>/', '', $this->name
);
247 $this->name
= trim($this->name
);
250 function checkcancel()
252 if (function_exists('hook_checkcancel')) {
253 return hook_checkcancel($this->headers
);
255 return ($this->headers
['from'] == $_SESSION['name']." <".$_SESSION['mail'].">");
258 /** convert message to html
259 * @param partid INT id of the multipart message that must be displaid
261 function to_html($partid = -1)
265 if (count($this->messages
) > 1) {
267 $this->set_body_to_part($partid);
269 // Select prefered text-format
270 foreach ($banana->body_mime
as $mime) {
271 for ($id = 0 ; $id < count($this->messages
) ; $id++
) {
272 if (preg_match("@$mime@", $this->messages
[$id]['headers']['content-type'])) {
274 $this->set_body_to_part($partid);
290 $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">';
291 $res .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>';
293 foreach ($banana->show_hdr
as $hdr) {
294 if (isset($this->headers
[$hdr])) {
295 $res2 = formatdisplayheader($hdr, $this->headers
[$hdr]);
297 $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n";
302 $res .= '<tr><th colspan="2">'._b_('Corps');
303 if (count($this->messages
) > 1) {
304 for ($i = 0 ; $i < count($this->messages
) ; $i++
) {
310 preg_match("@text/([^;]+);@", $this->messages
[$i]['headers']['content-type'], $format);
311 $format = textFormat_translate($format[1]);
313 $res .= '<a href="?group='.$banana->state
['group'].'&artid='.$this->id
.'&part='.$i.'">'.$format.'</a>';
319 $res .= '</th></tr>';
321 preg_match("@text/([^;]+);@", $this->headers
['content-type'], $format);
322 $format = $format[1];
323 $res .= '<tr><td colspan="2">';
324 if ($format == 'html') {
325 $res .= formatbody($this->body
, $format);
327 $res .= '<pre>'.formatbody($this->body
).'</pre>';
329 $res .= '</td></tr>';
331 if (count($this->pj
) > 0) {
332 $res .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>';
333 $res .= '<tr><td colspan="2">';
335 foreach ($this->pj
as $file) {
336 $res .= $file['filename'].' ('.$file['MIME'].') : ';
337 $res .= '<a href="pj.php?group='.$banana->state
['group'].'&artid='.$this->id
.'&pj='.$i.'">télécharger</a>';
338 $res .= ' . <a href="pj.php?group='.$banana->state
['group'].'&artid='.$this->id
.'&pj='.$i.'&action=view" target="_blank">aperçu</a>';
342 $res .= '</td></tr>';
345 $res .= '<tr><th colspan="2">'._b_('Apercu').'</th></tr>';
346 $ndx = $banana->spool
->getndx($this->id
);
347 $res .= '<tr><td class="thrd" colspan="2">'.$banana->spool
->to_html($ndx-$banana->tbefore
, $ndx+
$banana->tafter
, $ndx).'</td></tr>';
349 return $res.'</table>';