#!/usr/bin/perl
#
# DCForum99 Version 1.0
# dcboard.cgi
# Part of DCForum by DCScripts
# Copyright  ©1997-1999 DCScripts All Rights Reserved
#
# As part of the installation process, you will be asked
# to accept the terms of this Agreement. This Agreement is
# a legal contract, which specifies the terms of the license
# and warranty limitation between you and DCScripts and DCForum.
# You should carefully read this terms agreement as outlined in
# the readme.txt file included with this distribution before
# installing or using this software.  Unless you have a different license
# agreement obtained from DCScripts, installation or use of this software
# indicates your acceptance of the license and warranty limitation terms
# contained in this Agreement. If you do not agree to the terms of this
# Agreement, promptly delete and destroy all copies of the Software.
#
###################################################################
#
# NOTE: You may need to provide full path to each setup files
# Some server require this.
# $path is the directory path to the location of this file.

$path = ".";

#>>>>>>>>>>>>>>> NO NEED TO EDIT ANYTHING BELOW THIS LINE <<<<<<<<
#>>>>>>>>>>>>>>> NO NEED TO EDIT ANYTHING BELOW THIS LINE <<<<<<<<
#>>>>>>>>>>>>>>> NO NEED TO EDIT ANYTHING BELOW THIS LINE <<<<<<<<

require "$path/dcforum.setup";
require "$path/dcboard.setup";
require "$cgidir/cgi-lib.pl";
require "$cgidir/dcforumlib.pl";
require "$cgidir/dclib.pl";

# Print HTTP output header
print "Content-type: text/html\n";

# Read-in form data...
# Someday, I need to replace this with my own version
&ReadParse;

# define reference to %in
# this things going to be passed to everywhere
$r_in = \%in;

# Check and see if we are working with Archive
if ($r_in->{'database'} eq "" or $r_in->{'database'} ne 'archive') {
      $r_in->{'database'} = $database;
}
else {
  $database = "arc_" . $database;
   $r_in->{'arc_database'} = $database;
}

# Get current time and date
get_date($r_in);

# initialize some internal variables...
# Need to change this for speed improvement
$r_setup = initialize($password_file_dir,$forum_file,$board_setup_file);

# Set Global variables
# Must require this after the call to initialize
require "$path/global.pl";

# If the input forum type is Private, then
# set appropriate file extension and dir

if ($r_setup->{'forum_type'}->{ $r_in->{'forum'} } eq 'Private') {
   $ext = $privateext;
   $maindir = $privatedir;
}

# Get forum information and put them in old variables

$forum = $r_in->{'forum'};
$az = $r_in->{'az'};

# OK, before we do anything, let's check the user's session ID
# If it exists then read in user data

$r_in->{'userdata'} = {};
$session = get_cookie($cookie_name);
if ($session) {
   if (-e "$password_file_dir/$session.session") {
      $r_in->{'userdata'} = get_userdata("$password_file_dir/$session.session");
   }
   else {
      send_cookie($cookie_name,'','Thur, 31-Dec-98 12:00:00 GMT','');
      $session = '';
   }
}

# Next readin the team members
# and append it as a string 
#$r_team = [];

if (-e "$password_file_dir/$team_file_name") {
   $r_team = readdata("$password_file_dir/$team_file_name");
}
$team = join(" ",@{$r_team});

# OK, if $az is 'list' then list topics in a forum
# If not, list forums

if ($az eq "list") {

   # End of HTTP Header
   print "\n";

   # Create menu buttons
   $menu_button = create_button('list',$r_in,$r_setup);

   # Oppps, forum is offline
   if ($r_setup->{'forum_status'}->{ $forum } ne "on") {
      ($heading,$sub_heading,$html_output) = 
         access_error('forum off',$r_in,$r_setup);
   }
   # Oppps, no access allowed
   elsif ( ! check_forum_access($r_in,$r_setup,$forum) ) {
      ($heading,$sub_heading,$html_output) = 
         access_error('forum private',$r_in,$r_setup);
   }
   else {
      ($heading, $sub_heading, $html_output) = list($r_in,$r_setup);
   }

}

# Listing main entrance - the forum list

