#!/usr/bin/perl

###########################################################################
#                                                                         #
#    This script inspired by News ver 1.1b Written by Julian Figueroa     #
#             		   1997 -  1998				          #
#             	http://www.gamegirlz.com/theory		          	  #
#                                                                         #
###########################################################################
# COPYRIGHT NOTICE                                                        #
# Copyright 1998-1999 Richard Tirtadji  All Rights Reserved.              #
#                                                                         #
# News_updater may be used and modified free of charge by anyone so long  #
# as this copyright notice and the comments above remain intact. By       #
# using this code you agree to indemnify Richard from any    		  #
# liability that might arise from it's use.                               #
#                                                                         #
# Selling the code for this program without prior written consent is      #
# expressly forbidden.  In other words, please ask first before you try   #
# and make money off of the program.                                      #
#                                                                         #
# Obtain permission before redistributing this software over the Internet #
# or in any other medium.  In all cases copyright and header must remain  #
# intact.								  #
###########################################################################
#									  #
#	KKK  KKK	RRRRRRR						  #
#	KKK  KKK	RRRRRRRR	Programmer : Richard Tirtadji	  #
#	KKK KKK		RRR  RRR	Date	   : 03/06/99		  #
#	KKKKK		RRR  RRR	Version	   : 1.03		  #
#	KKKKK		RRRRRRR		Title	   : newsupdater.cgi  	  #
#	KKK KKK		RRR  RRR					  #
#	KKK  KKK	RRR  RRR					  #
#	KKK  KKK	RRR  RRR					  #
#									  #
#	Mail:webmaster@kingrichard.hypermart.net			  #
#	http://www.krdesigns.com					  #
#	King Richard Designs Scripts					  #
########################################################################### 
# NOTE: You may need to provide full path to each setup files
# Some server require this.
# $path is the directory path to the location of this file.
########################################################################### 

#程式所在真實路徑 或使用 "." 來隱藏您的路徑位置
#$path = "/usr/home/to/your/path/newsupdater";
$path = "/home/eznet/newsupdater";

#>>>>>>>>>>>>>>> 以下部份您不需要再做修改 <<<<<<<<
#>>>>>>>>>>>>>>> NO NEED TO EDIT ANYTHING BELOW THIS LINE <<<<<<<<

require "$path/setup.pl";
require "$path/kr.pl";

$v1="1";
$v2="0";
$v3="3";
#add on Chinese version
$cv1="C";
$cv2="1";

$host = $ENV{'HTTP_REFERER'};
$cgiurl = $ENV{'SCRIPT_NAME'};
print "Content-type: text/html\n\n";
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
	($name, $value) = split(/=/, $pair);
	$value =~ tr/+/ /;
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
#    $value =~ s/<([^>]|\n)*>//g;
	if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$value; }
	else { $INPUT{$name} = $value; }
}

if ($INPUT{'login'}) { &login; }
elsif ($INPUT{'preview'}) { &preview; }
elsif ($INPUT{'news'}) { &referer_check; }
elsif ($INPUT{'editing'}) { &referer_check; }
elsif ($INPUT{'fixed_page'}) { &referer_check; }
elsif ($INPUT{'deleting'}) { &referer_check; }
elsif ($INPUT{'newpass'}) { &newpass; }
else { &printoptions; }
exit;

sub referer_check {
#foreach $referer (@referers) { 
#	if ($host =~ /^$referer/i) { &start; } 
#else { 	&error("Please use the proper <a href=\"$cgiurl\">form</a>"); }
&start;
#}

}

