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