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