98119416b38c5e9993617cb927bffc42e261da30
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) {
179 $to .= ' '.$line->getAddress();
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,
311 $color, $FirstTimeSee) {
312 global $msn_user_support, $default_use_mdn, $draft_folder, $sent_folder,
313 $default_use_priority, $show_xmailer_default,
314 $mdn_user_support, $PHP_SELF, $javascript_on;
316 $header = $message->header
;
318 $env[_("Subject")] = getLongDateString($header->date
);
319 $from_o = $header->from
;
320 if (is_object($from_o)) {
321 $from_name = $from_o->getAddress();
323 $from_name = _("Unknown sender");
325 $env[_("From")] = htmlspecialchars($from_name);
326 $env[_("Date")] = htmlspecialchars($header->subject
);
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")] = $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 ||
$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" ' .
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_o = $message->header
->from
;
391 if (is_object($from_o)) {
392 $from_name = $from_o->getAddress();
396 if (!empty($idents) && $idents > 1) {
397 for ($i = 1; $i < $idents; $i++
) {
398 $enc_from_name = '"'.
399 encodeHeader(getPref($data_dir,
402 '" <' . getPref($data_dir, $username,
403 'email_address' . $i) . '>';
404 if (htmlspecialchars($enc_from_name) == $from_name) {
411 $s = '<table width="100%" cellpadding="3" cellspacing="0" align="center"'.
412 ' border="0" bgcolor="'.$color[9].'"><tr><td align="left" width="33%"><small>';
414 $msgs_url = $base_uri . 'src/';
415 if (isset($where) && isset($what)) {
419 $msgs_url .= 'search.php?where='.urlencode($where).'&pos='.$pos.
420 '&what='.urlencode($what).'&mailbox='.$urlMailbox;
422 $msgs_url .= 'right_main.php?sort='.$sort.'&startMessage='.
423 $startMessage.'&mailbox='.$urlMailbox;
425 $s .= '<a href="'. $msgs_url.'">'._("Message List").'</a>';
426 $s .= $topbar_delimiter;
428 $delete_url = $base_uri . 'src/delete_message.php?mailbox='.$urlMailbox.
429 '&message='.$passed_id.'&';
430 if (!(isset($passed_ent_id) && $passed_ent_id)) {
431 if ($where && $what) {
432 $delete_url .= 'where=' . urlencode($where) . '&what=' . urlencode($what);
434 $delete_url .= 'sort='. $sort . '&startMessage='. $startMessage;
436 $s .= '<a href="'. $delete_url.'">'._("Delete").'</a>';
439 $comp_uri = $base_uri . 'src/compose.php'.
440 '?passed_id='.$passed_id.
441 '&mailbox='.$urlMailbox.
442 (isset($passed_ent_id)?
'&passed_ent_id='.$passed_ent_id:'').
443 '&identity='.$identity;
446 if (($mailbox == $draft_folder) && ($save_as_draft)) {
447 $comp_alt_uri = $comp_uri . '&action=draft';
448 $comp_alt_string = _("Resume Draft");
449 } else if ($mailbox == $sent_folder) {
450 $comp_alt_uri = $comp_uri . '&action=edit_as_new';
451 $comp_alt_string = _("Edit Message as New");
453 if (isset($comp_alt_uri)) {
454 $s .= $topbar_delimiter;
455 if ($compose_new_win == '1') {
456 $s .= '<a href="javascript:void(0)" '.
457 'onclick="comp_in_new("'.$comp_alt_uri.'")">'.$comp_alt_string.'</a>';
459 $s .= '<a href="'.$comp_alt_uri.'">'.$comp_alt_string.'</a>';
463 $s .= '</small></td><td align="center" width="33%"><small>';
465 if (!(isset($where) && isset($what)) && !$passed_ent_id) {
466 $prev = findPreviousMessage($mbx_response['EXISTS'], $passed_id);
467 $next = findNextMessage($passed_id);
469 $uri = $base_uri . 'src/read_body.php?passed_id='.$prev.
470 '&mailbox='.$urlMailbox.'&sort='.$sort.
471 '&startMessage='.$startMessage.'&show_more=0';
472 $s .= '<a href="'.$uri.'">'._("Previous").'</a>';
476 $s .= $topbar_delimiter;
478 $uri = $base_uri . 'src/read_body.php?passed_id='.$next.
479 '&mailbox='.$urlMailbox.'&sort='.$sort.
480 '&startMessage='.$startMessage.'&show_more=0';
481 $s .= '<a href="'.$uri.'">'._("Next").'</a>';
485 } else if (isset($passed_ent_id) && $passed_ent_id) {
486 /* code for navigating through attached message/rfc822 messages */
487 $url = set_url_var($PHP_SELF, 'passed_ent_id',0);
488 $s .= '<a href="'.$url.'">'._("View Message").'</a>';
489 $par_ent_id = $message->parent
->entity_id
;
491 $par_ent_id = substr($par_ent_id,0,-2);
492 $s .= $topbar_delimiter;
493 $url = set_url_var($PHP_SELF, 'passed_ent_id',$par_ent_id);
494 $s .= '<a href="'.$url.'">'._("Up").'</a>';
498 $s .= '</small></td><td align="right" width="33%"><small>';
500 $comp_action_uri = $comp_uri . '&action=forward';
501 if ($compose_new_win == '1') {
502 $s .= '<a href="javascript:void(0)" '.
503 'onclick="comp_in_new(\''.$comp_action_uri.'\')">'._("Forward").'</a>';
505 $s .= '<a href="'.$comp_action_uri.'">'._("Forward").'</a>';
507 $s .= $topbar_delimiter;
509 $comp_action_uri = decodeHeader($comp_uri . '&action=reply');
510 if ($compose_new_win == '1') {
511 $s .= '<a href="javascript:void(0)" '.
512 'onclick="comp_in_new(\''.$comp_action_uri.'\')">'._("Reply").'</a>';
514 $s .= '<a href="'.$comp_action_uri.'">'._("Reply").'</a>';
516 $s .= $topbar_delimiter;
518 $comp_action_uri = $comp_uri . '&action=reply_all';
519 if ($compose_new_win == '1') {
520 $s .= '<a href="javascript:void(0)" '.
521 'onclick="comp_in_new(\''.$comp_action_uri.'\')">'._("Reply All").'</a>';
523 $s .= '<a href="'.$comp_action_uri.'">'._("Reply All").'</a>';
525 $s .= '</small></td></tr></table>';
529 function formatToolbar($mailbox, $passed_id, $passed_ent_id, $message, $color) {
532 $urlMailbox = encodeHeader($mailbox);
533 $s = '<table width="100%" cellpadding="3" cellspacing="0" align="center"'.
534 ' border="0" bgcolor="'.$color[9].'">'. "\n".
535 '<tr align="right"><td valign="top" align="right"><small>';
537 $viewheader_url = $PHP_SELF . '&view_hdr=1';
538 $s .= '<a href="'.$viewheader_url.'">'.("View Full Header").'</a>';
539 /* Output the printer friendly link if we are in subtle mode. */
540 $s .= ' | '.
541 printer_friendly_link($mailbox, $passed_id, $passed_ent_id, $color);
543 do_hook("read_body_header_right");
544 echo '</small></td></tr></table>'."\n";
549 * Main of read_boby.php --------------------------------------------------
558 global $uid_support, $sqimap_capabilities;
560 if (isset($mailbox)) {
561 $mailbox = urldecode( $mailbox );
564 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
567 $mbx_response = sqimap_mailbox_select($imapConnection, $mailbox, false, false, true);
569 if (!isset($messages)) {
571 session_register('messages');
575 * $message contains all information about the message
576 * including header and body
579 $uidvalidity = $mbx_response['UIDVALIDITY'];
581 if (!isset($messages[$uidvalidity])) {
582 $messages[$uidvalidity] = array();
584 if (!isset($messages[$uidvalidity][$passed_id]) ||
!$uid_support) {
585 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
586 $messages[$uidvalidity][$passed_id] = $message;
588 // $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
589 $message = $messages[$uidvalidity][$passed_id];
591 $FirstTimeSee = !$message->is_seen
;
592 $message->is_seen
= true;
593 $messages[$uidvalidity][$passed_id] = $message;
595 if (isset($passed_ent_id)) {
596 $message = $message->getEntity($passed_ent_id);
597 $message->id
= $passed_id;
598 $message->mailbox
= $mailbox;
602 $header = $message->header
;
604 //do_hook('html_top');
606 /* =============================================================================
607 * block for handling incoming url vars
609 * =============================================================================
614 * The following code shows the header of the message and then exit
616 if (isset($view_hdr)) {
617 $template_vars = array();
618 $template_vars['full_header'] = parse_viewheader($imapConnection,$passed_id, $passed_ent_id);
619 $template_vars['return_address'] = set_url_var($PHP_SELF, 'view_hdr');
620 view_header($template_vars, '', '</body></html>');
624 if (isset($sendreceipt)) {
625 if ( !$message->is_mdnsent
) {
626 if (isset($identity) ) {
627 $final_recipient = getPref($data_dir, $username, 'email_address' . '0', '' );
629 $final_recipient = getPref($data_dir, $username, 'email_address', '' );
632 $final_recipient = trim($final_recipient);
633 if ($final_recipient == '' ) {
634 $final_recipient = getPref($data_dir, $username, 'email_address', '' );
636 $supportMDN = ServerMDNSupport($mbx_response["PERMANENTFLAGS"]);
637 if ( SendMDN( $mailbox, $passed_id, $final_recipient, $message ) > 0 && $supportMDN ) {
638 ToggleMDNflag( true, $imapConnection, $mailbox, $passed_id, $uid_support);
639 $message->is_mdnsent
= true;
640 $messages[$uidvalidity][$passed_id]=$message;
645 /* =============================================================================
646 * end block for handling incoming url vars
648 * =============================================================================
650 $msgs[$passed_id]['FLAG_SEEN'] = true;
653 $ent_ar = $message->findDisplayEntity(array());
654 for ($i = 0; $i < count($ent_ar); $i++
) {
655 $messagebody .= formatBody($imapConnection, $message, $color, $wrap_at, $ent_ar[$i], $passed_id, $mailbox);
658 displayPageHeader($color, $mailbox);
659 do_hook('read_body_top');
660 formatMenuBar($mailbox, $passed_id, $passed_ent_id, $message, $mbx_response);
661 formatEnvheader($mailbox, $passed_id, $passed_ent_id, $message, $color, $FirstTimeSee);
662 formatToolbar($mailbox,$passed_id,$passed_ent_id,$message, $color);
663 echo '<table width="100%" cellpadding="3" cellspacing="3" align="center"'.
664 ' border="0" bgcolor="'.$color[4].'">';
665 echo '<tr><td>'.$messagebody.'</td></tr>';
666 echo '<tr><td>'.formatAttachments($message,$ent_ar,$mailbox, $passed_id).'</td></tr>';
669 /* show attached images inline -- if pref'fed so */
670 if (($attachment_common_show_images) &&
671 is_array($attachment_common_show_images_list)) {
672 foreach ($attachment_common_show_images_list as $img) {
673 $imgurl = '../src/download.php' .
675 'passed_id=' . urlencode($img['passed_id']) .
676 '&mailbox=' . urlencode($mailbox) .
677 '&passed_ent_id=' . urlencode($img['ent_id']) .
678 '&absolute_dl=true';
680 echo html_tag( 'table', "\n" .
681 html_tag( 'tr', "\n" .
682 html_tag( 'td', '<img src="' . $imgurl . '">' ."\n", 'left'
685 'center', '', 'cellspacing=0 border="0" cellpadding="2"');
689 do_hook('read_body_bottom');
690 do_hook('html_bottom');
691 sqimap_logout($imapConnection);