#!/usr/bin/perl

###############################################
#   sche3.cgi
#      V2.1 (2005.6.2)
#                     Copyright(C) CGI-design
###############################################

require './cgi-lib.pl';

$script = 'sche3.cgi';
$base = './schedata';				#データ格納ディレクトリ
$nofile = "$base/no.txt";			#記事番号
$recfile = "$base/rec.txt";			#カラー情報
$opfile = "$base/option.txt";		#オプション
$cgi_lib'maxdata = 200000;			#入力最大容量（byte）

@fc = ('#000000','#800000','#0000ff','#008040','#ff0000','#c100c1','#ff80c0');
@bc = ('#d3e3fe','#e6edff','#d5fdec','#e6ffff','#ffd9ec','#ff9f9f','#be8eb5','#fcc592','#fee9c5','#f8ddcf','#fef5da','#ffffff');
@wcolor = ("#ff0000","#000000","#000000","#000000","#000000","#000000","#0000ff");
@week = ('日','月','火','水','木','金','土');
@mdays = (31,28,31,30,31,30,31,31,30,31,30,31);

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/title.gif<>$base/home.gif<>$base/last.gif<>$base/next.gif<><>$base<>$base<>#fafaf5<>#000000<>#800000<>#0000ff<>#ffffff<>200<>200";
	close OUT;
	chmod(0666,$recfile);	open (OUT,">$recfile") || &error("OPEN ERROR");		print OUT "$fc[0]<>$bc[0]";		close OUT;
	chmod(0666,$nofile);
}

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

open (IN,"$opfile") || &error("OPEN ERROR");
($pass,$title,$home,$title_icon,$home_icon,$last_icon,$next_icon,$bg_img,$savedir,$loaddir,$bg_color,$text_color,$title_color,$sub_color,$sche_color,$max_w,$max_h) = split(/<>/,<IN>);
close IN;

($sec,$min,$hour,$nowday,$nowmon,$nowyear) = localtime;
$nowyear += 1900;
$nowmon++;

$logyear = $in{'year'};
$logmon = $in{'mon'};
$logday = $in{'day'};
if (!$logyear) {$logyear = $nowyear;	$logmon = $nowmon;	$logday = $nowday;}
$logfile = "$base/$logyear$logmon.txt";

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

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\">\n";
	print "<title>$title</title><link rel=\"stylesheet\" type=\"text/css\" href=\"$loaddir/style.css\"></head>\n";
	$head = 1;
}

###
sub main {
	&header;
	print "<body background=\"$bg_img\" bgcolor=\"$bg_color\" text=\"$text_color\"><center>\n";
	print "<table width=98%><tr><td width=100 valign=top>\n";
	if ($home) {if ($home_icon) {print "<a href=\"$home\"><img src=\"$home_icon\" border=0></a>";} else {print "<a href=\"$home\">[HOME]</a>";}}
	print "</td><td align=center>";
	if ($title_icon) {print "<img src=\"$title_icon\">";} else {print "<font color=\"$title_color\" size=\"+1\"><b>$title</b></font>";}
	print "</td><td width=100 align=right>｜<a href=\"$script?mode=admin\">編集</a>｜</td></tr></table>\n";
	&dsp;
	# 次の行は著作権表示ですので削除しないで下さい。#
	print "<a href=\"http://cgi-design.net\" target=\"_blank\">CGI-design</a>\n";
}

