###############################################################################
# BoardIndex.pl                                                               #
###############################################################################
# YaBB: Yet another Bulletin Board                                            #
# Open-Source Community Software for Webmasters                               #
# Version:        YaBB 2 RC 2                                                 #
# Released:       April 27, 2005                                              #
# Distributed by: http://www.yabbforum.com                                    #
# =========================================================================== #
# Copyright (c) 2000-2005 YaBB (www.yabbforum.com) - All Rights Reserved.     #
# Software by: The YaBB Development Team                                      #
#              with assistance from the YaBB community.                       #
# Sponsored by: Xnull Internet Media, Inc. - http://www.ximinc.com            #
#               Your source for web hosting, web design, and domains.         #
###############################################################################

#$| = 1;
$boardindexplver = 'YaBB 2 RC 2 $Revision: 1.79 $';
if($action eq 'detailedversion') { return 1; }

LoadLanguage("BoardIndex");
require "$templatesdir/$useboard/BoardIndex.template";

sub Del_Max_IM {
	fopen(DELMAXIM, "$memberdir/$username.msg");
	@messages = <DELMAXIM>;
	fclose(DELMAXIM);

	fopen(DELMAXIM, ">$memberdir/$username.msg", 1);
	for ($a = 0; $a < @messages; $a++) {
		chomp $messages[$a];
		if($a < $numibox) { print DELMAXIM "$messages[$a]\n"; }
   	}
   	fclose(DELMAXIM);
}

sub Del_Max_IMOUT {
	fopen(DELMAXIMOUT, "$memberdir/$username.outbox");
	@omessages = <DELMAXIMOUT>;
	fclose(DELMAXIMOUT);

	fopen(DELMAXIMOUT, ">$memberdir/$username.outbox", 1);
	for ($a = 0; $a < @omessages; $a++) {
		chomp $omessages[$a];
		if($a < $numobox) { print DELMAXIMOUT "$omessages[$a]\n"; }
   	}
   	fclose(DELMAXIMOUT);
}

sub Del_Max_STORE {
	fopen(DELMAXIMSTORE, "$memberdir/$username.imstore");
	@smessages = <DELMAXIMSTORE>;
	fclose(DELMAXIMSTORE);

	fopen(DELMAXIMSTORE, ">$memberdir/$username.imstore", 1);
	for ($a = 0; $a < @smessages; $a++) {
		chomp $smessages[$a];
		if($a < $numstore) { print DELMAXIMSTORE "$smessages[$a]\n"; }
   	}
   	fclose(DELMAXIMSTORE);
}