sub preview {
&checkpassword;
print <<top_preview;
<HTML><HEAD><TITLE>新聞製作工具 - King Richard Scripts</TITLE></HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function back(){
history.go(-1);
}
-->
</script>
<BODY BGCOLOR="black" TEXT="#ffffff" link="yellow">
<center><table cellspacing=0 cellpadding=0 border=0>
<tr><td align=center><FONT FACE="Arial,Helvetica" size=4><b>新聞製作管理</b></FONT></td></tr></table>
<table width=600 cellpadding=3 cellspacing=0 border=1 bordercolor=white>
<tr bgcolor=blue><td><FONT FACE="Arial,Helvetica" size=2>這只是預覽, 讓您看看有無任何錯誤發生, 您可以立刻回到上一頁重新編輯, 或是事後再使用新聞製作管理工具做修正 </font></td></tr>
<tr><td bgcolor=gray><br><br>
<center><font face="arial,helvetica" size=3>
top_preview

if ($printing_type == 0) {
print "<TABLE BORDER=0 CELLPADDING=5 width=500 bgcolor=$back_ground><tr><td align=left><b>預覽:</b></td></tr><TR><TD BGCOLOR=\"$background\">\n";
print "<B><A NAME=\"$HEADCOUNT\"><font size=$font_s_sub color=$font_c_sub face=$font_f_sub>$INPUT{'subject'}</FONT></B> - <font size=$font_s_time color=$font_c_time face=$font_f_time> <a href=mailto:$INPUT{'email'}>$INPUT{'author'}</A> @ 00:00:00 $standard_time</FONT><br>\n";
print "<font size=$font_s_text color=$font_c_text face=$font_f_text>$INPUT{'story'}<br>\n";
if ($INPUT{'source'}) {
print "來源 : <a href=\"$INPUT{'url_source'}\">$INPUT{'name_source'}</a>\n"; 
}
print  "</font></TD></TR></TABLE><br><br>\n\n";
}

elsif ($printing_type == 1) {
print "<TABLE BORDER=0 CELLPADDING=5 width=500 bgcolor=$back_ground><tr><td align=left><b>預覽:</b></td></tr><TR><TD BGCOLOR=\"$background\">\n";
print "<A NAME=\"$HEADCOUNT\"><p align=justify><font size=$font_s_time color=$font_c_time face=$font_f_time> <a href=mailto:$INPUT{'email'}>$INPUT{'author'}</A> @ 00:00:00 $standard_time</FONT> - <B><font size=$font_s_sub color=$font_c_sub face=$font_f_sub>$INPUT{'subject'}</FONT></B><br>\n";
print "<font size=$font_s_text color=$font_c_text face=$font_f_text>$INPUT{'story'}<br>\n";
if ($INPUT{'source'}) {
print "來源 : <a href=\"$INPUT{'url_source'}\">$INPUT{'name_source'}</a>\n"; 
}
print  "</font></TD></TR></TABLE><br><br>\n\n";
}

else {
print "<TABLE BORDER=0 CELLPADDING=5 width=500 bgcolor=$back_ground><tr><td align=left><b>預覽:</b></td></tr><TR><TD BGCOLOR=\"$background\">\n";
print "<B><A NAME=\"$HEADCOUNT\"><font size=$font_s_sub color=$font_c_sub face=$font_f_sub>$INPUT{'subject'}</FONT></B><font size=$font_s_time color=$font_c_time face=$font_f_time> 於 00:00:00 $standard_time</FONT><br>\n";
print "<font size=$font_s_text color=$font_c_text face=$font_f_text>$INPUT{'story'}<br>\n";
if ($INPUT{'source'}) {
print "來源 : <a href=\"$INPUT{'url_source'}\">$INPUT{'name_source'}</a>\n"; 
}
print  "</font><font size=$font_s_time color=$font_c_time face=$font_f_time>發佈者 <a href=mailto:$INPUT{'email'}>$INPUT{'author'}</A></FONT></TD></TR></TABLE><br><br>\n\n";
}


print <<bottom_preview;
<br><br><center><a href="javascript:back()">回上一頁發佈或修正</a></center><br>
</font></center></td></tr></table></center>
bottom_preview

&Bottom;
exit;	
}

sub start {
&checkpassword;
if ($INPUT{'news'}) {
&checkcomplete;
}

&grab_new_date;
&Top;
$STATUS = &DIRECTORY_CHECK($datadir);

print "<table width=600 cellpadding=3 cellspacing=0 border=1 bordercolor=white>\n";
print "<tr bgcolor=blue><td><FONT FACE=\"Arial,Helvetica\" size=2>發佈完成. 您可以手動修正事後的任何錯誤, 或使用新聞管理工具來編輯</font></td></tr>\n";
print "<tr><td bgcolor=gray>\n";
print "<center><font face=\"arial,helvetica\" size=3>\n";

if ($STATUS == 0){
	print  "資料目錄: 正常<br>\n";
	}

@CURRENT_DATE = &FILE_READ(CURRENT_DATE, "$datadir/CURRENT_DATE.TXT");
@TOP_STORY = &FILE_READ(TOP_STORY, "$datadir/TOP_STORY.TXT");
@CURRENT_NEWS = &FILE_READ(CURRENT_NEWS, "$datadir/CURRENT_NEWS.TXT");
if ($headline_on) {
@HEADLINE_FILE = &FILE_READ(HEADLINE_FILE, "$datadir/HEADLINE_FILE.TXT");	
@HEADLINE_TOP = &FILE_READ(HEADLINE_TOP, "$datadir/HEADLINE_TOP.TXT");
}
@ARCHIVED_NEWS = &FILE_READ(ARCHIVE, "$datadir/$special.TXT");
print  "資料檔案: 讀取正常<br>\n";

$COUNT = 1;
while($COUNT <= $NUMBER_OF_ARCHIVES)
{
	if ($headline_on) {
 	@TEMP_HEADARC = &FILE_READ(HEADARC, "$datadir/HEADLINE_ARC_$COUNT.TXT");
 	$TEMP2_HEADARC = join (/\n/,@TEMP_HEADARC);
 	$HEADARCS[$COUNT] = $TEMP2_HEADARC;
	}
 	@TEMP_ARCHIVES = &FILE_READ(TEMPFILE, "$datadir/ARCHIVE_$COUNT.TXT");
 	$TEMP2_ARCHIVE = join (/\n/,@TEMP_ARCHIVES);
 	$ARCHIVES[$COUNT] = $TEMP2_ARCHIVE;
 	$COUNT++;
 	close(TEMPFILE);
 	if ($headline_on) {
 	close(HEADARC);
 	}
}
print  "昨日新聞: 共 $NUMBER_OF_ARCHIVES 個<br>\n";
if ($INPUT{'news'}) { &new_news; }
elsif ($INPUT{'editing'}) { &editing; }
elsif ($INPUT{'fixed_page'}) { &fixed_page; }
elsif ($INPUT{'deleting'}) { &deleting; }

print "<br><form method=POST action=\"$cgiurl\"><input type=hidden name=password value=\"$log\"><input type=submit name=login value=\" 回到新聞管理工具 \"><br>\n";
print "</font></center></td></tr></table></center>\n";
&Bottom;
exit;
}

