<?php
$file = __FILE__;
include ("inc/inc_common.php");
include ("inc/inc_connect.php");
//check this has a group id
if ($groupid == "")
{
    header("Location: forums.php");
    exit;
}
//if this group has a password, do a check...
$result = mysql_query( "SELECT * FROM $grouptable WHERE intId = '$groupid'" );
$num_rows = mysql_num_rows( $result );
while ( $a_row = mysql_fetch_object( $result ) )
{
    if ($a_row->strPassword != "NONE")
    {
        $resultb = mysql_query( "SELECT * FROM $passwordtable WHERE intTopicId = '$groupid' AND strName = '$gfcookie[1]'" );
        $bnum_rows = mysql_num_rows( $resultb );
        while ( $b_row = mysql_fetch_object( $resultb ) )
        {
            $password1 = $b_row->strPassword;
        }
        if ($password1 != $a_row->strPassword)
        {
            header("Location:forums.php");
            exit;
        }
    }
}

//check that this is not a gallery page...
$result = mysql_query( "SELECT * FROM $grouptable WHERE intId = '$groupid'" );
$num_rows = mysql_num_rows( $result );
while ( $a_row = mysql_fetch_object( $result ) )
{
    if ($a_row->intGallery == 1)
    {
        header("Location: gallerytopics.php?groupid=$groupid");
        exit;
    }
}
include ("inc/inc_admincheck.php");
include ("inc/inc_getdate.php");
include ("inc/inc_forumname.php");
$page = "Topics in $name2 - $name3 (group $groupid)";
$pagelink = $file."?groupid=".$groupid;
include("inc/inc_setonline.php");
//double check for an unknown topic...
if ($name3 == "Unknown Topic")
{
    header("Location: forums.php");
    exit;
}
$title = "$name1 - $name2 - $name3";
include ("inc/inc_head.php");
include ("inc/inc_body.php");
$title = $name3;
include ("inc/inc_title.php");
print "</centeR><table border=0 width=\"100%\" cellpadding=1 cellspacing=0><tr><td  width=\"20%\">
<table border=0 cellpadding=1 cellspacing=0><tr><td>
<font face=\"$font\" size=$fontsize><image src=\"images/folder_icon.gif\">&nbsp;<a href=\"index.php\">$name1&nbsp;Home</a>
</td></tr>
<tr><td><image src=\"images/folder_line.gif\">
<font face=\"$font\" size=$fontsize><image src=\"images/folder_icon.gif\">&nbsp;<a href=\"forums.php\">$name2</a></td></tr>
<tr><td><font face=\"$font\" size=$fontsize>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<image src=\"images/folder_line.gif\">
<image src=\"images/open_folder_icon.gif\">&nbsp;$name3</td></tr>
</table>
</td><td><center>";

include ("inc/inc_navigation.php");
if (isset($gfcookie[1]))
{
    print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"new_topic.php?groupid=$groupid\"><img border=0 src= \"images/new_post.gif\"></a>";
}
include ("inc/inc_newmailsdisplay.php");
print "</td></tr></table><br><center>";

//end of include files.


print "<table border=0 cellpadding=0 cellspacing=0 width=\"90%\" align=\"center\">
  <tr><td>
<table border=0 align=\"right\" width=\"35%\" cellspacing=0 cellpadding=0><tr><td align=\"center\"><font face=\"$font\" size=$fontsizea>Moderated by: ";

$twincheck = 0;
$mods = 0;
$admins = 0;
$commacount = 0;
$result = mysql_query( "SELECT * FROM $userstable WHERE strForumsModerated != '' ORDER BY strName" );
$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])
        {
            if ($commacount == 1)
            {
                print ", ";
            }
            print "<a title=\"See $a_row->strName's Profile\" href=\"profile.php?userid=$a_row->strName&groupid=$groupid\">$a_row->strName</a>";
            if ($a_row->intAdminLevel > 2)
            {
                $admins ++;
            }
            else
            {
                $mods ++;
            }
            $commacount = 1;
            break;
        }

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




print "<br><br></tr></td></table></tr></td></table>
<table border=1 cellpadding=0 cellspacing=0 style=\"border-collapse: collapse\" bordercolor=\"$bordercolor\" width=\"90%\" align=\"center\">
  <tr>
    <td width=3% bgcolor=\"$fgcolour\" align=center><b>
    <font face=$font size=2>
    <img border=0 src=\"images/new_posts_icon.gif\"></font></b></td>
    <td width=3% bgcolor=$fgcolour align=center><font face=$font size=2>
    <img  src=\"images/smilies/smile[1].gif\"></a></font></td>
    <td width=29% bgcolor=$fgcolour align=center>
    <p align=center><b><font face=$font size=2>Topic Title</font></b></td>
    <td width=20% bgcolor=$fgcolour align=center>
    <p align=center><b><font face=$font size=2>Author</font></b></td>
    <td width=7% bgcolor=$fgcolour align=center><b>
    <font face=$font size=2>Replies</font></b></td>
    <td width=6% bgcolor=$fgcolour align=center><b>
    <font face=$font size=2>Views</font></b></td>
    <td width=31% bgcolor=$fgcolour align=center><b>
    <font face=$font size=2>Last Post</font></b></td>
  </tr>";


