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]);
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
);
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 if (count($this->messages
) > 0) {
82 $this->set_body_to_part(0);
86 /** extract new headers from the part
87 * @param $part STRING part of a multipart message
89 function get_part($part)
93 $lines = split("\n", $part);
94 while (count($lines)) {
95 $line = array_shift($lines);
97 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
98 $hdr = strtolower($hdr);
99 if (in_array($hdr, $banana->parse_hdr
)) {
100 $local_headers[$hdr] = $val;
106 return Array('headers' => $local_headers, 'body' => join("\n", $lines));
109 /** add an attachment
111 function add_attachment($part)
113 $local_header = $part['headers'];
114 $local_body = $part['body'];
116 if (!isset($local_header['content-transfer-encoding'])) {
120 if (isset($local_header['content-disposition'])) {
121 if (preg_match("/attachment/", $local_header['content-disposition'])) {
122 preg_match("/filename=\"?([^\"]+)\"?/", $local_header['content-disposition'], $filename);
123 $filename = $filename[1];
126 if (!isset($filename)) {
127 $filename = "attachment".count($pj);
130 if (isset($local_header['content-type'])) {
131 if (preg_match("/^\\s*([^ ;]+);/", $local_header['content-type'], $mimetype)) {
132 $mimetype = $mimetype[1];
135 if (!isset($mimetype)) {
139 array_push($this->pj
, Array('MIME' => $mimetype,
140 'filename' => $filename,
141 'encoding' => strtolower($local_header['content-transfer-encoding']),
142 'data' => $local_body));
146 /** decode an attachment
147 * @param pjid INT id of the attachment to decode
148 * @param action BOOL action to execute : true=view, false=download
150 function get_attachment($pjid, $action = false
)
152 if ($pjid >= count($this->pj
)) {
155 $file = $this->pj
[$pjid];
156 header('Content-Type: '.$file['MIME']);
158 header('Content-Disposition: attachment; filename="'.$file['filename'].'"');
160 if ($file['encoding'] == 'base64') {
161 echo base64_decode($file['data']);
163 header('Content-Transfer-Encoding: '.$file['encoding']);
170 /** set body to represent the given part
171 * @param partid INT index of the part in messages
173 function set_body_to_part($partid)
177 if (count($this->messages
) == 0) {
181 $local_header = $this->messages
[$partid]['headers'];
182 $this->body
= $this->messages
[$partid]['body'];
183 foreach ($banana->parse_hdr
as $hdr) {
184 if (isset($local_header[$hdr])) {
185 $this->headers
[$hdr] = $local_header[$hdr];
194 $hdrs = $banana->nntp
->head($this->id
);
201 foreach ($hdrs as $line) {
202 if (preg_match("/^[\t\r ]+/", $line)) {
203 $line = ($hdr=="X-Face"?
"":" ").ltrim($line);
204 if (in_array($hdr, $banana->parse_hdr
)) {
205 $this->headers
[$hdr] .= $line;
208 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
209 $hdr = strtolower($hdr);
210 if (in_array($hdr, $banana->parse_hdr
)) {
211 $this->headers
[$hdr] = $val;
216 foreach ($banana->hdecode
as $hdr) {
217 if (isset($this->headers
[$hdr])) {
218 $this->headers
[$hdr] = headerDecode($this->headers
[$hdr]);
222 $this->name
= $this->headers
['from'];
223 $this->name
= preg_replace('/<[^ ]*>/', '', $this->name
);
224 $this->name
= trim($this->name
);
227 function checkcancel()
229 if (function_exists('hook_checkcancel')) {
230 return hook_checkcancel($this->headers
);
232 return ($this->headers
['from'] == $_SESSION['name']." <".$_SESSION['mail'].">");
235 /** convert message to html
236 * @param partid INT id of the multipart message that must be displaid
238 function to_html($partid = 0)
243 $this->set_body_to_part($partid);
246 $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">';
247 $res .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>';
249 foreach ($banana->show_hdr
as $hdr) {
250 if (isset($this->headers
[$hdr])) {
251 $res2 = formatdisplayheader($hdr, $this->headers
[$hdr]);
253 $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n";
258 $res .= '<tr><th colspan="2">'._b_('Corps');
259 if (count($this->messages
) > 1) {
260 for ($i = 0 ; $i < count($this->messages
) ; $i++
) {
266 preg_match("@text/([^;]+);@", $this->messages
[$i]['headers']['content-type'], $format);
267 $format = textFormat_translate($format[1]);
269 $res .= '<a href="?group='.$banana->state
['group'].'&artid='.$this->id
.'&part='.$i.'">'.$format.'</a>';
275 $res .= '</th></tr>';
277 preg_match("@text/([^;]+);@", $this->headers
['content-type'], $format);
278 $format = $format[1];
279 $res .= '<tr><td colspan="2">';
280 if ($format == 'html') {
281 $res .= formatbody($this->body
, $format);
283 $res .= '<pre>'.formatbody($this->body
).'</pre>';
285 $res .= '</td></tr>';
287 if (count($this->pj
) > 0) {
288 $res .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>';
289 $res .= '<tr><td colspan="2">';
291 foreach ($this->pj
as $file) {
292 $res .= $file['filename'].' ('.$file['MIME'].') : ';
293 $res .= '<a href="pj.php?group='.$banana->state
['group'].'&artid='.$this->id
.'&pj='.$i.'">télécharger</a>';
294 if (preg_match("@(image|text)/@", $file['MIME'])) {
295 $res .= ' . <a href="pj.php?group='.$banana->state
['group'].'&artid='.$this->id
.'&pj='.$i.'&action=view">aperçu</a>';
300 $res .= '</td></tr>';
303 $res .= '<tr><th colspan="2">'._b_('apercu').'</th></tr>';
304 $ndx = $banana->spool
->getndx($this->id
);
305 $res .= '<tr><td class="thrd" colspan="2">'.$banana->spool
->to_html($ndx-$banana->tbefore
, $ndx+
$banana->tafter
, $ndx).'</td></tr>';
307 return $res.'</table>';