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 ********************************************************************************/
26 * @param $_id STRING MSGNUM or MSGID (a group should be selected in this case)
28 function BananaPost($_id)
35 if ($body = $banana->nntp
->body($_id)) {
36 $this->body
= join("\n", $body);
38 return ($this = null
);
41 if (isset($this->headers
['content-transfer-encoding'])) {
42 if (preg_match("/base64/", $this->headers
['content-transfer-encoding'])) {
43 $this->body
= base64_decode($this->body
);
44 } elseif (preg_match("/quoted-printable/", $this->headers
['content-transfer-encoding'])) {
45 $this->body
= quoted_printable_decode($this->body
);
49 if (preg_match("@multipart/([^;]+);@", $this->headers
['content-type'], $mpart_type)) {
50 preg_match("/boundary=\"?([^ \"]+)\"?/", $this->headers
['content-type'], $mpart_boundary);
51 $this->split_multipart($mpart_type[1], $mpart_boundary[1]);
54 if (preg_match('!charset=([^;]*)\s*(;|$)!', $this->headers
['content-type'], $matches)) {
55 $this->body
= iconv($matches[1], 'utf-8', $this->body
);
57 $this->body
= utf8_encode($this->body
);
61 /** split multipart messages
62 * @param $type STRING multipart type description
63 * @param $boundary STRING multipart boundary identification string
65 function split_multipart($type, $boundary)
69 $parts = preg_split("/\n--$boundary(--|\n)/", $this->body
);
70 foreach ($parts as $part) {
71 $part = $this->get_part($part);
72 $local_header = $part['headers'];
73 $local_body = $part['body'];
74 if (isset($local_header['content-disposition']) && preg_match("/attachment/", $local_header['content-disposition'])) {
75 $this->add_attachment($part);
76 } else if (isset($local_header['content-type']) && preg_match("@text/@", $local_header['content-type'])) {
77 $this->body
= $local_body;
78 foreach ($banana->parse_hdr
as $hdr) {
79 if (isset($local_header[$hdr])) {
80 $this->headers
[$hdr] = $local_header[$hdr];
87 /** extract new headers from the part
88 * @param $part STRING part of a multipart message
90 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 # echo join("\n", $lines)."<br/>------------------------------------<br/>";
107 return Array('headers' => $local_headers, 'body' => join("\n", $lines));
110 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));
143 /** decode an attachment
144 * @param pjid INT id of the attachment to decode
145 * @param action BOOL action to execute : true=view, false=download
147 function get_attachment($pjid, $action = false
) {
148 if ($pjid >= count($this->pj
)) {
151 $file = $this->pj
[$pjid];
152 header('Content-Type: '.$file['MIME']);
154 header('Content-Disposition: attachment; filename="'.$file['filename'].'"');
156 if ($file['encoding'] == 'base64') {
157 echo base64_decode($file['data']);
159 header('Content-Transfer-Encoding: '.$file['encoding']);
169 $hdrs = $banana->nntp
->head($this->id
);
176 foreach ($hdrs as $line) {
177 if (preg_match("/^[\t\r ]+/", $line)) {
178 $line = ($hdr=="X-Face"?
"":" ").ltrim($line);
179 if (in_array($hdr, $banana->parse_hdr
)) {
180 $this->headers
[$hdr] .= $line;
183 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
184 $hdr = strtolower($hdr);
185 if (in_array($hdr, $banana->parse_hdr
)) {
186 $this->headers
[$hdr] = $val;
191 foreach ($banana->hdecode
as $hdr) {
192 if (isset($this->headers
[$hdr])) {
193 $this->headers
[$hdr] = headerDecode($this->headers
[$hdr]);
197 $this->name
= $this->headers
['from'];
198 $this->name
= preg_replace('/<[^ ]*>/', '', $this->name
);
199 $this->name
= trim($this->name
);
202 function checkcancel()
204 if (function_exists('hook_checkcancel')) {
205 return hook_checkcancel($this->headers
);
207 return ($this->headers
['from'] == $_SESSION['name']." <".$_SESSION['mail'].">");
214 $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">';
215 $res .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>';
217 foreach ($banana->show_hdr
as $hdr) {
218 if (isset($this->headers
[$hdr])) {
219 $res2 = formatdisplayheader($hdr, $this->headers
[$hdr]);
221 $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n";
226 $res .= '<tr><th colspan="2">'._b_('Corps').'</th></tr>';
227 $res .= '<tr><td colspan="2"><pre>'.formatbody($this->body
).'</pre></td></tr>';
229 if (count($this->pj
) > 0) {
230 $res .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>';
231 $res .= '<tr><td colspan="2">';
233 foreach ($this->pj
as $file) {
234 $res .= $file['filename'].' ('.$file['MIME'].') : ';
235 $res .= '<a href="pj.php?group='.$banana->state
['group'].'&artid='.$this->id
.'&pj='.$i.'">télécharger</a>';
236 if (preg_match("@(image|text)/@", $file['MIME'])) {
237 $res .= ' . <a href="pj.php?group='.$banana->state
['group'].'&artid='.$this->id
.'&pj='.$i.'&action=view">aperçu</a>';
242 $res .= '</td></tr>';
245 $res .= '<tr><th colspan="2">'._b_('apercu').'</th></tr>';
246 $ndx = $banana->spool
->getndx($this->id
);
247 $res .= '<tr><td class="thrd" colspan="2">'.$banana->spool
->to_html($ndx-$banana->tbefore
, $ndx+
$banana->tafter
, $ndx).'</td></tr>';
249 return $res.'</table>';