Added ability to configure Google Mail (Gmail) as the mail server behind SquirrelMail
[squirrelmail.git] / src / read_body.php
index 0922c2427d2596ab1b78393b0ed205c2be6e1033..8d9bfb149b49038408000cab95f0519346a68940 100644 (file)
 /**
  * read_body.php
  *
- * Copyright (c) 1999-2002 The SquirrelMail Project Team
- * Licensed under the GNU GPL. For full terms see the file COPYING.
- *
  * This file is used for reading the msgs array and displaying
  * the resulting emails in the right frame.
  *
- * $Id$
+ * @copyright 1999-2010 The SquirrelMail Project Team
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License
+ * @version $Id$
+ * @package squirrelmail
  */
 
-/* Path for SquirrelMail required files. */
-define('SM_PATH','../');
+/** This is the read_body page */
+define('PAGE_NAME', 'read_body');
+
+/**
+ * Include the SquirrelMail initialization file.
+ */
+require('../include/init.php');
 
 /* SquirrelMail required files. */
-require_once(SM_PATH . 'include/validate.php');
 require_once(SM_PATH . 'functions/imap.php');
+require_once(SM_PATH . 'functions/imap_asearch.php'); // => move to mailbox_display
 require_once(SM_PATH . 'functions/mime.php');
 require_once(SM_PATH . 'functions/date.php');
 require_once(SM_PATH . 'functions/url_parser.php');
-require_once(SM_PATH . 'functions/html.php');
-
+require_once(SM_PATH . 'functions/identity.php');
+require_once(SM_PATH . 'functions/mailbox_display.php');
+require_once(SM_PATH . 'functions/forms.php');
+require_once(SM_PATH . 'functions/attachment_common.php');
+require_once(SM_PATH . 'functions/compose.php');
 
 /**
  * Given an IMAP message id number, this will look it up in the cached
- * and sorted msgs array and return the index. Used for finding the next
- * and previous messages.
+ * and sorted msgs array and return the index of the next message
  *
+ * @param int $passed_id The current message UID
  * @return the index of the next valid message from the array
  */
