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