sub editing {
        @FILE_DATA = &FILE_READ(FILE_TO_READ, "$datadir/$INPUT{'file_location'}");
        print "<form method=POST action=\"$cgiurl\">\n";
        print "<input type=hidden name=password value=\"$log\">新聞資料檔案編輯 :\n";
        print "<input type=\"text\" size=\"20\" name=\"file_location\" value=\"$INPUT{'file_location'}\"><br>\n";
        print "<textarea name=\"FIXED_TEXT\" rows=\"20\" cols=\"60\" wrap=\"virtual\">@FILE_DATA\n";
        print "</TEXTAREA><br>\n";
        print "<input type=submit name=\"fixed_page\" value=\" 修改存檔 \">\n";
        print "</form>\n";
}

sub deleting {
        open (THIS, ">$datadir/$INPUT{'file_location'}");
        close(THIS);
        print "最後結束狀態: 昨日過期新聞標題製作完成<br>\n";
 }

sub fixed_page {
        open (THIS, ">$datadir/$INPUT{'file_location'}");
        print THIS "$INPUT{'FIXED_TEXT'}";
        close(THIS);
        print "最後結束狀態: 新聞消息修改完成<br>\n";

}

sub new_news {
 	open(DATES, "$datadir/CURRENT_DATE.TXT");
  	@DATES = <DATES>;
 	close (DATES);
 	$TESTS = "@DATES";
 	$TEST = "<font size=$font_s_header color=$font_c_header face=$font_f_header><b> $tanggal </b></font><br>\n";
 	if ("$TESTS" eq "$TEST") { &NEXT;}
	else {
	 &AUTOARCHIVE_NEWS;
	 &NEXT;
 	}
 }

sub AUTOARCHIVE_NEWS {
 	open(CURRENT_TEST, ">$datadir/CURRENT_NEWS.TXT");
 	if ($headline_on) {	
 	open(HEAD_TEST1, ">$datadir/HEADLINE_FILE.TXT");
 	}	
 	open(BACKUP, ">$datadir/$special.TXT");
 	open(CURRENT_DATE, ">$datadir/CURRENT_DATE.TXT");
 	print CURRENT_DATE "<font size=$font_s_header color=$font_c_header face=$font_f_header><b> $tanggal </b></font><br>\n";
 	close (CURRENT_DATE);
 	

 	if ($NUMBER_OF_ARCHIVES == 0)
 	{
 		print BACKUP "\t@CURRENT_DATE\n";
 		print BACKUP "\t@CURRENT_NEWS\n";
 		print BACKUP "@ARCHIVED_NEWS\n\n";
 	}
 		
 
 	else
 	{
 		if ($headline_on) {
	 	open(HEADLINE1, ">$datadir/HEADLINE_ARC_1.TXT");
	 	print HEADLINE1 "@CURRENT_DATE\n";
	 	print HEADLINE1 "@HEADLINE_FILE\n";
	 	close(HEADLINE1);
	 	}
 		open(BACK, ">$datadir/ARCHIVE_1.TXT");
 		print BACK "@CURRENT_DATE\n";
 		print BACK "@CURRENT_NEWS\n";
 		close(BACK);	
 
 		$COUNT = 2;
 		while($COUNT <= $NUMBER_OF_ARCHIVES)
 		{
 			if ($headline_on) {
 			open(HEADLINE2, ">$datadir/HEADLINE_ARC_$COUNT.TXT");	 		
 			}
 			open(BACKUP3, ">$datadir/ARCHIVE_$COUNT.TXT");
 			$TOPRINT = $COUNT-1;
 			print BACKUP3 "$ARCHIVES[$TOPRINT]";
 			if ($headline_on) {
 			print HEADLINE2 "$HEADARCS[$TOPRINT]";
 			}
 			
 			$COUNT++;
 			if ($headline_on) {
 			close(HEADLINE2);
 			}
 			close(BACKUP3);

 		}
 	print BACKUP "$ARCHIVES[$NUMBER_OF_ARCHIVES]\n\n";
 	print BACKUP "@ARCHIVED_NEWS\n\n";
 	}

 	close (BACKUP);
 	if ($headline_on) {
 	close (HEAD_TEST1);
 	}
 	close (CURRENT_TEST);
}

sub NEXT {
 	&UPDATE_NEWS;
 	print "最後結束狀態: 新聞消息發佈完成<br>\n";
 	print "<FORM METHOD=\"post\" ACTION=\"$cgiurl\"><input type=hidden name=password value=\"$log\">重新修改編輯檔案 <input type=\"hidden\" name=\"file_location\" value=\"$LAST_FILE\">: <input type=\"submit\" name=\"editing\" value=\" 編輯 \"></form><br>\n";
}

