| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * mime.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2002 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('../functions/imap.php'); |
| 16 | require_once('../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 | $msg = &new message(); |
| 32 | $read = trim(substr ($read, 0, -1)); |
| 33 | $msg = $msg->parseStructure($read,0); |
| 34 | $msg->setEnt('0'); |
| 35 | if (count($flags)) { |
| 36 | foreach ($flags as $flag) { |
| 37 | $char = strtoupper($flag{1}); |
| 38 | switch ($char) { |
| 39 | case 'S': |
| 40 | if (strtolower($flag) == '\\seen') { |
| 41 | $msg->is_seen = true; |
| 42 | } |
| 43 | break; |
| 44 | case 'A': |
| 45 | if (strtolower($flag) == '\\answered') { |
| 46 | $msg->is_answered = true; |
| 47 | } |
| 48 | break; |
| 49 | case 'D': |
| 50 | if (strtolower($flag) == '\\deleted') { |
| 51 | $msg->is_deleted = true; |
| 52 | } |
| 53 | break; |
| 54 | case 'F': |
| 55 | if (strtolower($flag) == '\\flagged') { |
| 56 | $msg->is_flagged = true; |
| 57 | } |
| 58 | break; |
| 59 | case 'M': |
| 60 | if (strtolower($flag) == '$mdnsent') { |
| 61 | $msg->is_mdnsent = true; |
| 62 | } |
| 63 | break; |
| 64 | default: |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | // listEntities($msg); |
| 70 | return( $msg ); |
| 71 | } |
| 72 | |
| 73 | /* this starts the parsing of a particular structure. It is called recursively, |
| 74 | * so it can be passed different structures. It returns an object of type |
| 75 | * $message. |
| 76 | * First, it checks to see if it is a multipart message. If it is, then it |
| 77 | * handles that as it sees is necessary. If it is just a regular entity, |
| 78 | * then it parses it and adds the necessary header information (by calling out |
| 79 | * to mime_get_elements() |
| 80 | */ |
| 81 | |
| 82 | function mime_fetch_body($imap_stream, $id, $ent_id) { |
| 83 | global $uid_support; |
| 84 | /* |
| 85 | * do a bit of error correction. If we couldn't find the entity id, just guess |
| 86 | * that it is the first one. That is usually the case anyway. |
| 87 | */ |
| 88 | if (!$ent_id) { |
| 89 | $ent_id = 1; |
| 90 | } |
| 91 | $cmd = "FETCH $id BODY[$ent_id]"; |
| 92 | |
| 93 | $data = sqimap_run_command ($imap_stream, $cmd, true, $response, $message, $uid_support); |
| 94 | do { |
| 95 | $topline = trim(array_shift( $data )); |
| 96 | } while( $topline && $topline[0] == '*' && !preg_match( '/\* [0-9]+ FETCH.*/i', $topline )) ; |
| 97 | |
| 98 | $wholemessage = implode('', $data); |
| 99 | if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) { |
| 100 | |
| 101 | $ret = substr( $wholemessage, 0, $regs[1] ); |
| 102 | /* |
| 103 | There is some information in the content info header that could be important |
| 104 | in order to parse html messages. Let's get them here. |
| 105 | */ |
| 106 | if ( $ret{0} == '<' ) { |
| 107 | $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message, $uid_support); |
| 108 | } |
| 109 | } else if (ereg('"([^"]*)"', $topline, $regs)) { |
| 110 | $ret = $regs[1]; |
| 111 | } else { |
| 112 | global $where, $what, $mailbox, $passed_id, $startMessage; |
| 113 | $par = 'mailbox=' . urlencode($mailbox) . "&passed_id=$passed_id"; |
| 114 | if (isset($where) && isset($what)) { |
| 115 | $par .= '&where='. urlencode($where) . "&what=" . urlencode($what); |
| 116 | } else { |
| 117 | $par .= "&startMessage=$startMessage&show_more=0"; |
| 118 | } |
| 119 | $par .= '&response=' . urlencode($response) . |
| 120 | '&message=' . urlencode($message). |
| 121 | '&topline=' . urlencode($topline); |
| 122 | |
| 123 | echo '<tt><br>' . |
| 124 | '<table width="80%"><tr>' . |
| 125 | '<tr><td colspan=2>' . |
| 126 | _("Body retrieval error. The reason for this is most probably that the message is malformed. Please help us making future versions better by submitting this message to the developers knowledgebase!") . |
| 127 | " <A HREF=\"../src/retrievalerror.php?$par\"><br>" . |
| 128 | _("Submit message") . '</A><BR> ' . |
| 129 | '</td></tr>' . |
| 130 | '<td><b>' . _("Command:") . "</td><td>$cmd</td></tr>" . |
| 131 | '<td><b>' . _("Response:") . "</td><td>$response</td></tr>" . |
| 132 | '<td><b>' . _("Message:") . "</td><td>$message</td></tr>" . |
| 133 | '<td><b>' . _("FETCH line:") . "</td><td>$topline</td></tr>" . |
| 134 | "</table><BR></tt></font><hr>"; |
| 135 | |
| 136 | $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message, $uid_support); |
| 137 | array_shift($data); |
| 138 | $wholemessage = implode('', $data); |
| 139 | |
| 140 | $ret = $wholemessage; |
| 141 | } |
| 142 | return( $ret ); |
| 143 | } |
| 144 | |
| 145 | function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) { |
| 146 | global $uid_support; |
| 147 | // do a bit of error correction. If we couldn't find the entity id, just guess |
| 148 | // that it is the first one. That is usually the case anyway. |
| 149 | if (!$ent_id) { |
| 150 | $ent_id = 1; |
| 151 | } |
| 152 | $sid = sqimap_session_id($uid_support); |
| 153 | // Don't kill the connection if the browser is over a dialup |
| 154 | // and it would take over 30 seconds to download it. |
| 155 | |
| 156 |