| 1 | <?php |
| 2 | /** mime.php |
| 3 | ** |
| 4 | ** This contains the functions necessary to detect and decode MIME |
| 5 | ** messages. |
| 6 | ** |
| 7 | ** $Id$ |
| 8 | **/ |
| 9 | |
| 10 | if (defined('mime_php')) |
| 11 | return; |
| 12 | define('mime_php', true); |
| 13 | |
| 14 | require_once('../functions/imap.php'); |
| 15 | |
| 16 | /** Setting up the objects that have the structure for the message **/ |
| 17 | |
| 18 | class msg_header { |
| 19 | /** msg_header contains generic variables for values that **/ |
| 20 | /** could be in a header. **/ |
| 21 | |
| 22 | var $type0 = '', $type1 = '', $boundary = '', $charset = ''; |
| 23 | var $encoding = '', $size = 0, $to = array(), $from = '', $date = ''; |
| 24 | var $cc = array(), $bcc = array(), $reply_to = '', $subject = ''; |
| 25 | var $id = 0, $mailbox = '', $description = '', $filename = ''; |
| 26 | var $entity_id = 0, $message_id = 0, $name = ''; |
| 27 | } |
| 28 | |
| 29 | class message { |
| 30 | /** message is the object that contains messages. It is a recursive |
| 31 | object in that through the $entities variable, it can contain |
| 32 | more objects of type message. See documentation in mime.txt for |
| 33 | a better description of how this works. |
| 34 | **/ |
| 35 | var $header = ''; |
| 36 | var $entities = array(); |
| 37 | |
| 38 | function addEntity ($msg) { |
| 39 | $this->entities[] = $msg; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /* --------------------------------------------------------------------------------- */ |
| 44 | /* MIME DECODING */ |
| 45 | /* --------------------------------------------------------------------------------- */ |
| 46 | |
| 47 | // This function gets the structure of a message and stores it in the "message" class. |
| 48 | // It will return this object for use with all relevant header information and |
| 49 | // fully parsed into the standard "message" object format. |
| 50 | function mime_structure ($imap_stream, $header) { |
| 51 | |
| 52 | sqimap_messages_flag ($imap_stream, $header->id, $header->id, 'Seen'); |
| 53 | $ssid = sqimap_session_id(); |
| 54 | $lsid = strlen( $ssid ); |
| 55 | $id = $header->id; |
| 56 | fputs ($imap_stream, "$ssid FETCH $id BODYSTRUCTURE\r\n"); |
| 57 | // |
| 58 | // This should use sqimap_read_data instead of reading it itself |
| 59 | // |
| 60 | $read = fgets ($imap_stream, 10000); |
| 61 | $bodystructure = ''; |
| 62 | while( substr($read, 0, $lsid) <> $ssid && |
| 63 | !feof( $imap_stream ) ) { |
| 64 | $bodystructure .= $read; |
| 65 | $read = fgets ($imap_stream, 10000); |
| 66 | } |
| 67 | $read = $bodystructure; |
| 68 | |
| 69 | // isolate the body structure and remove beginning and end parenthesis |
| 70 | $read = trim(substr ($read, strpos(strtolower($read), 'bodystructure') + 13)); |
| 71 | $read = trim(substr ($read, 0, -1)); |
| 72 | $end = mime_match_parenthesis(0, $read); |
| 73 | while ($end == strlen($read)-1) { |
| 74 | $read = trim(substr ($read, 0, -1)); |
| 75 | $read = trim(substr ($read, 1)); |
| 76 | $end = mime_match_parenthesis(0, $read); |
| 77 | } |
| 78 | |
| 79 | $msg = mime_parse_structure ($read, 0); |
| 80 | $msg->header = $header; |
| 81 | return $msg; |
| 82 | } |
| 83 | |
| 84 | // this starts the parsing of a particular structure. It is called recursively, |
| 85 | // so it can be passed different structures. It returns an object of type |
| 86 | // $message. |
| 87 | // First, it checks to see if it is a multipart message. If it is, then it |
| 88 | // handles that as it sees is necessary. If it is just a regular entity, |
| 89 | // then it parses it and adds the necessary header information (by calling out |
| 90 | // to mime_get_elements() |
| 91 | function mime_parse_structure ($structure, $ent_id) { |
| 92 | |
| 93 | $msg = new message(); |
| 94 | if ($structure{0} == '(') { |
| 95 | $ent_id = mime_new_element_level($ent_id); |
| 96 | $start = $end = -1; |
| 97 | do { |
| 98 | $start = $end+1; |
| 99 | $end = mime_match_parenthesis ($start, $structure); |
| 100 | |
| 101 | $element = substr($structure, $start+1, ($end - $start)-1); |
| 102 | $ent_id = mime_increment_id ($ent_id); |
| 103 | $newmsg = mime_parse_structure ($element, $ent_id); |
| 104 | $msg->addEntity ($newmsg); |
| 105 | } while ($structure{$end+1} == '('); |
| 106 | } else { |
| 107 | // parse the elements |
| 108 | $msg = mime_get_element ($structure, $msg, $ent_id); |
| 109 | } |
| 110 | return $msg; |
| 111 | } |
| 112 | |
| 113 | // Increments the element ID. An element id can look like any of |
| 114 | // the following: 1, 1.2, 4.3.2.4.1, etc. This function increments |
| 115 | // the last number of the element id, changing 1.2 to 1.3. |
| 116 | function mime_increment_id ($id) { |
| 117 | |
| 118 | if (strpos($id, ".")) { |
| 119 | $first = substr($id, 0, strrpos($id, ".")); |
| 120 | $last = substr($id, strrpos($id, ".")+1); |
| 121 | $last++; |
| 122 | $new = $first . "." .$last; |
| 123 | } else { |
| 124 | $new = $id + 1; |
| 125 | } |
| 126 | |
| 127 | return $new; |
| 128 | } |
| 129 | |
| 130 | // See comment for mime_increment_id(). |
| 131 | // This adds another level on to the entity_id changing 1.3 to 1.3.0 |
| 132 | // NOTE: 1.3.0 is not a valid element ID. It MUST be incremented |
| 133 | // before it can be used. I left it this way so as not to have |
| 134 | // to make a special case if it is the first entity_id. It |
| 135 | // always increments it, and that works fine. |
| 136 | function mime_new_element_level ($id) { |
| 137 | |
| 138 | if (!$id) { |
| 139 | $id = 0; |
| 140 | } else { |
| 141 | $id = $id . '.0'; |
| 142 | } |
| 143 | |
| 144 | return( $id ); |
| 145 | } |
| 146 | |
| 147 | function mime_get_element (&$structure, $msg, $ent_id) { |
| 148 | |
| 149 | $elem_num = 1; |
| 150 | $msg->header = new msg_header(); |
| 151 | $msg->header->entity_id = $ent_id; |
| 152 | $properties = array(); |
| 153 | |
| 154 | while (strlen($structure) > 0) { |
| 155 | $structure = trim($structure); |
| 156 | $char = $structure{0}; |
| 157 | |
| 158 | if (strtolower(substr($structure, 0, 3)) == 'nil') { |
| 159 | $text = ''; |
| 160 | $structure = substr($structure, 3); |
| 161 | } else if ($char == '"') { |
| 162 | // loop through until we find the matching quote, and return that as a string |
| 163 | $pos = 1; |
| 164 | $text = ''; |
| 165 | while ( ($char = $structure{$pos} ) <> '"' && $pos < strlen($structure)) { |
| 166 | $text .= $char; |
| 167 | $pos++; |
| 168 | } |
| 169 | $structure = substr($structure, strlen($text) + 2); |
| 170 | } else if ($char == '(') { |
| 171 | // comment me |
| 172 | $end = mime_match_parenthesis (0, $structure); |
| 173 | $sub = substr($structure, 1, $end-1); |
| 174 | $properties = mime_get_props($properties, $sub); |
| 175 | $structure = substr($structure, strlen($sub) + 2); |
| 176 | } else { |
| 177 | // loop through until we find a space or an end parenthesis |
| 178 | $pos = 0; |
| 179 | $char = $structure{$pos}; |
| 180 | $text = ''; |
| 181 | while ($char != ' ' && $char != ')' && $pos < strlen($structure)) { |
| 182 | $text .= $char; |
| 183 | $pos++; |
| 184 | $char = $structure{$pos}; |
| 185 | } |
| 186 | $structure = substr($structure, strlen($text)); |
| 187 | } |
| 188 | |
| 189 | // This is where all the text parts get put into the header |
| 190 | switch ($elem_num) { |
| 191 | case 1: |
| 192 | $msg->header->type0 = strtolower($text); |
| 193 | break; |
| 194 | case 2: |
| 195 | $msg->header->type1 = strtolower($text); |
| 196 | break; |
| 197 | case 4: // Id |
| 198 | // Invisimail enclose images with <> |
| 199 | $msg->header->id = str_replace( '<', '', str_replace( '>', '', $text ) ); |
| 200 | break; |
| 201 | case 5: |
| 202 | $msg->header->description = $text; |
| 203 | break; |
| 204 | case 6: |
| 205 | $msg->header->encoding = strtolower($text); |
| 206 | break; |
| 207 | case 7: |
| 208 | $msg->header->size = $text; |
| 209 | break; |
| 210 | default: |
| 211 | if ($msg->header->type0 == 'text' && $elem_num == 8) { |
| 212 | // This is a plain text message, so lets get the number of lines |
| 213 | // that it contains. |
| 214 | $msg->header->num_lines = $text; |
| 215 | |
| 216 | } else if ($msg->header->type0 == 'message' && $msg->header->type1 == 'rfc822' && $elem_num == 8) { |
| 217 | // This is an encapsulated message, so lets start all over again and |
| 218 | // parse this message adding it on to the existing one. |
| 219 | $structure = trim($structure); |
| 220 | if ( $structure{0} == '(' ) { |
| 221 | $e = mime_match_parenthesis (0, $structure); |
| 222 | $structure = substr($structure, 0, $e); |
| 223 | $structure = substr($structure, 1); |
| 224 | $m = mime_parse_structure($structure, $msg->header->entity_id); |
| 225 | |
| 226 | // the following conditional is there to correct a bug that wasn't |
| 227 | // incrementing the entity IDs correctly because of the special case |
| 228 | // that message/rfc822 is. This fixes it fine. |
| 229 | if (substr($structure, 1, 1) != '(') |
| 230 | $m->header->entity_id = mime_increment_id(mime_new_element_level($ent_id)); |
| 231 | |
| 232 | // Now we'll go through and reformat the results. |
| 233 | if ($m->entities) { |
| 234 | for ($i=0; $i < count($m->entities); $i++) { |
| 235 | $msg->addEntity($m->entities[$i]); |
| 236 | } |
| 237 | } else { |
| 238 | $msg->addEntity($m); |
| 239 | } |
| 240 | $structure = ""; |
| 241 | } |
| 242 | } |
| 243 | break; |
| 244 | } |
| 245 | $elem_num++; |
| 246 | $text = ""; |
| 247 | } |
| 248 | // loop through the additional properties and put those in the various headers |
| 249 | if ($msg->header->type0 != 'message') { |
| 250 | for ($i=0; $i < count($properties); $i++) { |
| 251 | $msg->header->{$properties[$i]['name']} = $properties[$i]['value']; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | return $msg; |
| 256 | } |
| 257 | |
| 258 | // I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't |
| 259 | // figure out how to do this part, so I decided to go to bed. I woke up |
| 260 | // in the morning and had a flash of insight. I went to the white-board |
| 261 | // and scribbled it out, then spent a bit programming it, and this is the |
| 262 | // result. Nothing complicated, but I think my brain was fried yesterday. |
| 263 | // Funny how that happens some times. |
| 264 | // |
| 265 | // This gets properties in a nested parenthesisized list. For example, |
| 266 | // this would get passed something like: ("attachment" ("filename" "luke.tar.gz")) |
| 267 | // This returns an array called $props with all paired up properties. |
| 268 | // It ignores the "attachment" for now, maybe that should change later |
| 269 | // down the road. In this case, what is returned is: |
| 270 | // $props[0]["name"] = "filename"; |
| 271 | // $props[0]["value"] = "luke.tar.gz"; |
| 272 | function mime_get_props ($props, $structure) { |
| 273 | |
| 274 | while (strlen($structure) > 0) { |
| 275 | $structure = trim($structure); |
| 276 | $char = $structure{0}; |
| 277 | |
| 278 | if ($char == '"') { |
| 279 | $pos = 1; |
| 280 | $tmp = ''; |
| 281 | while ( ( $char = $structure{$pos} ) != '"' && |
| 282 | $pos < strlen($structure)) { |
| 283 | $tmp .= $char; |
| 284 | $pos++; |
| 285 | } |
| 286 | $structure = trim(substr($structure, strlen($tmp) + 2)); |
| 287 | $char = $structure{0}; |
| 288 | |
| 289 | if ($char == '"') { |
| 290 | $pos = 1; |
| 291 | $value = ''; |
| 292 | while ( ( $char = $structure{$pos} ) != '"' && |
| 293 | $pos < strlen($structure) ) { |
| 294 | $value .= $char; |
| 295 | $pos++; |
| 296 | } |
| 297 | $structure = trim(substr($structure, strlen($tmp) + 2)); |
| 298 | |
| 299 | $k = count($props); |
| 300 | $props[$k]['name'] = strtolower($tmp); |
| 301 | $props[$k]['value'] = $value; |
| 302 | } else if ($char == '(') { |
| 303 | $end = mime_match_parenthesis (0, $structure); |
| 304 | $sub = substr($structure, 1, $end-1); |
| 305 | if (! isset($props)) |
| 306 | $props = array(); |
| 307 | $props = mime_get_props($props, $sub); |
| 308 | $structure = substr($structure, strlen($sub) + 2); |
| 309 | } |
| 310 | return $props; |
| 311 | } else if ($char == '(') { |
| 312 | $end = mime_match_parenthesis (0, $structure); |
| 313 | $sub = substr($structure, 1, $end-1); |
| 314 | $props = mime_get_props($props, $sub); |
| 315 | $structure = substr($structure, strlen($sub) + 2); |
| 316 | return $props; |
| 317 | } else { |
| 318 | return $props; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | // Matches parenthesis. It will return the position of the matching |
| 324 | // parenthesis in $structure. For instance, if $structure was: |
| 325 | // ("text" "plain" ("val1name", "1") nil ... ) |
| 326 | // x x |
| 327 | // then this would return 42 to match up those two. |
| 328 | function mime_match_parenthesis ($pos, $structure) { |
| 329 | |
| 330 | $j = strlen( $structure ); |
| 331 | |
| 332 | // ignore all extra characters |
| 333 | // If inside of a string, skip string -- Boundary IDs and other |
| 334 | // things can have ) in them. |
| 335 | if( $structure{$pos} != '(' ) |
| 336 | return( $j ); |
| 337 | |
| 338 | while( $pos < $j ) { |
| 339 | $pos++; |
| 340 | if ($structure{$pos} == ')') { |
| 341 | return $pos; |
| 342 | } elseif ($structure{$pos} == '"') { |
| 343 | $pos++; |
| 344 | while( $structure{$pos} != '"' && |
| 345 | $pos < $j ) { |
| 346 | if (substr($structure, $pos, 2) == '\\"') |
| 347 | $pos++; |
| 348 | elseif (substr($structure, $pos, 2) == '\\\\') |
| 349 | $pos++; |
| 350 | $pos++; |
| 351 | } |
| 352 | } elseif ( $structure{$pos} == '(' ) { |
| 353 | $pos = mime_match_parenthesis ($pos, $structure); |
| 354 | } |
| 355 | } |
| 356 | echo "Error decoding mime structure. Report this as a bug!<br>\n"; |
| 357 | return( $pos ); |
| 358 | } |
| 359 | |
| 360 | function mime_fetch_body ($imap_stream, $id, $ent_id) { |
| 361 | // do a bit of error correction. If we couldn't find the entity id, just guess |
| 362 | // that it is the first one. That is usually the case anyway. |
| 363 | if (!$ent_id) $ent_id = 1; |
| 364 | |
| 365 | fputs ($imap_stream, sqimap_session_id() . " FETCH $id BODY[$ent_id]\r\n"); |
| 366 | $data = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message); |
| 367 | $topline = array_shift($data); |
| 368 | while (! ereg('\\* [0-9]+ FETCH ', $topline) && $data) |
| 369 | $topline = array_shift($data); |
| 370 | $wholemessage = implode('', $data); |
| 371 | |
| 372 | if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) { |
| 373 | return substr($wholemessage, 0, $regs[1]); |
| 374 | } |
| 375 | else if (ereg('"([^"]*)"', $topline, $regs)) { |
| 376 | return $regs[1]; |
| 377 | } |
| 378 | |
| 379 | $str = "Body retrieval error. Please report this bug!\n" . |
| 380 | "Response: $response\n" . |
| 381 | "Message: $message\n" . |
| 382 | "FETCH line: $topline" . |
| 383 | "---------------\n$wholemessage"; |
| 384 | foreach ($data as $d) { |
| 385 | $str .= htmlspecialchars($d) . "\n"; |
| 386 | } |
| 387 | return $str; |
| 388 | } |
| 389 | |
| 390 | function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) { |
| 391 | // do a bit of error correction. If we couldn't find the entity id, just guess |
| 392 | // that it is the first one. That is usually the case anyway. |
| 393 | if (!$ent_id) $ent_id = 1; |
| 394 | $sid = sqimap_session_id(); |
| 395 | // Don't kill the connection if the browser is over a dialup |
| 396 | // and it would take over 30 seconds to download it. |
| 397 | set_time_limit(0); |
| 398 | |
| 399 | fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n"); |
| 400 | $cnt = 0; |
| 401 | $continue = true; |
| 402 | $read = fgets ($imap_stream,4096); |
| 403 | // This could be bad -- if the section has sqimap_session_id() . ' OK' |
| 404 | // or similar, it will kill the download. |
| 405 | while (!ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) { |
| 406 | if (trim($read) == ')==') { |
| 407 | $read1 = $read; |
| 408 | $read = fgets ($imap_stream,4096); |
| 409 | if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) { |
| 410 | return; |
| 411 | } else { |
| 412 | echo decodeBody($read1, $encoding) . |
| 413 | decodeBody($read, $encoding); |
| 414 | } |
| 415 | } else if ($cnt) { |
| 416 | echo decodeBody($read, $encoding); |
| 417 | } |
| 418 | $read = fgets ($imap_stream,4096); |
| 419 | $cnt++; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /* -[ END MIME DECODING ]----------------------------------------------------------- */ |
| 424 | |
| 425 | |
| 426 | |
| 427 | /** This is the first function called. It decides if this is a multipart |
| 428 | message or if it should be handled as a single entity |
| 429 | **/ |
| 430 | function decodeMime ($imap_stream, &$header) { |
| 431 | global $username, $key, $imapServerAddress, $imapPort; |
| 432 | return mime_structure ($imap_stream, $header); |
| 433 | } |
| 434 | |
| 435 | // This is here for debugging purposese. It will print out a list |
| 436 | // of all the entity IDs that are in the $message object. |
| 437 | /* |
| 438 | function listEntities ($message) { |
| 439 | if ($message) { |
| 440 | if ($message->header->entity_id) |
| 441 | echo "<tt>" . $message->header->entity_id . ' : ' . $message->header->type0 . '/' . $message->header->type1 . '<br>'; |
| 442 | for ($i = 0; $message->entities[$i]; $i++) { |
| 443 | $msg = listEntities($message->entities[$i], $ent_id); |
| 444 | if ($msg) |
| 445 | return $msg; |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | */ |
| 450 | |
| 451 | // returns a $message object for a particular entity id |
| 452 | function getEntity ($message, $ent_id) { |
| 453 | if ($message) { |
| 454 | if ($message->header->entity_id == $ent_id && strlen($ent_id) == strlen($message->header->entity_id)) { |
| 455 | return $message; |
| 456 | } else { |
| 457 | for ($i = 0; isset($message->entities[$i]); $i++) { |
| 458 | $msg = getEntity ($message->entities[$i], $ent_id); |
| 459 | if ($msg) |
| 460 | return $msg; |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | // figures out what entity to display and returns the $message object |
| 467 | // for that entity. |
| 468 | function findDisplayEntity ($message, $textOnly = 1) |
| 469 | { |
| 470 | global $show_html_default; |
| 471 | |
| 472 | if (! $message) |
| 473 | return 0; |
| 474 | |
| 475 | if ($message->header->type0 == 'multipart' && |
| 476 | $message->header->type1 == 'alternative' && |
| 477 | $show_html_default && ! $textOnly) { |
| 478 | $entity = findDisplayEntityHTML($message); |
| 479 | if ($entity != 0) |
| 480 | return $entity; |
| 481 | } |
| 482 | |
| 483 | // Show text/plain or text/html -- the first one we find. |
| 484 | if ( $message->header->type0 == 'text' && |
| 485 | ( $message->header->type1 == 'plain' || |
| 486 | $message->header->type1 == 'html' ) && |
| 487 | isset($message->header->entity_id) ) |
| 488 | return $message->header->entity_id; |
| 489 | |
| 490 | for ($i=0; isset($message->entities[$i]); $i++) { |
| 491 | $entity = findDisplayEntity($message->entities[$i], $textOnly); |
| 492 | if ($entity != 0) |
| 493 | return $entity; |
| 494 | } |
| 495 | |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | // Shows the HTML version |
| 500 | function findDisplayEntityHTML ($message) { |
| 501 | if ($message->header->type0 == 'text' && |
| 502 | $message->header->type1 == 'html' && |
| 503 | isset($message->header->entity_id)) |
| 504 | return $message->header->entity_id; |
| 505 | for ($i = 0; isset($message->entities[$i]); $i ++) { |
| 506 | $entity = findDisplayEntityHTML($message->entities[$i]); |
| 507 | if ($entity != 0) |
| 508 | return $entity; |
| 509 | } |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | /** This returns a parsed string called $body. That string can then |
| 514 | be displayed as the actual message in the HTML. It contains |
| 515 | everything needed, including HTML Tags, Attachments at the |
| 516 | bottom, etc. |
| 517 | **/ |
| 518 | function formatBody($imap_stream, $message, $color, $wrap_at) { |
| 519 | // this if statement checks for the entity to show as the |
| 520 | // primary message. To add more of them, just put them in the |
| 521 | // order that is their priority. |
| 522 | global $startMessage, $username, $key, $imapServerAddress, $imapPort, |
| 523 | $show_html_default; |
| 524 | |
| 525 | $id = $message->header->id; |
| 526 | $urlmailbox = urlencode($message->header->mailbox); |
| 527 | |
| 528 | // Get the right entity and redefine message to be this entity |
| 529 | // Pass the 0 to mean that we want the 'best' viewable one |
| 530 | $ent_num = findDisplayEntity ($message, 0); |
| 531 | $body_message = getEntity($message, $ent_num); |
| 532 | if (($body_message->header->type0 == 'text') || |
| 533 | ($body_message->header->type0 == 'rfc822')) { |
| 534 | |
| 535 | $body = mime_fetch_body ($imap_stream, $id, $ent_num); |
| 536 | $body = decodeBody($body, $body_message->header->encoding); |
| 537 | $hookResults = do_hook("message_body", $body); |
| 538 | $body = $hookResults[1]; |
| 539 | |
| 540 | // If there are other types that shouldn't be formatted, add |
| 541 | // them here |
| 542 | if ($body_message->header->type1 != "html" || ! $show_html_default) { |
| 543 | translateText($body, $wrap_at, $body_message->header->charset); |
| 544 | } |
| 545 | |
| 546 | $body .= "<SMALL><CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$ent_num&mailbox=$urlmailbox&showHeaders=1\">". _("Download this as a file") ."</A></CENTER><BR></SMALL>"; |
| 547 | |
| 548 | /** Display the ATTACHMENTS: message if there's more than one part **/ |
| 549 | $body .= "</TD></TR></TABLE>"; |
| 550 | if (isset($message->entities[0])) { |
| 551 | $body .= formatAttachments ($message, $ent_num, $message->header->mailbox, $id); |
| 552 | } |
| 553 | $body .= "</TD></TR></TABLE>"; |
| 554 | } else { |
| 555 | $body = formatAttachments ($message, -1, $message->header->mailbox, $id); |
| 556 | } |
| 557 | return( $body ); |
| 558 | } |
| 559 | |
| 560 | // A recursive function that returns a list of attachments with links |
| 561 | // to where to download these attachments |
| 562 | function formatAttachments ($message, $ent_id, $mailbox, $id) { |
| 563 | global $where, $what; |
| 564 | global $startMessage, $color; |
| 565 | static $ShownHTML = 0; |
| 566 | |
| 567 | $body = ""; |
| 568 | if ($ShownHTML == 0) { |
| 569 | $ShownHTML = 1; |
| 570 | |
| 571 | $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n" . |
| 572 | "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n" . |
| 573 | _("Attachments") . ':' . |
| 574 | "</B></TH></TR><TR><TD>\n" . |
| 575 | "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n" . |
| 576 | formatAttachments ($message, $ent_id, $mailbox, $id) . |
| 577 | "</TABLE></TD></TR></TABLE>"; |
| 578 | |
| 579 | return( $body ); |
| 580 | } |
| 581 | |
| 582 | if ($message) { |
| 583 | if (!$message->entities) { |
| 584 | $type0 = strtolower($message->header->type0); |
| 585 | $type1 = strtolower($message->header->type1); |
| 586 | $name = decodeHeader($message->header->name); |
| 587 | |
| 588 | if ($message->header->entity_id != $ent_id) { |
| 589 | $filename = decodeHeader($message->header->filename); |
| 590 | if (trim($filename) == '') { |
| 591 | if (trim($name) == '') { |
| 592 | if( trim( $message->header->id ) == '' ) |
| 593 | $display_filename = 'untitled-[' . $message->header->entity_id . ']' ; |
| 594 | else |
| 595 | $display_filename = 'cid: ' . $message->header->id; |
| 596 | // $display_filename = 'untitled-[' . $message->header->entity_id . ']' ; |
| 597 | } else { |
| 598 | $display_filename = $name; |
| 599 | $filename = $name; |
| 600 | } |
| 601 | } else { |
| 602 | $display_filename = $filename; |
| 603 | } |
| 604 | |
| 605 | $urlMailbox = urlencode($mailbox); |
| 606 | $ent = urlencode($message->header->entity_id); |
| 607 | |
| 608 | $DefaultLink = |
| 609 | "../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent"; |
| 610 | if ($where && $what) |
| 611 | $DefaultLink .= '&where=' . urlencode($where) . '&what=' . urlencode($what); |
| 612 | $Links['download link']['text'] = _("download"); |
| 613 | $Links['download link']['href'] = |
| 614 | "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent"; |
| 615 | $ImageURL = ''; |
| 616 | |
| 617 | $HookResults = do_hook("attachment $type0/$type1", $Links, |
| 618 | $startMessage, $id, $urlMailbox, $ent, $DefaultLink, |
| 619 | $display_filename, $where, $what); |
| 620 | |
| 621 | $Links = $HookResults[1]; |
| 622 | $DefaultLink = $HookResults[6]; |
| 623 | |
| 624 | $body .= '<TR><TD> </TD><TD>' . |
| 625 | "<A HREF=\"$DefaultLink\">$display_filename</A> </TD>" . |
| 626 | '<TD><SMALL><b>' . show_readable_size($message->header->size) . |
| 627 | '</b> </small></TD>' . |
| 628 | "<TD><SMALL>[ $type0/$type1 ] </SMALL></TD>" . |
| 629 | '<TD><SMALL>'; |
| 630 | if ($message->header->description) |
| 631 | $body .= '<b>' . htmlspecialchars($message->header->description) . '</b>'; |
| 632 | $body .= '</SMALL></TD><TD><SMALL> '; |
| 633 | |
| 634 | |
| 635 | $SkipSpaces = 1; |
| 636 | foreach ($Links as $Val) { |
| 637 | if ($SkipSpaces) { |
| 638 | $SkipSpaces = 0; |
| 639 | } else { |
| 640 | $body .= ' | '; |
| 641 | } |
| 642 | $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>'; |
| 643 | } |
| 644 | |
| 645 | unset($Links); |
| 646 | |
| 647 | $body .= "</SMALL></TD></TR>\n"; |
| 648 | } |
| 649 | } else { |
| 650 | for ($i = 0; $i < count($message->entities); $i++) { |
| 651 | $body .= formatAttachments ($message->entities[$i], $ent_id, $mailbox, $id); |
| 652 | } |
| 653 | } |
| 654 | return( $body ); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | |
| 659 | /** this function decodes the body depending on the encoding type. **/ |
| 660 | function decodeBody($body, $encoding) { |
| 661 | $body = str_replace("\r\n", "\n", $body); |
| 662 | $encoding = strtolower($encoding); |
| 663 | |
| 664 | global $show_html_default; |
| 665 | |
| 666 | if ($encoding == 'quoted-printable') { |
| 667 | $body = quoted_printable_decode($body); |
| 668 | |
| 669 | |
| 670 | /* |
| 671 | Following code has been comented as I see no reason for it. |
| 672 | If there is any please tell me a mingo@rotedic.com |
| 673 | |
| 674 | while (ereg("=\n", $body)) |
| 675 | $body = ereg_replace ("=\n", "", $body); |
| 676 | */ |
| 677 | } else if ($encoding == 'base64') { |
| 678 | $body = base64_decode($body); |
| 679 | } |
| 680 | |
| 681 | // All other encodings are returned raw. |
| 682 | return $body; |
| 683 | } |
| 684 | |
| 685 | |
| 686 | // This functions decode strings that is encoded according to |
| 687 | // RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text). |
| 688 | function decodeHeader ($string) { |
| 689 | if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=', |
| 690 | $string, $res)) { |
| 691 | if (ucfirst($res[2]) == "B") { |
| 692 | $replace = base64_decode($res[3]); |
| 693 | } else { |
| 694 | $replace = ereg_replace("_", " ", $res[3]); |
| 695 | // Convert lowercase Quoted Printable to uppercase for |
| 696 | // quoted_printable_decode to understand it. |
| 697 | while (ereg("(=(([0-9][abcdef])|([abcdef][0-9])|([abcdef][abcdef])))", $replace, $res)) { |
| 698 | $replace = str_replace($res[1], strtoupper($res[1]), $replace); |
| 699 | } |
| 700 | $replace = quoted_printable_decode($replace); |
| 701 | } |
| 702 | |
| 703 | $replace = charset_decode ($res[1], $replace); |
| 704 | |
| 705 | // Remove the name of the character set. |
| 706 | $string = eregi_replace ('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=', |
| 707 | $replace, $string); |
| 708 | |
| 709 | // In case there should be more encoding in the string: recurse |
| 710 | return (decodeHeader($string)); |
| 711 | } else |
| 712 | return ($string); |
| 713 | } |
| 714 | |
| 715 | // Encode a string according to RFC 1522 for use in headers if it |
| 716 | // contains 8-bit characters or anything that looks like it should |
| 717 | // be encoded. |
| 718 | function encodeHeader ($string) { |
| 719 | global $default_charset; |
| 720 | |
| 721 | // Encode only if the string contains 8-bit characters or =? |
| 722 | $j = strlen( $string ); |
| 723 | $l = FALSE; // Must be encoded ? |
| 724 | $ret = ''; |
| 725 | for( $i=0; $i < $j; ++$i) { |
| 726 | switch( $string{$i} ) { |
| 727 | case '=': |
| 728 | $ret .= '=3D'; |
| 729 | break; |
| 730 | case '?': |
| 731 | $l = TRUE; |
| 732 | $ret .= '=3F'; |
| 733 | break; |
| 734 | case '_': |
| 735 | $ret .= '=5F'; |
| 736 | break; |
| 737 | case ' ': |
| 738 | $ret .= '_'; |
| 739 | break; |
| 740 | default: |
| 741 | $k = ord( $string{$i} ); |
| 742 | if( $k > 126 ) { |
| 743 | $ret .= sprintf("=%02X", $k); |
| 744 | $l = TRUE; |
| 745 | } else |
| 746 | $ret .= $string{$i}; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | if( $l ) |
| 751 | $string = "=?$default_charset?Q?$ret?="; |
| 752 | |
| 753 | return( $string ); |
| 754 | } |
| 755 | |
| 756 | ?> |