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;
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 $body = charset_encode_japanese($body);
192 $original_recipient = $to;
193 $original_message_id = $header->message_id
;
195 $part2 = "Reporting-UA : $SERVER_NAME ; SquirrelMail (version $version) \r\n";
196 if ($original_recipient != '') {
197 $part2 .= "Original-Recipient : $original_recipient\r\n";
199 $final_recipient = $sender;
200 $part2 .= "Final-Recipient: rfc822; $final_recipient\r\n" .
201 "Original-Message-ID : $original_message_id\r\n" .
202 "Disposition: manual-action/MDN-sent-manually; displayed\r\n";
204 $localfilename = GenerateRandomString(32, 'FILE', 7);
205 $full_localfilename = "$hashed_attachment_dir/$localfilename";
207 $fp = fopen( $full_localfilename, 'w');
208 fwrite ($fp, $part2);
211 $newAttachment = array();
212 $newAttachment['localfilename'] = $localfilename;
213 $newAttachment['type'] = "message/disposition-notification";
214 $newAttachment['session']=-1;
215 $attachments[] = $newAttachment;
217 return (SendMessage($recipient, '', '', _("Read:") . ' ' . $subject,
218 $body, 0, True, 3, -1) );
222 function ToggleMDNflag ( $set ,$imapConnection, $mailbox, $passed_id, $uid_support) {
224 $cmd = 'STORE ' . $passed_id . ' ' . $sg . 'FLAGS ($MDNSent)';
225 $read = sqimap_run_command ($imapConnection, $cmd, true, $response,
226 $readmessage, $uid_support);
229 function ClearAttachments() {
230 global $username, $attachments, $attachment_dir;
232 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
234 $rem_attachments = array();
235 foreach ($attachments as $info) {
236 if ($info['session'] == -1) {
237 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
238 if (file_exists($attached_file)) {
239 unlink($attached_file);
242 $rem_attachments[] = $info;
245 $attachments = $rem_attachments;
248 function formatRecipientString($recipients, $item ) {
249 global $show_more_cc, $show_more, $show_more_bcc,
252 if ((is_array($recipients)) && (isset($recipients[0]))) {
260 $url = set_url_var($PHP_SELF, 'show_more',0);
262 $url = set_url_var($PHP_SELF, 'show_more',1);
264 } else if ($item == 'cc') {
266 $url = set_url_var($PHP_SELF, 'show_more_cc',0);
269 $url = set_url_var($PHP_SELF, 'show_more_cc',1);
271 } else if ($item == 'bcc') {
272 if ($show_more_bcc) {
273 $url = set_url_var($PHP_SELF, 'show_more_bcc',0);
276 $url = set_url_var($PHP_SELF, 'show_more_bcc',1);
283 $ary[$i] = htmlspecialchars($ary[$i]->getAddress());
285 $string .= '<BR>'.$ary[$i];
289 $string .= ' (<A HREF="'.$url;
291 $string .= '">'._("less").'</A>)';
293 $string .= '">'._("more").'</A>)';
307 function formatEnvheader($mailbox, $passed_id, $passed_ent_id, $message,
308 $color, $FirstTimeSee) {
309 global $msn_user_support, $default_use_mdn, $draft_folder, $sent_folder,
310 $default_use_priority, $show_xmailer_default,
311 $mdn_user_support, $PHP_SELF, $javascript_on;
313 $header = $message->rfc822_header
;
315 $env[_("Subject")] = htmlspecialchars(decodeHeader($header->subject
));
316 $from_name = $header->getAddr_s('from');
318 $from_name = $header->getAddr_s('sender');
320 $from_name = _("Unknown sender");
323 $env[_("From")] = htmlspecialchars(decodeHeader($from_name));
324 $env[_("Date")] = getLongDateString($header->date
);
325 $env[_("To")] = formatRecipientString($header->to
, "to");
326 $env[_("Cc")] = formatRecipientString($header->cc
, "cc");
327 $env[_("Bcc")] = formatRecipientString($header->bcc
, "bcc");
328 if ($default_use_priority) {
329 $env[_("Priority")] = getPriorityStr($header->priority
);
331 if ($show_xmailer_default) {
332 $env[_("Mailer")] = decodeHeader($header->xmailer
);
334 if ($default_use_mdn) {
335 if ($mdn_user_support) {
337 if ($message->is_mdnsent
) {
338 $env[_("Read receipt")] = _("send");
340 if ( !($mailbox == $draft_folder ||
341 $mailbox == $sent_folder ||
342 $message->is_deleted ||
344 $mdn_url = $PHP_SELF . '&sendreceipt=1';
345 if ($FirstTimeSee && $javascript_on) {
346 $script = '<script language="JavaScript" type="text/javascript">' ."\n";
347 $script .= '<!--'. "\n";
348 $script .= 'if(window.confirm("' .
349 _("The message sender has requested a response to indicate that you have read this message. Would you like to send a receipt?") .
353 $script .= '// -->'. "\n";
354 $script .= '</script>'. "\n";
357 $env[_("Read receipt")] = _("requested") .
358 ' <a href="'.$mdn_url.'">['. _("Send read receipt now") .']</a>';
360 $env[_("Read receipt")] = _("requested");
367 $s = '<TABLE WIDTH="100%" CELLPADDING="0" CELLSPACING="0" BORDER="0"';
368 $s .= ' ALIGN="center" BGCOLOR="' . $color[0] . '">';
369 foreach ($env as $key => $val) {
372 $s .= html_tag('TD', '<B>' . $key . ': </B>', 'RIGHT', '', 'VALIGN="TOP" WIDTH="20%"') . "\n";
373 $s .= html_tag('TD', $val, 'left', '', 'VALIGN="TOP" WIDTH="80%"') . "\n";
378 do_hook("read_body_header");
379 formatToolbar($mailbox,$passed_id,$passed_ent_id,$message, $color);
383 function formatMenubar($mailbox, $passed_id, $passed_ent_id, $message, $mbx_response) {
384 global $base_uri, $sent_folder, $draft_folder, $where, $what, $color, $sort,
385 $startMessage, $compose_new_win, $PHP_SELF, $save_as_draft,
386 $enable_forward_as_attachment;
388 $topbar_delimiter = ' | ';
389 $urlMailbox = urlencode($mailbox);
390 $s = '<table width="100%" cellpadding="3" cellspacing="0" align="center"'.
391 ' border="0" bgcolor="'.$color[9].'"><tr><td align="left" width="33%"><small>';
393 $msgs_url = $base_uri . 'src/';
394 if (isset($where) && isset($what)) {
395 $msgs_url .= 'search.php?where='.urlencode($where).
396 '&what='.urlencode($what).'&mailbox='.$urlMailbox;
397 $msgs_str = _("Search results");
399 $msgs_url .= 'right_main.php?sort='.$sort.'&startMessage='.
400 $startMessage.'&mailbox='.$urlMailbox;
401 $msgs_str = _("Message List");
403 $s .= '<a href="'. $msgs_url.'">'.$msgs_str.'</a>';
404 $s .= $topbar_delimiter;
406 $delete_url = $base_uri . 'src/delete_message.php?mailbox='.$urlMailbox.
407 '&message='.$passed_id.'&';
408 if (!(isset($passed_ent_id) && $passed_ent_id)) {
409 if ($where && $what) {
410 $delete_url .= 'where=' . urlencode($where) . '&what=' . urlencode($what);
412 $delete_url .= 'sort='. $sort . '&startMessage='. $startMessage;
414 $s .= '<a href="'. $delete_url.'">'._("Delete").'</a>';
417 $comp_uri = $base_uri . 'src/compose.php'.
418 '?passed_id='.$passed_id.
419 '&mailbox='.$urlMailbox.
420 (isset($passed_ent_id)?
'&passed_ent_id='.$passed_ent_id:'');
422 if (($mailbox == $draft_folder) && ($save_as_draft)) {
423 $comp_alt_uri = $comp_uri . '&action=draft';
424 $comp_alt_string = _("Resume Draft");
425 } else if ($mailbox == $sent_folder) {
426 $comp_alt_uri = $comp_uri . '&action=edit_as_new';
427 $comp_alt_string = _("Edit Message as New");
429 if (isset($comp_alt_uri)) {
430 $s .= $topbar_delimiter;
431 if ($compose_new_win == '1') {
432 $s .= '<a href="javascript:void(0)" '.
433 'onclick="comp_in_new(\''.$comp_alt_uri.'\')">'.$comp_alt_string.'</a>';
435 $s .= '<a href="'.$comp_alt_uri.'">'.$comp_alt_string.'</a>';
439 $s .= '</small></td><td align="center" width="33%"><small>';
441 if (!(isset($where) && isset($what)) && !$passed_ent_id) {
442 $prev = findPreviousMessage($mbx_response['EXISTS'], $passed_id);
443 $next = findNextMessage($passed_id);
445 $uri = $base_uri . 'src/read_body.php?passed_id='.$prev.
446 '&mailbox='.$urlMailbox.'&sort='.$sort.
447 '&startMessage='.$startMessage.'&show_more=0';
448 $s .= '<a href="'.$uri.'">'._("Previous").'</a>';
452 $s .= $topbar_delimiter;
454 $uri = $base_uri . 'src/read_body.php?passed_id='.$next.
455 '&mailbox='.$urlMailbox.'&sort='.$sort.
456 '&startMessage='.$startMessage.'&show_more=0';
457 $s .= '<a href="'.$uri.'">'._("Next").'</a>';
461 } else if (isset($passed_ent_id) && $passed_ent_id) {
462 /* code for navigating through attached message/rfc822 messages */
463 $url = set_url_var($PHP_SELF, 'passed_ent_id',0);
464 $s .= '<a href="'.$url.'">'._("View Message").'</a>';
465 $par_ent_id = $message->parent
->entity_id
;
467 $par_ent_id = substr($par_ent_id,0,-2);
468 $s .= $topbar_delimiter;
469 $url = set_url_var($PHP_SELF, 'passed_ent_id',$par_ent_id);
470 $s .= '<a href="'.$url.'">'._("Up").'</a>';
474 $s .= '</small></td><td align="right" width="33%" nowrap><small>';
475 $comp_action_uri = $comp_uri . '&action=forward';
476 if ($compose_new_win == '1') {
477 $s .= '<a href="javascript:void(0)" '.
478 'onclick="comp_in_new(\''.$comp_action_uri.'\')">'._("Forward").'</a>';
480 $s .= '<a href="'.$comp_action_uri.'">'._("Forward").'</a>';
482 $s .= $topbar_delimiter;
484 if ($enable_forward_as_attachment) {
485 $comp_action_uri = $comp_uri . '&action=forward_as_attachment';
486 if ($compose_new_win == '1') {
487 $s .= '<a href="javascript:void(0)" '.
488 'onclick="comp_in_new(\''.$comp_action_uri.'\')">'._("Forward as Attachment").'</a>';
490 $s .= '<a href="'.$comp_action_uri.'">'._("Forward as Attachment").'</a>';
492 $s .= $topbar_delimiter;
496 $comp_action_uri = decodeHeader($comp_uri . '&action=reply');
497 if ($compose_new_win == '1') {
498 $s .= '<a href="javascript:void(0)" '.
499 'onclick="comp_in_new(\''.$comp_action_uri.'\')">'._("Reply").'</a>';
501 $s .= '<a href="'.$comp_action_uri.'">'._("Reply").'</a>';
503 $s .= $topbar_delimiter;
505 $comp_action_uri = $comp_uri . '&action=reply_all';
506 if ($compose_new_win == '1') {
507 $s .= '<a href="javascript:void(0)" '.
508 'onclick="comp_in_new(\''.$comp_action_uri.'\')">'._("Reply All").'</a>';
510 $s .= '<a href="'.$comp_action_uri.'">'._("Reply All").'</a>';
512 $s .= '</small></td></tr></table>';
516 function formatToolbar($mailbox, $passed_id, $passed_ent_id, $message, $color) {
517 global $QUERY_STRING, $base_uri;
519 $urlMailbox = urlencode($mailbox);
520 $url = $base_uri.'src/view_header.php?'.$QUERY_STRING;
523 $s .= '<TD VALIGN="MIDDLE" ALIGN="RIGHT" WIDTH="20%"><B>' . _("Options") . ": </B></TD>\n";
524 $s .= '<TD VALIGN="MIDDLE" ALIGN="LEFT" WIDTH="80%"><SMALL>';
525 $s .= '<a href="'.$url.'">'.("View Full Header").'</a>';
527 /* Output the printer friendly link if we are in subtle mode. */
528 $s .= ' | ';
529 $s .= printer_friendly_link($mailbox, $passed_id, $passed_ent_id, $color);
531 do_hook("read_body_header_right");
532 $s = "</SMALL></TD>\n";
539 /***************************/
540 /* Main of read_boby.php */
541 /***************************/
549 global $uid_support, $sqimap_capabilities;
551 if (isset($mailbox)) {
552 $mailbox = urldecode( $mailbox );
555 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
556 $mbx_response = sqimap_mailbox_select($imapConnection, $mailbox, false, false, true);
558 if (!isset($messages)) {
560 session_register('messages');
564 * $message contains all information about the message
565 * including header and body
568 $uidvalidity = $mbx_response['UIDVALIDITY'];
570 if (!isset($messages[$uidvalidity])) {
571 $messages[$uidvalidity] = array();
573 if (!isset($messages[$uidvalidity][$passed_id]) ||
!$uid_support) {
574 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
575 $messages[$uidvalidity][$passed_id] = $message;
577 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
578 // $message = $messages[$uidvalidity][$passed_id];
580 $FirstTimeSee = !$message->is_seen
;
581 $message->is_seen
= true;
582 $messages[$uidvalidity][$passed_id] = $message;
584 if (isset($passed_ent_id) && $passed_ent_id) {
585 $message = $message->getEntity($passed_ent_id);
586 if ($message->type0
!= 'message' && $message->type1
!= 'rfc822') {
587 $message = $message->parent
;
589 $read = sqimap_run_command ($imapConnection, "FETCH $passed_id BODY[$passed_ent_id.HEADER]", true, $response, $msg, $uid_support);
590 $rfc822_header = new rfc822_header();
591 $rfc822_header->parseHeader($read);
592 $message->rfc822_header
= $rfc822_header;
596 $header = $message->header
;
600 /****************************************/
601 /* Block for handling incoming url vars */
602 /****************************************/
604 if (isset($sendreceipt)) {
605 if ( !$message->is_mdnsent
) {
606 if (isset($identity) ) {
607 $final_recipient = getPref($data_dir, $username, 'email_address' . '0', '' );
609 $final_recipient = getPref($data_dir, $username, 'email_address', '' );
612 $final_recipient = trim($final_recipient);
613 if ($final_recipient == '' ) {
614 $final_recipient = getPref($data_dir, $username, 'email_address', '' );
616 $supportMDN = ServerMDNSupport($mbx_response["PERMANENTFLAGS"]);
617 if ( SendMDN( $mailbox, $passed_id, $final_recipient, $message ) > 0 && $supportMDN ) {
618 ToggleMDNflag( true, $imapConnection, $mailbox, $passed_id, $uid_support);
619 $message->is_mdnsent
= true;
620 $messages[$uidvalidity][$passed_id]=$message;
625 /***********************************************/
626 /* End of block for handling incoming url vars */
627 /***********************************************/
629 $msgs[$passed_id]['FLAG_SEEN'] = true;
632 do_hook('read_body_top');
633 if ($show_html_default == 1) {
634 $ent_ar = $message->findDisplayEntity(array());
636 $ent_ar = $message->findDisplayEntity(array(), array('text/plain'));
638 $cnt = count($ent_ar);
639 for ($i = 0; $i < $cnt; $i++
) {
640 $messagebody .= formatBody($imapConnection, $message, $color, $wrap_at, $ent_ar[$i], $passed_id, $mailbox);
642 $messagebody .= '<hr noshade size=1>';
646 displayPageHeader($color, $mailbox);
647 formatMenuBar($mailbox, $passed_id, $passed_ent_id, $message, $mbx_response);
648 formatEnvheader($mailbox, $passed_id, $passed_ent_id, $message, $color, $FirstTimeSee);
649 echo '<table width="100%" cellpadding="0" cellspacing="0" align="center" border="0">';
651 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[9].'">';
653 echo ' <table width="100%" cellpadding="3" cellspacing="0" align="center" border="0">';
654 echo ' <tr bgcolor="'.$color[4].'"><td>';
655 echo ' <table cellpadding="0" cellspacing="0" align="center" border="0">';
656 echo ' <tr><td><br>' . $messagebody . '</td></td>';
659 echo ' </table></td></tr>';
663 $attachmentsdisplay = formatAttachments($message,$ent_ar,$mailbox, $passed_id);
664 if ($attachmentsdisplay) {
666 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
668 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[4].'">';
669 echo ' <tr><td ALIGN="left" bgcolor="'.$color[9].'">';
670 echo ' <b>' . _("Attachments") . ':</b>';
673 echo ' <table width="100%" cellpadding="2" cellspacing="2" align="center"'.' border="0" bgcolor="'.$color[0].'"><tr><td>';
674 echo $attachmentsdisplay;
675 echo ' </td></tr></table>';
676 echo ' </table></td></tr>';
677 echo ' </table></td></tr>';
684 /* show attached images inline -- if pref'fed so */
685 if (($attachment_common_show_images) &&
686 is_array($attachment_common_show_images_list)) {
687 foreach ($attachment_common_show_images_list as $img) {
688 $imgurl = '../src/download.php' .
690 'passed_id=' . urlencode($img['passed_id']) .
691 '&mailbox=' . urlencode($mailbox) .
692 '&ent_id=' . urlencode($img['ent_id']) .
693 '&absolute_dl=true';
695 echo html_tag( 'table', "\n" .
696 html_tag( 'tr', "\n" .
697 html_tag( 'td', '<img src="' . $imgurl . '">' ."\n", 'left'
700 'center', '', 'cellspacing=0 border="0" cellpadding="2"');
704 do_hook('read_body_bottom');
705 do_hook('html_bottom');
706 //$message->clean_up();
707 sqimap_logout($imapConnection);