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 | |
13 | class 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); |
75c9bae4 |
61 | } elseif(isset($mpart_type)) { |
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 | { |
75c9bae4 |
93 | if (!isset($headers['content-type']) |
94 | || !preg_match("@multipart/([^;]+);@", $headers['content-type'], $type)) { |
a7cb2f0d |
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); |
7a0e2710 |
102 | foreach ($parts as $part) { |
a7cb2f0d |
103 | $part = $this->_get_part($part); |
7a0e2710 |
104 | $local_header = $part['headers']; |
a7cb2f0d |
105 | $local_body = $part['body']; |
106 | if (!$this->_split_multipart($local_header, $local_body)) { |
28ab52c0 |
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'])); |
a7cb2f0d |
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 | } |
cc43419f |
130 | } |
a529ea7b |
131 | } |
a7cb2f0d |
132 | return true; |
7a0e2710 |
133 | } |
134 | |
135 | /** extract new headers from the part |
136 | * @param $part STRING part of a multipart message |
137 | */ |
a529ea7b |
138 | function _get_part($part) |
1f75b135 |
139 | { |
7a0e2710 |
140 | global $banana; |
141 | |
75c9bae4 |
142 | $local_headers = Array(); |
7a0e2710 |
143 | $lines = split("\n", $part); |
144 | while (count($lines)) { |
145 | $line = array_shift($lines); |
146 | if ($line != "") { |
a7cb2f0d |
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 | } |
7a0e2710 |
155 | } |
156 | } else { |
157 | break; |
158 | } |
159 | } |
16b17ba5 |
160 | $local_body = join("\n", $lines); |
75c9bae4 |
161 | if (isset($local_headers['content-transfer-encoding']) |
162 | && preg_match("/quoted-printable/", $local_headers['content-transfer-encoding'])) { |
16b17ba5 |
163 | $local_body = quoted_printable_decode($local_body); |
164 | } |
165 | return Array('headers' => $local_headers, 'body' => $local_body); |
7a0e2710 |
166 | } |
167 | |
1f75b135 |
168 | /** add an attachment |
169 | */ |
a529ea7b |
170 | function _add_attachment($part) |
1f75b135 |
171 | { |
7a0e2710 |
172 | $local_header = $part['headers']; |
173 | $local_body = $part['body']; |
174 | |
28ab52c0 |
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))) { |
a7cb2f0d |
177 | $filename = $filename[1]; |
178 | } |
7a0e2710 |
179 | if (!isset($filename)) { |
75c9bae4 |
180 | $filename = "attachment" . count($this->pj); |
7a0e2710 |
181 | } |
182 | |
28ab52c0 |
183 | if (isset($local_header['content-type']) |
184 | && preg_match('/^\s*([^ ;]+);/', $local_header['content-type'], $mimetype)) { |
185 | $mimetype = $mimetype[1]; |
186 | } else { |
1f75b135 |
187 | return false; |
7a0e2710 |
188 | } |
189 | |
28ab52c0 |
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 | |
7a0e2710 |
197 | array_push($this->pj, Array('MIME' => $mimetype, |
198 | 'filename' => $filename, |
199 | 'encoding' => strtolower($local_header['content-transfer-encoding']), |
28ab52c0 |
200 | 'data' => $local_body, |
201 | 'cid' => $cid)); |
1f75b135 |
202 | return true; |
7a0e2710 |
203 | } |
204 | |
05074d42 |
205 | /** Fix body charset (convert body to utf8) |
206 | * @return false if failed |
207 | */ |
208 | function _fix_charset() |
209 | { |
bc599299 |
210 | if (preg_match('!charset="?([^;"]*)"?\s*(;|$)?!', $this->headers['content-type'], $matches)) { |
05074d42 |
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 | |
a529ea7b |
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 | } |
aef14768 |
230 | if ($format[1] == 'richtext') { |
231 | return htmlToPlainText(richtextToHtml($this->body)); |
232 | } else { |
233 | return htmlToPlainText($this->body); |
a529ea7b |
234 | } |
a529ea7b |
235 | } |
236 | |
28ab52c0 |
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 | |
7a0e2710 |
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 | */ |
1f75b135 |
260 | function get_attachment($pjid, $action = false) |
261 | { |
7a0e2710 |
262 | if ($pjid >= count($this->pj)) { |
263 | return false; |
264 | } else { |
265 | $file = $this->pj[$pjid]; |
a529ea7b |
266 | header('Content-Type: '.$file['MIME'].'; name="'.$file['filename'].'"'); |
7a0e2710 |
267 | if (!$action) { |
268 | header('Content-Disposition: attachment; filename="'.$file['filename'].'"'); |
a529ea7b |
269 | } else { |
270 | header('Content-Disposition: inline; filename="'.$file['filename'].'"'); |
271 | } |
7a0e2710 |
272 | if ($file['encoding'] == 'base64') { |
273 | echo base64_decode($file['data']); |
c3f19b42 |
274 | } else if ($file['encoding'] == 'x-uuencode') { |
275 | passthru('echo '.escapeshellarg($file['data']).' | uudecode -o /dev/stdout'); |
7a0e2710 |
276 | } else { |
277 | header('Content-Transfer-Encoding: '.$file['encoding']); |
278 | echo $file['data']; |
279 | } |
280 | return true; |
281 | } |
282 | } |
283 | |
1f75b135 |
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 | } |
cc43419f |
302 | |
05074d42 |
303 | $this->_fix_charset(); |
1f75b135 |
304 | return true; |
305 | } |
306 | |
78cd27b3 |
307 | function _header() |
308 | { |
309 | global $banana; |
310 | $hdrs = $banana->nntp->head($this->id); |
311 | if (!$hdrs) { |
78cd27b3 |
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); |
50cd076e |
340 | return true; |
78cd27b3 |
341 | } |
342 | |
343 | function checkcancel() |
344 | { |
345 | if (function_exists('hook_checkcancel')) { |
346 | return hook_checkcancel($this->headers); |
347 | } |
2ba32e89 |
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">'; |
edfc2d84 |
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'))); |
2ba32e89 |
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>'; |
78cd27b3 |
374 | } |
375 | |
1f75b135 |
376 | /** convert message to html |
377 | * @param partid INT id of the multipart message that must be displaid |
378 | */ |
cc43419f |
379 | function to_html($partid = -1) |
78cd27b3 |
380 | { |
381 | global $banana; |
382 | |
cc43419f |
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; |
1f75b135 |
406 | } |
407 | |
78cd27b3 |
408 | $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">'; |
1c8d9e47 |
409 | $res .= '<tr><th colspan="2" class="subject">' |
2ba32e89 |
410 | . $this->_browser() |
411 | . '<div class="banana_action">' |
1254696c |
412 | . makeImgLink(Array('group' => $banana->state['group'], |
2ba32e89 |
413 | 'action' => 'new'), |
414 | 'post.gif', |
edfc2d84 |
415 | _b_('Nouveau message')) |
2ba32e89 |
416 | . makeImgLink(Array('group' => $banana->state['group'], |
417 | 'artid' => $this->id, |
418 | 'action' => 'new'), |
419 | 'reply.gif', |
edfc2d84 |
420 | _b_('Répondre')); |
2ba32e89 |
421 | if ($this->checkCancel()) { |
422 | $res .= makeImgLink(Array('group' => $banana->state['group'], |
423 | 'artid' => $this->id, |
424 | 'action' => 'cancel'), |
425 | 'cancel.gif', |
edfc2d84 |
426 | _b_('Annuler')); |
2ba32e89 |
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">'; |
78cd27b3 |
432 | |
75c9bae4 |
433 | $xface = null; |
78cd27b3 |
434 | foreach ($banana->show_hdr as $hdr) { |
435 | if (isset($this->headers[$hdr])) { |
436 | $res2 = formatdisplayheader($hdr, $this->headers[$hdr]); |
ca3b1040 |
437 | if ($res2 && ($hdr != 'x-face' || !$banana->formatxface)) { |
78cd27b3 |
438 | $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n"; |
ca3b1040 |
439 | } else if ($res2) { |
440 | $xface = $res2; |
78cd27b3 |
441 | } |
442 | } |
443 | } |
ca3b1040 |
444 | $res .= '</table></td><td class="xface">'; |
445 | |
446 | if ($xface) { |
447 | $res .= $xface; |
448 | } |
449 | $res .= '</td></tr>'; |
78cd27b3 |
450 | |
1f75b135 |
451 | if (count($this->messages) > 1) { |
1c8d9e47 |
452 | $res .= '<tr><th colspan="2">'; |
1f75b135 |
453 | for ($i = 0 ; $i < count($this->messages) ; $i++) { |
1c8d9e47 |
454 | if ($i != 0) { |
1f75b135 |
455 | $res .= ' . '; |
456 | } |
457 | preg_match("@text/([^;]+);@", $this->messages[$i]['headers']['content-type'], $format); |
458 | $format = textFormat_translate($format[1]); |
459 | if ($i != $partid) { |
8e519bd8 |
460 | $res .= makeHREF(Array('group' => $banana->state['group'], |
f6df9eb2 |
461 | 'artid' => $this->id, |
462 | 'part' => $i), |
463 | $format); |
1f75b135 |
464 | } else { |
465 | $res .= $format; |
466 | } |
467 | } |
1c8d9e47 |
468 | $res .= '</th></tr>'; |
1f75b135 |
469 | } |
1f75b135 |
470 | |
471 | preg_match("@text/([^;]+);@", $this->headers['content-type'], $format); |
472 | $format = $format[1]; |
8155c0a5 |
473 | $res .= '<tr class="impair"><td colspan="2" class="body"'; |
1f75b135 |
474 | if ($format == 'html') { |
4cc80c9a |
475 | if (preg_match('@<body[^>]*bgcolor="?([#0-9a-f]+)"?[^>]*>@i', $this->body, $bgcolor)) { |
476 | $res .= ' bgcolor="'.$bgcolor[1].'"'; |
477 | } |
28ab52c0 |
478 | $this->body = preg_replace('/cid:([^\'" ]+)/e', "find_attachment('\\1')", $this->body); |
4cc80c9a |
479 | $res .= '>'.formatbody($this->body, $format); |
1f75b135 |
480 | } else { |
4cc80c9a |
481 | $res .= '><pre>'.formatbody($this->body).'</pre>'; |
1f75b135 |
482 | } |
483 | $res .= '</td></tr>'; |
7a0e2710 |
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) { |
da6dc0c8 |
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'].')'; |
7a0e2710 |
502 | $res .= '<br/>'; |
503 | $i++; |
504 | } |
505 | $res .= '</td></tr>'; |
506 | } |
78cd27b3 |
507 | |
78cd27b3 |
508 | $ndx = $banana->spool->getndx($this->id); |
e230b41a |
509 | $res .= '<tr><td class="thrd" colspan="2">' |
510 | . $banana->spool->to_html($ndx-$banana->tbefore, $ndx+$banana->tafter, $ndx) |
511 | . '</td></tr>'; |
78cd27b3 |
512 | return $res.'</table>'; |
513 | } |
514 | } |
515 | |
28ab52c0 |
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 | |
f6df9eb2 |
524 | // vim:set et sw=4 sts=4 ts=4 |
78cd27b3 |
525 | ?> |