fixes sort order when using server-side and $sort=6
[squirrelmail.git] / functions / mime.php
CommitLineData
59177427 1<?php
2ba13803 2
35586184 3/**
4 * mime.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 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 15require_once('../functions/imap.php');
16require_once('../functions/attachment_common.php');
8beafbbc 17
35586184 18/** Setting up the objects that have the structure for the message **/
19class msg_header {
20 /** msg_header contains generic variables for values that **/
21 /** could be in a header. **/
b74ba498 22
88cb1b4d 23 var $type0 = '', $type1 = '', $boundary = '', $charset = '',
24 $encoding = '', $size = 0, $to = array(), $from = '', $date = '',
25 $cc = array(), $bcc = array(), $reply_to = '', $subject = '',
26 $id = 0, $mailbox = '', $description = '', $filename = '',
c9d78ab4 27 $entity_id = 0, $message_id = 0, $name = '', $priority = 3, $type = '';
35586184 28}
b74ba498 29
451f74a2 30class message {
31 /** message is the object that contains messages. It is a recursive
32 object in that through the $entities variable, it can contain
33 more objects of type message. See documentation in mime.txt for
34 a better description of how this works.
35 **/
77b88425 36 var $header = '', $entities = array();
37
451f74a2 38 function addEntity ($msg) {
39 $this->entities[] = $msg;
40 }
41}
8beafbbc 42
451f74a2 43/* --------------------------------------------------------------------------------- */
44/* MIME DECODING */
45/* --------------------------------------------------------------------------------- */
b74ba498 46
451f74a2 47/* This function gets the structure of a message and stores it in the "message" class.
48 * It will return this object for use with all relevant header information and
49 * fully parsed into the standard "message" object format.
50 */
51function mime_structure ($imap_stream, $header) {
52
451f74a2 53 $ssid = sqimap_session_id();
54 $lsid = strlen( $ssid );
55 $id = $header->id;
56 fputs ($imap_stream, "$ssid FETCH $id BODYSTRUCTURE\r\n");
57 //
58 // This should use sqimap_read_data instead of reading it itself
59 //
77b88425 60 $read = fgets ($imap_stream, 9216);
451f74a2 61 $bodystructure = '';
62 while ( substr($read, 0, $lsid) <> $ssid &&
63 !feof( $imap_stream ) ) {
64 $bodystructure .= $read;
77b88425 65 $read = fgets ($imap_stream, 9216);
451f74a2 66 }
67 $read = $bodystructure;
77b88425 68
451f74a2 69 // isolate the body structure and remove beginning and end parenthesis
70 $read = trim(substr ($read, strpos(strtolower($read), 'bodystructure') + 13));
c9d78ab4 71
451f74a2 72 $read = trim(substr ($read, 0, -1));
73 $end = mime_match_parenthesis(0, $read);
74 while ($end == strlen($read)-1) {
75 $read = trim(substr ($read, 0, -1));
76 $read = trim(substr ($read, 1));
77 $end = mime_match_parenthesis(0, $read);
78 }
77b88425 79
451f74a2 80 $msg = mime_parse_structure ($read, 0);
81 $msg->header = $header;
77b88425 82
451f74a2 83 return( $msg );
84}
b74ba498 85
451f74a2 86/* this starts the parsing of a particular structure. It is called recursively,
87 * so it can be passed different structures. It returns an object of type
88 * $message.
89 * First, it checks to see if it is a multipart message. If it is, then it
90 * handles that as it sees is necessary. If it is just a regular entity,
91 * then it parses it and adds the necessary header information (by calling out
92 * to mime_get_elements()
93 */
94function mime_parse_structure ($structure, $ent_id) {
c9d78ab4 95 $properties = array();
451f74a2 96 $msg = new message();
97 if ($structure{0} == '(') {
c9d78ab4 98 $old_ent_id = $ent_id;
451f74a2 99 $ent_id = mime_new_element_level($ent_id);
100 $start = $end = -1;
101 do {
102 $start = $end+1;
103 $end = mime_match_parenthesis ($start, $structure);
c9d78ab4 104 /* add "forgotten" parent entities (alternative and relative) */
ead50b42 105 if (strpos($ent_id, '0') || strpos($ent_id, '0') == 0) {
c9d78ab4 106 $str = substr($structure, $end+1 );
107 $startprop = strrpos($str,'(');
108 $endprop = strrpos($str,')');
109 $propstr = substr($str, $startprop + 1, ($endprop - $startprop)-1);
110
111 $type1 = trim(substr($str,0, $startprop));
112 $pos = strrpos($type1,' ');
113 $type1 = strtolower(trim(substr($type1,$pos +1)));
114 $cnt = strlen($type1);
115 $type1 = substr($type1,0,$cnt-1);
116
117 $properties = mime_get_props($properties, $propstr);
118 if (count($properties)>0) {
119 $msg->header->entity_id = $old_ent_id;
120 $msg->header->type0 = 'multipart';
121 $msg->header->type1 = $type1;
122 }
123 for ($i=0; $i < count($properties); $i++) {
124 $msg->header->{$properties[$i]['name']} = $properties[$i]['value'];
125 $name = $properties[$i]['name'];
126 $value = $properties[$i]['value'];
127 }
128 }
451f74a2 129
130 $element = substr($structure, $start+1, ($end - $start)-1);
c9d78ab4 131
451f74a2 132 $ent_id = mime_increment_id ($ent_id);
133 $newmsg = mime_parse_structure ($element, $ent_id);
134 $msg->addEntity ($newmsg);
c9d78ab4 135
451f74a2 136 } while ($structure{$end+1} == '(');
137 } else {
138 // parse the elements
c9d78ab4 139 $msg = mime_get_element ($structure, $msg, $ent_id);
451f74a2 140 }
141 return $msg;
142}
e4a256af 143
451f74a2 144/* Increments the element ID. An element id can look like any of
145 * the following: 1, 1.2, 4.3.2.4.1, etc. This function increments
146 * the last number of the element id, changing 1.2 to 1.3.
147 */
148function mime_increment_id ($id) {
149
77b88425 150 if (strpos($id, '.')) {
151 $first = substr($id, 0, strrpos($id, '.'));
152 $last = substr($id, strrpos($id, '.')+1);
451f74a2 153 $last++;
77b88425 154 $new = $first . '.' .$last;
451f74a2 155 } else {
156 $new = $id + 1;
157 }
77b88425 158
451f74a2 159 return $new;
160}
161
162/*
163 * See comment for mime_increment_id().
164 * This adds another level on to the entity_id changing 1.3 to 1.3.0
165 * NOTE: 1.3.0 is not a valid element ID. It MUST be incremented
166 * before it can be used. I left it this way so as not to have
167 * to make a special case if it is the first entity_id. It
168 * always increments it, and that works fine.
169 */
170function mime_new_element_level ($id) {
171
77b88425 172 if (!$id) {
173 $id = 0;
174 } else {
175 $id = $id . '.0';
176 }
451f74a2 177
77b88425 178 return( $id );
451f74a2 179}
180
181function mime_get_element (&$structure, $msg, $ent_id) {
182
183 $elem_num = 1;
184 $msg->header = new msg_header();
185 $msg->header->entity_id = $ent_id;
186 $properties = array();
451f74a2 187 while (strlen($structure) > 0) {
188 $structure = trim($structure);
189 $char = $structure{0};
190
191 if (strtolower(substr($structure, 0, 3)) == 'nil') {
192 $text = '';
193 $structure = substr($structure, 3);
194 } else if ($char == '"') {
195 // loop through until we find the matching quote, and return that as a string
196 $pos = 1;
197 $text = '';
198 while ( ($char = $structure{$pos} ) <> '"' && $pos < strlen($structure)) {
199 $text .= $char;
200 $pos++;
201 }
202 $structure = substr($structure, strlen($text) + 2);
203 } else if ($char == '(') {
204 // comment me
205 $end = mime_match_parenthesis (0, $structure);
206 $sub = substr($structure, 1, $end-1);
207 $properties = mime_get_props($properties, $sub);
208 $structure = substr($structure, strlen($sub) + 2);
209 } else {
210 // loop through until we find a space or an end parenthesis
211 $pos = 0;
212 $char = $structure{$pos};
213 $text = '';
214 while ($char != ' ' && $char != ')' && $pos < strlen($structure)) {
215 $text .= $char;
216 $pos++;
217 $char = $structure{$pos};
218 }
219 $structure = substr($structure, strlen($text));
220 }
221
222 // This is where all the text parts get put into the header
223 switch ($elem_num) {
224 case 1:
225 $msg->header->type0 = strtolower($text);
226 break;
227 case 2:
228 $msg->header->type1 = strtolower($text);
229 break;
230 case 4: // Id
231 // Invisimail enclose images with <>
232 $msg->header->id = str_replace( '<', '', str_replace( '>', '', $text ) );
233 break;
234 case 5:
235 $msg->header->description = $text;
236 break;
237 case 6:
238 $msg->header->encoding = strtolower($text);
239 break;
240 case 7:
241 $msg->header->size = $text;
242 break;
243 default:
244 if ($msg->header->type0 == 'text' && $elem_num == 8) {
245 // This is a plain text message, so lets get the number of lines
246 // that it contains.
247 $msg->header->num_lines = $text;
248
249 } else if ($msg->header->type0 == 'message' && $msg->header->type1 == 'rfc822' && $elem_num == 8) {
250 // This is an encapsulated message, so lets start all over again and
251 // parse this message adding it on to the existing one.
252 $structure = trim($structure);
253 if ( $structure{0} == '(' ) {
254 $e = mime_match_parenthesis (0, $structure);
255 $structure = substr($structure, 0, $e);
256 $structure = substr($structure, 1);
257 $m = mime_parse_structure($structure, $msg->header->entity_id);
258
259 // the following conditional is there to correct a bug that wasn't
260 // incrementing the entity IDs correctly because of the special case
261 // that message/rfc822 is. This fixes it fine.
262 if (substr($structure, 1, 1) != '(')
263 $m->header->entity_id = mime_increment_id(mime_new_element_level($ent_id));
264
265 // Now we'll go through and reformat the results.
266 if ($m->entities) {
267 for ($i=0; $i < count($m->entities); $i++) {
268 $msg->addEntity($m->entities[$i]);
269 }
270 } else {
271 $msg->addEntity($m);
272 }
273 $structure = "";
274 }
275 }
276 break;
277 }
278 $elem_num++;
279 $text = "";
280 }
281 // loop through the additional properties and put those in the various headers
c9d78ab4 282 for ($i=0; $i < count($properties); $i++) {
283 $msg->header->{$properties[$i]['name']} = $properties[$i]['value'];
284 }
451f74a2 285
286 return $msg;
287}
288
289/*
290 * I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't
291 * figure out how to do this part, so I decided to go to bed. I woke up
292 * in the morning and had a flash of insight. I went to the white-board
293 * and scribbled it out, then spent a bit programming it, and this is the
294 * result. Nothing complicated, but I think my brain was fried yesterday.
295 * Funny how that happens some times.
296 *
297 * This gets properties in a nested parenthesisized list. For example,
298 * this would get passed something like: ("attachment" ("filename" "luke.tar.gz"))
299 * This returns an array called $props with all paired up properties.
300 * It ignores the "attachment" for now, maybe that should change later
301 * down the road. In this case, what is returned is:
302 * $props[0]["name"] = "filename";
303 * $props[0]["value"] = "luke.tar.gz";
304 */
305function mime_get_props ($props, $structure) {
306
307 while (strlen($structure) > 0) {
308 $structure = trim($structure);
309 $char = $structure{0};
451f74a2 310 if ($char == '"') {
311 $pos = 1;
312 $tmp = '';
313 while ( ( $char = $structure{$pos} ) != '"' &&
314 $pos < strlen($structure)) {
315 $tmp .= $char;
316 $pos++;
317 }
318 $structure = trim(substr($structure, strlen($tmp) + 2));
319 $char = $structure{0};
320
321 if ($char == '"') {
322 $pos = 1;
323 $value = '';
324 while ( ( $char = $structure{$pos} ) != '"' &&
325 $pos < strlen($structure) ) {
326 $value .= $char;
327 $pos++;
328 }
c9d78ab4 329 $structure = trim(substr($structure, strlen($value) + 2));
451f74a2 330 $k = count($props);
331 $props[$k]['name'] = strtolower($tmp);
332 $props[$k]['value'] = $value;
c9d78ab4 333 if ($structure != '') {
334 mime_get_props($props, $structure);
335 } else {
336 return $props;
337 }
451f74a2 338 } else if ($char == '(') {
339 $end = mime_match_parenthesis (0, $structure);
340 $sub = substr($structure, 1, $end-1);
c9d78ab4 341 if (! isset($props))
342 $props = array();
343 $props = mime_get_props($props, $sub);
344 $structure = substr($structure, strlen($sub) + 2);
345 return $props;
451f74a2 346 }
451f74a2 347 } else if ($char == '(') {
348 $end = mime_match_parenthesis (0, $structure);
349 $sub = substr($structure, 1, $end-1);
350 $props = mime_get_props($props, $sub);
351 $structure = substr($structure, strlen($sub) + 2);
352 return $props;
353 } else {
354 return $props;
355 }
356 }
357}
358
359/*
360 * Matches parenthesis. It will return the position of the matching
361 * parenthesis in $structure. For instance, if $structure was:
362 * ("text" "plain" ("val1name", "1") nil ... )
363 * x x
364 * then this would return 42 to match up those two.
365 */
366function mime_match_parenthesis ($pos, $structure) {
367
368 $j = strlen( $structure );
369
370 // ignore all extra characters
371 // If inside of a string, skip string -- Boundary IDs and other
372 // things can have ) in them.
373 if ( $structure{$pos} != '(' ) {
374 return( $j );
375 }
376
377 while ( $pos < $j ) {
378 $pos++;
379 if ($structure{$pos} == ')') {
8beafbbc 380 return $pos;
451f74a2 381 } elseif ($structure{$pos} == '"') {
b74ba498 382 $pos++;
451f74a2 383 while ( $structure{$pos} != '"' &&
384 $pos < $j ) {
385 if (substr($structure, $pos, 2) == '\\"') {
b74ba498 386 $pos++;
451f74a2 387 } elseif (substr($structure, $pos, 2) == '\\\\') {
b74ba498 388 $pos++;
451f74a2 389 }
b74ba498 390 $pos++;
5ffe5a7e 391 }
451f74a2 392 } elseif ( $structure{$pos} == '(' ) {
8beafbbc 393 $pos = mime_match_parenthesis ($pos, $structure);
451f74a2 394 }
395 }
396 echo _("Error decoding mime structure. Report this as a bug!") . '<br>';
397 return( $pos );
398}
399
346817d4 400function mime_fetch_body($imap_stream, $id, $ent_id ) {
09a4bde3 401
402 /*
403 * do a bit of error correction. If we couldn't find the entity id, just guess
404 * that it is the first one. That is usually the case anyway.
405 */
406 if (!$ent_id) {
451f74a2 407 $ent_id = 1;
09a4bde3 408 }
346817d4 409
6ab1bd9e 410 $cmd = "FETCH $id BODY[$ent_id]";
346817d4 411 $data = sqimap_run_command ($imap_stream, $cmd, true, $response, $message);
77b88425 412
413 do {
e976319c 414 $topline = trim(array_shift( $data ));
415 } while( $topline && $topline[0] == '*' && !preg_match( '/\* [0-9]+ FETCH.*/i', $topline )) ;
451f74a2 416 $wholemessage = implode('', $data);
417 if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
77b88425 418
451f74a2 419 $ret = substr( $wholemessage, 0, $regs[1] );
420 /*
421 There is some information in the content info header that could be important
422 in order to parse html messages. Let's get them here.
423 */
424 if ( $ret{0} == '<' ) {
1c72b151 425 $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message);
e5ea9327 426 /* BASE within HTML documents is illegal (see w3 spec)
427* $base = '';
428* $k = 10;
429* foreach( $data as $d ) {
430* if ( substr( $d, 0, 13 ) == 'Content-Base:' ) {
431* $j = strlen( $d );
432* $i = 13;
433* $base = '';
434* while ( $i < $j &&
435* ( !isNoSep( $d{$i} ) || $d{$i} == '"' ) )
436* $i++;
437* while ( $i < $j ) {
438* if ( isNoSep( $d{$i} ) )
439* $base .= $d{$i};
440* $i++;
441* }
442* $k = 0;
443* } elseif ( $k == 1 && !isnosep( $d{0} ) ) {
444* $base .= substr( $d, 1 );
445* }
446* $k++;
447* }
448* if ( $base <> '' ) {
449* $ret = "<base href=\"$base\">" . $ret;
450* }
7e235a1a 451* */
451f74a2 452 }
453 } else if (ereg('"([^"]*)"', $topline, $regs)) {
454 $ret = $regs[1];
455 } else {
456 global $where, $what, $mailbox, $passed_id, $startMessage;
e5ea9327 457 $par = 'mailbox=' . urlencode($mailbox) . "&amp;passed_id=$passed_id";
451f74a2 458 if (isset($where) && isset($what)) {
e5ea9327 459 $par .= '&amp;where='. urlencode($where) . "&amp;what=" . urlencode($what);
a3daaaf3 460 } else {
e5ea9327 461 $par .= "&amp;startMessage=$startMessage&amp;show_more=0";
451f74a2 462 }
e5ea9327 463 $par .= '&amp;response=' . urlencode($response) .
464 '&amp;message=' . urlencode($message).
465 '&amp;topline=' . urlencode($topline);
a019eeb8 466
346817d4 467 echo '<tt><br>' .
468 '<table width="80%"><tr>' .
469 '<tr><td colspan=2>' .
451f74a2 470 _("Body retrieval error. The reason for this is most probably that the message is malformed. Please help us making future versions better by submitting this message to the developers knowledgebase!") .
346817d4 471 " <A HREF=\"../src/retrievalerror.php?$par\"><br>" .
472 _("Submit message") . '</A><BR>&nbsp;' .
473 '</td></tr>' .
474 '<td><b>' . _("Command:") . "</td><td>$cmd</td></tr>" .
475 '<td><b>' . _("Response:") . "</td><td>$response</td></tr>" .
476 '<td><b>' . _("Message:") . "</td><td>$message</td></tr>" .
477 '<td><b>' . _("FETCH line:") . "</td><td>$topline</td></tr>" .
478 "</table><BR></tt></font><hr>";
479
1c72b151 480 $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message);
451f74a2 481 array_shift($data);
482 $wholemessage = implode('', $data);
a019eeb8 483
346817d4 484 $ret = $wholemessage;
a3daaaf3 485 }
451f74a2 486 return( $ret );
487}
d4467150 488
451f74a2 489function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
490 // do a bit of error correction. If we couldn't find the entity id, just guess
491 // that it is the first one. That is usually the case anyway.
492 if (!$ent_id) {
493 $ent_id = 1;
494 }
495 $sid = sqimap_session_id();
496 // Don't kill the connection if the browser is over a dialup
497 // and it would take over 30 seconds to download it.
b7206e1d 498
499 // donīt call set_time_limit in safe mode.
500 if (!ini_get("safe_mode")) {
501 set_time_limit(0);
502 }
346817d4 503
451f74a2 504 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
505 $cnt = 0;
506 $continue = true;
507 $read = fgets ($imap_stream,4096);
508 // This could be bad -- if the section has sqimap_session_id() . ' OK'
509 // or similar, it will kill the download.
510 while (!ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
511 if (trim($read) == ')==') {
512 $read1 = $read;
b74ba498 513 $read = fgets ($imap_stream,4096);
451f74a2 514 if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
515 return;
516 } else {
517 echo decodeBody($read1, $encoding) .
518 decodeBody($read, $encoding);
519 }
520 } else if ($cnt) {
521 echo decodeBody($read, $encoding);
b74ba498 522 }
451f74a2 523 $read = fgets ($imap_stream,4096);
524 $cnt++;
525 }
526}
beb9e459 527
451f74a2 528/* -[ END MIME DECODING ]----------------------------------------------------------- */
d4467150 529
aceb0d5c 530
d4467150 531
451f74a2 532/* This is the first function called. It decides if this is a multipart
533 message or if it should be handled as a single entity
534 */
535function decodeMime ($imap_stream, &$header) {
536 global $username, $key, $imapServerAddress, $imapPort;
537 return mime_structure ($imap_stream, $header);
538}
ea48eb25 539
451f74a2 540// This is here for debugging purposese. It will print out a list
541// of all the entity IDs that are in the $message object.
f0c4dc12 542
451f74a2 543function listEntities ($message) {
544if ($message) {
545 if ($message->header->entity_id)
546 echo "<tt>" . $message->header->entity_id . ' : ' . $message->header->type0 . '/' . $message->header->type1 . '<br>';
547 for ($i = 0; $message->entities[$i]; $i++) {
548 $msg = listEntities($message->entities[$i], $ent_id);
549 if ($msg)
550 return $msg;
551 }
552}
553}
f0c4dc12 554
451f74a2 555
556/* returns a $message object for a particular entity id */
557function getEntity ($message, $ent_id) {
558 if ($message) {
c9d78ab4 559 if ($message->header->entity_id == $ent_id && strlen($ent_id) == strlen($message->header->entity_id))
560 {
8beafbbc 561 return $message;
451f74a2 562 } else {
cd928157 563 for ($i = 0; isset($message->entities[$i]); $i++) {
451f74a2 564 $msg = getEntity ($message->entities[$i], $ent_id);
565 if ($msg) {
566 return $msg;
c9d78ab4 567 }
b1dadc61 568 }
451f74a2 569 }
570 }
571}
8beafbbc 572
451f74a2 573/*
574 * figures out what entity to display and returns the $message object
575 * for that entity.
576 */
c9d78ab4 577function findDisplayEntity ($msg, $textOnly = 1) {
451f74a2 578 global $show_html_default;
579
580 $entity = 0;
581
c9d78ab4 582 if ($msg) {
583 if ( $msg->header->type0 == 'multipart' &&
584 ( $msg->header->type1 == 'alternative' ||
585 $msg->header->type1 == 'mixed' ||
586 $msg->header->type1 == 'related' ) &&
451f74a2 587 $show_html_default && ! $textOnly ) {
c9d78ab4 588 $entity = findDisplayEntityHTML($msg);
451f74a2 589 }
f0c4dc12 590
451f74a2 591 // Show text/plain or text/html -- the first one we find.
592 if ( $entity == 0 &&
c9d78ab4 593 $msg->header->type0 == 'text' &&
594 ( $msg->header->type1 == 'plain' ||
595 $msg->header->type1 == 'html' ) &&
596 isset($msg->header->entity_id) ) {
597 $entity = $msg->header->entity_id;
451f74a2 598 }
599
600 $i = 0;
c9d78ab4 601 while ($entity == 0 && isset($msg->entities[$i]) ) {
602 $entity = findDisplayEntity($msg->entities[$i], $textOnly);
451f74a2 603 $i++;
a3daaaf3 604 }
a3daaaf3 605 }
451f74a2 606
607 return( $entity );
608}
b74ba498 609
451f74a2 610/* Shows the HTML version */
611function findDisplayEntityHTML ($message) {
8405ee35 612
451f74a2 613 if ( $message->header->type0 == 'text' &&
614 $message->header->type1 == 'html' &&
615 isset($message->header->entity_id)) {
616 return $message->header->entity_id;
617 }
618 for ($i = 0; isset($message->entities[$i]); $i ++) {
f0c4dc12 619 if ( $message->header->type0 == 'message' &&
620 $message->header->type1 == 'rfc822' &&
621 isset($message->header->entity_id)) {
622 return 0;
623 }
451f74a2 624 $entity = findDisplayEntityHTML($message->entities[$i]);
625 if ($entity != 0) {
626 return $entity;
627 }
628 }
c9d78ab4 629
451f74a2 630 return 0;
631}
632
da4c66e8 633/*
634 * translateText
635 * Extracted from strings.php 23/03/2002
636 */
637
638function translateText(&$body, $wrap_at, $charset) {
639 global $where, $what; /* from searching */
640 global $color; /* color theme */
641
642 require_once('../functions/url_parser.php');
643
644 $body_ary = explode("\n", $body);
645 $PriorQuotes = 0;
646 for ($i=0; $i < count($body_ary); $i++) {
647 $line = $body_ary[$i];
648 if (strlen($line) - 2 >= $wrap_at) {
649 sqWordWrap($line, $wrap_at);
650 }
651 $line = charset_decode($charset, $line);
652 $line = str_replace("\t", ' ', $line);
653
654 parseUrl ($line);
655
656 $Quotes = 0;
657 $pos = 0;
658 $j = strlen( $line );
659
660 while ( $pos < $j ) {
661 if ($line[$pos] == ' ') {
662 $pos ++;
663 } else if (strpos($line, '&gt;', $pos) === $pos) {
664 $pos += 4;
665 $Quotes ++;
666 } else {
667 break;
668 }
669 }
670
671 if ($Quotes > 1) {
672 if (! isset($color[14])) {
673 $color[14] = '#FF0000';
674 }
675 $line = '<FONT COLOR="' . $color[14] . '">' . $line . '</FONT>';
676 } elseif ($Quotes) {
677 if (! isset($color[13])) {
678 $color[13] = '#800000';
679 }
680 $line = '<FONT COLOR="' . $color[13] . '">' . $line . '</FONT>';
681 }
682
683 $body_ary[$i] = $line;
684 }
685 $body = '<pre>' . implode("\n", $body_ary) . '</pre>';
686}
687
c9d78ab4 688/* debugfunction for looping through entities and displaying correct entities */
689function listMyEntities ($message) {
690
691if ($message) {
692 if ($message->header->entity_id) {
693 echo "<tt>" . $message->header->entity_id . ' : ' . $message->header->type0 . '/' . $message->header->type1 . '<br>';
694 }
695 if (!($message->header->type0 == 'message' && $message->header->type1 == 'rfc822')) {
696 if (isset($message->header->boundary) ) {
697 $ent_id = $message->header->entity_id;
698 $var = $message->header->boundary;
699 if ($var !='')
700 echo "<b>$ent_id boundary = $var</b><br>";
701 }
702 if (isset($message->header->type) ) {
703 $var = $message->header->type;
704 if ($var !='')
705 echo "<b>$ent_id type = $var</b><br>";
706 }
707 for ($i = 0; $message->entities[$i]; $i++) {
708 $msg = listMyEntities($message->entities[$i]);
709 }
710
711 if ($msg ) return $msg;
712 }
713}
714
715}
716
717
718
451f74a2 719/* This returns a parsed string called $body. That string can then
720be displayed as the actual message in the HTML. It contains
721everything needed, including HTML Tags, Attachments at the
722bottom, etc.
723*/
724function formatBody($imap_stream, $message, $color, $wrap_at) {
725 // this if statement checks for the entity to show as the
726 // primary message. To add more of them, just put them in the
727 // order that is their priority.
e198531f 728 global $startMessage, $username, $key, $imapServerAddress, $imapPort, $body,
d03c24f4 729 $show_html_default, $has_unsafe_images, $view_unsafe_images, $sort;
730
731 $has_unsafe_images = 0;
7e235a1a 732
451f74a2 733 $id = $message->header->id;
f0c4dc12 734
451f74a2 735 $urlmailbox = urlencode($message->header->mailbox);
c9d78ab4 736// ListMyEntities($message);
451f74a2 737 // Get the right entity and redefine message to be this entity
738 // Pass the 0 to mean that we want the 'best' viewable one
739 $ent_num = findDisplayEntity ($message, 0);
740 $body_message = getEntity($message, $ent_num);
741 if (($body_message->header->type0 == 'text') ||
742 ($body_message->header->type0 == 'rfc822')) {
451f74a2 743 $body = mime_fetch_body ($imap_stream, $id, $ent_num);
744 $body = decodeBody($body, $body_message->header->encoding);
745 $hookResults = do_hook("message_body", $body);
746 $body = $hookResults[1];
451f74a2 747 // If there are other types that shouldn't be formatted, add
748 // them here
749 if ($body_message->header->type1 == 'html') {
750 if ( $show_html_default <> 1 ) {
a3daaaf3 751 $body = strip_tags( $body );
752 translateText($body, $wrap_at, $body_message->header->charset);
753 } else {
754 $body = MagicHTML( $body, $id );
755 }
451f74a2 756 } else {
9eea179c 757 translateText($body, $wrap_at, $body_message->header->charset);
451f74a2 758 }
793cc001 759
e5ea9327 760 $body .= "<CENTER><SMALL><A HREF=\"../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;passed_ent_id=$ent_num&amp;mailbox=$urlmailbox&amp;showHeaders=1\">". _("Download this as a file") ."</A></SMALL></CENTER><BR>";
7e235a1a 761 if ($has_unsafe_images) {
762 if ($view_unsafe_images) {
3d1c7564 763 $body .= "<CENTER><SMALL><A HREF=\"read_body.php?passed_id=$id&amp;mailbox=$urlmailbox&amp;sort=$sort&amp;startMessage=$startMessage&amp;show_more=0\">". _("Hide Unsafe Images") ."</A></SMALL></CENTER><BR>\n";
d03c24f4 764 } else {
3d1c7564 765 $body .= "<CENTER><SMALL><A HREF=\"read_body.php?passed_id=$id&amp;mailbox=$urlmailbox&amp;sort=$sort&amp;startMessage=$startMessage&amp;show_more=0&amp;view_unsafe_images=1\">". _("View Unsafe Images") ."</A></SMALL></CENTER><BR>\n";
d03c24f4 766 }
7e235a1a 767 }
77b88425 768
451f74a2 769 /** Display the ATTACHMENTS: message if there's more than one part **/
451f74a2 770 if (isset($message->entities[0])) {
771 $body .= formatAttachments ($message, $ent_num, $message->header->mailbox, $id);
772 }
451f74a2 773 } else {
774 $body = formatAttachments ($message, -1, $message->header->mailbox, $id);
775 }
776 return ($body);
777}
b74ba498 778
451f74a2 779/*
780 * A recursive function that returns a list of attachments with links
781 * to where to download these attachments
782 */
77b88425 783function formatAttachments($message, $ent_id, $mailbox, $id) {
784 global $where, $what;
785 global $startMessage, $color;
786 static $ShownHTML = 0;
451f74a2 787
77b88425 788 $body = '';
789 if ($ShownHTML == 0) {
451f74a2 790
77b88425 791 $ShownHTML = 1;
451f74a2 792 $body .= "<TABLE WIDTH=\"100%\" CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n" .
77b88425 793 "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n" .
794 _("Attachments") . ':' .
795 "</B></TH></TR><TR><TD>\n" .
796 "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n" .
797 formatAttachments($message, $ent_id, $mailbox, $id) .
798 "</TABLE></TD></TR></TABLE>";
799
800 } else if ($message) {
f0c4dc12 801 $header = $message->header;
802 $type0 = strtolower($header->type0);
803 $type1 = strtolower($header->type1);
2d0476d6 804 $name = '';
805 if (isset($header->name)) {
806 $name = decodeHeader($header->name);
807 }
1fbbe1d4 808 if ($type0 =='message' && $type1 == 'rfc822') {
f0c4dc12 809
810 $filename = decodeHeader($message->header->filename);
811 if (trim($filename) == '') {
812 if (trim($name) == '') {
813 $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
814 } else {
815 $display_filename = $name;
816 $filename = $name;
817 }
818 } else {
819 $display_filename = $filename;
820 }
821
822 $urlMailbox = urlencode($mailbox);
823 $ent = urlencode($message->header->entity_id);
824
825 $DefaultLink =
826 "../src/download.php?startMessage=$startMessage&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
827 if ($where && $what) {
828 $DefaultLink .= '&amp;where=' . urlencode($where) . '&amp;what=' . urlencode($what);
829 }
830 $Links['download link']['text'] = _("download");
831 $Links['download link']['href'] =
832 "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
833 $ImageURL = '';
834
835 /* this executes the attachment hook with a specific MIME-type.
836 * if that doens't have results, it tries if there's a rule
837 * for a more generic type. */
838 $HookResults = do_hook("attachment $type0/$type1", $Links,
839 $startMessage, $id, $urlMailbox, $ent, $DefaultLink, $display_filename, $where, $what);
840 if(count($HookResults[1]) <= 1) {
841 $HookResults = do_hook("attachment $type0/*", $Links,
842 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
843 $display_filename, $where, $what);
844 }
845
846 $Links = $HookResults[1];
847 $DefaultLink = $HookResults[6];
848
849 $body .= '<TR><TD>&nbsp;&nbsp;</TD><TD>' .
850 "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>" .
851 '<TD><SMALL><b>' . show_readable_size($message->header->size) .
852 '</b>&nbsp;&nbsp;</small></TD>' .
853 "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>" .
854 '<TD><SMALL>';
855 if ($message->header->description) {
856 $body .= '<b>' . htmlspecialchars(_($message->header->description)) . '</b>';
857 }
858 $body .= '</SMALL></TD><TD><SMALL>&nbsp;';
859
860
861 $SkipSpaces = 1;
862 foreach ($Links as $Val) {
863 if ($SkipSpaces) {
864 $SkipSpaces = 0;
865 } else {
866 $body .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
867 }
868 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
869 }
870
871 unset($Links);
77b88425 872
f0c4dc12 873 $body .= "</SMALL></TD></TR>\n";
874
875 return( $body );
876
877 } elseif (!$message->entities) {
77b88425 878
879 $type0 = strtolower($message->header->type0);
880 $type1 = strtolower($message->header->type1);
881 $name = decodeHeader($message->header->name);
882
883 if ($message->header->entity_id != $ent_id) {
884 $filename = decodeHeader($message->header->filename);
885 if (trim($filename) == '') {
886 if (trim($name) == '') {
887 if ( trim( $message->header->id ) == '' )
888 $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
889 else
890 $display_filename = 'cid: ' . $message->header->id;
891 // $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
892 } else {
893 $display_filename = $name;
894 $filename = $name;
895 }
896 } else {
897 $display_filename = $filename;
898 }
451f74a2 899
77b88425 900 $urlMailbox = urlencode($mailbox);
901 $ent = urlencode($message->header->entity_id);
451f74a2 902
77b88425 903 $DefaultLink =
e5ea9327 904 "../src/download.php?startMessage=$startMessage&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
77b88425 905 if ($where && $what) {
e5ea9327 906 $DefaultLink .= '&amp;where=' . urlencode($where) . '&amp;what=' . urlencode($what);
77b88425 907 }
908 $Links['download link']['text'] = _("download");
909 $Links['download link']['href'] =
e5ea9327 910 "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
77b88425 911 $ImageURL = '';
912
913 /* this executes the attachment hook with a specific MIME-type.
914 * if that doens't have results, it tries if there's a rule
915 * for a more generic type. */
916 $HookResults = do_hook("attachment $type0/$type1", $Links,
917 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
918 $display_filename, $where, $what);
919 if(count($HookResults[1]) <= 1) {
920 $HookResults = do_hook("attachment $type0/*", $Links,
921 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
922 $display_filename, $where, $what);
923 }
451f74a2 924
77b88425 925 $Links = $HookResults[1];
926 $DefaultLink = $HookResults[6];
927
928 $body .= '<TR><TD>&nbsp;&nbsp;</TD><TD>' .
929 "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>" .
930 '<TD><SMALL><b>' . show_readable_size($message->header->size) .
931 '</b>&nbsp;&nbsp;</small></TD>' .
932 "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>" .
933 '<TD><SMALL>';
934 if ($message->header->description) {
935 $body .= '<b>' . htmlspecialchars(_($message->header->description)) . '</b>';
936 }
937 $body .= '</SMALL></TD><TD><SMALL>&nbsp;';
b74ba498 938
b74ba498 939
77b88425 940 $SkipSpaces = 1;
941 foreach ($Links as $Val) {
942 if ($SkipSpaces) {
943 $SkipSpaces = 0;
944 } else {
945 $body .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
946 }
947 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
948 }
949
950 unset($Links);
951
952 $body .= "</SMALL></TD></TR>\n";
953 }
954 } else {
955 for ($i = 0; $i < count($message->entities); $i++) {
956 $body .= formatAttachments($message->entities[$i], $ent_id, $mailbox, $id);
957 }
451f74a2 958 }
77b88425 959 }
960 return( $body );
451f74a2 961}
b74ba498 962
b74ba498 963
451f74a2 964/** this function decodes the body depending on the encoding type. **/
965function decodeBody($body, $encoding) {
966 $body = str_replace("\r\n", "\n", $body);
967 $encoding = strtolower($encoding);
b74ba498 968
451f74a2 969 global $show_html_default;
4809f489 970
451f74a2 971 if ($encoding == 'quoted-printable') {
972 $body = quoted_printable_decode($body);
4809f489 973
7831268e 974
451f74a2 975 while (ereg("=\n", $body))
976 $body = ereg_replace ("=\n", "", $body);
358f007e 977
451f74a2 978 } else if ($encoding == 'base64') {
979 $body = base64_decode($body);
980 }
b74ba498 981
451f74a2 982 // All other encodings are returned raw.
983 return $body;
984}
985
986/*
987 * This functions decode strings that is encoded according to
988 * RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
79e07c7e 989 * Patched by Christian Schmidt <christian@ostenfeld.dk> 23/03/2002
451f74a2 990 */
991function decodeHeader ($string, $utfencode=true) {
79e07c7e 992 if (is_array($string)) {
993 $string = implode("\n", $string);
994 }
995 $i = 0;
996 while (preg_match('/^(.{' . $i . '})(.*)=\?([^?]*)\?(Q|B)\?([^?]*)\?=/Ui',
997 $string, $res)) {
998 $prefix = $res[1];
999 // Ignore white-space between consecutive encoded-words
1000 if (strspn($res[2], " \t") != strlen($res[2])) {
1001 $prefix .= $res[2];
1002 }
18aa168e 1003
79e07c7e 1004 if (ucfirst($res[4]) == 'B') {
1005 $replace = base64_decode($res[5]);
1006 } else {
1007 $replace = str_replace('_', ' ', $res[5]);
1008 $replace = preg_replace('/=([0-9a-f]{2})/ie', 'chr(hexdec("\1"))',
1009 $replace);
1010 /* Only encode into entities by default. Some places
1011 don't need the encoding, like the compose form. */
1012 if ($utfencode) {
1013 $replace = charset_decode($res[3], $replace);
1014 }
18aa168e 1015 }
79e07c7e 1016 $string = $prefix . $replace . substr($string, strlen($res[0]));
1017 $i = strlen($prefix) + strlen($replace);
18aa168e 1018 }
79e07c7e 1019 return( $string );
451f74a2 1020}
1021
1022/*
1023 * Encode a string according to RFC 1522 for use in headers if it
1024 * contains 8-bit characters or anything that looks like it should
1025 * be encoded.
1026 */
1027function encodeHeader ($string) {
1028 global $default_charset;
793cc001 1029
451f74a2 1030 // Encode only if the string contains 8-bit characters or =?
1031 $j = strlen( $string );
1032 $l = strstr($string, '=?'); // Must be encoded ?
1033 $ret = '';
1034 for( $i=0; $i < $j; ++$i) {
f7b3ba37 1035 switch( $string{$i} ) {
1036 case '=':
b74ba498 1037 $ret .= '=3D';
1038 break;
451f74a2 1039 case '?':
b74ba498 1040 $ret .= '=3F';
1041 break;
451f74a2 1042 case '_':
b74ba498 1043 $ret .= '=5F';
1044 break;
451f74a2 1045 case ' ':
b74ba498 1046 $ret .= '_';
1047 break;
451f74a2 1048 default:
b74ba498 1049 $k = ord( $string{$i} );
451f74a2 1050 if ( $k > 126 ) {
b74ba498 1051 $ret .= sprintf("=%02X", $k);
1052 $l = TRUE;
1053 } else
1054 $ret .= $string{$i};
f7b3ba37 1055 }
451f74a2 1056 }
793cc001 1057
451f74a2 1058 if ( $l ) {
f7b3ba37 1059 $string = "=?$default_charset?Q?$ret?=";
451f74a2 1060 }
346817d4 1061
451f74a2 1062 return( $string );
1063}
b74ba498 1064
451f74a2 1065/*
a3daaaf3 1066 Strips dangerous tags from html messages.
451f74a2 1067*/
1068function MagicHTML( $body, $id ) {
1069
793cc001 1070 global $message, $HTTP_SERVER_VARS,
5106a9be 1071 $attachment_common_show_images;
451f74a2 1072
5106a9be 1073 $attachment_common_show_images =
1074 FALSE; // Don't display attached images in HTML mode
451f74a2 1075 $j = strlen( $body ); // Legnth of the HTML
1076 $ret = ''; // Returned string
1077 $bgcolor = '#ffffff'; // Background style color (defaults to white)
1078 $textcolor = '#000000'; // Foreground style color (defaults to black)
1079 $leftmargin = ''; // Left margin style
1080 $title = ''; // HTML title if any
1081
1082 $i = 0;
1083 while ( $i < $j ) {
1084 if ( $body{$i} == '<' ) {
1085 $pos = $i + 1;
1086 $tag = '';
1087 while ($body{$pos} == ' ' || $body{$pos} == "\t" ||
88104625 1088 $body{$pos} == "\n") {
451f74a2 1089 $pos ++;
1090 }
1091 while (strlen($tag) < 4 && $body{$pos} != ' ' &&
793cc001 1092 $body{$pos} != "\t" && $body{$pos} != "\n" &&
1093 $pos < $j ) {
88104625 1094 if ($body{$pos} == "<"){
1095 $tag = '';
1096 $pos++;
1097 }
451f74a2 1098 $tag .= $body{$pos};
1099 $pos ++;
1100 }
cedad33a 1101 /*
1102 A comment in HTML is only three characters and isn't
1103 guaranteed to have a space after it. This fudges so
1104 it will be caught by the switch statement.
1105 */
1106 if (ereg("!--", $tag)) {
1107 $tag = "!-- ";
1108 }
451f74a2 1109 switch( strtoupper( $tag ) ) {
1110 // Strips the entire tag and contents
1111 case 'APPL':
cedad33a 1112 case 'EMBE':
451f74a2 1113 case 'FRAM':
1114 case 'SCRI':
1115 case 'OBJE':
1116 $etg = '/' . $tag;
1117 while ( $body{$i+1}.$body{$i+2}.$body{$i+3}.$body{$i+4}.$body{$i+5} <> $etg &&
1118 $i < $j ) $i++;
1119 while ( $i < $j && $body{++$i} <> '>' );
1120 // $ret .= "<!-- $tag removed -->";
1121 break;
1122 // Substitute Title
1123 case 'TITL':
1124 $i += 5;
1125 while ( $body{$i} <> '>' && // </title>
1126 $i < $j )
a3daaaf3 1127 $i++;
451f74a2 1128 $i++;
1129 $title = '';
1130 while ( $body{$i} <> '<' && // </title>
1131 $i < $j ) {
1132 $title .= $body{$i};
1133 $i++;
1134 }
1135 $i += 7;
1136 break;
1137 // Destroy these tags
1138 case 'HTML':
1139 case 'HEAD':
1140 case '/HTM':
1141 case '/HEA':
1142 case '!DOC':
1143 case 'META':
89863d1d 1144 //case 'DIV ':
1145 //case '/DIV':
451f74a2 1146 case '!-- ':
1147 $i += 4;
1148 while ( $body{$i} <> '>' &&
1149 $i < $j )
1150 $i++;
1151 // $i++;
1152 break;
1153 case 'STYL':
1154 $i += 5;
1155 while ( $body{$i} <> '>' && // </title>
1156 $i < $j )
1157 $i++;
1158 $i++;
1159 // We parse the style to look for interesting stuff
1160 $styleblk = '';
1161 while ( $body{$i} <> '>' &&
1162 $i < $j ) {
1163 // First we get the name of the style
1164 $style = '';
1165 while ( $body{$i} <> '>' &&
1166 $body{$i} <> '<' &&
1167 $body{$i} <> '{' &&
1168 $i < $j ) {
1169 if ( isnoSep( $body{$i} ) )
1170 $style .= $body{$i};
1171 $i++;
1172 }
1173 stripComments( $i, $j, $body );
1174 $style = strtoupper( trim( $style ) );
1175 if ( $style == 'BODY' ) {
1176 // Next we look into the definitions of the body style
1177 while ( $body{$i} <> '>' &&
1178 $body{$i} <> '}' &&
a3daaaf3 1179 $i < $j ) {
451f74a2 1180 // We look for the background color if any.
1181 if ( substr( $body, $i, 17 ) == 'BACKGROUND-COLOR:' ) {
1182 $i += 17;
1183 $bgcolor = getStyleData( $i, $j, $body );
1184 } elseif ( substr( $body, $i, 12 ) == 'MARGIN-LEFT:' ) {
1185 $i += 12;
1186 $leftmargin = getStyleData( $i, $j, $body );
1187 }
a3daaaf3 1188 $i++;
1189 }
451f74a2 1190 } else {
1191 // Other style are mantained
1192 $styleblk .= "$style ";
1193 while ( $body{$i} <> '>' &&
1194 $body{$i} <> '<' &&
1195 $body{$i} <> '}' &&
1196 $i < $j ) {
1197 $styleblk .= $body{$i};
a3daaaf3 1198 $i++;
451f74a2 1199 }
1200 $styleblk .= $body{$i};
1201 }
1202 stripComments( $i, $j, $body );
1203 if ( $body{$i} <> '>' )
1204 $i++;
1205 }
1206 if ( $styleblk <> '' )
1207 $ret .= "<style>$styleblk";
1208 break;
1209 case 'BODY':
1210 if ( $title <> '' )
1211 $ret .= '<b>' . _("Title:") . " </b>$title<br>\n";
1212 $ret .= "<TABLE";
1213 $i += 5;
1214 if (! isset($base)) {
1215 $base = '';
1216 }
1217 $ret .= stripEvent( $i, $j, $body, $id, $base );
1218 $ret .= " bgcolor=$bgcolor width=\"100%\"><tr>";
1219 if ( $leftmargin <> '' )
1220 $ret .= "<td width=$leftmargin>&nbsp;</td>";
1221 $ret .= '<td>';
1222 if (strtolower($bgcolor) == 'ffffff' ||
1223 strtolower($bgcolor) == '#ffffff')
1224 $ret .= '<font color=#000000>';
1225 break;
1226 case 'BASE':
cedad33a 1227 $i += 4;
451f74a2 1228 $base = '';
cedad33a 1229 if ( strncasecmp($body{$i}, 'font', 4) ) {
1230 $i += 5;
1231 while ( !isNoSep( $body{$i} ) && $i < $j ) {
1232 $i++;
1233 }
1234 while ( $body{$i} <> '>' && $i < $j ) {
1235 $base .= $body{$i};
1236 $i++;
1237 }
1238 $ret .= "<BASEFONT $base>\n";
1239 break;
1240 }
1241 $i++;
451f74a2 1242 while ( !isNoSep( $body{$i} ) &&
1243 $i < $j ) {
1244 $i++;
1245 }
1246 if ( strcasecmp( substr( $base, 0, 4 ), 'href' ) ) {
a3daaaf3 1247 $i += 5;
451f74a2 1248 while ( !isNoSep( $body{$i} ) &&
1249 $i < $j ) {
a3daaaf3 1250 $i++;
451f74a2 1251 }
1252 while ( $body{$i} <> '>' &&
a3daaaf3 1253 $i < $j ) {
451f74a2 1254 if ( $body{$i} <> '"' ) {
1255 $base .= $body{$i};
a3daaaf3 1256 }
451f74a2 1257 $i++;
a3daaaf3 1258 }
451f74a2 1259 // Debuging $ret .= "<!-- base == $base -->";
1260 if ( strcasecmp( substr( $base, 0, 4 ), 'file' ) <> 0 ) {
1261 $ret .= "\n<BASE HREF=\"$base\">\n";
a3daaaf3 1262 }
a3daaaf3 1263 }
451f74a2 1264 break;
1265 case '/BOD':
1266 $ret .= '</font></td></tr></TABLE>';
1267 $i += 6;
a3daaaf3 1268 break;
1269 default:
451f74a2 1270 // Following tags can contain some event handler, lets search it
1271 stripComments( $i, $j, $body );
1272 if (! isset($base)) {
1273 $base = '';
1274 }
1275 $ret .= stripEvent( $i, $j, $body, $id, $base ) . '>';
1276 // $ret .= "<!-- $tag detected -->";
1277 }
1278 } else {
1279 $ret .= $body{$i};
a3daaaf3 1280 }
451f74a2 1281 $i++;
1282 }
a3daaaf3 1283
451f74a2 1284return( "\n\n<!-- HTML Output ahead -->\n" .
1285 $ret .
7e235a1a 1286 /* Base is illegal within HTML
451f74a2 1287 "\n<!-- END of HTML Output --><base href=\"".
494424e3 1288 get_location() . '/'.
451f74a2 1289 "\">\n\n" );
7e235a1a 1290 */
e5ea9327 1291 "\n<!-- END of HTML Output -->\n\n" );
451f74a2 1292}
a3daaaf3 1293
451f74a2 1294function isNoSep( $char ) {
1295
1296 switch( $char ) {
1297 case ' ':
1298 case "\n":
1299 case "\t":
1300 case "\r":
1301 case '>':
1302 case '"':
1303 return( FALSE );
1304 break;
1305 default:
1306 return( TRUE );
1307 }
a3daaaf3 1308
451f74a2 1309}
a3daaaf3 1310
451f74a2 1311/*
1312 The following function is usefull to remove extra data that can cause
1313 html not to display properly. Especialy with MS stuff.
1314*/
1315
1316function stripComments( &$i, $j, &$body ) {
1317
ae35570b 1318 while ( $body{$i}.$body{$i+1}.$body{$i+2}.$body{$i+3} == '<!--' &&
451f74a2 1319 $i < $j ) {
1320 $i += 5;
1321 while ( $body{$i-2}.$body{$i-1}.$body{$i} <> '-->' &&
1322 $i < $j )
a3daaaf3 1323 $i++;
451f74a2 1324 $i++;
1325 }
a3daaaf3 1326
451f74a2 1327 return;
a3daaaf3 1328
451f74a2 1329}
a3daaaf3 1330
451f74a2 1331/* Gets the style data of a specific style */
a3daaaf3 1332
451f74a2 1333function getStyleData( &$i, $j, &$body ) {
a3daaaf3 1334
451f74a2 1335 // We skip spaces
1336 while ( $body{$i} <> '>' && !isNoSep( $body{$i} ) &&
1337 $i < $j ) {
1338 $i++;
1339 }
1340 // And get the color
1341 $ret = '';
1342 while ( isNoSep( $body{$i} ) &&
1343 $i < $j ) {
1344 $ret .= $body{$i};
1345 $i++;
1346 }
a3daaaf3 1347
451f74a2 1348 return( $ret );
1349}
a3daaaf3 1350
451f74a2 1351/*
1352Private function for strip_dangerous_tag. Look for event based coded and "remove" it
1353change on with no (onload -> noload)
1354*/
a3daaaf3 1355
451f74a2 1356function stripEvent( &$i, $j, &$body, $id, $base ) {
a3daaaf3 1357
d03c24f4 1358 global $message, $base_uri, $has_unsafe_images, $view_unsafe_images;
a3daaaf3 1359
451f74a2 1360 $ret = '';
a3daaaf3 1361
451f74a2 1362 while ( $body{$i} <> '>' &&
1363 $i < $j ) {
cca46357 1364 /**
1365 * [ 545933 ] Cross-site scripting vulnerability
1366 * <hr>
1367 * <img x="<foo>" src=javascript:alert(1) y="</foo>">
1368 * <hr>
1369 *
1370 * This code will ignore anything within the quotes
1371 * so they don't mess us up.
1372 */
1373 if ( $body{$i} == '"' || $body{$i} == "'" ){
1374 $quotechar = $body{$i};
1375 do {
1376 $ret .= $body{$i};
1377 $i++;
1378 } while ($body{$i} != $quotechar && $i < $j);
1379 }
451f74a2 1380 $etg = strtolower($body{$i}.$body{$i+1}.$body{$i+2});
1381 switch( $etg ) {
1382 case 'src':
1383 // This is probably a src specification
1384 $k = $i + 3;
1385 while( !isNoSep( $body{$k} )) {
1386 $k++;
1387 }
1388 if ( $body{$k} == '=' ) {
1389 /* It is indeed */
1390 $k++;
1391 while( !isNoSep( $body{$k} ) &&
1392 $k < $j ) {
1393 $k++;
1394 }
1395 $src = '';
1396 while ( $body{$k} <> '>' && isNoSep( $body{$k} ) &&
1397 $k < $j ) {
1398 $src .= $body{$k};
1399 $k++;
1400 }
f9cabaf8 1401 $k++;
451f74a2 1402 while( !isNoSep( $body{$k} ) &&
1403 $k < $j ) {
1404 $k++;
1405 }
f9cabaf8 1406 $k++;
451f74a2 1407 if ( strtolower( substr( $src, 0, 4 ) ) == 'cid:' ) {
1408 $src = substr( $src, 4 );
e5ea9327 1409 $src = "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=" .
451f74a2 1410 urlencode( $message->header->mailbox ) .
7e235a1a 1411 "&amp;passed_ent_id=" . find_ent_id( $src, $message );
1412 } else if ( strtolower( substr( $src, 0, 4 ) ) <> 'http' ||
451f74a2 1413 stristr( $src, $base_uri ) ) {
1414 /* Javascript and local urls goes out */
7e235a1a 1415 if (!$view_unsafe_images) {
d03c24f4 1416 $src = '../images/' . _("sec_remove_eng.png");
7e235a1a 1417 }
1418 $has_unsafe_images = 1;
451f74a2 1419 }
1420 $ret .= 'src="' . $src . '" ';
89863d1d 1421 $i = $k - 2;
451f74a2 1422 } else {
1423 $ret .= 'src';
1424 $i = $i + 3;
1425 }
1426
1427 break;
1428 case '../':
1429 // Retrolinks are not allowed without a base because they mess with SM security
1430 if ( $base == '' ) {
a3daaaf3 1431 $i += 2;
451f74a2 1432 } else {
1433 $ret .= '.';
a3daaaf3 1434 }
451f74a2 1435 break;
1436 case 'cid':
1437 // Internal link
1438 $k = $i-1;
1439 if ( $body{$i+3} == ':') {
1440 $i +=4;
1441 $name = '';
1442 while ( isNoSep( $body{$i} ) &&
1443 $i < $j ) {
1444 $name .= $body{$i++};
1445 }
1446 if ( $name <> '' ) {
e5ea9327 1447 $ret .= "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=" .
451f74a2 1448 urlencode( $message->header->mailbox ) .
e5ea9327 1449 "&amp;passed_ent_id=" . find_ent_id( $name, $message );
451f74a2 1450 if ( $body{$k} == '"' )
1451 $ret .= '" ';
1452 else
1453 $ret .= ' ';
1454 }
1455 if ( $body{$i} == '>' )
1456 $i -= 1;
1457 }
1458 break;
1459 case ' on':
1460 case "\non":
1461 case "\ron":
1462 case "\ton":
1463 $ret .= ' no';
1464 $i += 2;
1465 break;
1466 case 'pt:':
1467 if ( strcasecmp( $body{$i-4}.$body{$i-3}.$body{$i-2}.$body{$i-1}.$body{$i}.$body{$i+1}.$body{$i+2}, 'script:') == 0 ) {
1468 $ret .= '_no/';
1469 } else {
1470 $ret .= $etg;
1471 }
1472 $i += 2;
1473 break;
1474 default:
1475 $ret .= $body{$i};
a3daaaf3 1476 }
451f74a2 1477 $i++;
a3daaaf3 1478 }
451f74a2 1479 return( $ret );
1480}
a3daaaf3 1481
1482
451f74a2 1483/* This function trys to locate the entity_id of a specific mime element */
a3daaaf3 1484
451f74a2 1485function find_ent_id( $id, $message ) {
451f74a2 1486 $ret = '';
1487 for ($i=0; $ret == '' && $i < count($message->entities); $i++) {
c9d78ab4 1488 if ( $message->entities[$i]->header->entity_id == '' || $message->entities[$i]->header->type ) {
451f74a2 1489 $ret = find_ent_id( $id, $message->entities[$i] );
1490 } else {
1491 if ( strcasecmp( $message->entities[$i]->header->id, $id ) == 0 )
1492 $ret = $message->entities[$i]->header->entity_id;
a3daaaf3 1493 }
1494
a3daaaf3 1495 }
451f74a2 1496
1497 return( $ret );
1498
1499}
1c72b151 1500?>