If SERVER_NAME not set, use $domain as the domain in message-id.
[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
451f74a2 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;
9de42168 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>';
5e8de8b6 40 $errormessage = _("SquirrelMail could not decode the bodystructure of the message");
3d8371be 41 $errormessage .= '<BR>'._("the provided bodystructure by your imap-server").':<BR><BR>';
42 $errormessage .= '<table><tr><td>' . htmlspecialchars($read) . '</td></tr></table>';
9de42168 43 plain_error_message( $errormessage, $color );
3d8371be 44 echo '</body></html>';
9de42168 45 exit;
46 }
a4a70693 47 if (count($flags)) {
7a9e9c89 48 foreach ($flags as $flag) {
49 $char = strtoupper($flag{1});
50 switch ($char) {
3d8371be 51 case 'S':
52 if (strtolower($flag) == '\\seen') {
53 $msg->is_seen = true;
54 }
55 break;
56 case 'A':
57 if (strtolower($flag) == '\\answered') {
58 $msg->is_answered = true;
59 }
60 break;
61 case 'D':
62 if (strtolower($flag) == '\\deleted') {
63 $msg->is_deleted = true;
64 }
65 break;
66 case 'F':
67 if (strtolower($flag) == '\\flagged') {
68 $msg->is_flagged = true;
69 }
70 break;
71 case 'M':
72 if (strtolower($flag) == '$mdnsent') {
73 $msg->is_mdnsent = true;
74 }
75 break;
76 default:
77 break;
7a9e9c89 78 }
79 }
451f74a2 80 }
7a9e9c89 81 // listEntities($msg);
3d8371be 82 return $msg;
451f74a2 83}
b74ba498 84
22efa9fb 85
86
3d8371be 87/* This starts the parsing of a particular structure. It is called recursively,
451f74a2 88 * so it can be passed different structures. It returns an object of type
89 * $message.
90 * First, it checks to see if it is a multipart message. If it is, then it
91 * handles that as it sees is necessary. If it is just a regular entity,
92 * then it parses it and adds the necessary header information (by calling out
93 * to mime_get_elements()
94 */
451f74a2 95
1035e159 96function mime_fetch_body($imap_stream, $id, $ent_id=1) {
a4a70693 97 global $uid_support;
3d8371be 98 /* Do a bit of error correction. If we couldn't find the entity id, just guess
09a4bde3 99 * that it is the first one. That is usually the case anyway.
100 */
101 if (!$ent_id) {
1035e159 102 $cmd = "FETCH $id BODY[]";
103 } else {
104 $cmd = "FETCH $id BODY[$ent_id]";
09a4bde3 105 }
3d8371be 106
a4a70693 107 $data = sqimap_run_command ($imap_stream, $cmd, true, $response, $message, $uid_support);
77b88425 108 do {
3d8371be 109 $topline = trim(array_shift($data));
110 } while($topline && ($topline[0] == '*') && !preg_match('/\* [0-9]+ FETCH.*/i', $topline)) ;
a4a70693 111
451f74a2 112 $wholemessage = implode('', $data);
113 if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
3d8371be 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} == '<') {
a4a70693 119 $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message, $uid_support);
451f74a2 120 }
121 } else if (ereg('"([^"]*)"', $topline, $regs)) {
122 $ret = $regs[1];
123 } else {
124 global $where, $what, $mailbox, $passed_id, $startMessage;
3d8371be 125 $par = 'mailbox=' . urlencode($mailbox) . '&amp;passed_id=' . $passed_id;
451f74a2 126 if (isset($where) && isset($what)) {
3d8371be 127 $par .= '&amp;where=' . urlencode($where) . '&amp;what=' . urlencode($what);
a3daaaf3 128 } else {
3d8371be 129 $par .= '&amp;startMessage=' . $startMessage . '&amp;show_more=0';
451f74a2 130 }
e5ea9327 131 $par .= '&amp;response=' . urlencode($response) .
3d8371be 132 '&amp;message=' . urlencode($message) .
133 '&amp;topline=' . urlencode($topline);
a019eeb8 134
346817d4 135 echo '<tt><br>' .
136 '<table width="80%"><tr>' .
137 '<tr><td colspan=2>' .
0e5b61b4 138 _("Body retrieval error. The reason for this is most probably that the message is malformed.") .
346817d4 139 '</td></tr>' .
0e5b61b4 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>" .
346817d4 144 "</table><BR></tt></font><hr>";
145
a4a70693 146 $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message, $uid_support);
451f74a2 147 array_shift($data);
148 $wholemessage = implode('', $data);
a019eeb8 149
346817d4 150 $ret = $wholemessage;
a3daaaf3 151 }
3d8371be 152 return $ret;
451f74a2 153}
d4467150 154
1035e159 155function mime_print_body_lines ($imap_stream, $id, $ent_id=1, $encoding) {
a4a70693 156 global $uid_support;
1035e159 157
a4a70693 158 $sid = sqimap_session_id($uid_support);
3d8371be 159 /* Don't kill the connection if the browser is over a dialup
160 * and it would take over 30 seconds to download it.
161