#!/usr/bin/perl
###############################################################################################################################################
# LeoBBS 子論壇新文章 ver 2.0
###############################################################################################################################################
# 使用辦法： news.cgi?forum=子論壇號&max=顯示幾條文章&maxlength=標題長度&display=1&link=顏色16進制控制碼&vlink=顏色16進制控制碼&alink=顏色16進制控制碼
# 例： 在你主頁的適當位置加入以下語句
#      <script src="news.cgi?forum=1&max=10&maxlength=20&link=0000ff&vlink=7f007f&alink=ff0000&mode=topic"></script>
#      這樣就可以在相應位置顯示1號論壇的最新10個文章，標題長度為 20，顯示發文時間，用文章模式查看
#                                                   (display=0 表示不顯示發文時間)
#                                                   (mode=view 表示用新聞模式查看)
# link是自定義超連結的顏色，vlink是自定義已瀏覽的超連結的顏色，alink是自定義目前超連結的顏色
#
# 所有參數均可以省略，預設為查看第1個論壇的前10個文章，標題最多20個字符、顯示時間、用文章模式
###############################################################################################################################################

#####################################################
#  LEO SuperCool BBS / LeoBBS X / 雷傲極酷超級論壇  #
#####################################################
#  基於山鷹糊、花無缺製作的 LB5000 XP 2.30 免費版   #
#   新版程式製作 & 版權所有: 雷傲科技 (C)(R)2004    #
#####################################################
#      主頁地址： http://www.LeoBoard.com/          #
#      論壇地址： http://www.LeoBBS.com/            #
#####################################################

BEGIN {
    $startingtime=(times)[0]+(times)[1];
    foreach ($0,$ENV{'PATH_TRANSLATED'},$ENV{'SCRIPT_FILENAME'}){
    	my $LBPATH = $_;
    	next if ($LBPATH eq '');
    	$LBPATH =~ s/\\/\//g; $LBPATH =~ s/\/[^\/]+$//o;
        unshift(@INC,$LBPATH);
    }
}

#unless ($ENV{'HTTP_REFERER'} =~ /$ENV{'HTTP_HOST'}/) {
#print "Content-Type:text/html\n\n";
#print "document.write('<font color=red>　對不起，不准許非本論壇主機使用！</font>');";
#exit;
#}
use LBCGI;
$LBCGI::POST_MAX=2000;
$LBCGI::DISABLE_UPLOADS = 1;
$LBCGI::HEADERS_ONCE = 1;
require "data/boardinfo.cgi";
require "data/styles.cgi";
require "bbs.lib.pl";
require "rebuildlist.pl";

$|++;
$query = new LBCGI;
$number        = $query -> param('forum');
$number        = &stripMETA("$number");
$max	       = $query -> param('max');
$max           = &stripMETA("$max");
$display       = $query -> param('display');
$display       = &stripMETA("$display");
$maxlengths    = $query -> param('maxlength');
$maxlengths    = &stripMETA("$maxlengths");
$link	       = $query -> param('link');
$link          = &stripMETA("$link");
$alink	       = $query -> param('alink');
$alink         = &stripMETA("$alink");
$vlink	       = $query -> param('vlink');
$vlink         = &stripMETA("$vlink");
$mode       = $query -> param('mode');
$mode       = &stripMETA("$mode");
$mode = "" if (($mode ne "topic")&&($mode ne "view"));
$mode      = "topic" if ($mode eq "");  # 預設文章方式查看
$number    = 1  if ($number eq "");     # 預設查看第一個論壇
$display   = 1  if ($display eq "");    # 預設顯示文章時間
$max	   = 10 if ($max eq "");        # 預設顯示 10 個文章
$maxlengths= 30 if ($maxlengths eq "");  # 預設標題最多 30 個字符
$defaultsmilewidth  = "width=$defaultsmilewidth"   if ($defaultsmilewidth ne "" );
$defaultsmileheight = "height=$defaultsmileheight" if ($defaultsmileheight ne "");
print header(-charset=>big5);
if ($number !~ /^[0-9]+$/) {
   print "document.write('普通&老大，別亂攻擊我的程式呀！')\n";
   exit;
}

if (-e "${lbdir}data/style${inforum}.cgi") { require "${lbdir}data/style${inforum}.cgi"; }

if ($enterminmony > 0 || $enterminjf > 0 || $enterminweiwang > 0 || $allowusers ne '') {
    $str="-* 這是保密論壇，不能名單 *-";
    goto ENDPPP;
}
    my $filetoopen = "${lbdir}forum$number/foruminfo.cgi";
    open(FILE, "$filetoopen");
    my $forums = <FILE>;
    close(FILE);
    (my $no, $no, $no, $no, $no, $no ,$no ,$no ,$privateforum, $startnewthreads,$no) = split(/\t/,$forums);