sub UPDATE_NEWS {
 
 	### CHECK FORM DATA TO SEE IF AUTO LINK IS ENABLED
 	if ($INPUT{'autolink'} == 1) { 
 	&AUTO_LINK; 
 	print  "自動轉換連結: 開啟<br>\n";
 	}
 	
 
 	### OPEN COUNT FILE FOR TARGETING
 	if (open (HEADCOUNT, "$datadir/HEADCOUNT.TXT")) {
 		$HEADCOUNT = <HEADCOUNT>;
 		$HEADCOUNT++;
 		close(HEADCOUNT);
 		open (HEADCOUNT, ">$datadir/HEADCOUNT.TXT");
 		print HEADCOUNT "$HEADCOUNT";
 	}
 	else {
 		open (HEADCOUNT, ">$datadir/HEADCOUNT.TXT");
 		print HEADCOUNT "1";
 		$HEADCOUNT = 1;		
 	}
 	
  	if ($headline_on) {
 	if ($INPUT{'document'} == 1) {
 		@HEAD_TEST3 = &FILE_READ(HEAD_TEST3, "$datadir/HEADLINE_TOP.TXT");
 		open (HEADLINE_TOP2, ">$datadir/HEADLINE_TOP.TXT");
 		print HEADLINE_TOP2 "<font size=$font_s_time color=$font_c_time face=$font_f_time>$jam </font><font size=$font_s_sub color=$font_c_sub face=$font_f_sub>- <A HREF=$HEADLINE_TOP_LINK\#$HEADCOUNT>$INPUT{'subject'}</A></FONT><BR>\n";
 		print HEADLINE_TOP2 (@HEAD_TEST3);
		close (HEADLINE_TOP2);
		print  "重大新聞標題: 本項新聞已加入重大新聞標題中<br>\n";
		}
 	else {
 		#CHECK/READ DELAY FILE
 		@HEAD_TEST = &FILE_READ(HEAD_TEST, "$datadir/HEADLINE_FILE.TXT");
		open (HEADLINE, ">$datadir/HEADLINE_FILE.TXT");
 		print HEADLINE "<font size=$font_s_time color=$font_c_time face=$font_f_time>$jam </font><font size=$font_s_sub color=$font_c_sub face=$font_f_sub>- <A HREF=$HEADLINE_FILE_LINK\#$HEADCOUNT>$INPUT{'subject'}</A></FONT><BR>\n";
 		print HEADLINE (@HEAD_TEST);
		close (HEADLINE);
		print  "今日新聞標題: 本項新聞已加入今日新聞標題中<br>\n";
		}

 	close(HEADCOUNT);	
 	print  "CURRENT TARGET: $HEADCOUNT<br>\n";
 	}
 	
 	### WRITE NEWS TO TOPSTORY.TXT
 	if ($INPUT{'document'} == 1) {
 		#if (!(-z "$datadir/TOP_STORY.TXT")) {
 
 		#@TOP_TEST = &FILE_READ(TOP_TEST, "$datadir/TOP_STORY.TXT");
		#@CURRENT_TEST = &FILE_READ(CURRENT_TEST, "$datadir/CURRENT_NEWS.TXT");
		#open (CURRENT_NEWS, ">$datadir/CURRENT_NEWS.TXT");
 		#print CURRENT_NEWS (@TOP_TEST);
		#print CURRENT_NEWS (@CURRENT_TEST);
 		#close (CURRENT_NEWS);
 		#}
 		
 		$LAST_FILE = "TOP_STORY.TXT";
		@TOP_TEST = &FILE_READ(TOP_TEST, "$datadir/TOP_STORY.TXT");
 		open (UPDATING, ">$datadir/TOP_STORY.TXT");
 		print UPDATING "<!-- $tanggal 消息發佈 -->\n";
 		&PRINT_NEWS;
 		print UPDATING (@TOP_TEST);
  	}
 
 	### WRITE NEWS TO CURRENT_NEWS.TXT SHUFFLE ALL OTHER NEWS DOWN.
 	else {
 		$LAST_FILE = "CURRENT_NEWS.TXT";
		@CURRENT_TEST = &FILE_READ(CURRENT_TEST, "$datadir/CURRENT_NEWS.TXT");
 		open (UPDATING, ">$datadir/CURRENT_NEWS.TXT");
 		print UPDATING "<!-- $tanggal 新聞發佈 -->\n";
 		&PRINT_NEWS;
 		print UPDATING (@CURRENT_TEST);
 	}
 		
 	### IF MAKE HEADLINE IS CHECKED, WRITE HEADLINE TO HEADLINE_FILE.TXT
 	close(UPDATING);
 	
}