###
sub dsp {
	@data=@lognum=();
	%logno=%logsub=();
	if (-e $logfile) {
		open (IN,"$logfile") || &error("OPEN ERROR");
		while (<IN>) {
			push (@data,$_);
			($no,$day,$wday,$sub,$com,$img_type) = split(/<>/);
			$lognum[$day]++;
			$dn = "$day-$lognum[$day]";
			if ($com || $img_type) {$logno{$dn} = $no;}
			$logsub{$dn} = $sub;
		}
		close IN;
	}
	$mdays = $mdays[$logmon - 1];
	if ($logmon == 2 && $logyear % 4 == 0) {$mdays = 29;}

	print "<table width=95%><tr><td width=60>　<b>$logyear年</b></td><td align=right>\n";
	$mon = $logmon - 1;
	if ($mon < 1) {$mon = 12; $year = $logyear - 1;} else {$year = $logyear;}
	print "<form action=\"$script\" method=\"POST\">\n";
	print "<input type=hidden name=mode value=\"$mode\">\n";
	print "<input type=hidden name=pass value=\"$inpass\">\n";
	print "<input type=hidden name=year value=\"$year\">\n";
	print "<input type=hidden name=mon value=\"$mon\">\n";
	print "<input type=image src=\"$last_icon\"></td></form>\n";
	print "<td width=100 align=center><font size=\"+2\"><b>$logmon月</b></font></td>\n";
	$mon = $logmon + 1;
	if (12 < $mon) {$mon = 1; $year = $logyear + 1;} else {$year = $logyear;}
	print "<td><form action=\"$script\" method=\"POST\">\n";
	print "<input type=hidden name=mode value=\"$mode\">\n";
	print "<input type=hidden name=pass value=\"$inpass\">\n";
	print "<input type=hidden name=year value=\"$year\">\n";
	print "<input type=hidden name=mon value=\"$mon\">\n";
	print "<input type=image src=\"$next_icon\"></td></form><td width=60></td></tr></table>\n";

	print "<table width=95% bgcolor=\"#ffffff\" bordercolor=\"#aaaaaa\" border=1 cellspacing=0 cellpadding=4 style=\"border-collapse: collapse\">\n";
	print "<tr bgcolor=\"#eeeeee\" align=center>\n";
	for (0 .. 6) {print "<td width=14%><font color=\"$wcolor[$_]\"><b>$week[$_]</b></font></td>\n";}
	print "</tr>\n";

	&holi_set;
	$wday = &get_date($logyear,$logmon,1);
	$w=$n=0;
	$k=1;
	for (0 .. 41) {
		if (!$w) {print "<tr valign=top>";}
		if ($wday <= $_ && $k <= $mdays) {
			if ($w == 1) {$n++;}
			$wcolor = $wcolor[$w];
			if (2002 < $logyear) {
				&get_holiday($logmon,$k,$w,$n);
				if ($holiday) {$wcolor = $wcolor[0];}
			} else {$holiday = '';}

			if ($logyear == $nowyear && $logmon == $nowmon && $k == $nowday) {$bc = '#efff00';}
			elsif (!$w || $holiday) {$bc = '#fef4ef';}
			elsif ($w == 6) {$bc = '#eeffff';}
			else {$bc = '#fffff2';}
			if ($k < 10) {$day = "&nbsp;$k";} else {$day = $k;}

			print "<td bgcolor=\"$bc\" height=80><font color=\"$wcolor\" size=\"+1\"><b>$day</b></font> <font size=\"-1\"><font color=\"$wcolor\">$holiday</font>\n";
			if ($lognum[$k]) {
				for (1 .. $lognum[$k]) {
					$dn = "$k-$_";
					if ($logno{$dn}) {print "<br><a href=\"\#$logno{$dn}\">$logsub{$dn}</a>\n";} else {print "<br>$logsub{$dn}\n";}
				}
			}
			print "</font></td>\n";
			$k++;
		} else {print "<td></td>\n";}
		$w++;
		if ($w == 7) {
			print "</tr>\n";
			if ($mdays < $k) {last;}
			$w = 0;
		}
	}
	print "</table><br><br>\n";
	foreach (@data) {
		($no,$day,$wday,$sub,$com,$img_type,$img_w,$img_h,$img_big,$ftcolor,$frcolor) = split(/<>/);
		if ($mode || $com || $img_type) {&dsp_log;}
	}
}