else {

   # First end http header
    print "\n";

   $heading = $heading_list_forums;
   $sub_heading = $sub_heading_list_forums;   

   # If no $session, display drop down menu to fetch messages
   # from last few days

   unless ($session) {
      $sub_heading .= qq~
      <FORM METHOD="POST" ACTION="$exurl">
      <INPUT TYPE="HIDDEN" NAME="az" VALUE="n_mesg">
      <FONT SIZE="2"> Read new messages posted during last <SELECT NAME="days">
      <OPTION value="1">1 Day
      <OPTION value="2">2 Days
      <OPTION value="3">3 Days
      <OPTION value="4">4 Days
      <OPTION value="5">5 Days
      <OPTION value="6">6 Days
      <OPTION value="7">1 Week
      </SELECT>
      <INPUT TYPE="SUBMIT" NAME="submit" VALUE="Now!">
      </FONT></FORM>~;
   }

   # Create menus for the forum listing
   $menu_button = create_button("list_forums",$r_in,$r_setup);

   # list forums
   $html_output = list_forums($r_in,$r_setup);

}

# create forum_links
$html_forum_links = forum_links($r_in,$r_setup);

# Get template variables ready
my %namespace = (
     FORUMLINKS => $html_forum_links,
     MENU => $menu_button,
     TITLE => $heading,
     HEADER => $heading,
     SUBHEADER => $sub_heading,
     HTMLOUTPUT => $html_output
);

# display output
display_output($templatefile,\%namespace);

exit(0);

#####
#
# subroutine list_forums
# Creates the main lobby page
#
#####

sub list_forums {

   my ($r_in,$r_setup) = @_;
 
   my $current_time = time_format($r_in->{'localtime'});
   my $current_date = date_format($r_in->{'date'});

   my $html_output = '';

	$icon_display = 'yes';

	# Should Icon indicator be displayed?

      $html_output .= qq~
      <TABLE BORDER="0" WIDTH="$table_width" CELLPADDING="3" CELLSPACING="1">
      <TR BGCOLOR="$bg_color_0">
      <TH ALIGN="LEFT" VALIGN="TOP" COLSPAN="2">
      <font size="$font_size_0" face="$font_face_0" color="$font_color_0">
         Forums in $conf_name</font>
         </TH></TR>
	</TABLE>
   ~;

   $html_output .= qq~
   	<TABLE BORDER="0" WIDTH="$table_width" CELLPADDING="3" CELLSPACING="1">~;

   toggle_color('reset');

   foreach (@{ $r_setup->{'forums'} }) {

        if ($r_setup->{'forum_status'}->{$_} eq "on") {

           my $moderator_email = $r_setup->{'forum_owner_email'}->{$_};

           $r_setup->{'forum_desc'}->{$_} =~ s/&gt;/>/g;
           $r_setup->{'forum_desc'}->{$_} =~ s/&lt;/</g;

            my ($ltime,$date) = split(/\s/, $r_setup->{'forum_last'}->{$_});
            $ltime .= ":00";
            $ltime = time_format($ltime);
            $date = date_format($date);

            $html_output .= qq~
               <TR><TD ALIGN="LEFT" VALIGN="TOP" BGCOLOR="$bgcolor">
               <img src="$imgurl/folder.gif">
               <font size="$font_size" face="$font_face" color="$font_color">
               <b><a href="$boardurl?az=list&forum=$_">$r_setup->{'forum_name'}->{$_}</a>
               <br></b></font>
               <font size="$font_size" face="$font_face" color="$font_color">
               $r_setup->{'forum_desc'}->{$_}
               </font></TD><TD ALIGN="LEFT" VALIGN="TOP" BGCOLOR="$bgcolor">
                <font size="$font_size" face="$font_face" color="$font_color">
               <b>Moderator: <a href="mailto:$moderator_email">$r_setup->{'forum_owner'}->{$_}</a></b>
               <br>$r_setup->{'forum_type'}->{$_}&nbsp;Forum
               <br><b>$r_setup->{'forum_threads'}->{$_}</b>&nbsp;Current&nbsp;Discussions 
               <br>$r_setup->{'forum_archive'}->{$_}&nbsp;Archived&nbsp;Discussions
               <br>$date&nbsp;$ltime</font></TD></TR>~;

            toggle_color();

         }

		}

   $html_output .= "</TABLE>";

}

####
# subroutine list
# Reads in from $database and generates
# the main threads listing
####

