#############################################################
#  LeoBoard ver.5000 / LB5000 / 雷傲超級論壇 ver.5000
#
#  版權所有: 雷傲工作室(原藍寶石軟件工作室)
#
#  製作人  : 山鷹糊 (Shining Hu)
#            花無缺 (Ifairy Han)
#           
#  首頁網址: http://www.CGIer.com/      CGI 編程者之家
#	     http://www.LeoBoard.com/   雷傲論壇支援首頁
#	     http://www.leoBBS.com/     本論壇直通車
#            
#############################################################
sub rebuildLIST {
    my %IN = ( -Forum  => "", @_, );
    undef @sortdat;
    opendir (DIR, "${lbdir}forum$IN{-Forum}");
    my @dirdata = readdir(DIR);
    closedir (DIR);
    @dirdata = grep(/\.thd\.cgi$/,@dirdata);
    foreach (@dirdata) {
	(my $id, my $tr) = split(/\./,$_);
	my $rr = &readthreadpl($IN{-Forum},$id);
	push (@sortdat, $rr) if ($rr ne "");
    }
    undef @dirdata;
    @sortdat = sort({$a<=>$b}@sortdat);
    @sortdat = reverse(@sortdat);
    my $allposts   = 0;
    my $allthreads = 0;
    my $file = "$lbdir" . "boarddata/list$IN{-Forum}.cgi";
    &winlock($file) if (($OS_USED eq "Nt")||($OS_USED eq "Unix"));
    if (open (LIST, ">$file")) {
        flock (LIST, 2) if ($OS_USED eq "Unix");
        foreach (@sortdat) {
            chomp $_;
            $_ =~ s/[\a\f\n\e\0\r]//isg;
            (my $lastpostdate, my $topicid, my $topictitle, my $topicdescription, my $threadstate, my $threadposts, my $threadviews, my $startedby, my $startedpostdate, my $lastposter, my $posticon, my $posttemp) = split (/\t/,$_);
            next if (($topicid !~ /^[0-9]+$/)||($topictitle eq ""));
            if (($posticon =~ m/<br>/i)&&($threadstate ne "poll")&&($threadstate ne "pollclosed")) { $threadstate = "poll"; }
                else { $threadstate="open" if ($threadstate eq ""); }
	    print LIST "$topicid\t$topictitle\t$topicdescription\t$threadstate\t$threadposts\t$threadviews\t$startedby\t$startedpostdate\t$lastposter\t$lastpostdate\t$posticon\t$posttemp\t\n";
	    $allposts = $allposts + $threadposts;
	    $allthreads ++ ;
        }
        close (LIST);
    }
    &winunlock($file) if (($OS_USED eq "Nt")||($OS_USED eq "Unix"));
    return ("$allthreads|$allposts");
}

sub readthreadpl {
        my($inforum,$id) = @_;
	open (PLFILE, "${lbdir}forum$inforum/$id.pl");
	my $topicinfo = <PLFILE>;
	close (PLFILE);
	chomp $topicinfo;
        $topicinfo =~ s/[\a\f\n\e\0\r]//isg;
	(my $topicid, my $topictitle, my $topicdescription, my $threadstate, my $threadposts, my $threadviews, my $startedby, my $startedpostdate, my $lastposter, my $lastpostdate, my $posticon, my $inposttemp) = split (/\t/,$topicinfo);
	$maxsavepost = 24 if ($maxsavepost < 24);
	$maxsavepost = 99 if ($maxsavepost > 99);
	if (($topictitle eq "")||($startedby eq "")||($startedpostdate eq "")||($threadposts eq "")){
	    open (THDFILE, "${lbdir}forum$inforum/$id.thd.cgi");
	    my @topicall = <THDFILE>;
	    close (THDFILE);
            my $topicall = @topicall;
	    my $postfirst = $topicall[0];
            $postfirst =~ s/[\a\f\n\e\0\r]//isg;
	    my $postlast = $topicall[-1];
            $postlast =~ s/[\a\f\n\e\0\r]//isg;
	    (my $membername1, my $topictitle1, my $postipaddress1, my $showemoticons1, my $showsignature1, my $postdate1, my $post1, my $posticon1) = split(/\t/,$postfirst);
	    (my $membername2, my $topictitle2, my $postipaddress2, my $showemoticons2, my $showsignature2, my $postdate2, my $post2, my $posticon2) = split(/\t/,$postlast);
 	    $topictitle1 =~ s/^＊＃！＆＊//;
	    $threadviews = ($topicall+1) * 8 if ($threadviews eq "");
	    $membername2 = "" if ($topicall eq 0);
	    $post2       = "(保密)" if ($post2=~/LBHIDDEN\[(.*?)\]LBHIDDEN/sg);
	    $post2       = "(保密)" if ($post2=~/LBSALE\[(.*?)\]LBSALE/sg);
            $post2       = &temppost($post2);
	    $post2       = &lbhz($post2,$maxsavepost);

	    chomp $posticon1;
	    $posticon1 =~ s/\s//isg;
	    if ($posticon1 =~/<br>/i) {
      		$posticon1 =~ s/<br>/\t/ig;
      		my @temppoll = split(/\t/, $posticon1);
      		if ($#temppoll >= 1) { $posticon1 = "<br>"; } else { $posticon1 = ""; }
	    }
	    else { $posticon1 = $posticon2 if ($sortposticonshow eq "yes"); }
            if (($posticon =~ m/<br>/i)&&($threadstate ne "poll")&&($threadstate ne "pollclosed")) { $threadstate = "poll"; }
                else { $threadstate="open" if ($threadstate eq ""); }
            if ($topictitle1 eq "") { return ""; }
	      else {
		if (($dofilter eq "yes")||($dofilter eq "all")) {
	    	    my $temp = &postwordfilter("$topictitle1\t$topicdescription\t$post2");
	    	    ($topictitle1,$topicdescription,$post2) = split(/\t/,$temp);
		}
		my $line = "$postdate2\t$id\t$topictitle1\t$topicdescription\t$threadstate\t$topicall\t$threadviews\t$membername1\t$postdate1\t$membername2\t$posticon1\t$post2\t";
                $line =~ s/[\a\f\n\e\0\r]//isg;
	        return ("$line");
	    }
	}
	else {
	    $topictitle =~ s/^＊＃！＆＊//;
	    $posticon =~ s/\s//isg;
	    if ($posticon =~/<br>/i) { $posticon = "<br>"; }
   	    $threadviews = ($threadposts+1) * 8 if ($threadviews eq "");
#   	    $threadviews = 9999 if ($threadviews > 9999);
            if ($topictitle eq "") { return ""; }
	      else {
		if (($dofilter eq "yes")||($dofilter eq "all")) {
	    	    my $temp = &postwordfilter("$topictitle\t$topicdescription\t$inposttemp");
	    	    ($topictitle,$topicdescription,$inposttemp) = split(/\t/,$temp);
		}
	        my $line = "$lastpostdate\t$id\t$topictitle\t$topicdescription\t$threadstate\t$threadposts\t$threadviews\t$startedby\t$startedpostdate\t$lastposter\t$posticon\t$inposttemp\t";
                $line =~ s/[\a\f\n\e\0\r]//isg;
	        return ("$line");
	    }
	}
}
1;
