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