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