sub PRINT_NEWS {
	
	if ($printing_type == 0) {
   	print UPDATING "<B><A NAME=\"$HEADCOUNT\"><p align=justify><img SRC=$DataListIcon VSPACE=0 HSPACE=0 BORDER=0 NAME=DataListIcon WIDTH=16 HEIGHT=16><font size=$font_s_sub color=$font_c_sub face=$font_f_sub>$INPUT{'subject'}</FONT></B> - <font size=$font_s_time color=$font_c_time face=$font_f_time> <a href=mailto:$INPUT{'email'}>$INPUT{'author'}</A> @ $jam $standard_time</FONT><br>\n";
  	print UPDATING "<font size=$font_s_text color=$font_c_text face=$font_f_text>$INPUT{'story'}<br>\n";
  	
  	if ($INPUT{'source'}) {
  	print UPDATING "消息來源 : <a href=\"$INPUT{'url_source'}\">$INPUT{'name_source'}</a>\n"; 
  	}
    	print UPDATING "</font></p>\n";


        print "新聞發佈: 已新增至 $LAST_FILE 檔案中\n";
        print "<TABLE BORDER=0 CELLPADDING=5 width=500 bgcolor=$back_ground><tr><td align=left>您發佈的新聞內容</td></tr><TR><TD BGCOLOR=\"$background\">\n";
   	print "<B><A NAME=\"$HEADCOUNT\"><font size=$font_s_sub color=$font_c_sub face=$font_f_sub>$INPUT{'subject'}</FONT></B> - <font size=$font_s_time color=$font_c_time face=$font_f_time> <a href=mailto:$INPUT{'email'}>$INPUT{'author'}</A> @ $jam $standard_time</FONT><br>\n";
  	print "<font size=$font_s_text color=$font_c_text face=$font_f_text>$INPUT{'story'}<br>\n";
 	if ($INPUT{'source'}) {
  	print "消息來源 : <a href=\"$INPUT{'url_source'}\">$INPUT{'name_source'}</a>\n"; 
  	}
        print  "</font></TD></TR></TABLE><br><br>\n\n";
	}

	elsif ($printing_type == 1) {
   	print UPDATING "<A NAME=\"$HEADCOUNT\"><p align=justify><font size=$font_s_time color=$font_c_time face=$font_f_time> <a href=mailto:$INPUT{'email'}>$INPUT{'author'}</A> @ $jam $standard_time</FONT> - <B><font size=$font_s_sub color=$font_c_sub face=$font_f_sub>$INPUT{'subject'}</FONT></B><br>\n";
  	print UPDATING "<font size=$font_s_text color=$font_c_text face=$font_f_text>$INPUT{'story'}<br>\n";
  	
  	if ($INPUT{'source'}) {
  	print UPDATING "消息來源 : <a href=\"$INPUT{'url_source'}\">$INPUT{'name_source'}</a>\n"; 
  	}
    	print UPDATING "</font></p>\n";


        print "新聞發佈: 已新增至 $LAST_FILE 檔案中\n";
        print "<TABLE BORDER=0 CELLPADDING=5 width=500 bgcolor=$back_ground><tr><td align=left>您發佈的新聞內容</td></tr><TR><TD BGCOLOR=\"$background\">\n";
   	print "<A NAME=\"$HEADCOUNT\"><p align=justify><font size=$font_s_time color=$font_c_time face=$font_f_time> <a href=mailto:$INPUT{'email'}>$INPUT{'author'}</A> @ $jam $standard_time</FONT> - <B><font size=$font_s_sub color=$font_c_sub face=$font_f_sub>$INPUT{'subject'}</FONT></B><br>\n";
  	print "<font size=$font_s_text color=$font_c_text face=$font_f_text>$INPUT{'story'}<br>\n";
 	if ($INPUT{'source'}) {
  	print "消息來源 : <a href=\"$INPUT{'url_source'}\">$INPUT{'name_source'}</a>\n"; 
  	}
        print  "</font></TD></TR></TABLE><br><br>\n\n";
	}

	else {
   	print UPDATING "<B><A NAME=\"$HEADCOUNT\"><p align=justify><img SRC=$DataListIcon VSPACE=0 HSPACE=0 BORDER=0 NAME=DataListIcon WIDTH=16 HEIGHT=16><font size=$font_s_sub color=$font_c_sub face=$font_f_sub>$INPUT{'subject'}</FONT></B><font size=$font_s_time color=$font_c_time face=$font_f_time> 發佈時間: $jam $standard_time</FONT><br>\n";
  	print UPDATING "<font size=$font_s_text color=$font_c_text face=$font_f_text>$INPUT{'story'}<br>\n";
  	
  	if ($INPUT{'source'}) {
  	print UPDATING "消息來源 : <a href=\"$INPUT{'url_source'}\">$INPUT{'name_source'}</a><br>\n"; 
  	}
    	print UPDATING "</font><font size=$font_s_time color=$font_c_time face=$font_f_time>發佈者 <a href=mailto:$INPUT{'email'}>$INPUT{'author'}</A></FONT></p>\n";


        print "新聞發佈: 已新增至 $LAST_FILE 檔案中\n";
        print "<TABLE BORDER=0 CELLPADDING=5 width=500 bgcolor=$back_ground><tr><td align=left>您發佈的新聞內容</td></tr><TR><TD BGCOLOR=\"$background\">\n";
   	print "<B><A NAME=\"$HEADCOUNT\"><font size=$font_s_sub color=$font_c_sub face=$font_f_sub>$INPUT{'subject'}</FONT></B><font size=$font_s_time color=$font_c_time face=$font_f_time> 發佈時間: $jam $standard_time</FONT><br>\n";
  	print "<font size=$font_s_text color=$font_c_text face=$font_f_text>$INPUT{'story'}<br>\n";
 	if ($INPUT{'source'}) {
  	print "消息來源 : <a href=\"$INPUT{'url_source'}\">$INPUT{'name_source'}</a>\n"; 
  	}
        print  "</font><font size=$font_s_time color=$font_c_time face=$font_f_time>發佈者 <a href=mailto:$INPUT{'email'}>$INPUT{'author'}</A></FONT></TD></TR></TABLE><br><br>\n\n";
	}


}

sub AUTO_LINK {
 
 	@AUTOLINK_DATA = &FILE_READ(AUTOLINK, $AUTOLINK_FILE);
 	$AUTOLINK_TEMP = join(/\n/, @AUTOLINK_DATA);
 	@AUTOLINK_INFO = split(/::/,$AUTOLINK_TEMP);
 	$COUNT = 0;
 	$STORY_DATA = join(/\n/, $INPUT{'story'});
 
 	while ($COUNT < @AUTOLINK_INFO) {
 		$PATTERN = $AUTOLINK_INFO[$COUNT + 1];
 		$MATCH = $AUTOLINK_INFO[$COUNT];
 		$MATCH =~ s/_//g;
 		$REPLACE = "<A HREF=\"$PATTERN\" $DEFAULT_TARGET>$MATCH</A>";
 		$STORY_DATA =~ s/$AUTOLINK_INFO[$COUNT]/$REPLACE/g;
 		$COUNT = $COUNT + 2;
 	}
 	$INPUT{'story'} = $STORY_DATA;
 	close(AUTOLINK_FILE);
}