sub BoardIndex {
	my($lspostid,$lspostbd,$lssub,$lsposttime,$lsposter,$lsreply,$lsdatetime,$lastthreadtime,@goodboards,@loadboards);
	my($memcount, $latestmember) = &MembershipGet;
	$totalm = 0;
	$totalt = 0;
	$lastposttime =0;
	$lastthreadtime =0;

	$curforumurl = $curposlinks ? qq~<a href="$scripturl" class="nav">$mbname</a>~ : $mbname;
	if(!$INFO{'catselect'}) {
	} else {
		($tmpcat, $tmpmod, $tmpcol) = split(/\|/, $catinfo{$INFO{'catselect'}});
		$curforumurl = qq~<a href="$scripturl" class="nav">$mbname</a> &rsaquo; $tmpcat~;
	}

	if (!$iamguest){
		&Collapse_Load;
	}
	## first get all the boards based on the categories found in forum.master ##
	foreach $catid (@categoryorder){
		if($INFO{'catselect'} ne $catid  && $INFO{'catselect'}) { next; }
		$boardlist = $cat{$catid};
		(@bdlist) = split(/\,/, $boardlist);
		my ($catname, $catperms, $catallowcol) = split(/\|/, $catinfo{"$catid"});
		##Category Permissions Check
		my $access = &CatAccess($catperms);
		if (!$access){next;}
		$cat_boardcnt{$catid} = 0;
		## next determine all the boards a user has access to ##
		foreach $curboard (@bdlist) {
		## now fill all the neccesary hashes to show all board index stuff ##
			chomp $curboard;
			$cat_boardcnt{$catid}++;
			my ($boardname, $boardperms, $boardview) = split(/\|/, $board{"$curboard"});
			my $access = &AccessCheck($curboard, '', $boardperms);
			if (!$iamadmin && $access ne "granted" && $boardview != 1) {next;}
			push (@goodboards, "$catid|$curboard");
			push (@loadboards, $curboard);
		}
	}
	&BoardTotals("load",@loadboards);
	&getlog(@goodboards);
	foreach $curboard (@loadboards) {
		chomp $curboard;
		$lastposttime = ${$uid.$curboard}{'lastposttime'};
		${$uid.$curboard}{'lastposttime'} = ${$uid.$curboard}{'lastposttime'} eq 'N/A' || ! ${$uid.$curboard}{'lastposttime'} ? $boardindex_txt{'470'} : ${$uid.$curboard}{'lastposttime'};
		if(${$uid.$curboard}{'lastposttime'} != 0){$lastposttime{$curboard} = &timeformat(${$uid.$curboard}{'lastposttime'});} else {$lastposttime{$curboard} = $boardindex_txt{'470'};}
		$lastpostrealtime{$curboard} = ${$uid.$curboard}{'lastposttime'} eq 'N/A' || ! ${$uid.$curboard}{'lastposttime'} ? '' : ${$uid.$curboard}{'lastposttime'};
		$lsreply{$curboard} = ${$uid.$curboard}{'lastreply'}+ 1;
		if( ${$uid.$curboard}{'lastposter'} =~ m~\AGuest-(.*)~ ) {
			${$uid.$curboard}{'lastposter'} = $1;
			$lastposterguest{$curboard} = 1;
		}
		${$uid.$curboard}{'lastposter'} = ${$uid.$curboard}{'lastposter'} eq 'N/A' || ! ${$uid.$curboard}{'lastposter'} ? $boardindex_txt{'470'} : ${$uid.$curboard}{'lastposter'};
		${$uid.$curboard}{'messagecount'} = ${$uid.$curboard}{'messagecount'} || 0;
		${$uid.$curboard}{'threadcount'} = ${$uid.$curboard}{'threadcount'} || 0;
		$totalm += ${$uid.$curboard}{'messagecount'};
		$totalt += ${$uid.$curboard}{'threadcount'};
		## determine the true last post on all the boards a user has access to ##
		if ($lastposttime > $lastthreadtime){
			$lsdatetime = &timeformat($lastposttime);
			$lsposter = ${$uid.$curboard}{'lastposter'};
			$lssub = ${$uid.$curboard}{'lastsubject'};
			&ToChars($lssub);
			$lspostid = ${$uid.$curboard}{'lastpostid'};
			$lsreply = ${$uid.$curboard}{'lastreply'};
			$lastthreadtime = $lastposttime;
		}
	}
	foreach $catid (@categoryorder){
		if($INFO{'catselect'} ne $catid  && $INFO{'catselect'}) { next; }
		my ($catname, $catperms, $catallowcol) = split(/\|/, $catinfo{"$catid"});
		$cataccess = &CatAccess($catperms);
		if (!$cataccess){next;}
		if (!$iamguest) {
			my $newmsg = 0;
			$newms{$catname} = "";
			$newrowicon{$catname} = "";
			$newrowstart{$catname} = "";
			$newrowend{$catname} = "";

			# future: determine if there are new posts somewhere in this category, but only if it is collapsed

			if($catallowcol){
				$collapse_link = qq~<a href="$scripturl?action=collapse_cat;cat=$catid">~;
			}else{
				$collapse_link = qq~~;
			}

			## loop through any collapsed boards to find new posts in it and change the image to match ##
			if ( !$catcol{$catid} && $INFO{'catselect'} eq '') {

				foreach my $boardinfo (@goodboards) {
					chomp $boardinfo;
					my ($testcat);
					($testcat,$curboard)=split(/\|/,$boardinfo);

					if ($testcat ne $catid){next;}
					## as we fill the vars based on all boards we need to skip any cat already shown before ##
					($null, $null, $boardview) = split(/\|/, $board{"$curboard"}); 
					my $dlp = $yyuserlog{$curboard} ? $yyuserlog{$curboard} : 0;
					if( $max_log_days_old && ${$uid.$curboard}{'lastposttime'} ne $boardindex_txt{'470'} && !$iamguest && $dlp < $lastpostrealtime{$curboard} && ($dlp>$max_log_days_old*86400 || $dlp eq 0)  ) {
						unless ($boardview) { $newmsg = 1; }
					}
				}
				$newrowstart{$catname} = qq~<tr><td colspan="5" class="$new_msg_bg" height="18"><span class="$new_msg_class">~;
				$newrowend{$catname} = qq~</span></td></tr>~;
				if ( $newmsg ) {
					# future:
					if($catallowcol){
						$hash{$catname} = qq~ <img src="$imagesdir/cat_expand.gif" alt="$boardindex_exptxt{'1'}" border="0" /></a>~;
						$newrowicon{$catname} = qq~<img src="$imagesdir/on.gif" alt="$boardindex_txt{'333'}" border="0" style="margin-left: 4px; margin-right: 6px; vertical-align: middle;" />~;
						$newms{$catname} = qq~$boardindex_exptxt{'5'}~;
					}
				} else {
					# future:
					if($catallowcol){
						$hash{$catname} = qq~ <img src="$imagesdir/cat_expand.gif" alt="$boardindex_exptxt{'1'}" border="0" /></a>~;
						$newrowicon{$catname} = qq~<img src="$imagesdir/off.gif" alt="$boardindex_txt{'334'}" border="0" style="margin-left: 4px; margin-right: 6px; vertical-align: middle;" />~;
						$newms{$catname} = qq~$boardindex_exptxt{'6'}~;
					}
				}
			} else {
				if($catallowcol){
					if($INFO{'catselect'} ne '') {$collapse_link = ""; $hash{$catname} = "";}
					else {$hash{$catname} = qq~<img src="$imagesdir/cat_collapse.gif" alt="$boardindex_exptxt{'2'}" border="0" /></a>~;}
				}
			}
			if($cat_boardcnt{$catid} > 0) {
				$catlink = qq~$collapse_link $hash{$catname} <a href="$scripturl?catselect=$catid" title="$boardindex_txt{'797'} $catname">$catname</a>~;
			}
			else {
				$catlink = qq~<b>$catname</b>~;
			}
		} else {
			if($cat_boardcnt{$catid} > 0) {
				$catlink = qq~<a href="$scripturl?catselect=$catid">$catname</a>~;
			}
			else {
				$catlink = qq~<b>$catname</b>~;
			}
		}
		$templatecat = $catheader;
		$templatecat =~s/<yabb catlink>/$catlink/g;
		$templatecat =~ s/<yabb newmsg start>/$newrowstart{$catname}/g;
		$templatecat =~ s/<yabb newmsg icon>/$newrowicon{$catname}/g;
		$templatecat =~ s/<yabb newmsg>/$newms{$catname}/g;
		$templatecat =~ s/<yabb newmsg end>/$newrowend{$catname}/g;

		$tmptemplateblock .= $templatecat;
		## loop through any non collapsed boards to show the board index ##
		if( $catcol{$catid} || $INFO{'catselect'} ne '' || $iamguest){
			foreach my $boardinfo (@goodboards) {
				chomp $boardinfo;
				my ($testcat);
				($testcat,$curboard)=split(/\|/,$boardinfo);

				if ($testcat ne $catid){next;}
				## as we fill the vars based on all boards we need to skip any cat already shown before ##

				if (${$uid.$curboard}{'ann'} == 1) {${$uid.$curboard}{'pic'} = "ann.gif";}
				if (${$uid.$curboard}{'rbin'} == 1) {${$uid.$curboard}{'pic'} = "recycle.gif";}
				($boardname, $boardperms, $boardview) = split(/\|/, $board{"$curboard"});
				&ToChars($boardname);
				$INFO{'zeropost'} = 0; $zero = "";
				## hide the actual global announcement board for all normal users but admins and gmods ##
				if ($annboard eq $curboard && !$iamadmin && !$iamgmod) {next;}
				## hide the actual recycle bin board for all normal users but admins ##
				if ($annboard eq $curboard && $iamguest && !$iamadmin) {next;}
				$bdpic = ${$uid.$curboard}{'pic'};
				$bddescr = ${$uid.$curboard}{'description'};
				&ToChars($bddescr);
				$bdmods = ${$uid.$curboard}{'mods'};
				$bdmods =~ s/\, /\,/g;
				$bdmods =~ s/\ /\,/g;
				%moderators = ();
				foreach $curuser (split(/\,/, $bdmods)) {
					&LoadUser($curuser);
					$moderators{$curuser} = ${$uid.$curuser}{'realname'};
				}
				$showmods = '';
				if(scalar keys %moderators == 1) { $showmods = qq~$boardindex_txt{'298'}: ~; }
				elsif(scalar keys %moderators != 0) { $showmods = qq~$boardindex_txt{'63'}: ~; }
				while($tmpa = each(%moderators)) {
					&FormatUserName($tmpa);
					$showmods .= qq~<a href="$scripturl?action=viewprofile;username=$useraccount{$tmpa}">$moderators{$tmpa}</a>, ~;
				}
				$showmods =~ s/, \Z//;

				$bdmodgroups = ${$uid.$curboard}{'modgroups'};
				$bdmodgroups =~ s/\, /\,/g;
				%moderatorgroups = ();
				foreach $curgroup (split(/\,/, $bdmodgroups)) {
					$moderatorgroups{$curgroup} = $curgroup;
				}

				$showmodgroups = '';
				if(scalar keys %moderatorgroups == 1) { $showmodgroups = qq~$boardindex_txt{'298a'}: ~; }
				elsif(scalar keys %moderatorgroups != 0) { $showmodgroups = qq~$boardindex_txt{'63a'}: ~; }
				while($tmpa = each(%moderatorgroups)) {
					$showmodgroups .= qq~$moderatorgroups{$tmpa}, ~;
				}
				$showmodgroups =~ s/, \Z//;
				if($showmodgroups eq "" && $showmods eq "") { $showmodgroups = qq~<br />~; }
				if($showmodgroups ne "" && $showmods ne "") { $showmods .= qq~<br />~; }

				my $dlp = $yyuserlog{$curboard} ? $yyuserlog{$curboard} : 0;
				if( $max_log_days_old && ${$uid.$curboard}{'lastposttime'} ne $boardindex_txt{'470'} && !$iamguest && $dlp < $lastpostrealtime{$curboard} && ($dlp>$max_log_days_old*86400 || $dlp eq 0)) {
					$new = qq~<img src="$imagesdir/on.gif" alt="$boardindex_txt{'333'}" border="0" />~;
				}
				else { $new = qq~<img src="$imagesdir/off.gif" alt="$boardindex_txt{'334'}" border="0" />~; }
				if (${$uid.$curboard}{'ann'} == 1) {$new = qq~<img src="$imagesdir/ann.gif" alt="" border="0" />~;}
				if (${$uid.$curboard}{'rbin'} == 1) {$new = qq~<img src="$imagesdir/recycle.gif" alt="" border="0" />~;}
				$lastposter = ${$uid.$curboard}{'lastposter'};
				unless( $lastposterguest{$curboard} || ${$uid.$curboard}{'lastposter'} eq $boardindex_txt{'470'} ) {
					&LoadUser($lastposter);
					$registrationdate = ${$uid.$lastposter}{'regtime'};

					# No Need to open up each threadid.txt JUST to get last post time..
					# It's stored in forum.totals!
					#fopen (MNUM, "$datadir/${$uid.$curboard}{'lastpostid'}.txt");
					#@threadlist = <MNUM>; 
					#($dummy, $dummy, $dummy, $messagedate, $dummy) =  split(/\|/, $threadlist[${$uid.$curboard}{'lastreply'}],5);
					#fclose (MNUM);

					$messagedate = ${$uid.$curboard}{'lastposttime'};

					if((${$uid.$lastposter}{'regdate'} && $messagedate > $registrationdate) || ${$uid.$lastposter}{'status'} eq "Administrator" || ${$uid.$lastposter}{'status'} eq "Global Moderator") { $lastposter = qq~<a href="$scripturl?action=viewprofile;username=$lastposter">${$uid.$lastposter}{'realname'}</a>~; }
					else { $lastposter .= qq~ - $boardindex_txt{'470a'}~; }
				}
				${$uid.$curboard}{'lastposter'} ||= $boardindex_txt{'470'};
				${$uid.$curboard}{'lastposttime'} ||= $boardindex_txt{'470'};

				if ($bdpic =~ /\//i) {$bdpic = qq~ <img src="$bdpic" alt="" border="0" align="middle" /> ~;}
				elsif ($bdpic) {$bdpic = qq~ <img src="$imagesdir/$bdpic" alt="" border="0" align="middle" /> ~;}

				my $templateblock = $boardblock;
				my $lastpostlink = qq~<a href="$scripturl?num=${$uid.$curboard}{'lastpostid'}/${$uid.$curboard}{'lastreply'}#${$uid.$curboard}{'lastreply'}">$lastposttime{$curboard}</a>~;

				if(!$topiccut){$topiccut = 15;}
				my $lasttopictxt = ${$uid.$curboard}{'lastsubject'};
				$lasttopictxt =~ s/\A\[m\]/$maintxt{'758'}/;

				&LoadCensorList;	# Load Censor List
				# Censor subjects
				$lasttopictxt = &Censor($lasttopictxt);
				&ToChars($lasttopictxt);

				$convertstr = $lasttopictxt;
				$convertcut = $topiccut;
				&FromHTML($convertstr);
				&CountChars;
				my $lasttopictxt = $convertstr;
				&ToChars($lasttopictxt);

				if ($cliped){$lasttopictxt .= "...";}

				my $lasttopiclink = qq~<a href="$scripturl?num=${$uid.$curboard}{'lastpostid'}/${$uid.$curboard}{'lastreply'}#${$uid.$curboard}{'lastreply'}">$lasttopictxt</a>~;
				if(${$uid.$curboard}{'threadcount'}<0){${$uid.$curboard}{'threadcount'} = 0;}
				if(${$uid.$curboard}{'messagecount'}<0){${$uid.$curboard}{'messagecount'} = 0;}
				$templateblock =~ s/<yabb boardanchor>/$curboard/g;
				$templateblock =~ s/<yabb boardurl>/$scripturl\?board\=$curboard/g;
				$templateblock =~ s/<yabb new>/$new/g;
				$templateblock =~ s/<yabb boardpic>/$bdpic/g;
				$templateblock =~ s/<yabb boardname>/$boardname/g;
				$templateblock =~ s/<yabb boarddesc>/$bddescr/g;
				$templateblock =~ s/<yabb moderators>/$showmods$showmodgroups/g;
				$templateblock =~ s/<yabb threadcount>/${$uid.$curboard}{'threadcount'}/g;
				$templateblock =~ s/<yabb messagecount>/${$uid.$curboard}{'messagecount'}/g;
				$templateblock =~ s/<yabb lastpostlink>/$lastpostlink/g;
				$templateblock =~ s/<yabb lastposter>/$lastposter/g;
				$templateblock =~ s/<yabb lasttopiclink>/$lasttopiclink/g;

				$tmptemplateblock .= $templateblock;
			}
		}
		$tmptemplateblock .= $catfooter;
	++$catcount;
	}
	my $checkadded = 0;
	$users = qq~<span class="small">~;
	$guestlist = qq~<span class="small">~;
	$guests = 0;
	$numusers = 0;
	fopen(VARLOG, "$vardir/log.txt");
	@entries = <VARLOG>;
	fclose(VARLOG);
	foreach $curentry (@entries) {
		chomp $curentry;
		($name, $value, $last_ip) = split(/\|/, $curentry);
		if(!$last_ip){$last_ip = qq~</i></span><span class="error">$boardindex_txt{'no_ip'}</span><span class="small"><i>~;}
		if($name) {
			if(-e ("$memberdir/$name.vars")) {
				unless (${$uid.$name}{'realname'}) { &LoadUser($name);}
				&LoadMiniUser($name);
				$numusers++;
				$users .= qq~$link{$name}~;
				if(($iamadmin && $show_online_ip_admin) || ($iamgmod && $show_online_ip_gmod)) {
					$users .= qq~<i> ($last_ip)</i>~;
				}
				$users .= qq~, ~;
			}
			else { 
				$guests++; 
				if(($iamadmin && $show_online_ip_admin) || ($iamgmod && $show_online_ip_gmod)) {
					$guestlist .= qq~<i>$last_ip</i>, ~; 
				}
			}
		}
	}
	$users =~ s~, \Z~~;
	$users .= qq~</span>~;
	if($numusers) { $users .= qq~<br />~; }
	$guestlist =~ s~, \Z~~;
	$guestlist .= qq~</span>~;
	if(!$iamguest) {
		$ims = @immessages;

		if ($minnum > $numibox && $numibox ne "" && $enable_imlimit == 1) {
			$yymain .= qq~<script language="javascript" type="text/javascript"> if(confirm('$boardindex_imtxt{'11'} $minnum $boardindex_imtxt{'12'} $boardindex_txt{'316'}!\\n$numibox $boardindex_imtxt{'18'}')) { viewinstant(); } else cancel() </script>~;
			&Del_Max_IM;
		}

		if ($moutnum > $numobox && $numobox ne "" && $enable_imlimit == 1) {
			$yymain .= qq~<script language="javascript" type="text/javascript"> if(confirm('$boardindex_imtxt{'11'} $moutnum $boardindex_imtxt{'12'} $boardindex_txt{'320'}!\\n$numobox $boardindex_imtxt{'18'}')) { viewinstant(); } else cancel() </script>~;
			&Del_Max_IMOUT;
		}

		if ($storenum > $numstore && $numstore ne "" && $enable_imlimit == 1) {
			$yymain .= qq~<script language="javascript" type="text/javascript"> if(confirm('$boardindex_imtxt{'11'} $storenum $boardindex_imtxt{'12'} $boardindex_imtxt{'46'}!\\n$numstore $boardindex_imtxt{'18'}')) { viewinstant(); } else cancel() </script>~;
			&Del_Max_STORE;
		}

		$ims = qq~$boardindex_txt{'796'} <a href="$scripturl?action=im"><b>$ims</b></a> $boardindex_imtxt{'24'}: <a href="$scripturl?action=im"><b>$imnewcount</b></a>~;

            if ($INFO{'catselect'} eq ''){
			if ($colbutton) {
            		$collapselink = qq~$menusep<a href="$scripturl?action=collapse_all;status=0">$img{'collapse'}</a>~;
			}
			if (${$uid.$username}{'cathide'}) {
				$expandlink = qq~$menusep<a href="$scripturl?action=collapse_all;status=1">$img{'expand'}</a>~;
			}
            	$markalllink = qq~$menusep<a href="$scripturl?action=markallasread">$img{'markallread'}</a>~;
           	
           	} else {
	       	$markalllink = qq~$menusep<a href="$scripturl?action=markallasread;cat=$INFO{'catselect'}">$img{'markallread'}</a>~;
			$collapselink = ""; $expandlink = "";
       	}
	}
	if($INFO{'catselect'}) { &jumpto; }

	if($totalt<0){$totalt = 0;}
	if($totalm<0){$totalm = 0;}

	$guestson = qq~<span class="small">$boardindex_txt{'141'}: <b>$guests</b></span>~;
	$userson = qq~<span class="small">$boardindex_txt{'142'}: <b>$numusers</b></span>~;

	my $shared_login;
	if($iamguest) {
		require "$sourcedir/LogInOut.pl";
		$sharedLogin_title="";
		$shared_login = &sharedLogin;
	}

# Template it
	$boardindex_template =~ s/<yabb navigation>/$curforumurl/g;
	$boardindex_template =~ s/<yabb selecthtml>/$selecthtml/g;
	$boardindex_template =~ s/<yabb catsblock>/$tmptemplateblock/g;

	$boardhandellist =~ s/<yabb collapse>/$collapselink/g;
	$boardhandellist =~ s/<yabb expand>/$expandlink/g;
	$boardhandellist =~ s/<yabb markallread>/$markalllink/g;
	$boardhandellist =~ s/\Q$menusep//i;
	$boardindex_template =~ s/<yabb boardhandellist>/$boardhandellist/g;

	$boardindex_template =~ s/<yabb totaltopics>/$totalt/g;
	$boardindex_template =~ s/<yabb totalmessages>/$totalm/g;

	&LoadCensorList;	# Load Censor List

	if($Show_RecentBar) {

		$lssub = &Censor($lssub);
		&ToChars($lssub);


		$lastpostlink = qq~<a href="$scripturl?num=$lspostid/$lsreply#$lsreply">$lssub</a>~;
		$recentpostslink = qq~<a href="$scripturl?action=recent">$boardindex_txt{'792'}</a>~;
		$boardindex_template =~ s/<yabb lastpostlink>/$lastpostlink/g;
		$boardindex_template =~ s/<yabb recentposts>/$recentpostslink/g;
		$boardindex_template =~ s/<yabb lastpostdate>/$lsdatetime/g;
 	}
	else {
		$boardindex_template =~ s/<yabb lastpostlink>//g;
		$boardindex_template =~ s/<yabb recentposts>//g;
		$boardindex_template =~ s/<yabb lastpostdate>//g;
	}
	$membercountlink = qq~<a href="$scripturl?action=ml">$memcount</a>~;
	$boardindex_template =~ s/<yabb membercount>/$membercountlink/g;
	if ($showlatestmember) {
		&LoadUser($latestmember);
		$latestmemberlink = qq~<a href="$scripturl?action=viewprofile;username=$useraccount{$latestmember}">${$uid.$latestmember}{'realname'}</a>~;
		$boardindex_template =~ s/<yabb latestmember>/$latestmemberlink/g;
	}
	else {
		$boardindex_template =~ s/<yabb latestmember>//g;
	}
	$boardindex_template =~ s/<yabb ims>/$ims/g;
	$boardindex_template =~ s/<yabb guests>/$guestson/g;
	$boardindex_template =~ s/<yabb users>/$userson/g;
	$boardindex_template =~ s/<yabb onlineusers>/$users/g;
	$boardindex_template =~ s/<yabb onlineguests>/$guestlist/g;
	$boardindex_template =~ s/<yabb sharedlogin>/$shared_login/g;

	$yymain .= qq~$boardindex_template~;

	if($imnewcount > 0) {
		if($imnewcount > 1) { $en = "s"; $en2 = "$boardindex_imtxt{'47'}"; } else { $en = ""; $en2 = "$boardindex_imtxt{'48'}"; }

		chomp(${$uid.$username}{'im_imspop'});
		chomp(${$uid.$username}{'im_popup'});

		if(${$uid.$username}{'im_popup'} eq "on") {
			if(${$uid.$username}{'im_imspop'} eq "on") {
				$yymain .= qq~
	<script language="JavaScript" type="text/javascript">
	function rimpu() { window.open("$scripturl?action=im") }
	function cancel () { }
	</script>~;
			} else {
				$yymain .= qq~
	<script language="JavaScript" type="text/javascript">
	function rimpu() { location.href = ("$scripturl?action=im") }
	function cancel () { }
	</script>~;
			}

			$yymain .= qq~
	<script language="JavaScript" type="text/javascript">
	<!-- BEGIN
		if (confirm("$boardindex_imtxt{'14'} $imnewcount$boardindex_imtxt{'15'}?")) { rimpu(); } else cancel()
	// END -->
	</script>
		~;
		}
	}

	if($userimcfg[5] eq "on") {
		$yymain .= qq~<script language="javascript" type="text/javascript"> function viewinstant() { window.open("$scripturl?action=im") } </script>~;
	} else {
		$yymain .= qq~<script language="javascript" type="text/javascript"> function viewinstant() { location.href = ("$scripturl?action=im") } </script>~;
	}

	$yytitle = "$boardindex_txt{'18'}";
	&template;
	exit;
}

sub Collapse_Load {
	my (@userhide,$hidden);
	$colbutton = 1;
	my $i = 0;
	@userhide = split(/\,/, ${$uid.$username}{'cathide'});
	foreach my $key (@categoryorder){
		my ($catname, $catperms, $catallowcol) = split(/\|/, $catinfo{$key});
		$access = &CatAccess($catperms);
		if($catallowcol == 1 && $access) {$i++;}
		$catcol{$key} = 1;
		foreach $hidden(@userhide){
			chomp $hidden;
			if ($catallowcol == 1 && $key eq $hidden){$catcol{$key} = 0;}
		}
	}
	if($i == @userhide) {$colbutton = 0;}
$colloaded = 1;
}

sub Collapse_Write {
	my @userhide;
	# rewrite the category hash for the user
	foreach my $key (@categoryorder){
		my ($catname, $catperms, $catallowcol) = split(/\|/, $catinfo{$key});
		$access = &CatAccess($catperms);
		if ($catcol{$key} == 0 && $access) {push(@userhide,$key);}
	}
	${$uid.$username}{'cathide'} = join(",",@userhide);
	&UserAccount($username,"update");
	if (-e "$memberdir/$username.cat"){unlink "$memberdir/$username.cat";}
}

sub Collapse_Cat {
	if ($iamguest){ &fatal_error($boardindex_exptxt{'4'}); }
	my $changecat = $INFO{'cat'};
	unless($colloaded){ &Collapse_Load;}

	if ( $catcol{$changecat} eq 1 ) {
		$catcol{$changecat} = 0;
	}
	else {
		$catcol{$changecat} = 1;
	}
	&Collapse_Write;
	$yySetLocation = qq~$scripturl~; 
	&redirectexit; 
}

sub Collapse_All {
	my ($state,@catstatus);
	$state = $INFO{'status'};

	if ($iamguest){ &fatal_error($boardindex_exptxt{'4'}); }
	if ( $state != 1 && $state != 0 ){ &fatal_error($boardindex_exptxt{'7'}); }

	foreach my $key (@categoryorder){
		my ($catname, $catperms, $catallowcol) = split(/\|/, $catinfo{$key});
		if($catallowcol eq '1'){
			$catcol{$key} = $state;
		} else {
			$catcol{$key} = 1;
		}
	}
	&Collapse_Write;

	$yySetLocation = qq~$scripturl~; 
	&redirectexit; 
}

sub MarkAllRead {
	unless ($mloaded == 1){require "$boardsdir/forum.master";}

	CAT:foreach my $catid (@categoryorder){
		if($INFO{'cat'} ne ''){
			if($INFO{'cat'} eq $catid){
				$boardlist = $cat{$catid};
				(@bdlist) = split(/\,/, $boardlist);
				($catname, $catperms) = split(/\|/, $catinfo{"$catid"});
				##Category Permissions Check
				$cataccess = &CatAccess($catperms);
				if (!$cataccess){next;}
				BOARD:foreach $curboard (@bdlist) {
					#die $curboard unless $curboard eq 'general';
					chomp $curboard;
					&modlog("$curboard--mark");
					&modlog($curboard);
					#die if $curboard ne 'general';
					next BOARD;
				}
				last CAT;
			} else {
				next CAT;
			}
		} else {
			$boardlist = $cat{$catid};
			(@bdlist) = split(/\,/, $boardlist);
			($catname, $catperms) = split(/\|/, $catinfo{"$catid"});
			##Category Permissions Check
			$cataccess = &CatAccess($catperms);
			if (!$cataccess){next;}
			BOARD:foreach $curboard (@bdlist) {
				chomp $curboard;
				&modlog("$curboard--mark");
				&modlog($curboard);
				next BOARD;
			}
		}
	}
	&dumplog;
	$yySetLocation = qq~$scripturl~; 
	&redirectexit; 
}

1;


