<?php   
    include("global.inc");

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

    $msg = imap_header($mailbox, $msgnum);
    $struct = imap_fetchstructure($mailbox, $msgnum);

    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) && is_string(SENT_MAIL));
    body_start();
    $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();

    $flag = 0;
    for($z = 0; $z < count($struct->parts); $z++)
    {
        $part = !$struct->parts[$z] ? $part = $struct : $part = $struct->parts[$z];

        $att_name = get_att_name($part);
        if ($att_name != "Unknown")
        {
            // if it has a name, it's an attachment
            $f_name[$flag] = attach_display($part, $z+1);
            $flag++;
        }
    }
    if ($flag != 0)
    {
        title_start();
        title_left();
        echo L_FILES_HDR . ":";
        title_left_end();
        title_data();
        echo implode(", ", $f_name);
        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");
        echo "<a href=\"$url\">" . L_HEADERS_SHOW . "</a>";
    }
    else
    {
        $url = self_with_param("headers", "");
        echo "<a href=\"$url\">" . L_HEADERS_HIDE . "</a>";
    }
    title_left_end();
    title_data();
    if ($headers=="show")
    {
        echo "<pre>\n";
        echo htmlspecialchars(imap_fetchheader($mailbox, $msgnum, 0));
        echo "</pre>";
    }
    else
    {
        echo "&nbsp;";
    }
    title_data_end();
    title_end();

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

function display_mime_part($struct, $numprefix="")
{
    global $folder, $msgnum;
    $numparts = !$struct->parts ? "1" : count($struct->parts);
    echo "<!-- This message has " . $numparts . " part(s) -->\n";

     /* If multipart/alternative, display the best one. This is
        the last one we can handle.
        This is not entirely correct, as we will display later ones even
        if we don't know how to handle it.
    */
    $alternative = 0;
    if (get_mime_type($struct) == "multipart" 
        && strtoupper($struct->subtype) == "ALTERNATIVE")
    {
        $alternative = 1;
    }
    for($i = 0; $i < $numparts; $i++)
    {
        /* If multipart/alternative, last one first */
        if ($alternative)
            $nr = $numparts - $i - 1;
        else
            $nr = $i;
        $part = !$struct->parts[$nr] ?$part = $struct : $part = $struct->parts[$nr];

        if ($part->parts)
        {
            $subpartnum = $nr + 1;
            display_mime_part($part, $numprefix . $subpartnum . ".");
        }
        else
        {

            $att_name = get_att_name($part);
            if ($att_name == "Unknown")
            {
                $subpartnum = $nr + 1;
                $nicelydisplayed = inline_display($part, $numprefix . $subpartnum);
                echo "\n<br>";
            }
            /* If this picture is used as in in-line picture, there is no
               use displaying it again. */
            
            if (!ciddone($part))
            {
                // we want to display images here, even though they are
                // attachments.
                $mime_encoding = get_mime_encoding($part);
                $mime_type = get_mime_type($part);
                if(($mime_encoding == "base64") and ($mime_type == "image"))
                {
                    $subpartnum = $nr + 1;
                    echo "<p>" . image_display($folder, $msgnum, $part, $numprefix . $subpartnum, $att_name) . "</p>\n";
                }
            }
        }
        /* If multipart/alternative, as soon as it is displayed we're done */
        if ($alternative && $nicelydisplayed)
            break;
    }
}
    display_mime_part($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 ?>">
        <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();
    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 ?>">
    <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 ?>">
    <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 ?>">
    <input type="submit" value="<?php echo L_FORWARD_BTN ?>">
    </form>
<?php
    header_left_right_end();
    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 ?>">
    <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);
 


?>
