#!/usr/bin/perl 

###########################################################################
#    WWWThreads  - Interactive Discussion Forum
#    Rick Baker scream@screamingweb.com 
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
###########################################################################
  use lib '/home/simplew2/domains/forum.sportruck.com/public_html/cgi-bin';
  use wwwthreads qw(%config $dbh);  
  use strict;

# Assign the variables
  my %FORM   = wwwthreads::get_input;
  my $action = $FORM{'action'};
  my $Board  = $FORM{'Board'};
  my $page   = $FORM{'page'};
  my $view   = $FORM{'view'};
  my $sb     = $FORM{'sb'};
  my $sort   = $FORM{'sort'};



# If there is no specified action then just show the list of boards
  if(!$action){

    &show_list;

  }


# If action is 'list' then we give the list of posts for the selected board
  if($action eq "list"){

    &list_posts($Board,$page,$view,$sb,$sort);

  }


#End of this program
  wwwthreads::exit();


##########################################################################
## This subroutine shows the list of boards in the database
##########################################################################
  sub show_list{

  wwwthreads::send_header("$wwwthreads::config{'title'}");

  wwwthreads::db_connect();

  # --------------------------
  # Get the list of categories
    my $query = qq!
      SELECT DISTINCT Title,Number
      FROM Category
      ORDER BY Number
    !;
    my $sta = $dbh->prepare ($query) or die "Query syntax error: $DBI::errstr. Query: $query";
    $sta -> execute() or die "Can't execute query: $query. Reason: $DBI::errstr";
    my $catrows = $sta -> rows;


  # ----------------
  # Print the header
  # (see above)

  # --------------------------------------------------
  # Lets see how many total registered users there are
    my $query = qq!
       SELECT COUNT(*)
       FROM Users
    !;
    my $stb = $dbh->prepare ($query) or die "Query syntax error: $DBI::errstr. Query: $query";
    $stb -> execute() or die "Can't execute query: $query. Reason: $DBI::errstr";
    my ($registered) = $stb -> fetchrow_array;
    $stb -> finish;
    print "$registered Registered Users"; 


  # ----------------------------
  # Cycle through the categories
    for (my $j=0;$j<$catrows;$j++) {
      my($CatTitle,$CatNumber) = $sta -> fetchrow_array;

    # ---------------------------------------
    # Get the list of boards in this category
      my $CatTitle_q = $dbh -> quote($CatTitle);
      my $NULL_q     = $dbh -> quote("NULL");
      my $query = qq!
        SELECT Keyword,Sorter 
        FROM Category
        WHERE Title = $CatTitle_q
        AND Keyword <> $NULL_q 
        ORDER BY Sorter 
      !;
      my $stb = $dbh->prepare ($query) or die "Query syntax error: $DBI::errstr. Query: $query";
      $stb -> execute() or die "Can't execute query: $query. Reason: $DBI::errstr";
      my $boardrows = $stb -> rows;

    # -------------------------------------------------------------- 
    # If there are no boards in this category, we skip this category
      if (!$boardrows) { next; }

    # -------------------------------------------------------
    # Open up a table to surround all boards in this category
      print "<table border=0 width=100% bgcolor=$wwwthreads::config{'catcolor'}>";
      print "<tr><td>";
      print "<font size=+1><b>$CatTitle</b></font>";
      print "<br><br>";

    # ---------------------------------------------
    # Now cycle through the boards in this category
      for (my $k=0;$k<$boardrows;$k++) {

        my($Keyword,$Sorter) = $stb -> fetchrow_array;
        if(!$Keyword) { next; }
        my $Keyword_q = $dbh -> quote($Keyword);
        my $query = qq!
          SELECT Title,Description,Keyword,Moderator,Total,Last,HTML,Anonymous,Number,Locked,Expire,Markup
          FROM Boards
          WHERE Keyword = $Keyword_q
        !;
        my $sth = $dbh->prepare ($query) or die "Query syntax error: $DBI::errstr. Query: $query";
        $sth -> execute() or die "Can't execute query: $query. Reason: $DBI::errstr";
        my ($rows) = $sth -> rows;

        my ($Title,$Description,$Keyword,$Moderator,$Total,$Last,$HTML,$Anonymous,$Number,$Locked,$Expire,$Markup) = $sth -> fetchrow_array;
        $sth -> finish;
 
        my $time = "";
      # If Last = 0 then this is a new board so set Last Post to New Board
        if (!$Last){

          $time = "New Board";

        }else{

          $time = wwwthreads::convert_time($Last);

        }

        print qq~
        <table border=0 width=100% bgcolor=$wwwthreads::config{'tddark'} cellspacing=0>
        <tr><td width=50%>
        <a href="$wwwthreads::config{'cgiurl'}/wwwthreads.pl?action=list&Board=$Keyword">$Title</a></td><td width=25%>
        ~;


      # --------------------------------------------------
      # Check if the are new posts since they last visited
        my %cookie = wwwthreads::get_cookie();
        my $Username = $cookie{'Username'};
        $Username ||=" ";
        my $Username_q = $dbh -> quote($Username);
        my $LastViewed = $Keyword . "Last";
        my $query = qq!
          SELECT Last 
          FROM $LastViewed  
          WHERE Username = $Username_q
        !;
        my $sth = $dbh -> prepare($query) or die "Query syntax error: $DBI::errstr. Query: $query";
        $sth -> execute() or die "Can't execute query: $query. Reason: $DBI::errstr";
        my $checker = $sth -> fetchrow_array;
        $sth -> finish;
        $checker ||="0";


      # --------------------------------------
      # Let's see how many new posts there are
=fuck_this
        my $query = qq!
          SELECT COUNT(*)
          FROM $Keyword
          WHERE Posted > $checker
        !;
        my $sth = $dbh -> prepare($query) or die "Query syntax error: $DBI::errstr. Query: $query";
        $sth -> execute() or die "Can't execute query: $query. Reason: $DBI::errstr";
        my $newposts = $sth -> fetchrow_array;
		$shownew = "<font color=\"$wwwthreads::config{'newfontcolor'}\">($newposts new)</font>" if($newposts > 0);
        if( ($checker < $Last) && ($cookie{'Username'}) ) {
          print "<font size=-1>$Total Posts $shownew";
        } else {
          print "<font size=-1>$Total Posts";
        }
=cut
		# alternitive
        print "<font size=-1>$Total Posts";
 
        print qq~ 
        </font></td><td width=25%>
        <font size=-1>Last Post: $time</font></td></tr></table>
        <table border=0 width=100% cellspacing=0>
        <tr><td colspan=3 bgcolor=$wwwthreads::config{'tdlight'}>
        <font size=-1>$Description</font>
        <br>
        </td></tr>
        ~;

      # ----------------------------------------------------
      # If maininfo is on, then we list the board info here
        if ($wwwthreads::config{'maininfo'} eq "on") {

          print"<tr><td width=33% bgcolor=$wwwthreads::config{'tdlight'}>";
          print"<i><font size=-1>Moderated by: $Moderator</font></i>";
 
          if ($Locked ne "Open") {

            print"</td><td width=67% colspan=2 bgcolor=$wwwthreads::config{'tdlight'}><i><font size=-1>This board is read only</font></i>";
            print"</td></tr></table>";

          } else {

            print"</td><td width=33% bgcolor=$wwwthreads::config{'tdlight'}><i><font size=-1>HTML is $HTML / Markup is $Markup</font></i>";
            print"</td><td width=34% bgcolor=$wwwthreads::config{'tdlight'}><i><font size=-1>Anonymous Postings $Anonymous</font></i>";
            print"</td></tr>";

          }

        }
        print "<tr><td colspan=3></td></tr>";
        print "</table>";
      }
  
    # ----------------------------------------------------
    # This closes out the table we opened for the category
      print "</td></tr></table>";
      print "<br>";
      $stb -> finish;
    }

  # Print the footer;
    $sta -> finish;
    wwwthreads::send_footer();
  }


