#!/usr/bin/perl

###############################################
#   whats_new.cgi
#      
#                     Copyright(C) Center-Field
###############################################
use Jcode;
require './cgi-lib.pl';

$script = 'topics.cgi';
$script_path_from_index = 'topics/topics.cgi';
$base = './data';				#データ格納ディレクトリ
$colfile = "$base/whats_new.txt";			#記事
$opfile = "$base/option.txt";
$cgi_lib'maxdata = 20000000000;			#入力最大容量（byte）


$index_html_path 				= '../index.html';
$index_tpl_path 				= './tpl/index.tpl';
$contents_tpl_path 				= './tpl/contents.tpl';
$whats_new_contents_tpl_path 	= './tpl/whats_new_contents.tpl';
$whats_new_tpl_path 			= './tpl/whats_new.tpl';
$whats_new_indiv_tpl_path 		= './tpl/whats_new_indiv.tpl';
$header_tpl_path 				= './tpl/header.tpl';
$footer_tpl_path 				= './tpl/footer.tpl';
$anchor_class 					= "topics";

$pdf_icon 	= '<img src="formats/pdf.jpg">';
$xlsx_icon	= '<img src="formats/excel.jpg">';
$xls_icon 	= '<img src="formats/excel.jpg">';
$doc_icon 	= '<img src="formats/word.jpg">';
$docx_icon 	= '<img src="formats/word.jpg">';
$xdw_icon 	= '<img src="formats/docu.jpg">';
$other_icon = '<img src="formats/other.jpg">';

$pdf_icon_tp 	= '<img src="../formats/pdf.jpg">';
$xlsx_icon_tp	= '<img src="../formats/excel.jpg">';
$xls_icon_tp 	= '<img src="../formats/excel.jpg">';
$doc_icon_tp 	= '<img src="../formats/word.jpg">';
$docx_icon_tp 	= '<img src="../formats/word.jpg">';
$xdw_icon_tp 	= '<img src="../formats/docu.jpg">';
$other_icon_tp  = '<img src="../formats/other.jpg">';

$page_index = 5;	#トップページの表示件数

open (IN,"$opfile") || &error("OPEN ERROR_opfile");	$opdata = <IN>;		close IN;
if (!$opdata) {
	$pass = &crypt('cgi');
	chmod(0666,$opfile);	open(OUT,">$opfile") || &error("OPEN ERROR_opfile");
	print OUT "$pass<>コラム<><><>$base<>$base<><><>#fafaf5,#000000,#c00000,#800000<>20<>160<>160";
	close OUT;
	chmod(0666,$colfile);
}

###　メイン処理　###
&ReadParse;
$mode = $in{'mode'};
$num  = $in{'num' };

open (IN,"$opfile") || &error("OPEN ERROR_opfile");
($pass,$title,$com_adm,$home,$savedir,$loaddir,$bg_img,$home_icon,$colors,$page,$max_w,$max_h) = split(/<>/,<IN>);
close IN;
($bg_color,$text_color,$title_color,$sub_color) = split(/,/,$colors);
if ($page == 0) {$page = 20;}

if 	 ($mode eq 'admin')	{&admin;}
elsif($mode eq 'dsp'  )	{&dsp;}
else 					{&whtats_new_main;}

#&footer;
exit;

###
sub header {
	print "Content-type: text/html\n\n";
	print "<html><head><META HTTP-EQUIV=\"Content-type\" CONTENT=\"text/html; charset=Shift_JIS\">\n";
	print "<title>WEBページ管理画面</title><link rel=\"stylesheet\" type=\"text/css\" href=\"$loaddir/style.css\"></head>\n";
	$head = 1;
}

sub footer {
	print "</body></html>\n";
}

###
sub main {
	&header;
	
	print "<body>\n";
	&list;
	print "<table width=600><tr><td>";
	if (0 <= $back) {print "<a href=\"$script?num=$back\">&lt;&lt;BACK</a>";}
	print "　（全 ",$m+1,"件）　";
	if ($next <= $m) {print "<a href=\"$script?num=$next\">NEXT&gt;&gt;</a>";}
	print "</td><td align=right><a href=\"$script?mode=admin\">[/編集]</a></td></tr></table><br>\n";
	
}

