Fix E_ALL notice, thanks Alexandros.
[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;
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 */
102 if (!$ent_id) {
08b7f7cc 103 $cmd = "FETCH $id BODY[]";
1035e159 104 } else {
08b7f7cc 105 $cmd = "FETCH $id BODY[$ent_id]";
09a4bde3 106 }
3d8371be 107
a4a70693 108 $data = sqimap_run_command ($imap_stream, $cmd, true, $response, $message, $uid_support);
77b88425 109 do {
3d8371be 110 $topline = trim(array_shift($data));
111 } while($topline && ($topline[0] == '*') && !preg_match('/\* [0-9]+ FETCH.*/i', $topline)) ;
a4a70693 112
451f74a2 113 $wholemessage = implode('', $data);
114 if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
3d8371be 115 $ret = substr($wholemessage, 0, $regs[1]);
116 /* There is some information in the content info header that could be important
117 * in order to parse html messages. Let's get them here.
118 */
0600bdf1 119// if ($ret{0} == '<') {
120// $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message, $uid_support);
121// }
451f74a2 122 } else if (ereg('"([^"]*)"', $topline, $regs)) {
123 $ret = $regs[1];
124 } else {
125 global $where, $what, $mailbox, $passed_id, $startMessage;
3d8371be 126 $par = 'mailbox=' . urlencode($mailbox) . '&amp;passed_id=' . $passed_id;
451f74a2 127 if (isset($where) && isset($what)) {
3d8371be 128 $par .= '&amp;where=' . urlencode($where) . '&amp;what=' . urlencode($what);
a3daaaf3 129 } else {
3d8371be 130 $par .= '&amp;startMessage=' . $startMessage . '&amp;show_more=0';
451f74a2 131 }
e5ea9327 132 $par .= '&amp;response=' . urlencode($response) .
3d8371be 133 '&amp;message=' . urlencode($message) .
134 '&amp;topline=' . urlencode($topline);
a019eeb8 135
346817d4 136 echo '<tt><br>' .
137 '<table width="80%"><tr>' .
138 '<tr><td colspan=2>' .
0e5b61b4 139 _("Body retrieval error. The reason for this is most probably that the message is malformed.") .
346817d4 140 '</td></tr>' .
0e5b61b4 141 '<tr><td><b>' . _("Command:") . "</td><td>$cmd</td></tr>" .
142 '<tr><td><b>' . _("Response:") . "</td><td>$response</td></tr>" .
143 '<tr><td><b>' . _("Message:") . "</td><td>$message</td></tr>" .
144 '<tr><td><b>' . _("FETCH line:") . "</td><td>$topline</td></tr>" .
346817d4 145 "</table><BR></tt></font><hr>";
146
a4a70693 147 $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message, $uid_support);
451f74a2 148 array_shift($data);
149 $wholemessage = implode('', $data);
a019eeb8 150
346817d4 151 $ret = $wholemessage;
a3daaaf3 152 }
3d8371be 153 return $ret;
451f74a2 154}
d4467150 155
1035e159 156function mime_print_body_lines ($imap_stream, $id, $ent_id=1, $encoding) {
a4a70693 157 global $uid_support;
1035e159 158
a4a70693 159 $sid = sqimap_session_id($uid_support);
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