* Got bored and copied all the validate.php and define() stuff to 1.1
[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
17 include "../functions/i18n.php";
18 include "../functions/imap.php";
19 include "../config/config.php";
8beafbbc 20
cbcf32f6 21 /** Setting up the objects that have the structure for the message **/
8beafbbc 22
23 class msg_header {
24 /** msg_header contains generic variables for values that **/
25 /** could be in a header. **/
26
245a6892 27 var $type0 = '', $type1 = '', $boundary = '', $charset = '';
40023540 28 var $encoding = '', $size = 0, $to = array(), $from = '', $date = '';
29 var $cc = array(), $bcc = array(), $reply_to = '', $subject = '';
61423189 30 var $id = 0, $mailbox = '', $description = '', $filename = '';
66e1a00e 31 var $entity_id = 0, $message_id = 0, $name = '';
8beafbbc 32 }
33
34 class message {
35 /** message is the object that contains messages. It is a recursive
36 object in that through the $entities variable, it can contain
37 more objects of type message. See documentation in mime.txt for
38 a better description of how this works.
39 **/
61423189 40 var $header = '';
2df6ca53 41 var $entities = array();
8beafbbc 42
43 function addEntity ($msg) {
61423189 44 $this->entities[] = $msg;
8beafbbc 45 }
46 }
1fd97780 47
bcb432a3 48
bcb432a3 49
8beafbbc 50 /* --------------------------------------------------------------------------------- */
51 /* MIME DECODING */
52 /* --------------------------------------------------------------------------------- */
53
cbcf32f6 54 // This function gets the structure of a message and stores it in the "message" class.
55 // It will return this object for use with all relevant header information and
56 // fully parsed into the standard "message" object format.
8beafbbc 57 function mime_structure ($imap_stream, $header) {
e79bed1b 58 global $debug_mime;
8beafbbc 59 sqimap_messages_flag ($imap_stream, $header->id, $header->id, "Seen");
60
61 $id = $header->id;
62 fputs ($imap_stream, "a001 FETCH $id BODYSTRUCTURE\r\n");
245a6892 63 //
64 // This should use sqimap_read_data instead of reading it itself
65 //
e79bed1b 66 $read = fgets ($imap_stream, 10000);
254925d1 67 $response = substr($read, 0, 4);
245a6892 68 $bodystructure = "";
254925d1 69 while ($response != "a001") {
3e1266ef 70 $bodystructure .= $read;
254925d1 71 $read = fgets ($imap_stream, 10000);
72 $response = substr($read, 0, 4);
73 }
22ef7536 74 $read = $bodystructure;
8beafbbc 75
04632dbc 76 if ($debug_mime) echo "<tt>$read</tt><br><br>\n";
8beafbbc 77 // isolate the body structure and remove beginning and end parenthesis
22ef7536 78 $read = trim(substr ($read, strpos(strtolower($read), "bodystructure") + 13));
ea48eb25 79 $read = trim(substr ($read, 0, -1));
80 $end = mime_match_parenthesis(0, $read);
81 while ($end == strlen($read)-1) {
82 $read = trim(substr ($read, 0, -1));
83 $read = trim(substr ($read, 1));
84 $end = mime_match_parenthesis(0, $read);
85 }
8beafbbc 86
04632dbc 87 if ($debug_mime) echo "<tt>$read</tt><br><br>\n";
e79bed1b 88
85daa3ad 89 $msg = mime_parse_structure ($read, 0);
8beafbbc 90 $msg->header = $header;
91 return $msg;
92 }
93
cbcf32f6 94 // this starts the parsing of a particular structure. It is called recursively,
95 // so it can be passed different structures. It returns an object of type
96 // $message.
97 // First, it checks to see if it is a multipart message. If it is, then it
98 // handles that as it sees is necessary. If it is just a regular entity,
99 // then it parses it and adds the necessary header information (by calling out
100 // to mime_get_elements()
8beafbbc 101 function mime_parse_structure ($structure, $ent_id) {
e79bed1b 102 global $debug_mime;
04632dbc 103 if ($debug_mime) echo "<font color=008800><tt>START: mime_parse_structure()</tt></font><br>\n";
8beafbbc 104 $msg = new message();
105 if (substr($structure, 0, 1) == "(") {
106 $ent_id = mime_new_element_level($ent_id);
107 $start = $end = -1;
ea48eb25 108 if ($debug_mime) echo "<br><font color=0000aa><tt>$structure</tt></font><br>";
8beafbbc 109 do {
e79bed1b 110 if ($debug_mime) echo "<font color=008800><tt>Found entity...</tt></font><br>";
8beafbbc 111 $start = $end+1;
112 $end = mime_match_parenthesis ($start, $structure);
113
114 $element = substr($structure, $start+1, ($end - $start)-1);
ea48eb25 115 $ent_id = mime_increment_id ($ent_id);
8beafbbc 116 $newmsg = mime_parse_structure ($element, $ent_id);
117 $msg->addEntity ($newmsg);
118 } while (substr($structure, $end+1, 1) == "(");
119 } else {
120 // parse the elements
e79bed1b 121 if ($debug_mime) echo "<br><font color=0000aa><tt>$structure</tt></font><br>";
090595e1 122 $msg = mime_get_element ($structure, $msg, $ent_id);
e79bed1b 123 if ($debug_mime) echo "<br>";
8beafbbc 124 }
125 return $msg;
e79bed1b 126 if ($debug_mime) echo "<font color=008800><tt>&nbsp;&nbsp;END: mime_parse_structure()</tt></font><br>";
8beafbbc 127 }
128
129 // Increments the element ID. An element id can look like any of
130 // the following: 1, 1.2, 4.3.2.4.1, etc. This function increments
131 // the last number of the element id, changing 1.2 to 1.3.
132 function mime_increment_id ($id) {
ea48eb25 133 global $debug_mime;
8beafbbc 134 if (strpos($id, ".")) {
135 $first = substr($id, 0, strrpos($id, "."));
ea48eb25 136 $last = substr($id, strrpos($id, ".")+1);
8beafbbc 137 $last++;
ea48eb25 138 $new = $first . "." .$last;
8beafbbc 139 } else {
140 $new = $id + 1;
141 }
ea48eb25 142 if ($debug_mime) echo "<b>INCREMENT: $new</b><br>";
8beafbbc 143 return $new;
144 }
145
146 // See comment for mime_increment_id().
147 // This adds another level on to the entity_id changing 1.3 to 1.3.0
148 // NOTE: 1.3.0 is not a valid element ID. It MUST be incremented
149 // before it can be used. I left it this way so as not to have
150 // to make a special case if it is the first entity_id. It
151 // always increments it, and that works fine.
152 function mime_new_element_level ($id) {
ea48eb25 153 if (!$id) $id = 0;
154 else $id = $id . ".0";
155
8beafbbc 156 return $id;
157 }
158
ea48eb25 159 function mime_get_element (&$structure, $msg, $ent_id) {
e79bed1b 160 global $debug_mime;
8beafbbc 161 $elem_num = 1;
ea48eb25 162 $msg->header = new msg_header();
163 $msg->header->entity_id = $ent_id;
4bbe6ccc 164 $properties = array();
8beafbbc 165
166 while (strlen($structure) > 0) {
167 $structure = trim($structure);
168 $char = substr($structure, 0, 1);
169
e86ab872 170 if (strtolower(substr($structure, 0, 3)) == "nil") {
8beafbbc 171 $text = "";
172 $structure = substr($structure, 3);
173 } else if ($char == "\"") {
174 // loop through until we find the matching quote, and return that as a string
175 $pos = 1;
176 $char = substr($structure, $pos, 1);
245a6892 177 $text = "";
8beafbbc 178 while ($char != "\"" && $pos < strlen($structure)) {
179 $text .= $char;
180 $pos++;
181 $char = substr($structure, $pos, 1);
182 }
183 $structure = substr($structure, strlen($text) + 2);
184 } else if ($char == "(") {
185 // comment me
186 $end = mime_match_parenthesis (0, $structure);
187 $sub = substr($structure, 1, $end-1);
188 $properties = mime_get_props($properties, $sub);
189 $structure = substr($structure, strlen($sub) + 2);
190 } else {
191 // loop through until we find a space or an end parenthesis
192 $pos = 0;
193 $char = substr($structure, $pos, 1);
61d9e885 194 $text = "";
8beafbbc 195 while ($char != " " && $char != ")" && $pos < strlen($structure)) {
196 $text .= $char;
197 $pos++;
198 $char = substr($structure, $pos, 1);
aceb0d5c 199 }
8beafbbc 200 $structure = substr($structure, strlen($text));
aceb0d5c 201 }
e79bed1b 202 if ($debug_mime) echo "<tt>$elem_num : $text</tt><br>";
8beafbbc 203
204 // This is where all the text parts get put into the header
205 switch ($elem_num) {
206 case 1:
22ef7536 207 $msg->header->type0 = strtolower($text);
208 if ($debug_mime) echo "<tt>type0 = ".strtolower($text)."</tt><br>";
8beafbbc 209 break;
210 case 2:
22ef7536 211 $msg->header->type1 = strtolower($text);
212 if ($debug_mime) echo "<tt>type1 = ".strtolower($text)."</tt><br>";
8beafbbc 213 break;
ea48eb25 214 case 5:
215 $msg->header->description = $text;
216 if ($debug_mime) echo "<tt>description = $text</tt><br>";
217 break;
8beafbbc 218 case 6:
22ef7536 219 $msg->header->encoding = strtolower($text);
220 if ($debug_mime) echo "<tt>encoding = ".strtolower($text)."</tt><br>";
8beafbbc 221 break;
222 case 7:
ea48eb25 223 $msg->header->size = $text;
e79bed1b 224 if ($debug_mime) echo "<tt>size = $text</tt><br>";
8beafbbc 225 break;
226 default:
ea48eb25 227 if ($msg->header->type0 == "text" && $elem_num == 8) {
cbcf32f6 228 // This is a plain text message, so lets get the number of lines
229 // that it contains.
ea48eb25 230 $msg->header->num_lines = $text;
e79bed1b 231 if ($debug_mime) echo "<tt>num_lines = $text</tt><br>";
cbcf32f6 232
ea48eb25 233 } else if ($msg->header->type0 == "message" && $msg->header->type1 == "rfc822" && $elem_num == 8) {
234 // This is an encapsulated message, so lets start all over again and
235 // parse this message adding it on to the existing one.
236 $structure = trim($structure);
237 if (substr($structure, 0, 1) == "(") {
238 $e = mime_match_parenthesis (0, $structure);
239 $structure = substr($structure, 0, $e);
240 $structure = substr($structure, 1);
241 $m = mime_parse_structure($structure, $msg->header->entity_id);
cbcf32f6 242
243 // the following conditional is there to correct a bug that wasn't
244 // incrementing the entity IDs correctly because of the special case
245 // that message/rfc822 is. This fixes it fine.
ea48eb25 246 if (substr($structure, 1, 1) != "(")
247 $m->header->entity_id = mime_increment_id(mime_new_element_level($ent_id));
cbcf32f6 248
249 // Now we'll go through and reformat the results.
ea48eb25 250 if ($m->entities) {
251 for ($i=0; $i < count($m->entities); $i++) {
ea48eb25 252 $msg->addEntity($m->entities[$i]);
253 }
254 } else {
ea48eb25 255 $msg->addEntity($m);
256 }
257 $structure = "";
258 }
8beafbbc 259 }
260 break;
261 }
262 $elem_num++;
263 $text = "";
264 }
265 // loop through the additional properties and put those in the various headers
ea48eb25 266 if ($msg->header->type0 != "message") {
cbcf32f6 267 for ($i=0; $i < count($properties); $i++) {
268 $msg->header->{$properties[$i]["name"]} = $properties[$i]["value"];
269 if ($debug_mime) echo "<tt>".$properties[$i]["name"]." = " . $properties[$i]["value"] . "</tt><br>";
270 }
ea48eb25 271 }
e4a256af 272
ea48eb25 273 return $msg;
8beafbbc 274 }
275
276 // I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't
277 // figure out how to do this part, so I decided to go to bed. I woke up
278 // in the morning and had a flash of insight. I went to the white-board
279 // and scribbled it out, then spent a bit programming it, and this is the
280 // result. Nothing complicated, but I think my brain was fried yesterday.
cbcf32f6 281 // Funny how that happens some times.
8beafbbc 282 //
283 // This gets properties in a nested parenthesisized list. For example,
284 // this would get passed something like: ("attachment" ("filename" "luke.tar.gz"))
285 // This returns an array called $props with all paired up properties.
286 // It ignores the "attachment" for now, maybe that should change later
287 // down the road. In this case, what is returned is:
288 // $props[0]["name"] = "filename";
289 // $props[0]["value"] = "luke.tar.gz";
290 function mime_get_props ($props, $structure) {
e79bed1b 291 global $debug_mime;
8beafbbc 292 while (strlen($structure) > 0) {
293 $structure = trim($structure);
294 $char = substr($structure, 0, 1);
295
296 if ($char == "\"") {
297 $pos = 1;
298 $char = substr($structure, $pos, 1);
245a6892 299 $tmp = "";
8beafbbc 300 while ($char != "\"" && $pos < strlen($structure)) {
301 $tmp .= $char;
302 $pos++;
303 $char = substr($structure, $pos, 1);
304 }
305 $structure = trim(substr($structure, strlen($tmp) + 2));
306 $char = substr($structure, 0, 1);
307
308 if ($char == "\"") {
309 $pos = 1;
310 $char = substr($structure, $pos, 1);
245a6892 311 $value = "";
8beafbbc 312 while ($char != "\"" && $pos < strlen($structure)) {
313 $value .= $char;
314 $pos++;
315 $char = substr($structure, $pos, 1);
316 }
317 $structure = trim(substr($structure, strlen($tmp) + 2));
318
319 $k = count($props);
22ef7536 320 $props[$k]["name"] = strtolower($tmp);
8beafbbc 321 $props[$k]["value"] = $value;
322 } else if ($char == "(") {
323 $end = mime_match_parenthesis (0, $structure);
324 $sub = substr($structure, 1, $end-1);
245a6892 325 if (! isset($props))
326 $props = array();
8beafbbc 327 $props = mime_get_props($props, $sub);
328 $structure = substr($structure, strlen($sub) + 2);
329 }
330 return $props;
331 } else if ($char == "(") {
332 $end = mime_match_parenthesis (0, $structure);
333 $sub = substr($structure, 1, $end-1);
334 $props = mime_get_props($props, $sub);
335 $structure = substr($structure, strlen($sub) + 2);
ea48eb25 336 return $props;
8beafbbc 337 } else {
338 return $props;
7831268e 339 }
8beafbbc 340 }
341 }
7831268e 342
8beafbbc 343 // Matches parenthesis. It will return the position of the matching
344 // parenthesis in $structure. For instance, if $structure was:
345 // ("text" "plain" ("val1name", "1") nil ... )
346 // x x
347 // then this would return 42 to match up those two.
348 function mime_match_parenthesis ($pos, $structure) {
349 $char = substr($structure, $pos, 1);
350
351 // ignore all extra characters
5ffe5a7e 352 // If inside of a string, skip string -- Boundary IDs and other
353 // things can have ) in them.
4bbe6ccc 354 if ($char != '(')
355 return strlen($structure);
8beafbbc 356 while ($pos < strlen($structure)) {
357 $pos++;
358 $char = substr($structure, $pos, 1);
359 if ($char == ")") {
360 return $pos;
5ffe5a7e 361 } else if ($char == '"') {
362 $pos ++;
363 while (substr($structure, $pos, 1) != '"' &&
364 $pos < strlen($structure)) {
377a40b2 365 if (substr($structure, $pos, 2) == '\\"')
366 $pos ++;
367 elseif (substr($structure, $pos, 2) == '\\\\')
368 $pos ++;
5ffe5a7e 369 $pos ++;
370 }
8beafbbc 371 } else if ($char == "(") {
372 $pos = mime_match_parenthesis ($pos, $structure);
373 }
d4467150 374 }
377a40b2 375 echo "Error decoding mime structure. Report this as a bug!<br>\n";
376 return $pos;
8beafbbc 377 }
d4467150 378
8beafbbc 379 function mime_fetch_body ($imap_stream, $id, $ent_id) {
380 // do a bit of error correction. If we couldn't find the entity id, just guess
381 // that it is the first one. That is usually the case anyway.
382 if (!$ent_id) $ent_id = 1;
383
4c5da285 384 fputs ($imap_stream, "a010 FETCH $id BODY[$ent_id]\r\n");
385 $data = sqimap_read_data ($imap_stream, 'a010', true, $response, $message);
2d32cb9f 386 $topline = array_shift($data);
146e0c45 387 while (! ereg('\\* [0-9]+ FETCH ', $topline) && data)
2d32cb9f 388 $topline = array_shift($data);
6ff90ce9 389 $wholemessage = implode('', $data);
2d32cb9f 390
146e0c45 391 if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
2d32cb9f 392 return substr($wholemessage, 0, $regs[1]);
441f2d33 393 }
394 else if (ereg('"([^"]*)"', $topline, $regs)) {
395 return $regs[1];
396 }
2d32cb9f 397
398 $str = "Body retrival error. Please report this bug!\n";
399 $str .= "Response: $response\n";
400 $str .= "Message: $message\n";
401 $str .= "FETCH line: $topline";
402 $str .= "---------------\n$wholemessage";
403 foreach ($data as $d)
404 {
405 $str .= htmlspecialchars($d) . "\n";
406 }
407 return $str;
408
441f2d33 409 return "Body retrival error, please report this bug!\n\nTop line is \"$topline\"\n";
d4467150 410 }
411
beb9e459 412 function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
413 // do a bit of error correction. If we couldn't find the entity id, just guess
414 // that it is the first one. That is usually the case anyway.
415 if (!$ent_id) $ent_id = 1;
416
417 fputs ($imap_stream, "a001 FETCH $id BODY[$ent_id]\r\n");
418 $cnt = 0;
419 $continue = true;
420 $read = fgets ($imap_stream,4096);
421 while (!ereg("^a001 (OK|BAD|NO)(.*)$", $read, $regs)) {
422 if (trim($read) == ")==") {
423 $read1 = $read;
424 $read = fgets ($imap_stream,4096);
425 if (ereg("^a001 (OK|BAD|NO)(.*)$", $read, $regs)) {
426 return;
427 } else {
428 echo decodeBody($read1, $encoding);
429 echo decodeBody($read, $encoding);
430 }
431 } else if ($cnt) {
432 echo decodeBody($read, $encoding);
433 }
434 $read = fgets ($imap_stream,4096);
435 $cnt++;
436 }
437 }
438
8beafbbc 439 /* -[ END MIME DECODING ]----------------------------------------------------------- */
d4467150 440
aceb0d5c 441
d4467150 442
8beafbbc 443 /** This is the first function called. It decides if this is a multipart
444 message or if it should be handled as a single entity
4809f489 445 **/
090595e1 446 function decodeMime ($imap_stream, &$header) {
8beafbbc 447 global $username, $key, $imapServerAddress, $imapPort;
8d8ab69a 448 return mime_structure ($imap_stream, $header);
8beafbbc 449 }
b1dadc61 450
cbcf32f6 451 // This is here for debugging purposese. It will print out a list
452 // of all the entity IDs that are in the $message object.
ea48eb25 453 function listEntities ($message) {
454 if ($message) {
cbcf32f6 455 if ($message->header->entity_id)
456 echo "<tt>" . $message->header->entity_id . " : " . $message->header->type0 . "/" . $message->header->type1 . "<br>";
457 for ($i = 0; $message->entities[$i]; $i++) {
458 $msg = listEntities($message->entities[$i], $ent_id);
459 if ($msg)
460 return $msg;
461 }
ea48eb25 462 }
463 }
464
cbcf32f6 465 // returns a $message object for a particular entity id
8beafbbc 466 function getEntity ($message, $ent_id) {
467 if ($message) {
ea48eb25 468 if ($message->header->entity_id == $ent_id && strlen($ent_id) == strlen($message->header->entity_id)) {
8beafbbc 469 return $message;
b1dadc61 470 } else {
cd928157 471 for ($i = 0; isset($message->entities[$i]); $i++) {
8beafbbc 472 $msg = getEntity ($message->entities[$i], $ent_id);
473 if ($msg)
474 return $msg;
b1dadc61 475 }
8beafbbc 476 }
477 }
478 }
479
cbcf32f6 480 // figures out what entity to display and returns the $message object
481 // for that entity.
3ae6e629 482 function findDisplayEntity ($message, $textOnly = 1, $next = 'none')
e2ab93e5 483 {
484 global $show_html_default;
485
486 if (! $message)
487 return;
488
489 // Show text/plain or text/html -- the first one we find.
490 if ($message->header->type0 == 'text' &&
491 ($message->header->type1 == 'plain' ||
3d6c7e96 492 $message->header->type1 == 'html'))
e2ab93e5 493 {
494 // If the next part is an HTML version, this will
495 // all be true. Show it, if the user so desires.
496 // HTML mails this way all have entity_id of 2. 1 = text/plain
497 if ($next != 'none' &&
3ae6e629 498 $textOnly == 0 &&
e2ab93e5 499 $next->header->type0 == "text" &&
500 $next->header->type1 == "html" &&
501 $next->header->entity_id == 2 &&
502 $message->header->type1 == "plain" &&
503 isset($show_html_default) &&
504 $show_html_default)
505 $message = $next;
506
507 if (isset($message->header->entity_id))
508 return $message->header->entity_id;
509 }
510 else
511 {
3d6c7e96 512 for ($i=0; isset($message->entities[$i]); $i++)
e2ab93e5 513 {
514 $next = 'none';
515 if (isset($message->entities[$i + 1]))
516 $next = $message->entities[$i + 1];
3ae6e629 517 $entity = findDisplayEntity($message->entities[$i],
518 $textOnly, $next);
e2ab93e5 519 if ($entity != 0)
520 return $entity;
521 }
522 }
523 return 0;
b1dadc61 524 }
8405ee35 525
d068c0ec 526 /** This returns a parsed string called $body. That string can then
527 be displayed as the actual message in the HTML. It contains
528 everything needed, including HTML Tags, Attachments at the
529 bottom, etc.
4809f489 530 **/
8d8ab69a 531 function formatBody($imap_stream, $message, $color, $wrap_at) {
cbcf32f6 532 // this if statement checks for the entity to show as the
533 // primary message. To add more of them, just put them in the
534 // order that is their priority.
a037624d 535 global $startMessage, $username, $key, $imapServerAddress, $imapPort;
8beafbbc 536
8beafbbc 537 $id = $message->header->id;
538 $urlmailbox = urlencode($message->header->mailbox);
539
e4a256af 540 // Get the right entity and redefine message to be this entity
3ae6e629 541 // Pass the 0 to mean that we want the 'best' viewable one
542 $ent_num = findDisplayEntity ($message, 0);
2c252f5a 543 $body_message = getEntity($message, $ent_num);
d4ff4d67 544 if (($body_message->header->type0 == "text") ||
545 ($body_message->header->type0 == "rfc822")) {
546
d51894be 547 $body = mime_fetch_body ($imap_stream, $id, $ent_num);
d4ff4d67 548 $body = decodeBody($body, $body_message->header->encoding);
549
550 // If there are other types that shouldn't be formatted, add
551 // them here
f9b3e5d9 552 if ($body_message->header->type1 != "html") {
9eea179c 553 translateText($body, $wrap_at, $body_message->header->charset);
d4ff4d67 554 }
555
bc104ef3 556 $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 557
558 /** Display the ATTACHMENTS: message if there's more than one part **/
dd389be5 559 $body .= "</TD></TR></TABLE>";
719534f2 560 if (isset($message->entities[0])) {
d4ff4d67 561 $body .= formatAttachments ($message, $ent_num, $message->header->mailbox, $id);
d4ff4d67 562 }
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?>