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 |
13 | class 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)) { |
47117a0e |
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'])); |
df7b8d75 |
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 | } |
c6a522df |
129 | } |
d38804c3 |
130 | } |
df7b8d75 |
131 | return true; |
d43ebde4 |
132 | } |
133 | |
134 | /** extract new headers from the part |
135 | * @param $part STRING part of a multipart message |
136 | */ |
d38804c3 |
137 | function _get_part($part) |
8f6f50fb |
138 | { |
d43ebde4 |
139 | global $banana; |
140 | |
141 | $lines = split("\n", $part); |
142 | while (count($lines)) { |
143 | $line = array_shift($lines); |
144 | if ($line != "") { |
df7b8d75 |
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 | } |
d43ebde4 |
153 | } |
154 | } else { |
155 | break; |
156 | } |
157 | } |
76032c26 |
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); |
d43ebde4 |
163 | } |
164 | |
8f6f50fb |
165 | /** add an attachment |
166 | */ |
d38804c3 |
167 | function _add_attachment($part) |
8f6f50fb |
168 | { |
d43ebde4 |
169 | $local_header = $part['headers']; |
170 | $local_body = $part['body']; |
171 | |
47117a0e |
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))) { |
df7b8d75 |
174 | $filename = $filename[1]; |
175 | } |
d43ebde4 |
176 | if (!isset($filename)) { |
177 | $filename = "attachment".count($pj); |
178 | } |
179 | |
47117a0e |
180 | if (isset($local_header['content-type']) |
181 | && preg_match('/^\s*([^ ;]+);/', $local_header['content-type'], $mimetype)) { |
182 | $mimetype = $mimetype[1]; |
183 | } else { |
8f6f50fb |
184 | return false; |
d43ebde4 |
185 | } |
186 | |
47117a0e |
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 | |
d43ebde4 |
194 | array_push($this->pj, Array('MIME' => $mimetype, |
195 | 'filename' => $filename, |
196 | 'encoding' => strtolower($local_header['content-transfer-encoding']), |
47117a0e |
197 | 'data' => $local_body, |
198 | 'cid' => $cid)); |
8f6f50fb |
199 | return true; |
d43ebde4 |
200 | } |
201 | |
322b97de |
202 | /** Fix body charset (convert body to utf8) |
203 | * @return false if failed |
204 | */ |
205 | function _fix_charset() |
206 | { |
a0b774a3 |
207 | if (preg_match('!charset="?([^;"]*)"?\s*(;|$)?!', $this->headers['content-type'], $matches)) { |
322b97de |
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 | |
d38804c3 |
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 | } |
5d133234 |
227 | if ($format[1] == 'richtext') { |
228 | return htmlToPlainText(richtextToHtml($this->body)); |
229 | } else { |
230 | return htmlToPlainText($this->body); |
d38804c3 |
231 | } |
d38804c3 |
232 | } |
233 | |
47117a0e |
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 | |
d43ebde4 |
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 | */ |
8f6f50fb |
257 | function get_attachment($pjid, $action = false) |
258 | { |
d43ebde4 |
259 | if ($pjid >= count($this->pj)) { |
260 | return false; |
261 | } else { |
262 | $file = $this->pj[$pjid]; |
d38804c3 |
263 | header('Content-Type: '.$file['MIME'].'; name="'.$file['filename'].'"'); |
d43ebde4 |
264 | if (!$action) { |
265 | header('Content-Disposition: attachment; filename="'.$file['filename'].'"'); |
d38804c3 |
266 | } else { |
267 | header('Content-Disposition: inline; filename="'.$file['filename'].'"'); |
268 | } |
d43ebde4 |
269 | if ($file['encoding'] == 'base64') { |
270 | echo base64_decode($file['data']); |
e0f141dd |
271 | } else if ($file['encoding'] == 'x-uuencode') { |
272 | passthru('echo '.escapeshellarg($file['data']).' | uudecode -o /dev/stdout'); |
d43ebde4 |
273 | } else { |
274 | header('Content-Transfer-Encoding: '.$file['encoding']); |
275 | echo $file['data']; |
276 | } |
277 | return true; |
278 | } |
279 | } |
280 | |
8f6f50fb |
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 | } |
c6a522df |
299 | |
322b97de |
300 | $this->_fix_charset(); |
8f6f50fb |
301 | return true; |
302 | } |
303 | |
2dbc0167 |
304 | function _header() |
01681efd |
305 | { |
2dbc0167 |
306 | global $banana; |
b9ea5b30 |
307 | $hdrs = $banana->nntp->head($this->id); |
e785d91c |
308 | if (!$hdrs) { |
e785d91c |
309 | return false; |
310 | } |
01681efd |
311 | |
e785d91c |
312 | // parse headers |
313 | foreach ($hdrs as $line) { |
314 | if (preg_match("/^[\t\r ]+/", $line)) { |
01681efd |
315 | $line = ($hdr=="X-Face"?"":" ").ltrim($line); |
2dbc0167 |
316 | if (in_array($hdr, $banana->parse_hdr)) { |
01681efd |
317 | $this->headers[$hdr] .= $line; |
e785d91c |
318 | } |
319 | } else { |
320 | list($hdr, $val) = split(":[ \t\r]*", $line, 2); |
01681efd |
321 | $hdr = strtolower($hdr); |
2dbc0167 |
322 | if (in_array($hdr, $banana->parse_hdr)) { |
01681efd |
323 | $this->headers[$hdr] = $val; |
e785d91c |
324 | } |
e785d91c |
325 | } |
326 | } |
327 | // decode headers |
2dbc0167 |
328 | foreach ($banana->hdecode as $hdr) { |
01681efd |
329 | if (isset($this->headers[$hdr])) { |
330 | $this->headers[$hdr] = headerDecode($this->headers[$hdr]); |
e785d91c |
331 | } |
332 | } |
01681efd |
333 | |
334 | $this->name = $this->headers['from']; |
335 | $this->name = preg_replace('/<[^ ]*>/', '', $this->name); |
336 | $this->name = trim($this->name); |
d81ff988 |
337 | return true; |
1eed39ee |
338 | } |
b9ea5b30 |
339 | |
65d96b1f |
340 | function checkcancel() |
341 | { |
b9ea5b30 |
342 | if (function_exists('hook_checkcancel')) { |
343 | return hook_checkcancel($this->headers); |
344 | } |
d8e2470c |
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">'; |
512f2eb6 |
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'))); |
d8e2470c |
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>'; |
b9ea5b30 |
371 | } |
372 | |
8f6f50fb |
373 | /** convert message to html |
374 | * @param partid INT id of the multipart message that must be displaid |
375 | */ |
c6a522df |
376 | function to_html($partid = -1) |
65d96b1f |
377 | { |
378 | global $banana; |
379 | |
c6a522df |
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; |
8f6f50fb |
403 | } |
404 | |
65d96b1f |
405 | $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">'; |
4eedaedf |
406 | $res .= '<tr><th colspan="2" class="subject">' |
d8e2470c |
407 | . $this->_browser() |
408 | . '<div class="banana_action">' |
d83e052b |
409 | . makeImgLink(Array('group' => $banana->state['group'], |
d8e2470c |
410 | 'action' => 'new'), |
411 | 'post.gif', |
512f2eb6 |
412 | _b_('Nouveau message')) |
d8e2470c |
413 | . makeImgLink(Array('group' => $banana->state['group'], |
414 | 'artid' => $this->id, |
415 | 'action' => 'new'), |
416 | 'reply.gif', |
512f2eb6 |
417 | _b_('Répondre')); |
d8e2470c |
418 | if ($this->checkCancel()) { |
419 | $res .= makeImgLink(Array('group' => $banana->state['group'], |
420 | 'artid' => $this->id, |
421 | 'action' => 'cancel'), |
422 | 'cancel.gif', |
512f2eb6 |
423 | _b_('Annuler')); |
d8e2470c |
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">'; |
65d96b1f |
429 | |
430 | foreach ($banana->show_hdr as $hdr) { |
431 | if (isset($this->headers[$hdr])) { |
432 | $res2 = formatdisplayheader($hdr, $this->headers[$hdr]); |
218e9ec8 |
433 | if ($res2 && ($hdr != 'x-face' || !$banana->formatxface)) { |
65d96b1f |
434 | $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n"; |
218e9ec8 |
435 | } else if ($res2) { |
436 | $xface = $res2; |
65d96b1f |
437 | } |
438 | } |
439 | } |
218e9ec8 |
440 | $res .= '</table></td><td class="xface">'; |
441 | |
442 | if ($xface) { |
443 | $res .= $xface; |
444 | } |
445 | $res .= '</td></tr>'; |
65d96b1f |
446 | |
8f6f50fb |
447 | if (count($this->messages) > 1) { |
4eedaedf |
448 | $res .= '<tr><th colspan="2">'; |
8f6f50fb |
449 | for ($i = 0 ; $i < count($this->messages) ; $i++) { |
4eedaedf |
450 | if ($i != 0) { |
8f6f50fb |
451 | $res .= ' . '; |
452 | } |
453 | preg_match("@text/([^;]+);@", $this->messages[$i]['headers']['content-type'], $format); |
454 | $format = textFormat_translate($format[1]); |
455 | if ($i != $partid) { |
0eb1e7ef |
456 | $res .= makeHREF(Array('group' => $banana->state['group'], |
d5588318 |
457 | 'artid' => $this->id, |
458 | 'part' => $i), |
459 | $format); |
8f6f50fb |
460 | } else { |
461 | $res .= $format; |
462 | } |
463 | } |
4eedaedf |
464 | $res .= '</th></tr>'; |
8f6f50fb |
465 | } |
8f6f50fb |
466 | |
467 | preg_match("@text/([^;]+);@", $this->headers['content-type'], $format); |
468 | $format = $format[1]; |
8452d698 |
469 | $res .= '<tr class="impair"><td colspan="2" class="body"'; |
8f6f50fb |
470 | if ($format == 'html') { |
e27ae1a3 |
471 | if (preg_match('@<body[^>]*bgcolor="?([#0-9a-f]+)"?[^>]*>@i', $this->body, $bgcolor)) { |
472 | $res .= ' bgcolor="'.$bgcolor[1].'"'; |
473 | } |
47117a0e |
474 | $this->body = preg_replace('/cid:([^\'" ]+)/e', "find_attachment('\\1')", $this->body); |
e27ae1a3 |
475 | $res .= '>'.formatbody($this->body, $format); |
8f6f50fb |
476 | } else { |
e27ae1a3 |
477 | $res .= '><pre>'.formatbody($this->body).'</pre>'; |
8f6f50fb |
478 | } |
479 | $res .= '</td></tr>'; |
d43ebde4 |
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) { |
ec39d93e |
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'].')'; |
d43ebde4 |
498 | $res .= '<br/>'; |
499 | $i++; |
500 | } |
501 | $res .= '</td></tr>'; |
502 | } |
65d96b1f |
503 | |
65d96b1f |
504 | $ndx = $banana->spool->getndx($this->id); |
4f75645f |
505 | $res .= '<tr><td class="thrd" colspan="2">' |
506 | . $banana->spool->to_html($ndx-$banana->tbefore, $ndx+$banana->tafter, $ndx) |
507 | . '</td></tr>'; |
65d96b1f |
508 | return $res.'</table>'; |
509 | } |
1eed39ee |
510 | } |
511 | |
47117a0e |
512 | /** Wrapper for Post::find_attachment |
513 | */ |
514 | function find_attachment($cid) |
515 | { |
516 | global $banana; |
517 | return $banana->post->find_attachment($cid); |
518 | } |
519 | |
d5588318 |
520 | // vim:set et sw=4 sts=4 ts=4 |
1eed39ee |
521 | ?> |