* Added code to show text/html messages (immediately after text/plain and
[squirrelmail.git] / functions / mime.php
1 <?php
2 /** mime.php
3 **
4 ** This contains the functions necessary to detect and decode MIME
5 ** messages.
6 **
7 ** $Id$
8 **/
9
10 $debug_mime = false;
11 $mime_php = true;
12
13 if (!isset($i18n_php))
14 include "../functions/i18n.php";
15 if (!isset($imap_php))
16 include "../functions/imap.php";
17 if (!isset($config_php))
18 include "../config/config.php";
19
20
21 /** Setting up the objects that have the structure for the message **/
22
23 class msg_header {
24 /** msg_header contains generic variables for values that **/
25 /** could be in a header. **/
26
27 var $type0 = '', $type1 = '', $boundary = '', $charset = '';
28 var $encoding = '', $size = 0, $to = array(), $from = '', $date = '';
29 var $cc = array(), $bcc = array(), $reply_to = '', $subject = '';
30 var $id = 0, $mailbox = '', $description = '', $filename = '';
31 var $entity_id = 0, $message_id = 0, $name = '';
32 }
33
34 class message {
35 /** message is the object that contains messages. It is a recursive
36 object in that through the $entities variable, it can contain
37 more objects of type message. See documentation in mime.txt for
38 a better description of how this works.
39 **/
40 var $header = '';
41 var $entities = array();
42
43 function addEntity ($msg) {
44 $this->entities[] = $msg;
45 }
46 }
47
48
49
50 /* --------------------------------------------------------------------------------- */
51 /* MIME DECODING */
52 /* --------------------------------------------------------------------------------- */
53
54 // This function gets the structure of a message and stores it in the "message" class.
55 // It will return this object for use with all relevant header information and
56 // fully parsed into the standard "message" object format.
57 function mime_structure ($imap_stream, $header) {
58 global $debug_mime;
59 sqimap_messages_flag ($imap_stream, $header->id, $header->id, "Seen");
60
61 $id = $header->id;
62 fputs ($imap_stream, "a001 FETCH $id BODYSTRUCTURE\r\n");
63 //
64 // This should use sqimap_read_data instead of reading it itself
65 //
66 $read = fgets ($imap_stream, 10000);
67 $response = substr($read, 0, 4);
68 $bodystructure = "";
69 while ($response != "a001") {
70 $bodystructure .= $read;
71 $read = fgets ($imap_stream, 10000);
72 $response = substr($read, 0, 4);
73 }
74 $read = $bodystructure;
75
76 if ($debug_mime) echo "<tt>$read</tt><br><br>\n";
77 // isolate the body structure and remove beginning and end parenthesis
78 $read = trim(substr ($read, strpos(strtolower($read), "bodystructure") + 13));
79 $read = trim(substr ($read, 0, -1));
80 $end = mime_match_parenthesis(0, $read);
81 while ($end == strlen($read)-1) {
82 $read = trim(substr ($read, 0, -1));
83 $read = trim(substr ($read, 1));
84 $end = mime_match_parenthesis(0, $read);
85 }
86
87 if ($debug_mime) echo "<tt>$read</tt><br><br>\n";
88
89 $msg = mime_parse_structure ($read, 0);
90 $msg->header = $header;
91 return $msg;
92 }
93
94 // this starts the parsing of a particular structure. It is called recursively,
95 // so it can be passed different structures. It returns an object of type
96 // $message.
97 // First, it checks to see if it is a multipart message. If it is, then it
98 // handles that as it sees is necessary. If it is just a regular entity,
99 // then it parses it and adds the necessary header information (by calling out
100 // to mime_get_elements()
101 function mime_parse_structure ($structure, $ent_id) {
102 global $debug_mime;
103 if ($debug_mime) echo "<font color=008800><tt>START: mime_parse_structure()</tt></font><br>\n";
104 $msg = new message();
105 if (substr($structure, 0, 1) == "(") {
106 $ent_id = mime_new_element_level($ent_id);
107 $start = $end = -1;
108 if ($debug_mime) echo "<br><font color=0000aa><tt>$structure</tt></font><br>";
109 do {
110 if ($debug_mime) echo "<font color=008800><tt>Found entity...</tt></font><br>";
111 $start = $end+1;
112 $end = mime_match_parenthesis ($start, $structure);
113
114 $element = substr($structure, $start+1, ($end - $start)-1);
115 $ent_id = mime_increment_id ($ent_id);
116 $newmsg = mime_parse_structure ($element, $ent_id);
117 $msg->addEntity ($newmsg);
118 } while (substr($structure, $end+1, 1) == "(");
119 } else {
120 // parse the elements
121 if ($debug_mime) echo "<br><font color=0000aa><tt>$structure</tt></font><br>";
122 $msg = mime_get_element ($structure, $msg, $ent_id);
123 if ($debug_mime) echo "<br>";
124 }
125 return $msg;
126 if ($debug_mime) echo "<font color=008800><tt>&nbsp;&nbsp;END: mime_parse_structure()</tt></font><br>";
127 }
128
129 // Increments the element ID. An element id can look like any of
130 // the following: 1, 1.2, 4.3.2.4.1, etc. This function increments
131 // the last number of the element id, changing 1.2 to 1.3.
132 function mime_increment_id ($id) {
133 global $debug_mime;
134 if (strpos($id, ".")) {
135 $first = substr($id, 0, strrpos($id, "."));
136 $last = substr($id, strrpos($id, ".")+1);
137 $last++;
138 $new = $first . "." .$last;
139 } else {
140 $new = $id + 1;
141 }
142 if ($debug_mime) echo "<b>INCREMENT: $new</b><br>";
143 return $new;
144 }
145
146 // See comment for mime_increment_id().
147 // This adds another level on to the entity_id changing 1.3 to 1.3.0
148 // NOTE: 1.3.0 is not a valid element ID. It MUST be incremented
149 // before it can be used. I left it this way so as not to have
150 // to make a special case if it is the first entity_id. It
151 // always increments it, and that works fine.
152 function mime_new_element_level ($id) {
153 if (!$id) $id = 0;
154 else $id = $id . ".0";
155
156 return $id;
157 }
158
159 function mime_get_element (&$structure, $msg, $ent_id) {
160 global $debug_mime;
161 $elem_num = 1;
162 $msg->header = new msg_header();
163 $msg->header->entity_id = $ent_id;
164 $properties = array();
165
166 while (strlen($structure) > 0) {
167 $structure = trim($structure);
168 $char = substr($structure, 0, 1);
169
170 if (strtolower(substr($structure, 0, 3)) == "nil") {
171 $text = "";
172 $structure = substr($structure, 3);
173 } else if ($char == "\"") {
174 // loop through until we find the matching quote, and return that as a string
175 $pos = 1;
176 $char = substr($structure, $pos, 1);
177 $text = "";
178 while ($char != "\"" && $pos < strlen($structure)) {
179 $text .= $char;
180 $pos++;
181 $char = substr($structure, $pos, 1);
182 }
183 $structure = substr($structure, strlen($text) + 2);
184 } else if ($char == "(") {
185 // comment me
186 $end = mime_match_parenthesis (0, $structure);
187 $sub = substr($structure, 1, $end-1);
188 $properties = mime_get_props($properties, $sub);
189 $structure = substr($structure, strlen($sub) + 2);
190 } else {
191 // loop through until we find a space or an end parenthesis
192 $pos = 0;
193 $char = substr($structure, $pos, 1);
194 $text = "";
195 while ($char != " " && $char != ")" && $pos < strlen($structure)) {
196 $text .= $char;
197 $pos++;
198 $char = substr($structure, $pos, 1);
199 }
200 $structure = substr($structure, strlen($text));
201 }
202 if ($debug_mime) echo "<tt>$elem_num : $text</tt><br>";
203
204 // This is where all the text parts get put into the header
205 switch ($elem_num) {
206 case 1:
207 $msg->header->type0 = strtolower($text);
208 if ($debug_mime) echo "<tt>type0 = ".strtolower($text)."</tt><br>";
209 break;
210 case 2:
211 $msg->header->type1 = strtolower($text);
212 if ($debug_mime) echo "<tt>type1 = ".strtolower($text)."</tt><br>";
213 break;
214 case 5:
215 $msg->header->description = $text;
216 if ($debug_mime) echo "<tt>description = $text</tt><br>";
217 break;
218 case 6:
219 $msg->header->encoding = strtolower($text);
220 if ($debug_mime) echo "<tt>encoding = ".strtolower($text)."</tt><br>";
221 break;
222 case 7:
223 $msg->header->size = $text;
224 if ($debug_mime) echo "<tt>size = $text</tt><br>";
225 break;
226 default:
227 if ($msg->header->type0 == "text" && $elem_num == 8) {
228 // This is a plain text message, so lets get the number of lines
229 // that it contains.
230 $msg->header->num_lines = $text;
231 if ($debug_mime) echo "<tt>num_lines = $text</tt><br>";
232
233 } else if ($msg->header->type0 == "message" && $msg->header->type1 == "rfc822" && $elem_num == 8) {
234 // This is an encapsulated message, so lets start all over again and
235 // parse this message adding it on to the existing one.
236 $structure = trim($structure);
237 if (substr($structure, 0, 1) == "(") {
238 $e = mime_match_parenthesis (0, $structure);
239 $structure = substr($structure, 0, $e);
240 $structure = substr($structure, 1);
241 $m = mime_parse_structure($structure, $msg->header->entity_id);
242
243 // the following conditional is there to correct a bug that wasn't
244 // incrementing the entity IDs correctly because of the special case
245 // that message/rfc822 is. This fixes it fine.
246 if (substr($structure, 1, 1) != "(")
247 $m->header->entity_id = mime_increment_id(mime_new_element_level($ent_id));
248
249 // Now we'll go through and reformat the results.
250 if ($m->entities) {
251 for ($i=0; $i < count($m->entities); $i++) {
252 $msg->addEntity($m->entities[$i]);
253 }
254 } else {
255 $msg->addEntity($m);
256 }
257 $structure = "";
258 }
259 }
260 break;
261 }
262 $elem_num++;
263 $text = "";
264 }
265 // loop through the additional properties and put those in the various headers
266 if ($msg->header->type0 != "message") {
267 for ($i=0; $i < count($properties); $i++) {
268 $msg->header->{$properties[$i]["name"]} = $properties[$i]["value"];
269 if ($debug_mime) echo "<tt>".$properties[$i]["name"]." = " . $properties[$i]["value"] . "</tt><br>";
270 }
271 }
272
273 return $msg;
274 }
275
276 // I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't
277 // figure out how to do this part, so I decided to go to bed. I woke up
278 // in the morning and had a flash of insight. I went to the white-board
279 // and scribbled it out, then spent a bit programming it, and this is the
280 // result. Nothing complicated, but I think my brain was fried yesterday.
281 // Funny how that happens some times.
282 //
283 // This gets properties in a nested parenthesisized list. For example,
284 // this would get passed something like: ("attachment" ("filename" "luke.tar.gz"))
285 // This returns an array called $props with all paired up properties.
286 // It ignores the "attachment" for now, maybe that should change later
287 // down the road. In this case, what is returned is:
288 // $props[0]["name"] = "filename";
289 // $props[0]["value"] = "luke.tar.gz";
290 function mime_get_props ($props, $structure) {
291 global $debug_mime;
292 while (strlen($structure) > 0) {
293 $structure = trim($structure);
294 $char = substr($structure, 0, 1);
295
296 if ($char == "\"") {
297 $pos = 1;
298 $char = substr($structure, $pos, 1);
299 $tmp = "";
300 while ($char != "\"" && $pos < strlen($structure)) {
301 $tmp .= $char;
302 $pos++;
303 $char = substr($structure, $pos, 1);
304 }
305 $structure = trim(substr($structure, strlen($tmp) + 2));
306 $char = substr($structure, 0, 1);
307
308 if ($char == "\"") {
309 $pos = 1;
310 $char = substr($structure, $pos, 1);
311 $value = "";
312 while ($char != "\"" && $pos < strlen($structure)) {
313 $value .= $char;
314 $pos++;
315 $char = substr($structure, $pos, 1);
316 }
317 $structure = trim(substr($structure, strlen($tmp) + 2));
318
319 $k = count($props);
320 $props[$k]["name"] = strtolower($tmp);
321 $props[$k]["value"] = $value;
322 } else if ($char == "(") {
323 $end = mime_match_parenthesis (0, $structure);
324 $sub = substr($structure, 1, $end-1);
325 if (! isset($props))
326 $props = array();
327 $props = mime_get_props($props, $sub);
328 $structure = substr($structure, strlen($sub) + 2);
329 }
330 return $props;
331 } else if ($char == "(") {
332 $end = mime_match_parenthesis (0, $structure);
333 $sub = substr($structure, 1, $end-1);
334 $props = mime_get_props($props, $sub);
335 $structure = substr($structure, strlen($sub) + 2);
336 return $props;
337 } else {
338 return $props;
339 }
340 }
341 }
342
343 // Matches parenthesis. It will return the position of the matching
344 // parenthesis in $structure. For instance, if $structure was:
345 // ("text" "plain" ("val1name", "1") nil ... )
346 // x x
347 // then this would return 42 to match up those two.
348 function mime_match_parenthesis ($pos, $structure) {
349 $char = substr($structure, $pos, 1);
350
351 // ignore all extra characters
352 // If inside of a string, skip string -- Boundary IDs and other
353 // things can have ) in them.
354 if ($char != '(')
355 return strlen($structure);
356 while ($pos < strlen($structure)) {
357 $pos++;
358 $char = substr($structure, $pos, 1);
359 if ($char == ")") {
360 return $pos;
361 } else if ($char == '"') {
362 $pos ++;
363 while (substr($structure, $pos, 1) != '"' &&
364 $pos < strlen($structure)) {
365 if (substr($structure, $pos, 2) == '\\"')
366 $pos ++;
367 elseif (substr($structure, $pos, 2) == '\\\\')
368 $pos ++;
369 $pos ++;
370 }
371 } else if ($char == "(") {
372 $pos = mime_match_parenthesis ($pos, $structure);
373 }
374 }
375 echo "Error decoding mime structure. Report this as a bug!<br>\n";
376 return $pos;
377 }
378
379 function mime_fetch_body ($imap_stream, $id, $ent_id) {
380 // do a bit of error correction. If we couldn't find the entity id, just guess
381 // that it is the first one. That is usually the case anyway.
382 if (!$ent_id) $ent_id = 1;
383
384 fputs ($imap_stream, "a010 FETCH $id BODY[$ent_id]\r\n");
385 $data = sqimap_read_data ($imap_stream, 'a010', true, $response, $message);
386 $topline = array_shift($data);
387 while (! ereg('\\* [0-9]+ FETCH ', $topline) && data)
388 $topline = array_shift($data);
389 $wholemessage = implode('', $data);
390
391 if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
392 return substr($wholemessage, 0, $regs[1]);
393 }
394 else if (ereg('"([^"]*)"', $topline, $regs)) {
395 return $regs[1];
396 }
397
398 $str = "Body retrival error. Please report this bug!\n";
399 $str .= "Response: $response\n";
400 $str .= "Message: $message\n";
401 $str .= "FETCH line: $topline";
402 $str .= "---------------\n$wholemessage";
403 foreach ($data as $d)
404 {
405 $str .= htmlspecialchars($d) . "\n";
406 }
407 return $str;
408
409 return "Body retrival error, please report this bug!\n\nTop line is \"$topline\"\n";
410 }
411
412 function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
413 // do a bit of error correction. If we couldn't find the entity id, just guess
414 // that it is the first one. That is usually the case anyway.
415 if (!$ent_id) $ent_id = 1;
416
417 fputs ($imap_stream, "a001 FETCH $id BODY[$ent_id]\r\n");
418 $cnt = 0;
419 $continue = true;
420 $read = fgets ($imap_stream,4096);
421 while (!ereg("^a001 (OK|BAD|NO)(.*)$", $read, $regs)) {
422 if (trim($read) == ")==") {
423 $read1 = $read;
424 $read = fgets ($imap_stream,4096);
425 if (ereg("^a001 (OK|BAD|NO)(.*)$", $read, $regs)) {
426 return;
427 } else {
428 echo decodeBody($read1, $encoding);
429 echo decodeBody($read, $encoding);
430 }
431 } else if ($cnt) {
432 echo decodeBody($read, $encoding);
433 }
434 $read = fgets ($imap_stream,4096);
435 $cnt++;
436 }
437 }
438
439 /* -[ END MIME DECODING ]----------------------------------------------------------- */
440
441
442
443 /** This is the first function called. It decides if this is a multipart
444 message or if it should be handled as a single entity
445 **/
446 function decodeMime ($imap_stream, &$header) {
447 global $username, $key, $imapServerAddress, $imapPort;
448 return mime_structure ($imap_stream, $header);
449 }
450
451 // This is here for debugging purposese. It will print out a list
452 // of all the entity IDs that are in the $message object.
453 function listEntities ($message) {
454 if ($message) {
455 if ($message->header->entity_id)
456 echo "<tt>" . $message->header->entity_id . " : " . $message->header->type0 . "/" . $message->header->type1 . "<br>";
457 for ($i = 0; $message->entities[$i]; $i++) {
458 $msg = listEntities($message->entities[$i], $ent_id);
459 if ($msg)
460 return $msg;
461 }
462 }
463 }
464
465 // returns a $message object for a particular entity id
466 function getEntity ($message, $ent_id) {
467 if ($message) {
468 if ($message->header->entity_id == $ent_id && strlen($ent_id) == strlen($message->header->entity_id)) {
469 return $message;
470 } else {
471 for ($i = 0; isset($message->entities[$i]); $i++) {
472 $msg = getEntity ($message->entities[$i], $ent_id);
473 if ($msg)
474 return $msg;
475 }
476 }
477 }
478 }
479
480 // figures out what entity to display and returns the $message object
481 // for that entity.
482 function findDisplayEntity ($message, $next = 'none')
483 {
484 global $show_html_default;
485
486 if (! $message)
487 return;
488
489 // Show text/plain or text/html -- the first one we find.
490 if ($message->header->type0 == 'text' &&
491 ($message->header->type1 == 'plain' ||
492 $message->header->type2 == 'html'))
493 {
494 // If the next part is an HTML version, this will
495 // all be true. Show it, if the user so desires.
496 // HTML mails this way all have entity_id of 2. 1 = text/plain
497 if ($next != 'none' &&
498 $next->header->type0 == "text" &&
499 $next->header->type1 == "html" &&
500 $next->header->entity_id == 2 &&
501 $message->header->type1 == "plain" &&
502 isset($show_html_default) &&
503 $show_html_default)
504 $message = $next;
505
506 if (isset($message->header->entity_id))
507 return $message->header->entity_id;
508 }
509 else
510 {
511 for ($i=0; $message->entities[$i]; $i++)
512 {
513 $next = 'none';
514 if (isset($message->entities[$i + 1]))
515 $next = $message->entities[$i + 1];
516 $entity = findDisplayEntity($message->entities[$i], $next);
517 if ($entity != 0)
518 return $entity;
519 }
520 }
521 return 0;
522 }
523
524 /** This returns a parsed string called $body. That string can then
525 be displayed as the actual message in the HTML. It contains
526 everything needed, including HTML Tags, Attachments at the
527 bottom, etc.
528 **/
529 function formatBody($imap_stream, $message, $color, $wrap_at) {
530 // this if statement checks for the entity to show as the
531 // primary message. To add more of them, just put them in the
532 // order that is their priority.
533 global $startMessage, $username, $key, $imapServerAddress, $imapPort;
534
535 $id = $message->header->id;
536 $urlmailbox = urlencode($message->header->mailbox);
537
538 // Get the right entity and redefine message to be this entity
539 $ent_num = findDisplayEntity ($message);
540 $body_message = getEntity($message, $ent_num);
541 if (($body_message->header->type0 == "text") ||
542 ($body_message->header->type0 == "rfc822")) {
543
544 $body = mime_fetch_body ($imap_stream, $id, $ent_num);
545 $body = decodeBody($body, $body_message->header->encoding);
546
547 // If there are other types that shouldn't be formatted, add
548 // them here
549 if ($body_message->header->type1 != "html") {
550 translateText($body, $wrap_at, $body_message->header->charset);
551 }
552
553 $body .= "<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>";
554
555 /** Display the ATTACHMENTS: message if there's more than one part **/
556 $body .= "</TD></TR></TABLE>";
557 if (isset($message->entities[0])) {
558 $body .= formatAttachments ($message, $ent_num, $message->header->mailbox, $id);
559 }
560 } else {
561 $body .= formatAttachments ($message, -1, $message->header->mailbox, $id);
562 }
563 return $body;
564 }
565
566 // A recursive function that returns a list of attachments with links
567 // to where to download these attachments
568 function formatAttachments ($message, $ent_id, $mailbox, $id) {
569 global $where, $what;
570 global $startMessage, $color;
571 static $ShownHTML = 0;
572
573 $body = "";
574 if ($ShownHTML == 0)
575 {
576 $ShownHTML = 1;
577
578 $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n";
579 $body .= "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n";
580 $body .= _("Attachments") . ':';
581 $body .= "</B></TH></TR><TR><TD>\n";
582
583 $body .= "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n";
584
585 $body .= formatAttachments ($message, $ent_id, $mailbox, $id);
586
587 $body .= "</TABLE></TD></TR></TABLE>";
588
589 return $body;
590 }
591
592 if ($message) {
593 if (!$message->entities) {
594 $type0 = strtolower($message->header->type0);
595 $type1 = strtolower($message->header->type1);
596 $name = decodeHeader($message->header->name);
597
598 if ($message->header->entity_id != $ent_id) {
599 $filename = decodeHeader($message->header->filename);
600 if (trim($filename) == "") {
601 if (trim($name) == "") {
602 $display_filename = "untitled-".$message->header->entity_id;
603 } else {
604 $display_filename = $name;
605 $filename = $name;
606 }
607 } else {
608 $display_filename = $filename;
609 }
610
611 $urlMailbox = urlencode($mailbox);
612 $ent = urlencode($message->header->entity_id);
613
614 $DefaultLink =
615 "../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
616 if ($where && $what)
617 $DefaultLink .= '&where=' . urlencode($where) . '&what=' . urlencode($what);
618 $Links['download link']['text'] = _("download");
619 $Links['download link']['href'] =
620 "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
621 $ImageURL = '';
622
623 $HookResults = do_hook("attachment $type0/$type1", $Links,
624 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
625 $display_filename, $where, $what);
626
627 $Links = $HookResults[1];
628 $DefaultLink = $HookResults[6];
629
630 $body .= '<TR><TD>&nbsp;&nbsp;</TD><TD>';
631 $body .= "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>";
632 $body .= '<TD><SMALL><b>' . show_readable_size($message->header->size) .
633 '</b>&nbsp;&nbsp;</small></TD>';
634 $body .= "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>";
635 $body .= '<TD><SMALL>';
636 if ($message->header->description)
637 $body .= '<b>' . htmlspecialchars($message->header->description) . '</b>';
638 $body .= '</SMALL></TD><TD><SMALL>&nbsp;';
639
640
641 $SkipSpaces = 1;
642 foreach ($Links as $Val)
643 {
644 if ($SkipSpaces)
645 {
646 $SkipSpaces = 0;
647 }
648 else
649 {
650 $body .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
651 }
652 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
653 }
654
655 unset($Links);
656
657 $body .= "</SMALL></TD></TR>\n";
658 }
659 return $body;
660 } else {
661 for ($i = 0; $i < count($message->entities); $i++) {
662 $body .= formatAttachments ($message->entities[$i], $ent_id, $mailbox, $id);
663 }
664 return $body;
665 }
666 }
667 }
668
669
670 /** this function decodes the body depending on the encoding type. **/
671 function decodeBody($body, $encoding) {
672 $body = str_replace("\r\n", "\n", $body);
673 $encoding = strtolower($encoding);
674
675 if ($encoding == "quoted-printable") {
676 $body = quoted_printable_decode($body);
677
678 while (ereg("=\n", $body))
679 $body = ereg_replace ("=\n", "", $body);
680 } else if ($encoding == "base64") {
681 $body = base64_decode($body);
682 }
683
684 // All other encodings are returned raw.
685 return $body;
686 }
687
688
689 // This functions decode strings that is encoded according to
690 // RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
691 function decodeHeader ($string) {
692 if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
693 $string, $res)) {
694 if (ucfirst($res[2]) == "B") {
695 $replace = base64_decode($res[3]);
696 } else {
697 $replace = ereg_replace("_", " ", $res[3]);
698 // Convert lowercase Quoted Printable to uppercase for
699 // quoted_printable_decode to understand it.
700 while (ereg("(=([0-9][abcdef])|([abcdef][0-9])|([abcdef][abcdef]))", $replace, $res)) {
701 $replace = str_replace($res[1], strtoupper($res[1]), $replace);
702 }
703 $replace = quoted_printable_decode($replace);
704 }
705
706 $replace = charset_decode ($res[1], $replace);
707
708 $string = eregi_replace
709 ('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
710 $replace, $string);
711 // In case there should be more encoding in the string: recurse
712 return (decodeHeader($string));
713 } else
714 return ($string);
715 }
716
717 // Encode a string according to RFC 1522 for use in headers if it
718 // contains 8-bit characters or anything that looks like it should
719 // be encoded.
720 function encodeHeader ($string) {
721 global $default_charset;
722
723 // Encode only if the string contains 8-bit characters or =?
724 if (ereg("([\200-\377]|=\\?)", $string)) {
725 $newstring = "=?$default_charset?Q?";
726
727 // First the special characters
728 $string = str_replace("=", "=3D", $string);
729 $string = str_replace("?", "=3F", $string);
730 $string = str_replace("_", "=5F", $string);
731 $string = str_replace(" ", "_", $string);
732
733 for ( $ch = 127 ; $ch <= 255 ; $ch++ ) {
734 $replace = chr($ch);
735 $insert = sprintf("=%02X", $ch);
736 $string = str_replace($replace, $insert, $string);
737 $ch++;
738 }
739
740 $newstring = "=?$default_charset?Q?".$string."?=";
741
742 return $newstring;
743 }
744
745 return $string;
746 }
747
748 ?>