Gestion des piÚces jointes uuencode dans le cas de message non-multipart (Ex : Outlo...
[banana.git] / banana / post.inc.php
1 <?php
2 /********************************************************************************
3 * include/posts.inc.php : class for posts
4 * -----------------------
5 *
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
9
10 /** class for posts
11 */
12
13 class BananaPost
14 {
15 var $id;
16 /** headers */
17 var $headers;
18 /** body */
19 var $body;
20 /** formating */
21 var $messages;
22 /** attachment */
23 var $pj;
24 /** poster name */
25 var $name;
26
27 /** constructor
28 * @param $_id STRING MSGNUM or MSGID (a group should be selected in this case)
29 */
30 function BananaPost($_id)
31 {
32 global $banana;
33 $this->id = $_id;
34 $this->pj = array();
35 $this->messages = array();
36 $this->_header();
37
38 if ($body = $banana->nntp->body($_id)) {
39 $this->body = join("\n", $body);
40 } else {
41 return ($this = null);
42 }
43
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);
49 }
50 }
51
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]);
55 } else {
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);
59 } else {
60 $this->body = utf8_encode($this->body);
61 }
62 }
63 }
64
65 /** find and add uuencoded attachments
66 */
67 function _find_uuencode()
68 {
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 = str_replace($match[0], '', $this->body);
74 $body = $match[1];
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));
79 }
80 }
81 }
82 }
83
84 /** split multipart messages
85 * @param $type STRING multipart type description
86 * @param $boundary STRING multipart boundary identification string
87 */
88 function _split_multipart($type, $boundary)
89 {
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);
99 }
100 }
101 $this->set_body_to_part(0);
102 }
103
104 /** extract new headers from the part
105 * @param $part STRING part of a multipart message
106 */
107 function _get_part($part)
108 {
109 global $banana;
110
111 $lines = split("\n", $part);
112 while (count($lines)) {
113 $line = array_shift($lines);
114 if ($line != "") {
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;
119 }
120 } else {
121 break;
122 }
123 }
124 return Array('headers' => $local_headers, 'body' => join("\n", $lines));
125 }
126
127 /** add an attachment
128 */
129 function _add_attachment($part)
130 {
131 $local_header = $part['headers'];
132 $local_body = $part['body'];
133
134 if (!isset($local_header['content-transfer-encoding'])) {
135 return false;
136 }
137
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];
142 }
143 }
144 if (!isset($filename)) {
145 $filename = "attachment".count($pj);
146 }
147
148 if (isset($local_header['content-type'])) {
149 if (preg_match("/^\\s*([^ ;]+);/", $local_header['content-type'], $mimetype)) {
150 $mimetype = $mimetype[1];
151 }
152 }
153 if (!isset($mimetype)) {
154 return false;
155 }
156
157 array_push($this->pj, Array('MIME' => $mimetype,
158 'filename' => $filename,
159 'encoding' => strtolower($local_header['content-transfer-encoding']),
160 'data' => $local_body));
161 return true;
162 }
163
164 /** return body in plain text (useful for messages without a text/plain part)
165 */
166 function get_body()
167 {
168 preg_match("@text/([^;]+);@", $this->headers['content-type'], $format);
169 if ($format[1] == 'plain') {
170 return $this->body;
171 }
172 if ($format[1] == 'richtext') {
173 return htmlToPlainText(richtextToHtml($this->body));
174 } else {
175 return htmlToPlainText($this->body);
176 }
177 }
178
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
182 */
183 function get_attachment($pjid, $action = false)
184 {
185 if ($pjid >= count($this->pj)) {
186 return false;
187 } else {
188 $file = $this->pj[$pjid];
189 header('Content-Type: '.$file['MIME'].'; name="'.$file['filename'].'"');
190 if (!$action) {
191 header('Content-Disposition: attachment; filename="'.$file['filename'].'"');
192 } else {
193 header('Content-Disposition: inline; filename="'.$file['filename'].'"');
194 }
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');
199 } else {
200 header('Content-Transfer-Encoding: '.$file['encoding']);
201 echo $file['data'];
202 }
203 return true;
204 }
205 }
206
207 /** set body to represent the given part
208 * @param partid INT index of the part in messages
209 */
210 function set_body_to_part($partid)
211 {
212 global $banana;
213
214 if (count($this->messages) == 0) {
215 return false;
216 }
217
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];
223 }
224 }
225
226 if (preg_match('!charset=([^;]*)\s*(;|$)!', $this->headers['content-type'], $matches)) {
227 $this->body = iconv($matches[1], 'utf-8', $this->body);
228 } else {
229 $this->body = utf8_encode($this->body);
230 }
231 return true;
232 }
233
234 function _header()
235 {
236 global $banana;
237 $hdrs = $banana->nntp->head($this->id);
238 if (!$hdrs) {
239 $this = null;
240 return false;
241 }
242
243 // parse headers
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;
249 }
250 } else {
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;
255 }
256 }
257 }
258 // decode headers
259 foreach ($banana->hdecode as $hdr) {
260 if (isset($this->headers[$hdr])) {
261 $this->headers[$hdr] = headerDecode($this->headers[$hdr]);
262 }
263 }
264
265 $this->name = $this->headers['from'];
266 $this->name = preg_replace('/<[^ ]*>/', '', $this->name);
267 $this->name = trim($this->name);
268 }
269
270 function checkcancel()
271 {
272 if (function_exists('hook_checkcancel')) {
273 return hook_checkcancel($this->headers);
274 }
275 return ($this->headers['from'] == $_SESSION['name']." <".$_SESSION['mail'].">");
276 }
277
278 /** convert message to html
279 * @param partid INT id of the multipart message that must be displaid
280 */
281 function to_html($partid = -1)
282 {
283 global $banana;
284
285 if (count($this->messages) > 1) {
286 if ($partid != -1) {
287 $this->set_body_to_part($partid);
288 } else {
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'])) {
293 $partid = $id;
294 $this->set_body_to_part($partid);
295 break;
296 }
297 }
298 if ($partid != -1) {
299 break;
300 }
301 }
302 if ($partid == -1) {
303 $partid = 0;
304 }
305 }
306 } else {
307 $partid = 0;
308 }
309
310 $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">';
311 $res .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>';
312
313 foreach ($banana->show_hdr as $hdr) {
314 if (isset($this->headers[$hdr])) {
315 $res2 = formatdisplayheader($hdr, $this->headers[$hdr]);
316 if ($res2) {
317 $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n";
318 }
319 }
320 }
321
322 $res .= '<tr><th colspan="2">'._b_('Corps');
323 if (count($this->messages) > 1) {
324 for ($i = 0 ; $i < count($this->messages) ; $i++) {
325 if ($i == 0) {
326 $res .= ' : ';
327 } else {
328 $res .= ' . ';
329 }
330 preg_match("@text/([^;]+);@", $this->messages[$i]['headers']['content-type'], $format);
331 $format = textFormat_translate($format[1]);
332 if ($i != $partid) {
333 $res .= '<a href="?group='.$banana->state['group'].'&artid='.$this->id.'&part='.$i.'">'.$format.'</a>';
334 } else {
335 $res .= $format;
336 }
337 }
338 }
339 $res .= '</th></tr>';
340
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].'"';
347 }
348 $res .= '>'.formatbody($this->body, $format);
349 } else {
350 $res .= '><pre>'.formatbody($this->body).'</pre>';
351 }
352 $res .= '</td></tr>';
353
354 if (count($this->pj) > 0) {
355 $res .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>';
356 $res .= '<tr><td colspan="2">';
357 $i = 0;
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>';
362 $res .= '<br/>';
363 $i++;
364 }
365 $res .= '</td></tr>';
366 }
367
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>';
371
372 return $res.'</table>';
373 }
374 }
375
376 ?>