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