| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * mime.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2003 The SquirrelMail Project Team |
| 7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | * |
| 9 | * This contains the functions necessary to detect and decode MIME |
| 10 | * messages. |
| 11 | * |
| 12 | * $Id$ |
| 13 | */ |
| 14 | |
| 15 | require_once(SM_PATH . 'functions/imap.php'); |
| 16 | require_once(SM_PATH . 'functions/attachment_common.php'); |
| 17 | |
| 18 | /* --------------------------------------------------------------------------------- */ |
| 19 | /* MIME DECODING */ |
| 20 | /* --------------------------------------------------------------------------------- */ |
| 21 | |
| 22 | /* This function gets the structure of a message and stores it in the "message" class. |
| 23 | * It will return this object for use with all relevant header information and |
| 24 | * fully parsed into the standard "message" object format. |
| 25 | */ |
| 26 | |
| 27 | function mime_structure ($bodystructure, $flags=array()) { |
| 28 | |
| 29 | /* Isolate the body structure and remove beginning and end parenthesis. */ |
| 30 | $read = trim(substr ($bodystructure, strpos(strtolower($bodystructure), 'bodystructure') + 13)); |
| 31 | $read = trim(substr ($read, 0, -1)); |
| 32 | $i = 0; |
| 33 | $msg = Message::parseStructure($read,$i); |
| 34 | if (!is_object($msg)) { |
| 35 | include_once(SM_PATH . 'functions/display_messages.php'); |
| 36 | global $color, $mailbox; |
| 37 | displayPageHeader( $color, urldecode($mailbox) ); |
| 38 | echo "<BODY TEXT=\"$color[8]\" BGCOLOR=\"$color[4]\" LINK=\"$color[7]\" VLINK=\"$color[7]\" ALINK=\"$color[7]\">\n\n" . |
| 39 | '<CENTER>'; |
| 40 | $errormessage = _("SquirrelMail could not decode the bodystructure of the message"); |
| 41 | $errormessage .= '<BR>'._("the provided bodystructure by your imap-server").':<BR><BR>'; |
| 42 | $errormessage .= '<table><tr><td>' . htmlspecialchars($read) . '</td></tr></table>'; |
| 43 | plain_error_message( $errormessage, $color ); |
| 44 | echo '</body></html>'; |
| 45 | exit; |
| 46 | } |
| 47 | $msg->setEnt('0'); |
| 48 | if (count($flags)) { |
| 49 | foreach ($flags as $flag) { |
| 50 | $char = strtoupper($flag{1}); |
| 51 | switch ($char) { |
| 52 | case 'S': |
| 53 | if (strtolower($flag) == '\\seen') { |
| 54 | $msg->is_seen = true; |
| 55 | } |
| 56 | break; |
| 57 | case 'A': |
| 58 | if (strtolower($flag) == '\\answered') { |
| 59 | $msg->is_answered = true; |
| 60 | } |
| 61 | break; |
| 62 | case 'D': |
| 63 | if (strtolower($flag) == '\\deleted') { |
| 64 | $msg->is_deleted = true; |
| 65 | } |
| 66 | break; |
| 67 | case 'F': |
| 68 | if (strtolower($flag) == '\\flagged') { |
| 69 | $msg->is_flagged = true; |
| 70 | } |
| 71 | break; |
| 72 | case 'M': |
| 73 | if (strtolower($flag) == '$mdnsent') { |
| 74 | $msg->is_mdnsent = true; |
| 75 | } |
| 76 | break; |
| 77 | default: |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | // listEntities($msg); |
| 83 | return $msg; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | |
| 88 | /* This starts the parsing of a particular structure. It is called recursively, |
| 89 | * so it can be passed different structures. It returns an object of type |
| 90 | * $message. |
| 91 | * First, it checks to see if it is a multipart message. If it is, then it |
| 92 | * handles that as it sees is necessary. If it is just a regular entity, |
| 93 | * then it parses it and adds the necessary header information (by calling out |
| 94 | * to mime_get_elements() |
| 95 | */ |
| 96 | |
| 97 | function mime_fetch_body($imap_stream, $id, $ent_id) { |
| 98 | global $uid_support; |
| 99 | /* Do a bit of error correction. If we couldn't find the entity id, just guess |
| 100 | * that it is the first one. That is usually the case anyway. |
| 101 | */ |
| 102 | if (!$ent_id) { |
| 103 | $ent_id = 1; |
| 104 | } |
| 105 | $cmd = "FETCH $id BODY[$ent_id]"; |
| 106 | |
| 107 | $data = sqimap_run_command ($imap_stream, $cmd, true, $response, $message, $uid_support); |
| 108 | do { |
| 109 | $topline = trim(array_shift($data)); |
| 110 | } while($topline && ($topline[0] == '*') && !preg_match('/\* [0-9]+ FETCH.*/i', $topline)) ; |
| 111 | |
| 112 | $wholemessage = implode('', $data); |
| 113 | if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) { |
| 114 | $ret = substr($wholemessage, 0, $regs[1]); |
| 115 | /* There is some information in the content info header that could be important |
| 116 | * in order to parse html messages. Let's get them here. |
| 117 | */ |
| 118 | if ($ret{0} == '<') { |
| 119 | $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message, $uid_support); |
| 120 | } |
| 121 | } else if (ereg('"([^"]*)"', $topline, $regs)) { |
| 122 | $ret = $regs[1]; |
| 123 | } else { |
| 124 | global $where, $what, $mailbox, $passed_id, $startMessage; |
| 125 | $par = 'mailbox=' . urlencode($mailbox) . '&passed_id=' . $passed_id; |
| 126 | if (isset($where) && isset($what)) { |
| 127 | $par .= '&where=' . urlencode($where) . '&what=' . urlencode($what); |
| 128 | } else { |
| 129 | $par .= '&startMessage=' . $startMessage . '&show_more=0'; |
| 130 | } |
| 131 | $par .= '&response=' . urlencode($response) . |
| 132 | '&message=' . urlencode($message) . |
| 133 | '&topline=' . urlencode($topline); |
| 134 | |
| 135 | echo '<tt><br>' . |
| 136 | '<table width="80%"><tr>' . |
| 137 | '<tr><td colspan=2>' . |
| 138 | _("Body retrieval error. The reason for this is most probably that the message is malformed.") . |
| 139 | '</td></tr>' . |
| 140 | '<tr><td><b>' . _("Command:") . "</td><td>$cmd</td></tr>" . |
| 141 | '<tr><td><b>' . _("Response:") . "</td><td>$response</td></tr>" . |
| 142 | '<tr><td><b>' . _("Message:") . "</td><td>$message</td></tr>" . |
| 143 | '<tr><td><b>' . _("FETCH line:") . "</td><td>$topline</td></tr>" . |
| 144 | "</table><BR></tt></font><hr>"; |
| 145 | |
| 146 | $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message, $uid_support); |
| 147 | array_shift($data); |
| 148 | $wholemessage = implode('', $data); |
| 149 | |
| 150 | $ret = $wholemessage; |
| 151 | } |
| 152 | return $ret; |
| 153 | } |
| 154 | |
| 155 | function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) { |
| 156 | global $uid_support; |
| 157 | /* Do a bit of error correction. If we couldn't find the entity id, just guess |
| 158 | * that it is the first one. That is usually the case anyway. |
| 159 | */ |
| 160 | if (!$ent_id) { |
| 161 | $ent_id = 1; |
| 162 | } |
| 163 | $sid = sqimap_session_id($uid_support); |
| 164 | /* Don't kill the connection if the browser is over a dialup |
| 165 | * and it would take over 30 seconds to download it. |
| 166 |