<?php
include ("inc/inc_common.php");
include ("inc/inc_connect.php");
include ("inc/inc_getdate.php");

$cleantext = $tsttxt;
include ("inc/inc_swearfilter.php");
$reply = $cleantext;

if ($spellcheck == 1)
{
    $query = "INSERT INTO $spelltable (strName, memPost, memOrigPost, intTopicId) values( '$gfcookie[1]', '$reply', '$reply', '$topicid')";
    mysql_query( $query, $link ) or die ( "Error! : ".mysql_error() );

    header("Location: spellcheck.php?topicid=$topicid");
    exit;
}



// only uncomment the lines below if you are getting syntax errors when posting with an apostraphe or quote marks!

// $reply = str_replace ( "'", "&acute;", $reply);
// $reply = str_replace ( "\"", "&quot;", $reply);



$result = mysql_query( "SELECT * FROM $userstable WHERE strName = '$gfcookie[1]'" );
$num_rows = mysql_num_rows( $result );
//Dispay records
while ( $a_row = mysql_fetch_object( $result ) )
{
    $posts = $a_row->intPosts;
    $topics = $a_row->intTopics;
    $signature = $a_row->memSig;
}
if ( $num_rows == 0 )
{
    //user is deleted
    header("Location: logout.php");
    exit;
}

//add 1 to post count
if ($posts == "")
{
    $posts = "0";
}
$posts ++;
// send a message if star count has changed...
include ("inc/inc_postmessage.php");

$query = "UPDATE $userstable SET intPosts = '$posts', strLastPostTitle = '$topictitle', strLastPostGroupId = '$groupid', strLastPostTopicId = '$topicid', strLastPostDate = '$date' WHERE strName = '$gfcookie[1]'";
mysql_query( $query, $link )
or die ( "Could not add data to \"tblUser\" table: "
.mysql_error() );

//find out what forum group this is in

$result = mysql_query( "SELECT * FROM $grouptable WHERE intId = '$groupid'" );
$num_rows = mysql_num_rows( $result );
while ( $a_row = mysql_fetch_object( $result ) )
{
    $thisforum = $a_row->intGroupId;
}

//add the new post to the database
$query = "INSERT INTO $forumtable
( strName, strEmail, strAvatar, memSig, intPosts, strHomepage, strIp, ynShowEmail, strIcqNumber, strMsnName, strYahooId, strLocation, strForumComment, strDate, strTime, memPost, intTopicId, strEpochTime, intForumId, strTopicName, intForumGroup ) values( '$gfcookie[1]', '$gfcookie[2]', '$gfcookie[3]', '    $signature', '$gfcookie[5]', '$gfcookie[6]', '$REMOTE_ADDR', '$gfcookie[9]', '$gfcookie[10]', '$gfcookie[11]', '$gfcookie[12]', '$gfcookie[13]', '$gfcookie[14]', '$date', '$ftime', '$reply', '$topicid', '$epoch', '$groupid', '$topictitle', '$thisforum' )";

mysql_query( $query, $link ) or die ( "Could not add data to \"$forumtable\" table: "
.mysql_error() );

//find the first post in the topic and take the number of replies - add one and put them back along with last poster details...


$result = mysql_query( "SELECT * FROM $forumtable WHERE intTopicId = '$topicid' ORDER BY intId Limit 0,1" );
$num_rows = mysql_num_rows( $result );
//Dispay records
while ( $a_row = mysql_fetch_object( $result ) )
{
        $thisreplies = $a_row->intReplies;
        $thisid = $a_row->intTopicId;
}

if ($thisreplies == "")
{
    $thisreplies = "0";
}

$thisreplies++;


$result = mysql_query( "SELECT * FROM $forumtable WHERE intTopicId = '$topicid' ORDER BY intLastPost DESC LIMIT 0,1" );
$num_rows = mysql_num_rows( $result );
//Dispay records
while ( $a_row = mysql_fetch_object( $result ) )
{
    $thislastpost = $a_row->intLastPost;
}
$thislastpost++;




$query = "UPDATE $forumtable SET intReplies = '$thisreplies', strLastPostName = '$gfcookie[1]', strLastPostTime = '$ftime', strLastPostDate = '$date', intLastPost = '$thislastpost', strEpochTime = '$epoch' WHERE intTopicId = '$topicid'";
mysql_query( $query, $link ) or die ( "Could not add data to \"tblMain\" table: "
.mysql_error() );

//wipe all the 'up to date' entries from $topicsreadtable for this topic...

$query = "DELETE FROM $topicsreadtable WHERE intTopicId = '$topicid'";
mysql_query( $query, $link ) or die ( "Could not delete from \"$topicsreadtable\" table: ".mysql_error() );


//All done - let's get back to where we were...
//And to do that, we need the Post ID...
$counter = 0;
$result = mysql_query( "SELECT * FROM $forumtable WHERE intTopicId = '$topicid' ORDER BY intId" );
$num_rows = mysql_num_rows( $result );
//Dispay records
while ( $a_row = mysql_fetch_object( $result ) )
{
    $counter ++;
    $postid = $a_row->intId;
}

if ($counter > $postsperpage)
{
    $pages = floor($counter / $postsperpage);
    $pages ++;
    $limstart = $counter - $postsperpage;
    $limstart ++;

    header("Location: topic.php?topicid=$topicid&limitstart=$limstart&pagenum=$pages#post$postid");
    exit;
}

header("Location: topic.php?groupid=$groupid&topicid=$thisid#post$postid");
exit;



?>

