59177427 |
1 | <?php |
aceb0d5c |
2 | /** mime.php |
3 | ** |
d068c0ec |
4 | ** This contains the functions necessary to detect and decode MIME |
5 | ** messages. |
6 | ** |
aceb0d5c |
7 | **/ |
8 | |
e79bed1b |
9 | $debug_mime = false; |
d068c0ec |
10 | $mime_php = true; |
aceb0d5c |
11 | |
1fd97780 |
12 | if (!isset($i18n_php)) |
13 | include "../functions/i18n.php"; |
8beafbbc |
14 | if (!isset($imap_php)) |
15 | include "../functions/imap.php"; |
16 | if (!isset($config_php)) |
17 | include "../config/config.php"; |
18 | |
19 | |
cbcf32f6 |
20 | /** Setting up the objects that have the structure for the message **/ |
8beafbbc |
21 | |
22 | class msg_header { |
23 | /** msg_header contains generic variables for values that **/ |
24 | /** could be in a header. **/ |
25 | |
26 | var $type0, $type1, $boundary, $charset, $encoding; |
27 | var $to, $from, $date, $cc, $bcc, $reply_to, $subject; |
ea48eb25 |
28 | var $id, $mailbox, $description; |
8beafbbc |
29 | var $entity_id; |
30 | } |
31 | |
32 | class message { |
33 | /** message is the object that contains messages. It is a recursive |
34 | object in that through the $entities variable, it can contain |
35 | more objects of type message. See documentation in mime.txt for |
36 | a better description of how this works. |
37 | **/ |
38 | var $header; |
39 | var $entities; |
40 | |
41 | function addEntity ($msg) { |
42 | $this->entities[count($this->entities)] = $msg; |
43 | } |
44 | } |
1fd97780 |
45 | |
bcb432a3 |
46 | |
bcb432a3 |
47 | |
8beafbbc |
48 | /* --------------------------------------------------------------------------------- */ |
49 | /* MIME DECODING */ |
50 | /* --------------------------------------------------------------------------------- */ |
51 | |
cbcf32f6 |
52 | // This function gets the structure of a message and stores it in the "message" class. |
53 | // It will return this object for use with all relevant header information and |
54 | // fully parsed into the standard "message" object format. |
8beafbbc |
55 | function mime_structure ($imap_stream, $header) { |
e79bed1b |
56 | global $debug_mime; |
8beafbbc |
57 | sqimap_messages_flag ($imap_stream, $header->id, $header->id, "Seen"); |
58 | |
59 | $id = $header->id; |
60 | fputs ($imap_stream, "a001 FETCH $id BODYSTRUCTURE\r\n"); |
e79bed1b |
61 | $read = fgets ($imap_stream, 10000); |
62 | $read = strtolower($read); |
8beafbbc |
63 | |
e79bed1b |
64 | if ($debug_mime) echo "<tt>$read</tt><br><br>"; |
8beafbbc |
65 | // isolate the body structure and remove beginning and end parenthesis |
66 | $read = trim(substr ($read, strpos($read, "bodystructure") + 13)); |
ea48eb25 |
67 | $read = trim(substr ($read, 0, -1)); |
68 | $end = mime_match_parenthesis(0, $read); |
69 | while ($end == strlen($read)-1) { |
70 | $read = trim(substr ($read, 0, -1)); |
71 | $read = trim(substr ($read, 1)); |
72 | $end = mime_match_parenthesis(0, $read); |
73 | } |
8beafbbc |
74 | |
e79bed1b |
75 | if ($debug_mime) echo "<tt>$read</tt><br><br>"; |
76 | |
8beafbbc |
77 | $msg = mime_parse_structure ($read); |
78 | $msg->header = $header; |
79 | return $msg; |
80 | } |
81 | |
cbcf32f6 |
82 | // this starts the parsing of a particular structure. It is called recursively, |
83 | // so it can be passed different structures. It returns an object of type |
84 | // $message. |
85 | // First, it checks to see if it is a multipart message. If it is, then it |
86 | // handles that as it sees is necessary. If it is just a regular entity, |
87 | // then it parses it and adds the necessary header information (by calling out |
88 | // to mime_get_elements() |
8beafbbc |
89 | function mime_parse_structure ($structure, $ent_id) { |
e79bed1b |
90 | global $debug_mime; |
91 | if ($debug_mime) echo "<font color=008800><tt>START: mime_parse_structure()</tt></font><br>"; |
8beafbbc |
92 | $msg = new message(); |
93 | if (substr($structure, 0, 1) == "(") { |
94 | $ent_id = mime_new_element_level($ent_id); |
95 | $start = $end = -1; |
ea48eb25 |
96 | if ($debug_mime) echo "<br><font color=0000aa><tt>$structure</tt></font><br>"; |
8beafbbc |
97 | do { |
e79bed1b |
98 | if ($debug_mime) echo "<font color=008800><tt>Found entity...</tt></font><br>"; |
8beafbbc |
99 | $start = $end+1; |
100 | $end = mime_match_parenthesis ($start, $structure); |
101 | |
102 | $element = substr($structure, $start+1, ($end - $start)-1); |
ea48eb25 |
103 | $ent_id = mime_increment_id ($ent_id); |
8beafbbc |
104 | $newmsg = mime_parse_structure ($element, $ent_id); |
105 | $msg->addEntity ($newmsg); |
106 | } while (substr($structure, $end+1, 1) == "("); |
107 | } else { |
108 | // parse the elements |
e79bed1b |
109 | if ($debug_mime) echo "<br><font color=0000aa><tt>$structure</tt></font><br>"; |
ea48eb25 |
110 | $msg = mime_get_element (&$structure, $msg, $ent_id); |
e79bed1b |
111 | if ($debug_mime) echo "<br>"; |
8beafbbc |
112 | } |
113 | return $msg; |
e79bed1b |
114 | if ($debug_mime) echo "<font color=008800><tt> END: mime_parse_structure()</tt></font><br>"; |
8beafbbc |
115 | } |
116 | |
117 | // Increments the element ID. An element id can look like any of |
118 | // the following: 1, 1.2, 4.3.2.4.1, etc. This function increments |
119 | // the last number of the element id, changing 1.2 to 1.3. |
120 | function mime_increment_id ($id) { |
ea48eb25 |
121 | global $debug_mime; |
8beafbbc |
122 | if (strpos($id, ".")) { |
123 | $first = substr($id, 0, strrpos($id, ".")); |
ea48eb25 |
124 | $last = substr($id, strrpos($id, ".")+1); |
8beafbbc |
125 | $last++; |
ea48eb25 |
126 | $new = $first . "." .$last; |
8beafbbc |
127 | } else { |
128 | $new = $id + 1; |
129 | } |
ea48eb25 |
130 | if ($debug_mime) echo "<b>INCREMENT: $new</b><br>"; |
8beafbbc |
131 | return $new; |
132 | } |
133 | |
134 | // See comment for mime_increment_id(). |
135 | // This adds another level on to the entity_id changing 1.3 to 1.3.0 |
136 | // NOTE: 1.3.0 is not a valid element ID. It MUST be incremented |
137 | // before it can be used. I left it this way so as not to have |
138 | // to make a special case if it is the first entity_id. It |
139 | // always increments it, and that works fine. |
140 | function mime_new_element_level ($id) { |
ea48eb25 |
141 | if (!$id) $id = 0; |
142 | else $id = $id . ".0"; |
143 | |
8beafbbc |
144 | return $id; |
145 | } |
146 | |
ea48eb25 |
147 | function mime_get_element (&$structure, $msg, $ent_id) { |
e79bed1b |
148 | global $debug_mime; |
8beafbbc |
149 | $elem_num = 1; |
ea48eb25 |
150 | $msg->header = new msg_header(); |
151 | $msg->header->entity_id = $ent_id; |
8beafbbc |
152 | |
153 | while (strlen($structure) > 0) { |
154 | $structure = trim($structure); |
155 | $char = substr($structure, 0, 1); |
156 | |
157 | if (substr($structure, 0, 3) == "nil") { |
158 | $text = ""; |
159 | $structure = substr($structure, 3); |
160 | } else if ($char == "\"") { |
161 | // loop through until we find the matching quote, and return that as a string |
162 | $pos = 1; |
163 | $char = substr($structure, $pos, 1); |
164 | while ($char != "\"" && $pos < strlen($structure)) { |
165 | $text .= $char; |
166 | $pos++; |
167 | $char = substr($structure, $pos, 1); |
168 | } |
169 | $structure = substr($structure, strlen($text) + 2); |
170 | } else if ($char == "(") { |
171 | // comment me |
172 | $end = mime_match_parenthesis (0, $structure); |
173 | $sub = substr($structure, 1, $end-1); |
174 | $properties = mime_get_props($properties, $sub); |
175 | $structure = substr($structure, strlen($sub) + 2); |
176 | } else { |
177 | // loop through until we find a space or an end parenthesis |
178 | $pos = 0; |
179 | $char = substr($structure, $pos, 1); |
180 | while ($char != " " && $char != ")" && $pos < strlen($structure)) { |
181 | $text .= $char; |
182 | $pos++; |
183 | $char = substr($structure, $pos, 1); |
aceb0d5c |
184 | } |
8beafbbc |
185 | $structure = substr($structure, strlen($text)); |
aceb0d5c |
186 | } |
e79bed1b |
187 | if ($debug_mime) echo "<tt>$elem_num : $text</tt><br>"; |
8beafbbc |
188 | |
189 | // This is where all the text parts get put into the header |
190 | switch ($elem_num) { |
191 | case 1: |
ea48eb25 |
192 | $msg->header->type0 = $text; |
e79bed1b |
193 | if ($debug_mime) echo "<tt>type0 = $text</tt><br>"; |
8beafbbc |
194 | break; |
195 | case 2: |
ea48eb25 |
196 | $msg->header->type1 = $text; |
e79bed1b |
197 | if ($debug_mime) echo "<tt>type1 = $text</tt><br>"; |
8beafbbc |
198 | break; |
ea48eb25 |
199 | case 5: |
200 | $msg->header->description = $text; |
201 | if ($debug_mime) echo "<tt>description = $text</tt><br>"; |
202 | break; |
8beafbbc |
203 | case 6: |
ea48eb25 |
204 | $msg->header->encoding = $text; |
e79bed1b |
205 | if ($debug_mime) echo "<tt>encoding = $text</tt><br>"; |
8beafbbc |
206 | break; |
207 | case 7: |
ea48eb25 |
208 | $msg->header->size = $text; |
e79bed1b |
209 | if ($debug_mime) echo "<tt>size = $text</tt><br>"; |
8beafbbc |
210 | break; |
211 | default: |
ea48eb25 |
212 | if ($msg->header->type0 == "text" && $elem_num == 8) { |
cbcf32f6 |
213 | // This is a plain text message, so lets get the number of lines |
214 | // that it contains. |
ea48eb25 |
215 | $msg->header->num_lines = $text; |
e79bed1b |
216 | if ($debug_mime) echo "<tt>num_lines = $text</tt><br>"; |
cbcf32f6 |
217 | |
ea48eb25 |
218 | } else if ($msg->header->type0 == "message" && $msg->header->type1 == "rfc822" && $elem_num == 8) { |
219 | // This is an encapsulated message, so lets start all over again and |
220 | // parse this message adding it on to the existing one. |
221 | $structure = trim($structure); |
222 | if (substr($structure, 0, 1) == "(") { |
223 | $e = mime_match_parenthesis (0, $structure); |
224 | $structure = substr($structure, 0, $e); |
225 | $structure = substr($structure, 1); |
226 | $m = mime_parse_structure($structure, $msg->header->entity_id); |
cbcf32f6 |
227 | |
228 | // the following conditional is there to correct a bug that wasn't |
229 | // incrementing the entity IDs correctly because of the special case |
230 | // that message/rfc822 is. This fixes it fine. |
ea48eb25 |
231 | if (substr($structure, 1, 1) != "(") |
232 | $m->header->entity_id = mime_increment_id(mime_new_element_level($ent_id)); |
cbcf32f6 |
233 | |
234 | // Now we'll go through and reformat the results. |
ea48eb25 |
235 | if ($m->entities) { |
236 | for ($i=0; $i < count($m->entities); $i++) { |
ea48eb25 |
237 | $msg->addEntity($m->entities[$i]); |
238 | } |
239 | } else { |
ea48eb25 |
240 | $msg->addEntity($m); |
241 | } |
242 | $structure = ""; |
243 | } |
8beafbbc |
244 | } |
245 | break; |
246 | } |
247 | $elem_num++; |
248 | $text = ""; |
249 | } |
250 | // loop through the additional properties and put those in the various headers |
ea48eb25 |
251 | if ($msg->header->type0 != "message") { |
cbcf32f6 |
252 | for ($i=0; $i < count($properties); $i++) { |
253 | $msg->header->{$properties[$i]["name"]} = $properties[$i]["value"]; |
254 | if ($debug_mime) echo "<tt>".$properties[$i]["name"]." = " . $properties[$i]["value"] . "</tt><br>"; |
255 | } |
ea48eb25 |
256 | } |
257 | return $msg; |
8beafbbc |
258 | } |
259 | |
260 | // I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't |
261 | // figure out how to do this part, so I decided to go to bed. I woke up |
262 | // in the morning and had a flash of insight. I went to the white-board |
263 | // and scribbled it out, then spent a bit programming it, and this is the |
264 | // result. Nothing complicated, but I think my brain was fried yesterday. |
cbcf32f6 |
265 | // Funny how that happens some times. |
8beafbbc |
266 | // |
267 | // This gets properties in a nested parenthesisized list. For example, |
268 | // this would get passed something like: ("attachment" ("filename" "luke.tar.gz")) |
269 | // This returns an array called $props with all paired up properties. |
270 | // It ignores the "attachment" for now, maybe that should change later |
271 | // down the road. In this case, what is returned is: |
272 | // $props[0]["name"] = "filename"; |
273 | // $props[0]["value"] = "luke.tar.gz"; |
274 | function mime_get_props ($props, $structure) { |
e79bed1b |
275 | global $debug_mime; |
8beafbbc |
276 | while (strlen($structure) > 0) { |
277 | $structure = trim($structure); |
278 | $char = substr($structure, 0, 1); |
279 | |
280 | if ($char == "\"") { |
281 | $pos = 1; |
282 | $char = substr($structure, $pos, 1); |
283 | while ($char != "\"" && $pos < strlen($structure)) { |
284 | $tmp .= $char; |
285 | $pos++; |
286 | $char = substr($structure, $pos, 1); |
287 | } |
288 | $structure = trim(substr($structure, strlen($tmp) + 2)); |
289 | $char = substr($structure, 0, 1); |
290 | |
291 | if ($char == "\"") { |
292 | $pos = 1; |
293 | $char = substr($structure, $pos, 1); |
294 | while ($char != "\"" && $pos < strlen($structure)) { |
295 | $value .= $char; |
296 | $pos++; |
297 | $char = substr($structure, $pos, 1); |
298 | } |
299 | $structure = trim(substr($structure, strlen($tmp) + 2)); |
300 | |
301 | $k = count($props); |
302 | $props[$k]["name"] = $tmp; |
303 | $props[$k]["value"] = $value; |
304 | } else if ($char == "(") { |
305 | $end = mime_match_parenthesis (0, $structure); |
306 | $sub = substr($structure, 1, $end-1); |
307 | $props = mime_get_props($props, $sub); |
308 | $structure = substr($structure, strlen($sub) + 2); |
309 | } |
310 | return $props; |
311 | } else if ($char == "(") { |
312 | $end = mime_match_parenthesis (0, $structure); |
313 | $sub = substr($structure, 1, $end-1); |
314 | $props = mime_get_props($props, $sub); |
315 | $structure = substr($structure, strlen($sub) + 2); |
ea48eb25 |
316 | return $props; |
8beafbbc |
317 | } else { |
318 | return $props; |
7831268e |
319 | } |
8beafbbc |
320 | } |
321 | } |
7831268e |
322 | |
8beafbbc |
323 | // Matches parenthesis. It will return the position of the matching |
324 | // parenthesis in $structure. For instance, if $structure was: |
325 | // ("text" "plain" ("val1name", "1") nil ... ) |
326 | // x x |
327 | // then this would return 42 to match up those two. |
328 | function mime_match_parenthesis ($pos, $structure) { |
329 | $char = substr($structure, $pos, 1); |
330 | |
331 | // ignore all extra characters |
332 | while ($pos < strlen($structure)) { |
333 | $pos++; |
334 | $char = substr($structure, $pos, 1); |
335 | if ($char == ")") { |
336 | return $pos; |
337 | } else if ($char == "(") { |
338 | $pos = mime_match_parenthesis ($pos, $structure); |
339 | } |
d4467150 |
340 | } |
8beafbbc |
341 | } |
d4467150 |
342 | |
8beafbbc |
343 | function mime_fetch_body ($imap_stream, $id, $ent_id) { |
344 | // do a bit of error correction. If we couldn't find the entity id, just guess |
345 | // that it is the first one. That is usually the case anyway. |
346 | if (!$ent_id) $ent_id = 1; |
347 | |
348 | fputs ($imap_stream, "a001 FETCH $id BODY[$ent_id]\r\n"); |
e79bed1b |
349 | $topline = fgets ($imap_stream, 1024); |
350 | $size = substr ($topline, strpos($topline, "{")+1); |
351 | $size = substr ($size, 0, strpos($size, "}")); |
352 | $read = fread ($imap_stream, $size); |
353 | return $read; |
d4467150 |
354 | } |
355 | |
8beafbbc |
356 | /* -[ END MIME DECODING ]----------------------------------------------------------- */ |
d4467150 |
357 | |
aceb0d5c |
358 | |
d4467150 |
359 | |
8beafbbc |
360 | /** This is the first function called. It decides if this is a multipart |
361 | message or if it should be handled as a single entity |
4809f489 |
362 | **/ |
8beafbbc |
363 | function decodeMime ($body, $header) { |
364 | global $username, $key, $imapServerAddress, $imapPort; |
365 | $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
366 | sqimap_mailbox_select($imap_stream, $header->mailbox); |
367 | |
368 | return mime_structure ($imap_stream, $header); |
369 | } |
b1dadc61 |
370 | |
cbcf32f6 |
371 | // This is here for debugging purposese. It will print out a list |
372 | // of all the entity IDs that are in the $message object. |
ea48eb25 |
373 | function listEntities ($message) { |
374 | if ($message) { |
cbcf32f6 |
375 | if ($message->header->entity_id) |
376 | echo "<tt>" . $message->header->entity_id . " : " . $message->header->type0 . "/" . $message->header->type1 . "<br>"; |
377 | for ($i = 0; $message->entities[$i]; $i++) { |
378 | $msg = listEntities($message->entities[$i], $ent_id); |
379 | if ($msg) |
380 | return $msg; |
381 | } |
ea48eb25 |
382 | } |
383 | } |
384 | |
cbcf32f6 |
385 | // returns a $message object for a particular entity id |
8beafbbc |
386 | function getEntity ($message, $ent_id) { |
387 | if ($message) { |
ea48eb25 |
388 | if ($message->header->entity_id == $ent_id && strlen($ent_id) == strlen($message->header->entity_id)) { |
8beafbbc |
389 | return $message; |
b1dadc61 |
390 | } else { |
8beafbbc |
391 | for ($i = 0; $message->entities[$i]; $i++) { |
392 | $msg = getEntity ($message->entities[$i], $ent_id); |
393 | if ($msg) |
394 | return $msg; |
b1dadc61 |
395 | } |
8beafbbc |
396 | } |
397 | } |
398 | } |
399 | |
cbcf32f6 |
400 | // figures out what entity to display and returns the $message object |
401 | // for that entity. |
8beafbbc |
402 | function findDisplayEntity ($message) { |
403 | if ($message) { |
404 | if ($message->header->type0 == "text") { |
405 | if ($message->header->type1 == "plain" || |
406 | $message->header->type1 == "html") { |
407 | return $message->header->entity_id; |
408 | } |
409 | } else { |
410 | for ($i=0; $message->entities[$i]; $i++) { |
411 | return findDisplayEntity($message->entities[$i]); |
412 | } |
413 | } |
d4467150 |
414 | } |
b1dadc61 |
415 | } |
8405ee35 |
416 | |
d068c0ec |
417 | /** This returns a parsed string called $body. That string can then |
418 | be displayed as the actual message in the HTML. It contains |
419 | everything needed, including HTML Tags, Attachments at the |
420 | bottom, etc. |
4809f489 |
421 | **/ |
a8648d75 |
422 | function formatBody($message, $color, $wrap_at) { |
cbcf32f6 |
423 | // this if statement checks for the entity to show as the |
424 | // primary message. To add more of them, just put them in the |
425 | // order that is their priority. |
8beafbbc |
426 | global $username, $key, $imapServerAddress, $imapPort; |
427 | |
8beafbbc |
428 | $id = $message->header->id; |
429 | $urlmailbox = urlencode($message->header->mailbox); |
430 | |
431 | $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
432 | sqimap_mailbox_select($imap_stream, $message->header->mailbox); |
433 | |
434 | $ent_num = findDisplayEntity ($message); |
435 | $body = mime_fetch_body ($imap_stream, $id, $ent_num); |
8405ee35 |
436 | |
cbcf32f6 |
437 | // If there are other types that shouldn't be formatted, add |
438 | // them here |
439 | if ($message->header->type1 != "html") { |
17ce8467 |
440 | $body = translateText($body, $wrap_at, $charset); |
cbcf32f6 |
441 | } |
78509c54 |
442 | |
9f2215a1 |
443 | $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 |
444 | |
b1dadc61 |
445 | /** Display the ATTACHMENTS: message if there's more than one part **/ |
8beafbbc |
446 | if ($message->entities) { |
7831268e |
447 | $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=4 BORDER=0><TR><TD BGCOLOR=\"$color[0]\">"; |
448 | $body .= "<TT><B>ATTACHMENTS:</B></TT>"; |
449 | $body .= "</TD></TR><TR><TD BGCOLOR=\"$color[0]\">"; |
b1dadc61 |
450 | $num = 0; |
451 | |
8beafbbc |
452 | /** make this recurisve at some point **/ |
453 | $body .= formatAttachments ($message, $ent_num, $message->header->mailbox, $id); |
7831268e |
454 | $body .= "</TD></TR></TABLE>"; |
8405ee35 |
455 | } |
d4467150 |
456 | return $body; |
457 | } |
458 | |
8beafbbc |
459 | // A recursive function that returns a list of attachments with links |
460 | // to where to download these attachments |
461 | function formatAttachments ($message, $ent_id, $mailbox, $id) { |
462 | if ($message) { |
463 | if (!$message->entities) { |
464 | $type0 = strtolower($message->header->type0); |
465 | $type1 = strtolower($message->header->type1); |
466 | |
467 | if ($message->header->entity_id != $ent_id) { |
468 | $filename = $message->header->filename; |
469 | if (trim($filename) == "") { |
ea48eb25 |
470 | $display_filename = "untitled-".$message->header->entity_id; |
8beafbbc |
471 | } else { |
472 | $display_filename = $filename; |
473 | } |
474 | |
475 | $urlMailbox = urlencode($mailbox); |
476 | $ent = urlencode($message->header->entity_id); |
ea48eb25 |
477 | $body .= "<TT> <A HREF=\"../src/download.php?passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent\">" . $display_filename . "</A> (TYPE: $type0/$type1)"; |
478 | if ($message->header->description) |
479 | $body .= " <b>" . htmlspecialchars($message->header->description)."</b>"; |
480 | $body .= "</TT><BR>"; |
8beafbbc |
481 | $num++; |
482 | } |
483 | return $body; |
484 | } else { |
485 | for ($i = 0; $i < count($message->entities); $i++) { |
486 | $body .= formatAttachments ($message->entities[$i], $ent_id, $mailbox, $id); |
487 | } |
488 | return $body; |
489 | } |
490 | } |
491 | } |
4809f489 |
492 | |
493 | |
494 | /** this function decodes the body depending on the encoding type. **/ |
d4467150 |
495 | function decodeBody($body, $encoding) { |
496 | $encoding = strtolower($encoding); |
7831268e |
497 | |
ef3f274f |
498 | if ($encoding == "quoted-printable") { |
499 | $body = quoted_printable_decode($body); |
db87f79c |
500 | |
ef3f274f |
501 | while (ereg("=\n", $body)) |
502 | $body = ereg_replace ("=\n", "", $body); |
97be2168 |
503 | } else if ($encoding == "base64") { |
ef3f274f |
504 | $body = base64_decode($body); |
d4467150 |
505 | } |
ef3f274f |
506 | |
507 | // All other encodings are returned raw. |
508 | return $body; |
aceb0d5c |
509 | } |
a4c2cd49 |
510 | |
511 | |
512 | // This functions decode strings that is encoded according to |
513 | // RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text). |
2e434774 |
514 | function decodeHeader ($string) { |
1fd97780 |
515 | if (eregi('=\?([^?]+)\?(q|b)\?([^?]+)\?=', |
a4c2cd49 |
516 | $string, $res)) { |
1fd97780 |
517 | if (ucfirst($res[2]) == "B") { |
518 | $replace = base64_decode($res[3]); |
a4c2cd49 |
519 | } else { |
1fd97780 |
520 | $replace = ereg_replace("_", " ", $res[3]); |
a4c2cd49 |
521 | $replace = quoted_printable_decode($replace); |
522 | } |
523 | |
1fd97780 |
524 | $replace = charset_decode ($res[1], $replace); |
a4c2cd49 |
525 | |
526 | $string = eregi_replace |
1fd97780 |
527 | ('=\?([^?]+)\?(q|b)\?([^?]+)\?=', |
a4c2cd49 |
528 | $replace, $string); |
2e434774 |
529 | // In case there should be more encoding in the string: recurse |
530 | return (decodeHeader($string)); |
a4c2cd49 |
531 | } else |
532 | return ($string); |
533 | } |
534 | |
c3084273 |
535 | // Encode a string according to RFC 1522 for use in headers if it |
536 | // contains 8-bit characters |
537 | function encodeHeader ($string) { |
538 | global $default_charset; |
539 | |
540 | // Encode only if the string contains 8-bit characters |
541 | if (ereg("[\200-\377]", $string)) { |
542 | $newstring = "=?$default_charset?Q?"; |
543 | $newstring .= str_replace(" ", "_", $string); |
544 | |
545 | while (ereg("([\200-\377])", $newstring, $regs)) { |
546 | $replace = $regs[1]; |
547 | $insert = "=" . bin2hex($replace); |
548 | $newstring = str_replace($replace, $insert, $newstring); |
549 | } |
550 | |
551 | $newstring .= "?="; |
552 | |
553 | return $newstring; |
554 | } |
555 | |
556 | return $string; |
557 | } |
558 | |
9f9d7d28 |
559 | ?> |