#!/usr/bin/perl

###############################################
#   inf10.cgi改造
#      V1.0 (2009.1.6)
#                     Copyright(C) CGI-design
###############################################

require './cgi-lib.pl';

$script = 'inf10.cgi';
$base = './infdata';				#データ格納ディレクトリ
$catfile = "$base/cat.txt";			#分類
$cnofile = "$base/cno.txt";			#分類番号
$nofile = "$base/no.txt";			#記事番号
$opfile = "$base/option.txt";
$cgi_lib'maxdata = 40000000;			#入力最大容量（byte）

open (IN,"$opfile") || &error("OPEN ERROR");	$opdata = <IN>;		close IN;
if (!$opdata) {
	$pass = &crypt('cgi');
	chmod(0666,$opfile);
	open(OUT,">$opfile") || &error("OPEN ERROR");
	print OUT "$pass<>情報<><>$base<>$base<><><>#ECF0EC<>#000000<>#CAD8CB<>#5A6D45<>#ffffff<>#5A6D45<>160<>160<>25";
	close OUT;
	chmod(0666,$catfile);	chmod(0666,$cnofile);		chmod(0666,$nofile);
}

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

open (IN,"$opfile") || &error("OPEN ERROR");
($pass,$title,$home,$savedir,$loaddir,$home_icon,$bg_img,$bg_color,$text_color,$mbg_color,$cat_color,$sub_color,$subbg_color,$max_w,$max_h,$menurate) = split(/<>/,<IN>);
close IN;
if ($menurate == 0) {$menurate = 25;}

$catno = $in{'cno'};
if (!$catno) {open(IN,"$catfile") || &error("OPEN ERROR"); ($catno) = split(/<>/,<IN>); close IN;}
$inffile = "$base/$catno.txt";

if ($mode eq 'menu') {&menu;}
elsif ($mode eq 'main') {&main;}
elsif ($mode eq 'admin') {&admin;}
else {&frame;}

print "</center></body></html>\n";
exit;

###
sub header {
	print "Content-type: text/html\n\n";
	print "<html><head><meta http-equiv=\"content-type\" content=\"text/html;charset=shift_jis\" />
		<meta http-equiv=\"content-style-type\" content=\"text/css\" />
		<meta http-equiv=\"Content-Script-Type\" content=\"text/javascript\" />
		<meta name=\"keywords\" content=\"tanomura,タノムラ,鉄工所,破砕機,ギター,玉野市,造船,機械加工,金属加工,機械\" />
		<meta name=\"description\" content=\"今年創業70周年を迎える、熔断／製缶／機械加工の主力3部門を有する総合加工メーカー。主力は船舶用ディーゼルエンジン部品の加工、製造。自社製の破砕機もあり。音楽関係へのリンクもある。\" />
		<link href=\"mailto:info@tanomura.co.jp\" rev=\"made\" />
		<link href=\"index.html\" rel=\"next\" />
		<link href=\"favicon.ico\" rel=\"shortcut icon\" />
		<!--lightbox-->
		<script type=\"text/javascript\" src=\"./js/prototype.js\"></script>
		<script type=\"text/javascript\" src=\"./js/scriptaculous.js?load=effects\"></script>
		<script type=\"text/javascript\" src=\"./js/lightbox.js\"></script>
		<link href=\"./lightbox.css\" type=\"text/css\" rel=\"stylesheet\" media=\"screen\" />
		<!--lightboxここまで-->
		<link href=\"style.css\" type=\"text/css\" rel=\"stylesheet\" media=\"screen\" />
		<link href=\"style_p.css\" type=\"text/css\" rel=\"stylesheet\" media=\"print\" />
		<title>新着情報 - 株式会社タノムラ</title></head>\n";
	$head = 1;
}

###
sub frame {
	&header;
	print "<frameset cols=\"$menurate%,*\" border=0 frameborder=0>\n";
	print "<frame src=\"$script?mode=menu\" scrolling=\"auto\">\n";
	print "<frame src=\"$script?mode=main\" name=\"inf10\" scrolling=\"auto\">\n";
	print "</frameset>\n";
	print "</html>\n";
	exit;
}

