=?utf-8?q?Bon=20en=20fait=20skinnage=20des=20blockquote=20par=20la=20CSS
[banana.git] / banana / post.inc.php
CommitLineData
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 13class 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 }
d43ebde4 153 return Array('headers' => $local_headers, 'body' => join("\n", $lines));
154 }
155
8f6f50fb 156 /** add an attachment
157 */
d38804c3 158 function _add_attachment($part)
8f6f50fb 159 {
d43ebde4 160 $local_header = $part['headers'];
161 $local_body = $part['body'];
162
df7b8d75 163 if ((isset($local_header['content-disposition']) && preg_match("/filename=\"?([^\"]+)\"?/", $local_header['content-disposition'], $filename))
164 || (isset($local_header['content-type']) && preg_match("/name=\"?([^\"]+)\"?/", $local_header['content-type'], $filename))) {
165 $filename = $filename[1];
166 }
d43ebde4 167 if (!isset($filename)) {
168 $filename = "attachment".count($pj);
169 }
170
171 if (isset($local_header['content-type'])) {
172 if (preg_match("/^\\s*([^ ;]+);/", $local_header['content-type'], $mimetype)) {
173 $mimetype = $mimetype[1];
174 }
175 }
176 if (!isset($mimetype)) {
8f6f50fb 177 return false;
d43ebde4 178 }
179
180 array_push($this->pj, Array('MIME' => $mimetype,
181 'filename' => $filename,
182 'encoding' => strtolower($local_header['content-transfer-encoding']),
183 'data' => $local_body));
8f6f50fb 184 return true;
d43ebde4 185 }
186
d38804c3 187 /** return body in plain text (useful for messages without a text/plain part)
188 */
189 function get_body()
190 {
191 preg_match("@text/([^;]+);@", $this->headers['content-type'], $format);
192 if ($format[1] == 'plain') {
193 return $this->body;
194 }
5d133234 195 if ($format[1] == 'richtext') {
196 return htmlToPlainText(richtextToHtml($this->body));
197 } else {
198 return htmlToPlainText($this->body);
d38804c3 199 }
d38804c3 200 }
201
d43ebde4 202 /** decode an attachment
203 * @param pjid INT id of the attachment to decode
204 * @param action BOOL action to execute : true=view, false=download
205 */
8f6f50fb 206 function get_attachment($pjid, $action = false)
207 {
d43ebde4 208 if ($pjid >= count($this->pj)) {
209 return false;
210 } else {
211 $file = $this->pj[$pjid];
d38804c3 212 header('Content-Type: '.$file['MIME'].'; name="'.$file['filename'].'"');
d43ebde4 213 if (!$action) {
214 header('Content-Disposition: attachment; filename="'.$file['filename'].'"');
d38804c3 215 } else {
216 header('Content-Disposition: inline; filename="'.$file['filename'].'"');
217 }
d43ebde4 218 if ($file['encoding'] == 'base64') {
219 echo base64_decode($file['data']);
e0f141dd 220 } else if ($file['encoding'] == 'x-uuencode') {
221 passthru('echo '.escapeshellarg($file['data']).' | uudecode -o /dev/stdout');
d43ebde4 222 } else {
223 header('Content-Transfer-Encoding: '.$file['encoding']);
224 echo $file['data'];
225 }
226 return true;
227 }
228 }
229
8f6f50fb 230 /** set body to represent the given part
231 * @param partid INT index of the part in messages
232 */
233 function set_body_to_part($partid)
234 {
235 global $banana;
236
237 if (count($this->messages) == 0) {
238 return false;
239 }
240
241 $local_header = $this->messages[$partid]['headers'];
242 $this->body = $this->messages[$partid]['body'];
243 foreach ($banana->parse_hdr as $hdr) {
244 if (isset($local_header[$hdr])) {
245 $this->headers[$hdr] = $local_header[$hdr];
246 }
247 }
c6a522df 248
249 if (preg_match('!charset=([^;]*)\s*(;|$)!', $this->headers['content-type'], $matches)) {
250 $this->body = iconv($matches[1], 'utf-8', $this->body);
251 } else {
252 $this->body = utf8_encode($this->body);
253 }
8f6f50fb 254 return true;
255 }
256
2dbc0167 257 function _header()
01681efd 258 {
2dbc0167 259 global $banana;
b9ea5b30 260 $hdrs = $banana->nntp->head($this->id);
e785d91c 261 if (!$hdrs) {
262 $this = null;
263 return false;
264 }
01681efd 265
e785d91c 266 // parse headers
267 foreach ($hdrs as $line) {
268 if (preg_match("/^[\t\r ]+/", $line)) {
01681efd 269 $line = ($hdr=="X-Face"?"":" ").ltrim($line);
2dbc0167 270 if (in_array($hdr, $banana->parse_hdr)) {
01681efd 271 $this->headers[$hdr] .= $line;
e785d91c 272 }
273 } else {
274 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
01681efd 275 $hdr = strtolower($hdr);
2dbc0167 276 if (in_array($hdr, $banana->parse_hdr)) {
01681efd 277 $this->headers[$hdr] = $val;
e785d91c 278 }
e785d91c 279 }
280 }
281 // decode headers
2dbc0167 282 foreach ($banana->hdecode as $hdr) {
01681efd 283 if (isset($this->headers[$hdr])) {
284 $this->headers[$hdr] = headerDecode($this->headers[$hdr]);
e785d91c 285 }
286 }
01681efd 287
288 $this->name = $this->headers['from'];
289 $this->name = preg_replace('/<[^ ]*>/', '', $this->name);
290 $this->name = trim($this->name);
1eed39ee 291 }
b9ea5b30 292
65d96b1f 293 function checkcancel()
294 {
b9ea5b30 295 if (function_exists('hook_checkcancel')) {
296 return hook_checkcancel($this->headers);
297 }
298 return ($this->headers['from'] == $_SESSION['name']." <".$_SESSION['mail'].">");
299 }
300
8f6f50fb 301 /** convert message to html
302 * @param partid INT id of the multipart message that must be displaid
303 */
c6a522df 304 function to_html($partid = -1)
65d96b1f 305 {
306 global $banana;
307
c6a522df 308 if (count($this->messages) > 1) {
309 if ($partid != -1) {
310 $this->set_body_to_part($partid);
311 } else {
312 // Select prefered text-format
313 foreach ($banana->body_mime as $mime) {
314 for ($id = 0 ; $id < count($this->messages) ; $id++) {
315 if (preg_match("@$mime@", $this->messages[$id]['headers']['content-type'])) {
316 $partid = $id;
317 $this->set_body_to_part($partid);
318 break;
319 }
320 }
321 if ($partid != -1) {
322 break;
323 }
324 }
325 if ($partid == -1) {
326 $partid = 0;
327 }
328 }
329 } else {
330 $partid = 0;
8f6f50fb 331 }
332
65d96b1f 333 $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">';
334 $res .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>';
335
336 foreach ($banana->show_hdr as $hdr) {
337 if (isset($this->headers[$hdr])) {
338 $res2 = formatdisplayheader($hdr, $this->headers[$hdr]);
339 if ($res2) {
340 $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n";
341 }
342 }
343 }
344
8f6f50fb 345 $res .= '<tr><th colspan="2">'._b_('Corps');
346 if (count($this->messages) > 1) {
347 for ($i = 0 ; $i < count($this->messages) ; $i++) {
348 if ($i == 0) {
349 $res .= ' : ';
350 } else {
351 $res .= ' . ';
352 }
353 preg_match("@text/([^;]+);@", $this->messages[$i]['headers']['content-type'], $format);
354 $format = textFormat_translate($format[1]);
355 if ($i != $partid) {
356 $res .= '<a href="?group='.$banana->state['group'].'&artid='.$this->id.'&part='.$i.'">'.$format.'</a>';
357 } else {
358 $res .= $format;
359 }
360 }
361 }
362 $res .= '</th></tr>';
363
364 preg_match("@text/([^;]+);@", $this->headers['content-type'], $format);
365 $format = $format[1];
e27ae1a3 366 $res .= '<tr><td colspan="2"';
8f6f50fb 367 if ($format == 'html') {
e27ae1a3 368 if (preg_match('@<body[^>]*bgcolor="?([#0-9a-f]+)"?[^>]*>@i', $this->body, $bgcolor)) {
369 $res .= ' bgcolor="'.$bgcolor[1].'"';
370 }
371 $res .= '>'.formatbody($this->body, $format);
8f6f50fb 372 } else {
e27ae1a3 373 $res .= '><pre>'.formatbody($this->body).'</pre>';
8f6f50fb 374 }
375 $res .= '</td></tr>';
d43ebde4 376
377 if (count($this->pj) > 0) {
378 $res .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>';
379 $res .= '<tr><td colspan="2">';
380 $i = 0;
381 foreach ($this->pj as $file) {
382 $res .= $file['filename'].' ('.$file['MIME'].') : ';
383 $res .= '<a href="pj.php?group='.$banana->state['group'].'&artid='.$this->id.'&pj='.$i.'">télécharger</a>';
51e89e48 384 $res .= ' . <a href="pj.php?group='.$banana->state['group'].'&artid='.$this->id.'&pj='.$i.'&action=view" target="_blank">aperçu</a>';
d43ebde4 385 $res .= '<br/>';
386 $i++;
387 }
388 $res .= '</td></tr>';
389 }
65d96b1f 390
c6a522df 391 $res .= '<tr><th colspan="2">'._b_('Apercu').'</th></tr>';
65d96b1f 392 $ndx = $banana->spool->getndx($this->id);
393 $res .= '<tr><td class="thrd" colspan="2">'.$banana->spool->to_html($ndx-$banana->tbefore, $ndx+$banana->tafter, $ndx).'</td></tr>';
394
395 return $res.'</table>';
396 }
1eed39ee 397}
398
399?>