-function findNextMessage($passed_id) {
-    global $msort, $msgs, $sort, 
-           $thread_sort_messages, $allow_server_sort,
-           $server_sort_array;
-    if (!is_array($server_sort_array)) {
-        $thread_sort_messages = 0;
-        $allow_server_sort = FALSE;
-    }
-    $result = -1;
-    if ($thread_sort_messages || $allow_server_sort) {
-        $count = count($server_sort_array) - 1;
-        foreach($server_sort_array as $key=>$value) {
-            if ($passed_id == $value) {
-                if ($key == $count) {
-                    break;
-                }
-                $result = $server_sort_array[$key + 1];
-                break; 
-            }
-        }
+function findNextMessage($uidset,$passed_id='backwards') {
+    if (!is_array($uidset)) {
+        return -1;
+    }
+    if ($passed_id=='backwards' || !is_array($uidset)) { // check for backwards compattibilty gpg plugin
+        $passed_id = $uidset;
+    }
+    $result = sqm_array_get_value_by_offset($uidset,$passed_id,1);
+    if ($result === false) {
+        return -1;
     } else {
-        if (is_array($msort)) {
-            for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
-                if ($passed_id == $msgs[$key]['ID']) {
-                    next($msort);
-                    $key = key($msort);
-                    if (isset($key)){
-                        $result = $msgs[$key]['ID'];
-                        break;
-                    }
-                }
-            }
-        }
+        return $result;
     }
-    return $result;
 }
 
-/** returns the index of the previous message from the array. */
-function findPreviousMessage($numMessages, $passed_id) {
-    global $msort, $sort, $msgs,
-           $thread_sort_messages,
-           $allow_server_sort, $server_sort_array;
-    $result = -1;
-    if (!is_array($server_sort_array)) {
-        $thread_sort_messages = 0;
-        $allow_server_sort = FALSE;
+/**
+ * Given an IMAP message id number, this will look it up in the cached
+ * and sorted msgs array and return the index of the previous message
+ *
+ * @param int $passed_id The current message UID
+ * @return the index of the next valid message from the array
+ */
+
+function findPreviousMessage($uidset, $passed_id) {
+    if (!is_array($uidset)) {
+        return -1;
     }
-    if ($thread_sort_messages || $allow_server_sort ) {
-        foreach($server_sort_array as $key=>$value) {
-            if ($passed_id == $value) {
-                if ($key == 0) {
-                    break;
-                }
-                $result = $server_sort_array[$key - 1];
-                break;
-            }
-        }
+    $result = sqm_array_get_value_by_offset($uidset,$passed_id,-1);
+    if ($result === false) {
+        return -1;
     } else {
-        if (is_array($msort)) {
-            for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
-                if ($passed_id == $msgs[$key]['ID']) {
-                    prev($msort);
-                    $key = key($msort);
-                    if (isset($key)) {
-                        $result = $msgs[$key]['ID'];
-                        break;
-                    }
-                }
-            }
-        }
+        return $result;
     }
-    return $result;
 }
 
-/**
- * Displays a link to a page where the message is displayed more
- * "printer friendly".
- */
-function printer_friendly_link($mailbox, $passed_id, $passed_ent_id, $color) {
-    global $javascript_on;
-
-    $params = '?passed_ent_id=' . $passed_ent_id .
-              '&mailbox=' . urlencode($mailbox) .
-              '&passed_id=' . $passed_id;
-
-    $print_text = _("View Printable Version");
-
-    $result = '';
-    /* Output the link. */
-    if ($javascript_on) {
-        $result .= '<script language="javascript" type="text/javascript">' . "\n" .
-                   '<!--' . "\n" .
-                   "  function printFormat() {\n" .
-                   '    window.open("../src/printer_friendly_main.php' .
-                   $params . '","Print","width=800,height=600");' . "\n".
-                   "  }\n" .
-                   "// -->\n" .
-                   "</script>\n" .
-                   "<a href=\"javascript:printFormat();\">$print_text</a>\n";
+function html_toggle_href ($mailbox, $passed_id, $passed_ent_id, $message) {
+    global $base_uri, $show_html_default;
+
+    $has_html = false;
+    if ($message->header->type0 == 'message' && $message->header->type1 == 'rfc822') {
+        $type0 = $message->rfc822_header->content_type->type0;
+        $type1 = $message->rfc822_header->content_type->type1;
     } else {
-        $result .= '<A target="_blank" HREF="../src/printer_friendly_bottom.php' .
-                   "$params\">$print_text</a>\n";
+        $type0 = $message->header->type0;
+        $type1 = $message->header->type1;
+    }
+    if($type0 == 'multipart' &&
+       ($type1 == 'alternative' || $type1 == 'mixed' || $type1 == 'related' || $type1=='signed')) {
+        if ($message->findDisplayEntity(array(), array('text/html'), true)) {
+            $has_html = true;
+        }
+    }
+    /*
+     * Normal single part message so check its type.
+     */
+    else {
+        if($type0 == 'text' && $type1 == 'html') {
+            $has_html = true;
+        }
     }
-    return $result;
+    if($has_html == true) {
+        $vars = array('passed_ent_id', 'show_more', 'show_more_cc', 'override_type0', 'override_type1', 'startMessage','where', 'what');
+
+        $new_link = $base_uri . 'src/read_body.php?passed_id=' . urlencode($passed_id) .
+                    '&amp;passed_ent_id=' . urlencode($passed_ent_id) .
+                    '&amp;mailbox=' . urlencode($mailbox);
+        foreach($vars as $var) {
+            if(sqgetGlobalVar($var, $temp)) {
+                $new_link .= '&amp;' . $var . '=' . urlencode($temp);
+            }
+        }
+
+        if($show_html_default == 1) {
+            $new_link .= '&amp;show_html_default=0';
+        } else {
+            $new_link .= '&amp;show_html_default=1';
+        }
+        return $new_link;
+    }
+    return '';
 }
 
-function ServerMDNSupport($read) {
-    /* escaping $ doesn't work -> \x36 */    
-    $ret = preg_match('/(\x36MDNSent|\\\*)/i', $read);
-    return $ret;
+function ServerMDNSupport($aFlags) {
+    /* escaping $ doesn't work -> \x36 */
+    return ( in_array('$mdnsent',$aFlags,true) ||
+             in_array('\\*',$aFlags,true) ) ;
 }
 
-function SendMDN ( $mailbox, $passed_id, $sender, $message, $imapConnection) {
-    global $username, $attachment_dir, $_SERVER,
-           $version, $attachments, $squirrelmail_language, $default_charset,
-           $languages, $useSendmail, $domain, $sent_folder,
-           $popuser, $data_dir, $username;
+function SendMDN ( $mailbox, $passed_id, $message, $imapConnection) {
+    global $squirrelmail_language, $default_charset, $default_move_to_sent,
+           $languages, $useSendmail, $domain, $sent_folder, $username,
+           $data_dir;
 
-    $SERVER_NAME = $_SERVER['SERVER_NAME'];
+    sqgetGlobalVar('SERVER_NAME', $SERVER_NAME, SQ_SERVER);
 
     $header = $message->rfc822_header;
-    $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
 
     $rfc822_header = new Rfc822Header();
     $content_type  = new ContentType('multipart/report');
@@ -164,35 +146,29 @@ function SendMDN ( $mailbox, $passed_id, $sender, $message, $imapConnection) {
     }
     $rfc822_header->content_type = $content_type;
     $rfc822_header->to[] = $header->dnt;
-    $rfc822_header->subject = _("Read:") . ' ' . $header->subject;
-
-
-    $reply_to = '';
-    if (isset($identity) && $identity != 'default') {
-        $from_mail = getPref($data_dir, $username, 
-                             'email_address' . $identity);
-        $full_name = getPref($data_dir, $username, 
-                             'full_name' . $identity);
-        $from_addr = '"'.$full_name.'" <'.$from_mail.'>';
-        $reply_to  = getPref($data_dir, $username, 
-                             'reply_to' . $identity);
-    } else {
-        $from_mail = getPref($data_dir, $username, 'email_address');
-        $full_name = getPref($data_dir, $username, 'full_name');
-        $from_addr = '"'.$full_name.'" <'.$from_mail.'>';
-        $reply_to  = getPref($data_dir, $username,'reply_to');
-    }
-    if (!$from_addr) {
-       $from_addr = "$popuser@$domain";
-       $from_mail = $from_addr;
+    $rfc822_header->subject = _("Read:") . ' ' . decodeHeader($header->subject,true,false);
+
+    $idents = get_identities();
+    $needles = array();
+    if ($header->to) {
+        foreach ($header->to as $message_to) {
+             $needles[] = $message_to->mailbox.'@'.$message_to->host;
+        }
     }
+    $identity = find_identity($needles);
+    $from_addr = build_from_header($identity);
+    $reply_to = isset($idents[$identity]['reply_to']) ? $idents[$identity]['reply_to'] : '';
+    // FIXME: this must actually be the envelope address of the orginal message,
+    // but do we have that information? For now the first identity is our best guess.
+    $final_recipient = $idents[0]['email_address'];
+
     $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true);
     if ($reply_to) {
        $rfc822_header->reply_to = $rfc822_header->parseAddress($reply_to,true);
     }
 
     // part 1 (RFC2298)
-    $senton = getLongDateString( $header->date );
+    $senton = getLongDateString( $header->date, $header->date_unparsed );
     $to_array = $header->to;
     $to = '';
     foreach ($to_array as $line) {
@@ -201,16 +177,16 @@ function SendMDN ( $mailbox, $passed_id, $sender, $message, $imapConnection) {
     $now = getLongDateString( time() );
     set_my_charset();
     $body = _("Your message") . "\r\n\r\n" .
-            "\t" . _("To:") . ' ' . $to . "\r\n" .
-            "\t" . _("Subject:") . ' ' . $header->subject . "\r\n" .
-            "\t" . _("Sent:") . ' ' . $senton . "\r\n" .
+            "\t" . _("To") . ': ' . decodeHeader($to,false,false) . "\r\n" .
+            "\t" . _("Subject") . ': ' . decodeHeader($header->subject,false,false) . "\r\n" .
+            "\t" . _("Sent") . ': ' . $senton . "\r\n" .
             "\r\n" .
             sprintf( _("Was displayed on %s"), $now );
 
     $special_encoding = '';
-    if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && 
-        function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
-        $body = $languages[$squirrelmail_language]['XTRA_CODE']('encode', $body);
+    if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
+        function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_encode')) {
+        $body = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_encode', $body);
         if (strtolower($default_charset) == 'iso-2022-jp') {
             if (mb_detect_encoding($body) == 'ASCII') {
                 $special_encoding = '8bit';
@@ -219,6 +195,8 @@ function SendMDN ( $mailbox, $passed_id, $sender, $message, $imapConnection) {
                 $special_encoding = '7bit';
             }
         }
+    } elseif (sq_is8bit($body)) {
+        $special_encoding = '8bit';
     }
     $part1 = new Message();
     $part1->setBody($body);
@@ -228,7 +206,7 @@ function SendMDN ( $mailbox, $passed_id, $sender, $message, $imapConnection) {
     if ($special_encoding) {
         $mime_header->encoding = $special_encoding;
     } else {
-        $mime_header->encoding = 'us-ascii';
+        $mime_header->encoding = '7bit';
     }
     if ($default_charset) {
         $mime_header->parameters['charset'] = $default_charset;
@@ -239,11 +217,10 @@ function SendMDN ( $mailbox, $passed_id, $sender, $message, $imapConnection) {
     $original_recipient  = $to;
     $original_message_id = $header->message_id;
 
-    $report = "Reporting-UA : $SERVER_NAME ; SquirrelMail (version $version) \r\n";
+    $report = "Reporting-UA : $SERVER_NAME ; SquirrelMail (version " . SM_VERSION . ") \r\n";
     if ($original_recipient != '') {
         $report .= "Original-Recipient : $original_recipient\r\n";
     }
-    $final_recipient = $sender;
     $report .= "Final-Recipient: rfc822; $final_recipient\r\n" .
               "Original-Message-ID : $original_message_id\r\n" .
               "Disposition: manual-action/MDN-sent-manually; displayed\r\n";
@@ -253,7 +230,7 @@ function SendMDN ( $mailbox, $passed_id, $sender, $message, $imapConnection) {
     $mime_header = new MessageHeader;
     $mime_header->type0 = 'message';
     $mime_header->type1 = 'disposition-notification';
-    $mime_header->encoding = 'us-ascii';
+    $mime_header->encoding = '7bit';
     $part2->mime_header = $mime_header;
 
     $composeMessage = new Message();
@@ -264,78 +241,90 @@ function SendMDN ( $mailbox, $passed_id, $sender, $message, $imapConnection) {
 
     if ($useSendmail) {
         require_once(SM_PATH . 'class/deliver/Deliver_SendMail.class.php');
-        global $sendmail_path;
-        $deliver = new Deliver_SendMail();
+        global $sendmail_path, $sendmail_args;
+        // Check for outdated configuration
+        if (!isset($sendmail_args)) {
+            if ($sendmail_path=='/var/qmail/bin/qmail-inject') {
+                $sendmail_args = '';
+            } else {
+                $sendmail_args = '-i -t';
+            }
+        }
+        $deliver = new Deliver_SendMail(array('sendmail_args'=>$sendmail_args));
         $stream = $deliver->initStream($composeMessage,$sendmail_path);
     } else {
         require_once(SM_PATH . 'class/deliver/Deliver_SMTP.class.php');
         $deliver = new Deliver_SMTP();
-        global $smtpServerAddress, $smtpPort, $use_authenticated_smtp, $pop_before_smtp;
-        if ($use_authenticated_smtp) {
-            global $key, $onetimepad;
-            $user = $username;
-            $pass = OneTimePadDecrypt($key, $onetimepad);
-        } else {
-            $user = '';
-            $pass = '';
-        }
+        global $smtpServerAddress, $smtpPort, $pop_before_smtp, $pop_before_smtp_host;
         $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ? true : false;
+        if (empty($pop_before_smtp_host)) $pop_before_smtp_host = $smtpServerAddress;
+        get_smtp_user($user, $pass);
         $stream = $deliver->initStream($composeMessage,$domain,0,
-                                       $smtpServerAddress, $smtpPort, $authPop);
+                                       $smtpServerAddress, $smtpPort, $user, $pass, $authPop, $pop_before_smtp_host);
     }
     $success = false;
     if ($stream) {
-        $length  = $deliver->mail($composeMessage, $stream);
+        $deliver->mail($composeMessage, $stream);
         $success = $deliver->finalizeStream($stream);
     }
     if (!$success) {
-        $msg  = $deliver->dlv_msg . '<br>Server replied: '.$deliver->dlv_ret_nr;
-        require_once(SM_PATH . 'functions/display_messages.php');
-        plain_error_message($msg, $color);
+        $msg = _("Message not sent.") . "\n" .
+            $deliver->dlv_msg;
+        if (! empty($deliver->dlv_server_msg)) {
+            $msg.= "\n" .
+                _("Server replied:") . ' ' . $deliver->dlv_ret_nr . ' ' .
+                nl2br(htmlspecialchars($deliver->dlv_server_msg));
+        }
+        plain_error_message($msg);
     } else {
         unset ($deliver);
-        if (sqimap_mailbox_exists ($imapConnection, $sent_folder)) {
-            sqimap_append ($imapConnection, $sent_folder, $length);
+
+        // move to sent folder
+        //
+        $move_to_sent = getPref($data_dir,$username,'move_to_sent');
+        if (isset($default_move_to_sent) && ($default_move_to_sent != 0)) {
+            $svr_allow_sent = true;
+        } else {
+            $svr_allow_sent = false;
+        }
+
+        if (isset($sent_folder) && (($sent_folder != '') || ($sent_folder != 'none'))
+                && sqimap_mailbox_exists( $imapConnection, $sent_folder)) {
+            $fld_sent = true;
+        } else {
+            $fld_sent = false;
+        }
+
+        if ((isset($move_to_sent) && ($move_to_sent != 0)) || (!isset($move_to_sent))) {
+            $lcl_allow_sent = true;
+        } else {
+            $lcl_allow_sent = false;
+        }
+
+        if (($fld_sent && $svr_allow_sent && !$lcl_allow_sent) || ($fld_sent && $lcl_allow_sent)) {
+            $save_reply_with_orig=getPref($data_dir,$username,'save_reply_with_orig');
+            if ($save_reply_with_orig) {
+                $sent_folder = $mailbox;
+            }
             require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
             $imap_deliver = new Deliver_IMAP();
-            $imap_deliver->mail($composeMessage, $imapConnection);
-            sqimap_append_done ($imapConnection);
+            $imap_deliver->mail($composeMessage, $imapConnection, 0, 0, $imapConnection, $sent_folder);
             unset ($imap_deliver);
         }
     }
     return $success;
 }
 
-
-function ToggleMDNflag ($set ,$imapConnection, $mailbox, $passed_id, $uid_support) {
+function ToggleMDNflag ($set ,$imapConnection, $mailbox, $passed_id) {
     $sg   =  $set?'+':'-';
     $cmd  = 'STORE ' . $passed_id . ' ' . $sg . 'FLAGS ($MDNSent)';
-    $read = sqimap_run_command ($imapConnection, $cmd, true, $response, 
-                                $readmessage, $uid_support);
-}
-
-function ClearAttachments() {
-    global $username, $attachments, $attachment_dir;
-
-    $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
-
-    $rem_attachments = array();
-    foreach ($attachments as $info) {
-        if ($info['session'] == -1) {
-            $attached_file = "$hashed_attachment_dir/$info[localfilename]";
-            if (file_exists($attached_file)) {
-                unlink($attached_file);
-            }
-        } else {
-            $rem_attachments[] = $info;
-        }
-    }
-    $attachments = $rem_attachments;
+    $read = sqimap_run_command ($imapConnection, $cmd, true, $response,
+                                $readmessage, TRUE);
 }
 
 function formatRecipientString($recipients, $item ) {
-    global $show_more_cc, $show_more, $show_more_bcc,
-           $PHP_SELF;
+    global $show_more, $show_more_cc, $show_more_bcc,
+           $PHP_SELF, $oTemplate;
 
     $string = '';
     if ((is_array($recipients)) && (isset($recipients[0]))) {
@@ -364,244 +353,418 @@ function formatRecipientString($recipients, $item ) {
             }
         }
 
-        $cnt = count($recipients);
-        foreach($recipients as $r) {
-            $add = htmlspecialchars($r->getAddress());
-            if ($string) {
-                $string .= '<BR>' . $add;
-            } else {
-                $string = $add;
-                if ($cnt > 1) {
-                    $string .= '&nbsp;(<A HREF="'.$url;
-                    if ($show) {
-                       $string .= '">'._("less").'</A>)';
-                    } else {
-                       $string .= '">'._("more").'</A>)';
-                       break;
-                    }
-                }
-            }
+        $a = array();
+        foreach ($recipients as $r) {
+            $a[] = array(
+                            // note: decodeHeader is htmlsafe by default
+                            'Name'  => decodeHeader($r->getAddress(false)),
+                            'Email' => htmlspecialchars($r->getEmail()),
+                            'Full'  => decodeHeader($r->getAddress(true))
+                        );
         }
+
+        $oTemplate->assign('which_field', $item);
+        $oTemplate->assign('recipients', $a);
+        $oTemplate->assign('more_less_toggle_href', $url);
+        $oTemplate->assign('show_more', $show);
+
+        $string = $oTemplate->fetch('read_recipient_list.tpl');
     }
     return $string;
 }
 
-function formatEnvheader($mailbox, $passed_id, $passed_ent_id, $message, 
+function formatEnvheader($aMailbox, $passed_id, $passed_ent_id, $message,
                          $color, $FirstTimeSee) {
-    global $msn_user_support, $default_use_mdn, $draft_folder, $sent_folder,
-           $default_use_priority, $show_xmailer_default, 
-           $mdn_user_support, $PHP_SELF, $javascript_on;
+    global $default_use_mdn, $default_use_priority,
+           $show_xmailer_default, $mdn_user_support, $PHP_SELF,
+           $squirrelmail_language, $oTemplate;
+
+    $mailbox = $aMailbox['NAME'];
 
     $header = $message->rfc822_header;
     $env = array();
-    $env[_("Subject")] = htmlspecialchars(decodeHeader($header->subject));   
+    $env[_("Subject")] = str_replace("&nbsp;"," ",decodeHeader($header->subject));
+
     $from_name = $header->getAddr_s('from');
-    if (!$from_name) {
+    if (!$from_name)
         $from_name = $header->getAddr_s('sender');
-        if (!$from_name) {
-            $from_name = _("Unknown sender");
-        }
-    }
-    $env[_("From")] = htmlspecialchars(decodeHeader($from_name));
-    $env[_("Date")] = getLongDateString($header->date);
+    if (!$from_name)
+        $env[_("From")] = _("Unknown sender");
+    else
+        $env[_("From")] = decodeHeader($from_name);
+    $env[_("Date")] = getLongDateString($header->date, $header->date_unparsed);
     $env[_("To")] = formatRecipientString($header->to, "to");
     $env[_("Cc")] = formatRecipientString($header->cc, "cc");
     $env[_("Bcc")] = formatRecipientString($header->bcc, "bcc");
     if ($default_use_priority) {
-        $env[_("Priority")] = getPriorityStr($header->priority);
+        $oTemplate->assign('message_priority', $header->priority);
+        $env[_("Priority")] = $oTemplate->fetch('read_message_priority.tpl');
     }
     if ($show_xmailer_default) {
-        $env[_("Mailer")] = decodeHeader($header->xmailer);
+        $oTemplate->assign('xmailer', decodeHeader($header->xmailer));
+        $env[_("Mailer")] = $oTemplate->fetch('read_xmailer.tpl');
     }
+
+    // this is used for both mdn and also general use for plugins, etc
+    $oTemplate->assign('first_time_reading', $FirstTimeSee);
+
     if ($default_use_mdn) {
         if ($mdn_user_support) {
             if ($header->dnt) {
-                if ($message->is_mdnsent) {
-                    $env[_("Read receipt")] = _("send");
-                } else {
-                    $env[_("Read receipt")] = _("requested"); 
-                    if (!($mailbox == $draft_folder || 
-                          $mailbox == $sent_folder  || 
-                          $message->is_deleted ||
-                          $passed_ent_id)) {
-                        $mdn_url = $PHP_SELF . '&sendreceipt=1';
-                        if ($FirstTimeSee && $javascript_on) {
-                            $script  = '<script language="JavaScript" type="text/javascript">' . "\n";
-                            $script .= '<!--'. "\n";
-                            $script .= 'if(window.confirm("' .
-                                       _("The message sender has requested a response to indicate that you have read this message. Would you like to send a receipt?") .
-                                       '")) {  '."\n" .
-                                       '    sendMDN()'.
-                                       '}' . "\n";
-                            $script .= '// -->'. "\n";
-                            $script .= '</script>'. "\n";
-                            echo $script;
-                        }
-                        $env[_("Read receipt")] .= '&nbsp;<a href="' . $mdn_url . '">[' .
-                                                   _("Send read receipt now") . ']</a>';
-                    }
-                }
+                $mdn_url = $PHP_SELF;
+                $mdn_url = set_url_var($mdn_url, 'mailbox', urlencode($mailbox));
+                $mdn_url = set_url_var($mdn_url, 'passed_id', $passed_id);
+                $mdn_url = set_url_var($mdn_url, 'passed_ent_id', $passed_ent_id);
+                $mdn_url = set_url_var($mdn_url, 'sendreceipt', 1);
+
+                $oTemplate->assign('read_receipt_sent', $message->is_mdnsent);
+                $oTemplate->assign('send_receipt_href', $mdn_url);
+
+                $env[_("Read Receipt")] = $oTemplate->fetch('read_handle_receipt.tpl');
             }
         }
     }
 
-    $s  = '<TABLE WIDTH="100%" CELLPADDING="0" CELLSPACING="2" BORDER="0"';
-    $s .= ' ALIGN="center" BGCOLOR="'.$color[0].'">';
-    foreach ($env as $key => $val) {
-        if ($val) {
-            $s .= '<TR>';
-            $s .= html_tag('TD', '<B>' . $key . ':&nbsp;&nbsp;</B>', 'RIGHT', '', 'VALIGN="TOP" WIDTH="20%"') . "\n";
-            $s .= html_tag('TD', $val, 'left', '', 'VALIGN="TOP" WIDTH="80%"') . "\n";
-            $s .= '</TR>';
+    $statuses = array();
+    if (isset($aMailbox['MSG_HEADERS'][$passed_id]['FLAGS'])) {
+        if (isset($aMailbox['MSG_HEADERS'][$passed_id]['FLAGS']['\\deleted']) &&
+                  $aMailbox['MSG_HEADERS'][$passed_id]['FLAGS']['\\deleted'] === true) {
+            $statuses[] = _("deleted");
+        }
+        if (isset($aMailbox['MSG_HEADERS'][$passed_id]['FLAGS']['\\answered']) &&
+                  $aMailbox['MSG_HEADERS'][$passed_id]['FLAGS']['\\answered'] === true) {
+            $statuses[] = _("answered");
+        }
+        if (isset($aMailbox['MSG_HEADERS'][$passed_id]['FLAGS']['\\draft']) &&
+                  $aMailbox['MSG_HEADERS'][$passed_id]['FLAGS']['\\draft'] === true) {
+            $statuses[] = _("draft");
+        }
+        if (isset($aMailbox['MSG_HEADERS'][$passed_id]['FLAGS']['\\flagged']) &&
+                  $aMailbox['MSG_HEADERS'][$passed_id]['FLAGS']['\\flagged'] === true) {
+            $statuses[] = _("flagged");
+        }
+        if ( count($statuses) ) {
+            $env[_("Status")] = implode(', ', $statuses);
         }
     }
-    echo '<TABLE BGCOLOR="'.$color[9].'" WIDTH="100%" CELLPADDING="1"'.
-         ' CELLSPACING="0" BORDER="0" ALIIGN="center">'."\n";
-    echo '<TR><TD HEIGHT="7" COLSPAN="2" BGCOLOR="'.$color[4].'"></TD></TR><TR><TD align=center>'."\n";
-    echo $s;
-    do_hook("read_body_header");
-    formatToolbar($mailbox, $passed_id, $passed_ent_id, $message, $color);
-    echo '</TABLE>';
-    echo '</TD></TR><TR><TD HEIGHT="7" COLSPAN="2" BGCOLOR="'.$color[4].'"></TD></TR>'."\n";
-    echo '</TABLE>';
+
+    $env[_("Options")] = formatToolbar($mailbox, $passed_id, $passed_ent_id, $message, $color);
+
+
+    $oTemplate->assign('headers_to_display', $env);
+
+    $oTemplate->display('read_headers.tpl');
 }
 
-function formatMenubar($mailbox, $passed_id, $passed_ent_id, $message, $mbx_response) {
-    global $base_uri, $sent_folder, $draft_folder, $where, $what, $color, $sort,
-           $startMessage, $compose_new_win, $PHP_SELF, $save_as_draft,
-           $enable_forward_as_attachment;
+/**
+ * Format message toolbar
+ *
+ * @param array   $aMailbox      Current mailbox information array
+ * @param int     $passed_id     UID of current message
+ * @param int     $passed_ent_id Id of entity within message
+ * @param object  $message       Current message object
+ * @param void    $removedVar    This parameter is no longer used, but remains
+ *                               so as not to break this function's prototype
+ *                               (OPTIONAL)
+ * @param boolean $nav_on_top    When TRUE, the menubar is being constructed
+ *                               for use at the top of the page, otherwise it
+ *                               will be used for page bottom (OPTIONAL;
+ *                               default = TRUE)
+ */
+function formatMenubar($aMailbox, $passed_id, $passed_ent_id, $message,
+                       $removedVar=FALSE, $nav_on_top=TRUE) {
+
+    global $base_uri, $draft_folder, $where, $what, $sort,
+           $startMessage, $PHP_SELF, $save_as_draft,
+           $enable_forward_as_attachment, $imapConnection, $lastTargetMailbox,
+           $delete_prev_next_display, $show_copy_buttons,
+           $compose_new_win, $compose_width, $compose_height,
+           $oTemplate;
+
+    //FIXME cleanup argument list, use $aMailbox where possible
+    $mailbox = $aMailbox['NAME'];
 
-    $topbar_delimiter = '&nbsp;|&nbsp;';
     $urlMailbox = urlencode($mailbox);
-    $s = '<table width="100%" cellpadding="3" cellspacing="0" align="center"'.
-         ' border="0" bgcolor="'.$color[9].'"><tr><td align="left" width="33%"><small>';
-
-    $msgs_url = $base_uri . 'src/';
-    if (isset($where) && isset($what)) {
-        $msgs_url .= 'search.php?where=' . urlencode($where) .
-                     '&amp;what=' . urlencode($what) . '&amp;mailbox=' . $urlMailbox;
-        $msgs_str  = _("Search results");
-    } else {
-        $msgs_url .= 'right_main.php?sort=' . $sort . '&amp;startMessage=' .
-                     $startMessage . '&amp;mailbox=' . $urlMailbox;
-        $msgs_str  = _("Message List");
-    }
-    $s .= '<a href="' . $msgs_url . '">' . $msgs_str . '</a>';
-    $s .= $topbar_delimiter;
-
-    $delete_url = $base_uri . 'src/delete_message.php?mailbox=' . $urlMailbox .
-                  '&amp;message=' . $passed_id . '&amp;';
-    if (!(isset($passed_ent_id) && $passed_ent_id)) {
-        if ($where && $what) {
-            $delete_url .= 'where=' . urlencode($where) . '&amp;what=' . urlencode($what);
-        } else {
-            $delete_url .= 'sort=' . $sort . '&amp;startMessage=' . $startMessage;
+
+    // Create Prev & Next links
+    // Handle nested entities first (i.e. Mime Attach parts)
+    $prev_href = $next_href = $up_href = $del_href = $del_prev_href = $del_next_href = '';
+    $msg_list_href = $search_href = $view_msg_href = '';
+    if (isset($passed_ent_id) && $passed_ent_id) {
+        // code for navigating through attached message/rfc822 messages
+        $url = set_url_var($PHP_SELF, 'passed_ent_id',0);
+        $entities     = array();
+        $entity_count = array();
+        $c = 0;
+
+        foreach($message->parent->entities as $ent) {
+            if ($ent->type0 == 'message' && $ent->type1 == 'rfc822') {
+
+                $c++;
+                $entity_count[$c] = $ent->entity_id;
+                $entities[$ent->entity_id] = $c;
+            }
         }
-        $s .= '<a href="' . $delete_url . '">' . _("Delete") . '</a>';
-    }
 
-    $comp_uri = $base_uri . 'src/compose.php' .
-                            '?passed_id=' . $passed_id .
-                            '&amp;mailbox=' . $urlMailbox .
-                            (isset($passed_ent_id)?'&amp;passed_ent_id='.$passed_ent_id:'');
+        if(isset($entities[$passed_ent_id]) && $entities[$passed_ent_id] > 1) {
+            $prev_ent_id = $entity_count[$entities[$passed_ent_id] - 1];
+            $prev_href = set_url_var($PHP_SELF, 'passed_ent_id', $prev_ent_id);
+        }
 
-    if ($compose_new_win == '1') {
-        $link_open  = '<a href="javascript:void(0)" onclick="comp_in_new(\'';
-        $link_close = '\')">';
-    } else {
-        $link_open  = '<a href="';
-        $link_close = '">';
-    }
-    if (($mailbox == $draft_folder) && ($save_as_draft)) {
-        $comp_alt_uri = $comp_uri . '&amp;action=draft';
-        $comp_alt_string = _("Resume Draft");
-    } else if ($mailbox == $sent_folder) {
-        $comp_alt_uri = $comp_uri . '&amp;action=edit_as_new';
-        $comp_alt_string = _("Edit Message as New");
-    }
-    if (isset($comp_alt_uri)) {
-        $s .= $topbar_delimiter;
-        $s .= $link_open . $comp_alt_uri . $link_close . $comp_alt_string . '</a>';
-    }
+        if(isset($entities[$passed_ent_id]) && $entities[$passed_ent_id] < $c) {
+            $next_ent_id = $entity_count[$entities[$passed_ent_id] + 1];
+            $next_href = set_url_var($PHP_SELF, 'passed_ent_id', $next_ent_id);
+        }
+
+        $par_ent_id = $message->parent->entity_id;
+        if ($par_ent_id) {
+            $par_ent_id = substr($par_ent_id,0,-2);
+            if ( $par_ent_id != 0 ) {
+                $up_href = set_url_var($PHP_SELF, 'passed_ent_id',$par_ent_id);
+            }
+        }
 
-    $s .= '</small></td><td align="center" width="33%"><small>';
+        $view_msg_href = $url;
 
-    if (!(isset($where) && isset($what)) && !$passed_ent_id) {
-        $prev = findPreviousMessage($mbx_response['EXISTS'], $passed_id);
-        $next = findNextMessage($passed_id);
-        if ($prev != -1) {
-            $uri = $base_uri . 'src/read_body.php?passed_id='.$prev.
+    // Prev/Next links for regular messages
+    } else if ( true ) { //!(isset($where) && isset($what)) ) {
+        $prev = findPreviousMessage($aMailbox['UIDSET'][$what], $passed_id);
+        $next = findNextMessage($aMailbox['UIDSET'][$what],$passed_id);
+
+        if ($prev >= 0) {
+            $prev_href = $base_uri . 'src/read_body.php?passed_id='.$prev.
                    '&amp;mailbox='.$urlMailbox.'&amp;sort='.$sort.
+                   "&amp;where=$where&amp;what=$what" .
                    '&amp;startMessage='.$startMessage.'&amp;show_more=0';
-            $s .= '<a href="'.$uri.'">'._("Previous").'</a>';       
-        } else {
-            $s .= _("Previous");
         }
-        $s .= $topbar_delimiter;
-        if ($next != -1) {
-            $uri = $base_uri . 'src/read_body.php?passed_id='.$next.
+
+        if ($next >= 0) {
+            $next_href = $base_uri . 'src/read_body.php?passed_id='.$next.
                    '&amp;mailbox='.$urlMailbox.'&amp;sort='.$sort.
+                   "&amp;where=$where&amp;what=$what" .
                    '&amp;startMessage='.$startMessage.'&amp;show_more=0';
-            $s .= '<a href="'.$uri.'">'._("Next").'</a>';
-        } else {
-            $s .= _("Next");
         }
-    } else if (isset($passed_ent_id) && $passed_ent_id) {
-        /* code for navigating through attached message/rfc822 messages */
-        $url = set_url_var($PHP_SELF, 'passed_ent_id',0);
-        $s .= '<a href="'.$url.'">'._("View Message").'</a>';
-        $par_ent_id = $message->parent->entity_id;
-        if ($par_ent_id) {
-            $par_ent_id = substr($par_ent_id,0,-2);
-            $s .= $topbar_delimiter;
-            $url = set_url_var($PHP_SELF, 'passed_ent_id',$par_ent_id);
-            $s .= '<a href="'.$url.'">'._("Up").'</a>';
+
+        // Only bother with Delete & Prev and Delete & Next IF
+        // top display is enabled.
+        if ( $delete_prev_next_display == 1 &&
+               in_array('\\deleted', $aMailbox['PERMANENTFLAGS'],true) ) {
+            if ($prev >= 0) {
+                $del_prev_href = $base_uri . 'src/read_body.php?passed_id='.$prev.
+                       '&amp;mailbox='.$urlMailbox.'&amp;sort='.$sort.
+                       '&amp;startMessage='.$startMessage.'&amp;show_more=0'.
+                       "&amp;where=$where&amp;what=$what" .
+                       '&amp;delete_id='.$passed_id .
+                       '&amp;smtoken='.sm_generate_security_token();
+            }
+
+            if ($next >= 0) {
+                $del_next_href = $base_uri . 'src/read_body.php?passed_id='.$next.
+                       '&amp;mailbox='.$urlMailbox.'&amp;sort='.$sort.
+                       '&amp;startMessage='.$startMessage.'&amp;show_more=0'.
+                       "&amp;where=$where&amp;what=$what" .
+                       '&amp;delete_id='.$passed_id .
+                       '&amp;smtoken='.sm_generate_security_token();
+            }
         }
     }
 
-    $s .= '</small></td>' . "\n" . '<td align="right" width="33%" nowrap><small>';
-    $comp_action_uri = $comp_uri . '&amp;action=forward';
-    $s .= $link_open . $comp_action_uri . $link_close . _("Forward") . '</a>';
-    $s .= $topbar_delimiter;
+    $msg_list_href = get_message_list_uri($aMailbox['NAME'], $startMessage, $what);
+    if ($where == 'search.php')
+        $search_href = str_replace('read_body.php', 'search.php', $msg_list_href);
+    else
+        $search_href = '';
+
+    $comp_uri = $base_uri.'src/compose.php' .
+                '?passed_id=' . $passed_id .
+                '&amp;mailbox=' . $urlMailbox .
+                '&amp;startMessage=' . $startMessage .
+                 (isset($passed_ent_id) ? '&amp;passed_ent_id='.$passed_ent_id : '');
+
+    // Start form for reply/reply all/forward..
+    $target = '';
+    $on_click='';
+    $method='post';
+    $onsubmit='';
+    if ($compose_new_win == '1') {
+        if (!preg_match("/^[0-9]{3,4}$/", $compose_width)) {
+            $compose_width = '640';
+        }
+        if (!preg_match("/^[0-9]{3,4}$/", $compose_height)) {
+            $compose_height = '550';
+        }
+        if ( checkForJavascript() ) {
+          $on_click='comp_in_new_form(\''.$comp_uri.'\', this, this.form,'. $compose_width .',' . $compose_height .')';
+          $comp_uri = 'javascript:void(0)';
+          $method='get';
+          $onsubmit = 'return false';
+        } else {
+          $target = '_blank';
+        }
+    }
 
-    if ($enable_forward_as_attachment) {
-        $comp_action_uri = $comp_uri . '&amp;action=forward_as_attachment';
-        $s .= $link_open . $comp_action_uri . $link_close . _("Forward as Attachment") . '</a>';
-        $s .= $topbar_delimiter;
+    $oTemplate->assign('nav_on_top', $nav_on_top);
+
+    $oTemplate->assign('prev_href', $prev_href);
+    $oTemplate->assign('up_href', $up_href);
+    $oTemplate->assign('next_href', $next_href);
+    $oTemplate->assign('del_prev_href', $del_prev_href);
+    $oTemplate->assign('del_next_href', $del_next_href);
+    $oTemplate->assign('view_msg_href', $view_msg_href);
+
+    $oTemplate->assign('message_list_href', $msg_list_href);
+    $oTemplate->assign('search_href', $search_href);
+
+    $oTemplate->assign('form_extra', '');
+    $oTemplate->assign('form_method', $method);
+    $oTemplate->assign('form_target', $target);
+    $oTemplate->assign('form_onsubmit', $onsubmit);
+    $oTemplate->assign('compose_href', $comp_uri);
+    $oTemplate->assign('button_onclick', $on_click);
+    $oTemplate->assign('forward_as_attachment_enabled', $enable_forward_as_attachment==1);
+
+    //FIXME: I am surprised these aren't already given to the template; probably needs to be given at a higher level, so I have NO IDEA if this is the right place to do this...  adding them so template can construct its own API calls... we can build those herein too if preferrable
+    $oTemplate->assign('mailbox', $aMailbox['NAME']);
+    $oTemplate->assign('passed_id', $passed_id);
+    $oTemplate->assign('what', $what);
+
+    // If Draft folder - create Resume link
+    $resume_draft = $edit_as_new = false;
+    if (isDraftMailbox($mailbox) && ($save_as_draft)) {
+        $resume_draft = true;
+    } else if (handleAsSent($mailbox)) {
+        $edit_as_new = true;
+    }
+    $oTemplate->assign('can_resume_draft', $resume_draft);
+    $oTemplate->assign('can_edit_as_new', $edit_as_new);
+
+    $oTemplate->assign('mailboxes', sqimap_mailbox_option_array($imapConnection));
+    if (in_array('\\deleted', $aMailbox['PERMANENTFLAGS'],true)) {
+        $delete_url = $base_uri . "src/$where";
+        $oTemplate->assign('can_be_deleted', true);
+        $oTemplate->assign('move_delete_form_action', $base_uri.'src/'.$where);
+        $oTemplate->assign('delete_form_extra', addHidden('mailbox', $aMailbox['NAME'])."\n" .
+                                                addHidden('msg[0]', $passed_id)."\n" .
+                                                addHidden('startMessage', $startMessage)."\n" );
+        if (!(isset($passed_ent_id) && $passed_ent_id)) {
+            $oTemplate->assign('can_be_moved', true);
+            $oTemplate->assign('move_form_extra', addHidden('mailbox', $aMailbox['NAME'])."\n" .
+                                                  addHidden('msg[0]', $passed_id)."\n" );
+            $oTemplate->assign('last_move_target', isset($lastTargetMailbox) && !empty($lastTargetMailbox) ? $lastTargetMailbox : '');
+            $oTemplate->assign('can_be_copied', $show_copy_buttons==1);
+        } else {
+            $oTemplate->assign('can_be_moved', false);
+            $oTemplate->assign('move_form_extra', '');
+            $oTemplate->assign('last_move_target', '');
+            $oTemplate->assign('can_be_copied', false);
+        }
+    } else {
+        $oTemplate->assign('can_be_deleted', false);
+        $oTemplate->assign('move_delete_form_action', '');
+        $oTemplate->assign('delete_form_extra', '');
+        $oTemplate->assign('can_be_moved', false);
+        $oTemplate->assign('move_form_extra', '');
+        $oTemplate->assign('last_move_target', '');
+        $oTemplate->assign('can_be_copied', false);
     }
 
-    $comp_action_uri = decodeHeader($comp_uri . '&amp;action=reply');
-    $s .= $link_open . $comp_action_uri . $link_close . _("Reply") . '</a>';
-    $s .= $topbar_delimiter;
+    // access keys... only add to the top menubar, because adding
+    // them twice makes them less functional (press access key, *then*
+    // press <enter> to make it work)
+    //
+    if ($nav_on_top) {
+        global $accesskey_read_msg_reply, $accesskey_read_msg_reply_all,
+               $accesskey_read_msg_forward, $accesskey_read_msg_as_attach,
+               $accesskey_read_msg_delete, $accesskey_read_msg_bypass_trash,
+               $accesskey_read_msg_move, $accesskey_read_msg_move_to;
+    } else {
+        $accesskey_read_msg_reply = $accesskey_read_msg_reply_all =
+        $accesskey_read_msg_forward = $accesskey_read_msg_as_attach =
+        $accesskey_read_msg_delete = $accesskey_read_msg_bypass_trash =
+        $accesskey_read_msg_move = $accesskey_read_msg_move_to = 'NONE';
+    }
+    $oTemplate->assign('accesskey_read_msg_reply', $accesskey_read_msg_reply);
+    $oTemplate->assign('accesskey_read_msg_reply_all', $accesskey_read_msg_reply_all);
+    $oTemplate->assign('accesskey_read_msg_forward', $accesskey_read_msg_forward);
+    $oTemplate->assign('accesskey_read_msg_as_attach', $accesskey_read_msg_as_attach);
+    $oTemplate->assign('accesskey_read_msg_delete', $accesskey_read_msg_delete);
+    $oTemplate->assign('accesskey_read_msg_bypass_trash', $accesskey_read_msg_bypass_trash);
+    $oTemplate->assign('accesskey_read_msg_move_to', $accesskey_read_msg_move_to);
+    $oTemplate->assign('accesskey_read_msg_move', $accesskey_read_msg_move);
+
+    global $null;
+    do_hook('read_body_menu', $null);
+
+    if ($nav_on_top) {
+        $oTemplate->display('read_menubar_nav.tpl');
+        $oTemplate->display('read_menubar_buttons.tpl');
+    } else {
+        $oTemplate->display('read_menubar_buttons.tpl');
+        $oTemplate->display('read_menubar_nav.tpl');
+    }
 
-    $comp_action_uri = $comp_uri . '&amp;action=reply_all';
-    $s .= $link_open . $comp_action_uri . $link_close . _("Reply All") . '</a>';
-    $s .= '</small></td></tr></table>';
-    do_hook("read_body_menu_top");
-    echo $s;
-    do_hook("read_body_menu_bottom");
 }
 
 function formatToolbar($mailbox, $passed_id, $passed_ent_id, $message, $color) {
-    global $base_uri;
+    global $base_uri, $where, $what, $show_html_default,
+           $oTemplate, $download_href, $PHP_SELF,
+           $unsafe_image_toggle_href, $unsafe_image_toggle_text;
 
     $urlMailbox = urlencode($mailbox);
-    $url = $base_uri.'src/view_header.php?'.$_SERVER['QUERY_STRING'];
+    $urlPassed_id = urlencode($passed_id);
+    $urlPassed_ent_id = urlencode($passed_ent_id);
+
+    $query_string = 'mailbox=' . $urlMailbox . '&amp;passed_id=' . $urlPassed_id . '&amp;passed_ent_id=' . $urlPassed_ent_id;
+    if (!empty($where)) {
+        $query_string .= '&amp;where=' . urlencode($where);
+    }
+    if (!empty($what)) {
+        $query_string .= '&amp;what=' . urlencode($what);
+    }
+    $url = $base_uri.'src/view_header.php?'.$query_string;
+
+    $links = array();
+    $links[] = array (
+                        'URL'   => $url,
+                        'Text'  => _("View Full Header")
+                     );
+
+    if ( checkForJavaScript() ) { 
+        $links[] = array (
+                        'URL'   => 'javascript:printThis();',
+                        'Text'  => _("Print"),
+                     );
+    } else {
+        $links[] = array (
+                        'URL'   => set_url_var($PHP_SELF, 'print', '1'),
+                        'Text'  => _("Print"),
+                        'Target' => '_blank'
+                     );
+    }
 
-    $s  = "<TR>\n" .
-          '<TD VALIGN="MIDDLE" ALIGN="RIGHT" WIDTH="20%"><B>' . _("Options") . ":&nbsp;&nbsp;</B></TD>\n" .
-          '<TD VALIGN="MIDDLE" ALIGN="LEFT" WIDTH="80%"><SMALL>' .
-          '<a href="'.$url.'">'._("View Full Header").'</a>';
+    $links[] = array (
+                        'URL'   => $download_href,
+                        'Text'  => _("Download this as a file")
+                     );
+    $toggle = html_toggle_href($mailbox, $passed_id, $passed_ent_id, $message);
+    if (!empty($toggle)) {
+        $links[] = array (
+                            'URL'   => $toggle,
+                            'Text'  => $show_html_default==1 ? _("View as plain text") : _("View as HTML")
+                         );
+    }
+    if (!empty($unsafe_image_toggle_href)) {
+        $links[] = array (
+                            'URL'   => $unsafe_image_toggle_href,
+                            'Text'  => $unsafe_image_toggle_text
+                         );
+    }
+
+    do_hook('read_body_header_right', $links);
 
-    /* Output the printer friendly link if we are in subtle mode. */
-    $s .= '&nbsp;|&nbsp;' .
-          printer_friendly_link($mailbox, $passed_id, $passed_ent_id, $color);
-    echo $s;
-    do_hook("read_body_header_right");
-    $s = "</SMALL></TD>\n" .
-         "</TR>\n";
-    echo $s;
+    $oTemplate->assign('links', $links);
 
+    return $oTemplate->fetch('read_toolbar.tpl');
 }
 
 /***************************/
@@ -610,137 +773,205 @@ function formatToolbar($mailbox, $passed_id, $passed_ent_id, $message, $color) {
 
 /* get the globals we may need */
 
-$username = $_SESSION['username'];
-$key = $_COOKIE['key'];
-$onetimepad = $_SESSION['onetimepad'];
-$msgs = $_SESSION['msgs'];
-$base_uri = $_SESSION['base_uri'];
-$delimiter = $_SESSION['delimiter'];
-
-if (isset($_GET['passed_id'])) {
-    $passed_id = $_GET['passed_id'];
-}
-elseif (isset($_POST['passed_id'])) {
-    $passed_id = $_POST['passed_id'];
-}
-
-if (isset($_GET['passed_ent_id'])) {
-    $passed_ent_id = $_GET['passed_ent_id'];
-}
-elseif (isset($_POST['passed_ent_id'])) {
-    $passed_ent_id = $_POST['passed_ent_id'];
-}
-
-if (isset($_GET['sendreceipt'])) {
-    $sendreceipt = $_GET['sendreceipt'];
-}
-
-if (isset($_GET['sort'])) {
-    $sort = $_GET['sort'];
-}
-elseif (isset($_POST['sort'])) {
-    $sort = $_POST['sort'];
-}
-if (isset($_GET['startMessage'])) {
-    $startMessage = $_GET['startMessage'];
-}
-elseif (isset($_POST['startMessage'])) {
-    $startMessage = $_POST['startMessage'];
-}
-if (isset($_GET['show_more'])) {
-    $show_more = $_GET['show_more'];
+sqgetGlobalVar('delimiter', $delimiter,     SQ_SESSION);
+sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION);
+if (!sqgetGlobalVar('messages', $messages, SQ_SESSION) ) {
+    $messages = array();
 }
-if (isset($_GET['show_more_cc'])) {
-    $show_more_cc = $_GET['show_more_cc'];
+sqgetGlobalVar('delayed_errors',  $delayed_errors,  SQ_SESSION);
+if (is_array($delayed_errors)) {
+    $oErrorHandler->AssignDelayedErrors($delayed_errors);
+    sqsession_unregister("delayed_errors");
 }
-if (isset($_GET['show_more_bcc'])) {
-    $show_more_bcc = $_GET['show_more_bcc'];
+/** GET VARS */
+sqgetGlobalVar('sendreceipt',   $sendreceipt,   SQ_GET);
+if (!sqgetGlobalVar('where',         $where,         SQ_GET) ) {
+    $where = 'right_main.php';
 }
-if (isset($_GET['mailbox'])) {
-    $mailbox = $_GET['mailbox'];
+/*
+ * Used as entry key to the list of uid's cached in the mailbox cache
+ * we use the cached uid's to get the next and prev  message.
+ */
+if (!sqgetGlobalVar('what',          $what,          SQ_GET) ){
+    $what = 0;
 }
-elseif (isset($_POST['mailbox'])) {
-    $mailbox = $_POST['mailbox'];
+if ( sqgetGlobalVar('show_more', $temp,  SQ_GET) ) {
+    $show_more = (int) $temp;
 }
-if (isset($_GET['where'])) {
-    $where = $_GET['where'];
+if ( sqgetGlobalVar('show_more_cc', $temp,  SQ_GET) ) {
+    $show_more_cc = (int) $temp;
 }
-if (isset($_GET['what'])) {
-    $what = $_GET['what'];
+if ( sqgetGlobalVar('show_more_bcc', $temp,  SQ_GET) ) {
+    $show_more_bcc = (int) $temp;
 }
-if (isset($_GET['view_hdr'])) {
-    $view_hdr = $_GET['view_hdr'];
+if ( sqgetGlobalVar('view_hdr', $temp,  SQ_GET) ) {
+    $view_hdr = (int) $temp;
 }
-if (isset($_SESSION['server_sort_array'])) {
-    $server_sort_array = $_SESSION['server_sort_array'];
+
+if ( sqgetGlobalVar('account', $temp,  SQ_GET) ) {
+    $iAccount = (int) $temp;
+} else {
+    $iAccount = 0;
 }
-if (isset($_SESSION['msgs'])) {
-    $msgs = $_SESSION['msgs'];
+
+/** GET/POST VARS */
+sqgetGlobalVar('passed_id',     $passed_id, SQ_INORDER, NULL, SQ_TYPE_BIGINT);
+sqgetGlobalVar('passed_ent_id', $passed_ent_id);
+sqgetGlobalVar('mailbox',       $mailbox);
+
+if ( sqgetGlobalVar('sort', $temp) ) {
+    $sort = (int) $temp;
 }
-if (isset($_SESSION['msort'])) {
-    $msort = $_SESSION['msort'];
+if ( sqgetGlobalVar('startMessage', $temp) ) {
+    $startMessage = (int) $temp;
+} else {
+    $startMessage = 1;
 }
-if (isset($_POST['move_id'])) {
-    $move_id = $_POST['move_id'];
+if(sqgetGlobalVar('show_html_default', $temp)) {
+    $show_html_default = (int) $temp;
 }
-if (isset($_SESSION['lastTargetMailbox'])) {
-    $lastTargetMailbox = $_SESSION['lastTargetMailbox'];
+
+if(sqgetGlobalVar('view_unsafe_images', $temp)) {
+    $view_unsafe_images = (int) $temp;
+    if($view_unsafe_images == 1) {
+        $show_html_default = 1;
+    }
+} else {
+    $view_unsafe_images = 0;
 }
-if (isset($_SESSION['messages'])) {
-    $messages = $_SESSION['messages'];
+
+/**
+ * Retrieve mailbox cache
+ */
+sqgetGlobalVar('mailbox_cache',$mailbox_cache,SQ_SESSION);
+
+/* end of get globals */
+
+$imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
+$aMailbox = sqm_api_mailbox_select($imapConnection, $iAccount, $mailbox,array('setindex' => $what, 'offset' => $startMessage),array());
+
+
+/**
+ Start code to set the columns to fetch in case of hitting the next/prev link
+ The reason for this is the fact that the cache can be invalidated which means that the headers
+ to fetch aren't there anymore. Before they got calculated when the messagelist was shown.
+
+ Todo, better central handling of setting the mailbox options so we do not need to do the stuff below
+*/
+
+/**
+ * Replace From => To  in case it concerns a draft or sent folder
+ */
+$aColumns = array();
+if (($mailbox == $sent_folder || $mailbox == $draft_folder) &&
+    !in_array(SQM_COL_TO,$index_order)) {
+    $aNewOrder = array(); // nice var name ;)
+    foreach($index_order as $iCol) {
+        if ($iCol == SQM_COL_FROM) {
+            $iCol = SQM_COL_TO;
+        }
+        $aColumns[$iCol] = array();
+   }
 } else {
-    $messages = array();
+   foreach ($index_order as $iCol) {
+       $aColumns[$iCol] = array();
+   }
 }
 
+$aProps = array(
+    'columns' => $aColumns, // columns bound settings
+    'config'  => array(
+                        'highlight_list'        => $message_highlight_list, // row highlighting rules
+                        'trash_folder'          => $trash_folder,
+                        'sent_folder'           => $sent_folder,
+                        'draft_folder'          => $draft_folder));
 
+calcFetchColumns($aMailbox,$aProps);
+
+/**
+ End code to set the columns to fetch in case of hitting the next/prev link
+*/
 
-/* end of get globals */
-global $uid_support, $sqimap_capabilities;
 
-if (isset($mailbox)) {
-    $mailbox = urldecode( $mailbox );
+
+/**
+ * Check if cache is still valid, $what contains the key
+ * which gives us acces to the array with uid's. At this moment
+ * 0 is used for a normal message list and search uses 1 as key. This can be
+ * changed / extended in the future.
+ * If on a select of a mailbox we detect that the cache should be invalidated due to
+ * the delete of messages or due to new messages we empty the list with uid's and
+ * that's what we detect below.
+ */
+if (!is_array($aMailbox['UIDSET'][$what])) {
+    fetchMessageHeaders($imapConnection, $aMailbox);
 }
 
-$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
-$mbx_response = sqimap_mailbox_select($imapConnection, $mailbox, false, false, true);
+$iSetIndex = $aMailbox['SETINDEX'];
+$aMailbox['CURRENT_MSG'][$iSetIndex] = $passed_id;
 
+/**
+ * Update the seen state
+ * and ignore in_array('\\seen',$aMailbox['PERMANENTFLAGS'],true)
+ */
+if (isset($aMailbox['MSG_HEADERS'][$passed_id]['FLAGS'])) {
+    $aMailbox['MSG_HEADERS'][$passed_id]['FLAGS']['\\seen'] = true;
+}
+
+/**
+ * Process Delete from delete-move-next
+ * but only if delete_id was set
+ */
+if ( sqgetGlobalVar('delete_id', $delete_id, SQ_GET) ) {
+    handleMessageListForm($imapConnection,$aMailbox,$sButton='setDeleted', array($delete_id));
+}
 
 /**
  * $message contains all information about the message
  * including header and body
  */
 
-$uidvalidity = $mbx_response['UIDVALIDITY'];
-if (!isset($messages[$uidvalidity])) {
-   $messages[$uidvalidity] = array();
-}  
-if (!isset($messages[$uidvalidity][$passed_id]) || !$uid_support) {
-   $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
-   $FirstTimeSee = !$message->is_seen;
-   $message->is_seen = true;
-   $messages[$uidvalidity][$passed_id] = $message;
+if (isset($aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'])) {
+    $message = $aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'];
+    $FirstTimeSee = !$message->is_seen;
 } else {
-//   $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
-   $message = $messages[$uidvalidity][$passed_id];
-   $FirstTimeSee = !$message->is_seen;
+    $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
+    $FirstTimeSee = !$message->is_seen;
 }
 
+/**
+ * update message seen status and put in cache
+ */
+$message->is_seen = true;
+$aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'] = $message;
+
 if (isset($passed_ent_id) && $passed_ent_id) {
-   $message = $message->getEntity($passed_ent_id);
-   if ($message->type0 != 'message'  && $message->type1 != 'rfc822') {
-      $message = $message->parent;
-   }
-   $read = sqimap_run_command ($imapConnection, "FETCH $passed_id BODY[$passed_ent_id.HEADER]", true, $response, $msg, $uid_support);
-   $rfc822_header = new Rfc822Header();
-   $rfc822_header->parseHeader($read);
-   $message->rfc822_header = $rfc822_header;
+    $message = $message->getEntity($passed_ent_id);
+    if ($message->type0 != 'message'  && $message->type1 != 'rfc822') {
+        $message = $message->parent;
+    }
+    $read = sqimap_run_command ($imapConnection, "FETCH $passed_id BODY[$passed_ent_id.HEADER]", true, $response, $msg, TRUE);
+    $rfc822_header = new Rfc822Header();
+    $rfc822_header->parseHeader($read);
+    $message->rfc822_header = $rfc822_header;
+} else if ($message->type0 == 'message'  && $message->type1 == 'rfc822' && isset($message->entities[0])) {
+    $read = sqimap_run_command ($imapConnection, "FETCH $passed_id BODY[1.HEADER]", true, $response, $msg, TRUE);
+    $rfc822_header = new Rfc822Header();
+    $rfc822_header->parseHeader($read);
+    $message->rfc822_header = $rfc822_header;
 } else {
-   $passed_ent_id = 0;
+    $passed_ent_id = 0;
 }
 $header = $message->header;
 
-do_hook('html_top');
+// gmail does not mark messages as read when retrieving the message body
+// even though RFC 3501, section 6.4.5 (FETCH Command) says:
+// "The \Seen flag is implicitly set; if this causes the flags to change,
+// they SHOULD be included as part of the FETCH responses."
+//
+if ($imap_server_type == 'gmail') {
+    sqimap_toggle_flag($imapConnection, $passed_id, '\\Seen', true, true);
+}
 
 /****************************************/
 /* Block for handling incoming url vars */
@@ -748,33 +979,22 @@ do_hook('html_top');
 
 if (isset($sendreceipt)) {
    if ( !$message->is_mdnsent ) {
-      if (isset($identity) ) {
-         $final_recipient = getPref($data_dir, $username, 'email_address' . '0', '' );
-      } else {
-         $final_recipient = getPref($data_dir, $username, 'email_address', '' );
-      }
-
-      $final_recipient = trim($final_recipient);
-      if ($final_recipient == '' ) {
-         $final_recipient = getPref($data_dir, $username, 'email_address', '' );
-      }
-      $supportMDN = ServerMDNSupport($mbx_response["PERMANENTFLAGS"]);
-      if ( SendMDN( $mailbox, $passed_id, $final_recipient, $message, $imapConnection ) > 0 && $supportMDN ) {
-         ToggleMDNflag( true, $imapConnection, $mailbox, $passed_id, $uid_support);
+      $supportMDN = ServerMDNSupport($aMailbox["PERMANENTFLAGS"]);
+      if ( SendMDN( $mailbox, $passed_id, $message, $imapConnection ) > 0 && $supportMDN ) {
+         ToggleMDNflag( true, $imapConnection, $mailbox, $passed_id);
          $message->is_mdnsent = true;
-         $messages[$uidvalidity][$passed_id]=$message;
+         $aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'] = $message;
       }
-      ClearAttachments();
    }
 }
 /***********************************************/
 /* End of block for handling incoming url vars */
 /***********************************************/
 
-$msgs[$passed_id]['FLAG_SEEN'] = true;
-$messagebody = ''; 
-do_hook('read_body_top');
+$oTemplate->assign('aAttribs', array('class' => 'entity_sep'));
+$hr = $oTemplate->fetch('horizontal_rule.tpl');
+$messagebody = '';
+do_hook('read_body_top', $null);
 if ($show_html_default == 1) {
     $ent_ar = $message->findDisplayEntity(array());
 } else {
@@ -784,50 +1004,38 @@ $cnt = count($ent_ar);
 for ($i = 0; $i < $cnt; $i++) {
    $messagebody .= formatBody($imapConnection, $message, $color, $wrap_at, $ent_ar[$i], $passed_id, $mailbox);
    if ($i != $cnt-1) {
-       $messagebody .= '<hr noshade size=1>';
+       $messagebody .= $hr;
    }
 }
 
-displayPageHeader($color, $mailbox);
-formatMenuBar($mailbox, $passed_id, $passed_ent_id, $message, $mbx_response);
-formatEnvheader($mailbox, $passed_id, $passed_ent_id, $message, $color, $FirstTimeSee);
-echo '<table width="100%" cellpadding="0" cellspacing="0" align="center" border="0">';
-echo '  <tr><td>';
-echo '    <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[9].'">';
-echo '      <tr><td>';
-echo '        <table width="100%" cellpadding="3" cellspacing="0" align="center" border="0">';
-echo '          <tr bgcolor="'.$color[4].'"><td>';
-echo '            <table cellpadding="5" cellspacing="0"  border="0">';
-echo '              <tr>'. html_tag( 'td', '<br>'. $messagebody."\n", 'left').'</td>';
-echo '            </table>';
-echo '          </td></tr>';      
-echo '        </table></td></tr>';
-echo '    </table>';
-echo '  </td></tr>';
-
-$attachmentsdisplay = formatAttachments($message,$ent_ar,$mailbox, $passed_id);
-if ($attachmentsdisplay) {
-   echo '  <tr><td>';
-   echo '    <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
-   echo '     <tr><td>';
-   echo '       <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[4].'">';
-   echo '        <tr><td ALIGN="left" bgcolor="'.$color[9].'">';
-   echo '           <b>' . _("Attachments") . ':</b>';
-   echo '        </td></tr>';
-   echo '        <tr><td>';
-   echo '          <table width="100%" cellpadding="2" cellspacing="2" align="center"'.' border="0" bgcolor="'.$color[0].'"><tr><td>';
-   echo              $attachmentsdisplay;
-   echo '          </td></tr></table>';
-   echo '        </table></td></tr>';
-   echo '       </table></td></tr>';
-   echo '    </table>';
-   echo '  </td></tr>';
-}
-echo '</table>';
+/**
+ * Write mailbox with updated seen flag information back to cache.
+ */
+$mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox;
+sqsession_register($mailbox_cache,'mailbox_cache');
+$_SESSION['mailbox_cache'] = $mailbox_cache;
+
+// message list URI is used in page header when on read_body
+$oTemplate->assign('message_list_href', get_message_list_uri($aMailbox['NAME'], $startMessage, $what));
+
+displayPageHeader($color, $mailbox,'','');
+
+/* this is the non-javascript version of printer friendly */
+if ( sqgetGlobalVar('print', $print, SQ_GET) ) {
+    $oTemplate->display('read_message_print.tpl');
+} else {
+    formatMenubar($aMailbox, $passed_id, $passed_ent_id, $message,false);
+}
+formatEnvheader($aMailbox, $passed_id, $passed_ent_id, $message, $color, $FirstTimeSee);
+
+$oTemplate->assign('message_body', $messagebody);
+$oTemplate->display('read_message_body.tpl');
+
+formatAttachments($message,$ent_ar,$mailbox, $passed_id);
 
 /* show attached images inline -- if pref'fed so */
-if (($attachment_common_show_images) &&
-    is_array($attachment_common_show_images_list)) {
+if ($attachment_common_show_images && is_array($attachment_common_show_images_list)) {
+    $images = array();
     foreach ($attachment_common_show_images_list as $img) {
         $imgurl = SM_PATH . 'src/download.php' .
                 '?' .
@@ -835,23 +1043,19 @@ if (($attachment_common_show_images) &&
                 '&amp;mailbox='       . urlencode($mailbox) .
                 '&amp;ent_id=' . urlencode($img['ent_id']) .
                 '&amp;absolute_dl=true';
-
-        echo html_tag( 'table', "\n" .
-                    html_tag( 'tr', "\n" .
-                        html_tag( 'td', '<img src="' . $imgurl . '">' ."\n", 'left'
-                        )
-                    ) ,
-        'center', '', 'cellspacing=0 border="0" cellpadding="2"');
+        $a = array();
+        $a['Name'] = $img['name'];
+        $a['DisplayURL'] = $imgurl;
+        $a['DownloadURL'] = $img['download_href'];
+        $images[] = $a;
     }
+
+    $oTemplate->assign('images', $images);
+    $oTemplate->display('read_display_images_inline.tpl');
 }
 
-do_hook('read_body_bottom');
-do_hook('html_bottom');
-sqimap_logout($imapConnection);
-/* sessions are written at the end of the script. it's better to register 
-   them at the end so we avoid double session_register calls */
-sqsession_register($messages,'messages');
+formatMenubar($aMailbox, $passed_id, $passed_ent_id, $message, false, FALSE);
 
-?>
-</body>
-</html>
+do_hook('read_body_bottom', $null);
+sqimap_logout($imapConnection);
+$oTemplate->display('footer.tpl');