sub checkcomplete {
	unless ($INPUT{'subject'}) { &error("您必須給予一個標題才能發佈"); }
	unless ($INPUT{'story'}) { &error("您忘記輸入新聞內容了?"); }
	
	if ($INPUT{'source'}) {
			unless ($INPUT{'name_source'}) { &error("您選擇了引入消息來源, 但忘了輸入來源名稱"); }
			unless ($INPUT{'url_source'}=~/\http:/) { &error("您選擇了引入消息來源, 但忘了輸入連結網址"); }
	}
}

sub printoptions {
	open (PASSWORD, "$cgidir/password.txt");
	$password = <PASSWORD>;
	close (PASSWORD);
	chop ($password) if ($password =~ /\n$/);
	if (!$password) {
		&InitializePassword;
	}
	
	&Top;
	&Mainmenu;
	&Bottom;
	exit;
}

sub InitializePassword {
	&Top;
	print "<center><table cellspacing=0 cellpadding=0 border=0 width=600><tr><td>";
	print "<center><p align=justify><font face=arial size=3>在您使用新聞編輯工具之前, ";
	print "您必須設定您的管理員密碼. ";
	print "<br>所有知道此密碼的人員均可使用本管理程式發佈新聞, ";
	print "<HR>";
	print "請輸入您要使用的密碼. ";
	print "(輸入二次.)\n";
	print "<FORM METHOD=POST ACTION=$cgiurl>\n";
	print "<INPUT TYPE=SUBMIT NAME=newpass ";
	print "VALUE=\"設定管理員密碼:\"> ";
	print "<INPUT TYPE=PASSWORD NAME=password SIZE=10> ";
	print "<INPUT TYPE=PASSWORD NAME=passad2 SIZE=10>\n";
	print "</font></CENTER></p></FORM>\n";
	print "</td></tr></table></center>\n";
	&Bottom;
	exit;
}

sub newpass {
	unless ($INPUT{'password'} eq $INPUT{'passad2'}) {
		&error("您的管理員密碼沒有設定完成, 二次輸入的密碼不同!");
		exit;
	}

	&Top;
	$newpassword = crypt($INPUT{'passad2'}, aa);
	open (PASSWORD, ">$cgidir/password.txt") || &error("無法寫入檔案 $cgidir/password.txt");
	print PASSWORD "$newpassword";
	close (PASSWORD);
	print"<BR><B>管理員密碼設定完成 <a href=\"$cgiurl\">現在就登入!</a></b><br>";	
	&Bottom;
	exit;
}

sub checkpassword {
		$log = $INPUT{'password'};

		open (PASSWORD, "$cgidir/password.txt");
		$password = <PASSWORD>;
		close (PASSWORD);
		chop ($password) if ($password =~ /\n$/);
		if ($INPUT{'password'}) {
			$new = crypt($INPUT{'password'}, aa);
		}
		else {
			&error("您必須輸入正確的密碼");
		}
		unless (($password eq $new) || ($new eq $pair)) {
			&error ("您輸入的密碼錯誤!");
		}
}

sub Mainmenu {
print <<EOF;
<FORM METHOD=POST ACTION="$cgiurl">
<TABLE CELLPADDING=7 BORDER=1>
<TR align=center bgcolor=silver><TD><h4><b>輸入管理員密碼:</TD>
<TD><INPUT TYPE=PASSWORD NAME=password SIZE=15></TD>
<TD><INPUT TYPE=SUBMIT NAME=login VALUE="登入"></TD></TR>
</TABLE>
</FORM>
<br>
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=1>
<TR align=center><TD><font face=arial size=4>您目前使用的版本</font></TD><td><br></td><TD><font face=arial size=4>最後更新的版本</font></TD></TR>
<TR align=center><TD><img src="http://kingrichard.hypermart.net/img/digits/$v1.gif">.<img src="http://kingrichard.hypermart.net/img/digits/$v2.gif"><img src="http://kingrichard.hypermart.net/img/digits/$v3.gif">.<img src="http://www.goingfly.com/krdesigns/newsupdater_ver/digits/$cv1.gif"><img src="http://www.goingfly.com/krdesigns/newsupdater_ver/digits/$cv2.gif"></TD><td><br></td><TD><img src="http://kingrichard.hypermart.net/img/digits/freen_1.gif">.<img src="http://kingrichard.hypermart.net/img/digits/freen_2.gif"><img src="http://kingrichard.hypermart.net/img/digits/freen_3.gif">.<img src="http://www.goingfly.com/krdesigns/newsupdater_ver/freen_C.gif"><img src="http://www.goingfly.com/krdesigns/newsupdater_ver/freen_2.gif"></TD></TR>
<TR align=center><TD colspan=3><br></TD></TR>
<TR align=center><TD colspan=3><img src="http://kingrichard.hypermart.net/img/digits/announcement.gif"></TD></TR>
</table>
</br>
EOF
}

