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