<?php   

include("global.inc.php");
include("message.inc.php");

function cmp_msgudate($a, $b)
{
    global $msgs;
    if ($msgs[$a]->udate == $msgs[$b]->udate)
    {
        return 0;
    }
    return ($msgs[$a]->udate > $msgs[$b]->udate) ? 1 : -1;
}


function content()
{    
    global $mailbox;
    global $msgnum, $folder;
    global $msglist;

    content_start();
    header_start();
    header_left();
    global $user;
    echo "$folder - $user - " . PROG_NAME;
    header_left_right_end();
    header_right();
?>
    <form action="index.php" method="get">
    <input type="hidden" name="folder" value="<?php echo $folder ?>">
    <input type="submit" value="<?php echo L_GO_BACK_BTN ?>">
    </form>
<?php
    header_left_right_end();
    header_end();
    $issentmail = (($folder == SENT_MAIL) && (SENT_MAIL));

    if (!MULTIMESSAGE)
    {
        $msglist = array_slice($msglist, 0, 1);
    }
    // Sort on udate
    global $msgs;
    $msgs = array();
    foreach ($msglist as $msgnum)
    {
        $msgs[$msgnum] = imap_headerinfo($mailbox, imap_msgno($mailbox, $msgnum));
    }
    usort($msglist, "cmp_msgudate");

    foreach ($msglist as $msgnum)
    {
        body_start();
        print "<a name=\"$msgnum\"></a>\n";
        #$msg = imap_headerinfo($mailbox, imap_msgno($mailbox, $msgnum));
        $msg = $msgs[$msgnum];
        $struct = imap_fetchstructure($mailbox, $msgnum, FT_UID);

        $subject = !$msg->Subject ? L_NO_SUBJECT : $msg->Subject;
        $subject = htmlspecialchars(decode_header_string($subject));
        if(!$issentmail)
        {
            title_start();
            title_left();
            echo L_FROM_HDR . ":";
            title_left_end();
            title_data();
            if($msg->from)
            {
                echo make_clickable_list($msg->from, $folder);
            }   
            else
            {
                echo L_NO_FROM;
            }

            title_data_end();
            title_end();
        }
        title_start();
        title_left();
        echo L_TO_HDR . ":";
        title_left_end();
        title_data();
        if ($msg->to)
        {
            echo make_clickable_list($msg->to, $folder);
        }
        else
        {
            echo L_NO_TO;
        }
        title_data_end();
        title_end();
        if($msg->cc)
        {
            title_start();
            title_left();
            echo L_CC_HDR . ":";
            title_left_end();
            title_data();
            echo make_clickable_list($msg->cc, $folder);
            title_data_end();
            title_end();
        }
        title_start();
        title_left();
        echo L_DATE_HDR . ":";
        title_left_end();
        title_data();
        echo date(L_DATE_FORMAT, $msg->udate);
        title_data_end();

        get_attach_display_list($att_list, $msgnum, $struct);

        if (count($att_list) > 0)
        {
            title_start();
            title_left();
            echo L_FILES_HDR . ":";
            title_left_end();
            title_data();
            echo implode(", ", $att_list);
            title_data_end();
            title_end();
        }

        title_start();
        title_left();
        echo L_SUBJECT_HDR . ":";
        title_left_end();
        title_data();
        echo $subject;
        title_data_end();
        title_end();

        title_start();
        title_left();
        global $headers;
        if ($headers!="show")
        {
            $url = self_with_param("headers", "show");
            $url .= "#$msgnum";
            echo "<a class=\"intf\" href=\"$url\">" . L_HEADERS_SHOW . "</a>";
        }
        else
        {
            $url = self_with_param("headers", "");
            $url .= "#$msgnum";
            echo "<a class=\"intf\" href=\"$url\">" . L_HEADERS_HIDE . "</a>";
        }
        title_left_end();
        title_data();
        if ($headers=="show")
        {
            echo "<pre>\n";
            echo htmlspecialchars(imap_fetchheader($mailbox, $msgnum, FT_UID));
            echo "</pre>";
        }
        else
        {
            echo "&nbsp;";
        }
        title_data_end();
        title_end();

        body_row();
        body_left('colspan="2" class="body"');

        display_mime_part($msgnum, $struct);

        body_left_end();
        body_row_end();
        body_end();
        header_start();
        header_left();
        
        if (USE_FOLDERS) {
    ?>        
            <form method="get" action="action.php">
            <input type="hidden" name="folder" value="<?php echo $folder ?>">
            <input type="hidden" name="msgnum" value="<?php echo $msgnum ?>">
        <?php
            $msglistcpy = $msglist;
            foreach ($msglistcpy as $m)   
            {
                print "<input type=\"hidden\" name=\"msglist[]\" value=\"$m\">\n";
            }
        ?>
            <select name="tofolder" onChange="submit()">
            <option><?php echo L_FOLDER_MOVE ?>
            <?php list_folders($mailbox); ?>
            </select>
            <script type="text/javascript">
            <!--
                // A var named 'move' must always exist.
                document.write('<input type="hidden" name="move" value="1">');
            //-->
            </script>
            <noscript>
                <input type="submit" name="move" value="<?php echo L_SUBMIT_BTN ?>">
            </noscript>
            </form>
    <?php
        }
        echo "&nbsp;";
        header_left_right_end();
        if (WRITE_MESSAGES)
        {
            header_right();
    ?>
            <form method="get" action="compose.php">
            <input type="hidden" name="action" value="reply">
            <input type="hidden" name="folder" value="<?php echo $folder ?>">
            <input type="hidden" name="msgnum" value="<?php echo $msgnum ?>">
        <?php
            $msglistcpy = $msglist;
            foreach ($msglistcpy as $m)   
            {
                print "<input type=\"hidden\" name=\"msglist[]\" value=\"$m\">\n";
            }
        ?>
            <input type="submit" value="<?php echo L_REPLY_BTN ?>">
            </form>
    <?php
            header_left_right_end();
            header_right();
    ?>
            <form method="get" action="compose.php">
            <input type="hidden" name="action" value="replyall">
            <input type="hidden" name="folder" value="<?php echo $folder ?>">
            <input type="hidden" name="msgnum" value="<?php echo $msgnum ?>">
        <?php
            $msglistcpy = $msglist;
            foreach ($msglistcpy as $m)   
            {
                print "<input type=\"hidden\" name=\"msglist[]\" value=\"$m\">\n";
            }
        ?>
            <input type="submit" value="<?php echo L_REPLYALL_BTN ?>">
            </form>
    <?php
            header_left_right_end();
            header_right();
    ?>
            <form method="get" action="compose.php">
            <input type="hidden" name="action" value="forward">
            <input type="hidden" name="folder" value="<?php echo $folder ?>">
            <input type="hidden" name="msgnum" value="<?php echo $msgnum ?>">
        <?php
            $msglistcpy = $msglist;
            foreach ($msglistcpy as $m)   
            {
                print "<input type=\"hidden\" name=\"msglist[]\" value=\"$m\">\n";
            }
        ?>
            <input type="submit" value="<?php echo L_FORWARD_BTN ?>">
            </form>
    <?php
            header_left_right_end();
        }
        if (DELETE_MESSAGES)
        {
            header_right();
    ?>
            <form method="get" action="action.php">
            <input type="hidden" name="folder" value="<?php echo $folder ?>">
            <input type="hidden" name="delete" value="delete">
            <input type="hidden" name="msgnum" value="<?php echo $msgnum ?>">
        <?php
            $msglistcpy = $msglist;
            foreach ($msglistcpy as $m)   
            {
                print "<input type=\"hidden\" name=\"msglist[]\" value=\"$m\">\n";
            }

        ?>
            <input type="submit" value="<?php echo L_DELETE_BTN ?>">
            </form>
    <?php
            header_left_right_end();
        }
        header_end();
    }
    content_end();
    imap_close($mailbox);  
}


/* MAIN */
include(HTML_TEMPLATE);
 


?>