if (($startnewthreads eq "cert")&&($userincert eq "no")) {
    $str="-* 這是保密論壇，不能名單 *-";
    goto ENDPPP;
}

if ($privateforum ne "yes") {
    $filetoopen = "$lbdir" . "boarddata/listno$number.cgi";
    if (-e $filetoopen) {
        open(FILE, "$filetoopen");
        @topics = <FILE>;
        $topics = @topics;
        $max = $topics if ($topics<$max);
    
        $max--;
        undef $str;
        $addtime = $timedifferencevalue*3600 + $timezone*3600;
        foreach $topic (@topics[0 ... $max]) {
	    chomp $topic;
            $maxlength = $maxlengths;
            ($topicid, $no) = split(/\t/,$topic);

    my $rr = &readthreadpl($number,$topicid);
    if ($rr ne "") {
	($lastpostdate, $topicid, $topictitle, $topicdescription, $threadstate, $threadposts, $threadviews, $startedby, $startedpostdate, $lastposter, $posticon, $posttemp) = split (/\t/,$rr);
    }
    else { next; }

            next if ($topicid !~ /^[0-9]+$/);
            $lastpostdate = &longdate($lastpostdate + $addtime);
 	    $topictitle =~ s/^＊＃！＆＊//;
 	    
            if (($posticon1 eq "")||($posticon1 !~ /^[0-9]+$/)) {
		$posticon1 = int(myrand(23));
		if ($posticon1 <10 ) {$posticon1="0$posticon1.gif"};
                if ($posticon1 > 9 ) {$posticon1="$posticon1.gif"};
            }
	    $topictitle = &cleanarea("$topictitle");
 	    $topictitle =~ s/\'/\`/g;
            $topictitle =~ s/\&amp;/\&/g;
	    $topictitle =~ s/\&quot;/\"/g;
#	    $topictitle =~ s/\&lt;/</g;
#	    $topictitle =~ s/\&gt;/>/g;
	    $topictitle =~ s/ \&nbsp;/　/g;
	    $topictitle =~ s/  /　/g;
	    if  ($display eq 1) {
	        $disptime= " $lastpostdate";
	    }
	    else { undef $disptime; }
	    if (length($topictitle)>$maxlength) {
	        $topictitletemp=&lbhz("$topictitle",$maxlength); 
	        $topictitletemp =~ s/\'/\`/;
		$topictitletemp =~ s/\&/\&amp;/g;
		$topictitletemp =~ s/\&amp;\#/\&\#/isg;
		$topictitletemp =~ s/\&amp\;(.{1,6})\&\#59\;/\&$1\;/isg;
		$topictitletemp =~ s/\&\#([0-9]{1,6})\&\#59\;/\&\#$1\;/isg;
		$topictitletemp =~ s/\"/\&quot;/g;
		$topictitletemp =~ s/</\&lt;/g;
		$topictitletemp =~ s/>/\&gt;/g;
		$topictitletemp =~ s/ /\&nbsp;/g;
	        $topictitletemp = $topictitletemp ."&nbsp;" if (length($topictitletemp) < $maxlength);
	        $str.="<img src=$imagesurl/posticons/$posticon1 $defaultsmilewidth $defaultsmileheight border=0> <a href=$boardurl/$mode.cgi?forum=$number&topic=$topicid target=_blank><ACRONYM TITLE=\"主題： $topictitle\">$topictitletemp</ACRONYM></a>$disptime<br>";
	    }
	    else {
	        $topicspace=$maxlength-length($topictitle);
	        $topictitle =~ s/ /\&nbsp;/g;
	        $addspace = "";
	        for ($i=0;$i<$topicspace;$i++) {
	     	    $addspace = $addspace ."&nbsp;";
	        }
	        $str.="<img src=$imagesurl/posticons/$posticon1 $defaultsmilewidth $defaultsmileheight border=0> <a href=$boardurl/$mode.cgi?forum=$number&topic=$topicid target=_blank>$topictitle</a>$addspace$disptime<br>";
	    }
        }
    }
    else {
        $str="-* 沒有找到相應的論壇 *-";
    }
}
else {
    $str="-* 這是保密論壇，不能名單 *-";
}
ENDPPP:

print "document.write('<span LINK=#$link VLINK=#$vlink ALINK=#$alink>$str')\n";
exit;