sub login {
	&checkpassword;
	&Top;
&scr_top;

print <<EOF;
<table width=600 cellpadding=3 cellspacing=0 border=1 bordercolor=white>
<tr bgcolor=blue><td><FONT FACE="Arial,Helvetica" size=2>請輸入標題, 選擇管理員身份以及輸入要發佈的新聞消息. <br>如果您使用本程式有遇到任何問題, 請與網站管理員 <A HREF="mailto:$webmaster_mail">$webmaster_name</A> 連絡.</font></td></tr>
<tr><td bgcolor=gray>

<center>
<table width=600 cellpadding=0 cellspacing=0 border=0>
<tr valign=top><td>

<table width=400 cellpadding=0 cellspacing=0 border=0>
<FORM ACTION=$cgiurl METHOD=POST NAME=ArticleUpdate>
<input type=hidden name=password value="$log">
<tr align=center><td colspan=2 bgcolor=#DDEEFF><FONT FACE="Arial,Helvetica" color=black size=2><b>新聞內容編輯</b></font></td></tr>
<tr><td colspan=2><br></td></tr>
<tr><td align=right><FONT FACE="Arial,Helvetica" size=2>主題 :&nbsp;</font></td><td><input type=text size=23 name=subject></td></tr>
<tr><td align=right><FONT FACE="Arial,Helvetica" size=2>發佈者 :&nbsp;</font></td>
<td><select name="author" size="1" onChange="ByAuthor();">
EOF


foreach $list1 (@email_data) {
@output1 = split(/\|/,$list1);
chomp $output1[0];
print "<option value=\"$output1[0]\">$output1[0]</option>\n";
}

print "</select>\n";
print "</td></tr>\n";
print "<tr><td align=right><FONT FACE=\"Arial,Helvetica\" size=2>E-Mail 地址 :&nbsp;</font></td>\n";
print "<td><select name=\"email\" size=\"1\" onChange=\"ByEmail();\">\n";

foreach $list2 (@email_data) {
@output2 = split(/\|/,$list2);
chomp $output2[1];
print "<option value=\"$output2[1]\">$output2[1]</option>\n";
}



print "</select>\n";

print <<EOF;
</td></tr>
<tr><td colspan=2><br></td></tr>
<tr align=center><td colspan=2>
<textarea name=story rows=10 cols=40 wrap=virtual></textarea></td></tr>
<tr align=center><td align=right><FONT FACE="Arial,Helvetica" size=2>
引入消息來源 :&nbsp;</font></td><td align=left><input type=checkbox name=source></td>
<tr><td align=right><FONT FACE="Arial,Helvetica" size=2>來源名稱 :&nbsp;</font></td><td align=left><input type=text name=name_source size=30></td></tr>
<tr><td align=right><FONT FACE="Arial,Helvetica" size=2>來源網址 :&nbsp;</font></td><td align=left><input type=text name=url_source value="http://" size=30></td></tr>
<tr><td colspan=2><br></td></tr>
</table>

</td><td>

<table width=200 cellpadding=0 cellspacing=0 border=0>
<tr align=center><td bgcolor=#DDEEFF><FONT FACE="Arial,Helvetica" color=black size=2><b>HTML 標註</b></font></td></tr>
<tr><td><br></td></tr>
<tr><td>
<center>
<table width=200 cellpadding=0 cellspacing=0 border=0>
<tr align=center><td><!-- add on, add new tab and sort list -->
<input type=button value="LINK" onclick="addmore('Link','http://');"><br>
<input type=button value="MAIL" onclick="addmore('Mail','mailto:');"><br>
<input type=button value="圖片" onclick="addgraph();"><br>
<input type=button value="線段" onclick="addtostory('&lt;HR&gt;');"><br>
</td>
<td>
<input type=button value="粗體" onclick="addword('BOLD');"><br>
<input type=button value="斜體" onclick="addword('I');"><br>
<input type=button value="閃爍" onclick="addword('BLINK');"><br>
<input type=button value="底線" onclick="addword('U');"><br>
</td>
<td>
<input type=button value="內縮" onclick="addword('blockquote');"><br>
<input type=button value="置中" onclick="addword('CENTER');"><br>
<input type=button value="段落" onclick="addtostory('&lt;P&gt;');"><br>
<input type=button value="斷行" onclick="addtostory('&lt;BR&gt;');"><br>
</td>
</tr>
</table>
</center>
</td></tr>
<tr><td><br></td></tr>
<tr align=center><td bgcolor=#DDEEFF><FONT FACE="Arial,Helvetica" color=black size=2><b>設定</b></font></td></tr>
<tr><td><br></td></tr>
<tr><td>
<center>
<table width=200 cellpadding=0 cellspacing=0 border=0>
<tr><td align=right>
<FONT FACE="Arial,Helvetica" size=2>新聞類型 :&nbsp;</font></td><td align=left>
<select name=document>
<option selected value="2">今日新聞</option>
<option value="1">消息發佈</option>
</select></td></tr>
<tr><td align=right><FONT FACE="Arial,Helvetica" size=2>自動轉換連結 :&nbsp;</font></td><td align=left><select name=autolink><option selected value="1">On</option><option value="2">Off</option></select></td></tr>
</table>
</center>

</td></tr>
</table>
</td></tr>
<tr align=center><td colspan=2 bgcolor=#DDEEFF><FONT FACE="Arial,Helvetica" color=black size=2><b>預覽/發佈</b></font></td></tr>
<tr><td colspan=2><br></td></tr>
<tr align=center><td colspan=2><input type=submit name=preview value="預覽"> <input type=submit name=news value="發佈"></td></tr>
</form>
<tr><td colspan=2><br></td></tr>

<tr align=center><td colspan=2 bgcolor=#DDEEFF><FONT FACE="Arial,Helvetica" color=black size=2><b>新聞管理</b></font></td></tr>
<tr><td colspan=2>

<center>
<table cellspacing=0 cellpadding=2 border=0>
<tr><td colspan=2><br></td></tr>
<tr><td align=right><FONT FACE="Arial,Helvetica" size=2>清除新聞 :&nbsp;</font></td><td align=left>
<FORM ACTION=$cgiurl METHOD=POST>
<input type=hidden name=password value="$log">
<select name="file_location" size="1">
<option value="TOP_STORY.TXT">消息發佈</option>
<option selected value="CURRENT_NEWS.TXT">今日新聞</option>
<option value="HEADLINE_TOP.TXT">消息發佈 標題</option>
<option value="HEADLINE_FILE.TXT">今日新聞 標題</option>
</select><input type="submit" name="deleting" value="清除新聞"></td></tr>
</form>
<tr><td align=right><FONT FACE="Arial,Helvetica" size=2>編輯新聞 :&nbsp;</font></td><td align=left>
<FORM ACTION=$cgiurl METHOD=POST>
<input type=hidden name=password value="$log">
<select name="file_location" size="1">
<option value="TOP_STORY.TXT">消息發佈</option>
<option selected value="CURRENT_NEWS.TXT">今日新聞</option>
<option value="HEADLINE_TOP.TXT">消息發佈 標題</option>
<option value="HEADLINE_FILE.TXT">今日新聞 標題</option>
</select><input type="submit" name="editing" value="編輯新聞"></td></tr>
<tr><td colspan=2><br></td></tr>
</form>
</table>
</center>

<center><a href="$home_url">回到首頁</a></center>

</td></tr>
</table>
</center>

</td></tr>
</table>
</center>
EOF

&Bottom;
exit;
}


