aceb0d5c |
1 | <? |
2 | /** mime.php |
3 | ** |
4 | ** This contains the functions necessary to detect and decode MIME messages. |
5 | **/ |
6 | |
7 | |
4809f489 |
8 | /** This is the first function called. It decides if this is a multipart |
9 | message or if it should be handled as a single entity |
10 | **/ |
11 | function decodeMime($body, $bound, $type0, $type1, &$entities) { |
aceb0d5c |
12 | if ($type0 == "multipart") { |
aceb0d5c |
13 | $bound = trim($bound); |
f7835374 |
14 | $i = 0; |
4809f489 |
15 | while (($i < count($body)) && (substr($body[$i], 0, strlen("--$bound--")) != "--$bound--")) { |
16 | if (trim($body[$i]) == "--$bound") { |
17 | $j = $i+1; |
aceb0d5c |
18 | $p = 0; |
19 | |
4809f489 |
20 | /** Lets find the header for this entity **/ |
21 | /** If the first line after the boundary is blank, we use default values **/ |
22 | if (trim($body[$j]) == "") { |
23 | $ent_type0 = "text"; |
24 | $ent_type1 = "plain"; |
25 | $charset = "us-ascii"; |
aceb0d5c |
26 | $j++; |
4809f489 |
27 | /** If the first line ISNT blank, read in the header for this entity **/ |
28 | } else { |
29 | while ((substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") && (trim($body[$j]) != "")) { |
30 | $entity_header[$p] = $body[$j]; |
31 | $j++; |
32 | $p++; |
33 | } |
34 | /** All of these values are getting passed back to us **/ |
d29aac0e |
35 | sqimap_get_entity_header($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset, $filename); |
aceb0d5c |
36 | } |
bcb432a3 |
37 | |
bcb432a3 |
38 | |
4809f489 |
39 | /** OK, we have the header information, now lets decide what to do with it **/ |
40 | if ($ent_type0 == "multipart") { |
41 | $y = 0; |
42 | while (substr($body[$j], 0, strlen("--$bound--")) != "--$bound--") { |
43 | $ent_body[$y] = $body[$j]; |
44 | $y++; |
45 | $j++; |
46 | } |
47 | $ent = decodeMime($ent_body, $ent_bound, $ent_type0, $ent_type1, $entities); |
48 | $entities = $ent; |
bcb432a3 |
49 | } else { |
7c9499e1 |
50 | $j++; |
51 | $entity_body = ""; |
4809f489 |
52 | while (substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") { |
bcb432a3 |
53 | $entity_body .= $body[$j]; |
54 | $j++; |
55 | } |
4809f489 |
56 | $count = count($entities); |
57 | $entities[$count] = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset, $filename); |
bcb432a3 |
58 | } |
aceb0d5c |
59 | } |
60 | $i++; |
61 | } |
d4467150 |
62 | } else { |
7831268e |
63 | /** If this isn't a multipart message **/ |
64 | $j = 0; |
65 | $entity_body = ""; |
78509c54 |
66 | while ($j < count($body)) { |
7831268e |
67 | $entity_body .= $body[$j]; |
68 | $j++; |
69 | } |
70 | |
4809f489 |
71 | $count = count($entities); |
7831268e |
72 | $entities[$count] = getEntity($entity_body, $bound, $type0, $type1, $encoding, $charset, $filename); |
d4467150 |
73 | } |
74 | |
4809f489 |
75 | return $entities; |
d4467150 |
76 | } |
77 | |
78 | /** This gets one entity's properties **/ |
7c9499e1 |
79 | function getEntity($body, $bound, $type0, $type1, $encoding, $charset, $filename) { |
4809f489 |
80 | $msg["TYPE0"] = $type0; |
81 | $msg["TYPE1"] = $type1; |
82 | $msg["ENCODING"] = $encoding; |
83 | $msg["CHARSET"] = $charset; |
84 | $msg["FILENAME"] = $filename; |
d4467150 |
85 | |
7831268e |
86 | $msg["BODY"] = $body; |
aceb0d5c |
87 | |
d4467150 |
88 | return $msg; |
89 | } |
90 | |
4809f489 |
91 | /** This will check whether or not the message contains a certain type. It |
92 | searches through all the entities for a match. |
93 | **/ |
b1dadc61 |
94 | function containsType($message, $type0, $type1, &$ent_num) { |
95 | $type0 = strtolower($type0); |
96 | $type1 = strtolower($type1); |
d4467150 |
97 | for ($i = 0; $i < count($message["ENTITIES"]); $i++) { |
b1dadc61 |
98 | /** Check only on type0 **/ |
99 | if ( $type1 == "any_type" ) { |
100 | if ( ($message["ENTITIES"][$i]["TYPE0"] == $type0) ) { |
101 | $ent_num = $i; |
102 | return true; |
103 | } |
104 | |
105 | /** Check on type0 and type1 **/ |
106 | } else { |
107 | if ( ($message["ENTITIES"][$i]["TYPE0"] == $type0) && ($message["ENTITIES"][$i]["TYPE1"] == $type1) ) { |
108 | $ent_num = $i; |
109 | return true; |
110 | } |
d4467150 |
111 | } |
112 | } |
b1dadc61 |
113 | return false; |
114 | } |
8405ee35 |
115 | |
4809f489 |
116 | /** This returns a parsed string called $body. That string can then be displayed |
117 | as the actual message in the HTML. It contains everything needed, including |
118 | HTML Tags, Attachments at the bottom, etc. |
119 | **/ |
a8648d75 |
120 | function formatBody($message, $color, $wrap_at) { |
7831268e |
121 | |
4809f489 |
122 | /** this if statement checks for the entity to show as the primary message. To |
123 | add more of them, just put them in the order that is their priority. |
124 | **/ |
7085fa78 |
125 | $id = $message["INFO"]["ID"]; |
126 | $urlmailbox = urlencode($message["INFO"]["MAILBOX"]); |
7085fa78 |
127 | |
b1dadc61 |
128 | if (containsType($message, "text", "html", $ent_num)) { |
a8648d75 |
129 | $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); |
b1dadc61 |
130 | } else if (containsType($message, "text", "plain", $ent_num)) { |
a8648d75 |
131 | $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); |
4809f489 |
132 | } |
133 | // add other primary displaying message types here |
b1dadc61 |
134 | else { |
135 | // find any type that's displayable |
136 | if (containsType($message, "text", "any_type", $ent_num)) { |
a8648d75 |
137 | $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); |
b1dadc61 |
138 | } else if (containsType($message, "message", "any_type", $ent_num)) { |
a8648d75 |
139 | $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); |
8405ee35 |
140 | } |
141 | } |
142 | |
78509c54 |
143 | /** If there are other types that shouldn't be formatted, add them here **/ |
144 | if ($message["ENTITIES"][$ent_num]["TYPE1"] != "html") |
a8648d75 |
145 | $body = translateText($body, $wrap_at); |
146 | |
78509c54 |
147 | |
9f9d7d28 |
148 | $body .= "<BR><SMALL><CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$ent_num&mailbox=$urlmailbox\">". _("Download this as a file") ."</A></CENTER><BR></SMALL>"; |
7831268e |
149 | |
b1dadc61 |
150 | /** Display the ATTACHMENTS: message if there's more than one part **/ |
151 | if (count($message["ENTITIES"]) > 1) { |
7831268e |
152 | $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=4 BORDER=0><TR><TD BGCOLOR=\"$color[0]\">"; |
153 | $body .= "<TT><B>ATTACHMENTS:</B></TT>"; |
154 | $body .= "</TD></TR><TR><TD BGCOLOR=\"$color[0]\">"; |
b1dadc61 |
155 | $num = 0; |
156 | |
157 | for ($i = 0; $i < count($message["ENTITIES"]); $i++) { |
158 | /** If we've displayed this entity, go to the next one **/ |
159 | if ($ent_num == $i) |
160 | continue; |
161 | |
162 | $type0 = strtolower($message["ENTITIES"][$i]["TYPE0"]); |
163 | $type1 = strtolower($message["ENTITIES"][$i]["TYPE1"]); |
164 | |
165 | $num++; |
7c9499e1 |
166 | $filename = $message["ENTITIES"][$i]["FILENAME"]; |
b1dadc61 |
167 | if (trim($filename) == "") { |
7085fa78 |
168 | $display_filename = "untitled$i"; |
b1dadc61 |
169 | } else { |
170 | $display_filename = $filename; |
171 | } |
7c9499e1 |
172 | |
97be2168 |
173 | $urlMailbox = urlencode($message["INFO"]["MAILBOX"]); |
174 | $id = $message["INFO"]["ID"]; |
7831268e |
175 | $body .= "<TT> <A HREF=\"../src/download.php?passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$i\">" . $display_filename . "</A> <SMALL>(TYPE: $type0/$type1)</SMALL></TT><BR>"; |
8405ee35 |
176 | } |
7831268e |
177 | $body .= "</TD></TR></TABLE>"; |
8405ee35 |
178 | } |
d4467150 |
179 | return $body; |
180 | } |
181 | |
4809f489 |
182 | |
183 | |
184 | /** this function decodes the body depending on the encoding type. **/ |
d4467150 |
185 | function decodeBody($body, $encoding) { |
186 | $encoding = strtolower($encoding); |
7831268e |
187 | |
ef3f274f |
188 | if ($encoding == "quoted-printable") { |
189 | $body = quoted_printable_decode($body); |
db87f79c |
190 | |
ef3f274f |
191 | while (ereg("=\n", $body)) |
192 | $body = ereg_replace ("=\n", "", $body); |
97be2168 |
193 | } else if ($encoding == "base64") { |
ef3f274f |
194 | $body = base64_decode($body); |
d4467150 |
195 | } |
ef3f274f |
196 | |
197 | // All other encodings are returned raw. |
198 | return $body; |
aceb0d5c |
199 | } |
a4c2cd49 |
200 | |
201 | |
202 | // This functions decode strings that is encoded according to |
203 | // RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text). |
204 | function rfc1522Decode ($string) { |
205 | // Recognizing only US-ASCII and ISO-8859. Other charsets should |
206 | // probably be recognized as well. |
207 | if (eregi('=\?(us-ascii|iso-8859-([0-9])+)\?(q|b)\?([^?]+)\?=', |
208 | $string, $res)) { |
209 | if (ucfirst($res[3]) == "B") { |
210 | $replace = base64_decode($res[4]); |
211 | } else { |
212 | $replace = ereg_replace("_", " ", $res[4]); |
213 | $replace = quoted_printable_decode($replace); |
214 | } |
215 | |
216 | // Only US-ASCII and ISO-8859-1 can be displayed without further ado |
217 | if ($res[2] != "" && $res[2] != "1") { |
ef3f274f |
218 | // This gets rid of all characters with over 0x9F for other |
219 | // charsets. |
a4c2cd49 |
220 | $replace = strtr($replace, "\240\241\242\243\244\245\246\247". |
221 | "\250\251\252\253\254\255\256\257". |
222 | "\260\261\262\263\264\265\266\267". |
223 | "\270\271\272\273\274\275\276\277". |
224 | "\300\301\302\303\304\305\306\307". |
225 | "\310\311\312\313\314\315\316\317". |
226 | "\320\321\322\323\324\325\326\327". |
227 | "\330\331\332\333\334\335\336\337". |
228 | "\340\341\342\343\344\345\346\347". |
229 | "\350\351\352\353\354\355\356\357". |
230 | "\360\361\362\363\364\365\366\367". |
231 | "\370\371\372\373\374\375\376\377", |
232 | "????????????????????????????????????????". |
233 | "????????????????????????????????????????". |
234 | "????????????????????????????????????????". |
235 | "????????"); |
236 | } |
237 | |
238 | $string = eregi_replace |
239 | ('=\?(us-ascii|iso-8859-([0-9])+)\?(q|b)\?([^?]+)\?=', |
240 | $replace, $string); |
241 | |
242 | return (rfc1522Decode($string)); |
243 | } else |
244 | return ($string); |
245 | } |
246 | |
9f9d7d28 |
247 | ?> |