<?php

include "misc.inc";
include "common.inc";
include "db_connect.inc";

/**
 * Parameters:
 *
 * email - text
 * fido7password - text
 * subject    - text
 * body       - text
 * emailcopy  - true/false
 * authorcopy - true/false
 * emailid    - id
 * fromemail - text
 * fromname  - text
 */

$email     = trim($email);
$fromemail = trim($fromemail);

$ok=0; 

while(1)
{
    $login = current_user();
    if(!$login)
    {
        $errmsg="Вы должны сначала зарегистрироваться!";
        break;
    }

    if(strlen($email)==0 ||
       strlen($fido7password)==0 ||
       strlen($subject)==0 ||
       strlen($body)==0 ||
       strlen($fromemail)==0)
    {
        $errmsg="Вы должны заполнить все поля формы!";
        break;
    } 

    $message = $body . "\n\n--\nПослано через " . $PROGRAM_URL . "\n";
    $posting_email = str_replace(".","-",$OBEC_COMMENTS_GROUP) . "@fido7.ru";
    
    $lines = substr_count($message, "\n");

    // References: <parent msgid> references line from previous messagw
    if(strlen($fromname)>0)
        $xto=$fromname;
    else
        $xto="All";

    // RFC 1036 requires to include whole References field from
    // original message plust original message id. However to do so we
    // would have to parse message header from database.  At this
    // point doint full header parsing is too complicated (taking into
    // account quoting, line continuations, etc, so we do not include
    // any references except id of actual message we are replying
    // to. It is actually allowed by RFC1036.

    $sql = "SELECT MsgId FROM EmailMessages WHERE Id=" . addslashes($emailid);
    $result = mysql_db_query($db_database, $sql, $link);
    if(!$result)
    {
        $errmsg = "Error fetching original comment or article Message-Id";
        break;
    } 
    $row   = mysql_fetch_array($result);
    mysql_free_result ($result);

    $common_headers = 
    "From: " . $email . "\n" . 
    "Newsgroups: " . $OBEC_COMMENTS_GROUP . "\n" .
    "References: " . $row[0] . "\n" .
    "X-Comment-To: " . $xto . "\n" .
    "X-Complaints-To: " . $OBEC_EMAIL . "\n" .
    "Organization: " . $PROGRAM_NAME . " " . $PROGRAM_URL . "\n" .
    "Distribution: world\n" .
    "NNTP-Posting-Host: obec.org.ru\n" .
    "Path: obec.org.ru!not-for-mail\n" .
    "X-Mailer: " . $HTTP_USER_AGENT . " via " .$PROGRAM_NAME . "\n" .
    "X-OBEC-User: " . $login . "\n" .
    "MIME-Version: 1.0\n" .
    "Content-Type: text/plain; charset=koi8-r\n" .
    "Lines: " . $lines;

    // First send copy to newsgroup with CC to sender (if required)
    // this message contains sender password!
    if(!mail($posting_email, $subject, $message,
             (($emailcopy=="true")?"Cc: $email\n":"") .
             "Keywords: " . $fido7password . "\n" .
             $common_headers
       ))
    {
        $errmsg = "Ошибка отправки почтового сообщения на указанный адрес.";
        break;
    }

    if($authorcopy=="true" && strlen($fromemail)>0)
    {
        // First send copy to newsgroup with CC to sender of original message
        // this message does not contains sender password.
        if(!mail($fromemail, $subject, $message,
                 $common_headers
           ))
        {
            $errmsg = "Ошибка отправки почтового сообщения на указанный адрес.";
            break;
        }
    }
    
    $ok = 1;
    break;
} // while

if($ok)
{
    do_redirect("post_ok.php");
} else
{
    start_page();
}

?>

<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=koi8-r">
<TITLE>Posting error!</TITLE>
</HEAD>

<BODY link="#000033" vlink="#333333" alink="#CC0000">
<?php show_header(); ?>

<p>Ваше сообщение не было послано. Причина:
<pre>
        <?php print $errmsg; ?>
</pre>
Вы можете <a href="post.php">вернуться</a> и попробовать снова.

<?php show_footer(); ?>

</BODY>
</HTML>