###
sub menu {
	&header;
	print "<body><center>\n";
	if ($home) {
		print "<table width=100%><tr><td><a href=\"$home\" target=\"_parent\">";
		if ($home_icon) {print "<img src=\"$loaddir/$home_icon\" border=0>";} else {print "HOME";}
		print "</a></td></tr></table>\n";
	}
	print "<ul class=\"topics_ul\">\n";
	open (IN,"$catfile") || &error("OPEN ERROR");
	while (<IN>) {
		($cno,$date,$cat) = split(/<>/);
		print "<li>[$date] <a href=\"$script?mode=main&cno=$cno\"   target=\"_parent\">$cat</a></li>\n";
	}
	close IN;
	print "</ul>\n";
}

###
sub main {
	&header;
	print "
<body>
<!--ヘッダブロック-->
<div id=\"header\">
<div id=\"header_main\">
<h1><span><a href=\"index.html\">鉄と人のパワフル・ネットワーク　株式会社タノムラ</a></span><span>Let's have fun of making things! TANOMURA CORPORATION</span></h1>
<!--ヘッダメニュー-->
<dl id=\"header_menu\">
<dt id=\"header_menu1\"><a href=\"index.html\">HOME</a></dt>
<dt id=\"header_menu2\">　</dt>
<dt id=\"header_menu3\"><a href=\"contact.html\">お問い合わせ</a></dt>
<dt id=\"header_menu4\"><a href=\"link.html\">リンク</a></dt>
<dt id=\"header_menu5\"><a href=\"http://www.optic.or.jp/com/tanomura/tanomura_e.html\" target=\"_blank\">English</a></dt>
</dl>
<!--ヘッダメニューここまで-->
</div>
</div>
<!--ヘッダブロックここまで-->
		
<!--コンテンツブロック-->
<div id=\"sub_contents\">
<!--レフト-->
<div id=\"sub_contents_l\">
<!--メインメニュー-->
<dl id=\"sub_mainmenu\">
<dt>mainmenu</dt>
<dd class=\"dd\"><a href=\"company.html\">会社案内</a></dd>
<dd class=\"dd\"><a href=\"http://www.optic.or.jp/com/tanomura/tanomura.html\" target=\"_blank\">OPTIC(タノムラ)</a></dd>
</dl>
<br class=\"clear\" />
<!--メインメニューここまで-->
</div>
<!--レフトここまで-->
<!--ライト-->
<div id=\"sub_contents_r\">
<p id=\"pankuzu\">現在地：<a href=\"index.html\">ホーム</a> ＞ 新着情報</p>
<h2 class=\"h2_purple\"><strong>新着情報</strong><span>- Corporate guidance -</span></h2>
<!--ここから本文-->
\n";
	open (IN,"$catfile") || &error("OPEN ERROR");
	while (<IN>) {
		($cno,$date,$cat) = split(/<>/);
		if ($cno eq $catno) {last;}
	}
	print "<div class=\"p_left\">\n";
	close IN;
	print "<h3 class=\"h3_bg\">[ $date ] $cat</h3>\n";

	if ($catno) {
		open (IN,"$inffile") || &error("OPEN ERROR");
		while (<IN>) {
			($no,$sub,$com,$side,$img) = split(/<>/);
			&dsp;
		}
		close IN;
	}
	print "</div>\n";
	# 次の行は著作権表示ですので削除しないで下さい。#
	print "<br class=\"clear\">\n";
	print "<p class=\"page_top\"><a href=\"#header\">▲ページのトップへ</a></p>\n";
	print "<p class=\"p_right\"><span class=\"small\">Powered by </span><a class=\"small\" href=\"http://cgi-design.net\" target=\"_blank\">CGI-design</a></p>
\n";

print "

<!--ここまで本文-->
</div>
<!--ライトここまで-->
<br class=\"clear\" />
<p id=\"sub_contents_end\"></p>
</div>
<!--コンテンツブロックここまで-->

<!--フッターブロック-->
<div id=\"footer\">
<div id=\"footer_main\">
<p class=\"small\">｜ <a href=\"index.html\">HOME</a> ｜ <a href=\"company.html\">会社概要</a> ｜ <a href=\"http://www.optic.or.jp/com/tanomura/tanomura.html\" target=\"_blank\">OPTIC（タノムラ）</a> ｜ <a href=\"contact.html\">お問い合わせ</a> ｜ <a href=\"link.html\">リンク</a> ｜ <a href=\"http://www.optic.or.jp/com/tanomura/tanomura_e.html\" target=\"_blank\">English</a> ｜</p>
<p><strong>株式会社 タノムラ</strong>　〒706-0151 岡山県玉野市長尾765 TEL:0863-71-1511 FAX:0863-71-3763<br />Copyright&copy; TANOMURA CORPORATION. All Rights Reserved.</p>
</div>
</div>
<!--フッターブロックここまで-->
</body>

\n";



}