############################################################################
## This subroutine lists the posts for the selected board
############################################################################
  sub list_posts{

    my($Board,$page,$view,$sb,$sort) = @_;

  # ----------------------
  # Connect to the database
    wwwthreads::db_connect();


  # ----------------
  # Grab their prefs
    my %user = wwwthreads::authenticate();


  # --------------------------
  # Set the default sort order
    if ( (!$sb) && ($user{'Sort'}) ) { 
      $sb = $user{'Sort'};
    } 
    elsif( (!$sb) && (!$user{'Sort'}) ) {
      $sb = $wwwthreads::config{'sort'};
    }


  # -------------------------------------------------------
  # if $view isn't set then it gets set to the default view
    if ( (!$view) && ($user{'View'}) ) {
      $view = $user{'View'};  
    } 
    elsif ( (!$view) && (!$user{'View'}) ) {
      $view = $wwwthreads::config{'threaded'};
    }


  # ----------------------------------------
  # if $page isn't set then it defaults to 0
    if (!$page){
      $page = 0;
    }


  # -------------------
  # Grab the board info
    my $Board_q = $dbh -> quote($Board);
    my $query = qq!
      SELECT Title,Locked,Last,Moderator,HTML,Anonymous,Locked,Markup FROM Boards 
      WHERE Keyword = $Board_q
    !;
    my $sth = $dbh -> prepare($query) or die "Query syntax error: $DBI::errstr. Query: $query";
    $sth -> execute() or die "Can't execute query: $query. Reason: $DBI::errstr";
    my ($Title,$Locked,$Last,$Moderator,$HTML,$Anonymous,$Locked,$Markup) = $sth -> fetchrow_array;
    $sth -> finish;

  # ----------------------------------------------------------------
  # Grab the cookie from them so we know what the last post they saw
  # and updates this to the current last post of the board
    my %cookie = wwwthreads::get_cookie;
    my $tname = "$Board.temp";	# The temporary cookie
    my $Username = $user{'Username'};
    my $unread;
    $Username ||=" ";
    my $Username_q = $dbh -> quote($Username);
    my $LastViewed = "$Board"."Last";
    if( ($user{'Username'}) && (!$cookie{$tname}) ) {

      my $query = qq!
        SELECT Last
        FROM $LastViewed
        WHERE Username = $Username_q
      !;
      $sth = $dbh -> prepare ($query) or die "Query syntax error: $DBI::errstr. Query: $query";
      $sth -> execute() or die "Can't execute query: $query. Reason: $DBI::errstr";
      my $oldlast = $sth -> fetchrow_array;
      $sth -> finish;
      wwwthreads::set_cookie($tname,"$oldlast",0);
      $unread = $oldlast;

    }

  # ----------------------------------------------
  # Only update the database if they are logged in
    if ($user{'Username'}) {

      my $query = qq!
        REPLACE INTO $LastViewed
        (Username,Last)
        VALUES ($Username_q,$Last)
      !;
      $dbh -> do($query) or die "Can't execute query: $query. Reason: $DBI::errstr";
    
    }

    if (!$unread) {

      my %cookie = wwwthreads::get_cookie;
      $unread = $cookie{$tname};

    }

    my $rname = "$Board.read";
    my $read  = $cookie{$rname};

  # Print the header
    wwwthreads::send_header($wwwthreads::config{'title'});
  # Show them how its sorted
    my %sort_title =  (
      1 => 'Descending Subject',
      2 => 'Ascending Subject',
      3 => 'Descending Username',
      4 => 'Ascending Username',
      5 => 'Descending Date',
      6 => 'Ascending Date'
    );
    my $sort_t = $sort_title{$sb};


  # print the title of the board
    my $EModerator = wwwthreads::url_encode($Moderator);
    print qq~
    <table border=0 width=100% bgcolor="$wwwthreads::config{'tddark'}" cellspacing=0>
    <tr><td align=left><font size=+1><b>$Title</b></font>
    <br><font size=-1>Sorted by $sort_t</font></td>
    <td>&nbsp;
    </td><td valign=top align=right>
    ~;
    print "<font size=-1>";
    if ($Locked ne "Open") {
      print "Board is Read Only";
    }
    else {
      print "Anonymous Postings $Anonymous<br>";
      print "HTML is $HTML / Markup is $Markup";
    }
    print "</font>"; 
    print qq~
    </td></tr>
    <tr>
    <td><font size=-1>Moderated by 
    <a href=\"$wwwthreads::config{'cgiurl'}/showprofile.pl?User=$EModerator&Board=$Board&what=wwwthreads&page=$page&view=$view&sb=$sb\">$Moderator</a></font>
    </td><td>&nbsp;
    </td><td align=right>
    ~;

  # If the board is not open then we need to see if the user has logged in
  # and if they are an administrator before we give them a make post link.
    if($Locked ne "Open"){


    # Assign the variables
      my $Username = $user{'Username'};
      my $Password = $user{'Password'};

      if (!$Username) { $Username = "Nb"; }
      my $Username_q = $dbh -> quote($Username);

    # Check if this user is an administrator
      my $query =qq!
        SELECT Status
        FROM Users
        WHERE Username = $Username_q
      !;
      $sth = $dbh -> prepare ($query) or die "Query syntax error: $DBI::errstr. Query: $query";
      $sth -> execute() or die "Can't execute query: $query. Reason: $DBI::errstr";
      my $Administrator = $sth -> fetchrow_array;
      $sth -> finish;

    # If status is administrator give them a New Post link
      if( ($Administrator eq "Administrator") || ($Administrator eq "Moderator") ){
 
        print "<a href=\"$wwwthreads::config{'cgiurl'}/newpost.pl?Board=$Board&page=$page&view=$view&sb=$sb\"><img src=\"$wwwthreads::config{'images'}/newpost.gif\" alt = \"Make a New Post\" border=0></a>";

      }


    # Otherwise we let them know that this board is locked
      else {
  
        print"<img alt=\"*\" src=\"$wwwthreads::config{'images'}/greynewpost.gif\">";

      }


  # Otherwise the board is open and new posts can be made
    } else {

      print "<a href=\"$wwwthreads::config{'cgiurl'}/newpost.pl?Board=$Board&page=$page&view=$view&sb=$sb\"><img src=\"$wwwthreads::config{'images'}/newpost.gif\" alt = \"Make a New Post\" border=0></a>";

    }


  my $sortsubject= 1;
  my $sortposter= 3;
  my $sortdate = 6;
  # Make sure we are giving the proper sort options
    if ($sb == 1) {
      $sortsubject = 2;
    } else {
      $sortsubject = 1;
    }
    if ($sb == 3){
      $sortposter = 4;
    } else {
      $sortposter = 3;
    }
    if ($sb ==5)  {
      $sortdate = 6;
    } else {
      $sortdate = 5;
    } 

  # figure out how we are sorting them
    my %sort_opt =  (
       1 => 'Subject DESC',
       2 => 'Subject ASC',
       3 => 'Username DESC',
       4 => 'Username ASC',
       5 => 'Last_Post DESC',
       6 => 'Last_Post ASC'
    );
    my $sort_by = $sort_opt{$sb};


  # Find out how many posts to show per page
    my $PostsPer = $user{'PostsPer'};
    $PostsPer ||= $wwwthreads::config{'postsperpage'};
  # Now we calculate which posts to grab for this page.  We want to grab one
  # from the previous page and one from the next page so we know what the
  # previous and next posts will be
    my $Totalgrab = '';

    if (!($page > 0)){
      $Totalgrab = $PostsPer + 1;
    }else{
      my $Startat = $page *$PostsPer;
      my $Posts = $PostsPer +1;
      $Totalgrab = "$Startat, $Posts";
    }


   # New code 12-14-00 -- limit results
   my($max_rows, $max_main);
   $sth=$dbh->prepare("select threads, max from dzsUpdater where board=?");
   $sth->execute($Board);
   $sth->bind_columns(undef, \$max_rows, \$max_main);
   $sth->fetch;
   $sth->finish;
 

    $query = qq!
      SELECT Number,Parent,Posted,Last_Post,Username,Subject,Main,Status

      FROM $Board
      WHERE Number = Main
	!;

    if (defined $max_main) {
      $query .= "AND Main > $max_main";
    }
    $query .= qq!
      ORDER BY $sort_by
      LIMIT $Totalgrab
    !;

    $sth = $dbh -> prepare ($query) or die "Query syntax error: $DBI::errstr. Query: $query";
    $sth -> execute() or die "Can't execute query: $query. Reason: $DBI::errstr";
    my $nums = $sth -> rows;

  # If $page is greater than 0 then we need a link to the previous page
    if($page > 0){

      my $prev = $page -1;
      print "<a href=\"$wwwthreads::config{'cgiurl'}/wwwthreads.pl?action=list&Board=$Board&page=$prev&view=$view&sb=$sb\"><img src=\"$wwwthreads::config{'images'}/previous.gif\" alt = \"Previous Page\" border=0></a>";

    } else {

      print "<img alt=\"*\" src =\"$wwwthreads::config{'images'}/greyprevious.gif\">";
 
    }


  # Give a link to the forum index
    print "<a href=\"$wwwthreads::config{'cgiurl'}/wwwthreads.pl\"><img src=\"$wwwthreads::config{'images'}/all.gif\" alt = \"Show all Forums\" border=0></a>";

 
  # If total parent posts is greater than $PostsPer*Page then we give them
  # a link to the next page
    if($nums == ($PostsPer +1) ){

      my $next = $page +1;
      print "<a href=\"$wwwthreads::config{'cgiurl'}/wwwthreads.pl?action=list&Board=$Board&page=$next&view=$view&sb=$sb\"><img src=\"$wwwthreads::config{'images'}/next.gif\" alt = \"Next Page\" border=0></a>";

    } else {

      print "<img alt=\"*\" src=\"$wwwthreads::config{'images'}/greynext.gif\">";

    }


  # If $postlist is "threaded" then we need to give them the proper
  # button to expand or collapse the threads
    if($wwwthreads::config{'postlist'} eq "threaded"){

      if($view eq "collapsed"){

        print"<a href=\"$wwwthreads::config{'cgiurl'}/wwwthreads.pl?action=list&Board=$Board&page=$page&view=expanded&sb=$sb\"><img src=\"$wwwthreads::config{'images'}/expand.gif\" alt = \"Expand Threads\" border=0></a>";
        print "<img alt=\"*\" src=\"$wwwthreads::config{'images'}/greycollapse.gif\">";
 
      } else {

        print "<img alt=\"*\" src=\"$wwwthreads::config{'images'}/greyexpand.gif\">";      
        print"<a href=\"$wwwthreads::config{'cgiurl'}/wwwthreads.pl?action=list&Board=$Board&page=$page&view=collapsed&sb=$sb\"><img src=\"$wwwthreads::config{'images'}/collapse.gif\" alt = \"Collapse Threads\" border=0></a>";

      }

    }  


  # Finish out the board header
  
    print qq~
    </td></tr><tr><td colspan=3 bgcolor=$wwwthreads::config{'tdalt'} height=2><img alt=\"-\" src=\"$wwwthreads::config{'images'}/hr.gif\"></td></tr>
    </table>
    <table border=0 cellspacing = 0 width=100%>
    <tr><td bgcolor=$wwwthreads::config{'tdlight'} align=left>
    <a href="$wwwthreads::config{'cgiurl'}/wwwthreads.pl?action=list&Board=$Board&page=$page&view=$view&sb=$sortsubject"><b>Subject</b></a></td>
    <td bgcolor=$wwwthreads::config{'tdlight'} align=left nowrap width=10%>
    <a href="$wwwthreads::config{'cgiurl'}/wwwthreads.pl?action=list&Board=$Board&page=$page&view=$view&sb=$sortposter"><b>Poster</b></a></td>
    ~;

    # --------------------------------------------------------------------
    # If we are doing flat posts or the view is collapsed we need a column 
    # for the number of replies
      if (($wwwthreads::config{'postlist'} eq "flat") || ($view eq "collapsed"))
      {
        print "<td bgcolor=$wwwthreads::config{'tdlight'} align=left nowrap width=10%>";
        print "Replies</td>";
      }

    print qq~
    <td bgcolor=$wwwthreads::config{'tdlight'} align=left nowrap width=10%>
    ~;
    if ($view ne "expanded") {
      print"<a href=\"$wwwthreads::config{'cgiurl'}/wwwthreads.pl?action=list&Board=$Board&page=$page&view=$view&sb=$sortdate\"><b>Last Post</b></a></td></tr>";
    }
    else {
      print"<a href=\"$wwwthreads::config{'cgiurl'}/wwwthreads.pl?action=list&Board=$Board&page=$page&view=$view&sb=$sortdate\"><b>Posted On</b></a></td></tr>";
    }
  

  # List the posts for this page
    my $color = "$wwwthreads::config{'tddark'}";

    for(my $i=0;$i<$nums;$i++){
      
    # We need to break out of here if we are over $PostsPer, we just
    # needed to know if there was a next page or noet
      if($i==$PostsPer){ last; }


      my ($Number,$Parent,$OrigPost,$Posted,$Username,$Subject,$Main,$Open) = $sth -> fetchrow_array;

    # convert the time
      my $time;
      if ($view ne "expanded") {
        $time = wwwthreads::convert_time($Posted);
      } 
      else {
        $time = wwwthreads::convert_time($OrigPost);
      }

    # If view is expanded then we give the openfolder icon, if it is
    # collapsed then we give the closed folder.

      my $thisone = ",$Number,"; 
      my $folder = "closedfolder.gif";
      my $alt    = ".";

      if (($view eq "expanded") && ($Posted <= $unread)) { 

        $alt    = ".";
        $folder = "openfolder.gif";

      } 
      elsif (($view eq "collapsed") && ($Posted <= $unread)) {

          $alt    = ".";
          $folder = "closedfolder.gif";
  
      } 
      elsif (($view eq "expanded") && ($Posted > $unread) && (!($read =~/$thisone/)) ) 
      {

        $alt    = "*";
        $folder = "newopenfolder.gif";

      } 
      elsif (($view eq "collapsed") && ($Posted > $unread) && (!($read =~/$thisone/)) )
      {

        $alt    = "*";
        $folder = "newclosedfolder.gif";

      } 
      elsif (($view eq "collapsed") && ($Posted > $unread) && ($read =~/$thisone/) ) 
      {

        $alt    = ".";
        $folder = "closedfolder.gif";

      } 
      elsif (($view eq "expanded") && ($Posted > $unread) && ($read =~/$thisone/) ) 
      {

        $alt    = ".";
        $folder = "openfolder.gif";

      }

    # If this thread is locked then we give them the lock icon
      if ($Open eq "C"){ 
        $folder = "lock.gif"; 
        $alt    = "-";
      }

      print "<tr><td bgcolor=$color align=left>";
      print "<img alt =$alt src=\"$wwwthreads::config{'images'}/$folder\" hspace=5>";
      

      print "<a href=\"$wwwthreads::config{'cgiurl'}/showpost.pl?Board=$Board&Number=$Number&page=$page&view=$view&sb=$sb\">$Subject</a>";

      if ($Username eq "Anonymous"){

        print"</td><td bgcolor=$color align=left nowrap>Anonymous&nbsp;&nbsp;</td>";

      }else{
        my $EUsername = wwwthreads::url_encode($Username);
        print "</td><td bgcolor=$color align=left nowrap><a href=\"$wwwthreads::config{'cgiurl'}/showprofile.pl?User=$EUsername&Board=$Board&what=wwwthreads&page=$page&view=$view&sb=$sb\">$Username</a>&nbsp;&nbsp;";

      }

    # If we are doing flat posts or the view is collapsed we need to list
    # the number of replies
      if (($wwwthreads::config{'postlist'} eq "flat") || ($view eq "collapsed")) 
      {

        $query =qq!
          SELECT Number,Parent,Posted
          FROM $Board
          WHERE Main = $Number
          AND Number <> $Number
        !;
        my $sti = $dbh -> prepare($query) or die "Query syntax error: $DBI::errstr. Query: $query";
        $sti -> execute() or die "Can't execute query: $query. Reason: $DBI::errstr";
        my $replies = $sti -> rows;
        my $new = '';
        for (my $i=0;$i<$replies;$i++){
          my($No, $Pa, $Po) = $sti -> fetchrow_array;
          my $checkthis = ",$No,";  
          if ( ($Po > $unread) && (!($read =~/$checkthis/)) ) {
            $new = "yes"; 
          }
        }
        $sti -> finish;
        if ($replies < 1) { $replies = 0; }

        print "</td><td bgcolor=$color nowrap align=center>";
        print "$replies";
        if ($new eq "yes") {

          print "<img alt=\"new\" src=\"$wwwthreads::config{'images'}/new.gif\" alt=\"new\">";
        }   
        else {
          print "&nbsp;&nbsp;&nbsp;";
        } 
      }
   

      print "</td><td bgcolor=$color align=left nowrap>$time</td></tr>\n";

    # alternate the colors
      if ($color eq "$wwwthreads::config{'tdlight'}") {
        $color = "$wwwthreads::config{'tddark'}";
      } else {
        $color = "$wwwthreads::config{'tdlight'}";
      }
        
      
    # If we are not doing flat posts and the view is expanded then we
    # will list all the replies out in a threaded format.
      if (($wwwthreads::config{'postlist'} ne "flat") && ($view eq "expanded") ){
        $query = qq!
          SELECT Number
          FROM $Board
          WHERE Parent = $Parent
        !; 
        my $sta = $dbh -> prepare ($query) or die "Query syntax error: $DBI::errstr. Query: $query";
        $sta -> execute() or die "Can't execute query: $query. Reason: $DBI::errstr";
        my $followups = $sta -> rows;
        $sta -> finish;
        if ($followups > 0) {
          $color = &show_replies($Number,$Board,0,$unread,$read,$color,$page,$view,$sb,$sort);

        }

      }

    }

  # -------------------------------------------
  # Here is where we let them jump around pages
    print "</table><table width=100% border=0><tr><td align=center>";
    $sth -> finish; 
  # ---------------------------------
=junk
  # Find out how many pages there are     <-- monkey crap
    my $query = qq!
      SELECT Number,Main
      FROM $Board
      WHERE Number = Main
        AND Main > $max_num_to_find 
    !;
    $sth = $dbh -> prepare($query) or die "Query syntax error: $query. Reason: $DBI::errstr.";
    $sth -> execute() or die "Can't execute query: $query.  Reason: $DBI::errstr";
    my $rows = $sth -> rows;
    $sth -> finish;
=cut

    my $TotalPages = $max_rows / $PostsPer - 1;

    my ($TotalP,$Leftover) = split(/\./,$TotalPages);
    if ($Leftover > 0) { $TotalP++; }
    if ($TotalP > 1) { print "Pages: "; }

    my $Startpage = $page - 10;
    my $Endpage   = $page + 10;

    if ($Startpage < 0) { 
      $Endpage   = $Endpage - $Startpage;
      $Startpage = 0;
    }
    if ($Endpage > $TotalP) {
      $Endpage = $TotalP;
      $Startpage = $Endpage - 20;
    }
    if ($Startpage < 0) { $Startpage = 0; }
    if ($Startpage > 0) {
        my $prev = $Startpage - 1;
        print "<a href=\"$wwwthreads::config{'cgiurl'}/wwwthreads.pl?action=list&Board=$Board&page=$prev&view=$view&sb=$sb\">\<\<</a> ";
    }

    if ($max_rows > $PostsPer){ 
      for (my $i=$Startpage; $i<= $Endpage; $i++){
        if ($i == $page) {
          print "$i ";
        } else {
          print "<a href=\"$wwwthreads::config{'cgiurl'}/wwwthreads.pl?action=list&Board=$Board&page=$i&view=$view&sb=$sb\">$i</a> ";

        }
      }    
    }
    if ($Endpage < $TotalP) {
      my $next = $Endpage + 1;
      print "<a href=\"$wwwthreads::config{'cgiurl'}/wwwthreads.pl?action=list&Board=$Board&page=$next&view=$view&sb=$sb\">\>\></a> ";
    }

    print "</td></tr>";
    print "<tr><td align=right>";
    
    my $query = qq!
      SELECT Title,Keyword
      FROM Boards
    !;
    my $sth = $dbh -> prepare ($query) or die "Query syntax error: $DBI::errstr. Query: $query";
    $sth -> execute() or die "Can't execute query: $query. Reason: $DBI::errstr";

    print "<FORM METHOD=POST action=\"$wwwthreads::config{'cgiurl'}/jumper.pl\">";
    print "Jump to ";
    print "<select name=board>";
    while (my (@varlist) = $sth -> fetchrow_array) {
      print "<option value=\"$varlist[1]\">$varlist[0]\n";
    }
    print "</select>";
    print "<INPUT TYPE=submit name=Jump value=Jump>";
    print "</form>";
    print "</td></tr>";
    print "</table>";

  # Print the footer

    $sth -> finish;
    wwwthreads::send_footer;

  }