###
sub whtats_new_main {

	$back = $num - $page;
	$next = $num + $page;
	$m = -1;
	my $all = 0;
	my $r = 0;
	my $sub_str;
	my $filepath;
	my $dammy;
	my @contents_tpl = ();
	my $contents_tpl;
	
	open (IN,"$colfile") || &error("OPEN ERROR_colfile");
	while (<IN>)
		{
		$m++;
		$all++;
		if ($m < $num || $next <= $m) 	{ next; }
		else							{ $r++; }
		
		($no,$date,$sub,$com,$ftype) = split(/<>/);
		
		$filepath = "$path_for_index_link$loaddir/$no.$ftype";

		
		#my $sub_str = "<a href=\"$script?mode=dsp&no=$no&num=$num\">$sub</a>";
		
		open (FILE, "$whats_new_contents_tpl_path") || &error("OPEN ERROR_whats_new_contents_tpl_path");
		while(my $str = <FILE>)
			{
			if($ftype =~ /pdf/ or $ftype =~ /xls/ or $ftype =~ /xlsx/ or $ftype =~ /doc/ or $ftype =~ /xdw/ or $ftype =~ /docx/)
												{ $sub_str = "<a class=\"$anchor_class\" href=\"$filepath\" target=\"_blank\">$sub</a>"; }
			elsif(not $com and not $ftype)		{ $sub_str = "$sub"; }
			else								{ $sub_str = "<a class=\"$anchor_class\" href=\"$path_for_index_link$script?mode=dsp&no=$no&num=$num\">$sub</a>"; }
			
			if   ($ftype =~ /pdf/ ) { $icon = $pdf_icon_tp;  }
			elsif($ftype =~ /xlsx/) { $icon = $xlsx_icon_tp; }
			elsif($ftype =~ /xls/ ) { $icon = $xls_icon_tp;  }
			elsif($ftype =~ /doc/ ) { $icon = $doc_icon_tp;  }
			elsif($ftype =~ /docx/) { $icon = $docx_icon_tp; }
			elsif($ftype =~ /xdw/ ) { $icon = $xdw_icon_tp;  }
			else					{ $icon = ''; }
			
			$str =~ s/<!--icon-->/$icon/;
			$str =~ s/<!--date-->/$date/;
			$str =~ s/<!--sub-->/$sub_str/;
			push @contents_tpl, $str;
			}
		close FILE;
		}
	close IN;
	$contents_tpl = join("", @contents_tpl);
	Jcode::convert(\$contents_tpl ,"utf8" ,'sjis');


	my $linkstr;
	my $linkstr2;
	my $linkstr3;
	my $linkstr4;
	$m = $m+1;
	my $i;
	
	my $start_num = $all - $num;
	
	if($r >= $page)	{ $i = $page; }
	else			{ $i = $r; }
	
	for($i; $i>0; --$i)
		{
		push @num_no,$start_num;
		--$start_num;
		}
	
	
	if (0 <= $back) {$linkstr = "<a href=\"$script?mode=topics&num=$back\">&lt;&lt;BACK</a>";}
	$linkstr2 =  "　（全$all件）";
	if ($next <= $m) {$linkstr3 = "<a href=\"$script?mode=topics&num=$next\">NEXT&gt;&gt;</a>";}
	$linkstr4 = "$num_no[$#num_no]〜$num_no[0]件目を\表\示　";
	
	Jcode::convert(\$linkstr , "utf8" ,'sjis');
	Jcode::convert(\$linkstr2 ,"utf8" ,'sjis');
	Jcode::convert(\$linkstr3 ,"utf8" ,'sjis');
	Jcode::convert(\$linkstr4 ,"utf8" ,'sjis');
	
	open (FILE,"$whats_new_tpl_path") || &error("OPEN ERROR_whats_new_tpl_path");
	while(my $str = <FILE>)
		{
		$str =~ s/<!--whats_new-->/$contents_tpl/;
		$str =~ s/<!--botton_next_back-->/$linkstr$linkstr2$linkstr4$linkstr3/;
		push @tmp, $str;
		}
	my $tmp = join("", @tmp);
	close FILE;
	print "Content-type: text/html\n\n";
	print $tmp;
	exit;
	
	
	
	
#	&list;
#	print "<table width=600><tr><td>";
#	if (0 <= $back) {print "<a href=\"$script?num=$back\">&lt;&lt;BACK</a>";}
#	print "　（全 ",$m+1,"件）　";
#	if ($next <= $m) {print "<a href=\"$script?num=$next\">NEXT&gt;&gt;</a>";}
#	print "</td><td align=right><a href=\"$script?mode=admin\">[編集]</a></td></tr></table><br>\n";
}


