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