###########################################################################
# show replies - list the replies here if we are doing threaded, expanded
###########################################################################

sub show_replies {

    my ($Mother,$Board,$indent,$unread,$read,$color,$page,$view,$sb,$sort) = @_;
   

    my $query = qq!
      SELECT Number,Parent,Posted,Username,Subject,Status
      FROM $Board
      WHERE Parent = $Mother
      ORDER BY Number Desc
    !;
    my $sth = $dbh -> prepare ($query) or die "Query syntax error: $DBI::errstr. Query: $query";
    $sth -> execute() or die "Can't execute query: $query. Reason: $DBI::errstr";
    my ($followups) = $sth -> rows;
  
    $indent++;
    
    for(my $x=0;$x<$followups;$x++){

      my ($Number,$Parent,$Posted,$Username,$Subject,$Open) = $sth -> fetchrow_array;

      print "<tr><td bgcolor=$color>";
      for (my $y=0;$y<$indent;$y++){
    
        print "<img alt=\".\" src=\"$wwwthreads::config{'images'}/blank.gif\">";

      }

      my $time = wwwthreads::convert_time($Posted);
      my $folder = "closedfolder.gif";
      my $alt     = ".";
      my $thisone = ",$Number,";

      if( ($Posted > $unread) && (!($read =~/$thisone/)) ){

        $alt    = "*";
        $folder = "newclosedfolder.gif";
      } else {

        $alt    = ".";
        $folder = "closedfolder.gif";

      }

      if ($Open eq "C") { 
        $alt    = "-";
        $folder = "lock.gif"; 
      }

      print"<img alt=$alt src=\"$wwwthreads::config{'images'}/$folder\" hspace=5><a href=\"$wwwthreads::config{'cgiurl'}/showpost.pl?Board=$Board&Number=$Number&page=$page&view=$view&sb=$sb#Post$Number\">$Subject</a>";

        if ($Username eq "Anonymous"){

          print"</td><td bgcolor=$color>Anonymous</td>";

        }else{

          my $encoded = wwwthreads::url_encode($Username);
          print "</td><td bgcolor=$color><a href=\"$wwwthreads::config{'cgiurl'}/showprofile.pl?User=$encoded&Board=$Board&what=wwwthreads&page=$page&view=$view&sb=$sb\">$Username</a></td>";
        }

      print "<td bgcolor=$color>$time</td></tr>";

    # alternate the colors
      if ($color eq "$wwwthreads::config{'tdlight'}") {
        $color = "$wwwthreads::config{'tddark'}";
      } else {
        $color = "$wwwthreads::config{'tdlight'}";
      }

      $query =qq!
        SELECT Number
        FROM $Board
        WHERE Parent = $Number
      !;

      my $sta = $dbh -> prepare ($query) or die "Query syntax error: $DBI::errstr. Query: $query";
      $sta -> execute() or die "Can't execute query: $query. Reason: $DBI::errstr";
      my $follows = $sta -> rows;
      if ($follows > 0) {
        $sta -> finish;
        $color = &show_replies($Number,$Board,$indent,$unread,$read,$color,$page,$view,$sb,$sort);

      }

    }

    $indent--;
    $sth -> finish;
    return $color;
  }