###
sub list {
	$back = $num - $page;
	$next = $num + $page;
	$m = -1;
	
	print "<table width=\"639\" cellspacing=\"1\" cellpadding=\"8\" bgcolor=\"#CCCCCC\">\n";
	open (IN,"$colfile") || &error("OPEN ERROR_colfile");
	while (<IN>) {
		$m++;
		if ($m < $num || $next <= $m) {next;}
		#($no,$date,$sub) = split(/<>/);
		($no,$date,$sub,$com,$ftype) = split(/<>/);
		my $filepath = "$path_for_index_link$loaddir/$no.$ftype";
		
		if($ftype =~ /pdf/ or $ftype =~ /xls/ or $ftype =~ /xlsx/ or $ftype =~ /doc/ or $ftype =~ /xdw/ or $ftype =~ /docx/)
											{ $sub_str = "<a href=\"$filepath\" target=\"_blank\">$sub</a>"; }
		elsif(not $com and not $ftype)		{ $sub_str = "$sub"; }
		else								{ $sub_str = "<a href=\"$script?mode=dsp&no=$no&num=$num\" target=\"_blank\">$sub</a>"; }
		
		print "<tr><td width=\"150\" bgcolor=\"#FFFFFF\">";
		if ($mode eq 'admin')	{print "<input type=submit name=$no value=\"修正\"> $date";}
		else					{print "$date";}
		print "</td><td bgcolor=\"#FFFFFF\">$sub_str</td></tr>\n";
		#print "</td><td bgcolor=\"#FFFFFF\"><a href=\"$script?mode=dsp&no=$no&num=$num\" target=\"_blank\">$sub</a></td></tr>\n";
	}
	close IN;
	print "</table>\n";
	
	
}
###
sub list_calc {
	$back = $num - $page;
	$next = $num + $page;
	$m = -1;
	my $all = 0;
	my $r = 0;
	@num_no = ();

	open (IN,"$colfile") || &error("OPEN ERROR_colfile");
	while (<IN>) {
		$m++;
		$all++;
		if ($m < $num || $next <= $m)	{next;}
		else							{ $r++ }
		($no,$date,$sub) = split(/<>/);
	}
	close IN;

	my $i;
	my $start_num = $all - $num;
	
	if($r >= $page)	{ $i = $page; }
	else			{ $i = $r; }
	
	for($i; $i>0; --$i)
		{
		push @num_no,$start_num;
		--$start_num;
		}
}
###
sub list_make_index {
	##変数一時保存作業
	my $page_make_index = $page;
	my $next_make_index = $next;
	my $back_make_index = $back;
	my $m_make_index = $m;
	my $num_make_index = $num;
	
	$num = undef;
	$back = undef;
	$next = undef;
	
	$page = $page_index;
	$back = $num - $page;
	$next = $num + $page;
	$m = -1;
	
	my $sub_str;
	my $path_for_index_link = 'topics/';
	my @contents_tpl = ();
	my $contents_tpl;
	
	open (IN,"$colfile") || &error("OPEN ERROR_colfile");
	while (<IN>) {
		$m++;
		if ($m < $num || $next <= $m) {next;}
		($no,$date,$sub,$com,$ftype) = split(/<>/);
		my $filepath = "$path_for_index_link$loaddir/$no.$ftype";
		
		#my $sub_str = "<a href=\"$script_path_from_index?mode=dsp&no=$no&num=$num\">$sub</a>";

		open (FILE, "$contents_tpl_path") || &error("OPEN ERROR_contents_tpl_path");
		while(my $str = <FILE>)
			{
			#if($ftype)	{ $sub_str = "<a href=\"$filepath\" target=\"_blank\">$sub</a>"; }
			#else		{ $sub_str = "$sub"; }
			if($ftype =~ /pdf/ or $ftype =~ /xls/ or $ftype =~ /xlsx/ or $ftype =~ /doc/ or $ftype =~ /xdw/ or $ftype =~ /docx/)
												{ $sub_str = "<a class=\"$anchor_class\" href=\"$filepath\" target=\"_blank\">$sub</a>"; }
			elsif(not $com and not $ftype)		{ $sub_str = "$sub"; }
			else								{ $sub_str = "<a class=\"$anchor_class\" href=\"$path_for_index_link$script?mode=dsp&no=$no&num=$num\">$sub</a>"; }
			
			if($ftype)
				{
				if   ($ftype =~ /pdf/ ) { $icon = $pdf_icon;  }
				elsif($ftype =~ /xlsx/) { $icon = $xlsx_icon; }
				elsif($ftype =~ /xls/ ) { $icon = $xls_icon;  }
				elsif($ftype =~ /doc/ ) { $icon = $doc_icon;  }
				elsif($ftype =~ /docx/) { $icon = $docx_icon; }
				elsif($ftype =~ /xdw/ ) { $icon = $xdw_icon;  }
				else					{ $icon = $other_icon; }
				}
			
			$str =~ s/<!--icon-->/$icon/;
			$str =~ s/<!--date-->/$date/;
			$str =~ s/<!--sub-->/$sub_str/;
			push @contents_tpl, $str;
			}
		close FILE;
	}
	close IN;
	$contents_tpl = join("", @contents_tpl);
	Jcode::convert(\$contents_tpl ,"utf8" ,'sjis');
	
	my $switch = 1;
	#open (FILE,"$index_tpl_path") || &error("OPEN ERROR");
	open (FILE,"$index_html_path") || &error("OPEN ERROR_index_html_path");
	while(my $str = <FILE>)
		{
		if($str =~ /<!--what_new_start-->/){ $switch = 2;}
		
		if($switch == 2)
			{
			if   ($str =~ /<!--what_new_start-->/)	{ $str = "<!--what_new_start-->"."\n"; }
			elsif($str =~ /<!--what_new_end-->/)	{ $str = "\n"."<!--what_new_end-->"."\n"; }
			elsif($str =~ /<!--what_new-->/)		{ $str = '<!--what_new-->'."\n".$contents_tpl; }
			else{ $str = ''; }
			}
		
		if($str =~ /<!--what_new_end-->/){ $switch = 1;}
		
		push @tmp, $str;
		}
	my $tmp = join("", @tmp);
	close FILE;
	open (FILE, ">$index_html_path") || &error("OPEN ERROR_index_html_path");
	flock(FILE, 2);
	print FILE "$tmp";
	close FILE;
	
	##変数もどし作業
	$page = $page_make_index;
	$next = $next_make_index;
	$back = $back_make_index;
	$m = $m_make_index;
	$num = $num_make_index;
}
###
sub dspbk {
	&header;
	print "<body background=\"$loaddir/$bg_img\" bgcolor=\"$bg_color\" text=\"$text_color\" link=\"$text_color\" vlink=\"$text_color\" alink=\"$text_color\">\n";
	#print "<table width=97%><tr><td><a href=\"$script?num=$num\">[戻る]</a></td></tr></table>\n";
	open (IN,"$colfile") || &error("OPEN ERROR_colfile");
	while (<IN>) {
		($no,$date,$sub,$com,$imgt,$imgw,$imgh,$imgb) = split(/<>/);
		if ($no eq $in{'no'}) {last;}
	}
	close IN;
	$com =~ s/([^=^\"]|^)(http\:[\w\.\~\-\/\?\&\+\=\:\@\%\;\#\%]+)/$1<a href=\"$2\" target=\"_blank\">$2<\/a>/g;
	print "<table width=500 cellspacing=0 cellpadding=0>\n";
	print "<tr><td><font color=\"$sub_color\" size=\"+1\"><b>■ $sub</b></font></td><td align=right valign=bottom>$date</td></tr>\n";
	print "<tr><td colspan=2 style=\"line-height:140%\"><hr size=1>\n";
	if ($imgt) {
		$imgfile = "$loaddir/$no.$imgt";
		$imgsrc = "<img src=\"$imgfile\" width=$imgw height=$imgh border=0 hspace=8 align=left>";
		if ($imgb) {print "<a href=\"$imgfile\" target=\"_blank\">$imgsrc</a>";} else {print $imgsrc;}
	}
	print "$com<br clear=left><br><hr size=1></td></tr></table>\n";
}
###
sub dsp {
	#&header;
	#print "<body background=\"$loaddir/$bg_img\" bgcolor=\"$bg_color\" text=\"$text_color\" link=\"$text_color\" vlink=\"$text_color\" alink=\"$text_color\">\n";
	#print "<table width=97%><tr><td><a href=\"$script?num=$num\">[戻る]</a></td></tr></table>\n";
	open (IN,"$colfile") || &error("OPEN ERROR_colfile");
	while (<IN>) {
		($no,$date,$sub,$com,$imgt,$imgw,$imgh,$imgb) = split(/<>/);
		if ($no eq $in{'no'}) {last;}
	}
	close IN;
	$com =~ s/([^=^\"]|^)(http\:[\w\.\~\-\/\?\&\+\=\:\@\%\;\#\%]+)/$1<a href=\"$2\" target=\"_blank\">$2<\/a>/g;
	
	Jcode::convert(\$sub ,"utf8" ,'sjis');
	Jcode::convert(\$com ,"utf8" ,'sjis');

	open (FILE,"$whats_new_indiv_tpl_path") || &error("OPEN ERROR_whats_new_indiv_tpl_path");
	while(my $str = <FILE>)
		{
		$str =~ s/<!--date-->/$date/;
		$str =~ s/<!--sub-->/$sub/;
		if( (not $imgt =~ /pdf/)or(not $imgt =~ /xls/)or(not $imgt =~ /xlsx/)or(not $imgt =~ /doc/)or(not $imgt =~ /docx/)or(not $imgt =~ /xdw/) )
		{
			if($imgt)
				{
				$imgfile = "$loaddir/$no.$imgt";
				$imgsrc = "<img src=\"$imgfile\" width=$imgw height=$imgh border=0>";
				$imgsrc_big = "<a href=\"$imgfile\" target=\"_blank\">$imgsrc</a>";
				if($imgb)	{$str =~ s/<!--pict-->/$imgsrc_big/; $str =~ s/<!--width-->/$imgw/;}
				else		{$str =~ s/<!--pict-->/$imgsrc/;     $str =~ s/<!--width-->/$imgw/;}
				}
			else{$str =~ s/<td align="left" valign="top" width="<!--width-->"><!--pict--><\/td>//;}
		}
		else{$str =~ s/<td align="left" valign="top" width="<!--width-->"><!--pict--><\/td>//;}
		$str =~ s/<!--com-->/$com/;
		push @tmp, $str;
		}
	my $tmp = join("", @tmp);
	close FILE;
	print "Content-type: text/html\n\n";
	print $tmp;
}

### 
sub admin {
	
	print "Content-type: text/html\n\n";
	open (FILE,"$header_tpl_path") || &error("OPEN ERROR_header_tpl_path");
	while(my $str = <FILE>){ print $str; }
	close FILE;


#	$inpass = $in{'pass'};
#	if ($inpass eq '') {
#		#print "<table width=97%><tr><td><a href=\"$script\">[Return]</a></td></tr></table>\n";
#		print "<table style=\"margin-left:10px;\"><tr><td><span class=\"font14\"><b>トップページ&nbsp;&nbsp;TOPICS&nbsp;編集画面へのログイン</b></span></td></tr>\n";
#		print "<tr><td>";
#		print "<form class=\"form_login\" action=\"topics.cgi\" method=\"POST\">\n";
#		print "<input type=hidden name=mode value=\"admin\">\n";
#		print "<input type=password size=15 maxlength=8 name=pass>\n";
#		print "<input type=submit value=\" 認証 \"></form>\n";
#		print "</td></tr>";
#		print "</table><br />\n";
#
#		open (FILE,"$footer_tpl_path") || &error("OPEN ERROR_footer_tpl_path");
#		while(my $str = <FILE>){ print $str; }
#		close FILE;
#
#		exit;
#	}
#	$mat = &decrypt($inpass,$pass);
#	if (!$mat) {&error("パスワードが違います");}

	#基本設定（通常はコメントアウト）
	#print "<table width=\"634\" bgcolor=\"#2ea5ba\"><tr><td>　<a href=\"$script\"><font color=\"#ffffff\"><b>Return</b></font></a></td>\n";
	print "<table width=\"647\" bgcolor=\"#0083D7\" cellpadding=\"0\" cellspacing=\"0\"><tr><td width=\"10\"></td><td height=\"25\"><span style=\"font-size:15px; color:white;\"><b>トップページ&nbsp;&nbsp;Topicsの編集</b></span></td></tr></table><br /><br />\n";
	print "<table width=\"663\"><tr><td>　<b>新規に記事を登録する場合は、下記のフォームから</b></td>\n";
	print "<td align=right><form action=\"$script\" method=POST>\n";
	print "<input type=hidden name=mode value=\"admin\">\n";
	print "<input type=hidden name=pass value=\"$inpass\">\n";
	print "<input type=submit value=\"新規登録フォーム\">\n";
	#print "<input type=submit name=set value=\"基本設定\">\n";
	print "</td></form><td width=10></td></tr></table>\n";

	if ($in{'set'}) {&setup;} else {&edtin;}
}

###
sub edtin {
	if ($in{'newwrt'})
		{
		&newwrt;
		&list_make_index;
		}
	elsif ($in{'edtwrt'})
		{
		&edtwrt;
		&list_make_index;
		}
	elsif ($in{'delwrt'})
		{
		&delwrt;
		&list_make_index;
		}

	&in_form;
	print "<br><br>";
	print "\n";
	
	&list_calc;

	#件数表示（上段）
	print "<table width=\"657\"><tr><td><b>&nbsp;&nbsp;修正、削除する場合は[修正]をクリックして下さい。</b></td><td align=\"right\">";
	print "<table><tr>";
	if (0 <= $back) {
		print "<td><form action=\"$script\" method=POST>\n";
		print "<input type=hidden name=mode value=\"admin\">\n";
		print "<input type=hidden name=pass value=\"$inpass\">\n";
		print "<input type=hidden name=num value=\"$back\">\n";
		print "<input type=submit value=\"BACK\"></td></form>\n";
	}
	print "<td align=\"left\">（全",$m+1,"件） ",$num_no[$#num_no],"〜",$num_no[0],"件目を\表\示","</td>\n";
	if ($next <= $m) {
		print "<td><form action=\"$script\" method=POST>\n";
		print "<input type=hidden name=mode value=\"admin\">\n";
		print "<input type=hidden name=pass value=\"$inpass\">\n";
		print "<input type=hidden name=num value=\"$next\">\n";
		print "<input type=submit value=\"NEXT\"></td></form>\n";
	}
	print "<td></td></tr></table>\n";
	print "</td></tr></table>";
	
	
	print "<form action=\"$script\" method=\"POST\">\n";
	print "<input type=hidden name=mode value=\"admin\">\n";
	print "<input type=hidden name=pass value=\"$inpass\">\n";
	print "<input type=hidden name=edt value=\"1\">\n";
	print "<input type=hidden name=num value=\"$num\">\n";
	&list;
	print "</form>\n";	

	
	#件数表示（下段）
	print "<table width=\"657\"><tr><td align=\"right\">";
	print "<table><tr>";
	if (0 <= $back) {
		print "<td><form action=\"$script\" method=POST>\n";
		print "<input type=hidden name=mode value=\"admin\">\n";
		print "<input type=hidden name=pass value=\"$inpass\">\n";
		print "<input type=hidden name=num value=\"$back\">\n";
		print "<input type=submit value=\"BACK\"></td></form>\n";
	}
	print "<td align=\"left\">（全 ",$m+1,"件）",$num_no[$#num_no],"〜",$num_no[0],"件目を\表\示","</td>\n";
	if ($next <= $m) {
		print "<td><form action=\"$script\" method=POST>\n";
		print "<input type=hidden name=mode value=\"admin\">\n";
		print "<input type=hidden name=pass value=\"$inpass\">\n";
		print "<input type=hidden name=num value=\"$next\">\n";
		print "<input type=submit value=\"NEXT\"></td></form>\n";
	}
	print "<td></td></tr></table>\n";
	print "</td></tr></table><br><br>\n";

	open (FILE,"$footer_tpl_path") || &error("OPEN ERROR_footer_tpl_path");
	while(my $str = <FILE>){ print $str; }
	close FILE;
}

###
sub in_form {
	print "<form action=\"$script\" method=POST enctype=\"multipart/form-data\">\n";
	print "<input type=hidden name=mode value=\"admin\">\n";
	print "<input type=hidden name=pass value=\"$inpass\">\n";
	if ($in{'edt'}) {
		open (IN,"$colfile") || &error("OPEN ERROR_colfile");
		while (<IN>) {
			($no,$date,$sub,$com) = split(/<>/);
			if ($in{$no}) {last;}
		}
		close IN;
		print "<input type=hidden name=no value=\"$no\">\n";
		print "<input type=hidden name=num value=\"$num\">\n";
		$sub =~ s/<br>/\r/g;
		$com =~ s/<br>/\r/g;
	} else {
		$sub=$com='';
	}
#	print "<table bgcolor=\"#e6e4ce\" cellspacing=10><tr><td><table cellspacing=1 cellpadding=0>\n";
#	print "<tr><td>題名&nbsp;</td><td><input type=text size=100 name=sub value=\"$sub\" style=\"ime-mode:active;\"></td></tr>\n";
#	print "<tr><td valign=top><br>内容</td><td><textarea cols=80 rows=10 name=com style=\"ime-mode:active;\">$com</textarea></td></tr>\n";
#	print "<tr><td>画像</td><td><input type=file size=70 name=img></td></tr>\n";
#	print "<tr><td></td><td>\n";
	print "<table width=\"637\" bgcolor=\"#e6e4ce\" cellspacing=10><tr><td><table cellspacing=1 cellpadding=0>\n";
	print "<tr><td>内容<br />（見出し）</td><td><textarea cols=77 rows=3 name=sub style=\"ime-mode:active;\">$sub</textarea></td></tr>\n";
	print "<tr><td>詳細内容<br >（本文）</td><td><textarea cols=77 rows=10 name=com style=\"ime-mode:active;\">$com</textarea></td></tr>\n";
	print "<tr><td>ファイル</td><td><input type=file size=90 name=img></td></tr>\n";
	print "<tr><td></td><td>\n";
	if ($in{'edt'}) {
		print "<table width=100%><tr><td><input type=submit name=edtwrt value=\"修正する\"></td>\n";
		print "<td width=40 bgcolor=red align=center><input type=submit name=delwrt value=\"削除\"></td></tr></table>\n";
	} else {print "<input type=submit name=newwrt value=\"新規登録\">";}
	print "</td></tr></table></td></tr></table></form>\n";
}

###
sub newwrt {
	&in_chk;
	($sec,$min,$hour,$mday,$mon,$year) = localtime;
	#$date = sprintf("%4d年%2d月%2d日",$year+1900,++$mon,$mday);
	#$date = sprintf("%4d/%2d/%2d",$year+1900,++$mon,$mday);
	$date = sprintf("%4d.%2d.%2d",$year+1900,++$mon,$mday);

	open (IN,"$colfile") || &error("OPEN ERROR_colfile");		@new = <IN>;		close IN;
	($no) = split(/<>/,$new[0]);
	$no++;
	&img("$savedir/$no",'img');
	
	unshift(@new,"$no<>$date<>$in{'sub'}<>$in{'com'}<>$type<>$width<>$height<>$big<>\n");
	open (OUT,">$colfile") || &error("OPEN ERROR_colfile");		print OUT @new;		close OUT;
}

###
sub in_chk {
	if (!$in{'sub'}) {&error("内容を入力して下さい");}
	#if (!$in{'com'}) {&error("内容を入力して下さい");}
	$in{'com'} =~ s/\r\n|\r|\n/<br>/g;
	$in{'sub'} =~ s/\r\n|\r|\n/<br>/g;
}

###
sub edtwrt {
	&in_chk;
	&img("$savedir/$in{'no'}",'img');
	@new = ();
	open (IN,"$colfile") || &error("OPEN ERROR_colfile");
	while (<IN>) {
		($no,$date,$sub,$com,$imgt,$imgw,$imgh,$imgb) = split(/<>/);
		if ($no eq $in{'no'}) {
			if ($type) {$imgt = $type; $imgw = $width; $imgh = $height; $imgb = $big;}
			push(@new,"$no<>$date<>$in{'sub'}<>$in{'com'}<>$imgt<>$imgw<>$imgh<>$imgb<>\n");
		} else {push(@new,$_);}
	}
	close IN;
	open (OUT,">$colfile") || &error("OPEN ERROR_colfile");		print OUT @new;		close OUT;
	
	if($type)
		{
		@ext_type = ('pdf','xls','xlsx','doc','docx','xdw','jpg','gif','png');
		foreach my $str(@ext_type)
			{
			if($str eq $type)	{}
			else				{ if(-e "$savedir/$in{'no'}.$str"){ unlink("$savedir/$in{'no'}.$str"); } }
			}
		}
	
	
}

###
sub delwrt {
	@new = ();
	open (IN,"$colfile") || &error("OPEN ERROR_colfile");
	while (<IN>) {
		($no,$date,$sub,$com,$imgt) = split(/<>/);
		if ($no eq $in{'no'}) {if ($imgt) {unlink "$savedir/$no.$imgt";}} else {push(@new,$_);}
	}
	close IN;
	open (OUT,">$colfile") || &error("OPEN ERROR_colfile");		print OUT @new;		close OUT;
}

###
sub setup {
	if ($in{'wrt'}) {
		if ($in{'newpass'} ne '') {$pass = &crypt($in{'newpass'});}
		$title = $in{'title'};
		$com_adm = $in{'com_adm'};		$com_adm =~ s/\r\n|\r|\n/<br>/g;
		$home = $in{'home'};
		$savedir = $in{'savedir'};		$loaddir = $in{'loaddir'};

		$icon = $in{'icon'};
		if ($icon) {
			&img("$savedir/$icon",'img');
			if ($type) {$icontype = "$icon.$type";} else {$icontype = '';}
			if ($icon eq 'wall') {$bg_img = $icontype;}
			elsif ($icon eq 'home') {$home_icon = $icontype;}
		}
		$colors = $in{'colors'};	$colors =~ s/\0/,/g;
		$page = $in{'page'};
		$max_w = $in{'max_w'};		$max_h = $in{'max_h'};

		open (OUT,">$opfile") || &error("OPEN ERROR_opfile");
		print OUT "$pass<>$title<>$com_adm<>$home<>$savedir<>$loaddir<>$bg_img<>$home_icon<>$colors<>$page<>$max_w<>$max_h";
		close OUT;
	}
	print "下記に入力後、「設定する」を押して下さい。\n";
	print "<form action=\"$script\" method=POST enctype=\"multipart/form-data\">\n";
	print "<input type=hidden name=mode value=\"admin\">\n";
	print "<input type=hidden name=pass value=\"$inpass\">\n";
	print "<input type=hidden name=set value=\"1\">\n";
	print "<input type=submit name=wrt value=\"設定する\"><br><br>\n";

	print "<table bgcolor=\"#dddddd\" cellspacing=10><tr><td><table cellspacing=1 cellpadding=0>\n";
	print "<tr><td><b>タイトル</b></td><td><input type=text size=40 name=title value=\"$title\"></td></tr>\n";
	$com_adm =~ s/<br>/\r/g;
	print "<tr><td valign=top><br><b>コメント</b></td><td><textarea cols=60 rows=6 name=com_adm>$com_adm</textarea></td></tr>\n";
	print "<tr><td><b>ホームURL</b></td><td><input type=text size=60 name=home value=\"$home\"></td></tr>\n";
	print "<tr><td><b>画像格納ディレクトリ</b></td><td><input type=text size=60 name=savedir value=\"$savedir\"></td></tr>\n";
	print "<tr><td><b>画像読出ディレクトリ</b></td><td><input type=text size=60 name=loaddir value=\"$loaddir\"></td></tr>\n";
	print "<tr><td valign=top><br><b>壁紙、アイコン</b></td><td>\n";
	@name = ('壁紙','ホーム');
	@data = ($bg_img,$home_icon);
	@icon = ('wall','home');
	for (0 .. $#name) {
		print "<input type=radio name=icon value=\"$icon[$_]\">$name[$_]";
		if ($data[$_]) {
			if ($_) {$w = '';} else {$w = ' width=30';}
			print "<img src=\"$loaddir/$data[$_]\"$w>　";
		} else {print "（無）　";}
	}
	print "<br><input type=file size=60 name=img></td></tr>\n";

	print "<tr><td></td><td><a href=\"$loaddir/color.htm\" target=\"_blank\">カラーコード</a></td></tr>\n";
	@name = ('基本背景色','基本文字色','タイトル色','題名色');
	@colors = split(/,/,$colors);
	for (0 .. $#name) {
		print "<tr><td><b>$name[$_]</b></td><td><table cellspacing=0 cellpadding=0><tr>\n";
		print "<td><input type=text name=colors size=10 value=\"$colors[$_]\" style=\"ime-mode:inactive;\"></td>\n";
		print "<td width=5></td><td width=80 bgcolor=\"$colors[$_]\"></td></tr></table></td></tr>\n";
	}
	print "<tr><td><b>記事表\示</b></td><td><input type=text size=3 name=page value=\"$page\" style=\"text-align:right; ime-mode:disabled;\">件/ページ</td></tr>\n";
	print "<tr><td><b>画像表\示</b></td><td>横max<input type=text size=4 name=max_w value=\"$max_w\" style=\"text-align:right; ime-mode:disabled;\">px　　縦max<input type=text size=4 name=max_h value=\"$max_h\" style=\"text-align:right; ime-mode:disabled;\">px</td></tr>\n";
	print "<tr><td><b>パスワード変更</b></td><td><input type=password size=10 maxlength=8 name=newpass> （英数8文字以内）</td></tr>\n";
	print "</table></td></tr></table></form>\n";
}

#$date##	&img("$savedir/$no",'img');
sub img {
	$type=$width=$height=$big=$mac='';
	$imgdata = $in{"$_[1]"};
	if (!$imgdata) {return;}

	foreach (@in) {
		if (/$_[1]/ and /Content-Type:(.+)/i) {
			if ($1 =~ /image\/.*jpeg/i)				{$type = 'jpg';	}
			elsif ($1 =~ /image\/gif/i)				{$type = 'gif';	}
			elsif ($1 =~ /image\/.*png/i)			{$type = 'png';	}
			elsif ($1 =~ /.*pdf/i)					{$type = 'pdf';	}
			elsif ($1 =~ /.*ms-excel/i)				{$type = 'xls';	}
			elsif ($1 =~ /.*spreadsheetml.*/i)		{$type = 'xlsx';}
			elsif ($1 =~ /.*wordprocessingml.*/i)	{$type = 'docx';}
			elsif ($1 =~ /msword/i)					{$type = 'doc';	}
			elsif ($1 =~ /.*fujixerox/i)			{$type = 'xdw';	}
		}
		if (/application\/x-macbinary/i) {$mac = 1;}
	}
	if (!$type) {&error("このファイルはアップロードできません");}

	if ($mac) {
		$leng = substr($imgdata,83,4);
		$leng = unpack("%N",$leng);
		$imgdata = substr($imgdata,128,$leng);
	}
	$img_file = "$_[0].$type";
	open (IMG,">$img_file") || &error("$img_fileファイルを作成できません");
	binmode IMG;
	print IMG $imgdata;
	close IMG;
	chmod (0666,$img_file);


	if($type eq 'jpg' or $type eq 'gif' or $type eq 'png')
	{
		($t,$width,$height) = &getImageSize("$img_file");
		if (!$width || !$height) {&error("ファイルを認識できません");}

		$big = 0;
		if ($max_w && $max_w < $width) {$rate_w = $max_w / $width; $big = 1;} else {$rate_w = 1;}
		if ($max_h && $max_h < $height) {$rate_h = $max_h / $height; $big = 1;} else {$rate_h = 1;}
		if ($big) {
		if ($rate_w < $rate_h) {$rate = $rate_w;} else {$rate = $rate_h;}
		$width = int($width * $rate);
		$height = int($height * $rate);
		}	
	}


}

#=========================================
# Get Image Pixel Size.（出典：stdio-902）
#=========================================
sub getImageSize {
	local($file_name) = @_;
	local($head);

	return if (!open IMG, $file_name);
	binmode IMG;
	read IMG, $head, 8;
	if ($head eq "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a") {
		local($width, $height);
		if (read(IMG, $head, 4) != 4 || read(IMG, $head, 4) != 4 || $head ne 'IHDR') {
			close IMG;
			return "PNG", 0;
		}
		read IMG, $head, 8;
		close IMG;
		$width = unpack "N", substr($head, 0, 4);
		$height = unpack "N", substr($head, 4, 4);
		return "PNG", $width, $height;
	}
	$head = substr $head, 0, 3;
	if ($head eq "\x47\x49\x46") {
		local($head, $width, $height);
		seek IMG, 6, 0;
		read IMG, $head, 4;
		close IMG;
		($width, $height) = unpack "vv", $head;
		return "GIF", $width, $height;
	}
	$head = substr $head, 0, 2;
	if ($head eq "\xff\xd8") {
		local($head, $width, $height, $w1, $w2, $h1, $h2, $l1, $l2, $length);
		seek IMG, 2, 0;
		while (read IMG, $head, 1) {
			last if ($head eq "");
			if ($head eq "\xff") {
				$head = getc IMG;
				if ($head =~ /^[\xc0-\xc3\xc5-\xcf]$/) {
					seek IMG, 3, 1;
					last if (read(IMG, $head, 4) != 4);
					close IMG;
					($h1, $h2, $w1, $w2) = unpack "C4", $head;
					$height = $h1 * 256 + $h2;
					$width  = $w1 * 256 + $w2;
					return "JPG", $width, $height;
				} elsif ($head eq "\xd9" || $head eq "\xda") {
					last;
				} else {
					last if (read(IMG, $head, 2) != 2);
					($l1, $l2) = unpack "CC", $head;
					$length = $l1 * 256 + $l2;
					seek IMG, $length - 2, 1;
				}
			}
		}
		close IMG;
		return "JPG", 0;
	}
	return 0;
}

###
sub crypt {
	@salt = ('a' .. 'z','A' .. 'Z','0' .. '9');
	srand;
	$salt = "$salt[int(rand($#salt))]$salt[int(rand($#salt))]";
	return crypt($_[0],$salt);
}

###
sub decrypt {
	$salt = $_[1] =~ /^\$1\$(.*)\$/ && $1 || substr($_[1],0,2);
	if (crypt($_[0],$salt) eq $_[1] || crypt($_[0],'$1$' . $salt) eq $_[1]) {return 1;}
	return 0;
}

###
sub error {
	#if (!$head) {&header; print "<body>\n";}
	print "<br><br><table cellpadding=\"3\"><tr><td>&nbsp;&nbsp;&nbsp;<strong>ERROR !!<font color=red><b>$_[0]</b></strong></td><td><a href=\"#\" onclick=\"history.back()\">[戻る]</a></td></tr></table><br /><br /><br />\n";
	#print "</body></html>\n";
	
	open (FILE,"$footer_tpl_path") || &error("OPEN ERROR_footer_tpl_path");
	while(my $str = <FILE>){ print $str; }
	close FILE;
	
	exit;
}