sub list {
  
   my ($r_in,$r_setup) = @_;
   my $heading;
	my $temp_output;
   my $exist = "";
   my $msg_num;
   my $moderator_email = $r_setup->{'forum_owner_email'}->{$forum};
   my $datafile  = "$maindir/$forum/$database";
   my $r_rowdata = readdata($datafile);
   my $num_messages = @{$r_rowdata};
   my $num_next = int(($num_messages - 1)/$mesg_max) + 1;
   my $start = $r_in->{'mm'} + 1;
   my $stop = $r_in->{'mm'} + $mesg_max;

   $stop = $num_messages if ($stop > $num_messages);

   $heading = "Topics in $r_setup->{'forum_name'}->{$forum}";

   $html_output .= qq~
   <center><font face="$font_face_3" color="$font_color_3" size="$font_size_3">
            Viewing thread number $start-$stop<br>
         ~;

   if ($r_in->{'arc_database'}) {
      $other = "[<a href=\"$boardurl?forum=$forum&az=list\">Current</a>
      ($r_setup->{'forum_threads'}->{$forum} threads)]\n";
   }
   else {
      $other = "[<a href=\"$boardurl?forum=$forum&az=list&database=archive\">Archive</a>
      ($r_setup->{'forum_archive'}->{$forum})]\n";
   }

   $stop--;
   $start--;
  
   for ($j=1;$j<=$num_next;$j++) {

      my $mesg_marker = ($j-1)*$mesg_max;
      my $j_start = ($j-1)*$mesg_max + 1;
      my $j_stop = $j*$mesg_max;

      $j_stop=$num_messages if ($j_stop > $num_messages);

      if ($mesg_marker == $start) {      
         $temp_output .=qq~
         [$j] ~;
      }
      elsif ($r_in->{'arc_database'}) {
         $temp_output .=qq~
         [<A HREF="$boardurl?az=list&forum=$forum&mm=$mesg_marker&database=archive">$j</A>] ~;
      }
      else {
         $temp_output .=qq~
         [<A HREF="$boardurl?az=list&forum=$forum&mm=$mesg_marker">$j</A>] ~;
      }
   }

   $html_output .= qq~
	$temp_output
   $other
   </font>
   </center>
   ~;

   $html_output .= qq~
   <CENTER>
   <TABLE BORDER="0" WIDTH="$table_width" cellspacing="1" cellpadding="3">
   <TR>
	<TD COLSPAN="4" ALIGN="RIGHT">
   <font face="$font_face_3" color="$font_color_3" size="1">
   Moderator: <a href="mailto:$r_setup->{'forum_owner_email'}->{$forum}">$r_setup->{'forum_owner'}->{$forum}</a><br>
   Time Zone: $time_zone<br>
   Forum Type: $r_setup->{'forum_type'}->{$forum}
	</FONT>
   </TD>
   </TR>
   <a name="TOP">&nbsp;</a>
   ~;

   if ($r_setup->{'board_setup'}->{'display_caption'} eq "on") {
      $table_heading = get_table_heading('main');
   }
   else {
      $table_heading = get_table_heading();
   }

   $html_output .= $table_heading;
  
   my @row = @$r_rowdata[$start..$stop];
  
   toggle_color('reset');

   foreach (@row) {

      my $thisimg = "folder.gif";

      my ($num,$date,$ltime,$name,$subject,$caption,$replies) = split(/\|/, $_);

      if ($r_setup->{'board_setup'}->{'display_caption'} eq "on" && $caption ne "") {
         $caption = "<br>$caption...";
      }
      else {
         $caption = "";
      }


      if (length($subject) > $subject_length_max) {
         $subject = substr($subject,0,$subject_length_max);
         $subject .= "...";
      }

		# If threaded_main is on, then display threaded main

      my $r_thread = readdata("$maindir/$forum/$thread_dir/$num.$thread_ext");

      shift(@$r_thread);
      shift(@$r_thread);

		my $re_num = @{$r_thread};

      if ($r_setup->{'board_setup'}->{'display_caption'} eq "on") {

         $caption = trim($caption);
         $caption =~ s/&pip;/\|/g;
         $html_output .=qq~
            <TR BGCOLOR="$bgcolor"><TD ALIGN="LEFT" VALIGN="TOP">
            <img src="$imgurl/$thisimg">~;

			if ($re_num > $r_setup->{'board_setup'}->{'max_messages'}) {
   	         $html_output .=qq~
               <FONT SIZE="$font_size" COLOR="$font_color" FACE="$font_face">
               <a href="$exurl?az=show_thread&om=$num&forum=$forum&omm=0"><b>$subject</b></a>
               [<a href="$mainurl/$forum/$num\.$ext"><b>Display All</b></a>]
               $caption
               </FONT></TD>
      	      ~;
			}
			else {
            	$html_output .=qq~
               <FONT SIZE="$font_size" COLOR="$font_color" FACE="$font_face">
               <a href="$mainurl/$forum/$num\.$ext"><b>$subject</b></a>
               $caption</FONT>
               </TD>
            	~;
			}
   
         $ltime = time_format($ltime);
         $date = date_format($date);
   
         $html_output .=qq~
         <TD ALIGN="Left" VALIGN="TOP">
            <FONT SIZE="$font_size" COLOR="$font_color" FACE="$font_face">
            <b>$name</b><br>
            $date $ltime<br>
            <b>$replies</b> messages</FONT>
         </TD>
         </TR>
         ~;
			toggle_color();

      }
      else {

         $html_output .=qq~
            <TR BGCOLOR="$bgcolor"><TD ALIGN="LEFT" VALIGN="TOP">
            <img src="$imgurl/$thisimg">~;

			if ($re_num > $r_setup->{'board_setup'}->{'max_messages'}) {

   	         $html_output .=qq~
               <FONT SIZE="$font_size" COLOR="$font_color" FACE="$font_face">
               <a href="$exurl?az=show_thread&om=$num&forum=$forum&omm=0"><b>$subject</b></a>~;

	         if ($r_setup->{'forum_type'}->{$forum} eq "Private") {
   	         $html_output .=qq~
               [<a href="$exurl?az=show_thread&om=$num&forum=$forum"><b>Display&nbsp;All</b></a>]
      	      ~;
         	}
         	else {
            	$html_output .=qq~
               [<a href="$mainurl/$forum/$num\.$ext"><b>Display&nbsp;All</b></a>]
            	~;
         	}

   	         $html_output .=qq~
               $caption
               </FONT></TD>
      	      ~;
			}
			else {

	         if ($r_setup->{'forum_type'}->{$forum} eq "Private") {
	            $html_output .=qq~
               <FONT SIZE="$font_size" COLOR="$font_color" FACE="$font_face">
               <a href="$exurl?az=show_thread&om=$num&forum=$forum"><b>$subject</b></a>
               </FONT></TD>
   	         ~;
      	   }
         	else {
            	$html_output .=qq~
               <FONT SIZE="$font_size" COLOR="$font_color" FACE="$font_face">
               <a href="$mainurl/$forum/$num\.$ext"><b>$subject</b></a>
					</FONT></TD>
            	~;
         	}   
			}

         $ltime = time_format($ltime);
         $date = date_format($date);

         $html_output .=qq~
         <TD ALIGN="CENTER"  VALIGN="TOP"">
            <FONT SIZE="$font_size" COLOR="$font_color" FACE="$font_face">$name</FONT></TD>      
         <TD ALIGN="CENTER" VALIGN="TOP">
            <FONT SIZE="$font_size" COLOR="$font_color" FACE="$font_face">$date $ltime</FONT>
         </TD>
         <TD ALIGN="CENTER" VALIGN="TOP">
            <FONT SIZE="$font_size" COLOR="$font_color" FACE="$font_face">$replies</FONT>
         </TD>
         </TR>
         ~;
			toggle_color();

      }


  } #End of foreach row

	unless ($r_setup->{'board_setup'}->{'threaded_main_listing'}
				 eq "on" && $r_in->{'arc_database'} eq '') {

   	$html_output .=qq~
   	<TR><TD COLSPAN="4" BGCOLOR="$bg_color_0" ALIGN="CENTER">&nbsp;</TD></TR>
   	<TR><TD COLSPAN="4" ALIGN="RIGHT">
         <FONT SIZE="$font_size" COLOR="$font_color" FACE="$font_face">
			$temp_output</font></TD></TR></TABLE>	
  			</CENTER>~;

	}

   return ($heading, $sub_heading, $html_output);

}