Added error message in case the bodystructure couldn't processed.
[squirrelmail.git] / functions / mime.php
CommitLineData
59177427 1<?php
2ba13803 2
35586184 3/**
4 * mime.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 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
35586184 15require_once('../functions/imap.php');
16require_once('../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
a4a70693 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();
451f74a2 32 $read = trim(substr ($read, 0, -1));
bb822beb 33 $res = $msg->parseStructure($read);
34 $msg = $res[0];
9de42168 35 if (!is_object($msg)) {
36 include_once( '../functions/display_messages.php' );
37 global $color, $mailbox;
38 displayPageHeader( $color, urldecode($mailbox) );
39 echo "<BODY TEXT=\"$color[8]\" BGCOLOR=\"$color[4]\" LINK=\"$color[7]\" VLINK=\"$color[7]\" ALINK=\"$color[7]\">\n\n" .
40 '<CENTER>';
41 $errormessage = _("Squirrelmail could not decode the bodystructure of the message");
42 $errormessage .= '<BR>'._("the provided bodystructure by your imap-server").':<BR><BR>';
43 $errormessage .= '<table><tr><td>'.$read.'</td></tr></table>';
44 plain_error_message( $errormessage, $color );
45 echo '</body></html>';
46 exit;
47 }
a4a70693 48 $msg->setEnt('0');
49 if (count($flags)) {
7a9e9c89 50 foreach ($flags as $flag) {
51 $char = strtoupper($flag{1});
52 switch ($char) {
53 case 'S':
54 if (strtolower($flag) == '\\seen') {
55 $msg->is_seen = true;
56 }
57 break;
58 case 'A':
59 if (strtolower($flag) == '\\answered') {
60 $msg->is_answered = true;
61 }
62 break;
63 case 'D':
64 if (strtolower($flag) == '\\deleted') {
65 $msg->is_deleted = true;
66 }
67 break;
68 case 'F':
69 if (strtolower($flag) == '\\flagged') {
70 $msg->is_flagged = true;
71 }
72 break;
73 case 'M':
74 if (strtolower($flag) == '$mdnsent') {
75 $msg->is_mdnsent = true;
76 }
77 break;
78 default:
79 break;
80 }
81 }
451f74a2 82 }
7a9e9c89 83 // listEntities($msg);
451f74a2 84 return( $msg );
85}
b74ba498 86
451f74a2 87/* this starts the parsing of a particular structure. It is called recursively,
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
93f92f03 96function mime_fetch_body($imap_stream, $id, $ent_id) {
a4a70693 97 global $uid_support;
09a4bde3 98 /*
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) {
451f74a2 103 $ent_id = 1;
09a4bde3 104 }
6ab1bd9e 105 $cmd = "FETCH $id BODY[$ent_id]";
a4a70693 106
107 $data = sqimap_run_command ($imap_stream, $cmd, true, $response, $message, $uid_support);
77b88425 108 do {
e976319c 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)) {
77b88425 114
451f74a2 115 $ret = substr( $wholemessage, 0, $regs[1] );
116 /*
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 */
120 if ( $ret{0} == '<' ) {
a4a70693 121 $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message, $uid_support);
451f74a2 122 }
123 } else if (ereg('"([^"]*)"', $topline, $regs)) {
124 $ret = $regs[1];
125 } else {
126 global $where, $what, $mailbox, $passed_id, $startMessage;
e5ea9327 127 $par = 'mailbox=' . urlencode($mailbox) . "&amp;passed_id=$passed_id";
451f74a2 128 if (isset($where) && isset($what)) {
e5ea9327 129 $par .= '&amp;where='. urlencode($where) . "&amp;what=" . urlencode($what);
a3daaaf3 130 } else {
e5ea9327 131 $par .= "&amp;startMessage=$startMessage&amp;show_more=0";
451f74a2 132 }
e5ea9327 133 $par .= '&amp;response=' . urlencode($response) .
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>' .
451f74a2 140 _("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!") .
346817d4 141 " <A HREF=\"../src/retrievalerror.php?$par\"><br>" .
142 _("Submit message") . '</A><BR>&nbsp;' .
143 '</td></tr>' .
144 '<td><b>' . _("Command:") . "</td><td>$cmd</td></tr>" .
145 '<td><b>' . _("Response:") . "</td><td>$response</td></tr>" .
146 '<td><b>' . _("Message:") . "</td><td>$message</td></tr>" .
147 '<td><b>' . _("FETCH line:") . "</td><td>$topline</td></tr>" .
148 "</table><BR></tt></font><hr>";
149
a4a70693 150 $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message, $uid_support);
451f74a2 151 array_shift($data);
152 $wholemessage = implode('', $data);
a019eeb8 153
346817d4 154 $ret = $wholemessage;
a3daaaf3 155 }
451f74a2 156 return( $ret );
157}
d4467150 158
451f74a2 159function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
a4a70693 160 global $uid_support;
451f74a2 161 // do a bit of error correction. If we couldn't find the entity id, just guess
162 // that it is the first one. That is usually the case anyway.
163 if (!$ent_id) {
164 $ent_id = 1;
165 }
a4a70693 166 $sid = sqimap_session_id($uid_support);
451f74a2 167 // Don't kill the connection if the browser is over a dialup
168 // and it would take over 30 seconds to download it.
b7206e1d 169
85015544 170