Tyler: This is not very original but should do the trick
[squirrelmail.git] / functions / mime.php
CommitLineData
59177427 1<?php
2ba13803 2
7350889b 3 /**
4 * mime.php
5 *
6 * Copyright (c) 1999-2001 The Squirrelmail Development Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This contains the functions necessary to detect and decode MIME
10 * messages.
11 *
12 * $Id$
13 */
b74ba498 14
0fc2aca0 15 require_once('../functions/imap.php');
7baf86a9 16 require_once('../functions/attachment_common.php');
8beafbbc 17
cbcf32f6 18 /** Setting up the objects that have the structure for the message **/
8beafbbc 19
20 class msg_header {
21 /** msg_header contains generic variables for values that **/
22 /** could be in a header. **/
b74ba498 23
245a6892 24 var $type0 = '', $type1 = '', $boundary = '', $charset = '';
40023540 25 var $encoding = '', $size = 0, $to = array(), $from = '', $date = '';
26 var $cc = array(), $bcc = array(), $reply_to = '', $subject = '';
61423189 27 var $id = 0, $mailbox = '', $description = '', $filename = '';
66e1a00e 28 var $entity_id = 0, $message_id = 0, $name = '';
020abcf3 29 // var $priority = "";
8beafbbc 30 }
b74ba498 31
8beafbbc 32 class message {
33 /** message is the object that contains messages. It is a recursive
b74ba498 34 object in that through the $entities variable, it can contain
8beafbbc 35 more objects of type message. See documentation in mime.txt for
36 a better description of how this works.
b74ba498 37 **/
61423189 38 var $header = '';
2df6ca53 39 var $entities = array();
b74ba498 40
8beafbbc 41 function addEntity ($msg) {
61423189 42 $this->entities[] = $msg;
8beafbbc 43 }
44 }
1fd97780 45
8beafbbc 46 /* --------------------------------------------------------------------------------- */
47 /* MIME DECODING */
48 /* --------------------------------------------------------------------------------- */
b74ba498 49
cbcf32f6 50 // This function gets the structure of a message and stores it in the "message" class.
51 // It will return this object for use with all relevant header information and
52 // fully parsed into the standard "message" object format.
8beafbbc 53 function mime_structure ($imap_stream, $header) {
b74ba498 54
55 sqimap_messages_flag ($imap_stream, $header->id, $header->id, 'Seen');
56 $ssid = sqimap_session_id();
57 $lsid = strlen( $ssid );
8beafbbc 58 $id = $header->id;
b74ba498 59 fputs ($imap_stream, "$ssid FETCH $id BODYSTRUCTURE\r\n");
245a6892 60 //
61 // This should use sqimap_read_data instead of reading it itself
62 //
e79bed1b 63 $read = fgets ($imap_stream, 10000);
b74ba498 64 $bodystructure = '';
65 while( substr($read, 0, $lsid) <> $ssid &&
66 !feof( $imap_stream ) ) {
3e1266ef 67 $bodystructure .= $read;
254925d1 68 $read = fgets ($imap_stream, 10000);
254925d1 69 }
22ef7536 70 $read = $bodystructure;
8beafbbc 71
8beafbbc 72 // isolate the body structure and remove beginning and end parenthesis
b74ba498 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
85daa3ad 82 $msg = mime_parse_structure ($read, 0);
8beafbbc 83 $msg->header = $header;
84 return $msg;
85 }
86
cbcf32f6 87 // this starts the parsing of a particular structure. It is called recursively,
88 // so it can be passed different structures. It returns an object of type
89 // $message.
90 // First, it checks to see if it is a multipart message. If it is, then it
91 // handles that as it sees is necessary. If it is just a regular entity,
92 // then it parses it and adds the necessary header information (by calling out
93 // to mime_get_elements()
8beafbbc 94 function mime_parse_structure ($structure, $ent_id) {
b74ba498 95
8beafbbc 96 $msg = new message();
b74ba498 97 if ($structure{0} == '(') {
8beafbbc 98 $ent_id = mime_new_element_level($ent_id);
99 $start = $end = -1;
100 do {
8beafbbc 101 $start = $end+1;
102 $end = mime_match_parenthesis ($start, $structure);
b74ba498 103
8beafbbc 104 $element = substr($structure, $start+1, ($end - $start)-1);
ea48eb25 105 $ent_id = mime_increment_id ($ent_id);
8beafbbc 106 $newmsg = mime_parse_structure ($element, $ent_id);
107 $msg->addEntity ($newmsg);
b74ba498 108 } while ($structure{$end+1} == '(');
8beafbbc 109 } else {
110 // parse the elements
090595e1 111 $msg = mime_get_element ($structure, $msg, $ent_id);
8beafbbc 112 }
bc64f471 113 return $msg;
8beafbbc 114 }
115
116 // Increments the element ID. An element id can look like any of
117 // the following: 1, 1.2, 4.3.2.4.1, etc. This function increments
118 // the last number of the element id, changing 1.2 to 1.3.
119 function mime_increment_id ($id) {
b74ba498 120
8beafbbc 121 if (strpos($id, ".")) {
122 $first = substr($id, 0, strrpos($id, "."));
ea48eb25 123 $last = substr($id, strrpos($id, ".")+1);
8beafbbc 124 $last++;
ea48eb25 125 $new = $first . "." .$last;
8beafbbc 126 } else {
127 $new = $id + 1;
128 }
b74ba498 129
8beafbbc 130 return $new;
131 }
132
133 // See comment for mime_increment_id().
134 // This adds another level on to the entity_id changing 1.3 to 1.3.0
b74ba498 135 // NOTE: 1.3.0 is not a valid element ID. It MUST be incremented
8beafbbc 136 // before it can be used. I left it this way so as not to have
137 // to make a special case if it is the first entity_id. It
138 // always increments it, and that works fine.
139 function mime_new_element_level ($id) {
ea48eb25 140
b74ba498 141 if (!$id) {
142 $id = 0;
143 } else {
144 $id = $id . '.0';
145 }
146
147 return( $id );
8beafbbc 148 }
149
ea48eb25 150 function mime_get_element (&$structure, $msg, $ent_id) {
b74ba498 151
8beafbbc 152 $elem_num = 1;
ea48eb25 153 $msg->header = new msg_header();
154 $msg->header->entity_id = $ent_id;
4bbe6ccc 155 $properties = array();
b74ba498 156
8beafbbc 157 while (strlen($structure) > 0) {
158 $structure = trim($structure);
b74ba498 159 $char = $structure{0};
8beafbbc 160
b74ba498 161 if (strtolower(substr($structure, 0, 3)) == 'nil') {
162 $text = '';
8beafbbc 163 $structure = substr($structure, 3);
b74ba498 164 } else if ($char == '"') {
8beafbbc 165 // loop through until we find the matching quote, and return that as a string
166 $pos = 1;
b74ba498 167 $text = '';
168 while ( ($char = $structure{$pos} ) <> '"' && $pos < strlen($structure)) {
8beafbbc 169 $text .= $char;
170 $pos++;
b74ba498 171 }
8beafbbc 172 $structure = substr($structure, strlen($text) + 2);
b74ba498 173 } else if ($char == '(') {
8beafbbc 174 // comment me
175 $end = mime_match_parenthesis (0, $structure);
176 $sub = substr($structure, 1, $end-1);
177 $properties = mime_get_props($properties, $sub);
178 $structure = substr($structure, strlen($sub) + 2);
179 } else {
180 // loop through until we find a space or an end parenthesis
181 $pos = 0;
b74ba498 182 $char = $structure{$pos};
183 $text = '';
184 while ($char != ' ' && $char != ')' && $pos < strlen($structure)) {
8beafbbc 185 $text .= $char;
186 $pos++;
b74ba498 187 $char = $structure{$pos};
aceb0d5c 188 }
8beafbbc 189 $structure = substr($structure, strlen($text));
aceb0d5c 190 }
8beafbbc 191
192 // This is where all the text parts get put into the header
193 switch ($elem_num) {
b74ba498 194 case 1:
22ef7536 195 $msg->header->type0 = strtolower($text);
8beafbbc 196 break;
b74ba498 197 case 2:
22ef7536 198 $msg->header->type1 = strtolower($text);
8beafbbc 199 break;
b74ba498 200 case 4: // Id
201 // Invisimail enclose images with <>
202 $msg->header->id = str_replace( '<', '', str_replace( '>', '', $text ) );
203 break;
ea48eb25 204 case 5:
205 $msg->header->description = $text;
ea48eb25 206 break;
8beafbbc 207 case 6:
22ef7536 208 $msg->header->encoding = strtolower($text);
8beafbbc 209 break;
210 case 7:
ea48eb25 211 $msg->header->size = $text;
8beafbbc 212 break;
213 default:
b74ba498 214 if ($msg->header->type0 == 'text' && $elem_num == 8) {
cbcf32f6 215 // This is a plain text message, so lets get the number of lines
216 // that it contains.
ea48eb25 217 $msg->header->num_lines = $text;
cbcf32f6 218
b74ba498 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
ea48eb25 221 // parse this message adding it on to the existing one.
222 $structure = trim($structure);
b74ba498 223 if ( $structure{0} == '(' ) {
ea48eb25 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);
b74ba498 228
cbcf32f6 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.
b74ba498 232 if (substr($structure, 1, 1) != '(')
ea48eb25 233 $m->header->entity_id = mime_increment_id(mime_new_element_level($ent_id));
b74ba498 234
cbcf32f6 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 }
b74ba498 243 $structure = "";
ea48eb25 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
b74ba498 252 if ($msg->header->type0 != 'message') {
cbcf32f6 253 for ($i=0; $i < count($properties); $i++) {
b74ba498 254 $msg->header->{$properties[$i]['name']} = $properties[$i]['value'];
cbcf32f6 255 }
ea48eb25 256 }
e4a256af 257
ea48eb25 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.
b74ba498 271 // It ignores the "attachment" for now, maybe that should change later
8beafbbc 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) {
b74ba498 276
8beafbbc 277 while (strlen($structure) > 0) {
278 $structure = trim($structure);
b74ba498 279 $char = $structure{0};
8beafbbc 280
b74ba498 281 if ($char == '"') {
8beafbbc 282 $pos = 1;
b74ba498 283 $tmp = '';
284 while ( ( $char = $structure{$pos} ) != '"' &&
285 $pos < strlen($structure)) {
8beafbbc 286 $tmp .= $char;
287 $pos++;
b74ba498 288 }
8beafbbc 289 $structure = trim(substr($structure, strlen($tmp) + 2));
b74ba498 290 $char = $structure{0};
8beafbbc 291
b74ba498 292 if ($char == '"') {
8beafbbc 293 $pos = 1;
b74ba498 294 $value = '';
295 while ( ( $char = $structure{$pos} ) != '"' &&
296 $pos < strlen($structure) ) {
8beafbbc 297 $value .= $char;
298 $pos++;
b74ba498 299 }
8beafbbc 300 $structure = trim(substr($structure, strlen($tmp) + 2));
b74ba498 301
8beafbbc 302 $k = count($props);
b74ba498 303 $props[$k]['name'] = strtolower($tmp);
304 $props[$k]['value'] = $value;
305 } else if ($char == '(') {
8beafbbc 306 $end = mime_match_parenthesis (0, $structure);
307 $sub = substr($structure, 1, $end-1);
b74ba498 308 if (! isset($props))
309 $props = array();
8beafbbc 310 $props = mime_get_props($props, $sub);
311 $structure = substr($structure, strlen($sub) + 2);
312 }
313 return $props;
b74ba498 314 } else if ($char == '(') {
8beafbbc 315 $end = mime_match_parenthesis (0, $structure);
316 $sub = substr($structure, 1, $end-1);
317 $props = mime_get_props($props, $sub);
318 $structure = substr($structure, strlen($sub) + 2);
ea48eb25 319 return $props;
8beafbbc 320 } else {
321 return $props;
7831268e 322 }
8beafbbc 323 }
324 }
7831268e 325
8beafbbc 326 // Matches parenthesis. It will return the position of the matching
327 // parenthesis in $structure. For instance, if $structure was:
328 // ("text" "plain" ("val1name", "1") nil ... )
329 // x x
330 // then this would return 42 to match up those two.
331 function mime_match_parenthesis ($pos, $structure) {
b74ba498 332
333 $j = strlen( $structure );
8beafbbc 334
335 // ignore all extra characters
5ffe5a7e 336 // If inside of a string, skip string -- Boundary IDs and other
337 // things can have ) in them.
b74ba498 338 if( $structure{$pos} != '(' )
339 return( $j );
340
341 while( $pos < $j ) {
8beafbbc 342 $pos++;
b74ba498 343 if ($structure{$pos} == ')') {
8beafbbc 344 return $pos;
b74ba498 345 } elseif ($structure{$pos} == '"') {
346 $pos++;
347 while( $structure{$pos} != '"' &&
348 $pos < $j ) {
349 if (substr($structure, $pos, 2) == '\\"')
350 $pos++;
351 elseif (substr($structure, $pos, 2) == '\\\\')
352 $pos++;
353 $pos++;
5ffe5a7e 354 }
b74ba498 355 } elseif ( $structure{$pos} == '(' ) {
8beafbbc 356 $pos = mime_match_parenthesis ($pos, $structure);
357 }
d4467150 358 }
377a40b2 359 echo "Error decoding mime structure. Report this as a bug!<br>\n";
b74ba498 360 return( $pos );
8beafbbc 361 }
d4467150 362
a3daaaf3 363 function mime_fetch_body ($imap_stream, $id, $ent_id ) {
364 // do a bit of error correction. If we couldn't find the entity id, just guess
365 // that it is the first one. That is usually the case anyway.
366 if (!$ent_id)
367 $ent_id = 1;
368 $sid = sqimap_session_id();
369 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
370 $data = sqimap_read_data ($imap_stream, $sid, true, $response, $message);
371 $topline = array_shift($data);
372 while (! ereg('\\* [0-9]+ FETCH ', $topline) && $data)
373 $topline = array_shift($data);
374 $wholemessage = implode('', $data);
375 if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
376 $ret = substr( $wholemessage, 0, $regs[1] );
377 /*
378 There is some information in the content info header that could be important
379 in order to parse html messages. Let's get them here.
380 */
381 if( $ret{0} == '<' ) {
382 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id.MIME]\r\n");
383 $data = sqimap_read_data ($imap_stream, $sid, true, $response, $message);
384 $base = '';
385 $k = 10;
386 foreach( $data as $d ) {
387 if( substr( $d, 0, 13 ) == 'Content-Base:' ) {
388 $j = strlen( $d );
389 $i = 13;
390 $base = '';
391 while( $i < $j &&
392 ( !isNoSep( $d{$i} ) || $d{$i} == '"' ) )
393 $i++;
394 while( $i < $j ) {
395 if( isNoSep( $d{$i} ) )
396 $base .= $d{$i};
397 $i++;
398 }
399 $k = 0;
400 } elseif( $k == 1 && !isnosep( $d{0} ) ) {
401 $base .= substr( $d, 1 );
402 }
403 $k++;
404 }
405 if( $base <> '' )
406 $ret = "<base href=\"$base\">" . $ret;
407 }
408 } else if (ereg('"([^"]*)"', $topline, $regs)) {
409 $ret = $regs[1];
410 } else {
a019eeb8 411 global $where, $what, $mailbox, $passed_id, $startMessage;
412 $par = "mailbox=".urlencode($mailbox)."&passed_id=$passed_id";
413 if (isset($where) && isset($what)) {
414 $par .= "&where=".urlencode($where)."&what=".urlencode($what);
415 } else {
416 $par .= "&startMessage=$startMessage&show_more=0";
a3daaaf3 417 }
a019eeb8 418 $par .= '&response='.urlencode($response).'&message='.urlencode($message).
419 '&topline='.urlencode($topline);
420
421 echo '<b><font color=$color[2]>Body retrieval error. The reason for this is most probably that<BR> ' .
422 'the message is malformed. Please help us making future versions<BR> ' .
423 "better by submitting this message to the developers knowledgebase!<BR>\n" .
424 "<A HREF=\"../src/retrievalerror.php?$par\">Submit message</A><BR>" .
425
426 "<tt>Response: $response<BR>" .
427 "Message: $message<BR>" .
428 "FETCH line: $topline<BR></tt></font></b>";
429
430 fputs ($imap_stream, "$sid FETCH $passed_id BODY[]\r\n");
431 $data = sqimap_read_data ($imap_stream, $sid, true, $response, $message);
432 array_shift($data);
433 $wholemessage = implode('', $data);
434
435 $ret = "---------------\n$wholemessage";
436
a3daaaf3 437 }
438 return( $ret );
439 }
d4467150 440
beb9e459 441 function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
442 // do a bit of error correction. If we couldn't find the entity id, just guess
443 // that it is the first one. That is usually the case anyway.
444 if (!$ent_id) $ent_id = 1;
b74ba498 445 $sid = sqimap_session_id();
1d1e02f4 446 // Don't kill the connection if the browser is over a dialup
447 // and it would take over 30 seconds to download it.
448 set_time_limit(0);
449
b74ba498 450 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
451 $cnt = 0;
452 $continue = true;
453 $read = fgets ($imap_stream,4096);
454 // This could be bad -- if the section has sqimap_session_id() . ' OK'
455 // or similar, it will kill the download.
456 while (!ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
457 if (trim($read) == ')==') {
458 $read1 = $read;
459 $read = fgets ($imap_stream,4096);
460 if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
461 return;
462 } else {
463 echo decodeBody($read1, $encoding) .
464 decodeBody($read, $encoding);
465 }
466 } else if ($cnt) {
467 echo decodeBody($read, $encoding);
468 }
469 $read = fgets ($imap_stream,4096);
470 $cnt++;
471 }
beb9e459 472 }
473
8beafbbc 474 /* -[ END MIME DECODING ]----------------------------------------------------------- */
d4467150 475
aceb0d5c 476
d4467150 477
8beafbbc 478 /** This is the first function called. It decides if this is a multipart
479 message or if it should be handled as a single entity
4809f489 480 **/
090595e1 481 function decodeMime ($imap_stream, &$header) {
8beafbbc 482 global $username, $key, $imapServerAddress, $imapPort;
8d8ab69a 483 return mime_structure ($imap_stream, $header);
8beafbbc 484 }
b1dadc61 485
cbcf32f6 486 // This is here for debugging purposese. It will print out a list
487 // of all the entity IDs that are in the $message object.
b74ba498 488 /*
ea48eb25 489 function listEntities ($message) {
490 if ($message) {
cbcf32f6 491 if ($message->header->entity_id)
b74ba498 492 echo "<tt>" . $message->header->entity_id . ' : ' . $message->header->type0 . '/' . $message->header->type1 . '<br>';
cbcf32f6 493 for ($i = 0; $message->entities[$i]; $i++) {
494 $msg = listEntities($message->entities[$i], $ent_id);
495 if ($msg)
496 return $msg;
497 }
ea48eb25 498 }
499 }
b74ba498 500 */
ea48eb25 501
cbcf32f6 502 // returns a $message object for a particular entity id
8beafbbc 503 function getEntity ($message, $ent_id) {
504 if ($message) {
ea48eb25 505 if ($message->header->entity_id == $ent_id && strlen($ent_id) == strlen($message->header->entity_id)) {
8beafbbc 506 return $message;
b1dadc61 507 } else {
cd928157 508 for ($i = 0; isset($message->entities[$i]); $i++) {
8beafbbc 509 $msg = getEntity ($message->entities[$i], $ent_id);
510 if ($msg)
511 return $msg;
b1dadc61 512 }
b74ba498 513 }
8beafbbc 514 }
515 }
516
a3daaaf3 517 // figures out what entity to display and returns the $message object
518 // for that entity.
519 function findDisplayEntity ($message, $textOnly = 1) {
520 global $show_html_default;
521
522 $entity = 0;
523
524 if ($message) {
525 if ( $message->header->type0 == 'multipart' &&
526 ( $message->header->type1 == 'alternative' ||
527 $message->header->type1 == 'related' ) &&
528 $show_html_default && ! $textOnly ) {
529 $entity = findDisplayEntityHTML($message);
530 }
531
532 // Show text/plain or text/html -- the first one we find.
533 if ( $entity == 0 &&
534 $message->header->type0 == 'text' &&
535 ( $message->header->type1 == 'plain' ||
536 $message->header->type1 == 'html' ) &&
537 isset($message->header->entity_id) ) {
538 $entity = $message->header->entity_id;
539 }
540
541 $i = 0;
542 while ($entity == 0 && isset($message->entities[$i]) ) {
543 $entity = findDisplayEntity($message->entities[$i], $textOnly);
544 $i++;
545 }
546 }
547
548 return( $entity );
549 }
b74ba498 550
c3a80dac 551 // Shows the HTML version
552 function findDisplayEntityHTML ($message) {
b74ba498 553 if ($message->header->type0 == 'text' &&
c3a80dac 554 $message->header->type1 == 'html' &&
b74ba498 555 isset($message->header->entity_id))
556 return $message->header->entity_id;
c3a80dac 557 for ($i = 0; isset($message->entities[$i]); $i ++) {
558 $entity = findDisplayEntityHTML($message->entities[$i]);
b74ba498 559 if ($entity != 0)
560 return $entity;
c3a80dac 561 }
e2ab93e5 562 return 0;
b1dadc61 563 }
8405ee35 564
d068c0ec 565 /** This returns a parsed string called $body. That string can then
566 be displayed as the actual message in the HTML. It contains
567 everything needed, including HTML Tags, Attachments at the
568 bottom, etc.
4809f489 569 **/
8d8ab69a 570 function formatBody($imap_stream, $message, $color, $wrap_at) {
cbcf32f6 571 // this if statement checks for the entity to show as the
572 // primary message. To add more of them, just put them in the
573 // order that is their priority.
5f1c529a 574 global $startMessage, $username, $key, $imapServerAddress, $imapPort,
575 $show_html_default;
8beafbbc 576
8beafbbc 577 $id = $message->header->id;
578 $urlmailbox = urlencode($message->header->mailbox);
579
e4a256af 580 // Get the right entity and redefine message to be this entity
3ae6e629 581 // Pass the 0 to mean that we want the 'best' viewable one
582 $ent_num = findDisplayEntity ($message, 0);
2c252f5a 583 $body_message = getEntity($message, $ent_num);
b74ba498 584 if (($body_message->header->type0 == 'text') ||
585 ($body_message->header->type0 == 'rfc822')) {
586
d51894be 587 $body = mime_fetch_body ($imap_stream, $id, $ent_num);
d4ff4d67 588 $body = decodeBody($body, $body_message->header->encoding);
b74ba498 589 $hookResults = do_hook("message_body", $body);
590 $body = $hookResults[1];
b36d403c 591
d4ff4d67 592 // If there are other types that shouldn't be formatted, add
b74ba498 593 // them here
a3daaaf3 594 if ($body_message->header->type1 == 'html') {
595 if( $show_html_default <> 1 ) {
596 $body = strip_tags( $body );
597 translateText($body, $wrap_at, $body_message->header->charset);
598 } else {
599 $body = MagicHTML( $body, $id );
600 }
601 } else {
9eea179c 602 translateText($body, $wrap_at, $body_message->header->charset);
b74ba498 603 }
604
7b9592dc 605 $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>";
b74ba498 606
d4ff4d67 607 /** Display the ATTACHMENTS: message if there's more than one part **/
dd389be5 608 $body .= "</TD></TR></TABLE>";
719534f2 609 if (isset($message->entities[0])) {
d4ff4d67 610 $body .= formatAttachments ($message, $ent_num, $message->header->mailbox, $id);
d4ff4d67 611 }
b74ba498 612 $body .= "</TD></TR></TABLE>";
d4ff4d67 613 } else {
cba164a0 614 $body = formatAttachments ($message, -1, $message->header->mailbox, $id);
8405ee35 615 }
b74ba498 616 return( $body );
d4467150 617 }
618
8beafbbc 619 // A recursive function that returns a list of attachments with links
620 // to where to download these attachments
621 function formatAttachments ($message, $ent_id, $mailbox, $id) {
f4991a86 622 global $where, $what;
dd389be5 623 global $startMessage, $color;
719534f2 624 static $ShownHTML = 0;
b74ba498 625
626 $body = "";
627 if ($ShownHTML == 0) {
dd389be5 628 $ShownHTML = 1;
b74ba498 629
630 $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n" .
631 "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n" .
632 _("Attachments") . ':' .
633 "</B></TH></TR><TR><TD>\n" .
634 "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n" .
635 formatAttachments ($message, $ent_id, $mailbox, $id) .
636 "</TABLE></TD></TR></TABLE>";
637
638 return( $body );
dd389be5 639 }
b74ba498 640
8beafbbc 641 if ($message) {
642 if (!$message->entities) {
643 $type0 = strtolower($message->header->type0);
644 $type1 = strtolower($message->header->type1);
66e1a00e 645 $name = decodeHeader($message->header->name);
b74ba498 646
8beafbbc 647 if ($message->header->entity_id != $ent_id) {
888c82e2 648 $filename = decodeHeader($message->header->filename);
b74ba498 649 if (trim($filename) == '') {
650 if (trim($name) == '') {
651 if( trim( $message->header->id ) == '' )
652 $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
653 else
654 $display_filename = 'cid: ' . $message->header->id;
655 // $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
656 } else {
657 $display_filename = $name;
658 $filename = $name;
659 }
8beafbbc 660 } else {
661 $display_filename = $filename;
662 }
b74ba498 663
8beafbbc 664 $urlMailbox = urlencode($mailbox);
665 $ent = urlencode($message->header->entity_id);
b74ba498 666
667 $DefaultLink =
bc104ef3 668 "../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
dd389be5 669 if ($where && $what)
670 $DefaultLink .= '&where=' . urlencode($where) . '&what=' . urlencode($what);
701c9c6b 671 $Links['download link']['text'] = _("download");
b74ba498 672 $Links['download link']['href'] =
bc104ef3 673 "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
dd389be5 674 $ImageURL = '';
b74ba498 675
dd389be5 676 $HookResults = do_hook("attachment $type0/$type1", $Links,
b74ba498 677 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
ef30bf50 678 $display_filename, $where, $what);
dd389be5 679
680 $Links = $HookResults[1];
681 $DefaultLink = $HookResults[6];
682
b74ba498 683 $body .= '<TR><TD>&nbsp;&nbsp;</TD><TD>' .
684 "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>" .
685 '<TD><SMALL><b>' . show_readable_size($message->header->size) .
686 '</b>&nbsp;&nbsp;</small></TD>' .
687 "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>" .
688 '<TD><SMALL>';
ea48eb25 689 if ($message->header->description)
dd389be5 690 $body .= '<b>' . htmlspecialchars($message->header->description) . '</b>';
fde32e3f 691 $body .= '</SMALL></TD><TD><SMALL>&nbsp;';
b74ba498 692
693
dd389be5 694 $SkipSpaces = 1;
b74ba498 695 foreach ($Links as $Val) {
696 if ($SkipSpaces) {
dd389be5 697 $SkipSpaces = 0;
b74ba498 698 } else {
fde32e3f 699 $body .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
dd389be5 700 }
fde32e3f 701 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
dd389be5 702 }
b74ba498 703
dd389be5 704 unset($Links);
b74ba498 705
fde32e3f 706 $body .= "</SMALL></TD></TR>\n";
8beafbbc 707 }
8beafbbc 708 } else {
709 for ($i = 0; $i < count($message->entities); $i++) {
710 $body .= formatAttachments ($message->entities[$i], $ent_id, $mailbox, $id);
711 }
8beafbbc 712 }
b74ba498 713 return( $body );
8beafbbc 714 }
715 }
4809f489 716
717
718 /** this function decodes the body depending on the encoding type. **/
d4467150 719 function decodeBody($body, $encoding) {
623332f3 720 $body = str_replace("\r\n", "\n", $body);
d4467150 721 $encoding = strtolower($encoding);
7831268e 722
358f007e 723 global $show_html_default;
724
b74ba498 725 if ($encoding == 'quoted-printable') {
ef3f274f 726 $body = quoted_printable_decode($body);
b74ba498 727
728
729 /*
730 Following code has been comented as I see no reason for it.
731 If there is any please tell me a mingo@rotedic.com
732
ef3f274f 733 while (ereg("=\n", $body))
734 $body = ereg_replace ("=\n", "", $body);
b74ba498 735 */
736 } else if ($encoding == 'base64') {
ef3f274f 737 $body = base64_decode($body);
d4467150 738 }
b74ba498 739
a47b5200 740 // All other encodings are returned raw.
ef3f274f 741 return $body;
aceb0d5c 742 }
a4c2cd49 743
744
b74ba498 745 // This functions decode strings that is encoded according to
a4c2cd49 746 // RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
2e434774 747 function decodeHeader ($string) {
b74ba498 748 if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
a4c2cd49 749 $string, $res)) {
1fd97780 750 if (ucfirst($res[2]) == "B") {
751 $replace = base64_decode($res[3]);
a4c2cd49 752 } else {
1fd97780 753 $replace = ereg_replace("_", " ", $res[3]);
b74ba498 754 // Convert lowercase Quoted Printable to uppercase for
755 // quoted_printable_decode to understand it.
756 while (ereg("(=(([0-9][abcdef])|([abcdef][0-9])|([abcdef][abcdef])))", $replace, $res)) {
757 $replace = str_replace($res[1], strtoupper($res[1]), $replace);
758 }
a4c2cd49 759 $replace = quoted_printable_decode($replace);
760 }
761
1fd97780 762 $replace = charset_decode ($res[1], $replace);
a4c2cd49 763
9be55c4b 764 // Remove the name of the character set.
765 $string = eregi_replace ('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
a4c2cd49 766 $replace, $string);
9be55c4b 767
2e434774 768 // In case there should be more encoding in the string: recurse
769 return (decodeHeader($string));
b74ba498 770 } else
a4c2cd49 771 return ($string);
772 }
773
c3084273 774 // Encode a string according to RFC 1522 for use in headers if it
bb60fa3f 775 // contains 8-bit characters or anything that looks like it should
776 // be encoded.
c3084273 777 function encodeHeader ($string) {
778 global $default_charset;
b74ba498 779
f7b3ba37 780 // Encode only if the string contains 8-bit characters or =?
781 $j = strlen( $string );
782 $l = FALSE; // Must be encoded ?
783 $ret = '';
784 for( $i=0; $i < $j; ++$i) {
785 switch( $string{$i} ) {
786 case '=':
b74ba498 787 $ret .= '=3D';
788 break;
789 case '?':
790 $l = TRUE;
791 $ret .= '=3F';
792 break;
793 case '_':
794 $ret .= '=5F';
795 break;
796 case ' ':
797 $ret .= '_';
798 break;
799 default:
800 $k = ord( $string{$i} );
801 if( $k > 126 ) {
802 $ret .= sprintf("=%02X", $k);
803 $l = TRUE;
804 } else
805 $ret .= $string{$i};
f7b3ba37 806 }
807 }
b74ba498 808
f7b3ba37 809 if( $l )
810 $string = "=?$default_charset?Q?$ret?=";
b74ba498 811
f7b3ba37 812 return( $string );
813 }
c3084273 814
a3daaaf3 815 /*
816 Strips dangerous tags from html messages.
817 */
818
819 function MagicHTML( $body, $id ) {
820
821 global $message, $PHP_SELF, $HTTP_SERVER_VARS;
822
823 $j = strlen( $body ); // Legnth of the HTML
824 $ret = ''; // Returned string
825 $bgcolor = '#ffffff'; // Background style color (defaults to white)
05c7f8f2 826 $textcolor = '#000000'; // Foreground style color (defaults to black)
a3daaaf3 827 $leftmargin = ''; // Left margin style
828 $title = ''; // HTML title if any
829
830 $i = 0;
831 while( $i < $j ) {
832 if( $body{$i} == '<' ) {
05c7f8f2 833 $pos = $i + 1;
834 $tag = '';
835 while ($body{$pos} == ' ' || $body{$pos} == "\t" ||
836 $body{$pos} == "\n")
837 $pos ++;
838 while (strlen($tag) < 4 && $body{$pos} != ' ' &&
839 $body{$pos} != "\t" && $body{$pos} != "\n") {
840 $tag .= $body{$pos};
841 $pos ++;
842 }
a3daaaf3 843 switch( strtoupper( $tag ) ) {
844 // Strips the entire tag and contents
845 case 'APPL':
846 case 'EMBB':
847 case 'FRAM':
848 case 'SCRI':
849 case 'OBJE':
850 $etg = '/' . $tag;
851 while( $body{$i+1}.$body{$i+2}.$body{$i+3}.$body{$i+4}.$body{$i+5} <> $etg &&
852 $i < $j ) $i++;
853 while( $i < $j && $body{++$i} <> '>' );
854 // $ret .= "<!-- $tag removed -->";
855 break;
856 // Substitute Title
857 case 'TITL':
858 $i += 5;
859 while( $body{$i} <> '>' && // </title>
860 $i < $j )
861 $i++;
862 $i++;
863 $title = '';
864 while( $body{$i} <> '<' && // </title>
865 $i < $j ) {
866 $title .= $body{$i};
867 $i++;
868 }
869 $i += 7;
870 break;
871 // Destroy these tags
872 case 'HTML':
873 case 'HEAD':
874 case '/HTM':
875 case '/HEA':
876 case '!DOC':
877 case 'META':
878 case 'DIV ':
879 case '/DIV':
880 case '!-- ':
881 $i += 4;
882 while( $body{$i} <> '>' &&
883 $i < $j )
884 $i++;
885 // $i++;
886 break;
887 case 'STYL':
888 $i += 5;
889 while( $body{$i} <> '>' && // </title>
890 $i < $j )
891 $i++;
892 $i++;
893 // We parse the style to look for interesting stuff
894 $styleblk = '';
895 while( $body{$i} <> '>' &&
896 $i < $j ) {
897 // First we get the name of the style
898 $style = '';
899 while( $body{$i} <> '>' &&
900 $body{$i} <> '<' &&
901 $body{$i} <> '{' &&
902 $i < $j ) {
903 if( isnoSep( $body{$i} ) )
904 $style .= $body{$i};
905 $i++;
906 }
5487a8d0 907 stripComments( $i, $j, $body );
a3daaaf3 908 $style = strtoupper( trim( $style ) );
909 if( $style == 'BODY' ) {
910 // Next we look into the definitions of the body style
911 while( $body{$i} <> '>' &&
912 $body{$i} <> '}' &&
913 $i < $j ) {
914 // We look for the background color if any.
915 if( substr( $body, $i, 17 ) == 'BACKGROUND-COLOR:' ) {
916 $i += 17;
917 $bgcolor = getStyleData( $i, $j, $body );
918 } elseif ( substr( $body, $i, 12 ) == 'MARGIN-LEFT:' ) {
919 $i += 12;
920 $leftmargin = getStyleData( $i, $j, $body );
921 }
922 $i++;
923 }
924 } else {
925 // Other style are mantained
926 $styleblk .= "$style ";
927 while( $body{$i} <> '>' &&
928 $body{$i} <> '<' &&
929 $body{$i} <> '}' &&
930 $i < $j ) {
931 $styleblk .= $body{$i};
932 $i++;
933 }
934 $styleblk .= $body{$i};
935 }
5487a8d0 936 stripComments( $i, $j, $body );
a3daaaf3 937 if( $body{$i} <> '>' )
938 $i++;
939 }
940 if( $styleblk <> '' )
941 $ret .= "<style>$styleblk";
942 break;
943 case 'BODY':
944 if( $title <> '' )
945 $ret .= '<b>' . _("Title:") . " </b>$title<br>\n";
946 $ret .= "<TABLE";
947 $i += 5;
6dc0e464 948 if (! isset($base))
949 $base = '';
950 $ret .= stripEvent( $i, $j, $body, $id, $base );
951 $ret .= " bgcolor=$bgcolor width=\"100%\"><tr>";
a3daaaf3 952 if( $leftmargin <> '' )
953 $ret .= "<td width=$leftmargin>&nbsp;</td>";
954 $ret .= '<td>';
6dc0e464 955 if (strtolower($bgcolor) == 'ffffff' ||
956 strtolower($bgcolor) == '#ffffff')
957 $ret .= '<font color=#000000>';
a3daaaf3 958 break;
959 case 'BASE':
960 $i += 5;
961 $base = '';
962 while( !isNoSep( $body{$i} ) &&
963 $i < $j )
964 $i++;
965 if( strcasecmp( substr( $base, 0, 4 ), 'href' ) ) {
966 $i += 5;
967 while( !isNoSep( $body{$i} ) &&
968 $i < $j )
969 $i++;
970 while( $body{$i} <> '>' &&
971 $i < $j ) {
972 if( $body{$i} <> '"' )
973 $base .= $body{$i};
974 $i++;
975 }
976 // Debuging $ret .= "<!-- base == $base -->";
977 if( strcasecmp( substr( $base, 0, 4 ), 'file' ) <> 0 )
978 $ret .= "\n<BASE HREF=\"$base\">\n";
979 }
980 break;
981 case '/BOD':
05c7f8f2 982 $ret .= '</font></td></tr></TABLE>';
a3daaaf3 983 $i += 6;
984 break;
985 default:
986 // Following tags can contain some event handler, lets search it
987 stripComments( $i, $j, $body );
4aaea201 988 if (! isset($base))
989 $base = '';
a3daaaf3 990 $ret .= stripEvent( $i, $j, $body, $id, $base ) . '>';
991 // $ret .= "<!-- $tag detected -->";
992 }
993 } else {
994 $ret .= $body{$i};
995 }
996 $i++;
997 }
998
999 return( "\n\n<!-- HTML Output ahead -->\n" .
1000 $ret .
1001 "\n<!-- END of HTML Output --><base href=\"".
1002 $HTTP_SERVER_VARS["SERVER_NAME"] . substr( $PHP_SELF, 0, strlen( $PHP_SELF ) - 13 ) .
1003 "\">\n\n" );
1004 }
1005
1006 function isNoSep( $char ) {
1007
1008 switch( $char ) {
1009 case ' ':
1010 case "\n":
1011 case "\t":
1012 case "\r":
1013 case '>':
1014 case '"':
1015 return( FALSE );
1016 break;
1017 default:
1018 return( TRUE );
1019 }
1020
1021 }
1022
1023 /*
1024 The following function is usefull to remove extra data that can cause
1025 html not to display properly. Especialy with MS stuff.
1026 */
1027
1028 function stripComments( &$i, $j, &$body ) {
1029
1030 while( $body{$i}.$body{$i+1}.$body{$i+2}.$body{$i+3} == '<!--' &&
1031 $i < $j ) {
1032 $i += 5;
1033 while( $body{$i-2}.$body{$i-1}.$body{$i} <> '-->' &&
1034 $i < $j )
1035 $i++;
1036 $i++;
1037 }
1038
1039 return;
1040
1041 }
1042
1043 /* Gets the style data of a specific style */
1044
1045 function getStyleData( &$i, $j, &$body ) {
1046
1047 // We skip spaces
1048 while( $body{$i} <> '>' && !isNoSep( $body{$i} ) &&
1049 $i < $j ) {
1050 $i++;
1051 }
1052 // And get the color
1053 $ret = '';
1054 while( isNoSep( $body{$i} ) &&
1055 $i < $j ) {
1056 $ret .= $body{$i};
1057 $i++;
1058 }
1059
1060 return( $ret );
1061 }
1062
1063 /*
1064 Private function for strip_dangerous_tag. Look for event based coded and "remove" it
1065 change on with no (onload -> noload)
1066 */
1067
1068 function stripEvent( &$i, $j, &$body, $id, $base ) {
1069
1070 global $message;
1071
1072 $ret = '';
1073
1074 while( $body{$i} <> '>' &&
1075 $i < $j ) {
1076 $etg = strtolower($body{$i}.$body{$i+1}.$body{$i+2});
1077 switch( $etg ) {
1078 case '../':
1079 // Retrolinks are not allowed without a base because they mess with SM security
1080 if( $base == '' ) {
1081 $i += 2;
1082 } else {
1083 $ret .= '.';
1084 }
1085 break;
1086 case 'cid':
1087 // Internal link
1088 $k = $i-1;
1089 if( $body{$i+3} == ':') {
1090 $i +=4;
1091 $name = '';
1092 while( isNoSep( $body{$i} ) &&
1093 $i < $j )
1094 $name .= $body{$i++};
1095 if( $name <> '' ) {
1096 $ret .= "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=" .
1097 urlencode( $message->header->mailbox ) .
1098 "&passed_ent_id=" . find_ent_id( $name, $message );
1099 if( $body{$k} == '"' )
1100 $ret .= '" ';
1101 else
1102 $ret .= ' ';
1103 }
1104 if( $body{$i} == '>' )
1105 $i -= 1;
1106 }
1107 break;
1108 case ' on':
1109 case "\non":
1110 case "\ron":
1111 case "\ton":
1112 $ret .= ' no';
1113 $i += 2;
1114 break;
1115 case 'pt:':
1116 if( strcasecmp( $body{$i-4}.$body{$i-3}.$body{$i-2}.$body{$i-1}.$body{$i}.$body{$i+1}.$body{$i+2}, 'script:') == 0 ) {
1117 $ret .= '_no/';
1118 } else {
1119 $ret .= $etg;
1120 }
1121 $i += 2;
1122 break;
1123 default:
1124 $ret .= $body{$i};
1125 }
1126 $i++;
1127 }
1128 return( $ret );
1129 }
1130
1131
1132 /* This function trys to locate the entity_id of a specific mime element */
1133
1134 function find_ent_id( $id, $message ) {
1135
1136 $ret = '';
1137 for ($i=0; $ret == '' && $i < count($message->entities); $i++) {
1138
1139 if( $message->entities[$i]->header->entity_id == '' ) {
1140 $ret = find_ent_id( $id, $message->entities[$i] );
1141 } else {
1142 if( strcasecmp( $message->entities[$i]->header->id, $id ) == 0 )
1143 $ret = $message->entities[$i]->header->entity_id;
1144 }
1145
1146 }
1147
1148 return( $ret );
1149
1150 }
5487a8d0 1151?>