###
sub dsp {
	$com =~ s/([^=^\"]|^)(http\:[\w\.\~\-\/\?\&\+\=\:\@\%\;\#\%]+)/$1<a href=\"$2\" target=\"_blank\">$2<\/a>/g;
	if ($sub) {print "<h4 class=\"h4_purple\">$sub</h4>\n";}
	print "<table width=100%><tr><td>\n";
	@img = split(/:/,$img);
	$n = 0;
	for ($i=0; $i<$#img; $i+=4) {if ($img[$i]) {$n++;}}
	if ($n < 3) {
		if ($side) {&imgdsp(' align=right');} else {&imgdsp(' align=left');}
		print "$com";
	} else {
		print "$com</td></tr><tr><td align=center>\n";
		&imgdsp('');
	}
	print "</td></tr></table><br class=\"clear\" />\n";
}

###
sub imgdsp {
	$j = 0;
	for ($i=0; $i<$#img; $i+=4) {
		if ($img[$i]) {
			$imgfile = "$loaddir/$no-$j.$img[$i]";
			$imgsrc = "<img class=\"topics_img\" src=\"$imgfile\" border=0 width=$img[$i+1] height=$img[$i+2] hspace=7$_[0]>";
			if ($img[$i+3]) {print "<a href=\"$imgfile\" target=\"_blank\" rel=\"lightbox[sample]\" title=\"拡大画像\">$imgsrc</a>";} else {print $imgsrc;}
		}
		$j++;
	}
}

###
sub admin {
	&header;
	print "<body><center>\n";
	$inpass = $in{'pass'};
	if ($inpass eq '') {
		print "<table width=97%><tr><td><a href=\"index.html\">トップページへ戻る</a></td></tr></table>\n";
		print "<br><br><br><br><h4>パスワードを入力して下さい</h4>\n";
		print "<form action=\"$script\" method=POST>\n";
		print "<input type=hidden name=mode value=\"admin\">\n";
		print "<input type=password size=10 maxlength=8 name=pass>\n";
		print " <input type=submit value=\" 認証 \"></form>\n";
		print "</center></body></html>\n";
		exit;
	}
	$mat = &decrypt($inpass,$pass);
	if (!$mat) {&error("パスワードが違います");}

	print "<table width=95% bgcolor=\"#8c4600\"><tr><td>　<a href=\"index.html\"><font color=\"#ffffff\"><b>トップページへ戻る</b></font></a></td>\n";
	print "<form action=\"$script\" method=POST><td align=right>\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=cat value=\"記事設定\">\n";
	print "<input type=submit name=set value=\"基本設定\"></td></form><td width=10></td></tr></table><br>\n";

	$wrt = $in{'wrt'};
	if ($in{'cat'}) {&catset;}
	elsif ($in{'set'}) {&setup;}
	else {&edtin;}
}

###
sub edtin {
	if (!$catno) {print "<br><br><br>記事を設定して下さい。\n"; return;}

	if ($in{'newwrt'}) {&newwrt;}
	elsif ($in{'edtwrt'}) {&edtwrt;}
	elsif ($in{'delwrt'}) {&delwrt;}
	elsif ($in{'sort'}) {&edtsort;}

	&in_form;
	&cat_idx;
	
	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=cno value=\"$catno\">\n";
	print "<input type=hidden name=edt value=\"1\">\n";
	print "<hr width=90%><input type=submit name=sort value=\"並び替え\">　段落の並び替えは表\示順を指定して「並び替え」を押して下さい。<br><br>\n";

	$rate = 106 - $menurate;
	print "<table width=$rate% cellspacing=0 cellpadding=0><col span=2 valign=top>\n";
	$k = 0;
	open (IN,"$inffile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$sub,$com,$side,$img) = split(/<>/);
		print "<tr><td><input type=text size=4 name=num$k value=\"",$k+1,"\" style=\"text-align:right; ime-mode:disabled;\"></td>\n";
		print "<td><input type=submit name=$no value=\"修正\"></td><td width=91% bgcolor=\"$bg_color\">\n";
		&dsp;
		print "</td></tr>\n";
		$k++;
	}
	close IN;
	print "<tr><td></td><td></td><td bgcolor=\"$bg_color\"><hr color=\"$subbg_color\"></td></tr>\n";
	print "</table></form>\n";
}

###
sub cat_idx {
	$catcols = int(100 / $menurate);
	$perw = int(100 / $catcols);
	print "<table width=90% bgcolor=\"#aaaaaa\" cellspacing=1 cellpadding=1>\n";
	$k = 0;
	open (IN,"$catfile") || &error("OPEN ERROR");
	while (<IN>) {
		($cno,$date,$cat) = split(/<>/);
		if (!$k) {print "<tr bgcolor=\"#ffffff\">";}
		if ($cno eq $catno) {print "<td width=$perw% bgcolor=\"#ffff00\">";} else {print "<td width=$perw%>";}
		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=cno value=\"$cno\">\n";
		print "<input type=submit value=\"選択\"> $cat</td></form>\n";
		$k++;
		if ($k == $catcols) {print "</tr>\n"; $k = 0;}
	}
	close IN;
	if ($k) {
		for ($k+1 .. $catcols) {print "<td></td>";}
		print "</tr>";
	}
	print "</table><br>\n";
}

###
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";
	print "<input type=hidden name=cno value=\"$catno\">\n";

	if ($in{'edt'}) {
		open (IN,"$inffile") || &error("OPEN ERROR");
		while (<IN>) {
			($no,$sub,$com,$side,$img) = split(/<>/);
			if ($in{$no}) {last;}
		}
		close IN;
		print "<input type=hidden name=no value=\"$no\">\n";
		$com =~ s/<br>/\r/g;
		@img = split(/:/,$img);
	} else {
		$sub=$com=$side='';
		@img = ();
	}
	print "<table bgcolor=\"#e6e4ce\" cellspacing=10><tr><td><table cellspacing=1 cellpadding=0>\n";
	print "<tr><td>段落<br>タイトル</td><td><input type=text size=60 name=sub value=\"$sub\" style=\"ime-mode:active;\">　<small>つけない場合は空白にしておいて下さい。</small></td></tr>\n";
	print "<tr><td valign=top><br>内容</td><td><textarea cols=80 rows=12 name=com style=\"ime-mode:active;\">$com</textarea><br><a class=\"small\" href=\"./setsumei.html\" target=\"_blank\">色・大きさ等の装飾について</a></td></tr>\n";
	for (0 .. 3) {
		print "<tr><td>画像",$_+1,"</td><td><input type=file size=60 name=img$_>";
		if ($img[4*$_]) {print " <input type=checkbox name=del$_ value=\"1\">削除";}
		print "</td></tr>\n";
	}
	$chk0=$chk1='';
	if ($side) {$chk1 = ' checked';} else {$chk0 = ' checked';}
	print "<tr><td></td><td>画像位置　<input type=radio name=side value=\"0\"$chk0>左寄せ　<input type=radio name=side value=\"1\"$chk1>右寄せ　　（画像位置は画像が2枚以下の場合に有効）</td></tr>\n";
	print "<tr><td></td><td>";
	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><br>\n";
}

###
sub edtsort {
	@new = ();
	open (IN,"$inffile") || &error("OPEN ERROR");		@data = <IN>;		close IN;
	for (0 .. $#data) {$num{$_} = $in{"num$_"};}
	foreach (sort {$num{$a} <=> $num{$b}} keys(%num)) {push(@new,$data[$_]);}
	open (OUT,">$inffile") || &error("OPEN ERROR");		print OUT @new;		close OUT;
	$in{'edt'} = '';
}

###
sub newwrt {
	$in{'com'} =~ s/\r\n|\r|\n/<br>/g;
	open (IN,"$nofile") || &error("OPEN ERROR"); 		$no = <IN>; 		close IN;
	$no++;
	open (OUT,">$nofile") || &error("OPEN ERROR");		print OUT $no;		close OUT;

	$img = '';
	for (0 .. 3) {
		&img("$savedir/$no-$_","img$_");
		$img .= "$type:$width:$height:$big:";
	}
	open (OUT,">>$inffile") || &error("OPEN ERROR");
	print OUT "$no<>$in{'sub'}<>$in{'com'}<>$in{'side'}<>$img<>\n";
	close OUT;
}

###
sub edtwrt {
	$in{'com'} =~ s/\r\n|\r|\n/<br>/g;
	@new = ();
	open (IN,"$inffile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$sub,$com,$side,$img) = split(/<>/);
		if ($no eq $in{'no'}) {
			@img = split(/:/,$img);
			$img = '';
			for $n (0 .. 3) {
				if ($in{"del$n"}) {
					unlink "$savedir/$no-$n.$img[4*$n]";
					$img .= "::::";
				} else {
					&img("$savedir/$no-$n","img$n");
					if ($type) {
						if ($type ne $img[4*$n]) {unlink "$savedir/$no-$n.$img[4*$n]";}
						$img .= "$type:$width:$height:$big:";
					} else {
						$img .= "$img[4*$n]:$img[4*$n+1]:$img[4*$n+2]:$img[4*$n+3]:";
					}
				}
			}
			push(@new,"$no<>$in{'sub'}<>$in{'com'}<>$in{'side'}<>$img<>\n");
		} else {push(@new,$_);}
	}
	close IN;
	open (OUT,">$inffile") || &error("OPEN ERROR");		print OUT @new;		close OUT;
}

###
sub delwrt {
	@new = ();
	open (IN,"$inffile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$sub,$com,$side,$img) = split(/<>/);
		if ($no eq $in{'no'}) {
			@img = split(/:/,$img);
			for (0 .. 3) {
				if ($img[4*$_]) {unlink "$savedir/$no-$_.$img[4*$_]";}
			}
		} else {push(@new,$_);}
	}
	close IN;
	open (OUT,">$inffile") || &error("OPEN ERROR");		print OUT @new;		close OUT;
}

###
sub catset {
	if ($wrt) {
		@new = ();
		%num = ();
		open (IN,"$catfile") || &error("OPEN ERROR");		@cat = <IN>;		close IN;
		for (0 .. $#cat) {$num{$_} = $in{"num$_"};}
		foreach (sort {$num{$a} <=> $num{$b}} keys(%num)) {
			($cno) = split(/<>/,$cat[$_]);
			if ($in{"del$cno"}) {
				$inffile = "$base/$cno.txt";
				open (IN,"$inffile") || &error("OPEN ERROR");
				while (<IN>) {
					($no,$sub,$com,$side,$img) = split(/<>/);
					@img = split(/:/,$img);
					for (0 .. 3) {
						if ($img[4*$_]) {unlink "$savedir/$no-$_.$img[4*$_]";}
					}
				}
				close IN;
				unlink "$inffile";
			} else {
				push(@new,"$cno<>$in{\"date$cno\"}<>$in{\"cat$cno\"}<>\n");
			}
		}
		open (OUT,">$catfile") || &error("OPEN ERROR");		print OUT @new;		close OUT;
	} elsif ($in{'add'}) {
		open (IN,"$cnofile") || &error("OPEN ERROR");		$cno = <IN>;		close IN;
		$cno++;
		open (OUT,">$cnofile") || &error("OPEN ERROR");		print OUT $cno;		close OUT;
		$inffile = "$base/$cno.txt";
		open (OUT,">$inffile") || &error("OPEN ERROR");		close OUT;			chmod(0666,$inffile);

		($sec,$min,$hour,$mday,$mon,$year) = localtime;
		$date = sprintf("%4d.%02d.%02d",$year+1900,++$mon,$mday);
		open (IN,"$catfile") || &error("OPEN ERROR");		@new = <IN>;		close IN;
		unshift(@new,"$cno<>$date<><>\n");
		open (OUT,">$catfile") || &error("OPEN ERROR");		print OUT @new;		close OUT;
	}
	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=cat value=\"1\">\n";
	print "<input type=submit name=wrt value=\"登録する\">　<input type=submit name=add value=\"記事の追加\"><br><br>\n";

	print "<table bgcolor=\"#ddffff\" bordercolor=\"#cdcdcd\" border=1 cellspacing=0 cellpadding=2 style=\"border-collapse: collapse\">\n";
	print "<tr bgcolor=\"#e6edff\" align=center><td width=60>表\示順</td><td width=120>記事登録日</td><td width=300>記事タイトル</td><td width=40>削除</td></tr>\n";
	$k = 0;
	open (IN,"$catfile") || &error("OPEN ERROR");
	while (<IN>) {
		($cno,$date,$cat) = split(/<>/);
		print "<tr align=center><td><input type=text size=4 name=num$k value=\"",$k+1,"\" style=\"text-align:right; ime-mode:disabled;\"></td>\n";
		print "<td><input type=text size=15 name=date$cno value=\"$date\" style=\"ime-mode:inactive;\"></td>\n";
		print "<td><input type=text size=50 name=cat$cno value=\"$cat\" style=\"ime-mode:active;\"></td>\n";
		print "<td bgcolor=red><input type=checkbox name=del$cno value=\"1\"></td></tr>\n";
		$k++;
	}
	close IN;
	print "</table></form>\n";
}

###
sub setup {
	if ($wrt) {
		if ($in{'newpass'} ne '') {$pass = &crypt($in{'newpass'});}
		$title = $in{'title'};
		$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;}
		}
		$bg_color = $in{'color0'};
		$text_color = $in{'color1'};
		$mbg_color = $in{'color2'};
		$cat_color = $in{'color3'};
		$sub_color = $in{'color4'};
		$subbg_color = $in{'color5'};

		$max_w = $in{'max_w'};			$max_h = $in{'max_h'};
		$menurate = $in{'menurate'};

		open (OUT,">$opfile") || &error("OPEN ERROR");
		print OUT "$pass<>$title<>$home<>$savedir<>$loaddir<>$home_icon<>$bg_img<>$bg_color<>$text_color<>$mbg_color<>$cat_color<>$sub_color<>$subbg_color<>$max_w<>$max_h<>$menurate";
		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";
	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";
	@name = ('壁紙','ホーム');
	@data = ($bg_img,$home_icon);
	@icon = ('wall','home');
	print "<tr><td valign=top><br><b>壁紙、アイコン</b></td><td>\n";
	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 = ('基本背景色','基本文字色','記事背景色','記事色','題名色','題名背景色');
	@data = ($bg_color,$text_color,$mbg_color,$cat_color,$sub_color,$subbg_color);
	for (0 .. $#name) {
		print "<tr><td><b>$name[$_]</b></td><td><table cellspacing=0 cellpadding=0><tr>\n";
		print "<td><input type=text name=color$_ size=10 value=\"$data[$_]\" style=\"ime-mode:inactive;\"></td>\n";
		print "<td width=5></td><td width=80 bgcolor=\"$data[$_]\"></td></tr></table></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=text size=4 name=menurate value=\"$menurate\" style=\"text-align:right; ime-mode:disabled;\">%</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";
}

###
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';}
		}
		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);

	($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><center>\n";}
	print "<br><br><br><br><h3>ERROR !!</h3><font color=red><b>$_[0]</b></font>\n";
	print "</center></body></html>\n";
	exit;
}