if ($topicsperpage == 0)
{
    $result = mysql_query( "SELECT * FROM $forumtable WHERE intForumId = '$groupid' AND ynTopicRow = 'Yes' ORDER BY ynPriority DESC, strEpochTime DESC" );
}
else
{
    if (!isset($topiclimit))
    {
        $topiclimit = 0;
    }
    $result = mysql_query( "SELECT * FROM $forumtable WHERE intForumId = '$groupid' AND ynTopicRow = 'Yes' ORDER BY ynPriority DESC, strEpochTime DESC LIMIT $topiclimit, $topicsperpage" );
}

$num_rows = mysql_num_rows( $result );
while ( $a_row = mysql_fetch_object( $result ) )
{
    $topicdeleted = 0;

    if ($twincheck == $a_row->intTopicId)
    {
        continue;
    }
    $twincheck = $a_row->intTopicId;

    if ($a_row->intPostHidden == 1 AND $cAdminLevel == 0)
    {
        continue;
    }
    if ($a_row->intPostHidden == 1 AND $cAdminLevel > 0)
    {
        $topicdeleted = 1;
    }
    print " <tr>
    <td width=3% align=center>
    <p align=center><font face=$font size=2>";

    //work out replies and see if the thread is locked or not

    //Active topic?
    $active = 0;
    if (isset($gfcookie[1]))
    {
        $resultb = mysql_query( "SELECT * FROM $topicsreadtable WHERE intTopicId = '$a_row->intTopicId' AND strName = '$gfcookie[1]'" );
        $read = mysql_num_rows( $resultb );

        if ($read == 0)
        {
            //user is up to date with this topic...
            $active = 1;
        }
    }

    if ($gfcookie[1] == $a_row->strLastPostName)
    {
        $active = 0;
    }

    $posticon = "no_new_posts_icon";

    if ( $active == 1)
    {
        $posticon = "new_posts_icon";
    }


    if ($a_row->intReplies > $postsforhottopic)
    {
        $posticon = "hot_topic_no_new_posts_icon";
        if ($active == 1)
        {
            $posticon = "hot_topic_new_posts_icon";
        }
    }

    if ($a_row->ynLocked == "Yes")
    {
        $posticon = "lockedtopic";
    }

    if ($topicdeleted == 1)
    {
        $posticon = "delete_icon";
    }

    if ($a_row->ynPriority == 1)
    {
        $posticon = "priority_post_icon";
        if ($cAdminLevel > 1)
        {
            print "<a title=\"Make This A Normal Topic\" OnClick=\"return confirm('Are you sure you want to unstick this topic?')\" href=\"unstick.php?groupid=$groupid&topicid=$a_row->intTopicId\">";
        }
    }
    else
    {
        if ($cAdminLevel > 1)
        {
            print "<a title=\"Make This A Priority Topic\" OnClick=\"return confirm('Are you sure you want to make this a priority topic?')\" href=\"unstick.php?groupid=$groupid&topicid=$a_row->intTopicId&stick=1\">";
        }
    }
    print "<img border=0 src=\"images/$posticon.gif\">";


    print "</font></b></td>
    <td width=3% align=center>
    <img  src=\"$a_row->strSmilieUrl\"></td>
    <td width=29% align=center>
    <p align=left><font face=$font size=2>";

    //check to see if the admin level required to delete this topic is present...
    if ($cAdminLevel > 2)
    {
        if ($topicdeleted == 1)
        {
            print "&nbsp;<a OnClick=\"return confirm('Are you sure you want to Undelete this topic?')\" title=\"Undelete this topic\" href=\"deletetopic.php?delid=$a_row->intTopicId&groupid=$groupid&undelete=1\"><img border=0 src=\"images\undelete_icon.gif\"></a>";
        }
        else
        {
            print "&nbsp;<a OnClick=\"return confirm('Are you sure you want to delete this topic?')\" title=\"Delete this topic\" href=\"deletetopic.php?delid=$a_row->intTopicId&groupid=$groupid\"><img border=0 src=\"images\delete_icon.gif\"></a>";
        }
    }

    //check to see if the admin level required to lock / unlock this topic is present...
    if ($cAdminLevel > 0)
    {
        if ($a_row->ynLocked == "Yes")
        {
            print "&nbsp;<a OnClick=\"return confirm('Are you sure you want to unlock this topic?')\" title=\"Lock this topic\" href=\"unlocktopic.php?lockid=$a_row->intTopicId&groupid=$groupid\"><img border=0 src=\"images\forum_locked_icon.gif\"></a>";
        }
        else
        {
            print "&nbsp;<a OnClick=\"return confirm('Are you sure you want to lock this topic?')\"title=\"Lock this topic\" href=\"locktopic.php?lockid=$a_row->intTopicId&groupid=$groupid\"><img border=0 src=\"images\forum_unlock_icon.gif\"></a>";
        }

    }
    $time = $a_row->strLastPostTime;
    include ("inc/inc_time.php");
    print "&nbsp;<a title=\"Read this topic\" href=\"topic.php?topicid=$a_row->intTopicId&groupid=$groupid\">$a_row->strTopicName</a>";

    if ($topicdeleted == 1)
    {
        print "&nbsp;<font size=$fontsizea color=\"$ratecolor\">Deleted";
    }
    if ($postsperpage != 0)
    {
        $postse = $a_row->intReplies;
        $postse ++;
        if ( $postse > $postsperpage)
        {
            $pages = floor($postse / $postsperpage);
            $pages ++;
            print "<font size=$fontsizea><br>&nbsp;&nbsp;&nbsp;&nbsp;Page ";
            $countz = 0;
            while ($countz < $pages)
            {
                $limstart = $countz * $postsperpage;
                $countz++;
                if ($countz > 5)
                {
                    $limstart = $pages * $postsperpage;
                    print "... <a href=\"topic.php?topicid=$a_row->intTopicId&limitstart=$limstart&pagenum=$pages\">Last Page</a>";
                    break;
                }
                print "<a href=\"topic.php?topicid=$a_row->intTopicId&limitstart=$limstart&pagenum=$countz\">$countz</a> ";

            }
        }
    }
    print "</font></td>
    <td width=20% align=center>
    <p align=center><font face=\"$font\" size=$fontsize color=\"$fontcolor\"><a title=\"See $a_row->strName's Profile\" href=\"profile.php?userid=$a_row->strName&groupid=$groupid\">$a_row->strName</a></font></td>
    <td width=7% align=center><font face=\"$font\" size=$fontsize color=\"$fontcolor\">$a_row->intReplies</font></td>
    <td width=6% align=center><font face=\"$font\" size=$fontsize color=\"$fontcolor\">$a_row->intViews</font></td>
    <td width=31% align=center>
    <font face=\"$font\" size=$fontsize color=\"$fontcolor\">By <a title=\"See $a_row->strLastPostName's Profile\" href=\"profile.php?userid=$a_row->strLastPostName&groupid=$groupid\">$a_row->strLastPostName</a> at $time$am on the $a_row->strLastPostDate</td>
    </tr>";
}






