<?php

Header("Cache-Control: no-cache");
Header("Pragma: no-cache");
Header("Expires: Sat, Jan 01 2000 01:01:01 GMT");

include("global.inc.php");

if (!WRITE_MESSAGES)
    exit;

function content()
{
    global $mailbox;
    global $msgnum, $msglist, $action, $folder;
    global $to, $mailto, $cc, $subject, $body;

    // These can be passed in
    if ($to)
        $to = htmlspecialchars(stripslashes($to));
    if ($mailto)
        $mailto = htmlspecialchars(stripslashes($mailto));

    if($msgnum)
    {
        $msg = imap_headerinfo($mailbox, imap_msgno($mailbox, $msgnum));
        $struct = imap_fetchstructure($mailbox, $msgnum, FT_UID);

        /* construct headers */
        if($action == "reply")
        {
            if (count($msg->reply_to))
                $to = make_formaddress_list($msg->reply_to);
            else
                $to = make_formaddress_list($msg->from);
            $subject = !$msg->Subject ? L_NO_SUBJECT : decode_header_string($msg->Subject);
            $begin = strtoupper(substr($subject, 0, 3)) != "RE:" ? "Re: " : "";
            $subject = $begin . $subject;
        }
        if($action == "replyall")
        {
            $to = make_formaddress_list($msg->from) . ", " .
                                make_formaddress_list($msg->to);
            $cc = make_formaddress_list($msg->cc);

            $subject = !$msg->Subject ? L_NO_SUBJECT : decode_header_string($msg->Subject);
            $begin = strtoupper(substr($subject, 0, 3)) != "RE:" ? "Re: " : "";
            $subject = $begin . $subject;
        }
        if($action == "forward")
        {
            $subject = !$msg->Subject ? L_NO_SUBJECT : decode_header_string($msg->Subject);
            $begin = strtoupper(substr($subject, 0, 3)) != "FW:" ? "Fw: " : "";
            $subject = $begin . $subject;
        }
        $subject = htmlspecialchars($subject);

        /* construct body */
        $personal = make_formaddress_list($msg->from);
        $de_subject = !$msg->Subject ? L_NO_SUBJECT : $msg->Subject;
        $de_subject = decode_header_string($de_subject);

        $body = L_ORIG_MSG . "\n";
        $body .= L_FROM_HDR . ": " . $personal . "\n";
        $body .= L_DATE_HDR . ": " . date(L_DATE_FORMAT, $msg->udate) . "\n";
        $body .= L_TO_HDR . ": ";

        if ($msg->to)
        {
            $de_to = make_formaddress_list($msg->to);
            $body .= $de_to . "\n";
        }
        else
        {
            $body .= L_NO_TO . "\n";
        }

        if ($msg->cc)
        {
            $de_cc = make_formaddress_list($msg->to);
            $body .= L_CC_HDR . ": " . $de_cc . "\n";
        }
        $body .= L_SUBJECT_HDR . ": $de_subject\n\n";

function add_mime_part($struct, $numprefix="")
{
    global $mailbox, $msgnum;
    $numparts = !$struct->parts ? "1" : count($struct->parts);
    for($i = 0; $i < $numparts; $i++)
    {
        $part = !$struct->parts[$i] ? $part = $struct : $part = $struct->parts[$i];
        if ($part->parts)
            $body .= add_mime_part($part, $numprefix . $i+1 . ".");
        else
        {
            if (get_att_name($part) == "Unknown")
            {
                $subpartnum = $i + 1;
                if (strtoupper($part->subtype) == "PLAIN")
                {
                    $mime_encoding = get_mime_encoding($part);
                    $dsp = imap_fetchbody($mailbox, $msgnum,  $numprefix . $subpartnum, FT_UID);
                    if ($mime_encoding == "base64") {
                        $dsp = imap_base64($dsp);
                    }
                    if ($mime_encoding == "qprint") {
                        $dsp = my_qprint($dsp);
                    }
                    $dsp = make_utf8($dsp, get_charset($part));
                    
                    $lines = explode("\n", $dsp);
                    foreach ($lines as $l)
                    {
                        $begin = substr($l, 0, 1) != ">" ? "> " : ">";
                        $body .= htmlspecialchars($begin . $l . "\n");
                    }
                }
            }
        }
    }
    return $body;
}
        $body .= add_mime_part($struct);

    }
    content_start();
    header_start();
    header_left();
?>
<?php
    global $user;
    echo L_MESSAGE_COMPOSE . " - $user - " . PROG_NAME;
    header_left_right_end();
    header_right();
    if ($msgnum != "")
    {
        echo "<form action=\"message.php#$msgnum\" method=\"get\">";
    }
    else
    {
        echo "<form action=\"index.php#$msgnum\" method=\"get\">";
    }
?>
    <input type="hidden" name="folder" value="<?php echo $folder ?>">
<?php
    if (is_array($msglist))
    {
        foreach ($msglist as $m)   
        {
            print "<input type=\"hidden\" name=\"msglist[]\" value=\"$m\">\n";
        }
    }
?>
    <input type="submit" value="<?php echo L_GO_BACK_BTN ?>">
    </form>

    <form enctype="multipart/form-data" name="doit" action="send_message.php" method="POST">
    <input type="hidden" name="folder" value="<?php echo $folder ?>">
    <input type="hidden" name="msgnum" value="<?php echo $msgnum ?>">
<?php
    if (is_array($msglist))
    {
        foreach ($msglist as $m)   
        {
            print "<input type=\"hidden\" name=\"msglist[]\" value=\"$m\">\n";
        }
    }

    header_left_right_end();
    header_end();
    body_compose_start();
    title_start();
    title_left();
    
    echo "<label for=\"to\">";
    echo L_TO_HDR . ":";
    echo "</label>";
    title_left_end();
    title_data();
?>
    <input type="text" name="to" id="to" size="80" value="<?php
    if ($mailto)
    {
        echo substr($mailto, 7, strlen($mailto));
    }
    else
    {
        echo $to;
    }

    ?>">
<?php
    title_data_end();
    title_end();
    
    title_start();
    title_left();
    echo "<label for=\"cc\">";
    echo L_CC_HDR . ":";
    echo "</label>";
    title_left_end();
    title_data();
?>
    <input type="text" name="cc" id="cc" size="80" value="<?php echo $cc ?>">
<?php
    title_data_end();
    title_end();

    title_start();
    title_left();
    echo "<label for=\"bcc\">";
    echo L_BCC_HDR . ":";
    echo "</label>";
    title_left_end();
    title_data();
?>
    <input type="text" name="bcc" id="bcc" size="80" value="<?php echo $bcc ?>">
<?php
    title_data_end();
    title_end();
 
    title_start();
    title_left();
    echo "<label for=\"attach\">";
    echo L_FILE_HDR . ":";
    echo "</label>";
    title_left_end();
    title_data();
?>
    <input type="file" name="attach" id="attach" size="68">
<?php
    title_data_end();
    title_end();

    title_start();
    title_left();
    echo "<label for=\"subject\">";
    echo L_SUBJECT_HDR . ":";
    echo "</label>";
    title_left_end();
    title_data();
?>
    <input type="text" name="subject" id="subject" size="80" value="<?php echo $subject; ?>">
<?php
    title_data_end();
    title_end();

    body_row();
    body_left('colspan="2" class="body"');
?>
<textarea name="body" cols="80" rows="25"><?php echo $body; ?></textarea>
<?php
    body_left_end();
    body_row_end();
    body_end();

    header_start();
    header_right();
?>
    <input type="submit" value="<?php echo L_SEND_BTN ?>">
</form>
<?php
    header_left_right_end();
    header_end();

    content_end();
?>

<script type="text/javascript">
<!--
document.doit.body.focus();
if(document.doit.subject.value == "") document.doit.subject.focus();
if(document.doit.to.value == "") document.doit.to.focus();
//-->
</script>

<?php
}



/* MAIN */
include(HTML_TEMPLATE);

?>