sub scr_top {
open (SCR, "$cgidir/scripts.txt");
@scr = <SCR>;
close (SCR);
		foreach $line (@scr) {
		print "$line";
	}
}

sub Top {

	print "<HTML><HEAD><TITLE>新聞製作管理 - King Richard Scripts</TITLE></HEAD>\n";
	print "<BODY BGCOLOR=\"black\" TEXT=\"#ffffff\" link=\"yellow\">\n";

	print "<center><table cellspacing=0 cellpadding=0 border=0>\n";
	print "<tr><td align=center><FONT FACE=\"Arial,Helvetica\" size=4><b>新聞製作管理工具</b></FONT></td></tr></table>\n";
}

sub Bottom {
	print "<center><BR>\n";
	print "<small>News Updater Scripts version $v1.$v2$v3 can be download FREE at <A HREF=\"http://www.krdesigns.com\">King Richard Scripts</A></small>\n";
	print "<BR>\n";
	print "<small>新聞製作管理工具 $v1.$v2$v3$cv1$cv2 中文版 您可以在 <A HREF=\"http://www.goingfly.com\">GoingFly OnLine</A> 免費下載使用</small>\n";
	print "</center></body></html>\n";
}

sub error{
$errors = $_[0] ;

&Top;
print"<h2 align=center>發生了一個錯誤</H2><center>";
print"<blockquote><b>錯誤在 : <br><font size=+1 color=red>$errors</font><br><br>";
&Bottom;
exit;
}


sub grab_new_date {
$time = time;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime ($time);

if ($min < 10) { $min = "0$min"; }
if ($sec < 10) { $sec = "0$sec"; }
$year = 1900+$year;
$chineseday = $mday;	#add Chinese display day 

@months1 = (January,February,March,April,May,June,July,August,September,October,November,December);
$special = "$months1[$mon]$year";

if ($type_date == 0) {
	@days = (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday);
	$hari = $days[$wday];
	if ($mday < 10) { $mday = "0$mday"; }	
	$bulan = $mon + 1;
	if ($bulan < 10) { $bulan = "0$bulan"; }
	$tanggal = "$hari - $bulan/$mday/$year";
}

elsif ($type_date == 1) {
	&daysup;
	@days = (Sun,Mon,Tue,Wed,Thu,Fri,Sat);
	$hari = $days[$wday];
	@months = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec); 
	$bulan = $months[$mon];
	$tanggal = "$hari - $bulan $mday, $year";
}

elsif ($type_date == 2) {
	&daysup;
	@days = (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday);
	$hari = $days[$wday];
	@months = (January,February,March,April,May,June,July,August,September,October,November,December);
	$bulan = $months[$mon];
	$tanggal = "$hari - $bulan $mday, $year";
}

else { 
	&daysup;
	@days = (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday);
	my @days = qw!星期日 星期一 星期二 星期三 星期四 星期五 星期六!; #add on Chinese Display days
	$hari = $days[$wday];
	@months = (January,February,March,April,May,June,July,August,September,October,November,December);
	my @months = qw!一 二 三 四 五 六 七 八 九 十 十一 十二!; #add on Chinese Display months
	$bulan = $months[$mon];
	$chineseyear = ($year - 1911);
	$tanggal = "中華民國 $chineseyear 年 $bulan 月 $chineseday 日 - $hari";
}

if ($type_time) {
	$ampm = "AM";
	if ($hour eq 12) { $ampm = "PM"; }
	if ($hour eq 0) { $hour = "12"; }
	if ($hour > 12) {
		$hour = ($hour - 12);
		$ampm = "PM";
	}
	if ($hour < 10) { $hour = "0$hour"; }

	if ($sec_activate) { 
				$jam = "$hour\:$min\:$sec $ampm";
			}
	else { $jam = "$hour\:$min $ampm"; }
}

else { 
if ($hour < 10) { $hour = "0$hour"; }
if ($sec_activate) { 
		$jam = "$hour\:$min\:$sec";
	}
else { $jam = "$hour\:$min"; }

}
}

sub daysup {
	if ($mday == 1) { $mday = "$mday<sup>st</sup>"; }
	elsif ($mday == 2) { $mday = "$mday<sup>nd</sup>"; }
	elsif ($mday == 3) { $mday = "$mday<sup>rd</sup>"; }
	else { $mday = "$mday<sup>th</sup>"; }	
}