<?php
include ("inc/inc_connect.php");
include ("inc/inc_common.php");
include ("inc/inc_getdate.php");
include ("inc/inc_admincheck.php");
if ($file != "none" AND $file != "")
{
    if (strtolower(substr("$file_name", -3)) != "jpg" AND strtolower(substr("$file_name", -3)) != "gif")
    {
        header("Location: gallery.php?groupid=$groupid&topicid=$topicid&noimage=file#replyhere");
        exit;
    }
    if (strtolower(substr("$file", -3)) != "jpg" AND strtolower(substr("$file_name", -3)) != "gif")
    {
        header("Location: gallery.php?groupid=$groupid&topicid=$topicid&noimage=file#replyhere");
        exit;
    }

    $pathonserver = "images/";
    //where to put the stuff

    $folder = "user/";
    //the folder to put it in


    $fileonserver = $pathonserver.$folder.$file_name;
    while (file_exists($fileonserver))
    {
        $rand = rand(0, 9);
        $file_name = $rand.$file_name;
        $fileonserver = $pathonserver.$folder.$file_name;
    }

    copy($file, $fileonserver);
    unlink($file);


// /* If you server does not allow the creation of thumbnails, remove the comments (//) at the beginning of this line, which will comment out the rest.
    if (strtolower(substr("$file_name", -3)) == "jpg")
    {
        $src_name = $pathonserver.$folder.$file_name;
        $dest_name = $pathonserver.$folder."tn_".$file_name;

        $src = imagecreatefromjpeg($src_name);

        $tnw = imagesx($src);
        $tnw = $tnw / 150;
        $tnh= imagesy($src);
        $tnh = floor($tnh / $tnw);

 //       $w = floor(imagesx($src) * 0.2);
 //       $h = $tnh;
        $w = 150;
        $h = 150;

        $dest = imagecreate($w,$h);

        imagecopyresized($dest, $src, 0, 0, 0, 0, $w, $h, imagesx($src), imagesy($src));

        imagejpeg($dest, $dest_name, 80);

        imagedestroy($src);
        imagedestroy($dest);
    }

    if (strtolower(substr("$file_name", -3)) == "gif")
    {
        $src_name = $pathonserver.$folder.$file_name;
        $dest_name = $pathonserver.$folder."tn_".$file_name;

        $src = imagecreatefromgif($src_name);

        $tnw = imagesx($src);
        $tnw = $tnw / 150;
        $tnh= imagesy($src);
        $tnh = floor($tnh / $tnw);

 //       $w = floor(imagesx($src) * 0.2);
 //       $h = $tnh;
        $w = 150;
        $h = 150;


        $dest = imagecreate($w,$h);

        imagecopyresized($dest, $src, 0, 0, 0, 0, $w, $h, imagesx($src), imagesy($src));

        imagejpeg($dest, $dest_name, 80);

        imagedestroy($src);
        imagedestroy($dest);


    }
    // */

    $imageurl = "$fileonserver";
    $imagethumb = "$dest_name";
}

if ($imageurl == "" OR $imageurl == "http://yourhost.com/yourimage.jpg")
{
    header("Location: gallery.php?groupid=$groupid&topicid=$topicid&noimage=nope#replyhere");
    exit;
}
if ($imageurl == "" OR $imageurl == "http://yourhost.com/yourimage.jpg")
{
    header("Location: gallery.php?groupid=$groupid&topicid=$topicid&noimage=nope#replyhere");
    exit;
}


///* This may not be working. If your GrimForum errors when uploading an image, uncomment this line (which will comment all the rest).

if (!file_exists($imageurl))
{
    header("Location: gallery.php?groupid=$groupid&topicid=$topicid&noimage=notfound#replyhere");
    exit;
}


if (!is_readable($imageurl))
{
    header("Location: gallery.php?groupid=$groupid&topicid=$topicid&noimage=notfound#replyhere");
    exit;
}
if (strtolower(substr("$imageurl", -3)) != "jpg" AND strtolower(substr("$imageurl", -3)) != "gif")
{
    header("Location: gallery.php?groupid=$groupid&topicid=$topicid&noimage=file#replyhere");
    exit;
}

// */

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

$cleantext = $imagetitle;
include ("inc/inc_swearfilter.php");
$postthis = $cleantext;
include ("formatpostssmall.php");
$imagetitle = $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);



$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, strGalleryImage, strGalleryThumb, ynGalleryRow ) 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', '$imagetitle', '$thisforum', '$imageurl', '$imagethumb', 'Yes' )";

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...

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

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



?>