###############################################################################
# Recent.pl                                                                   #
###############################################################################
# YaBB: Yet another Bulletin Board                                            #
# Open-Source Community Software for Webmasters                               #
# Version:        YaBB 2 RC 1                                                 #
# Released:       December 27, 2004                                           #
# 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.         #
###############################################################################

$recentplver = 'YaBB 2 RC 1 $Revision: 1.14 $';
if($action eq 'detailedversion') { return 1; }

sub RecentTopics {
	my $display = $INFO{'display'} ||= 10;
	my( @memset, @categories, %data, $numfound, $curcat, %catname, %cataccess, %catboards, $openmemgr, @membergroups, %openmemgr, $curboard, @threads, @boardinfo, $i, $c, @messages, $tnum, $tsub, $tname, $temail, $tdate, $treplies, $tusername, $ticon, $tstate, $mname, $memail, $mdate, $musername, $micon, $mattach, $mip, $mns, $mtime, $counter, $board, $notify );

	$numfound=0;

	unless ($mloaded == 1) { require "$boardsdir/forum.master"; }
	foreach $catid (@categoryorder){$boardlist = $cat{$catid};
		
		(@bdlist) = split(/\,/, $boardlist);
		($catname, $catperms) = split(/\|/, $catinfo{"$catid"});
		$cataccess = &CatAccess($catperms);
		if (!$cataccess){next;}

		foreach $curboard (@bdlist) {
			($boardname{$curboard}, $boardperms, $boardview) = split(/\|/, $board{"$curboard"});
			
			my $access = &AccessCheck($curboard, '', $boardperms);
			if (!$iamadmin && $access ne "granted" &&  $boardview != 1) {next;}
			
			$catname{$curboard} = $catname;

			fopen(REC_BDTXT, "$boardsdir/$curboard.txt");
				for ($i = 0; $i < $display && ($buffer = <REC_BDTXT>); $i++) {
					($tnum, $tsub, $tname, $temail, $tdate, $treplies, $tusername, $ticon, $tstate) = split( /\|/, $buffer );
					unless($tstate =~ /h/){
						$mtime = $tdate;
						$data[$numfound] = "$mtime|$curboard|$tnum|$treplies|$tusername|$tname|$tstate";
						$numfound++;
					}
				}
			fclose(REC_BDTXT);
		}
	}

	@data = reverse sort @data;
	$numfound=0;

	for ($i = 0; $i < @data; $i++) {
		($mtime, $curboard, $tnum, $treplies, $tusername, $tname, $tstate) = split( /\|/, $data[$i] );
		unless($tstate =~ /h/){
			fopen(REC_THRETXT, "$datadir/$tnum.txt") || next;
			while( <REC_THRETXT> ) { $message = $_; }
			# get only the last post for this thread.
			fclose(REC_THRETXT);
			chomp $message;

			if( $message ) {
				($msub, $mname, $memail, $mdate, $musername, $micon, $mattach, $mip, $message, $mns) = split(/\|/,$message);
				$messages[$numfound] = "$curboard|$tnum|$treplies|$tusername|$tname|$msub|$mname|$memail|$mdate|$musername|$micon|$mattach|$mip|$message|$mns|$tstate";
				$numfound++;
			}
			if ($numfound == $display) {last;}
		}
	}

	if( $numfound>0 ) {
		$counter = 1;
		# Load Censor List
		&LoadCensorList;
	}
	else {
		$yymain .= qq~<hr class="hr" /><b>$txt{'170'}</b><hr />~;
	}
	for( $i = 0; $i < $numfound; $i++ ) {
		($board, $tnum, $c, $tusername, $tname, $msub, $mname, $memail, $mdate, $musername, $micon, $mattach, $mip, $message, $mns, $tstate) = split( /\|/, $messages[$i] );
		$displayname = $mname;

		if( $tusername ne 'Guest' && -e "$memberdir/$tusername.vars" ) {
			&LoadUser($tusername);
			$tname = exists ${$uid.$tusername}{'realname'} ? ${$uid.$tusername}{'realname'} : $tname;
			$tname ||= $txt{'470'};
			$tname = qq~<a href="$scripturl?action=viewprofile;username=$useraccount{$tusername}" title="$txt{'27'}: $musername">$tname</a>~;
		}
		if( $musername ne 'Guest' && -e "$memberdir/$musername.vars" ) {
			&LoadUser($musername);
			$mname = exists ${$uid.$musername}{'realname'} ? ${$uid.$musername}{'realname'} : $mname;
			$mname ||= $txt{'470'};
			$mname = qq~<a href="$scripturl?action=viewprofile;username=$useraccount{$musername}" title="$txt{'27'}: $musername">$mname</a>~;
		}
		$message = &Censor($message);
		$msub = &Censor($msub);
		&wrap;
		if($enable_ubbc) { $ns = $mns; if(!$yyYaBBCloaded) { require "$sourcedir/YaBBC.pl"; } &DoUBBC; }
		&wrap2;
		&ToChars($msub);
		&ToChars($message);
		&ToChars($boardname{$board});

		if($enable_notification) { $notify = qq~$menusep<a href="$scripturl?board=$board;action=notify;thread=$tnum/$startnum">$img{'notify'}</a>~; }
		$mdate = &timeformat($mdate);
		$yymain .= qq~
<table border="0" width="100%" cellspacing="1" class="bordercolor" style="table-layout: fixed;">
  <tr>
    <td width="5%" align="center" class="titlebg">$counter</td>
    <td width="95%" class="titlebg">&nbsp;$catname{$board} / $boardname{$board} / <a href="$scripturl?num=$tnum/$c#$c"><u>$msub</u></a><br />
    &nbsp;<span class="small">$maintxt{'30'}: $mdate&nbsp;</span></td>
  </tr><tr>
    <td colspan="2" class="catbg">$maintxt{'109'} $tname | $maintxt{'22'} $maintxt{'525'} $mname</td>
  </tr><tr>
    <td colspan="2" class="windowbg2" valign="top"><div style="max-height: 150px; width: 100%; overflow: auto;">$message</div></td>
  </tr><tr>
    <td colspan="2" class="catbg" align="right">
~;
if ($tstate != 1)
{
	$yymain .= qq~<a href="$scripturl?board=$board;action=post;num=$tnum/$c#$c;title=Post+reply">$img{'reply'}</a>$menusep<a href="$scripturl?board=$board;action=post;num=$tnum;quote=$c;title=Post+reply">$img{'recentquote'}</a>$notify &nbsp;~;
}
$yymain .= qq~
    </td>
  </tr>
</table><br />
~;
		++$counter;
	}

	$yymain .= qq~
<font size="1"><a href="$cgi">$txt{'236'}</a> $txt{'237'}<br /></font>
~;
	$yytitle = $txt{'214'};
	&template;
	exit;
}

