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

if ($topicname == "")
{
    $topicname = "No Name";
}

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

$length = strlen($topicname);
if ($length > 63)
{
    $topicname = substr("$topicname", 0, 60);
    $topicname = $topicname."...";
}

$cleantext = $topicname;
include ("inc/inc_swearfilter.php");
$postthis = $cleantext;
include ("formatpostssmall.php");
$topicname = $postthis;


// 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);




//make sure the user is logged in and check that they haven't been deleted since last post
if (!isset($gfcookie[1]))
{
    header("Location: notloggedin.php");
    exit;
}

if (isset($votetitle))
{
    $R1 = "images/voteicon.gif";
}

$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 and topic count
if ($posts == "")
{
    $posts = "0";
}

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

$posts ++;
$topics ++;


$dateofpost = $date;
$timeofpost = $time;
$epochtime = $epoch;

// send a message if star count has changed...
include ("inc/inc_postmessage.php");

$result = mysql_query(  "SELECT * FROM $forumtable WHERE ynTopicRow = 'Yes' ORDER BY intTopicId DESC LIMIT 0,1" );
$num_rows = mysql_num_rows( $result );

while ( $a_row = mysql_fetch_object( $result ) )
{
    $newtopicid = $a_row->intTopicId;
}

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

$newtopicid ++;

$query = "UPDATE $userstable SET intPosts = '$posts', intTopics = '$topics', strLastPostTitle = '$topicname', strLastPostGroupId = '$groupid', strLastPostTopicId = '$newtopicid', strLastPostDate = '$dateofpost' 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;
    $hbh = $a_row->intHbhForum;
    $open = $a_row->intOpenForum;
}

$query = "INSERT INTO $forumtable ( strName, strEmail, strAvatar, memSig, intPosts, strHomepage, strIp, ynShowEmail, strIcqNumber, strMsnName, strYahooId, strLocation, strForumComment, strDate, strTime, memPost, intTopicId, strTopicName, strEpochTime, ynPriority, intForumId, intForumGroup, intHbhForum, intOpenForum ) 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]', '$dateofpost', '$timeofpost', '$reply', '$newtopicid', '$topicname', '$epochtime', '$priority', '$groupid', '$thisforum', '$hbh', '$open' )";

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

$thisreplies = 0;
$thislastpost = 0;

$query = "UPDATE $forumtable SET intReplies = '$thisreplies', strLastPostName = '$gfcookie[1]', strLastPostTime = '$timeofpost', strLastPostDate = '$dateofpost', intLastPost = '$thislastpost', ynTopicRow = 'Yes', strSmilieUrl = '$R1' WHERE intTopicId = '$newtopicid'";
mysql_query( $query, $link ) or die ( "Could not add data to \"tblMain\" table: ".mysql_error() );


//is this a rated topic?

if (isset($ratetext))
{
    $ratetext = substr("$ratetext", 0, 25);
    $ratetext = str_replace ( "<", "&lt;", $ratetext);
    $ratetext = str_replace ( ">", "&gt;", $ratetext);
    $cleantext = $ratetext;
    include ("inc/inc_swearfilter.php");
    $ratetext = $cleantext;
    $query = "UPDATE $forumtable SET intRateTopic = '1', strRateThis = '$ratetext' WHERE intTopicId = '$newtopicid'";
    mysql_query( $query, $link ) or die ( "Could not add data to \"tblMain\" table: ".mysql_error() );
}

//is it a vote?

if (isset($votetitle))
{
    $cleantext = $voteoption1;
    include ("inc/inc_swearfilter.php");
    $voteoption1 = $cleantext;
    $cleantext = $voteoption2;
    include ("inc/inc_swearfilter.php");
    $voteoption2 = $cleantext;
    $cleantext = $voteoption3;
    include ("inc/inc_swearfilter.php");
    $voteoption3 = $cleantext;
    $cleantext = $voteoption4;
    include ("inc/inc_swearfilter.php");
    $voteoption4 = $cleantext;
    $cleantext = $voteoption5;
    include ("inc/inc_swearfilter.php");
    $voteoption5 = $cleantext;
    $cleantext = $voteoption6;
    include ("inc/inc_swearfilter.php");
    $voteoption6 = $cleantext;
    $cleantext = $voteoption7;
    include ("inc/inc_swearfilter.php");
    $voteoption7 = $cleantext;
    $cleantext = $voteoption8;
    include ("inc/inc_swearfilter.php");
    $voteoption8 = $cleantext;
    $cleantext = $voteoption9;
    include ("inc/inc_swearfilter.php");
    $voteoption9 = $cleantext;
    $cleantext = $voteoption10;
    include ("inc/inc_swearfilter.php");
    $voteoption10 = $cleantext;

    $query = "INSERT INTO $votetable (intTieToTopic, strStarter, intTotalVotes, strTitle, memInfo, strOption1, strOption2, strOption3, strOption4, strOption5, strOption6, strOption7, strOption8, strOption9, strOption10) VALUES ('$newtopicid', '$gfcookie[1]', '0', '$votetitle', '$votetext', '$voteoption1', '$voteoption2', '$voteoption3', '$voteoption4', '$voteoption5', '$voteoption6', '$voteoption7', '$voteoption8', '$voteoption9', '$voteoption10')";
    mysql_query( $query, $link ) or die ( "Could not add data to \"$votetable\" table: ".mysql_error() );

    $result = mysql_query( "SELECT * FROM $votetable WHERE intTieToTopic = '$newtopicid'" );
    $num_rows = mysql_num_rows( $result );
    while ( $a_row = mysql_fetch_object( $result ) )
    {
        $voteid = $a_row->intId;
    }

    $query = "UPDATE $forumtable SET intVoteId = '$voteid' WHERE intTopicId = '$newtopicid'";
    mysql_query( $query, $link ) or die ( "Could not add data to \"tblMain\" table: ".mysql_error() );
}

header("Location: topic.php?topicid=$newtopicid&groupid=$groupid");
exit;



?>
