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');
22 require_once('../src/view_header.php');
25 * Given an IMAP message id number, this will look it up in the cached
26 * and sorted msgs array and return the index. Used for finding the next
27 * and previous messages.
29 * @return the index of the next valid message from the array
31 function findNextMessage($passed_id) {
32 global $msort, $msgs, $sort,
33 $thread_sort_messages, $allow_server_sort,
35 if (!is_array($server_sort_array)) {
36 $thread_sort_messages = 0;
37 $allow_server_sort = FALSE;
40 if ($thread_sort_messages ||
$allow_server_sort) {
41 reset($server_sort_array);
42 while(list($key, $value) = each ($server_sort_array)) {
43 if ($passed_id == $value) {
44 if ($key == (count($server_sort_array) - 1)) {
48 $result = $server_sort_array[$key +
1];
53 elseif ($sort == 6 && !$allow_server_sort &&
54 !$thread_sort_messages ) {
55 if ($passed_id != 1) {
56 $result = $passed_id - 1;
59 elseif (!$allow_server_sort && !$thread_sort_messages ) {
60 if (!is_array($msort)) {
63 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
64 if ($passed_id == $msgs[$key]['ID']) {
68 $result = $msgs[$key]['ID'];
77 /** returns the index of the previous message from the array. */
78 function findPreviousMessage($numMessages, $passed_id) {
79 global $msort, $sort, $msgs,
80 $thread_sort_messages,
81 $allow_server_sort, $server_sort_array;
83 if (!is_array($server_sort_array)) {
84 $thread_sort_messages = 0;
85 $allow_server_sort = FALSE;
87 if ($thread_sort_messages ||
$allow_server_sort ) {
88 reset($server_sort_array);
89 while(list($key, $value) = each ($server_sort_array)) {
90 if ($passed_id == $value) {
95 $result = $server_sort_array[$key -1];
100 elseif ($sort == 6 && !$allow_server_sort &&
101 !$thread_sort_messages) {
102 if ($passed_id != $numMessages) {
103 $result = $passed_id +
1;
106 elseif (!$thread_sort_messages && !$allow_server_sort) {
107 if (!is_array($msort)) {
110 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
111 if ($passed_id == $msgs[$key]['ID']) {
116 $result = $msgs[$key]['ID'];
126 * Displays a link to a page where the message is displayed more
127 * "printer friendly".
129 function printer_friendly_link($mailbox, $passed_id, $passed_ent_id, $color) {
130 global $javascript_on;
132 $params = '?passed_ent_id=' . $passed_ent_id .
133 '&mailbox=' . urlencode($mailbox) .
134 '&passed_id=' . $passed_id;
136 $print_text = _("View Printable Version");
139 /* Output the link. */
140 if ($javascript_on) {
141 $result .= '<script language="javascript" type="text/javascript">' . "\n" .
143 " function printFormat() {\n" .
144 ' window.open("../src/printer_friendly_main.php' .
145 $params . '","Print","width=800,height=600");' . "\n".
149 "<a href=\"javascript:printFormat();\">$print_text</a>\n";
151 $result .= '<A target="_blank" HREF="../src/printer_friendly_bottom.php' .
152 "$params\">$print_text</a>\n";
157 function ServerMDNSupport( $read ) {
158 /* escaping $ doesn't work -> \x36 */
159 $ret = preg_match( '/(\x36MDNSent|\\\*)/i', $read );
163 function SendMDN ( $mailbox, $passed_id, $sender, $message) {
164 global $username, $attachment_dir, $SERVER_NAME,
165 $version, $attachments;
167 $header = $message->header
;
168 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
170 $recipient_o = $header->dnt
;
171 $recipient = $recipient_o->getAddress(true);
175 $senton = getLongDateString( $header->date
);
176 $to_array = $header->to
;
178 foreach ($to_array as $line) {
182 $subject = $header->subject
;
183 $now = getLongDateString( time() );
187 $body = _("Your message") . "\r\n\r\n" .
188 "\t" . _("To:") . ' ' . $to . "\r\n" .
189 "\t" . _("Subject:") . ' ' . $subject . "\r\n" .
190 "\t" . _("Sent:") . ' ' . $senton . "\r\n" .
192 sprintf( _("Was displayed on %s"), $now );
195 $original_recipient = $to;
196 $original_message_id = $header->message_id
;
198 $part2 = "Reporting-UA : $SERVER_NAME ; SquirrelMail (version $version) \r\n";
199 if ($original_recipient != '') {
200 $part2 .= "Original-Recipient : $original_recipient\r\n";
202 $final_recipient = $sender;
203 $part2 .= "Final-Recipient: rfc822; $final_recipient\r\n" .
204 "Original-Message-ID : $original_message_id\r\n" .
205 "Disposition: manual-action/MDN-sent-manually; displayed\r\n";
207 $localfilename = GenerateRandomString(32, 'FILE', 7);
208 $full_localfilename = "$hashed_attachment_dir/$localfilename";
210 $fp = fopen( $full_localfilename, 'w');
211 fwrite ($fp, $part2);
214 $newAttachment = array();
215 $newAttachment['localfilename'] = $localfilename;
216 $newAttachment['type'] = "message/disposition-notification";
217 $newAttachment['session']=-1;
218 $attachments[] = $newAttachment;
220 return (SendMessage($recipient, '', '', _("Read:") . ' ' . $subject,
221 $body, 0, True, 3, -1) );
225 function ToggleMDNflag ( $set ,$imapConnection, $mailbox, $passed_id, $uid_support) {
227 $cmd = 'STORE ' . $passed_id . ' ' . $sg . 'FLAGS ($MDNSent)';
228 $read = sqimap_run_command ($imapConnection, $cmd, true, $response,
229 $readmessage, $uid_support);
232 function ClearAttachments() {
233 global $username, $attachments, $attachment_dir;
235 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
237 $rem_attachments = array();
238 foreach ($attachments as $info) {
239 if ($info['session'] == -1) {
240 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
241 if (file_exists($attached_file)) {
242 unlink($attached_file);
245 $rem_attachments[] = $info;
248 $attachments = $rem_attachments;
251 function formatRecipientString($recipients, $item ) {
252 global $show_more_cc, $show_more, $show_more_bcc,
255 if ((is_array($recipients)) && (isset($recipients[0]))) {
263 $url = set_url_var($PHP_SELF, 'show_more',0);
265 $url = set_url_var($PHP_SELF, 'show_more',1);
267 } else if ($item == 'cc') {
269 $url = set_url_var($PHP_SELF, 'show_more_cc',0);
272 $url = set_url_var($PHP_SELF, 'show_more_cc',1);
274 } else if ($item == 'bcc') {
275 if ($show_more_bcc) {
276 $url = set_url_var($PHP_SELF, 'show_more_bcc',0);
279 $url = set_url_var($PHP_SELF, 'show_more_bcc',1);
286 $ary[$i] = htmlspecialchars($ary[$i]->getAddress());
288 $string .= '<BR>'.$ary[$i];
292 $string .= ' (<A HREF="'.$url;
294 $string .= '">'._("less").'</A>)';
296 $string .= '">'._("more").'</A>)';
310 function formatEnvheader($mailbox, $passed_id, $passed_ent_id, $message, $color) {
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->header
;
317 $env[_("Subject")] = getLongDateString($header->date
);
318 $from_o = $header->from
;
319 if (is_object($from_o)) {
320 $from_name = $from_o->getAddress();
322 $from_name = _("Unknown sender");
324 $env[_("From")] = htmlspecialchars($from_name);
325 $env[_("Date")] = htmlspecialchars($header->subject
);
326 $env[_("To")] = formatRecipientString($header->to
, "to");
327 $env[_("Cc")] = formatRecipientString($header->cc
, "cc");
328 $env[_("Bcc")] = formatRecipientString($header->bcc
, "bcc");
329 if ($default_use_priority) {
330 $env[_("Priority")] = getPriorityStr($header->priority
);
332 if ($show_xmailer_default) {
333 $env[_("Mailer")] = $header->xmailer
;
335 if ($default_use_mdn) {
336 if ($mdn_user_support) {
338 if ($message->is_mdnsent
) {
339 $env[_("Read receipt")] = _("send");
341 if ( !($mailbox == $draft_folder ||
342 $mailbox == $sent_folder ||
$message->is_deleted
)) {
343 $mdn_url = $PHP_SELF . '&sendreceipt=1';
344 $FirstTimeSee = false;
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" ' .
369 foreach ($env as $key => $val) {
372 $s .= html_tag( 'td', '<b>'.$key.': </b>', 'right', $color[0], 'valign="top" width="20%"') . "\n";
373 $s .= html_tag( 'td', $val, 'left', $color[0], 'valign="top" width="80%"');
381 function formatMenubar($mailbox, $passed_id, $passed_ent_id, $message, $mbx_response) {
382 global $base_uri, $sent_folder, $draft_folder, $where, $what, $color, $sort,
383 $startMessage, $data_dir, $username, $compose_new_win, $PHP_SELF;
385 $topbar_delimiter = ' | ';
386 $urlMailbox = encodeHeader($mailbox);
389 $idents = getPref($data_dir, $username, 'identities');
390 $from_name = $message->header
->from
->getAddress();
391 if (!empty($idents) && $idents > 1) {
392 for ($i = 1; $i < $idents; $i++
) {
393 $enc_from_name = '"'.
394 encodeHeader(getPref($data_dir,
397 '" <' . getPref($data_dir, $username,
398 'email_address' . $i) . '>';
399 if (htmlspecialchars($enc_from_name) == $from_name) {
406 $s = '<table width="100%" cellpadding="3" cellspacing="0" align="center"'.
407 ' border="0" bgcolor="'.$color[9].'"><tr><td align="left" width="33%"><small>';
409 $msgs_url = $base_uri . 'src/';
410 if (isset($where) && isset($what)) {
414 $msgs_url .= 'search.php?where='.urlencode($where).'&pos='.$pos.
415 '&what='.urlencode($what).'&mailbox='.$urlMailbox;
417 $msgs_url .= 'right_main.php?sort='.$sort.'&startMessage='.
418 $startMessage.'&mailbox='.$urlMailbox;
420 $s .= '<a href="'. $msgs_url.'">'._("Message List").'</a>';
421 $s .= $topbar_delimiter;
423 $delete_url = $base_uri . 'src/delete_message.php?mailbox='.$urlMailbox.
424 '&message='.$passed_id.'&';
425 if (!(isset($passed_ent_id) && $passed_ent_id)) {
426 if ($where && $what) {
427 $delete_url .= 'where=' . urlencode($where) . '&what=' . urlencode($what);
429 $delete_url .= 'sort='. $sort . '&startMessage='. $startMessage;
431 $s .= '<a href="'. $delete_url.'">'._("Delete").'</a>';
434 $comp_uri = $base_uri . 'src/compose.php'.
435 '?passed_id='.$passed_id.
436 '&mailbox='.$urlMailbox.
437 (isset($passed_ent_id)?
'&passed_ent_id='.$passed_ent_id:'').
438 '&identity='.$identity;
441 if (($mailbox == $draft_folder) && ($save_as_draft)) {
442 $comp_alt_uri = $comp_uri . '&action=draft';
443 $comp_alt_string = _("Resume Draft");
444 } else if ($mailbox == $sent_folder) {
445 $comp_alt_uri = $comp_uri . '&action=edit_as_new';
446 $comp_alt_string = _("Edit Message as New");
448 if (isset($comp_alt_uri)) {
449 $s .= $topbar_delimiter;
450 if ($compose_new_win == '1') {
451 $s .= '<a href="javascript:void(0)" '.
452 'onclick="comp_in_new("'.$comp_alt_uri.'")">'.$comp_alt_string.'</a>';
454 $s .= '<a href="'.$comp_alt_uri.'">'.$comp_alt_string.'</a>';
458 $s .= '</small></td><td align="center" width="33%"><small>';
460 if (!(isset($where) && isset($what)) && !$passed_ent_id) {
461 $prev = findPreviousMessage($mbx_response['EXISTS'], $passed_id);
462 $next = findNextMessage($passed_id);
464 $uri = $base_uri . 'src/read_body.php?passed_id='.$prev.
465 '&mailbox='.$urlMailbox.'&sort='.$sort.
466 '&startMessage='.$startMessage.'&show_more=0';
467 $s .= '<a href="'.$uri.'">'._("Previous").'</a>';
471 $s .= $topbar_delimiter;
473 $uri = $base_uri . 'src/read_body.php?passed_id='.$next.
474 '&mailbox='.$urlMailbox.'&sort='.$sort.
475 '&startMessage='.$startMessage.'&show_more=0';
476 $s .= '<a href="'.$uri.'">'._("Next").'</a>';
480 } else if (isset($passed_ent_id) && $passed_ent_id) {
481 /* code for navigating through attached message/rfc822 messages */
482 $url = set_url_var($PHP_SELF, 'passed_ent_id',0);
483 $s .= '<a href="'.$url.'">'._("View Message").'</a>';
484 $par_ent_id = $message->parent
->entity_id
;
486 $par_ent_id = substr($par_ent_id,0,-2);
487 $s .= $topbar_delimiter;
488 $url = set_url_var($PHP_SELF, 'passed_ent_id',$par_ent_id);
489 $s .= '<a href="'.$url.'">'._("Up").'</a>';
493 $s .= '</small></td><td align="right" width="33%"><small>';
495 $comp_action_uri = $comp_uri . '&action=forward';
496 if ($compose_new_win == '1') {
497 $s .= '<a href="javascript:void(0)" '.
498 'onclick="comp_in_new(\''.$comp_action_uri.'\')">'._("Forward").'</a>';
500 $s .= '<a href="'.$comp_action_uri.'">'._("Forward").'</a>';
502 $s .= $topbar_delimiter;
504 $comp_action_uri = decodeHeader($comp_uri . '&action=reply');
505 if ($compose_new_win == '1') {
506 $s .= '<a href="javascript:void(0)" '.
507 'onclick="comp_in_new(\''.$comp_action_uri.'\')">'._("Reply").'</a>';
509 $s .= '<a href="'.$comp_action_uri.'">'._("Reply").'</a>';
511 $s .= $topbar_delimiter;
513 $comp_action_uri = $comp_uri . '&action=reply_all';
514 if ($compose_new_win == '1') {
515 $s .= '<a href="javascript:void(0)" '.
516 'onclick="comp_in_new(\''.$comp_action_uri.'\')">'._("Reply All").'</a>';
518 $s .= '<a href="'.$comp_action_uri.'">'._("Reply All").'</a>';
520 $s .= '</small></td></tr></table>';
524 function formatToolbar($mailbox, $passed_id, $passed_ent_id, $message, $color) {
527 $urlMailbox = encodeHeader($mailbox);
528 $s = '<table width="100%" cellpadding="3" cellspacing="0" align="center"'.
529 ' border="0" bgcolor="'.$color[9].'">'. "\n".
530 '<tr align="right"><td valign="top" align="right"><small>';
532 $viewheader_url = $PHP_SELF . '&view_hdr=1';
533 $s .= '<a href="'.$viewheader_url.'">'.("View Full Header").'</a>';
534 /* Output the printer friendly link if we are in subtle mode. */
535 $s .= ' | '.
536 printer_friendly_link($mailbox, $passed_id, $passed_ent_id, $color);
538 do_hook("read_body_header_right");
539 echo '</small></td></tr></table>'."\n";
544 * Main of read_boby.php --------------------------------------------------
553 global $uid_support, $sqimap_capabilities;
555 if (isset($mailbox)) {
556 $mailbox = urldecode( $mailbox );
559 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
562 $mbx_response = sqimap_mailbox_select($imapConnection, $mailbox, false, false, true);
564 if (!isset($messages)) {
566 session_register('messages');
570 * $message contains all information about the message
571 * including header and body
574 $uidvalidity = $mbx_response['UIDVALIDITY'];
576 if (!isset($messages[$uidvalidity])) {
577 $messages[$uidvalidity] = array();
579 if (!isset($messages[$uidvalidity][$passed_id]) ||
!$uid_support) {
580 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
581 $messages[$uidvalidity][$passed_id] = $message;
583 // $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
584 $message = $messages[$uidvalidity][$passed_id];
586 if (isset($passed_ent_id)) {
587 $message = $message->getEntity($passed_ent_id);
588 $message->id
= $passed_id;
589 $message->mailbox
= $mailbox;
593 $header = $message->header
;
595 //do_hook('html_top');
598 * The following code sets necesarry stuff for the MDN thing
600 if($default_use_mdn &&
601 ($mdn_user_support = getPref($data_dir, $username, 'mdn_user_support',
602 $default_use_mdn))) {
603 $supportMDN = ServerMDNSupport($mbx_response["PERMANENTFLAGS"]);
604 $FirstTimeSee = !$message->is_seen
;
607 /* =============================================================================
608 * block for handling incoming url vars
610 * =============================================================================
615 * The following code shows the header of the message and then exit
617 if (isset($view_hdr)) {
618 $template_vars = array();
619 $template_vars['full_header'] = parse_viewheader($imapConnection,$passed_id, $passed_ent_id);
620 $template_vars['return_address'] = set_url_var($PHP_SELF, 'view_hdr');
621 view_header($template_vars, '', '</body></html>');
625 if (isset($sendreceipt)) {
626 if ( !$message->is_mdnsent
) {
627 if (isset($identity) ) {
628 $final_recipient = getPref($data_dir, $username, 'email_address' . '0', '' );
630 $final_recipient = getPref($data_dir, $username, 'email_address', '' );
633 $final_recipient = trim($final_recipient);
634 if ($final_recipient == '' ) {
635 $final_recipient = getPref($data_dir, $username, 'email_address', '' );
638 if ( SendMDN( $mailbox, $passed_id, $final_recipient, $message ) > 0 && $supportMDN ) {
639 ToggleMDNflag( true, $imapConnection, $mailbox, $passed_id, $uid_support);
640 $message->is_mdnsent
= true;
641 $messages[$uidvalidity][$passed_id]=$message;
646 /* =============================================================================
647 * end block for handling incoming url vars
649 * =============================================================================
651 $msgs[$passed_id]['FLAG_SEEN'] = true;
654 $ent_ar = $message->findDisplayEntity(array());
655 for ($i = 0; $i < count($ent_ar); $i++
) {
656 $messagebody .= formatBody($imapConnection, $message, $color, $wrap_at, $ent_ar[$i], $passed_id, $mailbox);
659 displayPageHeader($color, $mailbox);
660 do_hook('read_body_top');
661 formatMenuBar($mailbox, $passed_id, $passed_ent_id, $message, $mbx_response);
662 formatEnvheader($mailbox, $passed_id, $passed_ent_id, $message, $color);
663 formatToolbar($mailbox,$passed_id,$passed_ent_id,$message, $color);
664 echo '<table width="100%" cellpadding="3" cellspacing="3" align="center"'.
665 ' border="0" bgcolor="'.$color[4].'">';
666 echo '<tr><td>'.$messagebody.'</td></tr>';
667 echo '<tr><td>'.formatAttachments($message,$ent_ar,$mailbox, $passed_id).'</td></tr>';
670 /* show attached images inline -- if pref'fed so */
671 if (($attachment_common_show_images) &&
672 is_array($attachment_common_show_images_list)) {
673 foreach ($attachment_common_show_images_list as $img) {
674 $imgurl = '../src/download.php' .
676 'passed_id=' . urlencode($img['passed_id']) .
677 '&mailbox=' . urlencode($mailbox) .
678 '&passed_ent_id=' . urlencode($img['ent_id']) .
679 '&absolute_dl=true';
681 echo html_tag( 'table', "\n" .
682 html_tag( 'tr', "\n" .
683 html_tag( 'td', '<img src="' . $imgurl . '">' ."\n", 'left'
686 'center', '', 'cellspacing=0 border="0" cellpadding="2"');
690 do_hook('read_body_bottom');
691 do_hook('html_bottom');
692 sqimap_logout($imapConnection);