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