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$ |
d6c32258 |
13 | * @package squirrelmail |
35586184 |
14 | */ |
b74ba498 |
15 | |
d6c32258 |
16 | /** The typical includes... */ |
b68edc75 |
17 | require_once(SM_PATH . 'functions/imap.php'); |
18 | require_once(SM_PATH . 'functions/attachment_common.php'); |
8beafbbc |
19 | |
7c7b74b3 |
20 | /* -------------------------------------------------------------------------- */ |
21 | /* MIME DECODING */ |
22 | /* -------------------------------------------------------------------------- */ |
b74ba498 |
23 | |
d6c32258 |
24 | /** |
25 | * Get the MIME structure |
26 | * |
27 | * This function gets the structure of a message and stores it in the "message" class. |
451f74a2 |
28 | * It will return this object for use with all relevant header information and |
29 | * fully parsed into the standard "message" object format. |
30 | */ |
a4a70693 |
31 | function mime_structure ($bodystructure, $flags=array()) { |
c9d78ab4 |
32 | |
3d8371be |
33 | /* Isolate the body structure and remove beginning and end parenthesis. */ |
a4a70693 |
34 | $read = trim(substr ($bodystructure, strpos(strtolower($bodystructure), 'bodystructure') + 13)); |
451f74a2 |
35 | $read = trim(substr ($read, 0, -1)); |
22efa9fb |
36 | $i = 0; |
37 | $msg = Message::parseStructure($read,$i); |
9de42168 |
38 | if (!is_object($msg)) { |
bd9c880b |
39 | include_once(SM_PATH . 'functions/display_messages.php'); |
3d8371be |
40 | global $color, $mailbox; |
c96c32f4 |
41 | /* removed urldecode because $_GET is auto urldecoded ??? */ |
a48eba8f |
42 | displayPageHeader( $color, $mailbox ); |
5e8de8b6 |
43 | $errormessage = _("SquirrelMail could not decode the bodystructure of the message"); |
3d8371be |
44 | $errormessage .= '<BR>'._("the provided bodystructure by your imap-server").':<BR><BR>'; |
472e7acb |
45 | $errormessage .= '<pre>' . htmlspecialchars($read) . '</pre>'; |
9de42168 |
46 | plain_error_message( $errormessage, $color ); |
3d8371be |
47 | echo '</body></html>'; |
9de42168 |
48 | exit; |
49 | } |
a4a70693 |
50 | if (count($flags)) { |
7a9e9c89 |
51 | foreach ($flags as $flag) { |
52 | $char = strtoupper($flag{1}); |
53 | switch ($char) { |
3d8371be |
54 | case 'S': |
55 | if (strtolower($flag) == '\\seen') { |
56 | $msg->is_seen = true; |
57 | } |
58 | break; |
59 | case 'A': |
60 | if (strtolower($flag) == '\\answered') { |
61 | $msg->is_answered = true; |
62 | } |
63 | break; |
64 | case 'D': |
65 | if (strtolower($flag) == '\\deleted') { |
66 | $msg->is_deleted = true; |
67 | } |
68 | break; |
69 | case 'F': |
70 | if (strtolower($flag) == '\\flagged') { |
71 | $msg->is_flagged = true; |
72 | } |
73 | break; |
74 | case 'M': |
75 | if (strtolower($flag) == '$mdnsent') { |
76 | $msg->is_mdnsent = true; |
77 | } |
78 | break; |
79 | default: |
80 | break; |
7a9e9c89 |
81 | } |
82 | } |
451f74a2 |
83 | } |
7a9e9c89 |
84 | // listEntities($msg); |
3d8371be |
85 | return $msg; |
451f74a2 |
86 | } |
b74ba498 |
87 | |
22efa9fb |
88 | |
89 | |
3d8371be |
90 | /* This starts the parsing of a particular structure. It is called recursively, |
451f74a2 |
91 | * so it can be passed different structures. It returns an object of type |
92 | * $message. |
93 | * First, it checks to see if it is a multipart message. If it is, then it |
94 | * handles that as it sees is necessary. If it is just a regular entity, |
95 | * then it parses it and adds the necessary header information (by calling out |
96 | * to mime_get_elements() |
97 | */ |
451f74a2 |
98 | |
4d592352 |
99 | function mime_fetch_body($imap_stream, $id, $ent_id=1, $fetch_size=0) { |
da2415c1 |
100 | global $uid_support; |
3d8371be |
101 | /* Do a bit of error correction. If we couldn't find the entity id, just guess |
09a4bde3 |
102 | * that it is the first one. That is usually the case anyway. |
103 | */ |
7c7b74b3 |
104 | |
09a4bde3 |
105 | if (!$ent_id) { |
08b7f7cc |
106 | $cmd = "FETCH $id BODY[]"; |
1035e159 |
107 | } else { |
08b7f7cc |
108 | $cmd = "FETCH $id BODY[$ent_id]"; |
09a4bde3 |
109 | } |
3d8371be |
110 | |
4d592352 |
111 | if ($fetch_size!=0) $cmd .= "<0.$fetch_size>"; |
da2415c1 |
112 | |
a4a70693 |
113 | $data = sqimap_run_command ($imap_stream, $cmd, true, $response, $message, $uid_support); |
77b88425 |
114 | do { |
3d8371be |
115 | $topline = trim(array_shift($data)); |
116 | } while($topline && ($topline[0] == '*') && !preg_match('/\* [0-9]+ FETCH.*/i', $topline)) ; |
a4a70693 |
117 | |
451f74a2 |
118 | $wholemessage = implode('', $data); |
119 | if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) { |
3d8371be |
120 | $ret = substr($wholemessage, 0, $regs[1]); |
121 | /* There is some information in the content info header that could be important |
122 | * in order to parse html messages. Let's get them here. |
123 | */ |
0600bdf1 |
124 | // if ($ret{0} == '<') { |
125 | // $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message, $uid_support); |
126 | // } |
451f74a2 |
127 | } else if (ereg('"([^"]*)"', $topline, $regs)) { |
128 | $ret = $regs[1]; |
129 | } else { |
130 | global $where, $what, $mailbox, $passed_id, $startMessage; |
3d8371be |
131 | $par = 'mailbox=' . urlencode($mailbox) . '&passed_id=' . $passed_id; |
451f74a2 |
132 | if (isset($where) && isset($what)) { |
3d8371be |
133 | $par .= '&where=' . urlencode($where) . '&what=' . urlencode($what); |
a3daaaf3 |
134 | } else { |
3d8371be |
135 | $par .= '&startMessage=' . $startMessage . '&show_more=0'; |
451f74a2 |
136 | } |
e5ea9327 |
137 | $par .= '&response=' . urlencode($response) . |
3d8371be |
138 | '&message=' . urlencode($message) . |
139 | '&topline=' . urlencode($topline); |
a019eeb8 |
140 | |
346817d4 |
141 | echo '<tt><br>' . |
142 | '<table width="80%"><tr>' . |
143 | '<tr><td colspan=2>' . |
0e5b61b4 |
144 | _("Body retrieval error. The reason for this is most probably that the message is malformed.") . |
346817d4 |
145 | '</td></tr>' . |
0e5b61b4 |
146 | '<tr><td><b>' . _("Command:") . "</td><td>$cmd</td></tr>" . |
147 | '<tr><td><b>' . _("Response:") . "</td><td>$response</td></tr>" . |
148 | '<tr><td><b>' . _("Message:") . "</td><td>$message</td></tr>" . |
149 | '<tr><td><b>' . _("FETCH line:") . "</td><td>$topline</td></tr>" . |
346817d4 |
150 | "</table><BR></tt></font><hr>"; |
151 | |
a4a70693 |
152 | $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message, $uid_support); |
451f74a2 |
153 | array_shift($data); |
154 | $wholemessage = implode('', $data); |
a019eeb8 |
155 | |
346817d4 |
156 | $ret = $wholemessage; |
a3daaaf3 |
157 | } |
3d8371be |
158 | return $ret; |
451f74a2 |
159 | } |
d4467150 |
160 | |
1035e159 |
161 | function mime_print_body_lines ($imap_stream, $id, $ent_id=1, $encoding) { |
a4a70693 |
162 | global $uid_support; |
1035e159 |
163 | |
3d8371be |
164 | /* Don't kill the connection if the browser is over a dialup |
165 | * and it would take over 30 seconds to download it. |
166 |