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