#!/usr/bin/perl
###############################################################################################################################################
# LB5000 分論壇新文章 ver 1.9
# 程式編製： 山鷹糊  webmaster@cgier.com
# 版權所有： CGI 編程者之家  http://www.CGIer.com  http://www.LeoBoard.com
###############################################################################################################################################
# 使用方法： 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個字元、顯示時間、用文章模式
###############################################################################################################################################
BEGIN {
    $LBPATH = '.';
    my $pgm = $0;
    $pgm =~s/\\/\//g;
    $pgm =~s/^.*\/([^\/]+)$/$1/g;
    unless (-e $LBPATH.'/'.$pgm) {
        foreach ($0, $ENV{'SCRIPT_FILENAME'}, $ENV{'PATH_TRANSLATED'}) {
            s!\\!/!g; s/^(.*)\/[^\/]+$/$1/g;
            if (-e $_ . '/' .$pgm) { $LBPATH = $_; last; }
        }
    }
    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 "lb.lib.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;
}
    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, $no) = split(/\t/,$forums);

if ($privateforum ne "yes") {
    $filetoopen = "$lbdir" . "boarddata/list$number.cgi";
    if (-e $filetoopen) {
	&winlock($filetoopen) if (($OS_USED eq "Unix")||($OS_USED eq "Nt"));
        open(FILE, "$filetoopen");
	flock (FILE, 1) if ($OS_USED eq "Unix");
        @topics = <FILE>;
        close(FILE);
	&winunlock($filetoopen) if (($OS_USED eq "Unix")||($OS_USED eq "Nt"));
        $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, $topictitle, $topicdescription, $threadstate, $threadposts, $threadviews, $startedby, $startedpostdate, $lastposter, $lastpostdate, $posticon1) = split(/\t/,$topic);
            next if ($topicid !~ /^[0-9]+$/);
            $lastpostdate = &longdate($lastpostdate + $addtime);
 	    $topictitle =~ s/^＊＃！＆＊//;
 	    
            if (($posticon1 eq "")||($posticon1 !~ /^[0-9]+$/)) {
		$posticon1 = int(rand(23));
		if ($posticon1 <10 ) {$posticon1="0$posticon1.gif"};
                if ($posticon1 > 9 ) {$posticon1="$posticon1.gif"};
            }

	    if (($dofilter eq "no")||($dofilter eq "all")) {
	        $topictitle = &postwordfilter("$topictitle");
	    }
	    $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/\&/\&amp;/g;
	        $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="-* 這是保密論壇 *-";
}
print "document.write('<span LINK=#$link VLINK=#$vlink ALINK=#$alink>$str')\n";
exit;
