* $data didn't have the $
[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 **
245a6892 7 ** $Id$
aceb0d5c 8 **/
f435778e 9
10 if (defined('mime_php'))
11 return;
12 define('mime_php', true);
aceb0d5c 13
f435778e 14 global $debug_mime;
e79bed1b 15 $debug_mime = false;
f435778e 16
f435778e 17 include "../functions/imap.php";
8beafbbc 18
cbcf32f6 19 /** Setting up the objects that have the structure for the message **/
8beafbbc 20
21 class msg_header {
22 /** msg_header contains generic variables for values that **/
23 /** could be in a header. **/
24
245a6892 25 var $type0 = '', $type1 = '', $boundary = '', $charset = '';
40023540 26 var $encoding = '', $size = 0, $to = array(), $from = '', $date = '';
27 var $cc = array(), $bcc = array(), $reply_to = '', $subject = '';
61423189 28 var $id = 0, $mailbox = '', $description = '', $filename = '';
66e1a00e 29 var $entity_id = 0, $message_id = 0, $name = '';
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 **/
61423189 38 var $header = '';
2df6ca53 39 var $entities = array();
8beafbbc 40
41 function addEntity ($msg) {
61423189 42 $this->entities[] = $msg;
8beafbbc 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");
245a6892 61 //
62 // This should use sqimap_read_data instead of reading it itself
63 //
e79bed1b 64 $read = fgets ($imap_stream, 10000);
254925d1 65 $response = substr($read, 0, 4);
245a6892 66 $bodystructure = "";
254925d1 67 while ($response != "a001") {
3e1266ef 68 $bodystructure .= $read;
254925d1 69 $read = fgets ($imap_stream, 10000);
70 $response = substr($read, 0, 4);
71 }
22ef7536 72 $read = $bodystructure;
8beafbbc 73
04632dbc 74 if ($debug_mime) echo "<tt>$read</tt><br><br>\n";
8beafbbc 75 // isolate the body structure and remove beginning and end parenthesis
22ef7536 76 $read = trim(substr ($read, strpos(strtolower($read), "bodystructure") + 13));
ea48eb25 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 }
8beafbbc 84
04632dbc 85 if ($debug_mime) echo "<tt>$read</tt><br><br>\n";
e79bed1b 86
85daa3ad 87 $msg = mime_parse_structure ($read, 0);
8beafbbc 88 $msg->header = $header;
89 return $msg;
90 }
91
cbcf32f6 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()
8beafbbc 99 function mime_parse_structure ($structure, $ent_id) {
e79bed1b 100 global $debug_mime;
04632dbc 101 if ($debug_mime) echo "<font color=008800><tt>START: mime_parse_structure()</tt></font><br>\n";
8beafbbc 102 $msg = new message();
103 if (substr($structure, 0, 1) == "(") {
104 $ent_id = mime_new_element_level($ent_id);
105 $start = $end = -1;
ea48eb25 106 if ($debug_mime) echo "<br><font color=0000aa><tt>$structure</tt></font><br>";
8beafbbc 107 do {
e79bed1b 108 if ($debug_mime) echo "<font color=008800><tt>Found entity...</tt></font><br>";
8beafbbc 109 $start = $end+1;
110 $end = mime_match_parenthesis ($start, $structure);
111
112 $element = substr($structure, $start+1, ($end - $start)-1);
ea48eb25 113 $ent_id = mime_increment_id ($ent_id);
8beafbbc 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
e79bed1b 119 if ($debug_mime) echo "<br><font color=0000aa><tt>$structure</tt></font><br>";
090595e1 120 $msg = mime_get_element ($structure, $msg, $ent_id);
e79bed1b 121 if ($debug_mime) echo "<br>";
8beafbbc 122 }
123 return $msg;
e79bed1b 124 if ($debug_mime) echo "<font color=008800><tt>&nbsp;&nbsp;END: mime_parse_structure()</tt></font><br>";
8beafbbc 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) {
ea48eb25 131 global $debug_mime;
8beafbbc 132 if (strpos($id, ".")) {
133 $first = substr($id, 0, strrpos($id, "."));
ea48eb25 134 $last = substr($id, strrpos($id, ".")+1);
8beafbbc 135 $last++;
ea48eb25 136 $new = $first . "." .$last;
8beafbbc 137 } else {
138 $new = $id + 1;
139 }
ea48eb25 140 if ($debug_mime) echo "<b>INCREMENT: $new</b><br>";
8beafbbc 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) {
ea48eb25 151 if (!$id) $id = 0;
152 else $id = $id . ".0";
153
8beafbbc 154 return $id;
155 }
156
ea48eb25 157 function mime_get_element (&$structure, $msg, $ent_id) {
e79bed1b 158 global $debug_mime;
8beafbbc 159 $elem_num = 1;
ea48eb25 160 $msg->header = new msg_header();
161 $msg->header->entity_id = $ent_id;
4bbe6ccc 162 $properties = array();
8beafbbc 163
164 while (strlen($structure) > 0) {
165 $structure = trim($structure);
166 $char = substr($structure, 0, 1);
167
e86ab872 168 if (strtolower(substr($structure, 0, 3)) == "nil") {
8beafbbc 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);
245a6892 175 $text = "";
8beafbbc 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);
61d9e885 192 $text = "";
8beafbbc 193 while ($char != " " && $char != ")" && $pos < strlen($structure)) {
194 $text .= $char;
195 $pos++;
196 $char = substr($structure, $pos, 1);
aceb0d5c 197 }
8beafbbc 198 $structure = substr($structure, strlen($text));
aceb0d5c 199 }
e79bed1b 200 if ($debug_mime) echo "<tt>$elem_num : $text</tt><br>";
8beafbbc 201
202 // This is where all the text parts get put into the header
203 switch ($elem_num) {
204 case 1:
22ef7536 205 $msg->header->type0 = strtolower($text);
206 if ($debug_mime) echo "<tt>type0 = ".strtolower($text)."</tt><br>";
8beafbbc 207 break;
208 case 2:
22ef7536 209 $msg->header->type1 = strtolower($text);
210 if ($debug_mime) echo "<tt>type1 = ".strtolower($text)."</tt><br>";
8beafbbc 211 break;
ea48eb25 212 case 5:
213 $msg->header->description = $text;
214 if ($debug_mime) echo "<tt>description = $text</tt><br>";
215 break;
8beafbbc 216 case 6:
22ef7536 217 $msg->header->encoding = strtolower($text);
218 if ($debug_mime) echo "<tt>encoding = ".strtolower($text)."</tt><br>";
8beafbbc 219 break;
220 case 7:
ea48eb25 221 $msg->header->size = $text;
e79bed1b 222 if ($debug_mime) echo "<tt>size = $text</tt><br>";
8beafbbc 223 break;
224 default:
ea48eb25 225 if ($msg->header->type0 == "text" && $elem_num == 8) {
cbcf32f6 226 // This is a plain text message, so lets get the number of lines
227 // that it contains.
ea48eb25 228 $msg->header->num_lines = $text;
e79bed1b 229 if ($debug_mime) echo "<tt>num_lines = $text</tt><br>";
cbcf32f6 230
ea48eb25 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);
cbcf32f6 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.
ea48eb25 244 if (substr($structure, 1, 1) != "(")
245 $m->header->entity_id = mime_increment_id(mime_new_element_level($ent_id));
cbcf32f6 246
247 // Now we'll go through and reformat the results.
ea48eb25 248 if ($m->entities) {
249 for ($i=0; $i < count($m->entities); $i++) {
ea48eb25 250 $msg->addEntity($m->entities[$i]);
251 }
252 } else {
ea48eb25 253 $msg->addEntity($m);
254 }
255 $structure = "";
256 }
8beafbbc 257 }
258 break;
259 }
260 $elem_num++;
261 $text = "";
262 }
263 // loop through the additional properties and put those in the various headers
ea48eb25 264 if ($msg->header->type0 != "message") {
cbcf32f6 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 }
ea48eb25 269 }
e4a256af 270
ea48eb25 271 return $msg;
8beafbbc 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.
cbcf32f6 279 // Funny how that happens some times.
8beafbbc 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) {
e79bed1b 289 global $debug_mime;
8beafbbc 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);
245a6892 297 $tmp = "";
8beafbbc 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);
245a6892 309 $value = "";
8beafbbc 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);
22ef7536 318 $props[$k]["name"] = strtolower($tmp);
8beafbbc 319 $props[$k]["value"] = $value;
320 } else if ($char == "(") {
321 $end = mime_match_parenthesis (0, $structure);
322 $sub = substr($structure, 1, $end-1);
245a6892 323 if (! isset($props))
324 $props = array();
8beafbbc 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);
ea48eb25 334 return $props;
8beafbbc 335 } else {
336 return $props;
7831268e 337 }
8beafbbc 338 }
339 }
7831268e 340
8beafbbc 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
5ffe5a7e 350 // If inside of a string, skip string -- Boundary IDs and other
351 // things can have ) in them.
4bbe6ccc 352 if ($char != '(')
353 return strlen($structure);
8beafbbc 354 while ($pos < strlen($structure)) {
355 $pos++;
356 $char = substr($structure, $pos, 1);
357 if ($char == ")") {
358 return $pos;
5ffe5a7e 359 } else if ($char == '"') {
360 $pos ++;
361 while (substr($structure, $pos, 1) != '"' &&
362 $pos < strlen($structure)) {
377a40b2 363 if (substr($structure, $pos, 2) == '\\"')
364 $pos ++;
365 elseif (substr($structure, $pos, 2) == '\\\\')
366 $pos ++;
5ffe5a7e 367 $pos ++;
368 }
8beafbbc 369 } else if ($char == "(") {
370 $pos = mime_match_parenthesis ($pos, $structure);
371 }
d4467150 372 }
377a40b2 373 echo "Error decoding mime structure. Report this as a bug!<br>\n";
374 return $pos;
8beafbbc 375 }
d4467150 376
8beafbbc 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
4c5da285 382 fputs ($imap_stream, "a010 FETCH $id BODY[$ent_id]\r\n");
383 $data = sqimap_read_data ($imap_stream, 'a010', true, $response, $message);
2d32cb9f 384 $topline = array_shift($data);
1eb943d4 385 while (! ereg('\\* [0-9]+ FETCH ', $topline) && $data)
2d32cb9f 386 $topline = array_shift($data);
6ff90ce9 387 $wholemessage = implode('', $data);
2d32cb9f 388
146e0c45 389 if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
2d32cb9f 390 return substr($wholemessage, 0, $regs[1]);
441f2d33 391 }
392 else if (ereg('"([^"]*)"', $topline, $regs)) {
393 return $regs[1];
394 }
2d32cb9f 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
441f2d33 407 return "Body retrival error, please report this bug!\n\nTop line is \"$topline\"\n";
d4467150 408 }
409
beb9e459 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 fputs ($imap_stream, "a001 FETCH $id BODY[$ent_id]\r\n");
416 $cnt = 0;
417 $continue = true;
418 $read = fgets ($imap_stream,4096);
419 while (!ereg("^a001 (OK|BAD|NO)(.*)$", $read, $regs)) {
420 if (trim($read) == ")==") {
421 $read1 = $read;
422 $read = fgets ($imap_stream,4096);
423 if (ereg("^a001 (OK|BAD|NO)(.*)$", $read, $regs)) {
424 return;
425 } else {
426 echo decodeBody($read1, $encoding);
427 echo decodeBody($read, $encoding);
428 }
429 } else if ($cnt) {
430 echo decodeBody($read, $encoding);
431 }
432 $read = fgets ($imap_stream,4096);
433 $cnt++;
434 }
435 }
436
8beafbbc 437 /* -[ END MIME DECODING ]----------------------------------------------------------- */
d4467150 438
aceb0d5c 439
d4467150 440
8beafbbc 441 /** This is the first function called. It decides if this is a multipart
442 message or if it should be handled as a single entity
4809f489 443 **/
090595e1 444 function decodeMime ($imap_stream, &$header) {
8beafbbc 445 global $username, $key, $imapServerAddress, $imapPort;
8d8ab69a 446 return mime_structure ($imap_stream, $header);
8beafbbc 447 }
b1dadc61 448
cbcf32f6 449 // This is here for debugging purposese. It will print out a list
450 // of all the entity IDs that are in the $message object.
ea48eb25 451 function listEntities ($message) {
452 if ($message) {
cbcf32f6 453 if ($message->header->entity_id)
454 echo "<tt>" . $message->header->entity_id . " : " . $message->header->type0 . "/" . $message->header->type1 . "<br>";
455 for ($i = 0; $message->entities[$i]; $i++) {
456 $msg = listEntities($message->entities[$i], $ent_id);
457 if ($msg)
458 return $msg;
459 }
ea48eb25 460 }
461 }
462
cbcf32f6 463 // returns a $message object for a particular entity id
8beafbbc 464 function getEntity ($message, $ent_id) {
465 if ($message) {
ea48eb25 466 if ($message->header->entity_id == $ent_id && strlen($ent_id) == strlen($message->header->entity_id)) {
8beafbbc 467 return $message;
b1dadc61 468 } else {
cd928157 469 for ($i = 0; isset($message->entities[$i]); $i++) {
8beafbbc 470 $msg = getEntity ($message->entities[$i], $ent_id);
471 if ($msg)
472 return $msg;
b1dadc61 473 }
8beafbbc 474 }
475 }
476 }
477
cbcf32f6 478 // figures out what entity to display and returns the $message object
479 // for that entity.
3ae6e629 480 function findDisplayEntity ($message, $textOnly = 1, $next = 'none')
e2ab93e5 481 {
482 global $show_html_default;
483
484 if (! $message)
79a3b1e5 485 return 0;
e2ab93e5 486
487 // Show text/plain or text/html -- the first one we find.
488 if ($message->header->type0 == 'text' &&
489 ($message->header->type1 == 'plain' ||
3d6c7e96 490 $message->header->type1 == 'html'))
e2ab93e5 491 {
492 // If the next part is an HTML version, this will
493 // all be true. Show it, if the user so desires.
494 // HTML mails this way all have entity_id of 2. 1 = text/plain
495 if ($next != 'none' &&
3ae6e629 496 $textOnly == 0 &&
e2ab93e5 497 $next->header->type0 == "text" &&
498 $next->header->type1 == "html" &&
7b9592dc 499 ($next->header->entity_id == 2 ||
500 $next->header->entity_id == 1.2) &&
e2ab93e5 501 $message->header->type1 == "plain" &&
502 isset($show_html_default) &&
503 $show_html_default)
504 $message = $next;
505
506 if (isset($message->header->entity_id))
507 return $message->header->entity_id;
508 }
509 else
510 {
3d6c7e96 511 for ($i=0; isset($message->entities[$i]); $i++)
e2ab93e5 512 {
513 $next = 'none';
514 if (isset($message->entities[$i + 1]))
515 $next = $message->entities[$i + 1];
3ae6e629 516 $entity = findDisplayEntity($message->entities[$i],
517 $textOnly, $next);
e2ab93e5 518 if ($entity != 0)
519 return $entity;
520 }
521 }
522 return 0;
b1dadc61 523 }
8405ee35 524
d068c0ec 525 /** This returns a parsed string called $body. That string can then
526 be displayed as the actual message in the HTML. It contains
527 everything needed, including HTML Tags, Attachments at the
528 bottom, etc.
4809f489 529 **/
8d8ab69a 530 function formatBody($imap_stream, $message, $color, $wrap_at) {
cbcf32f6 531 // this if statement checks for the entity to show as the
532 // primary message. To add more of them, just put them in the
533 // order that is their priority.
a037624d 534 global $startMessage, $username, $key, $imapServerAddress, $imapPort;
8beafbbc 535
8beafbbc 536 $id = $message->header->id;
537 $urlmailbox = urlencode($message->header->mailbox);
538
e4a256af 539 // Get the right entity and redefine message to be this entity
3ae6e629 540 // Pass the 0 to mean that we want the 'best' viewable one
541 $ent_num = findDisplayEntity ($message, 0);
2c252f5a 542 $body_message = getEntity($message, $ent_num);
d4ff4d67 543 if (($body_message->header->type0 == "text") ||
544 ($body_message->header->type0 == "rfc822")) {
545
d51894be 546 $body = mime_fetch_body ($imap_stream, $id, $ent_num);
d4ff4d67 547 $body = decodeBody($body, $body_message->header->encoding);
548
549 // If there are other types that shouldn't be formatted, add
550 // them here
f9b3e5d9 551 if ($body_message->header->type1 != "html") {
9eea179c 552 translateText($body, $wrap_at, $body_message->header->charset);
d4ff4d67 553 }
554
7b9592dc 555 $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>";
d4ff4d67 556
557 /** Display the ATTACHMENTS: message if there's more than one part **/
dd389be5 558 $body .= "</TD></TR></TABLE>";
719534f2 559 if (isset($message->entities[0])) {
d4ff4d67 560 $body .= formatAttachments ($message, $ent_num, $message->header->mailbox, $id);
d4ff4d67 561 }
d5f34863 562 $body .= "</TD></TR></TABLE>";
d4ff4d67 563 } else {
cba164a0 564 $body = formatAttachments ($message, -1, $message->header->mailbox, $id);
8405ee35 565 }
d4467150 566 return $body;
567 }
568
8beafbbc 569 // A recursive function that returns a list of attachments with links
570 // to where to download these attachments
571 function formatAttachments ($message, $ent_id, $mailbox, $id) {
f4991a86 572 global $where, $what;
dd389be5 573 global $startMessage, $color;
719534f2 574 static $ShownHTML = 0;
dd389be5 575
61423189 576 $body = "";
dd389be5 577 if ($ShownHTML == 0)
578 {
579 $ShownHTML = 1;
580
581 $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n";
582 $body .= "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n";
701c9c6b 583 $body .= _("Attachments") . ':';
dd389be5 584 $body .= "</B></TH></TR><TR><TD>\n";
585
586 $body .= "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n";
587
588 $body .= formatAttachments ($message, $ent_id, $mailbox, $id);
589
590 $body .= "</TABLE></TD></TR></TABLE>";
591
592 return $body;
593 }
594
8beafbbc 595 if ($message) {
596 if (!$message->entities) {
597 $type0 = strtolower($message->header->type0);
598 $type1 = strtolower($message->header->type1);
66e1a00e 599 $name = decodeHeader($message->header->name);
8beafbbc 600
601 if ($message->header->entity_id != $ent_id) {
888c82e2 602 $filename = decodeHeader($message->header->filename);
8beafbbc 603 if (trim($filename) == "") {
66e1a00e 604 if (trim($name) == "") {
605 $display_filename = "untitled-".$message->header->entity_id;
606 } else {
607 $display_filename = $name;
608 $filename = $name;
609 }
8beafbbc 610 } else {
611 $display_filename = $filename;
612 }
613
614 $urlMailbox = urlencode($mailbox);
615 $ent = urlencode($message->header->entity_id);
dd389be5 616
617 $DefaultLink =
bc104ef3 618 "../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
dd389be5 619 if ($where && $what)
620 $DefaultLink .= '&where=' . urlencode($where) . '&what=' . urlencode($what);
701c9c6b 621 $Links['download link']['text'] = _("download");
dd389be5 622 $Links['download link']['href'] =
bc104ef3 623 "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
dd389be5 624 $ImageURL = '';
625
626 $HookResults = do_hook("attachment $type0/$type1", $Links,
627 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
ef30bf50 628 $display_filename, $where, $what);
dd389be5 629
630 $Links = $HookResults[1];
631 $DefaultLink = $HookResults[6];
632
fde32e3f 633 $body .= '<TR><TD>&nbsp;&nbsp;</TD><TD>';
634 $body .= "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>";
6e7468f6 635 $body .= '<TD><SMALL><b>' . show_readable_size($message->header->size) .
636 '</b>&nbsp;&nbsp;</small></TD>';
fde32e3f 637 $body .= "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>";
638 $body .= '<TD><SMALL>';
ea48eb25 639 if ($message->header->description)
dd389be5 640 $body .= '<b>' . htmlspecialchars($message->header->description) . '</b>';
fde32e3f 641 $body .= '</SMALL></TD><TD><SMALL>&nbsp;';
dd389be5 642
643
644 $SkipSpaces = 1;
645 foreach ($Links as $Val)
646 {
647 if ($SkipSpaces)
648 {
649 $SkipSpaces = 0;
650 }
651 else
652 {
fde32e3f 653 $body .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
dd389be5 654 }
fde32e3f 655 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
dd389be5 656 }
657
658 unset($Links);
659
fde32e3f 660 $body .= "</SMALL></TD></TR>\n";
8beafbbc 661 }
662 return $body;
663 } else {
664 for ($i = 0; $i < count($message->entities); $i++) {
665 $body .= formatAttachments ($message->entities[$i], $ent_id, $mailbox, $id);
666 }
667 return $body;
668 }
669 }
670 }
4809f489 671
672
673 /** this function decodes the body depending on the encoding type. **/
d4467150 674 function decodeBody($body, $encoding) {
623332f3 675 $body = str_replace("\r\n", "\n", $body);
d4467150 676 $encoding = strtolower($encoding);
7831268e 677
ef3f274f 678 if ($encoding == "quoted-printable") {
679 $body = quoted_printable_decode($body);
db87f79c 680
ef3f274f 681 while (ereg("=\n", $body))
682 $body = ereg_replace ("=\n", "", $body);
97be2168 683 } else if ($encoding == "base64") {
ef3f274f 684 $body = base64_decode($body);
d4467150 685 }
ef3f274f 686
687 // All other encodings are returned raw.
688 return $body;
aceb0d5c 689 }
a4c2cd49 690
691
692 // This functions decode strings that is encoded according to
693 // RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
2e434774 694 function decodeHeader ($string) {
146e0c45 695 if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
a4c2cd49 696 $string, $res)) {
1fd97780 697 if (ucfirst($res[2]) == "B") {
698 $replace = base64_decode($res[3]);
a4c2cd49 699 } else {
1fd97780 700 $replace = ereg_replace("_", " ", $res[3]);
5155cfcb 701 // Convert lowercase Quoted Printable to uppercase for
702 // quoted_printable_decode to understand it.
9be55c4b 703 while (ereg("(=(([0-9][abcdef])|([abcdef][0-9])|([abcdef][abcdef])))", $replace, $res)) {
5155cfcb 704 $replace = str_replace($res[1], strtoupper($res[1]), $replace);
705 }
a4c2cd49 706 $replace = quoted_printable_decode($replace);
707 }
708
1fd97780 709 $replace = charset_decode ($res[1], $replace);
a4c2cd49 710
9be55c4b 711 // Remove the name of the character set.
712 $string = eregi_replace ('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
a4c2cd49 713 $replace, $string);
9be55c4b 714
2e434774 715 // In case there should be more encoding in the string: recurse
716 return (decodeHeader($string));
a4c2cd49 717 } else
718 return ($string);
719 }
720
c3084273 721 // Encode a string according to RFC 1522 for use in headers if it
bb60fa3f 722 // contains 8-bit characters or anything that looks like it should
723 // be encoded.
c3084273 724 function encodeHeader ($string) {
725 global $default_charset;
726
bb60fa3f 727 // Encode only if the string contains 8-bit characters or =?
46562113 728 if (ereg("([\200-\377]|=\\?)", $string)) {
c3084273 729
bb60fa3f 730 // First the special characters
731 $string = str_replace("=", "=3D", $string);
732 $string = str_replace("?", "=3F", $string);
733 $string = str_replace("_", "=5F", $string);
734 $string = str_replace(" ", "_", $string);
735
b75d343f 736 for ( $ch = 127 ; $ch <= 255 ; $ch++ ) {
737 $replace = chr($ch);
738 $insert = sprintf("=%02X", $ch);
bb60fa3f 739 $string = str_replace($replace, $insert, $string);
c3084273 740 }
741
bb60fa3f 742 $newstring = "=?$default_charset?Q?".$string."?=";
743
c3084273 744 return $newstring;
745 }
746
747 return $string;
748 }
749
9f9d7d28 750?>