sub RecentPosts {
	my $display = $INFO{'display'} ||= 10;
	my( @memset, @categories, %data, $numfound, $curcat, %catname, %cataccess, %catboards, $openmemgr, @membergroups, %openmemgr, $curboard, @threads, @boardinfo, $i, $c, @messages, $tnum, $tsub, $tname, $temail, $tdate, $treplies, $tusername, $ticon, $tstate, $mname, $memail, $mdate, $musername, $micon, $mattach, $mip, $mns, $mtime, $counter, $board, $notify );

	$numfound=0;

	unless ($mloaded == 1) { require "$boardsdir/forum.master"; }
	foreach $catid (@categoryorder){$boardlist = $cat{$catid};
		
		(@bdlist) = split(/\,/, $boardlist);
		($catname, $catperms) = split(/\|/, $catinfo{"$catid"});
		$cataccess = &CatAccess($catperms);
		if (!$cataccess){next;}

		foreach $curboard (@bdlist) {
			($boardname{$curboard}, $boardperms, $boardview) = split(/\|/, $board{"$curboard"});
			
			my $access = &AccessCheck($curboard, '', $boardperms);
			if (!$iamadmin && $access ne "granted" &&  $boardview != 1) {next;}
			
			$catname{$curboard} = $catname;

			fopen(REC_BDTXT, "$boardsdir/$curboard.txt");
				for ($i = 0; $i < $display && ($buffer = <REC_BDTXT>); $i++) {
					($tnum, $tsub, $tname, $temail, $tdate, $treplies, $tusername, $ticon, $tstate) = split( /\|/, $buffer );
					unless($tstate =~ /h/){
						$mtime = $tdate;
						$data[$numfound] = "$mtime|$curboard|$tnum|$treplies|$tusername|$tname|$tstate";
						$numfound++;
					}
				}
			fclose(REC_BDTXT);
		}
	}

	@data = reverse sort @data;

	$numfound=0;
	$threadfound = @data > $display ? $display : @data;

	for ($i = 0; $i < $threadfound; $i++) {
		($mtime, $curboard, $tnum, $treplies, $tusername, $tname, $tstate) = split( /\|/, $data[$i] );
		unless($tstate =~ /h/){

			fopen(REC_THRETXT, "$datadir/$tnum.txt") || next;
			@mess = <REC_THRETXT>;
			fclose(REC_THRETXT);

			$threadfrom = @mess > $display ? @mess-$display : 0;
			for ($ii = $threadfrom; $ii < @mess+1; $ii++) {
				if( $mess[$ii] ) {
					($msub, $mname, $memail, $mdate, $musername, $micon, $mattach, $mip, $message, $mns) = split(/\|/,$mess[$ii]);
					$mtime = $mdate;
					$messages[$numfound] = "$mtime|$curboard|$tnum|$ii|$tusername|$tname|$msub|$mname|$memail|$mdate|$musername|$micon|$mattach|$mip|$message|$mns|$tstate"; 
					$numfound++;
				}
			}
		}
	}

	@messages = reverse sort @messages;

	if( $numfound>0 ) {
		if( $numfound>$display ) { $numfound=$display; }
		$counter = 1;
		# Load Censor List
		&LoadCensorList;
	}
	else {
		$yymain .= qq~<hr class="hr"><b>$txt{'170'}</b><hr>~;
	}
	for( $i = 0; $i < $numfound; $i++ ) {
		($dummy, $board, $tnum, $c, $tusername, $tname, $msub, $mname, $memail, $mdate, $musername, $micon, $mattach, $mip, $message, $mns, $tstate) = split( /\|/, $messages[$i] );
		$displayname = $mname;

		if( $tusername ne 'Guest' && -e "$memberdir/$tusername.vars" ) {
			&LoadUser($tusername);
			$tname = exists ${$uid.$tusername}{'realname'} ? ${$uid.$tusername}{'realname'} : $tname;
			$tname ||= $txt{'470'};
			$tname = qq~<a href="$scripturl?action=viewprofile;username=$useraccount{$tusername}" title="$txt{'27'}: $musername">$tname</a>~;
		}
		if( $musername ne 'Guest' && -e "$memberdir/$musername.vars" ) {
			&LoadUser($musername);
			$mname = exists ${$uid.$musername}{'realname'} ? ${$uid.$musername}{'realname'} : $mname;
			$mname ||= $txt{'470'};
			$mname = qq~<a href="$scripturl?action=viewprofile;username=$useraccount{$musername}" title="$txt{'27'}: $musername">$mname</a>~;
		}
		$message = &Censor($message);
		$msub = &Censor($msub);
		&wrap;
		if($enable_ubbc) { $ns = $mns; if(!$yyYaBBCloaded) { require "$sourcedir/YaBBC.pl"; } &DoUBBC; }
		&wrap2;
		&ToChars($msub);
		&ToChars($message);
		&ToChars($boardname{$board});
		if($enable_notification) { $notify = qq~$menusep<a href="$scripturl?board=$board;action=notify;thread=$tnum/$startnum">$img{'notify'}</a>~; }
		$mdate = &timeformat($mdate);
		$yymain .= qq~
<table border="0" width="100%" cellspacing="1" class="bordercolor" style="table-layout: fixed;">
  <tr>
    <td width="5%" align="center" class="titlebg">$counter</td>
    <td width="95%" class="titlebg">&nbsp;$catname{$board} / $boardname{$board} / <a href="$scripturl?num=$tnum/$c#$c"><u>$msub</u></a><br />
    &nbsp;<span class="small">$maintxt{'30'}: $mdate&nbsp;</span></td>
  </tr><tr>
    <td colspan="2" class="catbg">$maintxt{'109'} $tname | $maintxt{'22'} $maintxt{'525'} $mname</td>
  </tr><tr>
    <td colspan="2" class="windowbg2" valign="top">$message</td>
  </tr><tr>
    <td colspan="2" class="catbg" align="right">
~;
if ($tstate != 1)
{
	$yymain .= qq~<a href="$scripturl?board=$board;action=post;num=$tnum/$c#$c;title=Post+reply">$img{'reply'}</a>$menusep<a href="$scripturl?board=$board;action=post;num=$tnum;quote=$c;title=Post+reply">$img{'recentquote'}</a>$notify &nbsp;~;
}
$yymain .= qq~
    </td>
  </tr>
</table><br />
~;
		++$counter;
	}

	$yymain .= qq~
<font size="1"><a href="$cgi">$txt{'236'}</a> $txt{'237'}<br /></font>
~;
	$yytitle = $txt{'214'};
	&template;
	exit;
}

