a19542bc72a40835237651b75dd31c997dedc7d6
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This file is used for reading the msgs array and displaying
10 * the resulting emails in the right frame.
15 require_once('../src/validate.php');
16 require_once('../functions/imap.php');
17 require_once('../functions/mime.php');
18 require_once('../functions/date.php');
19 require_once('../functions/url_parser.php');
20 require_once('../functions/smtp.php');
21 require_once('../functions/html.php');
24 * Given an IMAP message id number, this will look it up in the cached
25 * and sorted msgs array and return the index. Used for finding the next
26 * and previous messages.
28 * @return the index of the next valid message from the array
30 function findNextMessage($passed_id) {
31 global $msort, $msgs, $sort,
32 $thread_sort_messages, $allow_server_sort,
34 if (!is_array($server_sort_array)) {
35 $thread_sort_messages = 0;
36 $allow_server_sort = FALSE;
39 if ($thread_sort_messages ||
$allow_server_sort) {
40 reset($server_sort_array);
41 while(list($key, $value) = each ($server_sort_array)) {
42 if ($passed_id == $value) {
43 if ($key == (count($server_sort_array) - 1)) {
47 $result = $server_sort_array[$key +
1];
51 } else if ($sort == 6 && !$allow_server_sort &&
52 !$thread_sort_messages ) {
53 if ($passed_id != 1) {
54 $result = $passed_id - 1;
56 } else if (!$allow_server_sort && !$thread_sort_messages ) {
57 if (!is_array($msort)) {
60 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
61 if ($passed_id == $msgs[$key]['ID']) {
65 $result = $msgs[$key]['ID'];
74 /** returns the index of the previous message from the array. */
75 function findPreviousMessage($numMessages, $passed_id) {
76 global $msort, $sort, $msgs,
77 $thread_sort_messages,
78 $allow_server_sort, $server_sort_array;
80 if (!is_array($server_sort_array)) {
81 $thread_sort_messages = 0;
82 $allow_server_sort = FALSE;
84 if ($thread_sort_messages ||
$allow_server_sort ) {
85 reset($server_sort_array);
86 while(list($key, $value) = each ($server_sort_array)) {
87 if ($passed_id == $value) {
92 $result = $server_sort_array[$key -1];
96 } else if ($sort == 6 && !$allow_server_sort &&
97 !$thread_sort_messages) {
98 if ($passed_id != $numMessages) {
99 $result = $passed_id +
1;
101 } else if (!$thread_sort_messages && !$allow_server_sort) {
102 if (!is_array($msort)) {
105 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
106 if ($passed_id == $msgs[$key]['ID']) {
110 //echo $msort[$key]; /* Why again were we echoing here? */
111 $result = $msgs[$key]['ID'];
121 * Displays a link to a page where the message is displayed more
122 * "printer friendly".
124 function printer_friendly_link($mailbox, $passed_id, $passed_ent_id, $color) {
125 global $javascript_on;
127 $params = '?passed_ent_id=' . $passed_ent_id .
128 '&mailbox=' . urlencode($mailbox) .
129 '&passed_id=' . $passed_id;
131 $print_text = _("View Printable Version");
134 /* Output the link. */
135 if ($javascript_on) {
136 $result .= '<script language="javascript" type="text/javascript">' . "\n" .
138 " function printFormat() {\n" .
139 ' window.open("../src/printer_friendly_main.php' .
140 $params . '","Print","width=800,height=600");' . "\n".
144 "<a href=\"javascript:printFormat();\">$print_text</a>\n";
146 $result .= '<A target="_blank" HREF="../src/printer_friendly_bottom.php' .
147 "$params\">$print_text</a>\n";
152 function ServerMDNSupport( $read ) {
153 /* escaping $ doesn't work -> \x36 */
154 $ret = preg_match( '/(\x36MDNSent|\\\*)/i', $read );
158 function SendMDN ( $mailbox, $passed_id, $sender, $message) {
159 global $username, $attachment_dir, $SERVER_NAME,
160 $version, $attachments, $squirrelmail_language;
162 $header = $message->rfc822_header
;
163 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
165 $recipient_o = $header->dnt
;
166 $recipient = $recipient_o->getAddress(true);
170 $senton = getLongDateString( $header->date
);
171 $to_array = $header->to
;
173 foreach ($to_array as $line) {
174 $to .= ' '.$line->getAddress();
177 $subject = $header->subject
;
178 $now = getLongDateString( time() );
182 $body = _("Your message") . "\r\n\r\n" .
183 "\t" . _("To:") . ' ' . $to . "\r\n" .
184 "\t" . _("Subject:") . ' ' . $subject . "\r\n" .
185 "\t" . _("Sent:") . ' ' . $senton . "\r\n" .
187 sprintf( _("Was displayed on %s"), $now );
189 if (function_exists($languages[$squrrelmail_language]['XTRA_CODE'])) {
190 $body = $languages[$squirrelmail_language]['XTRA_CODE']('encode', $body);
194 $original_recipient = $to;
195 $original_message_id = $header->message_id
;
197 $part2 = "Reporting-UA : $SERVER_NAME ; SquirrelMail (version $version) \r\n";
198 if ($original_recipient != '') {
199 $part2 .= "Original-Recipient : $original_recipient\r\n";
201 $final_recipient = $sender;
202 $part2 .= "Final-Recipient: rfc822; $final_recipient\r\n" .
203 "Original-Message-ID : $original_message_id\r\n" .
204 "Disposition: manual-action/MDN-sent-manually; displayed\r\n";
206 $localfilename = GenerateRandomString(32, 'FILE', 7);
207 $full_localfilename = "$hashed_attachment_dir/$localfilename";
209 $fp = fopen( $full_localfilename, 'w');
210 fwrite ($fp, $part2);
213 $newAttachment = array();
214 $newAttachment['localfilename'] = $localfilename;
215 $newAttachment['type'] = "message/disposition-notification";
216 $newAttachment['session']=-1;
217 $attachments[] = $newAttachment;
219 return (SendMessage($recipient, '', '', _("Read:") . ' ' . $subject,
220 $body, 0, True, 3, -1) );
224 function ToggleMDNflag ( $set ,$imapConnection, $mailbox, $passed_id, $uid_support) {
226 $cmd = 'STORE ' . $passed_id . ' ' . $sg . 'FLAGS ($MDNSent)';
227 $read = sqimap_run_command ($imapConnection, $cmd, true, $response,
228 $readmessage, $uid_support);
231 function ClearAttachments() {
232 global $username, $attachments, $attachment_dir;
234 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
236 $rem_attachments = array();
237 foreach ($attachments as $info) {
238 if ($info['session'] == -1) {
239 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
240 if (file_exists($attached_file)) {
241 unlink($attached_file);
244 $rem_attachments[] = $info;
247 $attachments = $rem_attachments;
250 function formatRecipientString($recipients, $item ) {
251 global $show_more_cc, $show_more, $show_more_bcc,
254 if ((is_array($recipients)) && (isset($recipients[0]))) {
262 $url = set_url_var($PHP_SELF, 'show_more',0);
264 $url = set_url_var($PHP_SELF, 'show_more',1);
266 } else if ($item == 'cc') {
268 $url = set_url_var($PHP_SELF, 'show_more_cc',0);
271 $url = set_url_var($PHP_SELF, 'show_more_cc',1);
273 } else if ($item == 'bcc') {
274 if ($show_more_bcc) {
275 $url = set_url_var($PHP_SELF, 'show_more_bcc',0);
278 $url = set_url_var($PHP_SELF, 'show_more_bcc',1);
285 $ary[$i] = htmlspecialchars($ary[$i]->getAddress());
287 $string .= '<BR>'.$ary[$i];
291 $string .= ' (<A HREF="'.$url;
293 $string .= '">'._("less").'</A>)';
295 $string .= '">'._("more").'</A>)';
309 function formatEnvheader($mailbox, $passed_id, $passed_ent_id, $message,
310 $color, $FirstTimeSee) {
311 global $msn_user_support, $default_use_mdn, $draft_folder, $sent_folder,
312 $default_use_priority, $show_xmailer_default,
313 $mdn_user_support, $PHP_SELF, $javascript_on;
315 $header = $message->rfc822_header
;
317 $env[_("Subject")] = htmlspecialchars(decodeHeader($header->subject
));
318 $from_name = $header->getAddr_s('from');
320 $from_name = $header->getAddr_s('sender');
322 $from_name = _("Unknown sender");
325 $env[_("From")] = htmlspecialchars(decodeHeader($from_name));
326 $env[_("Date")] = getLongDateString($header->date
);
327 $env[_("To")] = formatRecipientString($header->to
, "to");
328 $env[_("Cc")] = formatRecipientString($header->cc
, "cc");
329 $env[_("Bcc")] = formatRecipientString($header->bcc
, "bcc");
330 if ($default_use_priority) {
331 $env[_("Priority")] = getPriorityStr($header->priority
);
333 if ($show_xmailer_default) {
334 $env[_("Mailer")] = decodeHeader($header->xmailer
);
336 if ($default_use_mdn) {
337 if ($mdn_user_support) {
339 if ($message->is_mdnsent
) {
340 $env[_("Read receipt")] = _("send");
342 if ( !($mailbox == $draft_folder ||
343 $mailbox == $sent_folder ||
344 $message->is_deleted ||
346 $mdn_url = $PHP_SELF . '&sendreceipt=1';
347 if ($FirstTimeSee && $javascript_on) {
348 $script = '<script language="JavaScript" type="text/javascript">' ."\n";
349 $script .= '<!--'. "\n";
350 $script .= 'if(window.confirm("' .
351 _("The message sender has requested a response to indicate that you have read this message. Would you like to send a receipt?") .
355 $script .= '// -->'. "\n";
356 $script .= '</script>'. "\n";
359 $env[_("Read receipt")] = _("requested") .
360 ' <a href="'.$mdn_url.'">['. _("Send read receipt now") .']</a>';
362 $env[_("Read receipt")] = _("requested");
369 $s = '<TABLE WIDTH="100%" CELLPADDING="0" CELLSPACING="0" BORDER="0"';
370 $s .= ' ALIGN="center" BGCOLOR="' . $color[0] . '">';
371 foreach ($env as $key => $val) {
374 $s .= html_tag('TD', '<B>' . $key . ': </B>', 'RIGHT', '', 'VALIGN="TOP" WIDTH="20%"') . "\n";
375 $s .= html_tag('TD', $val, 'left', '', 'VALIGN="TOP" WIDTH="80%"') . "\n";
380 do_hook("read_body_header");
381 formatToolbar($mailbox,$passed_id,$passed_ent_id,$message, $color);
385 function formatMenubar($mailbox, $passed_id, $passed_ent_id, $message, $mbx_response) {
386 global $base_uri, $sent_folder, $draft_folder, $where, $what, $color, $sort,
387 $startMessage, $compose_new_win, $PHP_SELF, $save_as_draft,
388 $enable_forward_as_attachment;
390 $topbar_delimiter = ' | ';
391 $urlMailbox = urlencode($mailbox);
392 $s = '<table width="100%" cellpadding="3" cellspacing="0" align="center"'.
393 ' border="0" bgcolor="'.$color[9].'"><tr><td align="left" width="33%"><small>';
395 $msgs_url = $base_uri . 'src/';
396 if (isset($where) && isset($what)) {
397 $msgs_url .= 'search.php?where='.urlencode($where).
398 '&what='.urlencode($what).'&mailbox='.$urlMailbox;
399 $msgs_str = _("Search results");
401 $msgs_url .= 'right_main.php?sort='.$sort.'&startMessage='.
402 $startMessage.'&mailbox='.$urlMailbox;
403 $msgs_str = _("Message List");
405 $s .= '<a href="'. $msgs_url.'">'.$msgs_str.'</a>';
406 $s .= $topbar_delimiter;
408 $delete_url = $base_uri . 'src/delete_message.php?mailbox='.$urlMailbox.
409 '&message='.$passed_id.'&';
410 if (!(isset($passed_ent_id) && $passed_ent_id)) {
411 if ($where && $what) {
412 $delete_url .= 'where=' . urlencode($where) . '&what=' . urlencode($what);
414 $delete_url .= 'sort='. $sort . '&startMessage='. $startMessage;
416 $s .= '<a href="'. $delete_url.'">'._("Delete").'</a>';
419 $comp_uri = $base_uri . 'src/compose.php'.
420 '?passed_id='.$passed_id.
421 '&mailbox='.$urlMailbox.
422 (isset($passed_ent_id)?
'&passed_ent_id='.$passed_ent_id:'');
424 if (($mailbox == $draft_folder) && ($save_as_draft)) {
425 $comp_alt_uri = $comp_uri . '&action=draft';
426 $comp_alt_string = _("Resume Draft");
427 } else if ($mailbox == $sent_folder) {
428 $comp_alt_uri = $comp_uri . '&action=edit_as_new';
429 $comp_alt_string = _("Edit Message as New");
431 if (isset($comp_alt_uri)) {
432 $s .= $topbar_delimiter;
433 if ($compose_new_win == '1') {
434 $s .= '<a href="javascript:void(0)" '.
435 'onclick="comp_in_new(\''.$comp_alt_uri.'\')">'.$comp_alt_string.'</a>';
437 $s .= '<a href="'.$comp_alt_uri.'">'.$comp_alt_string.'</a>';
441 $s .= '</small></td><td align="center" width="33%"><small>';
443 if (!(isset($where) && isset($what)) && !$passed_ent_id) {
444 $prev = findPreviousMessage($mbx_response['EXISTS'], $passed_id);
445 $next = findNextMessage($passed_id);
447 $uri = $base_uri . 'src/read_body.php?passed_id='.$prev.
448 '&mailbox='.$urlMailbox.'&sort='.$sort.
449 '&startMessage='.$startMessage.'&show_more=0';
450 $s .= '<a href="'.$uri.'">'._("Previous").'</a>';
454 $s .= $topbar_delimiter;
456 $uri = $base_uri . 'src/read_body.php?passed_id='.$next.
457 '&mailbox='.$urlMailbox.'&sort='.$sort.
458 '&startMessage='.$startMessage.'&show_more=0';
459 $s .= '<a href="'.$uri.'">'._("Next").'</a>';
463 } else if (isset($passed_ent_id) && $passed_ent_id) {
464 /* code for navigating through attached message/rfc822 messages */
465 $url = set_url_var($PHP_SELF, 'passed_ent_id',0);
466 $s .= '<a href="'.$url.'">'._("View Message").'</a>';
467 $par_ent_id = $message->parent
->entity_id
;
469 $par_ent_id = substr($par_ent_id,0,-2);
470 $s .= $topbar_delimiter;
471 $url = set_url_var($PHP_SELF, 'passed_ent_id',$par_ent_id);
472 $s .= '<a href="'.$url.'">'._("Up").'</a>';
476 $s .= '</small></td><td align="right" width="33%" nowrap><small>';
477 $comp_action_uri = $comp_uri . '&action=forward';
478 if ($compose_new_win == '1') {
479 $s .= '<a href="javascript:void(0)" '.
480 'onclick="comp_in_new(\''.$comp_action_uri.'\')">'._("Forward").'</a>';
482 $s .= '<a href="'.$comp_action_uri.'">'._("Forward").'</a>';
484 $s .= $topbar_delimiter;
486 if ($enable_forward_as_attachment) {
487 $comp_action_uri = $comp_uri . '&action=forward_as_attachment';
488 if ($compose_new_win == '1') {
489 $s .= '<a href="javascript:void(0)" '.
490 'onclick="comp_in_new(\''.$comp_action_uri.'\')">'._("Forward as Attachment").'</a>';
492 $s .= '<a href="'.$comp_action_uri.'">'._("Forward as Attachment").'</a>';
494 $s .= $topbar_delimiter;
497 $comp_action_uri = decodeHeader($comp_uri . '&action=reply');
498 if ($compose_new_win == '1') {
499 $s .= '<a href="javascript:void(0)" '.
500 'onclick="comp_in_new(\''.$comp_action_uri.'\')">'._("Reply").'</a>';
502 $s .= '<a href="'.$comp_action_uri.'">'._("Reply").'</a>';
504 $s .= $topbar_delimiter;
506 $comp_action_uri = $comp_uri . '&action=reply_all';
507 if ($compose_new_win == '1') {
508 $s .= '<a href="javascript:void(0)" '.
509 'onclick="comp_in_new(\''.$comp_action_uri.'\')">'._("Reply All").'</a>';
511 $s .= '<a href="'.$comp_action_uri.'">'._("Reply All").'</a>';
513 $s .= '</small></td></tr></table>';
514 do_hook("read_body_menu_top");
516 do_hook("read_body_menu_bottom");
519 function formatToolbar($mailbox, $passed_id, $passed_ent_id, $message, $color) {
520 global $QUERY_STRING, $base_uri;
522 $urlMailbox = urlencode($mailbox);
523 $url = $base_uri.'src/view_header.php?'.$QUERY_STRING;
526 '<TD VALIGN="MIDDLE" ALIGN="RIGHT" WIDTH="20%"><B>' . _("Options") . ": </B></TD>\n" .
527 '<TD VALIGN="MIDDLE" ALIGN="LEFT" WIDTH="80%"><SMALL>' .
528 '<a href="'.$url.'">'._("View Full Header").'</a>';
530 /* Output the printer friendly link if we are in subtle mode. */
531 $s .= ' | ' .
532 printer_friendly_link($mailbox, $passed_id, $passed_ent_id, $color);
534 do_hook("read_body_header_right");
535 $s = "</SMALL></TD>\n" .
541 /***************************/
542 /* Main of read_body.php */
543 /***************************/
551 global $uid_support, $sqimap_capabilities;
553 if (isset($mailbox)) {
554 $mailbox = urldecode( $mailbox );
557 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
558 $mbx_response = sqimap_mailbox_select($imapConnection, $mailbox, false, false, true);
560 if (!isset($messages)) {
562 session_register('messages');
566 * $message contains all information about the message
567 * including header and body
570 $uidvalidity = $mbx_response['UIDVALIDITY'];
572 if (!isset($messages[$uidvalidity])) {
573 $messages[$uidvalidity] = array();
575 if (!isset($messages[$uidvalidity][$passed_id]) ||
!$uid_support) {
576 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
577 $messages[$uidvalidity][$passed_id] = $message;
579 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
580 // $message = $messages[$uidvalidity][$passed_id];
582 $FirstTimeSee = !$message->is_seen
;
583 $message->is_seen
= true;
584 $messages[$uidvalidity][$passed_id] = $message;
586 if (isset($passed_ent_id) && $passed_ent_id) {
587 $message = $message->getEntity($passed_ent_id);
588 if ($message->type0
!= 'message' && $message->type1
!= 'rfc822') {
589 $message = $message->parent
;
591 $read = sqimap_run_command ($imapConnection, "FETCH $passed_id BODY[$passed_ent_id.HEADER]", true, $response, $msg, $uid_support);
592 $rfc822_header = new rfc822_header();
593 $rfc822_header->parseHeader($read);
594 $message->rfc822_header
= $rfc822_header;
598 $header = $message->header
;
602 /****************************************/
603 /* Block for handling incoming url vars */
604 /****************************************/
606 if (isset($sendreceipt)) {
607 if ( !$message->is_mdnsent
) {
608 if (isset($identity) ) {
609 $final_recipient = getPref($data_dir, $username, 'email_address' . '0', '' );
611 $final_recipient = getPref($data_dir, $username, 'email_address', '' );
614 $final_recipient = trim($final_recipient);
615 if ($final_recipient == '' ) {
616 $final_recipient = getPref($data_dir, $username, 'email_address', '' );
618 $supportMDN = ServerMDNSupport($mbx_response["PERMANENTFLAGS"]);
619 if ( SendMDN( $mailbox, $passed_id, $final_recipient, $message ) > 0 && $supportMDN ) {
620 ToggleMDNflag( true, $imapConnection, $mailbox, $passed_id, $uid_support);
621 $message->is_mdnsent
= true;
622 $messages[$uidvalidity][$passed_id]=$message;
627 /***********************************************/
628 /* End of block for handling incoming url vars */
629 /***********************************************/
631 $msgs[$passed_id]['FLAG_SEEN'] = true;
634 do_hook('read_body_top');
635 if ($show_html_default == 1) {
636 $ent_ar = $message->findDisplayEntity(array());
638 $ent_ar = $message->findDisplayEntity(array(), array('text/plain'));
640 $cnt = count($ent_ar);
641 for ($i = 0; $i < $cnt; $i++
) {
642 $messagebody .= formatBody($imapConnection, $message, $color, $wrap_at, $ent_ar[$i], $passed_id, $mailbox);
644 $messagebody .= '<hr noshade size=1>';
648 displayPageHeader($color, $mailbox);
649 formatMenuBar($mailbox, $passed_id, $passed_ent_id, $message, $mbx_response);
650 formatEnvheader($mailbox, $passed_id, $passed_ent_id, $message, $color, $FirstTimeSee);
651 echo '<table width="100%" cellpadding="0" cellspacing="0" align="center" border="0">';
653 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[9].'">';
655 echo ' <table width="100%" cellpadding="3" cellspacing="0" align="center" border="0">';
656 echo ' <tr bgcolor="'.$color[4].'"><td>';
657 echo ' <table cellpadding="0" cellspacing="0" align="center" border="0">';
658 echo ' <tr><td><br>' . $messagebody . '</td></td>';
661 echo ' </table></td></tr>';
665 $attachmentsdisplay = formatAttachments($message,$ent_ar,$mailbox, $passed_id);
666 if ($attachmentsdisplay) {
668 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
670 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[4].'">';
671 echo ' <tr><td ALIGN="left" bgcolor="'.$color[9].'">';
672 echo ' <b>' . _("Attachments") . ':</b>';
675 echo ' <table width="100%" cellpadding="2" cellspacing="2" align="center"'.' border="0" bgcolor="'.$color[0].'"><tr><td>';
676 echo $attachmentsdisplay;
677 echo ' </td></tr></table>';
678 echo ' </table></td></tr>';
679 echo ' </table></td></tr>';
686 /* show attached images inline -- if pref'fed so */
687 if (($attachment_common_show_images) &&
688 is_array($attachment_common_show_images_list)) {
689 foreach ($attachment_common_show_images_list as $img) {
690 $imgurl = '../src/download.php' .
692 'passed_id=' . urlencode($img['passed_id']) .
693 '&mailbox=' . urlencode($mailbox) .
694 '&ent_id=' . urlencode($img['ent_id']) .
695 '&absolute_dl=true';
697 echo html_tag( 'table', "\n" .
698 html_tag( 'tr', "\n" .
699 html_tag( 'td', '<img src="' . $imgurl . '">' ."\n", 'left'
702 'center', '', 'cellspacing=0 border="0" cellpadding="2"');
706 do_hook('read_body_bottom');
707 do_hook('html_bottom');
708 //$message->clean_up();
709 sqimap_logout($imapConnection);