Added patch from seth randall to fix address group behaviour.
[squirrelmail.git] / functions / mime.php
CommitLineData
59177427 1<?php
2ba13803 2
35586184 3/**
4 * mime.php
5 *
76911253 6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
35586184 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 */
b74ba498 14
b68edc75 15require_once(SM_PATH . 'functions/imap.php');
16require_once(SM_PATH . 'functions/attachment_common.php');
8beafbbc 17
7c7b74b3 18/* -------------------------------------------------------------------------- */
19/* MIME DECODING */
20/* -------------------------------------------------------------------------- */
b74ba498 21
451f74a2 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 */
77b88425 26
a4a70693 27function mime_structure ($bodystructure, $flags=array()) {
c9d78ab4 28
3d8371be 29 /* Isolate the body structure and remove beginning and end parenthesis. */
a4a70693 30 $read = trim(substr ($bodystructure, strpos(strtolower($bodystructure), 'bodystructure') + 13));
451f74a2 31 $read = trim(substr ($read, 0, -1));
22efa9fb 32 $i = 0;
33 $msg = Message::parseStructure($read,$i);
9de42168 34 if (!is_object($msg)) {
bd9c880b 35 include_once(SM_PATH . 'functions/display_messages.php');
3d8371be 36 global $color, $mailbox;
c96c32f4 37 /* removed urldecode because $_GET is auto urldecoded ??? */
a48eba8f 38 displayPageHeader( $color, $mailbox );
9de42168 39 echo "<BODY TEXT=\"$color[8]\" BGCOLOR=\"$color[4]\" LINK=\"$color[7]\" VLINK=\"$color[7]\" ALINK=\"$color[7]\">\n\n" .
40 '<CENTER>';
5e8de8b6 41 $errormessage = _("SquirrelMail could not decode the bodystructure of the message");
3d8371be 42 $errormessage .= '<BR>'._("the provided bodystructure by your imap-server").':<BR><BR>';
43 $errormessage .= '<table><tr><td>' . htmlspecialchars($read) . '</td></tr></table>';
9de42168 44 plain_error_message( $errormessage, $color );
3d8371be 45 echo '</body></html>';
9de42168 46 exit;
47 }
a4a70693 48 if (count($flags)) {
7a9e9c89 49 foreach ($flags as $flag) {
50 $char = strtoupper($flag{1});
51 switch ($char) {
3d8371be 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;
7a9e9c89 79 }
80 }
451f74a2 81 }
7a9e9c89 82 // listEntities($msg);
3d8371be 83 return $msg;
451f74a2 84}
b74ba498 85
22efa9fb 86
87
3d8371be 88/* This starts the parsing of a particular structure. It is called recursively,
451f74a2 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 */
451f74a2 96
1035e159 97function mime_fetch_body($imap_stream, $id, $ent_id=1) {
a4a70693 98 global $uid_support;
3d8371be 99 /* Do a bit of error correction. If we couldn't find the entity id, just guess
09a4bde3 100 * that it is the first one. That is usually the case anyway.
101 */
7c7b74b3 102
09a4bde3 103 if (!$ent_id) {
08b7f7cc 104 $cmd = "FETCH $id BODY[]";
1035e159 105 } else {
08b7f7cc 106 $cmd = "FETCH $id BODY[$ent_id]";
09a4bde3 107 }
3d8371be 108
a4a70693 109 $data = sqimap_run_command ($imap_stream, $cmd, true, $response, $message, $uid_support);
77b88425 110 do {
3d8371be 111 $topline = trim(array_shift($data));
112 } while($topline && ($topline[0] == '*') && !preg_match('/\* [0-9]+ FETCH.*/i', $topline)) ;
a4a70693 113
451f74a2 114 $wholemessage = implode('', $data);
115 if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
3d8371be 116 $ret = substr($wholemessage, 0, $regs[1]);
117 /* There is some information in the content info header that could be important
118 * in order to parse html messages. Let's get them here.
119 */
0600bdf1 120// if ($ret{0} == '<') {
121// $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message, $uid_support);
122// }
451f74a2 123 } else if (ereg('"([^"]*)"', $topline, $regs)) {
124 $ret = $regs[1];
125 } else {
126 global $where, $what, $mailbox, $passed_id, $startMessage;
3d8371be 127 $par = 'mailbox=' . urlencode($mailbox) . '&amp;passed_id=' . $passed_id;
451f74a2 128 if (isset($where) && isset($what)) {
3d8371be 129 $par .= '&amp;where=' . urlencode($where) . '&amp;what=' . urlencode($what);
a3daaaf3 130 } else {
3d8371be 131 $par .= '&amp;startMessage=' . $startMessage . '&amp;show_more=0';
451f74a2 132 }
e5ea9327 133 $par .= '&amp;response=' . urlencode($response) .
3d8371be 134 '&amp;message=' . urlencode($message) .
135 '&amp;topline=' . urlencode($topline);
a019eeb8 136
346817d4 137 echo '<tt><br>' .
138 '<table width="80%"><tr>' .
139 '<tr><td colspan=2>' .
0e5b61b4 140 _("Body retrieval error. The reason for this is most probably that the message is malformed.") .
346817d4 141 '</td></tr>' .
0e5b61b4 142 '<tr><td><b>' . _("Command:") . "</td><td>$cmd</td></tr>" .
143 '<tr><td><b>' . _("Response:") . "</td><td>$response</td></tr>" .
144 '<tr><td><b>' . _("Message:") . "</td><td>$message</td></tr>" .
145 '<tr><td><b>' . _("FETCH line:") . "</td><td>$topline</td></tr>" .
346817d4 146 "</table><BR></tt></font><hr>";
147
a4a70693 148 $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message, $uid_support);
451f74a2 149 array_shift($data);
150 $wholemessage = implode('', $data);
a019eeb8 151
346817d4 152 $ret = $wholemessage;
a3daaaf3 153 }
3d8371be 154 return $ret;
451f74a2 155}
d4467150 156
1035e159 157function mime_print_body_lines ($imap_stream, $id, $ent_id=1, $encoding) {
a4a70693 158 global $uid_support;
1035e159 159
3d8371be 160 /* Don't kill the connection if the browser is over a dialup
161 * and it would take over 30 seconds to download it.
162