sub LastPost {
	my $lastboard = $_[0];
	if ( (!$lastboard) || (! -e "$boardsdir/$lastboard.ttl") ) {
		$numfound=0;
		foreach $catid (@categoryorder){$boardlist = $cat{$catid};
			(@bdlist) = split(/\,/, $boardlist);
			($catname, $catperms) = split(/\|/, $catinfo{"$catid"});
			$cataccess = &CatAccess($catperms);
			if (!$cataccess){next;}

			foreach $curboard (@bdlist) {
				($boardname{$curboard}, $boardperms, $boardview) = split(/\|/, $board{"$curboard"});
			
				my $access = &AccessCheck($curboard, '', $boardperms);
				if (!$iamadmin && $access ne "granted" &&  $boardview != 1) {next;}
				my($threadcount, $messagecount, $lastposttime, $lastposter, $lastpostid, $lastreply, $lastsub ) = &BoardCountGet($curboard);
			}
		}
		$mydatetime = &timeformat($lastposttime);
	}
	else {
		my($threadcount, $messagecount, $lastposttime, $lastposter, $lastpostid,$lastreply, $lastsub ) = &BoardCountGet($lastboard);
		$mydatetime = &timeformat($lastposttime);
	}
	$yymain .= qq~<br />$boardindex_txt{'235'}: <a href="$scripturl?num=$lastpostid/$lastreply#$lastreply"><b>$lastsub</b></a> ($mydatetime)<br />$boardindex_txt{'791'} <a href="$scripturl?action=recent">$boardindex_txt{'792'}</a> $boardindex_txt{'793'}~;
}

