| 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 | |
| 27 | /** constructor |
| 28 | * @param $_id STRING MSGNUM or MSGID (a group should be selected in this case) |
| 29 | */ |
| 30 | function BananaPost($_id) |
| 31 | { |
| 32 | global $banana; |
| 33 | $this->id = $_id; |
| 34 | $this->pj = array(); |
| 35 | $this->messages = array(); |
| 36 | $this->_header(); |
| 37 | |
| 38 | if ($body = $banana->nntp->body($_id)) { |
| 39 | $this->body = join("\n", $body); |
| 40 | } else { |
| 41 | return ($this = null); |
| 42 | } |
| 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 | } |
| 50 | } |
| 51 | |
| 52 | if ($this->_split_multipart($this->headers, $this->body)) { |
| 53 | $this->set_body_to_part(0); |
| 54 | } else { |
| 55 | $this->_split_multipart($mpart_type[1], $mpart_boundary[1]); |
| 56 | $this->_find_uuencode(); |
| 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 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 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') { |
| 73 | $this->body = trim(str_replace($match[0], '', $this->body)); |
| 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 | |
| 84 | /** split multipart messages |
| 85 | * @param $type STRING multipart type description |
| 86 | * @param $boundary STRING multipart boundary identification string |
| 87 | */ |
| 88 | function _split_multipart($headers, $body) |
| 89 | { |
| 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); |
| 98 | foreach ($parts as $part) { |
| 99 | $part = $this->_get_part($part); |
| 100 | $local_header = $part['headers']; |
| 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 | } |
| 124 | } |
| 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | /** extract new headers from the part |
| 130 | * @param $part STRING part of a multipart message |
| 131 | */ |
| 132 | function _get_part($part) |
| 133 | { |
| 134 | global $banana; |
| 135 | |
| 136 | $lines = split("\n", $part); |
| 137 | while (count($lines)) { |
| 138 | $line = array_shift($lines); |
| 139 | if ($line != "") { |
| 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 | } |
| 148 | } |
| 149 | } else { |
| 150 | break; |
| 151 | } |
| 152 | } |
| 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); |
| 158 | } |
| 159 | |
| 160 | /** add an attachment |
| 161 | */ |
| 162 | function _add_attachment($part) |
| 163 | { |
| 164 | $local_header = $part['headers']; |
| 165 | $local_body = $part['body']; |
| 166 | |
| 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 | } |
| 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)) { |
| 181 | return false; |
| 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)); |
| 188 | return true; |
| 189 | } |
| 190 | |
| 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 | } |
| 199 | if ($format[1] == 'richtext') { |
| 200 | return htmlToPlainText(richtextToHtml($this->body)); |
| 201 | } else { |
| 202 | return htmlToPlainText($this->body); |
| 203 | } |
| 204 | } |
| 205 | |
| 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 | */ |
| 210 | function get_attachment($pjid, $action = false) |
| 211 | { |
| 212 | if ($pjid >= count($this->pj)) { |
| 213 | return false; |
| 214 | } else { |
| 215 | $file = $this->pj[$pjid]; |
| 216 | header('Content-Type: '.$file['MIME'].'; name="'.$file['filename'].'"'); |
| 217 | if (!$action) { |
| 218 | header('Content-Disposition: attachment; filename="'.$file['filename'].'"'); |
| 219 | } else { |
| 220 | header('Content-Disposition: inline; filename="'.$file['filename'].'"'); |
| 221 | } |
| 222 | if ($file['encoding'] == 'base64') { |
| 223 | echo base64_decode($file['data']); |
| 224 | } else if ($file['encoding'] == 'x-uuencode') { |
| 225 | passthru('echo '.escapeshellarg($file['data']).' | uudecode -o /dev/stdout'); |
| 226 | } else { |
| 227 | header('Content-Transfer-Encoding: '.$file['encoding']); |
| 228 | echo $file['data']; |
| 229 | } |
| 230 | return true; |
| 231 | } |
| 232 | } |
| 233 | |
| 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 | } |
| 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); |
| 257 | } |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | function _header() |
| 262 | { |
| 263 | global $banana; |
| 264 | $hdrs = $banana->nntp->head($this->id); |
| 265 | if (!$hdrs) { |
| 266 | $this = null; |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | // parse headers |
| 271 | foreach ($hdrs as $line) { |
| 272 | if (preg_match("/^[\t\r ]+/", $line)) { |
| 273 | $line = ($hdr=="X-Face"?"":" ").ltrim($line); |
| 274 | if (in_array($hdr, $banana->parse_hdr)) { |
| 275 | $this->headers[$hdr] .= $line; |
| 276 | } |
| 277 | } else { |
| 278 | list($hdr, $val) = split(":[ \t\r]*", $line, 2); |
| 279 | $hdr = strtolower($hdr); |
| 280 | if (in_array($hdr, $banana->parse_hdr)) { |
| 281 | $this->headers[$hdr] = $val; |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | // decode headers |
| 286 | foreach ($banana->hdecode as $hdr) { |
| 287 | if (isset($this->headers[$hdr])) { |
| 288 | $this->headers[$hdr] = headerDecode($this->headers[$hdr]); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | $this->name = $this->headers['from']; |
| 293 | $this->name = preg_replace('/<[^ ]*>/', '', $this->name); |
| 294 | $this->name = trim($this->name); |
| 295 | } |
| 296 | |
| 297 | function checkcancel() |
| 298 | { |
| 299 | if (function_exists('hook_checkcancel')) { |
| 300 | return hook_checkcancel($this->headers); |
| 301 | } |
| 302 | return ($this->headers['from'] == $_SESSION['name']." <".$_SESSION['mail'].">"); |
| 303 | } |
| 304 | |
| 305 | /** convert message to html |
| 306 | * @param partid INT id of the multipart message that must be displaid |
| 307 | */ |
| 308 | function to_html($partid = -1) |
| 309 | { |
| 310 | global $banana; |
| 311 | |
| 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; |
| 335 | } |
| 336 | |
| 337 | $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">'; |
| 338 | $res .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>'; |
| 339 | $res .= '<tr><td class="headers"><table cellpadding="0" cellspacing="0">'; |
| 340 | |
| 341 | foreach ($banana->show_hdr as $hdr) { |
| 342 | if (isset($this->headers[$hdr])) { |
| 343 | $res2 = formatdisplayheader($hdr, $this->headers[$hdr]); |
| 344 | if ($res2 && ($hdr != 'x-face' || !$banana->formatxface)) { |
| 345 | $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n"; |
| 346 | } else if ($res2) { |
| 347 | $xface = $res2; |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | $res .= '</table></td><td class="xface">'; |
| 352 | |
| 353 | if ($xface) { |
| 354 | $res .= $xface; |
| 355 | } |
| 356 | $res .= '</td></tr>'; |
| 357 | |
| 358 | $res .= '<tr><th colspan="2">'._b_('Corps'); |
| 359 | if (count($this->messages) > 1) { |
| 360 | for ($i = 0 ; $i < count($this->messages) ; $i++) { |
| 361 | if ($i == 0) { |
| 362 | $res .= ' : '; |
| 363 | } else { |
| 364 | $res .= ' . '; |
| 365 | } |
| 366 | preg_match("@text/([^;]+);@", $this->messages[$i]['headers']['content-type'], $format); |
| 367 | $format = textFormat_translate($format[1]); |
| 368 | if ($i != $partid) { |
| 369 | $res .= '<a href="?group='.$banana->state['group'].'&artid='.$this->id.'&part='.$i.'">'.$format.'</a>'; |
| 370 | } else { |
| 371 | $res .= $format; |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | $res .= '</th></tr>'; |
| 376 | |
| 377 | preg_match("@text/([^;]+);@", $this->headers['content-type'], $format); |
| 378 | $format = $format[1]; |
| 379 | $res .= '<tr><td colspan="2"'; |
| 380 | if ($format == 'html') { |
| 381 | if (preg_match('@<body[^>]*bgcolor="?([#0-9a-f]+)"?[^>]*>@i', $this->body, $bgcolor)) { |
| 382 | $res .= ' bgcolor="'.$bgcolor[1].'"'; |
| 383 | } |
| 384 | $res .= '>'.formatbody($this->body, $format); |
| 385 | } else { |
| 386 | $res .= '><pre>'.formatbody($this->body).'</pre>'; |
| 387 | } |
| 388 | $res .= '</td></tr>'; |
| 389 | |
| 390 | if (count($this->pj) > 0) { |
| 391 | $res .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>'; |
| 392 | $res .= '<tr><td colspan="2">'; |
| 393 | $i = 0; |
| 394 | foreach ($this->pj as $file) { |
| 395 | $res .= $file['filename'].' ('.$file['MIME'].') : '; |
| 396 | $res .= '<a href="?group='.$banana->state['group'].'&artid='.$this->id.'&pj='.$i.'">télécharger</a>'; |
| 397 | $res .= ' . <a href="?group='.$banana->state['group'].'&artid='.$this->id.'&pj='.$i.'&action=view" target="_blank">aperçu</a>'; |
| 398 | $res .= '<br/>'; |
| 399 | $i++; |
| 400 | } |
| 401 | $res .= '</td></tr>'; |
| 402 | } |
| 403 | |
| 404 | $res .= '<tr><th colspan="2">'._b_('Apercu').'</th></tr>'; |
| 405 | $ndx = $banana->spool->getndx($this->id); |
| 406 | $res .= '<tr><td class="thrd" colspan="2">'.$banana->spool->to_html($ndx-$banana->tbefore, $ndx+$banana->tafter, $ndx).'</td></tr>'; |
| 407 | |
| 408 | return $res.'</table>'; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | ?> |