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(); |
c6a522df |
64 | if (preg_match('!charset=([^;]*)\s*(;|$)!', $this->headers['content-type'], $matches)) { |
65 | $this->body = iconv($matches[1], 'utf-8', $this->body); |
66 | } else { |
67 | $this->body = utf8_encode($this->body); |
68 | } |
e785d91c |
69 | } |
1eed39ee |
70 | } |
1eed39ee |
71 | |
e0f141dd |
72 | /** find and add uuencoded attachments |
73 | */ |
74 | function _find_uuencode() |
75 | { |
76 | if (preg_match_all('@\n(begin \d+ ([^\r\n]+)\r?(?:\n(?!end)[^\n]*)*\nend)@', $this->body, $matches, PREG_SET_ORDER)) { |
77 | foreach ($matches as $match) { |
78 | $mime = trim(exec('echo '.escapeshellarg($match[1]).' | uudecode -o /dev/stdout | file -bi -')); |
79 | if ($mime != 'application/x-empty') { |
73ab762c |
80 | $this->body = trim(str_replace($match[0], '', $this->body)); |
e0f141dd |
81 | $body = $match[1]; |
82 | $header['content-type'] = $mime.'; name="'.$match[2].'"'; |
83 | $header['content-transfer-encoding'] = 'x-uuencode'; |
84 | $header['content-disposition'] = 'attachment; filename="'.$match[2].'"'; |
85 | $this->_add_attachment(Array('headers' => $header, 'body' => $body)); |
86 | } |
87 | } |
88 | } |
89 | } |
90 | |
d43ebde4 |
91 | /** split multipart messages |
92 | * @param $type STRING multipart type description |
93 | * @param $boundary STRING multipart boundary identification string |
94 | */ |
df7b8d75 |
95 | function _split_multipart($headers, $body) |
d43ebde4 |
96 | { |
df7b8d75 |
97 | if (!preg_match("@multipart/([^;]+);@", $headers['content-type'], $type)) { |
98 | return false; |
99 | } |
100 | |
101 | preg_match("/boundary=\"?([^ \"]+)\"?/", $headers['content-type'], $boundary); |
102 | $boundary = $boundary[1]; |
103 | $type = $type[1]; |
104 | $parts = preg_split("@\n--$boundary(--|\n)@", $body); |
d43ebde4 |
105 | foreach ($parts as $part) { |
df7b8d75 |
106 | $part = $this->_get_part($part); |
d43ebde4 |
107 | $local_header = $part['headers']; |
df7b8d75 |
108 | $local_body = $part['body']; |
109 | if (!$this->_split_multipart($local_header, $local_body)) { |
110 | $is_text = isset($local_header['content-type']) && preg_match("@text/([^;]+);@", $local_header['content-type']) |
111 | && (!isset($local_header['content-disposition']) || !preg_match('@attachment@', $local_header['content-disposition'])); |
112 | |
113 | // alternative ==> multiple formats for messages |
114 | if ($type == 'alternative' && $is_text) { |
115 | array_push($this->messages, $part); |
116 | |
117 | // !alternative ==> une body, others are attachments |
118 | } else if ($is_text) { |
119 | if (count($this->messages) == 0) { |
120 | $this->body = $local_body; |
121 | foreach (array_keys($local_header) as $key) { |
122 | $this->header[$key] = $local_header[$key]; |
123 | } |
124 | array_push($this->messages, $part); |
125 | } else { |
126 | $this->_add_attachment($part); |
127 | } |
128 | } else { |
129 | $this->_add_attachment($part); |
130 | } |
c6a522df |
131 | } |
d38804c3 |
132 | } |
df7b8d75 |
133 | return true; |
d43ebde4 |
134 | } |
135 | |
136 | /** extract new headers from the part |
137 | * @param $part STRING part of a multipart message |
138 | */ |
d38804c3 |
139 | function _get_part($part) |
8f6f50fb |
140 | { |
d43ebde4 |
141 | global $banana; |
142 | |
143 | $lines = split("\n", $part); |
144 | while (count($lines)) { |
145 | $line = array_shift($lines); |
146 | if ($line != "") { |
df7b8d75 |
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 | } |
d43ebde4 |
155 | } |
156 | } else { |
157 | break; |
158 | } |
159 | } |
76032c26 |
160 | $local_body = join("\n", $lines); |
161 | if (preg_match("/quoted-printable/", $local_headers['content-transfer-encoding'])) { |
162 | $local_body = quoted_printable_decode($local_body); |
163 | } |
164 | return Array('headers' => $local_headers, 'body' => $local_body); |
d43ebde4 |
165 | } |
166 | |
8f6f50fb |
167 | /** add an attachment |
168 | */ |
d38804c3 |
169 | function _add_attachment($part) |
8f6f50fb |
170 | { |
d43ebde4 |
171 | $local_header = $part['headers']; |
172 | $local_body = $part['body']; |
173 | |
df7b8d75 |
174 | if ((isset($local_header['content-disposition']) && preg_match("/filename=\"?([^\"]+)\"?/", $local_header['content-disposition'], $filename)) |
175 | || (isset($local_header['content-type']) && preg_match("/name=\"?([^\"]+)\"?/", $local_header['content-type'], $filename))) { |
176 | $filename = $filename[1]; |
177 | } |
d43ebde4 |
178 | if (!isset($filename)) { |
179 | $filename = "attachment".count($pj); |
180 | } |
181 | |
182 | if (isset($local_header['content-type'])) { |
183 | if (preg_match("/^\\s*([^ ;]+);/", $local_header['content-type'], $mimetype)) { |
184 | $mimetype = $mimetype[1]; |
185 | } |
186 | } |
187 | if (!isset($mimetype)) { |
8f6f50fb |
188 | return false; |
d43ebde4 |
189 | } |
190 | |
191 | array_push($this->pj, Array('MIME' => $mimetype, |
192 | 'filename' => $filename, |
193 | 'encoding' => strtolower($local_header['content-transfer-encoding']), |
194 | 'data' => $local_body)); |
8f6f50fb |
195 | return true; |
d43ebde4 |
196 | } |
197 | |
d38804c3 |
198 | /** return body in plain text (useful for messages without a text/plain part) |
199 | */ |
200 | function get_body() |
201 | { |
202 | preg_match("@text/([^;]+);@", $this->headers['content-type'], $format); |
203 | if ($format[1] == 'plain') { |
204 | return $this->body; |
205 | } |
5d133234 |
206 | if ($format[1] == 'richtext') { |
207 | return htmlToPlainText(richtextToHtml($this->body)); |
208 | } else { |
209 | return htmlToPlainText($this->body); |
d38804c3 |
210 | } |
d38804c3 |
211 | } |
212 | |
d43ebde4 |
213 | /** decode an attachment |
214 | * @param pjid INT id of the attachment to decode |
215 | * @param action BOOL action to execute : true=view, false=download |
216 | */ |
8f6f50fb |
217 | function get_attachment($pjid, $action = false) |
218 | { |
d43ebde4 |
219 | if ($pjid >= count($this->pj)) { |
220 | return false; |
221 | } else { |
222 | $file = $this->pj[$pjid]; |
d38804c3 |
223 | header('Content-Type: '.$file['MIME'].'; name="'.$file['filename'].'"'); |
d43ebde4 |
224 | if (!$action) { |
225 | header('Content-Disposition: attachment; filename="'.$file['filename'].'"'); |
d38804c3 |
226 | } else { |
227 | header('Content-Disposition: inline; filename="'.$file['filename'].'"'); |
228 | } |
d43ebde4 |
229 | if ($file['encoding'] == 'base64') { |
230 | echo base64_decode($file['data']); |
e0f141dd |
231 | } else if ($file['encoding'] == 'x-uuencode') { |
232 | passthru('echo '.escapeshellarg($file['data']).' | uudecode -o /dev/stdout'); |
d43ebde4 |
233 | } else { |
234 | header('Content-Transfer-Encoding: '.$file['encoding']); |
235 | echo $file['data']; |
236 | } |
237 | return true; |
238 | } |
239 | } |
240 | |
8f6f50fb |
241 | /** set body to represent the given part |
242 | * @param partid INT index of the part in messages |
243 | */ |
244 | function set_body_to_part($partid) |
245 | { |
246 | global $banana; |
247 | |
248 | if (count($this->messages) == 0) { |
249 | return false; |
250 | } |
251 | |
252 | $local_header = $this->messages[$partid]['headers']; |
253 | $this->body = $this->messages[$partid]['body']; |
254 | foreach ($banana->parse_hdr as $hdr) { |
255 | if (isset($local_header[$hdr])) { |
256 | $this->headers[$hdr] = $local_header[$hdr]; |
257 | } |
258 | } |
c6a522df |
259 | |
260 | if (preg_match('!charset=([^;]*)\s*(;|$)!', $this->headers['content-type'], $matches)) { |
261 | $this->body = iconv($matches[1], 'utf-8', $this->body); |
262 | } else { |
263 | $this->body = utf8_encode($this->body); |
76032c26 |
264 | } |
8f6f50fb |
265 | return true; |
266 | } |
267 | |
2dbc0167 |
268 | function _header() |
01681efd |
269 | { |
2dbc0167 |
270 | global $banana; |
b9ea5b30 |
271 | $hdrs = $banana->nntp->head($this->id); |
e785d91c |
272 | if (!$hdrs) { |
e785d91c |
273 | return false; |
274 | } |
01681efd |
275 | |
e785d91c |
276 | // parse headers |
277 | foreach ($hdrs as $line) { |
278 | if (preg_match("/^[\t\r ]+/", $line)) { |
01681efd |
279 | $line = ($hdr=="X-Face"?"":" ").ltrim($line); |
2dbc0167 |
280 | if (in_array($hdr, $banana->parse_hdr)) { |
01681efd |
281 | $this->headers[$hdr] .= $line; |
e785d91c |
282 | } |
283 | } else { |
284 | list($hdr, $val) = split(":[ \t\r]*", $line, 2); |
01681efd |
285 | $hdr = strtolower($hdr); |
2dbc0167 |
286 | if (in_array($hdr, $banana->parse_hdr)) { |
01681efd |
287 | $this->headers[$hdr] = $val; |
e785d91c |
288 | } |
e785d91c |
289 | } |
290 | } |
291 | // decode headers |
2dbc0167 |
292 | foreach ($banana->hdecode as $hdr) { |
01681efd |
293 | if (isset($this->headers[$hdr])) { |
294 | $this->headers[$hdr] = headerDecode($this->headers[$hdr]); |
e785d91c |
295 | } |
296 | } |
01681efd |
297 | |
298 | $this->name = $this->headers['from']; |
299 | $this->name = preg_replace('/<[^ ]*>/', '', $this->name); |
300 | $this->name = trim($this->name); |
d81ff988 |
301 | return true; |
1eed39ee |
302 | } |
b9ea5b30 |
303 | |
65d96b1f |
304 | function checkcancel() |
305 | { |
b9ea5b30 |
306 | if (function_exists('hook_checkcancel')) { |
307 | return hook_checkcancel($this->headers); |
308 | } |
309 | return ($this->headers['from'] == $_SESSION['name']." <".$_SESSION['mail'].">"); |
310 | } |
311 | |
8f6f50fb |
312 | /** convert message to html |
313 | * @param partid INT id of the multipart message that must be displaid |
314 | */ |
c6a522df |
315 | function to_html($partid = -1) |
65d96b1f |
316 | { |
317 | global $banana; |
318 | |
c6a522df |
319 | if (count($this->messages) > 1) { |
320 | if ($partid != -1) { |
321 | $this->set_body_to_part($partid); |
322 | } else { |
323 | // Select prefered text-format |
324 | foreach ($banana->body_mime as $mime) { |
325 | for ($id = 0 ; $id < count($this->messages) ; $id++) { |
326 | if (preg_match("@$mime@", $this->messages[$id]['headers']['content-type'])) { |
327 | $partid = $id; |
328 | $this->set_body_to_part($partid); |
329 | break; |
330 | } |
331 | } |
332 | if ($partid != -1) { |
333 | break; |
334 | } |
335 | } |
336 | if ($partid == -1) { |
337 | $partid = 0; |
338 | } |
339 | } |
340 | } else { |
341 | $partid = 0; |
8f6f50fb |
342 | } |
343 | |
65d96b1f |
344 | $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">'; |
345 | $res .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>'; |
218e9ec8 |
346 | $res .= '<tr><td class="headers"><table cellpadding="0" cellspacing="0">'; |
65d96b1f |
347 | |
348 | foreach ($banana->show_hdr as $hdr) { |
349 | if (isset($this->headers[$hdr])) { |
350 | $res2 = formatdisplayheader($hdr, $this->headers[$hdr]); |
218e9ec8 |
351 | if ($res2 && ($hdr != 'x-face' || !$banana->formatxface)) { |
65d96b1f |
352 | $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n"; |
218e9ec8 |
353 | } else if ($res2) { |
354 | $xface = $res2; |
65d96b1f |
355 | } |
356 | } |
357 | } |
218e9ec8 |
358 | $res .= '</table></td><td class="xface">'; |
359 | |
360 | if ($xface) { |
361 | $res .= $xface; |
362 | } |
363 | $res .= '</td></tr>'; |
65d96b1f |
364 | |
8f6f50fb |
365 | $res .= '<tr><th colspan="2">'._b_('Corps'); |
366 | if (count($this->messages) > 1) { |
367 | for ($i = 0 ; $i < count($this->messages) ; $i++) { |
368 | if ($i == 0) { |
369 | $res .= ' : '; |
370 | } else { |
371 | $res .= ' . '; |
372 | } |
373 | preg_match("@text/([^;]+);@", $this->messages[$i]['headers']['content-type'], $format); |
374 | $format = textFormat_translate($format[1]); |
375 | if ($i != $partid) { |
0eb1e7ef |
376 | $res .= makeHREF(Array('group' => $banana->state['group'], |
d5588318 |
377 | 'artid' => $this->id, |
378 | 'part' => $i), |
379 | $format); |
8f6f50fb |
380 | } else { |
381 | $res .= $format; |
382 | } |
383 | } |
384 | } |
385 | $res .= '</th></tr>'; |
386 | |
387 | preg_match("@text/([^;]+);@", $this->headers['content-type'], $format); |
388 | $format = $format[1]; |
e27ae1a3 |
389 | $res .= '<tr><td colspan="2"'; |
8f6f50fb |
390 | if ($format == 'html') { |
e27ae1a3 |
391 | if (preg_match('@<body[^>]*bgcolor="?([#0-9a-f]+)"?[^>]*>@i', $this->body, $bgcolor)) { |
392 | $res .= ' bgcolor="'.$bgcolor[1].'"'; |
393 | } |
394 | $res .= '>'.formatbody($this->body, $format); |
8f6f50fb |
395 | } else { |
e27ae1a3 |
396 | $res .= '><pre>'.formatbody($this->body).'</pre>'; |
8f6f50fb |
397 | } |
398 | $res .= '</td></tr>'; |
d43ebde4 |
399 | |
400 | if (count($this->pj) > 0) { |
401 | $res .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>'; |
402 | $res .= '<tr><td colspan="2">'; |
403 | $i = 0; |
404 | foreach ($this->pj as $file) { |
405 | $res .= $file['filename'].' ('.$file['MIME'].') : '; |
0eb1e7ef |
406 | $res .= makeHREF(Array('group' => $banana->state['group'], |
d5588318 |
407 | 'artid' => $this->id, |
408 | 'pj' => $i), |
409 | _b_('télécharger')); |
410 | $res .= ' . '; |
0eb1e7ef |
411 | $res .= makeHREF(Array('group' => $banana->state['group'], |
412 | 'artid' => $this->id, |
413 | 'pj' => $i, |
d5588318 |
414 | 'action'=> 'view'), |
0eb1e7ef |
415 | _b_('aperçu')); |
d43ebde4 |
416 | $res .= '<br/>'; |
417 | $i++; |
418 | } |
419 | $res .= '</td></tr>'; |
420 | } |
65d96b1f |
421 | |
c6a522df |
422 | $res .= '<tr><th colspan="2">'._b_('Apercu').'</th></tr>'; |
65d96b1f |
423 | $ndx = $banana->spool->getndx($this->id); |
424 | $res .= '<tr><td class="thrd" colspan="2">'.$banana->spool->to_html($ndx-$banana->tbefore, $ndx+$banana->tafter, $ndx).'</td></tr>'; |
425 | |
426 | return $res.'</table>'; |
427 | } |
1eed39ee |
428 | } |
429 | |
d5588318 |
430 | // vim:set et sw=4 sts=4 ts=4 |
1eed39ee |
431 | ?> |