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; |
1eed39ee |
26 | |
e785d91c |
27 | /** constructor |
e785d91c |
28 | * @param $_id STRING MSGNUM or MSGID (a group should be selected in this case) |
29 | */ |
2dbc0167 |
30 | function BananaPost($_id) |
d4c19591 |
31 | { |
2dbc0167 |
32 | global $banana; |
8f6f50fb |
33 | $this->id = $_id; |
34 | $this->pj = array(); |
35 | $this->messages = array(); |
d81ff988 |
36 | if (!$this->_header()) { |
37 | return null; |
38 | } |
39 | |
01681efd |
40 | |
8d99c683 |
41 | if ($body = $banana->nntp->body($_id)) { |
42 | $this->body = join("\n", $body); |
43 | } else { |
d81ff988 |
44 | return null; |
8d99c683 |
45 | } |
01681efd |
46 | |
47 | if (isset($this->headers['content-transfer-encoding'])) { |
48 | if (preg_match("/base64/", $this->headers['content-transfer-encoding'])) { |
49 | $this->body = base64_decode($this->body); |
50 | } elseif (preg_match("/quoted-printable/", $this->headers['content-transfer-encoding'])) { |
51 | $this->body = quoted_printable_decode($this->body); |
52 | } |
e785d91c |
53 | } |
01681efd |
54 | |
df7b8d75 |
55 | if ($this->_split_multipart($this->headers, $this->body)) { |
56 | $this->set_body_to_part(0); |
c42efe2f |
57 | } else { |
df7b8d75 |
58 | $this->_split_multipart($mpart_type[1], $mpart_boundary[1]); |
e0f141dd |
59 | $this->_find_uuencode(); |
c6a522df |
60 | if (preg_match('!charset=([^;]*)\s*(;|$)!', $this->headers['content-type'], $matches)) { |
61 | $this->body = iconv($matches[1], 'utf-8', $this->body); |
62 | } else { |
63 | $this->body = utf8_encode($this->body); |
64 | } |
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 | |
d38804c3 |
194 | /** return body in plain text (useful for messages without a text/plain part) |
195 | */ |
196 | function get_body() |
197 | { |
198 | preg_match("@text/([^;]+);@", $this->headers['content-type'], $format); |
199 | if ($format[1] == 'plain') { |
200 | return $this->body; |
201 | } |
5d133234 |
202 | if ($format[1] == 'richtext') { |
203 | return htmlToPlainText(richtextToHtml($this->body)); |
204 | } else { |
205 | return htmlToPlainText($this->body); |
d38804c3 |
206 | } |
d38804c3 |
207 | } |
208 | |
d43ebde4 |
209 | /** decode an attachment |
210 | * @param pjid INT id of the attachment to decode |
211 | * @param action BOOL action to execute : true=view, false=download |
212 | */ |
8f6f50fb |
213 | function get_attachment($pjid, $action = false) |
214 | { |
d43ebde4 |
215 | if ($pjid >= count($this->pj)) { |
216 | return false; |
217 | } else { |
218 | $file = $this->pj[$pjid]; |
d38804c3 |
219 | header('Content-Type: '.$file['MIME'].'; name="'.$file['filename'].'"'); |
d43ebde4 |
220 | if (!$action) { |
221 | header('Content-Disposition: attachment; filename="'.$file['filename'].'"'); |
d38804c3 |
222 | } else { |
223 | header('Content-Disposition: inline; filename="'.$file['filename'].'"'); |
224 | } |
d43ebde4 |
225 | if ($file['encoding'] == 'base64') { |
226 | echo base64_decode($file['data']); |
e0f141dd |
227 | } else if ($file['encoding'] == 'x-uuencode') { |
228 | passthru('echo '.escapeshellarg($file['data']).' | uudecode -o /dev/stdout'); |
d43ebde4 |
229 | } else { |
230 | header('Content-Transfer-Encoding: '.$file['encoding']); |
231 | echo $file['data']; |
232 | } |
233 | return true; |
234 | } |
235 | } |
236 | |
8f6f50fb |
237 | /** set body to represent the given part |
238 | * @param partid INT index of the part in messages |
239 | */ |
240 | function set_body_to_part($partid) |
241 | { |
242 | global $banana; |
243 | |
244 | if (count($this->messages) == 0) { |
245 | return false; |
246 | } |
247 | |
248 | $local_header = $this->messages[$partid]['headers']; |
249 | $this->body = $this->messages[$partid]['body']; |
250 | foreach ($banana->parse_hdr as $hdr) { |
251 | if (isset($local_header[$hdr])) { |
252 | $this->headers[$hdr] = $local_header[$hdr]; |
253 | } |
254 | } |
c6a522df |
255 | |
256 | if (preg_match('!charset=([^;]*)\s*(;|$)!', $this->headers['content-type'], $matches)) { |
257 | $this->body = iconv($matches[1], 'utf-8', $this->body); |
258 | } else { |
259 | $this->body = utf8_encode($this->body); |
76032c26 |
260 | } |
8f6f50fb |
261 | return true; |
262 | } |
263 | |
2dbc0167 |
264 | function _header() |
01681efd |
265 | { |
2dbc0167 |
266 | global $banana; |
b9ea5b30 |
267 | $hdrs = $banana->nntp->head($this->id); |
e785d91c |
268 | if (!$hdrs) { |
e785d91c |
269 | return false; |
270 | } |
01681efd |
271 | |
e785d91c |
272 | // parse headers |
273 | foreach ($hdrs as $line) { |
274 | if (preg_match("/^[\t\r ]+/", $line)) { |
01681efd |
275 | $line = ($hdr=="X-Face"?"":" ").ltrim($line); |
2dbc0167 |
276 | if (in_array($hdr, $banana->parse_hdr)) { |
01681efd |
277 | $this->headers[$hdr] .= $line; |
e785d91c |
278 | } |
279 | } else { |
280 | list($hdr, $val) = split(":[ \t\r]*", $line, 2); |
01681efd |
281 | $hdr = strtolower($hdr); |
2dbc0167 |
282 | if (in_array($hdr, $banana->parse_hdr)) { |
01681efd |
283 | $this->headers[$hdr] = $val; |
e785d91c |
284 | } |
e785d91c |
285 | } |
286 | } |
287 | // decode headers |
2dbc0167 |
288 | foreach ($banana->hdecode as $hdr) { |
01681efd |
289 | if (isset($this->headers[$hdr])) { |
290 | $this->headers[$hdr] = headerDecode($this->headers[$hdr]); |
e785d91c |
291 | } |
292 | } |
01681efd |
293 | |
294 | $this->name = $this->headers['from']; |
295 | $this->name = preg_replace('/<[^ ]*>/', '', $this->name); |
296 | $this->name = trim($this->name); |
d81ff988 |
297 | return true; |
1eed39ee |
298 | } |
b9ea5b30 |
299 | |
65d96b1f |
300 | function checkcancel() |
301 | { |
b9ea5b30 |
302 | if (function_exists('hook_checkcancel')) { |
303 | return hook_checkcancel($this->headers); |
304 | } |
305 | return ($this->headers['from'] == $_SESSION['name']." <".$_SESSION['mail'].">"); |
306 | } |
307 | |
8f6f50fb |
308 | /** convert message to html |
309 | * @param partid INT id of the multipart message that must be displaid |
310 | */ |
c6a522df |
311 | function to_html($partid = -1) |
65d96b1f |
312 | { |
313 | global $banana; |
314 | |
c6a522df |
315 | if (count($this->messages) > 1) { |
316 | if ($partid != -1) { |
317 | $this->set_body_to_part($partid); |
318 | } else { |
319 | // Select prefered text-format |
320 | foreach ($banana->body_mime as $mime) { |
321 | for ($id = 0 ; $id < count($this->messages) ; $id++) { |
322 | if (preg_match("@$mime@", $this->messages[$id]['headers']['content-type'])) { |
323 | $partid = $id; |
324 | $this->set_body_to_part($partid); |
325 | break; |
326 | } |
327 | } |
328 | if ($partid != -1) { |
329 | break; |
330 | } |
331 | } |
332 | if ($partid == -1) { |
333 | $partid = 0; |
334 | } |
335 | } |
336 | } else { |
337 | $partid = 0; |
8f6f50fb |
338 | } |
339 | |
65d96b1f |
340 | $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">'; |
341 | $res .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>'; |
218e9ec8 |
342 | $res .= '<tr><td class="headers"><table cellpadding="0" cellspacing="0">'; |
65d96b1f |
343 | |
344 | foreach ($banana->show_hdr as $hdr) { |
345 | if (isset($this->headers[$hdr])) { |
346 | $res2 = formatdisplayheader($hdr, $this->headers[$hdr]); |
218e9ec8 |
347 | if ($res2 && ($hdr != 'x-face' || !$banana->formatxface)) { |
65d96b1f |
348 | $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n"; |
218e9ec8 |
349 | } else if ($res2) { |
350 | $xface = $res2; |
65d96b1f |
351 | } |
352 | } |
353 | } |
218e9ec8 |
354 | $res .= '</table></td><td class="xface">'; |
355 | |
356 | if ($xface) { |
357 | $res .= $xface; |
358 | } |
359 | $res .= '</td></tr>'; |
65d96b1f |
360 | |
8f6f50fb |
361 | $res .= '<tr><th colspan="2">'._b_('Corps'); |
362 | if (count($this->messages) > 1) { |
363 | for ($i = 0 ; $i < count($this->messages) ; $i++) { |
364 | if ($i == 0) { |
365 | $res .= ' : '; |
366 | } else { |
367 | $res .= ' . '; |
368 | } |
369 | preg_match("@text/([^;]+);@", $this->messages[$i]['headers']['content-type'], $format); |
370 | $format = textFormat_translate($format[1]); |
371 | if ($i != $partid) { |
372 | $res .= '<a href="?group='.$banana->state['group'].'&artid='.$this->id.'&part='.$i.'">'.$format.'</a>'; |
373 | } else { |
374 | $res .= $format; |
375 | } |
376 | } |
377 | } |
378 | $res .= '</th></tr>'; |
379 | |
380 | preg_match("@text/([^;]+);@", $this->headers['content-type'], $format); |
381 | $format = $format[1]; |
e27ae1a3 |
382 | $res .= '<tr><td colspan="2"'; |
8f6f50fb |
383 | if ($format == 'html') { |
e27ae1a3 |
384 | if (preg_match('@<body[^>]*bgcolor="?([#0-9a-f]+)"?[^>]*>@i', $this->body, $bgcolor)) { |
385 | $res .= ' bgcolor="'.$bgcolor[1].'"'; |
386 | } |
387 | $res .= '>'.formatbody($this->body, $format); |
8f6f50fb |
388 | } else { |
e27ae1a3 |
389 | $res .= '><pre>'.formatbody($this->body).'</pre>'; |
8f6f50fb |
390 | } |
391 | $res .= '</td></tr>'; |
d43ebde4 |
392 | |
393 | if (count($this->pj) > 0) { |
394 | $res .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>'; |
395 | $res .= '<tr><td colspan="2">'; |
396 | $i = 0; |
397 | foreach ($this->pj as $file) { |
398 | $res .= $file['filename'].' ('.$file['MIME'].') : '; |
608f5b71 |
399 | $res .= '<a href="?group='.$banana->state['group'].'&artid='.$this->id.'&pj='.$i.'">télécharger</a>'; |
400 | $res .= ' . <a href="?group='.$banana->state['group'].'&artid='.$this->id.'&pj='.$i.'&action=view" target="_blank">aperçu</a>'; |
d43ebde4 |
401 | $res .= '<br/>'; |
402 | $i++; |
403 | } |
404 | $res .= '</td></tr>'; |
405 | } |
65d96b1f |
406 | |
c6a522df |
407 | $res .= '<tr><th colspan="2">'._b_('Apercu').'</th></tr>'; |
65d96b1f |
408 | $ndx = $banana->spool->getndx($this->id); |
409 | $res .= '<tr><td class="thrd" colspan="2">'.$banana->spool->to_html($ndx-$banana->tbefore, $ndx+$banana->tafter, $ndx).'</td></tr>'; |
410 | |
411 | return $res.'</table>'; |
412 | } |
1eed39ee |
413 | } |
414 | |
415 | ?> |