if ($num_rows == 0)
{
    print "</table><br><center><font face=$font size=2><br>There are no topics in this group.";

    if (isset($gfcookie[1]))
    {
        print " <a href=\"new_topic.php?groupid=$groupid\">Add one</a>!";
    }

    print "<br><br>";
}

print "</table><br>";

if ($topicsperpage != 0)
{
    $result = mysql_query( "SELECT * FROM $forumtable WHERE intForumId = '$groupid' AND ynTopicRow = 'Yes' ORDER BY ynPriority DESC, strEpochTime DESC" );
    $num_rows = mysql_num_rows( $result );


    $pages = floor($num_rows / $topicsperpage);
    $pages ++;
    if ($pages > 1)
    {
        if (!isset($pagenum))
        {
            $pagenum = 1;
        }

        $nextpage = $pagenum + 1;
        $prevpage = $pagenum - 1;

        print "<br><br>";
        if ($pagenum != 1 AND $pagenum != 2)
        {
            print "<a href=\"topics.php?groupid=$groupid&topiclimit=0\">First Page</a> ";
        }
        if ($pagenum != 1)
        {
            $tlimstart = $topiclimit - $topicsperpage;
            print "<a href=\"topics.php?groupid=$groupid&topiclimit=$tlimstart&pagenum=$prevpage\">Previous Page</a> ";
        }
        $count = 0;
        while ($count < $pages)
        {
            $tlimstart = $count * $topicsperpage;
            $count++;

            if ($count == $pagenum)
            {
                print "$count ";
            }
            else
            {
                print "<a href=\"topics.php?groupid=$groupid&topiclimit=$tlimstart&pagenum=$count\">$count</a> ";
            }
        }
        if ($pagenum < $pages)
        {
            $limstartb = $limitstart + $topicsperpage;
            print "<a href=\"topics.php?groupid=$groupid&topiclimit=$limstartb&pagenum=$nextpage\">Next Page</a> ";
        }
        if ($pagenum < ($pages - 1))
        {
            print "<a href=\"topics.php?groupid=$groupid&topiclimit=$tlimstart&pagenum=$count\">Last Page</a>";
        }
        print "<br><br>";
    }
}





