#!/usr/bin/perl
#
#               --------------
#                CGI 新聞系統
#               --------------
#               顯示所有新聞標題
#
#         檔案: shownews.pl
#         說明: 顯示所有新聞的程式.
#         作者: 王正奉
#     電子郵件: daniel2@ms7.hinet.net
#     首頁網址////www.cgi-search.com.tw/
#         版本: 3.5
#
# 版權注意:
#
# 本軟體著作權屬 CGI 手寫體王朝 作者: 王正奉 所有.
#
# 本人保留對本軟體之更改與訂正之權利, 任何人不得任意修改本
# 軟體之任一部份(如須更改請先通知作者)。使用者執行本軟體所
# 需之設定或美化外觀所需之更改不在此限。使用者沒有本人同意
# 不得將本軟體放置網站供他人下載或翻譯成其他語言。使用者須
# 將版權說明和著作權申明保留不得刪除。
# 
# 如有違反著作權法之規定, 本人自動保留追訴之權利。
# 
# 其他著作權未說明依中文說明.txt檔案為準，
# 如有任何疑問請與王正奉連絡。
# 
# =====================================================================
# 輔助程式
# ---------------------------------------------------------------------
require "setup.pl";
&form_info;
print "Content-type: text/html\n\n";

##########################################################
## 執行表單訊息                                         ##
##########################################################
sub form_info  {

if ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
} else {
$buffer = $ENV{'QUERY_STRING'};
}
@pairs = split(/&/, $buffer);
foreach $pair (@pairs){
($name, $value) = split(/=/, $pair);

$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%0D%0A/ /g;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<!--(.|\n)*-->//g;
$form{$name} = $value;
}
}

##########################################################
##  顯示新聞標題                                       ##
##########################################################

open(DATA,"$db_news_data");
if ($flock) { flock (DATA,1); }
@data=<DATA>;
if ($flock) { flock (DATA,8); }
close(DATA);
foreach $line (@data) {
($jtime,$jdate,$jname,$file,$html,$layout,$noth)=split(/∥/,$line);
if ($form{'action'} eq $file) {
$name=$jname;
$java=$kjava;
$layout_url="$templates_url/$layout";
last;
}
}
close(DATA);
open (PAGE,"<$templates_path/$layout/front_page.html") or &cgierr("無法開啟 front_page.html 檔案"); 
@sheet=<PAGE>;	
close(PAGE);
foreach $line2 (@sheet) {
$line2 =~ s/#site_title#/$site_title/gi;
$line2 =~ s/#style#/$style/gi;
$line2 =~ s/#site_url#/$site_url/gi;
$line2 =~ s/#site_logo#/$site_logo/gi;
$line2 =~ s/#name#/$name/gi;
$line2 =~ s/#news_url#/$news_url/gi;
$line2 =~ s/#newimg#/$layout_url/gi;
$line2 =~ s/#borderColor#/$borderColor/gi;
$line2 =~ s/#trbgcolor#/$trbgcolor/gi;
$line2 =~ s/#titl_fontcolor#/$titl_fontcolor/gi;
if ($line2 =~ /#front_page#/i) {

open(NEWS,"$data_path/$form{'action'}\.db");
if ($flock) { flock (NEWS,1); }
@list=reverse(<NEWS>);
if ($flock) { flock (NEWS,8); }
close(NEWS);
$addsignin=@list;

$page= $form{'page'};
$stop=1;
$more=1;

if ($page eq ""){ 
$min=1; $max=$newspage; $page=1;
}
if ($page ne ""){
$min=(($page-1)*$newspage)+1; $max=$page*$newspage;
}

foreach $line3 (@list) {
($count,$number,$jtime,$jdate,$title,$comments,$noth)=split(/∥/,$line3);
if ($more>=$min && $more <=$max) {
if ($mem == 1){
print "<tr bgcolor=\"$tdbgcolor_line1\" onMouseOver=this.style.backgroundColor='$tdbgcolor_line2' onMouseOut=this.style.backgroundColor=''><td valign=\"top\" align=\"center\" width=16%>$jdate</td>";
$mem--;
}else{
print "<tr bgcolor=\"$tdbgcolor_line2\" onMouseOver=this.style.backgroundColor='$tdbgcolor_line1' onMouseOut=this.style.backgroundColor=''><td valign=\"top\" align=\"center\" width=16%>$jdate</td>";
$mem++;
}
if (length($comments) > $com_length ) {
$comtop=substr($comments,0,$com_length);
$comtop="$comtop......";
}else {
$comtop="$comments";
}
$comtop=~ s/\'//g;
$comtop=~ s/\"//g;
$comtop=~ s/\)//g;
$comtop=~ s/\(//g;
print "<A onmouseout=nd() onmouseover=\"dcc('<TABLE WIDTH=400 BORDER=1 BORDERCOLOR=$table_bordercolor CELLPADDING=2 CELLSPACING=0 BGCOLOR=$table_bgcolor><TD><FONT COLOR=$font_color SIZE=2>$comtop</FONT></TD></TABLE>','','20')\">";
print "<td><a href=$view_script?action=$form{'action'}&id=$number>$title</a>";
if ($jdate eq $date) {
print "<img src=$layout_url/new.gif width=28 height=11 alt=新 border=0></td></a>";
} else { print "</td></a>"; }
if ($hite eq "ON") {
print "<td align=right>$count</td>";
}
print "</tr>\n";
}
$more++;
}
$line2 = "";
}
if ($line2 =~ /#page#/i) {
while ($addsignin>0) {
print "<A HREF=\"$show_script?action=$form{'action'}&page=$stop\">" if ($page != $stop); 
print "$stop ";
print "</a>" if ($page != $stop);
$addsignin=$addsignin-$newspage;
$stop++;
}
close(NEWS);
$line2 = "";
}
$line2 =~ s/#site_url#/$site_url/gi;
$line2 =~ s/#newimg#/$layout_url/gi;
$line2 =~ s/#footer#/$footer/gi;
print "$line2";
}
close (PAGE);
exit;

##########################################################
## 程式執行錯誤.                                        ##
##########################################################
sub cgierr {

print <<ERROR;
<head>
<title>CGI 執行錯誤</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
$style
</head>
$body
<center>
$site_logo<br>
<hr size=1 width=600>
<font size=+2 color=darkred>CGI 執行錯誤</font><p>
<table border="0">
<tr><td>
對不起!! 以下是執行錯誤的訊息:<BR>
<b>$_[0]</b><BR>
請按<a href="javascript: history.go(-1)">返 回</a>和通知管理員.
<br>
</td></tr></table>
<BR><hr size=1 width=600>
$footer
<br></center>
</body>
</html>
ERROR
exit;
}