Minimal support of multipart/related
[banana.git] / banana / post.inc.php
CommitLineData
78cd27b3 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
13class BananaPost
14{
15 var $id;
16 /** headers */
17 var $headers;
18 /** body */
19 var $body;
1f75b135 20 /** formating */
21 var $messages;
7a0e2710 22 /** attachment */
23 var $pj;
78cd27b3 24 /** poster name */
25 var $name;
856dc84a 26 /** test validity */
27 var $valid = true;
78cd27b3 28
29 /** constructor
30 * @param $_id STRING MSGNUM or MSGID (a group should be selected in this case)
31 */
32 function BananaPost($_id)
33 {
34 global $banana;
1f75b135 35 $this->id = $_id;
36 $this->pj = array();
37 $this->messages = array();
50cd076e 38 if (!$this->_header()) {
856dc84a 39 $this->valid = false;
50cd076e 40 return null;
41 }
42
78cd27b3 43
44 if ($body = $banana->nntp->body($_id)) {
45 $this->body = join("\n", $body);
46 } else {
856dc84a 47 $this->valid = false;
50cd076e 48 return null;
78cd27b3 49 }
50
51 if (isset($this->headers['content-transfer-encoding'])) {
52 if (preg_match("/base64/", $this->headers['content-transfer-encoding'])) {
53 $this->body = base64_decode($this->body);
54 } elseif (preg_match("/quoted-printable/", $this->headers['content-transfer-encoding'])) {
55 $this->body = quoted_printable_decode($this->body);
56 }
57 }
58
a7cb2f0d 59 if ($this->_split_multipart($this->headers, $this->body)) {
60 $this->set_body_to_part(0);
78cd27b3 61 } else {
a7cb2f0d 62 $this->_split_multipart($mpart_type[1], $mpart_boundary[1]);
c3f19b42 63 $this->_find_uuencode();
05074d42 64 $this->_fix_charset();
78cd27b3 65 }
66 }
67
c3f19b42 68 /** find and add uuencoded attachments
69 */
70 function _find_uuencode()
71 {
72 if (preg_match_all('@\n(begin \d+ ([^\r\n]+)\r?(?:\n(?!end)[^\n]*)*\nend)@', $this->body, $matches, PREG_SET_ORDER)) {
73 foreach ($matches as $match) {
74 $mime = trim(exec('echo '.escapeshellarg($match[1]).' | uudecode -o /dev/stdout | file -bi -'));
75 if ($mime != 'application/x-empty') {
f12fdb59 76 $this->body = trim(str_replace($match[0], '', $this->body));
c3f19b42 77 $body = $match[1];
78 $header['content-type'] = $mime.'; name="'.$match[2].'"';
79 $header['content-transfer-encoding'] = 'x-uuencode';
80 $header['content-disposition'] = 'attachment; filename="'.$match[2].'"';
81 $this->_add_attachment(Array('headers' => $header, 'body' => $body));
82 }
83 }
84 }
85 }
86
7a0e2710 87 /** split multipart messages
88 * @param $type STRING multipart type description
89 * @param $boundary STRING multipart boundary identification string
90 */
a7cb2f0d 91 function _split_multipart($headers, $body)
7a0e2710 92 {
a7cb2f0d 93 if (!preg_match("@multipart/([^;]+);@", $headers['content-type'], $type)) {
94 return false;
95 }
96
97 preg_match("/boundary=\"?([^ \"]+)\"?/", $headers['content-type'], $boundary);
98 $boundary = $boundary[1];
99 $type = $type[1];
100 $parts = preg_split("@\n--$boundary(--|\n)@", $body);
7a0e2710 101 foreach ($parts as $part) {
a7cb2f0d 102 $part = $this->_get_part($part);
7a0e2710 103 $local_header = $part['headers'];
a7cb2f0d 104 $local_body = $part['body'];
105 if (!$this->_split_multipart($local_header, $local_body)) {
28ab52c0 106 $is_text = isset($local_header['content-type'])
107 && preg_match("@text/([^;]+);@", $local_header['content-type'])
108 && (!isset($local_header['content-disposition'])
109 || !preg_match('@attachment@', $local_header['content-disposition']));
a7cb2f0d 110
111 // alternative ==> multiple formats for messages
112 if ($type == 'alternative' && $is_text) {
113 array_push($this->messages, $part);
114
115 // !alternative ==> une body, others are attachments
116 } else if ($is_text) {
117 if (count($this->messages) == 0) {
118 $this->body = $local_body;
119 foreach (array_keys($local_header) as $key) {
120 $this->header[$key] = $local_header[$key];
121 }
122 array_push($this->messages, $part);
123 } else {
124 $this->_add_attachment($part);
125 }
126 } else {
127 $this->_add_attachment($part);
128 }
cc43419f 129 }
a529ea7b 130 }
a7cb2f0d 131 return true;
7a0e2710 132 }
133
134 /** extract new headers from the part
135 * @param $part STRING part of a multipart message
136 */
a529ea7b 137 function _get_part($part)
1f75b135 138 {
7a0e2710 139 global $banana;
140
141 $lines = split("\n", $part);
142 while (count($lines)) {
143 $line = array_shift($lines);
144 if ($line != "") {
a7cb2f0d 145 if (preg_match('@^[\t\r ]+@', $line) && isset($hdr)) {
146 $local_headers[$hdr] .= ' '.trim($line);
147 } else {
148 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
149 $hdr = strtolower($hdr);
150 if (in_array($hdr, $banana->parse_hdr)) {
151 $local_headers[$hdr] = $val;
152 }
7a0e2710 153 }
154 } else {
155 break;
156 }
157 }
16b17ba5 158 $local_body = join("\n", $lines);
159 if (preg_match("/quoted-printable/", $local_headers['content-transfer-encoding'])) {
160 $local_body = quoted_printable_decode($local_body);
161 }
162 return Array('headers' => $local_headers, 'body' => $local_body);
7a0e2710 163 }
164
1f75b135 165 /** add an attachment
166 */
a529ea7b 167 function _add_attachment($part)
1f75b135 168 {
7a0e2710 169 $local_header = $part['headers'];
170 $local_body = $part['body'];
171
28ab52c0 172 if ((isset($local_header['content-disposition']) && preg_match('/filename="?([^"]+)"?/', $local_header['content-disposition'], $filename))
173 || (isset($local_header['content-type']) && preg_match('/name="?([^"]+)"?/', $local_header['content-type'], $filename))) {
a7cb2f0d 174 $filename = $filename[1];
175 }
7a0e2710 176 if (!isset($filename)) {
177 $filename = "attachment".count($pj);
178 }
179
28ab52c0 180 if (isset($local_header['content-type'])
181 && preg_match('/^\s*([^ ;]+);/', $local_header['content-type'], $mimetype)) {
182 $mimetype = $mimetype[1];
183 } else {
1f75b135 184 return false;
7a0e2710 185 }
186
28ab52c0 187 if (isset($local_header['content-id'])
188 && preg_match('/^\s*<([^> ]+)>/', $local_header['content-id'], $cid)) {
189 $cid = $cid[1];
190 } else {
191 $cid = null;
192 }
193
7a0e2710 194 array_push($this->pj, Array('MIME' => $mimetype,
195 'filename' => $filename,
196 'encoding' => strtolower($local_header['content-transfer-encoding']),
28ab52c0 197 'data' => $local_body,
198 'cid' => $cid));
1f75b135 199 return true;
7a0e2710 200 }
201
05074d42 202 /** Fix body charset (convert body to utf8)
203 * @return false if failed
204 */
205 function _fix_charset()
206 {
bc599299 207 if (preg_match('!charset="?([^;"]*)"?\s*(;|$)?!', $this->headers['content-type'], $matches)) {
05074d42 208 $body = iconv($matches[1], 'utf-8', $this->body);
209 if (strlen($body) == 0) {
210 return false;
211 }
212 $this->body = $body;
213 } else {
214 $this->body = utf8_encode($this->body);
215 }
216 return true;
217 }
218
a529ea7b 219 /** return body in plain text (useful for messages without a text/plain part)
220 */
221 function get_body()
222 {
223 preg_match("@text/([^;]+);@", $this->headers['content-type'], $format);
224 if ($format[1] == 'plain') {
225 return $this->body;
226 }
aef14768 227 if ($format[1] == 'richtext') {
228 return htmlToPlainText(richtextToHtml($this->body));
229 } else {
230 return htmlToPlainText($this->body);
a529ea7b 231 }
a529ea7b 232 }
233
28ab52c0 234 /** return local url for the given cid
235 * @param cid STRING
236 */
237 function find_attachment($cid)
238 {
239 global $banana;
240 $i = 0;
241 foreach ($this->pj as $pj) {
242 if ($pj['cid'] == $cid) {
243 return htmlentities(makeLink(Array('group' => $banana->state['group'],
244 'artid' => $this->id,
245 'pj' => $i,
246 'action' => 'view')));
247 }
248 $i++;
249 }
250 return 'cid:' . $cid;;
251 }
252
7a0e2710 253 /** decode an attachment
254 * @param pjid INT id of the attachment to decode
255 * @param action BOOL action to execute : true=view, false=download
256 */
1f75b135 257 function get_attachment($pjid, $action = false)
258 {
7a0e2710 259 if ($pjid >= count($this->pj)) {
260 return false;
261 } else {
262 $file = $this->pj[$pjid];
a529ea7b 263 header('Content-Type: '.$file['MIME'].'; name="'.$file['filename'].'"');
7a0e2710 264 if (!$action) {
265 header('Content-Disposition: attachment; filename="'.$file['filename'].'"');
a529ea7b 266 } else {
267 header('Content-Disposition: inline; filename="'.$file['filename'].'"');
268 }
7a0e2710 269 if ($file['encoding'] == 'base64') {
270 echo base64_decode($file['data']);
c3f19b42 271 } else if ($file['encoding'] == 'x-uuencode') {
272 passthru('echo '.escapeshellarg($file['data']).' | uudecode -o /dev/stdout');
7a0e2710 273 } else {
274 header('Content-Transfer-Encoding: '.$file['encoding']);
275 echo $file['data'];
276 }
277 return true;
278 }
279 }
280
1f75b135 281 /** set body to represent the given part
282 * @param partid INT index of the part in messages
283 */
284 function set_body_to_part($partid)
285 {
286 global $banana;
287
288 if (count($this->messages) == 0) {
289 return false;
290 }
291
292 $local_header = $this->messages[$partid]['headers'];
293 $this->body = $this->messages[$partid]['body'];
294 foreach ($banana->parse_hdr as $hdr) {
295 if (isset($local_header[$hdr])) {
296 $this->headers[$hdr] = $local_header[$hdr];
297 }
298 }
cc43419f 299
05074d42 300 $this->_fix_charset();
1f75b135 301 return true;
302 }
303
78cd27b3 304 function _header()
305 {
306 global $banana;
307 $hdrs = $banana->nntp->head($this->id);
308 if (!$hdrs) {
78cd27b3 309 return false;
310 }
311
312 // parse headers
313 foreach ($hdrs as $line) {
314 if (preg_match("/^[\t\r ]+/", $line)) {
315 $line = ($hdr=="X-Face"?"":" ").ltrim($line);
316 if (in_array($hdr, $banana->parse_hdr)) {
317 $this->headers[$hdr] .= $line;
318 }
319 } else {
320 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
321 $hdr = strtolower($hdr);
322 if (in_array($hdr, $banana->parse_hdr)) {
323 $this->headers[$hdr] = $val;
324 }
325 }
326 }
327 // decode headers
328 foreach ($banana->hdecode as $hdr) {
329 if (isset($this->headers[$hdr])) {
330 $this->headers[$hdr] = headerDecode($this->headers[$hdr]);
331 }
332 }
333
334 $this->name = $this->headers['from'];
335 $this->name = preg_replace('/<[^ ]*>/', '', $this->name);
336 $this->name = trim($this->name);
50cd076e 337 return true;
78cd27b3 338 }
339
340 function checkcancel()
341 {
342 if (function_exists('hook_checkcancel')) {
343 return hook_checkcancel($this->headers);
344 }
2ba32e89 345 if (!isset($_SESSION)) {
346 return false;
347 }
348 return ($this->headers['from'] == $_SESSION['name'] . ' <' . $_SESSION['mail']. '>');
349 }
350
351 /** Make some links to browse the current newsgroup
352 */
353 function _browser()
354 {
355 global $banana;
356 $ret = '<div class="banana_menu">';
edfc2d84 357 $actions = Array('prevThread' => Array('prev_thread', _b_('Discussion précédente')),
358 'prevPost' => Array('prev', _b_('Article précédent')),
359 'nextPost' => Array('next', _b_('Article suivant')),
360 'nextThread' => Array('next_thread', _b_('Discussion suivante')));
2ba32e89 361 foreach ($actions as $method=>$params) {
362 $id = $banana->spool->$method($this->id);
363 if (!is_null($id)) {
364 $ret .= makeImgLink(Array('group' => $banana->state['group'],
365 'artid' => $id),
366 $params[0] . '.gif',
367 $params[1]);
368 }
369 }
370 return $ret . '</div>';
78cd27b3 371 }
372
1f75b135 373 /** convert message to html
374 * @param partid INT id of the multipart message that must be displaid
375 */
cc43419f 376 function to_html($partid = -1)
78cd27b3 377 {
378 global $banana;
379
cc43419f 380 if (count($this->messages) > 1) {
381 if ($partid != -1) {
382 $this->set_body_to_part($partid);
383 } else {
384 // Select prefered text-format
385 foreach ($banana->body_mime as $mime) {
386 for ($id = 0 ; $id < count($this->messages) ; $id++) {
387 if (preg_match("@$mime@", $this->messages[$id]['headers']['content-type'])) {
388 $partid = $id;
389 $this->set_body_to_part($partid);
390 break;
391 }
392 }
393 if ($partid != -1) {
394 break;
395 }
396 }
397 if ($partid == -1) {
398 $partid = 0;
399 }
400 }
401 } else {
402 $partid = 0;
1f75b135 403 }
404
78cd27b3 405 $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">';
1c8d9e47 406 $res .= '<tr><th colspan="2" class="subject">'
2ba32e89 407 . $this->_browser()
408 . '<div class="banana_action">'
1254696c 409 . makeImgLink(Array('group' => $banana->state['group'],
2ba32e89 410 'action' => 'new'),
411 'post.gif',
edfc2d84 412 _b_('Nouveau message'))
2ba32e89 413 . makeImgLink(Array('group' => $banana->state['group'],
414 'artid' => $this->id,
415 'action' => 'new'),
416 'reply.gif',
edfc2d84 417 _b_('Répondre'));
2ba32e89 418 if ($this->checkCancel()) {
419 $res .= makeImgLink(Array('group' => $banana->state['group'],
420 'artid' => $this->id,
421 'action' => 'cancel'),
422 'cancel.gif',
edfc2d84 423 _b_('Annuler'));
2ba32e89 424 }
425 $res .= '</div>'
426 . formatDisplayHeader('subject', $this->headers['subject'])
427 . '</th></tr>'
428 . '<tr class="pair"><td class="headers"><table cellpadding="0" cellspacing="0">';
78cd27b3 429
430 foreach ($banana->show_hdr as $hdr) {
431 if (isset($this->headers[$hdr])) {
432 $res2 = formatdisplayheader($hdr, $this->headers[$hdr]);
ca3b1040 433 if ($res2 && ($hdr != 'x-face' || !$banana->formatxface)) {
78cd27b3 434 $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n";
ca3b1040 435 } else if ($res2) {
436 $xface = $res2;
78cd27b3 437 }
438 }
439 }
ca3b1040 440 $res .= '</table></td><td class="xface">';
441
442 if ($xface) {
443 $res .= $xface;
444 }
445 $res .= '</td></tr>';
78cd27b3 446
1f75b135 447 if (count($this->messages) > 1) {
1c8d9e47 448 $res .= '<tr><th colspan="2">';
1f75b135 449 for ($i = 0 ; $i < count($this->messages) ; $i++) {
1c8d9e47 450 if ($i != 0) {
1f75b135 451 $res .= ' . ';
452 }
453 preg_match("@text/([^;]+);@", $this->messages[$i]['headers']['content-type'], $format);
454 $format = textFormat_translate($format[1]);
455 if ($i != $partid) {
8e519bd8 456 $res .= makeHREF(Array('group' => $banana->state['group'],
f6df9eb2 457 'artid' => $this->id,
458 'part' => $i),
459 $format);
1f75b135 460 } else {
461 $res .= $format;
462 }
463 }
1c8d9e47 464 $res .= '</th></tr>';
1f75b135 465 }
1f75b135 466
467 preg_match("@text/([^;]+);@", $this->headers['content-type'], $format);
468 $format = $format[1];
8155c0a5 469 $res .= '<tr class="impair"><td colspan="2" class="body"';
1f75b135 470 if ($format == 'html') {
4cc80c9a 471 if (preg_match('@<body[^>]*bgcolor="?([#0-9a-f]+)"?[^>]*>@i', $this->body, $bgcolor)) {
472 $res .= ' bgcolor="'.$bgcolor[1].'"';
473 }
28ab52c0 474 $this->body = preg_replace('/cid:([^\'" ]+)/e', "find_attachment('\\1')", $this->body);
4cc80c9a 475 $res .= '>'.formatbody($this->body, $format);
1f75b135 476 } else {
4cc80c9a 477 $res .= '><pre>'.formatbody($this->body).'</pre>';
1f75b135 478 }
479 $res .= '</td></tr>';
7a0e2710 480
481 if (count($this->pj) > 0) {
482 $res .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>';
483 $res .= '<tr><td colspan="2">';
484 $i = 0;
485 foreach ($this->pj as $file) {
da6dc0c8 486 $res .= makeImgLink(Array('group' => $banana->state['group'],
487 'artid' => $this->id,
488 'pj' => $i),
489 'save.gif',
490 _b_('Télécharger')) . ' ';
491 $res .= makeImgLink(Array('group' => $banana->state['group'],
492 'artid' => $this->id,
493 'pj' => $i,
494 'action'=> 'view'),
495 'preview.gif',
496 _b_('Aperçu'));
497 $res .= ' ' . $file['filename'].' ('.$file['MIME'].')';
7a0e2710 498 $res .= '<br/>';
499 $i++;
500 }
501 $res .= '</td></tr>';
502 }
78cd27b3 503
78cd27b3 504 $ndx = $banana->spool->getndx($this->id);
e230b41a 505 $res .= '<tr><td class="thrd" colspan="2">'
506 . $banana->spool->to_html($ndx-$banana->tbefore, $ndx+$banana->tafter, $ndx)
507 . '</td></tr>';
78cd27b3 508 return $res.'</table>';
509 }
510}
511
28ab52c0 512/** Wrapper for Post::find_attachment
513 */
514function find_attachment($cid)
515{
516 global $banana;
517 return $banana->post->find_attachment($cid);
518}
519
f6df9eb2 520// vim:set et sw=4 sts=4 ts=4
78cd27b3 521?>