sub RecentTopicsList {
	my $display = $_[0];
	if ( $display ) { $look = 'bindex'; }
	else {
		$look = 'simple';
		$display = $INFO{'display'} ||= 10;
		if ( $display < 0 ) { $display=5; }
		elsif ( $display > 100 ) { $display=100; }
	}

	my( @memset, @categories, %data, $numfound, $curcat, %catname, %cataccess, %catboards, $openmemgr, @membergroups, %openmemgr, $curboard, @threads, @boardinfo, $i, $c, @messages, $tnum, $tsub, $tname, $temail, $tdate, $treplies, $tusername, $ticon, $tstate, $mname, $memail, $mdate, $musername, $micon, $mattach, $mip, $mns, $mtime, $counter, $board, $notify );
	$numfound=0;

	foreach $catid (@categoryorder){$boardlist = $cat{$catid};
		
		(@bdlist) = split(/\,/, $boardlist);
		($catname, $catperms) = split(/\|/, $catinfo{"$catid"});
		$cataccess = &CatAccess($catperms);
		if (!$cataccess){next;}

		foreach $curboard (@bdlist) {
			($boardname{$curboard}, $boardperms, $boardview) = split(/\|/, $board{"$curboard"});
			
			my $access = &AccessCheck($curboard, '', $boardperms);
			if (!$iamadmin && $access ne "granted" &&  $boardview != 1) {next;}
			
			$catname{$curboard} = $catname;

			fopen(REC_BDTXT, "$boardsdir/$curboard.txt");
				for ($i = 0; $i < $display && ($buffer = <REC_BDTXT>); $i++) {
					($tnum, $tsub, $tname, $temail, $tdate, $treplies, $tusername, $ticon, $tstate) = split( /\|/, $buffer );
					unless($tstate =~ /h/){
						$mtime = $tdate;
						$data[$numfound] = "$mtime|$curboard|$tnum|$treplies";
						$numfound++;
					}
				}
			fclose(REC_BDTXT);
		}
	}

	@data = reverse sort @data;
	$numfound=0;

	for ($i = 0; $i < @data; $i++) {
		($mtime, $curboard, $tnum, $treplies) = split( /\|/, $data[$i] );

		fopen(REC_THRETXT, "$datadir/$tnum.txt") || next;
		while( <REC_THRETXT> ) { $message = $_; }
		# get only the last post for this thread.
		fclose(REC_THRETXT);
		chomp $message;

		if( $message ) {
			($msub, $mname, $memail, $mdate, $musername, $micon, $mattach, $mip, $message, $mns) = split(/\|/,$message);
			$messages[$numfound] = "$curboard|$tnum|$treplies|$msub|$mname|$mdate|$musername";
			$numfound++;
		}
		if ($numfound == $display) {last;}
	}

	# Load Censor List
	&LoadCensorList;

	$counter = 1;

	if ( $look eq 'bindex') {
$yymain .= qq~
	<td align="left" class="catbg" colspan="2"> <font class="catbg"><b>$display most recent topics</b></font></td>
         </tr><tr>
           <td class="windowbg" width="20" valign="middle" align="center"><img src="$imagesdir/xx.gif" border="0" alt="" /></td>
           <td class="windowbg2" valign="top" align="center">
            <table width="98%"  align="center">
~;
		for( $i = 0; $i < $numfound; $i++ ) {
			($board, $tnum, $c, $msub, $mname, $mdate, $musername) = split( /\|/, $messages[$i] );
			$msub = &Censor($msub); 
			&ToChars($msub);
			&ToChars($boardname{$board});

			if( $musername ne 'Guest' && -e "$memberdir/$musername.vars" ) {
				&LoadUser($musername);
				$mname = exists ${$uid.$musername}{'realname'} ? ${$uid.$musername}{'realname'} : $mname;
				$mname ||= $txt{'470'};
				$mname = qq~<a href="$scripturl?action=viewprofile;username=$useraccount{$musername}" title="$txt{'27'}: $musername">$mname</a>~;
			}
			$mdate = &timeformat($mdate);

$yymain .= qq~
              </tr><tr>
              <td valign="top" align="right" width="35%"><font size="1">[&nbsp$catname{$board} / $boardname{$board}&nbsp]&nbsp</td>
       	      <td valign="top" align="left" width="40%"><font size="1"><a href="$scripturl?num=$tnum/$c#$c" class="nav" target="_parent">$msub</a> $txt{'525'} $mname</td>
              <td valign="top" align="left" width="25%"><font size="1">$mdate</td>
~;
			++$counter;
		}

$yymain .= qq~
             </tr>	
            </table>
           <font size="1"><a href="$scripturl?action=recentlist;display=50">...more...</a><br /></font>
           </td>
	  </tr>
~;
	}
	else {
$yymain .= qq~
    <table cellpadding="4" cellspacing="1" border="0" width="100%" class="bordercolor" style="table-layout: fixed;">
      <tr>
        <td class="titlebg" align="center" colspan="2">
        <font class="text1"><b>$display most recent topics</b></font></td>
      </tr>
      <tr>
        <td class="windowbg2" valign="top" align="center">
        <table width="98%" cellpadding="0" align="center">
~;
		for( $i = 0; $i < $numfound; $i++ ) {
			($board, $tnum, $c, $msub, $mname, $mdate, $musername) = split( /\|/, $messages[$i] );
			$msub = &Censor($msub); 
			&ToChars($msub);
			if( $musername ne 'Guest' && -e "$memberdir/$musername.vars" ) {
				&LoadUser($musername);
				$mname = exists ${$uid.$musername}{'realname'} ? ${$uid.$musername}{'realname'} : $mname;
				$mname ||= $txt{'470'};
				$mname = qq~<a href="$scripturl?action=viewprofile;username=$useraccount{$musername}" title="$txt{'27'}: $musername">$mname</a>~;
			}
			$mdate = &timeformat($mdate);
$yymain .= qq~
          <tr>
            <td valign="top" align="right" ><font size="1">
                $counter.
            </td>
             <td valign="top" align="right" width="35%"><font size="1">
                $catname{$board} » $boardname{$board}&nbsp»&nbsp
            </td>
       	    <td valign="top" align="left" width="40%"><font size="1">
                <a href="$scripturl?num=$tnum/$c#$c" class="nav" target="_parent">$msub</a> $txt{'525'} $mname
            </td>
       	    <td valign="top" align="left" width="25%"><font size="1">
		$mdate
            </td>
          </tr>
~;
			++$counter;
		}
$yymain .= qq~
        </table>
      </table><br />
~;
	&template;
	exit;
	}
}

1;