Tomas Kuliavas
[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
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));
3d8371be 32 $msg =& new Message();
33 $res = $msg->parseStructure($read);
34 $msg = $res[0];
9de42168 35 if (!is_object($msg)) {
bd9c880b 36 include_once(SM_PATH . 'functions/display_messages.php');
3d8371be 37 global $color, $mailbox;
9de42168 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>';
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 $msg->setEnt('0');
49 if (count($flags)) {
7a9e9c89 50 foreach ($flags as $flag) {
51 $char = strtoupper($flag{1});
52 switch ($char) {
3d8371be 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;
7a9e9c89 80 }
81 }
451f74a2 82 }
7a9e9c89 83 // listEntities($msg);
3d8371be 84 return $msg;
451f74a2 85}
b74ba498 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
93f92f03 96function mime_fetch_body($imap_stream, $id, $ent_id) {
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) {
451f74a2 102 $ent_id = 1;
09a4bde3 103 }
6ab1bd9e 104 $cmd = "FETCH $id BODY[$ent_id]";
3d8371be 105
a4a70693 106 $data = sqimap_run_command ($imap_stream, $cmd, true, $response, $message, $uid_support);
77b88425 107 do {
3d8371be 108 $topline = trim(array_shift($data));
109 } while($topline && ($topline[0] == '*') && !preg_match('/\* [0-9]+ FETCH.*/i', $topline)) ;
a4a70693 110
451f74a2 111 $wholemessage = implode('', $data);
112 if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
3d8371be 113 $ret = substr($wholemessage, 0, $regs[1]);
114 /* There is some information in the content info header that could be important
115 * in order to parse html messages. Let's get them here.
116 */
117 if ($ret{0} == '<') {
a4a70693 118 $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message, $uid_support);
451f74a2 119 }
120 } else if (ereg('"([^"]*)"', $topline, $regs)) {
121 $ret = $regs[1];
122 } else {
123 global $where, $what, $mailbox, $passed_id, $startMessage;
3d8371be 124 $par = 'mailbox=' . urlencode($mailbox) . '&amp;passed_id=' . $passed_id;
451f74a2 125 if (isset($where) && isset($what)) {
3d8371be 126 $par .= '&amp;where=' . urlencode($where) . '&amp;what=' . urlencode($what);
a3daaaf3 127 } else {
3d8371be 128 $par .= '&amp;startMessage=' . $startMessage . '&amp;show_more=0';
451f74a2 129 }
e5ea9327 130 $par .= '&amp;response=' . urlencode($response) .
3d8371be 131 '&amp;message=' . urlencode($message) .
132 '&amp;topline=' . urlencode($topline);
a019eeb8 133
346817d4 134 echo '<tt><br>' .
135 '<table width="80%"><tr>' .
136 '<tr><td colspan=2>' .
0e5b61b4 137 _("Body retrieval error. The reason for this is most probably that the message is malformed.") .
346817d4 138 '</td></tr>' .
0e5b61b4 139 '<tr><td><b>' . _("Command:") . "</td><td>$cmd</td></tr>" .
140 '<tr><td><b>' . _("Response:") . "</td><td>$response</td></tr>" .
141 '<tr><td><b>' . _("Message:") . "</td><td>$message</td></tr>" .
142 '<tr><td><b>' . _("FETCH line:") . "</td><td>$topline</td></tr>" .
346817d4 143 "</table><BR></tt></font><hr>";
144
a4a70693 145 $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message, $uid_support);
451f74a2 146 array_shift($data);
147 $wholemessage = implode('', $data);
a019eeb8 148
346817d4 149 $ret = $wholemessage;
a3daaaf3 150 }
3d8371be 151 return $ret;
451f74a2 152}
d4467150 153
451f74a2 154function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
a4a70693 155 global $uid_support;
3d8371be 156 /* Do a bit of error correction. If we couldn't find the entity id, just guess
157 * that it is the first one. That is usually the case anyway.
158 */
451f74a2 159 if (!$ent_id) {
160 $ent_id = 1;
161 }
a4a70693 162 $sid = sqimap_session_id($uid_support);
3d8371be 163 /* Don't kill the connection if the browser is over a dialup
164 * and it would take over 30 seconds to download it.
165