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

$modid = "0";
$result = mysql_query( "SELECT * FROM $userstable WHERE strForumsModerated != '' ORDER BY strLastOnline DESC" );
$num_rows = mysql_num_rows( $result );
while ( $a_row = mysql_fetch_object( $result ) )
{
    $fmod = explode(",", $a_row->strForumsModerated);
    $count = 0;
    while ($count < 100)
    {
        if ($groupid == $fmod[$count])
        {
            $modid = $a_row->strName;
            break;
        }

        if ($fmod[$count] == "")
        {
            break;
        }
        $count ++;
    }
    if ($modid != "0")
    {
        break;
    }
}

if ($modid == "0")
{
    $result = mysql_query( "SELECT * FROM $userstable WHERE intAdminLevel > 0 ORDER BY intAdminLevel, intId DESC LIMIT 0,1" );
    $num_rows = mysql_num_rows( $result );
    while ( $a_row = mysql_fetch_object( $result ) )
    {
        $to = "$a_row->strName";
    }
}
else
{
    $to = "$modid";
}

$message = "$to,[br]The following post has been reported by $gfcookie[1].[br]Please take a look.[br][br] [report]topic.php?groupid=$groupid&topicid=$topic&#post$postid\t[unreport][br][br]Cheers.";

$query = "INSERT INTO $pmtable ( strTo, strFrom, strSubject, memMessage, strDateRecieved, strTimeRecieved, strEpochTime ) values( '$to', 'Forum Admin', 'Post Report', '$message', '$date', '$time', '$epoch')";

mysql_query( $query, $link );

$result=mysql_query("SELECT * FROM $userstable where strName = '$gfcookie[1]'");
$num_rows = mysql_num_rows( $result );
while ( $a_row = mysql_fetch_object( $result ) )
{
    $reports = $a_row->intThreadsReported;
}

$reports ++;

$query = "UPDATE $userstable SET intThreadsReported = '$reports' WHERE strName = '$gfcookie[1]'";
mysql_query( $query, $link ) or die ( "Error! : ".mysql_error() );

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