$result = mysql_query( "SELECT * FROM $forumtable WHERE ynTopicRow = 'Yes' AND intForumId = '$groupid'" );
$num_rows = mysql_num_rows( $result );
//Work Out Stats
$totaltopics = 0;
$totalposts = 0;
while ( $a_row = mysql_fetch_object( $result ) )
{
  $totaltopics++;
  $totalposts = $totalposts + $a_row->intReplies;
}

$result = mysql_query( "SELECT * FROM $forumtable WHERE intForumId = '$groupid' ORDER BY strName" );
$num_rows = mysql_num_rows( $result );
//Work Out Stats
$totalusers = 0;
$prevname = 0;
while ( $a_row = mysql_fetch_object( $result ) )
{
    if ("$a_row->strName" == "$prevname")
    {
        continue;
    }
    $prevname = "$a_row->strName";
    $totalusers ++;
}

$result = mysql_query( "SELECT * FROM $userstable ORDER BY intId DESC" );
$num_rowsb = mysql_num_rows( $result );
//Work Out Stats
while ( $a_row = mysql_fetch_object( $result ) )
{
    if ( ! $a_row->ynBanIp == "Yes")
    {
        $newmember = $a_row->strName;
        break;
    }
}


if ( $mods == 1 )
{
    $plural = "";
}
else
{
    $plural = "s";
}

if ( $admins == 1 )
{
    $plural2 = "";
}
else
{
    $plural2 = "s";
}

$totalusers = $totalusers - $admins;
$totalusers = $totalusers - $mods;
$numberonline = 0;

$result = mysql_query( "SELECT * FROM $userstable ORDER BY intId" );
$num_rows = mysql_num_rows( $result );
//Work Out Stats
while ( $a_row=mysql_fetch_object( $result ) )
{
    $onlinenow = $epoch - $a_row->strLastOnline;
    if ( $onlinenow < 300 )
    {
        $numberonline ++;
    }
}
if ($totalusers < 0)
{
    $totalusers = 0;
}

print "</table>";



print "<br><br>
  <table border=0 cellpadding=0 cellspacing=0 style=border-collapse: collapse bordercolor=#111111 width=90%>
    <tr>
      <td width=23%>&nbsp;</td>
      <td width=23%>&nbsp;</td>
      <td width=54% bgcolor=$fgcolour><table border=1 width=100% bordercolor=#111111 cellpadding=0 cellspacing=0><tr><td width=100%>
      <font face=$font size=2><center>There are $totalusers
      users, $mods moderator$plural and $admins administrator$plural2 in this forum.<br>
      In the $name3 forum they have posted $totaltopics topics and $totalposts replies.<br>There are <a href=\"members.php?groupid=$gropupid&showonline=1\">$numberonline members</a> online right now.<br>
      Welcome our
      newest $name1 member:
      <a title=\"See $newmember's Profile\" href=\"profile.php?userid=$newmember&groupid=$groupid\">$newmember</td>
    </tr></td></tr></table>
  </table>

<br>";

print "<table width=60% border=0>
<tr>
<td width=16%><center>
<img src=\"images/no_new_posts_icon.gif\">
</td>
<td width=16%><center>
<img src=\"images/new_posts_icon.gif\">
</td>
<td width=16%><center>
<img src=\"images/hot_topic_no_new_posts_icon.gif\">
</td>
<td width=16%><center>
<img src=\"images/hot_topic_new_posts_icon.gif\">
</td>
<td width=17%><center>
<img src=\"images/priority_post_icon.gif\">
</td>
<td width=16%><center>
<img src=\"images/lockedtopic.gif\">
</td>
</tr>
<tr>
<td width=16%><center>
<font face=$font size=2>
Normal Topic<br>(No replies)
</td>
<td width=16%><center><font face=$font size=2>
Normal Topic<br>(New Replies)
</td>
<td width=16%><center><font face=$font size=2>
Hot Topic<br>(No Replies)
</td>
<td width=16%><center><font face=$font size=2>
Hot Topic<br>(New Replies)
</td>
<td width=17%><center><font face=$font size=2>
Priority Topic
</td>
<td width=16%><center><font face=$font size=2>
Closed Topic
</td>
</tr>
</table>";

include("inc/inc_birthday.php");
include("inc/inc_footer.php");
?>