###
sub dsp_log {
	$com =~ s/([^=^\"]|^)(http\:[\w\.\~\-\/\?\&\+\=\:\@\%\;\#\%]+)/$1<a href=\"$2\" target=\"_blank\">$2<\/a>/g;
	print "<a name=\"$no\"></a><table width=700 bgcolor=\"$frcolor\" cellspacing=4 cellpadding=0 style=\"margin-top:5px;\">\n";
	print "<tr><td height=20>　<b>$logyear年$logmon月$day日<font color=\"$wcolor[$wday]\">($week[$wday])</font></b>　　<font color=\"$sub_color\"><b>$sub</b></font></td>\n";
	if ($mode eq 'admin') {
		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=hidden name=act value=\"edt\">\n";
		print "<input type=hidden name=no value=\"$no\">\n";
		print "<input type=hidden name=year value=\"$logyear\">\n";
		print "<input type=hidden name=mon value=\"$logmon\">\n";
		print "<input type=submit value=\"修正\"></td></form>\n";
	}
	print "</tr><tr><td colspan=2><table width=100% bgcolor=\"$sche_color\" cellspacing=8><tr><td>\n";
	if ($img_type) {
		$imgfile = "$loaddir/$no.$img_type";
		$imgsrc = "<img src=\"$imgfile\" border=0 width=$img_w height=$img_h hspace=8 vspace=2 align=left>";
		if ($img_big) {print "<a href=\"$imgfile\" target=\"_blank\">$imgsrc</a>";} else {print $imgsrc;}
	}
	print "<font color=\"$ftcolor\">$com</font></td></tr></table></td></tr></table>\n";
	print "<table width=680 cellpadding=0><tr><td align=right><a href=\"\#top\">▲top</a></td></tr></table>\n";
}

###
sub get_date {
	my($y,$m,$d) = @_;
	if ($m < 3){$y--; $m+=12;}
	return ($y+int($y/4)-int($y/100)+int($y/400)+int((13*$m+8)/5)+$d)%7;
}

###
sub holi_set {
	$def = 0.242194*($logyear-1980)-int(($logyear-1980)/4);
	$spr = int(20.8431+$def);
	$aut = int(23.2488+$def);
	%holi_d = ('0101','元日','0211','建国記念の日',"03$spr",'春分の日','0429','みどりの日','0503','憲法記念日','0505','こどもの日',"09$aut",'秋分の日','1103','文化の日','1123','勤労感謝の日','1223','天皇誕生日');
	%holi_w = ('012','成人の日','073','海の日','093','敬老の日','102','体育の日');
}

###
sub get_holiday {
	$sm = sprintf("%02d%02d",$_[0],$_[1]);
	$holiday = $holi_d{$sm};
	if ($sm eq '0504' && 1 < $_[2]) {$holiday = '国民の休日';}
	if ($holiday && !$_[2]) {$hflag = 1;}
	if (!$holiday && $_[2] == 1) {
		$smw = sprintf("%02d$_[3]",$_[0]);
		$holiday = $holi_w{$smw};
		if ($hflag) {$holiday = '振替休日'; $hflag = 0;}
	}
	if ($logyear eq '2009' && $sm eq '0922') {$holiday = '国民の休日';}
}

###
sub admin {
	&header;
	print "<body><center>\n";
	$inpass = $in{'pass'};
	if ($inpass eq '') {
		print "<table width=97%><tr><td><a href=\"$script\">[Return]</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 name=pass size=10 maxlength=8>\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=\"$script\"><font color=\"#ffffff\"><b>Return</b></font></a></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=\"　設定　\"></td></form><td width=10></td></tr></table><br>\n";

	$act = $in{'act'};
	if ($in{'set'}) {&setup;} else {&edt;}
}

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

	&in_form;
	print "<a name=\"top\"></a><hr width=95%>記事を修正・削除する場合は[修正]をクリックして下さい。<br><br>\n";
	&dsp;
}

###
sub in_form {
	print "<table bgcolor=\"#edefde\" cellspacing=8><tr><td><table cellspacing=2 cellpadding=0>\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";
	if (!$act) {
		print "<tr><td>日付</td><td><select name=year>\n";
		for (2004 .. $nowyear+1) {
			if ($_ == $logyear) {$sel = ' selected';} else {$sel = '';}
			print "<option value=\"$_\"$sel>$_</option>\n";
		}
		print "</select>年 <select name=mon>\n";
		for (1 .. 12) {
			if ($_ == $logmon) {$sel = ' selected';} else {$sel = '';}
			print "<option value=\"$_\"$sel>$_</option>\n";
		}
		print "</select>月 <select name=day>\n";
		for (1 .. 31) {
			if ($_ == $logday) {$sel = ' selected';} else {$sel = '';}
			print "<option value=\"$_\"$sel>$_</option>\n";
		}
		print "</select>日</td></tr>\n";
		$sub=$com='';
		open (IN,"$recfile") || &error("OPEN ERROR");	($ftcolor,$frcolor) = split(/<>/,<IN>);		close IN;
	} else {
		print "<input type=hidden name=year value=\"$logyear\">\n";
		print "<input type=hidden name=mon value=\"$logmon\">\n";
		print "<input type=hidden name=no value=\"$in{'no'}\">\n";
		open (IN,"$logfile") || &error("OPEN ERROR");
		while (<IN>) {
			($no,$day,$wday,$sub,$com,$img_type,$img_w,$img_h,$img_big,$ftcolor,$frcolor) = split(/<>/);
			if ($no eq $in{'no'}) {last;}
		}
		close IN;
		$com =~ s/<br>/\r/g;
		print "<tr><td>日付</td><td>&nbsp;<b>$logyear年$logmon月$day日<font color=\"$wcolor[$wday]\">($week[$wday])</font></b></td></tr>\n";
	}
	print "<tr><td>題名</td><td><input type=text name=sub size=50 value=\"$sub\"></td></tr>\n";
	print "<tr><td valign=top><br>内容</td><td><textarea cols=80 rows=20 name=com wrap=\"soft\">$com</textarea></td></tr>\n";
	print "<tr><td>画像</td><td><input type=file name=img size=70></td></tr>\n";
	print "<tr><td>文字色</td><td><table cellspacing=0 cellpadding=0><tr align=center>\n";
	foreach (@fc) {
		if ($_ eq $ftcolor) {$chk = ' checked';} else {$chk = '';}
		print "<td width=35 bgcolor=\"$_\"><input type=radio name=ftcolor value=\"$_\"$chk></td>\n";
	}
	print "</tr></table></td></tr>\n";
	print "<tr><td>枠色</td><td><table cellspacing=0 cellpadding=0><tr align=center>\n";
	foreach (@bc) {
		if ($_ eq $frcolor) {$chk = ' checked';} else {$chk = '';}
		print "<td width=35 bgcolor=\"$_\"><input type=radio name=frcolor value=\"$_\"$chk></td>\n";
	}
	print "</tr></table></td></tr>\n";
	print "<tr><td></td><td>";
	if (!$act) {print "<input type=submit name=newwrt value=\"登録する\">";}
	else {
		print "<table width=100% cellspacing=0 cellpadding=2><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";
	}
	print "</td></tr></table></td></tr></table></form>\n";
}

###
sub newwrt {
	$in{'com'} =~ s/\r\n|\r|\n/<br>/g;
	$wday = &get_date($logyear,$logmon,$logday);

	open (IN,"$nofile") || &error("OPEN ERROR"); 		$no = <IN>; 		close IN;
	$no++;
	open (OUT,">$nofile") || &error("OPEN ERROR");		print OUT $no;		close OUT;
	&img("$savedir/$no",'img');
	$newdata = "$no<>$logday<>$wday<>$in{'sub'}<>$in{'com'}<>$type<>$width<>$height<>$big<>$in{'ftcolor'}<>$in{'frcolor'}<>\n";

	if (-e $logfile) {
		@new = ();
		$flag = 0;
		open (IN,"$logfile") || &error("OPEN ERROR");
		while (<IN>) {
			($no,$day) = split(/<>/);
			if (!$flag && $logday < $day) {push(@new,$newdata); $flag = 1;}
			push(@new,$_);
		}
		close IN;
		if (!$flag) {push(@new,$newdata);}
		open (OUT,">$logfile") || &error("OPEN ERROR");		print OUT @new;			close OUT;
	} else {
		open (OUT,">$logfile") || &error("OPEN ERROR");		print OUT $newdata;		close OUT;		chmod(0666,$logfile);
	}
	open (OUT,">$recfile") || &error("OPEN ERROR");			print OUT "$in{'ftcolor'}<>$in{'frcolor'}";		close OUT;
}

###
sub edtwrt {
	$in{'com'} =~ s/\r\n|\r|\n/<br>/g;
	&img("$savedir/$in{'no'}",'img');
	@new = ();
	open (IN,"$logfile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$day,$wday,$sub,$com,$img_type,$img_w,$img_h,$img_big) = split(/<>/);
		if ($no eq $in{'no'}) {
			if ($type) {$img_type = $type; $img_w = $width; $img_h = $height; $img_big = $big;}
			push(@new,"$no<>$day<>$wday<>$in{'sub'}<>$in{'com'}<>$img_type<>$img_w<>$img_h<>$img_big<>$in{'ftcolor'}<>$in{'frcolor'}<>\n");
		} else {push(@new,$_);}
	}
	close IN;
	open (OUT,">$logfile") || &error("OPEN ERROR");		print OUT @new;		close OUT;
}

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

###
sub setup {
	if ($in{'wrt'}) {
		if ($in{'newpass'} ne '') {$pass = &crypt($in{'newpass'});}
		$title = $in{'title'};
		$home = $in{'home'};
		$title_icon = $in{'title_icon'};
		$home_icon = $in{'home_icon'};
		$last_icon = $in{'last_icon'};
		$next_icon = $in{'next_icon'};
		$bg_img = $in{'bg_img'};
		$savedir = $in{'savedir'};
		$loaddir = $in{'loaddir'};

		$bg_color = $in{'color0'};
		$text_color = $in{'color1'};
		$title_color = $in{'color2'};
		$sub_color = $in{'color3'};
		$sche_color = $in{'color4'};

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

		open (OUT,">$opfile") || &error("OPEN ERROR");
		print OUT "$pass<>$title<>$home<>$title_icon<>$home_icon<>$last_icon<>$next_icon<>$bg_img<>$savedir<>$loaddir<>$bg_color<>$text_color<>$title_color<>$sub_color<>$sche_color<>$max_w<>$max_h";
		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=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 name=title size=60 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=title_icon value=\"$title_icon\">";
	if ($title_icon) {print "　<img src=\"$title_icon\">";}
	print "</td></tr>\n";
	print "<tr><td><b>ホームアイコン</b></td><td><input type=text size=60 name=home_icon value=\"$home_icon\">\n";
	if ($home_icon) {print "　<img src=\"$home_icon\">";}
	print "</td></tr>\n";
	print "<tr><td><b>LASTアイコン</b></td><td><input type=text size=60 name=last_icon value=\"$last_icon\">　<img src=\"$last_icon\"></td></tr>\n";
	print "<tr><td><b>NEXTアイコン</b></td><td><input type=text size=60 name=next_icon value=\"$next_icon\">　<img src=\"$next_icon\"></td></tr>\n";
	print "<tr><td><b>壁紙</b></td><td><input type=text size=60 name=bg_img value=\"$bg_img\">\n";
	if ($bg_img) {print "　<img src=\"$bg_img\" width=30 align=middle>";}
	print "</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></td><td><a href=\"$base/color.htm\" target=\"_blank\">カラーコード</a></td></tr>\n";
	@name = ('基本背景色','基本文字色','タイトル色','題名色','内容背景色');
	@data = ($bg_color,$text_color,$title_color,$sub_color,$sche_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[$_]\"></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 name=max_w size=4 value=\"$max_w\" style=\"text-align: right\">px、 縦max<input type=text name=max_h size=4 value=\"$max_h\" style=\"text-align: right\">px</td></tr>\n";
	print "<tr><td><b>パスワード変更</b></td><td><input type=password name=newpass size=10 maxlength=8> （英数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;
}
