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