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