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 |
15 | require_once('../functions/imap.php'); |
16 | require_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 |
27 | function 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]; |
a4a70693 |
35 | $msg->setEnt('0'); |
36 | if (count($flags)) { |
37 | foreach ($flags as $flag) { |
38 | $char = strtoupper($flag{1}); |
39 | switch ($char) { |
40 | case 'S': |
41 | if (strtolower($flag) == '\\seen') { |
42 | $msg->is_seen = true; |
43 | } |
44 | break; |
45 | case 'A': |
46 | if (strtolower($flag) == '\\answered') { |
47 | $msg->is_answered = true; |
48 | } |
49 | break; |
50 | case 'D': |
51 | if (strtolower($flag) == '\\deleted') { |
52 | $msg->is_deleted = true; |
53 | } |
54 | break; |
55 | case 'F': |
56 | if (strtolower($flag) == '\\flagged') { |
57 | $msg->is_flagged = true; |
58 | } |
59 | break; |
60 | case 'M': |
f792c641 |
61 | if (strtolower($flag) == '$mdnsent') { |
62 | $msg->is_mdnsent = true; |
a4a70693 |
63 | } |
64 | break; |
65 | default: |
66 | break; |
67 | } |
68 | } |
451f74a2 |
69 | } |
1d142b8d |
70 | // listEntities($msg); |
451f74a2 |
71 | return( $msg ); |
72 | } |
b74ba498 |
73 | |
451f74a2 |
74 | /* this starts the parsing of a particular structure. It is called recursively, |
75 | * so it can be passed different structures. It returns an object of type |
76 | * $message. |
77 | * First, it checks to see if it is a multipart message. If it is, then it |
78 | * handles that as it sees is necessary. If it is just a regular entity, |
79 | * then it parses it and adds the necessary header information (by calling out |
80 | * to mime_get_elements() |
81 | */ |
451f74a2 |
82 | |
93f92f03 |
83 | function mime_fetch_body($imap_stream, $id, $ent_id) { |
a4a70693 |
84 | global $uid_support; |
09a4bde3 |
85 | /* |
86 | * do a bit of error correction. If we couldn't find the entity id, just guess |
87 | * that it is the first one. That is usually the case anyway. |
88 | */ |
89 | if (!$ent_id) { |
451f74a2 |
90 | $ent_id = 1; |
09a4bde3 |
91 | } |
6ab1bd9e |
92 | $cmd = "FETCH $id BODY[$ent_id]"; |
a4a70693 |
93 | |
94 | $data = sqimap_run_command ($imap_stream, $cmd, true, $response, $message, $uid_support); |
77b88425 |
95 | do { |
e976319c |
96 | $topline = trim(array_shift( $data )); |
97 | } while( $topline && $topline[0] == '*' && !preg_match( '/\* [0-9]+ FETCH.*/i', $topline )) ; |
a4a70693 |
98 | |
451f74a2 |
99 | $wholemessage = implode('', $data); |
100 | if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) { |
77b88425 |
101 | |
451f74a2 |
102 | $ret = substr( $wholemessage, 0, $regs[1] ); |
103 | /* |
104 | There is some information in the content info header that could be important |
105 | in order to parse html messages. Let's get them here. |
106 | */ |
107 | if ( $ret{0} == '<' ) { |
a4a70693 |
108 | $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message, $uid_support); |
451f74a2 |
109 | } |
110 | } else if (ereg('"([^"]*)"', $topline, $regs)) { |
111 | $ret = $regs[1]; |
112 | } else { |
113 | global $where, $what, $mailbox, $passed_id, $startMessage; |
e5ea9327 |
114 | $par = 'mailbox=' . urlencode($mailbox) . "&passed_id=$passed_id"; |
451f74a2 |
115 | if (isset($where) && isset($what)) { |
e5ea9327 |
116 | $par .= '&where='. urlencode($where) . "&what=" . urlencode($what); |
a3daaaf3 |
117 | } else { |
e5ea9327 |
118 | $par .= "&startMessage=$startMessage&show_more=0"; |
451f74a2 |
119 | } |
e5ea9327 |
120 | $par .= '&response=' . urlencode($response) . |
121 | '&message=' . urlencode($message). |
122 | '&topline=' . urlencode($topline); |
a019eeb8 |
123 | |
346817d4 |
124 | echo '<tt><br>' . |
125 | '<table width="80%"><tr>' . |
126 | '<tr><td colspan=2>' . |
451f74a2 |
127 | _("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 |
128 | " <A HREF=\"../src/retrievalerror.php?$par\"><br>" . |
129 | _("Submit message") . '</A><BR> ' . |
130 | '</td></tr>' . |
131 | '<td><b>' . _("Command:") . "</td><td>$cmd</td></tr>" . |
132 | '<td><b>' . _("Response:") . "</td><td>$response</td></tr>" . |
133 | '<td><b>' . _("Message:") . "</td><td>$message</td></tr>" . |
134 | '<td><b>' . _("FETCH line:") . "</td><td>$topline</td></tr>" . |
135 | "</table><BR></tt></font><hr>"; |
136 | |
a4a70693 |
137 | $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message, $uid_support); |
451f74a2 |
138 | array_shift($data); |
139 | $wholemessage = implode('', $data); |
a019eeb8 |
140 | |
346817d4 |
141 | $ret = $wholemessage; |
a3daaaf3 |
142 | } |
451f74a2 |
143 | return( $ret ); |
144 | } |
d4467150 |
145 | |
451f74a2 |
146 | function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) { |
a4a70693 |
147 | global $uid_support; |
451f74a2 |
148 | // do a bit of error correction. If we couldn't find the entity id, just guess |
149 | // that it is the first one. That is usually the case anyway. |
150 | if (!$ent_id) { |
151 | $ent_id = 1; |
152 | } |
a4a70693 |
153 | $sid = sqimap_session_id($uid_support); |
451f74a2 |
154 | // Don't kill the connection if the browser is over a dialup |
155 | // and it would take over 30 seconds to download it. |
b7206e1d |
156 | |
83be314a |
157 |