78cd27b3 |
1 | <?php |
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 | |
13 | class BananaPost |
14 | { |
15 | var $id; |
16 | /** headers */ |
17 | var $headers; |
18 | /** body */ |
19 | var $body; |
1f75b135 |
20 | /** formating */ |
21 | var $messages; |
7a0e2710 |
22 | /** attachment */ |
23 | var $pj; |
78cd27b3 |
24 | /** poster name */ |
25 | var $name; |
856dc84a |
26 | /** test validity */ |
27 | var $valid = true; |
78cd27b3 |
28 | |
29 | /** constructor |
30 | * @param $_id STRING MSGNUM or MSGID (a group should be selected in this case) |
31 | */ |
32 | function BananaPost($_id) |
33 | { |
34 | global $banana; |
1f75b135 |
35 | $this->id = $_id; |
36 | $this->pj = array(); |
37 | $this->messages = array(); |
50cd076e |
38 | if (!$this->_header()) { |
856dc84a |
39 | $this->valid = false; |
50cd076e |
40 | return null; |
41 | } |
42 | |
78cd27b3 |
43 | |
44 | if ($body = $banana->nntp->body($_id)) { |
45 | $this->body = join("\n", $body); |
46 | } else { |
856dc84a |
47 | $this->valid = false; |
50cd076e |
48 | return null; |
78cd27b3 |
49 | } |
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 | } |
57 | } |
58 | |
a7cb2f0d |
59 | if ($this->_split_multipart($this->headers, $this->body)) { |
60 | $this->set_body_to_part(0); |
78cd27b3 |
61 | } else { |
a7cb2f0d |
62 | $this->_split_multipart($mpart_type[1], $mpart_boundary[1]); |
c3f19b42 |
63 | $this->_find_uuencode(); |
cc43419f |
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 | } |
78cd27b3 |
69 | } |
70 | } |
71 | |
c3f19b42 |
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') { |
f12fdb59 |
80 | $this->body = trim(str_replace($match[0], '', $this->body)); |
c3f19b42 |
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 | |
7a0e2710 |
91 | /** split multipart messages |
92 | * @param $type STRING multipart type description |
93 | * @param $boundary STRING multipart boundary identification string |
94 | */ |
a7cb2f0d |
95 | function _split_multipart($headers, $body) |
7a0e2710 |
96 | { |
a7cb2f0d |
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); |
7a0e2710 |
105 | foreach ($parts as $part) { |
a7cb2f0d |
106 | $part = $this->_get_part($part); |
7a0e2710 |
107 | $local_header = $part['headers']; |
a7cb2f0d |
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 | } |
cc43419f |
131 | } |
a529ea7b |
132 | } |
a7cb2f0d |
133 | return true; |
7a0e2710 |
134 | } |
135 | |
136 | /** extract new headers from the part |
137 | * @param $part STRING part of a multipart message |
138 | */ |
a529ea7b |
139 | function _get_part($part) |
1f75b135 |
140 | { |
7a0e2710 |
141 | global $banana; |
142 | |
143 | $lines = split("\n", $part); |
144 | while (count($lines)) { |
145 | $line = array_shift($lines); |
146 | if ($line != "") { |
a7cb2f0d |
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 | } |
7a0e2710 |
155 | } |
156 | } else { |
157 | break; |
158 | } |
159 | } |
16b17ba5 |
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); |
7a0e2710 |
165 | } |
166 | |
1f75b135 |
167 | /** add an attachment |
168 | */ |
a529ea7b |
169 | function _add_attachment($part) |
1f75b135 |
170 | { |
7a0e2710 |
171 | $local_header = $part['headers']; |
172 | $local_body = $part['body']; |
173 | |
a7cb2f0d |
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 | } |
7a0e2710 |
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)) { |
1f75b135 |
188 | return false; |
7a0e2710 |
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)); |
1f75b135 |
195 | return true; |
7a0e2710 |
196 | } |
197 | |
a529ea7b |
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 | } |
aef14768 |
206 | if ($format[1] == 'richtext') { |
207 | return htmlToPlainText(richtextToHtml($this->body)); |
208 | } else { |
209 | return htmlToPlainText($this->body); |
a529ea7b |
210 | } |
a529ea7b |
211 | } |
212 | |
7a0e2710 |
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 | */ |
1f75b135 |
217 | function get_attachment($pjid, $action = false) |
218 | { |
7a0e2710 |
219 | if ($pjid >= count($this->pj)) { |
220 | return false; |
221 | } else { |
222 | $file = $this->pj[$pjid]; |
a529ea7b |
223 | header('Content-Type: '.$file['MIME'].'; name="'.$file['filename'].'"'); |
7a0e2710 |
224 | if (!$action) { |
225 | header('Content-Disposition: attachment; filename="'.$file['filename'].'"'); |
a529ea7b |
226 | } else { |
227 | header('Content-Disposition: inline; filename="'.$file['filename'].'"'); |
228 | } |
7a0e2710 |
229 | if ($file['encoding'] == 'base64') { |
230 | echo base64_decode($file['data']); |
c3f19b42 |
231 | } else if ($file['encoding'] == 'x-uuencode') { |
232 | passthru('echo '.escapeshellarg($file['data']).' | uudecode -o /dev/stdout'); |
7a0e2710 |
233 | } else { |
234 | header('Content-Transfer-Encoding: '.$file['encoding']); |
235 | echo $file['data']; |
236 | } |
237 | return true; |
238 | } |
239 | } |
240 | |
1f75b135 |
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 | } |
cc43419f |
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); |
16b17ba5 |
264 | } |
1f75b135 |
265 | return true; |
266 | } |
267 | |
78cd27b3 |
268 | function _header() |
269 | { |
270 | global $banana; |
271 | $hdrs = $banana->nntp->head($this->id); |
272 | if (!$hdrs) { |
78cd27b3 |
273 | return false; |
274 | } |
275 | |
276 | // parse headers |
277 | foreach ($hdrs as $line) { |
278 | if (preg_match("/^[\t\r ]+/", $line)) { |
279 | $line = ($hdr=="X-Face"?"":" ").ltrim($line); |
280 | if (in_array($hdr, $banana->parse_hdr)) { |
281 | $this->headers[$hdr] .= $line; |
282 | } |
283 | } else { |
284 | list($hdr, $val) = split(":[ \t\r]*", $line, 2); |
285 | $hdr = strtolower($hdr); |
286 | if (in_array($hdr, $banana->parse_hdr)) { |
287 | $this->headers[$hdr] = $val; |
288 | } |
289 | } |
290 | } |
291 | // decode headers |
292 | foreach ($banana->hdecode as $hdr) { |
293 | if (isset($this->headers[$hdr])) { |
294 | $this->headers[$hdr] = headerDecode($this->headers[$hdr]); |
295 | } |
296 | } |
297 | |
298 | $this->name = $this->headers['from']; |
299 | $this->name = preg_replace('/<[^ ]*>/', '', $this->name); |
300 | $this->name = trim($this->name); |
50cd076e |
301 | return true; |
78cd27b3 |
302 | } |
303 | |
304 | function checkcancel() |
305 | { |
306 | if (function_exists('hook_checkcancel')) { |
307 | return hook_checkcancel($this->headers); |
308 | } |
309 | return ($this->headers['from'] == $_SESSION['name']." <".$_SESSION['mail'].">"); |
310 | } |
311 | |
1f75b135 |
312 | /** convert message to html |
313 | * @param partid INT id of the multipart message that must be displaid |
314 | */ |
cc43419f |
315 | function to_html($partid = -1) |
78cd27b3 |
316 | { |
317 | global $banana; |
318 | |
cc43419f |
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; |
1f75b135 |
342 | } |
343 | |
78cd27b3 |
344 | $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">'; |
345 | $res .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>'; |
ca3b1040 |
346 | $res .= '<tr><td class="headers"><table cellpadding="0" cellspacing="0">'; |
78cd27b3 |
347 | |
348 | foreach ($banana->show_hdr as $hdr) { |
349 | if (isset($this->headers[$hdr])) { |
350 | $res2 = formatdisplayheader($hdr, $this->headers[$hdr]); |
ca3b1040 |
351 | if ($res2 && ($hdr != 'x-face' || !$banana->formatxface)) { |
78cd27b3 |
352 | $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n"; |
ca3b1040 |
353 | } else if ($res2) { |
354 | $xface = $res2; |
78cd27b3 |
355 | } |
356 | } |
357 | } |
ca3b1040 |
358 | $res .= '</table></td><td class="xface">'; |
359 | |
360 | if ($xface) { |
361 | $res .= $xface; |
362 | } |
363 | $res .= '</td></tr>'; |
78cd27b3 |
364 | |
1f75b135 |
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) { |
376 | $res .= '<a href="?group='.$banana->state['group'].'&artid='.$this->id.'&part='.$i.'">'.$format.'</a>'; |
377 | } else { |
378 | $res .= $format; |
379 | } |
380 | } |
381 | } |
382 | $res .= '</th></tr>'; |
383 | |
384 | preg_match("@text/([^;]+);@", $this->headers['content-type'], $format); |
385 | $format = $format[1]; |
4cc80c9a |
386 | $res .= '<tr><td colspan="2"'; |
1f75b135 |
387 | if ($format == 'html') { |
4cc80c9a |
388 | if (preg_match('@<body[^>]*bgcolor="?([#0-9a-f]+)"?[^>]*>@i', $this->body, $bgcolor)) { |
389 | $res .= ' bgcolor="'.$bgcolor[1].'"'; |
390 | } |
391 | $res .= '>'.formatbody($this->body, $format); |
1f75b135 |
392 | } else { |
4cc80c9a |
393 | $res .= '><pre>'.formatbody($this->body).'</pre>'; |
1f75b135 |
394 | } |
395 | $res .= '</td></tr>'; |
7a0e2710 |
396 | |
397 | if (count($this->pj) > 0) { |
398 | $res .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>'; |
399 | $res .= '<tr><td colspan="2">'; |
400 | $i = 0; |
401 | foreach ($this->pj as $file) { |
402 | $res .= $file['filename'].' ('.$file['MIME'].') : '; |
236c6b2a |
403 | $res .= '<a href="?group='.$banana->state['group'].'&artid='.$this->id.'&pj='.$i.'">télécharger</a>'; |
404 | $res .= ' . <a href="?group='.$banana->state['group'].'&artid='.$this->id.'&pj='.$i.'&action=view" target="_blank">aperçu</a>'; |
7a0e2710 |
405 | $res .= '<br/>'; |
406 | $i++; |
407 | } |
408 | $res .= '</td></tr>'; |
409 | } |
78cd27b3 |
410 | |
cc43419f |
411 | $res .= '<tr><th colspan="2">'._b_('Apercu').'</th></tr>'; |
78cd27b3 |
412 | $ndx = $banana->spool->getndx($this->id); |
413 | $res .= '<tr><td class="thrd" colspan="2">'.$banana->spool->to_html($ndx-$banana->tbefore, $ndx+$banana->tafter, $ndx).'</td></tr>'; |
414 | |
415 | return $res.'</table>'; |
416 | } |
417 | } |
418 | |
419 | ?> |