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