Brand new link abstraction allowing link format customization
[banana.git] / banana / post.inc.php
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;
20 /** formating */
21 var $messages;
22 /** attachment */
23 var $pj;
24 /** poster name */
25 var $name;
26 /** test validity */
27 var $valid = true;
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;
35 $this->id = $_id;
36 $this->pj = array();
37 $this->messages = array();
38 if (!$this->_header()) {
39 $this->valid = false;
40 return null;
41 }
42
43
44 if ($body = $banana->nntp->body($_id)) {
45 $this->body = join("\n", $body);
46 } else {
47 $this->valid = false;
48 return null;
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
59 if ($this->_split_multipart($this->headers, $this->body)) {
60 $this->set_body_to_part(0);
61 } else {
62 $this->_split_multipart($mpart_type[1], $mpart_boundary[1]);
63 $this->_find_uuencode();
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 }
69 }
70 }
71
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') {
80 $this->body = trim(str_replace($match[0], '', $this->body));
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
91 /** split multipart messages
92 * @param $type STRING multipart type description
93 * @param $boundary STRING multipart boundary identification string
94 */
95 function _split_multipart($headers, $body)
96 {
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);
105 foreach ($parts as $part) {
106 $part = $this->_get_part($part);
107 $local_header = $part['headers'];
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 }
131 }
132 }
133 return true;
134 }
135
136 /** extract new headers from the part
137 * @param $part STRING part of a multipart message
138 */
139 function _get_part($part)
140 {
141 global $banana;
142
143 $lines = split("\n", $part);
144 while (count($lines)) {
145 $line = array_shift($lines);
146 if ($line != "") {
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 }
155 }
156 } else {
157 break;
158 }
159 }
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);
165 }
166
167 /** add an attachment
168 */
169 function _add_attachment($part)
170 {
171 $local_header = $part['headers'];
172 $local_body = $part['body'];
173
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 }
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)) {
188 return false;
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));
195 return true;
196 }
197
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 }
206 if ($format[1] == 'richtext') {
207 return htmlToPlainText(richtextToHtml($this->body));
208 } else {
209 return htmlToPlainText($this->body);
210 }
211 }
212
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 */
217 function get_attachment($pjid, $action = false)
218 {
219 if ($pjid >= count($this->pj)) {
220 return false;
221 } else {
222 $file = $this->pj[$pjid];
223 header('Content-Type: '.$file['MIME'].'; name="'.$file['filename'].'"');
224 if (!$action) {
225 header('Content-Disposition: attachment; filename="'.$file['filename'].'"');
226 } else {
227 header('Content-Disposition: inline; filename="'.$file['filename'].'"');
228 }
229 if ($file['encoding'] == 'base64') {
230 echo base64_decode($file['data']);
231 } else if ($file['encoding'] == 'x-uuencode') {
232 passthru('echo '.escapeshellarg($file['data']).' | uudecode -o /dev/stdout');
233 } else {
234 header('Content-Transfer-Encoding: '.$file['encoding']);
235 echo $file['data'];
236 }
237 return true;
238 }
239 }
240
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 }
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);
264 }
265 return true;
266 }
267
268 function _header()
269 {
270 global $banana;
271 $hdrs = $banana->nntp->head($this->id);
272 if (!$hdrs) {
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);
301 return true;
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
312 /** convert message to html
313 * @param partid INT id of the multipart message that must be displaid
314 */
315 function to_html($partid = -1)
316 {
317 global $banana;
318
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;
342 }
343
344 $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">';
345 $res .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>';
346 $res .= '<tr><td class="headers"><table cellpadding="0" cellspacing="0">';
347
348 foreach ($banana->show_hdr as $hdr) {
349 if (isset($this->headers[$hdr])) {
350 $res2 = formatdisplayheader($hdr, $this->headers[$hdr]);
351 if ($res2 && ($hdr != 'x-face' || !$banana->formatxface)) {
352 $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n";
353 } else if ($res2) {
354 $xface = $res2;
355 }
356 }
357 }
358 $res .= '</table></td><td class="xface">';
359
360 if ($xface) {
361 $res .= $xface;
362 }
363 $res .= '</td></tr>';
364
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 .= makeHREF(Array('group' => $banana->state['group'],
377 'artid' => $this->id,
378 'part' => $i),
379 $format);
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];
389 $res .= '<tr><td colspan="2"';
390 if ($format == 'html') {
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);
395 } else {
396 $res .= '><pre>'.formatbody($this->body).'</pre>';
397 }
398 $res .= '</td></tr>';
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'].') : ';
406 $res .= makeHREF(Array('group' => $banana->state['group'],
407 'artid' => $this->id,
408 'pj' => $i),
409 _b_('télécharger'));
410 $res .= ' . ';
411 $res .= makeHREF(Array('group' => $banana->state['group'],
412 'artid' => $this->id,
413 'pj' => $i,
414 'action'=> 'view'),
415 _b_('aperçu'));
416 $res .= '<br/>';
417 $i++;
418 }
419 $res .= '</td></tr>';
420 }
421
422 $res .= '<tr><th colspan="2">'._b_('Apercu').'</th></tr>';
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 }
428 }
429
430 ?>