Added a much better fix for the IE/SSL problem. No security hazards
[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
fde32e3f 26 var $type0, $type1, $boundary, $charset, $encoding, $size;
8beafbbc 27 var $to, $from, $date, $cc, $bcc, $reply_to, $subject;
ea48eb25 28 var $id, $mailbox, $description;
e4a256af 29 var $entity_id, $message_id, $charset;
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);
254925d1 62 $response = substr($read, 0, 4);
63 while ($response != "a001") {
3e1266ef 64 $bodystructure .= $read;
254925d1 65 $read = fgets ($imap_stream, 10000);
66 $response = substr($read, 0, 4);
67 }
22ef7536 68 // $read = strtolower($bodystructure);
69 $read = $bodystructure;
8beafbbc 70
04632dbc 71 if ($debug_mime) echo "<tt>$read</tt><br><br>\n";
8beafbbc 72 // isolate the body structure and remove beginning and end parenthesis
22ef7536 73 $read = trim(substr ($read, strpos(strtolower($read), "bodystructure") + 13));
ea48eb25 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 }
8beafbbc 81
04632dbc 82 if ($debug_mime) echo "<tt>$read</tt><br><br>\n";
e79bed1b 83
85daa3ad 84 $msg = mime_parse_structure ($read, 0);
8beafbbc 85 $msg->header = $header;
86 return $msg;
87 }
88
cbcf32f6 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()
8beafbbc 96 function mime_parse_structure ($structure, $ent_id) {
e79bed1b 97 global $debug_mime;
04632dbc 98 if ($debug_mime) echo "<font color=008800><tt>START: mime_parse_structure()</tt></font><br>\n";
8beafbbc 99 $msg = new message();
100 if (substr($structure, 0, 1) == "(") {
101 $ent_id = mime_new_element_level($ent_id);
102 $start = $end = -1;
ea48eb25 103 if ($debug_mime) echo "<br><font color=0000aa><tt>$structure</tt></font><br>";
8beafbbc 104 do {
e79bed1b 105 if ($debug_mime) echo "<font color=008800><tt>Found entity...</tt></font><br>";
8beafbbc 106 $start = $end+1;
107 $end = mime_match_parenthesis ($start, $structure);
108
109 $element = substr($structure, $start+1, ($end - $start)-1);
ea48eb25 110 $ent_id = mime_increment_id ($ent_id);
8beafbbc 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
e79bed1b 116 if ($debug_mime) echo "<br><font color=0000aa><tt>$structure</tt></font><br>";
ea48eb25 117 $msg = mime_get_element (&$structure, $msg, $ent_id);
e79bed1b 118 if ($debug_mime) echo "<br>";
8beafbbc 119 }
120 return $msg;
e79bed1b 121 if ($debug_mime) echo "<font color=008800><tt>&nbsp;&nbsp;END: mime_parse_structure()</tt></font><br>";
8beafbbc 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) {
ea48eb25 128 global $debug_mime;
8beafbbc 129 if (strpos($id, ".")) {
130 $first = substr($id, 0, strrpos($id, "."));
ea48eb25 131 $last = substr($id, strrpos($id, ".")+1);
8beafbbc 132 $last++;
ea48eb25 133 $new = $first . "." .$last;
8beafbbc 134 } else {
135 $new = $id + 1;
136 }
ea48eb25 137 if ($debug_mime) echo "<b>INCREMENT: $new</b><br>";
8beafbbc 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) {
ea48eb25 148 if (!$id) $id = 0;
149 else $id = $id . ".0";
150
8beafbbc 151 return $id;
152 }
153
ea48eb25 154 function mime_get_element (&$structure, $msg, $ent_id) {
e79bed1b 155 global $debug_mime;
8beafbbc 156 $elem_num = 1;
ea48eb25 157 $msg->header = new msg_header();
158 $msg->header->entity_id = $ent_id;
8beafbbc 159
160 while (strlen($structure) > 0) {
161 $structure = trim($structure);
162 $char = substr($structure, 0, 1);
163
e86ab872 164 if (strtolower(substr($structure, 0, 3)) == "nil") {
8beafbbc 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);
aceb0d5c 191 }
8beafbbc 192 $structure = substr($structure, strlen($text));
aceb0d5c 193 }
e79bed1b 194 if ($debug_mime) echo "<tt>$elem_num : $text</tt><br>";
8beafbbc 195
196 // This is where all the text parts get put into the header
197 switch ($elem_num) {
198 case 1:
22ef7536 199 $msg->header->type0 = strtolower($text);
200 if ($debug_mime) echo "<tt>type0 = ".strtolower($text)."</tt><br>";
8beafbbc 201 break;
202 case 2:
22ef7536 203 $msg->header->type1 = strtolower($text);
204 if ($debug_mime) echo "<tt>type1 = ".strtolower($text)."</tt><br>";
8beafbbc 205 break;
ea48eb25 206 case 5:
207 $msg->header->description = $text;
208 if ($debug_mime) echo "<tt>description = $text</tt><br>";
209 break;
8beafbbc 210 case 6:
22ef7536 211 $msg->header->encoding = strtolower($text);
212 if ($debug_mime) echo "<tt>encoding = ".strtolower($text)."</tt><br>";
8beafbbc 213 break;
214 case 7:
ea48eb25 215 $msg->header->size = $text;
e79bed1b 216 if ($debug_mime) echo "<tt>size = $text</tt><br>";
8beafbbc 217 break;
218 default:
ea48eb25 219 if ($msg->header->type0 == "text" && $elem_num == 8) {
cbcf32f6 220 // This is a plain text message, so lets get the number of lines
221 // that it contains.
ea48eb25 222 $msg->header->num_lines = $text;
e79bed1b 223 if ($debug_mime) echo "<tt>num_lines = $text</tt><br>";
cbcf32f6 224
ea48eb25 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);
cbcf32f6 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.
ea48eb25 238 if (substr($structure, 1, 1) != "(")
239 $m->header->entity_id = mime_increment_id(mime_new_element_level($ent_id));
cbcf32f6 240
241 // Now we'll go through and reformat the results.
ea48eb25 242 if ($m->entities) {
243 for ($i=0; $i < count($m->entities); $i++) {
ea48eb25 244 $msg->addEntity($m->entities[$i]);
245 }
246 } else {
ea48eb25 247 $msg->addEntity($m);
248 }
249 $structure = "";
250 }
8beafbbc 251 }
252 break;
253 }
254 $elem_num++;
255 $text = "";
256 }
257 // loop through the additional properties and put those in the various headers
ea48eb25 258 if ($msg->header->type0 != "message") {
cbcf32f6 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 }
ea48eb25 263 }
e4a256af 264
ea48eb25 265 return $msg;
8beafbbc 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.
cbcf32f6 273 // Funny how that happens some times.
8beafbbc 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) {
e79bed1b 283 global $debug_mime;
8beafbbc 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);
22ef7536 310 $props[$k]["name"] = strtolower($tmp);
8beafbbc 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);
ea48eb25 324 return $props;
8beafbbc 325 } else {
326 return $props;
7831268e 327 }
8beafbbc 328 }
329 }
7831268e 330
8beafbbc 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
5ffe5a7e 340 // If inside of a string, skip string -- Boundary IDs and other
341 // things can have ) in them.
8beafbbc 342 while ($pos < strlen($structure)) {
343 $pos++;
344 $char = substr($structure, $pos, 1);
345 if ($char == ")") {
346 return $pos;
5ffe5a7e 347 } else if ($char == '"') {
348 $pos ++;
349 while (substr($structure, $pos, 1) != '"' &&
350 $pos < strlen($structure)) {
351 $pos ++;
352 }
8beafbbc 353 } else if ($char == "(") {
354 $pos = mime_match_parenthesis ($pos, $structure);
355 }
d4467150 356 }
8beafbbc 357 }
d4467150 358
8beafbbc 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
4c5da285 364 fputs ($imap_stream, "a010 FETCH $id BODY[$ent_id]\r\n");
365 $data = sqimap_read_data ($imap_stream, 'a010', true, $response, $message);
2d32cb9f 366 $topline = array_shift($data);
367 while (! ereg('\* [0-9]+ FETCH ', $topline) && data)
368 $topline = array_shift($data);
6ff90ce9 369 $wholemessage = implode('', $data);
2d32cb9f 370
441f2d33 371 if (ereg('\{([^\}]*)\}', $topline, $regs)) {
2d32cb9f 372 return substr($wholemessage, 0, $regs[1]);
441f2d33 373 }
374 else if (ereg('"([^"]*)"', $topline, $regs)) {
375 return $regs[1];
376 }
2d32cb9f 377
378 $str = "Body retrival error. Please report this bug!\n";
379 $str .= "Response: $response\n";
380 $str .= "Message: $message\n";
381 $str .= "FETCH line: $topline";
382 $str .= "---------------\n$wholemessage";
383 foreach ($data as $d)
384 {
385 $str .= htmlspecialchars($d) . "\n";
386 }
387 return $str;
388
441f2d33 389 return "Body retrival error, please report this bug!\n\nTop line is \"$topline\"\n";
d4467150 390 }
391
beb9e459 392 function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
393 // do a bit of error correction. If we couldn't find the entity id, just guess
394 // that it is the first one. That is usually the case anyway.
395 if (!$ent_id) $ent_id = 1;
396
397 fputs ($imap_stream, "a001 FETCH $id BODY[$ent_id]\r\n");
398 $cnt = 0;
399 $continue = true;
400 $read = fgets ($imap_stream,4096);
401 while (!ereg("^a001 (OK|BAD|NO)(.*)$", $read, $regs)) {
402 if (trim($read) == ")==") {
403 $read1 = $read;
404 $read = fgets ($imap_stream,4096);
405 if (ereg("^a001 (OK|BAD|NO)(.*)$", $read, $regs)) {
406 return;
407 } else {
408 echo decodeBody($read1, $encoding);
409 echo decodeBody($read, $encoding);
410 }
411 } else if ($cnt) {
412 echo decodeBody($read, $encoding);
413 }
414 $read = fgets ($imap_stream,4096);
415 $cnt++;
416 }
417 }
418
8beafbbc 419 /* -[ END MIME DECODING ]----------------------------------------------------------- */
d4467150 420
aceb0d5c 421
d4467150 422
8beafbbc 423 /** This is the first function called. It decides if this is a multipart
424 message or if it should be handled as a single entity
4809f489 425 **/
8d8ab69a 426 function decodeMime ($imap_stream, $body, $header) {
8beafbbc 427 global $username, $key, $imapServerAddress, $imapPort;
8d8ab69a 428 return mime_structure ($imap_stream, $header);
8beafbbc 429 }
b1dadc61 430
cbcf32f6 431 // This is here for debugging purposese. It will print out a list
432 // of all the entity IDs that are in the $message object.
ea48eb25 433 function listEntities ($message) {
434 if ($message) {
cbcf32f6 435 if ($message->header->entity_id)
436 echo "<tt>" . $message->header->entity_id . " : " . $message->header->type0 . "/" . $message->header->type1 . "<br>";
437 for ($i = 0; $message->entities[$i]; $i++) {
438 $msg = listEntities($message->entities[$i], $ent_id);
439 if ($msg)
440 return $msg;
441 }
ea48eb25 442 }
443 }
444
cbcf32f6 445 // returns a $message object for a particular entity id
8beafbbc 446 function getEntity ($message, $ent_id) {
447 if ($message) {
ea48eb25 448 if ($message->header->entity_id == $ent_id && strlen($ent_id) == strlen($message->header->entity_id)) {
8beafbbc 449 return $message;
b1dadc61 450 } else {
8beafbbc 451 for ($i = 0; $message->entities[$i]; $i++) {
452 $msg = getEntity ($message->entities[$i], $ent_id);
453 if ($msg)
454 return $msg;
b1dadc61 455 }
8beafbbc 456 }
457 }
458 }
459
cbcf32f6 460 // figures out what entity to display and returns the $message object
461 // for that entity.
8beafbbc 462 function findDisplayEntity ($message) {
463 if ($message) {
464 if ($message->header->type0 == "text") {
465 if ($message->header->type1 == "plain" ||
466 $message->header->type1 == "html") {
467 return $message->header->entity_id;
468 }
469 } else {
470 for ($i=0; $message->entities[$i]; $i++) {
471 return findDisplayEntity($message->entities[$i]);
472 }
473 }
d4467150 474 }
b1dadc61 475 }
8405ee35 476
d068c0ec 477 /** This returns a parsed string called $body. That string can then
478 be displayed as the actual message in the HTML. It contains
479 everything needed, including HTML Tags, Attachments at the
480 bottom, etc.
4809f489 481 **/
8d8ab69a 482 function formatBody($imap_stream, $message, $color, $wrap_at) {
cbcf32f6 483 // this if statement checks for the entity to show as the
484 // primary message. To add more of them, just put them in the
485 // order that is their priority.
a037624d 486 global $startMessage, $username, $key, $imapServerAddress, $imapPort;
8beafbbc 487
8beafbbc 488 $id = $message->header->id;
489 $urlmailbox = urlencode($message->header->mailbox);
490
e4a256af 491 // Get the right entity and redefine message to be this entity
8beafbbc 492 $ent_num = findDisplayEntity ($message);
2c252f5a 493 $body_message = getEntity($message, $ent_num);
d4ff4d67 494 if (($body_message->header->type0 == "text") ||
495 ($body_message->header->type0 == "rfc822")) {
496
497 $body = mime_fetch_body ($imap_stream, $id, $ent_num);
498 $body = decodeBody($body, $body_message->header->encoding);
499
500 // If there are other types that shouldn't be formatted, add
501 // them here
f9b3e5d9 502 if ($body_message->header->type1 != "html") {
9eea179c 503 translateText($body, $wrap_at, $body_message->header->charset);
d4ff4d67 504 }
505
bc104ef3 506 $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>";
d4ff4d67 507
508 /** Display the ATTACHMENTS: message if there's more than one part **/
dd389be5 509 $body .= "</TD></TR></TABLE>";
d4ff4d67 510 if ($message->entities) {
d4ff4d67 511 $body .= formatAttachments ($message, $ent_num, $message->header->mailbox, $id);
d4ff4d67 512 }
513 } else {
dd389be5 514 $body .= formatAttachments ($message, -1, $message->header->mailbox, $id);
8405ee35 515 }
d4467150 516 return $body;
517 }
518
8beafbbc 519 // A recursive function that returns a list of attachments with links
520 // to where to download these attachments
521 function formatAttachments ($message, $ent_id, $mailbox, $id) {
f4991a86 522 global $where, $what;
dd389be5 523 global $startMessage, $color;
524 static $ShownHTML;
525
526 if ($ShownHTML == 0)
527 {
528 $ShownHTML = 1;
529
530 $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n";
531 $body .= "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n";
532 $body .= _('Attachments') . ':';
533 $body .= "</B></TH></TR><TR><TD>\n";
534
535 $body .= "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n";
536
537 $body .= formatAttachments ($message, $ent_id, $mailbox, $id);
538
539 $body .= "</TABLE></TD></TR></TABLE>";
540
541 return $body;
542 }
543
8beafbbc 544 if ($message) {
545 if (!$message->entities) {
546 $type0 = strtolower($message->header->type0);
547 $type1 = strtolower($message->header->type1);
548
549 if ($message->header->entity_id != $ent_id) {
888c82e2 550 $filename = decodeHeader($message->header->filename);
8beafbbc 551 if (trim($filename) == "") {
ea48eb25 552 $display_filename = "untitled-".$message->header->entity_id;
8beafbbc 553 } else {
554 $display_filename = $filename;
555 }
556
557 $urlMailbox = urlencode($mailbox);
558 $ent = urlencode($message->header->entity_id);
dd389be5 559
560 $DefaultLink =
bc104ef3 561 "../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
dd389be5 562 if ($where && $what)
563 $DefaultLink .= '&where=' . urlencode($where) . '&what=' . urlencode($what);
564 $Links['download link']['text'] = _('download');
565 $Links['download link']['href'] =
bc104ef3 566 "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
dd389be5 567 $ImageURL = '';
568
569 $HookResults = do_hook("attachment $type0/$type1", $Links,
570 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
ef30bf50 571 $display_filename, $where, $what);
dd389be5 572
573 $Links = $HookResults[1];
574 $DefaultLink = $HookResults[6];
575
fde32e3f 576 $body .= '<TR><TD>&nbsp;&nbsp;</TD><TD>';
577 $body .= "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>";
6e7468f6 578 $body .= '<TD><SMALL><b>' . show_readable_size($message->header->size) .
579 '</b>&nbsp;&nbsp;</small></TD>';
fde32e3f 580 $body .= "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>";
581 $body .= '<TD><SMALL>';
ea48eb25 582 if ($message->header->description)
dd389be5 583 $body .= '<b>' . htmlspecialchars($message->header->description) . '</b>';
fde32e3f 584 $body .= '</SMALL></TD><TD><SMALL>&nbsp;';
dd389be5 585
586
587 $SkipSpaces = 1;
588 foreach ($Links as $Val)
589 {
590 if ($SkipSpaces)
591 {
592 $SkipSpaces = 0;
593 }
594 else
595 {
fde32e3f 596 $body .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
dd389be5 597 }
fde32e3f 598 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
dd389be5 599 }
600
601 unset($Links);
602
fde32e3f 603 $body .= "</SMALL></TD></TR>\n";
8beafbbc 604 }
605 return $body;
606 } else {
607 for ($i = 0; $i < count($message->entities); $i++) {
608 $body .= formatAttachments ($message->entities[$i], $ent_id, $mailbox, $id);
609 }
610 return $body;
611 }
612 }
613 }
4809f489 614
615
616 /** this function decodes the body depending on the encoding type. **/
d4467150 617 function decodeBody($body, $encoding) {
623332f3 618 $body = str_replace("\r\n", "\n", $body);
d4467150 619 $encoding = strtolower($encoding);
7831268e 620
ef3f274f 621 if ($encoding == "quoted-printable") {
622 $body = quoted_printable_decode($body);
db87f79c 623
ef3f274f 624 while (ereg("=\n", $body))
625 $body = ereg_replace ("=\n", "", $body);
97be2168 626 } else if ($encoding == "base64") {
ef3f274f 627 $body = base64_decode($body);
d4467150 628 }
ef3f274f 629
630 // All other encodings are returned raw.
631 return $body;
aceb0d5c 632 }
a4c2cd49 633
634
635 // This functions decode strings that is encoded according to
636 // RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
2e434774 637 function decodeHeader ($string) {
1fd97780 638 if (eregi('=\?([^?]+)\?(q|b)\?([^?]+)\?=',
a4c2cd49 639 $string, $res)) {
1fd97780 640 if (ucfirst($res[2]) == "B") {
641 $replace = base64_decode($res[3]);
a4c2cd49 642 } else {
1fd97780 643 $replace = ereg_replace("_", " ", $res[3]);
5155cfcb 644 // Convert lowercase Quoted Printable to uppercase for
645 // quoted_printable_decode to understand it.
b75d343f 646 while (ereg("(=([0-9][abcdef])|([abcdef][0-9])|([abcdef][abcdef]))", $replace, $res)) {
5155cfcb 647 $replace = str_replace($res[1], strtoupper($res[1]), $replace);
648 }
a4c2cd49 649 $replace = quoted_printable_decode($replace);
650 }
651
1fd97780 652 $replace = charset_decode ($res[1], $replace);
a4c2cd49 653
654 $string = eregi_replace
1fd97780 655 ('=\?([^?]+)\?(q|b)\?([^?]+)\?=',
a4c2cd49 656 $replace, $string);
2e434774 657 // In case there should be more encoding in the string: recurse
658 return (decodeHeader($string));
a4c2cd49 659 } else
660 return ($string);
661 }
662
c3084273 663 // Encode a string according to RFC 1522 for use in headers if it
bb60fa3f 664 // contains 8-bit characters or anything that looks like it should
665 // be encoded.
c3084273 666 function encodeHeader ($string) {
667 global $default_charset;
668
bb60fa3f 669 // Encode only if the string contains 8-bit characters or =?
46562113 670 if (ereg("([\200-\377]|=\\?)", $string)) {
c3084273 671 $newstring = "=?$default_charset?Q?";
c3084273 672
bb60fa3f 673 // First the special characters
674 $string = str_replace("=", "=3D", $string);
675 $string = str_replace("?", "=3F", $string);
676 $string = str_replace("_", "=5F", $string);
677 $string = str_replace(" ", "_", $string);
678
b75d343f 679 for ( $ch = 127 ; $ch <= 255 ; $ch++ ) {
680 $replace = chr($ch);
681 $insert = sprintf("=%02X", $ch);
bb60fa3f 682 $string = str_replace($replace, $insert, $string);
b75d343f 683 $ch++;
c3084273 684 }
685
bb60fa3f 686 $newstring = "=?$default_charset?Q?".$string."?=";
687
c3084273 688 return $newstring;
689 }
690
691 return $string;
692 }
693
9f9d7d28 694?>