#!/usr/bin/perl

###############################################
#   voice.cgi
#      V1.0 (2008.1.15)
#                     Copyright(C) CGI-design
###############################################

require './cgi-lib.pl';

$script = 'voice.cgi';
$base = './voicedata';				#データ格納ディレクトリ
$voicefile = "$base/voice.txt";		#記事
$nofile = "$base/no.txt";			#記事番号
$catfile = "$base/cat.txt";			#分類
$cnofile = "$base/cno.txt";			#分類番号
$opfile = "$base/option.txt";
$cgi_lib'maxdata = 200000;			#入力最大容量（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<>./temp.html<>$base<>$base<>#ffffff,#000000,#C48E74,#ffffee,#C48E74,#ffffee,#FF6600<>4<>20<>500<>160<>160";
	close OUT;
	chmod(0666,$voicefile);		chmod(0666,$nofile);	chmod(0666,$catfile);	chmod(0666,$cnofile);
}

###　メイン処理　###
&ReadParse;

$mode = $in{'mode'};
$catno = $in{'cno'};
$num = $in{'num'};

open (IN,"$opfile") || &error("OPEN ERROR");
($pass,$tempfile,$savedir,$loaddir,$colors,$cols,$page,$dspw,$max_w,$max_h) = split(/<>/,<IN>);
close IN;
($bg_color,$text_color,$frame_color,$catbg_color,$sub_color,$combg_color,$vol_color) = split(/,/,$colors);
if ($cols == 0) {$cols = 4;}
if ($page == 0) {$page = 20;}
$perw = int(100 / $cols);
$back = $num - $page;
$next = $num + $page;

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

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

###
sub top {
	&header;
	print "<body bgcolor=\"$bg_color\" text=\"$text_color\">\n";
	$m = -1;
	@data = ();
	open (IN,"$voicefile") || &error("OPEN ERROR");
	while (<IN>) {
		$m++;
		if ($m < $num || $next <= $m) {next;}
		push(@data,$_);
	}
	close IN;
	print "<div class=\"frame\"><b>現在のコメント <a href=\"$script\" target=\"_parent\"><font color=\"$vol_color\">",$m+1,"件</font></a></b>\n";

	print "<table cellspacing=0>\n";
	foreach (@data) {
		($no,$cno,$imgt,$imgw,$imgh,$imgb,$year,$mon,$day,$sub) = split(/<>/);
		print "<tr><td nowrap>$mon/$day</td><td nowrap><a href=\"$script?mode=detail&no=$no\" target=\"_parent\">$sub</a></td></tr>\n";
	}
	print "</table>\n";
	if (0 <= $back) {print "<a href=\"$script?mode=top&num=$back\"><img src=\"$loaddir/back.gif\" border=0></a> ";}
	if ($next <= $m) {print "<a href=\"$script?mode=top&num=$next\"><img src=\"$loaddir/next.gif\" border=0></a>";}
	print "</div></body></html>\n";
}

###
sub main {
	if (!$mode && $catno) {&detcatf; return;}

	$htmp = $ftmp = '';
	$flag = 0;
	open (IN,"$tempfile") || &error("OPEN ERROR");
	while (<IN>) {
		if (/<!--cgidata-->/) {$flag = 1;}
		elsif (!$flag) {$htmp .= $_;}
		else {$ftmp .= $_;}
	}
	close IN;

	print "Content-type: text/html\n\n";
	print $htmp;
	$head = 1;

	&idx;
	if ($mode eq 'detail') {&detail;}
	elsif ($mode eq 'detcat') {&detcat;}
	else {&list;}

	print $ftmp;
}

###
sub list {
	&detdspa;
	if (0 <= $back) {print "<a href=\"$script?num=$back\"><img src=\"$loaddir/back.gif\" border=0></a>　　";}
	if ($next <= $m) {print "<a href=\"$script?num=$next\"><img src=\"$loaddir/next.gif\" border=0></a>";}
}

###
sub idx {
	print "<table width=100% bgcolor=\"$catbg_color\" bordercolor=\"$frame_color\" border=1 cellspacing=0 cellpadding=3 style=\"border-collapse: collapse\">\n";
	$k = 0;
	%cat = ();
	open (IN,"$catfile") || &error("OPEN ERROR");
	while (<IN>) {
		($cno,$cat) = split(/<>/);
		$cat{$cno} = $cat;
		if (!$k) {print "<tr align=center>";}
		print "<td width=$perw% style=\"padding:10px\"><a href=\"$script?mode=detcat&cno=$cno\">$cat</a></td>\n";
		$k++;
		if ($k == $cols) {print "</tr>\n"; $k = 0;}
	}
	close IN;
	if ($k) {
		for ($k+1 .. $cols) {print "<td width=$perw%></td>";}
		print "</tr>";
	}
	print "</table><br>\n";
}

###
sub detail {
	open (IN,"$voicefile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$cno,$imgt,$imgw,$imgh,$imgb,$year,$mon,$day,$sub,$com) = split(/<>/);
		if ($no eq $in{'no'}) {last;}
	}
	close IN;
	&dsp;
	print "<table width=100%><tr><td><a href=\"javascript:history.back();\">→戻る</a></td></tr></table>\n";
}

###
sub dsp {
	print "<table width=100%><tr><td><font color=\"$sub_color\"><b>■$sub</b></font></td><td align=right>";
	if ($mode eq 'admin') {print "<input type=submit name=$no value=\"修正\">";}
	print "</td></tr></table>\n";

	$com =~ s/([^=^\"]|^)(http\:[\w\.\~\-\/\?\&\+\=\:\@\%\;\#\%]+)/$1<a href=\"$2\" target=\"_blank\">$2<\/a>/g;
	print "<table width=100% cellspacing=0 cellpadding=10><tr><td bgcolor=\"$combg_color\" style=\"padding:10px\">\n";
	if ($imgt) {
		$imgfile = "$loaddir/$no.$imgt";
		$imgsrc = "<img src=\"$imgfile\" width=$imgw height=$imgh border=0 hspace=8 align=right>";
		if ($imgb) {print "<a href=\"$imgfile\" target=\"_blank\">$imgsrc</a>";} else {print $imgsrc;}
	}
	print "$com</td></tr></table>\n";
	print "<hr width=100% size=1 style=\"border:1px dotted $text_color;margin:10px 0;\">\n";
}

###
sub detcat {
	print "<table width=100%><tr><td style=\"border-bottom:1px solid #cccccc\"><b>$cat{$catno}</b></td></tr></table><br>\n";
	&detdsp;
	if (0 <= $back) {print "<a href=\"$script?mode=detcat&cno=$catno&num=$back\"><img src=\"$loaddir/back.gif\" border=0></a>　　";}
	if ($next <= $m) {print "<a href=\"$script?mode=detcat&cno=$catno&num=$next\"><img src=\"$loaddir/next.gif\" border=0></a>";}
}

###
sub detdsp {
	$m = -1;
	open (IN,"$voicefile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$cno,$imgt,$imgw,$imgh,$imgb,$year,$mon,$day,$sub,$com) = split(/<>/);
		if ($cno ne $catno) {next;}
		$m++;
		if ($m < $num) {next;}
		if ($next <= $m) {last;}
		&dsp;
	}
	close IN;
}

###
sub detdspa {
	$m = -1;
	open (IN,"$voicefile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$cno,$imgt,$imgw,$imgh,$imgb,$year,$mon,$day,$sub,$com) = split(/<>/);
		$m++;
		if ($m < $num) {next;}
		if ($next <= $m) {last;}
		&dsp;
	}
	close IN;
}

###
sub detcatf {
	&header;
	print "<body bgcolor=\"$bg_color\" text=\"$text_color\"><center>\n";
	&detdsp;
	if (0 <= $back) {print "<a href=\"$script?cno=$catno&num=$back\"><img src=\"$loaddir/back.gif\" border=0></a>　　";}
	if ($next <= $m) {print "<a href=\"$script?cno=$catno&num=$next\"><img src=\"$loaddir/next.gif\" border=0></a>";}
	print "</center></body></html>\n";
}

###
sub admin {
	&header;
	print "<body><center>\n";
	$inpass = $in{'pass'};
	if ($inpass eq '') {
		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=\"$script?mode=admin\"><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 {&regin;}

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

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

	if (!$catno) {open (IN,"$catfile") || &error("OPEN ERROR");		($catno) = split(/<>/,<IN>);	close IN;}
	&in_form;
	print "<hr width=600>修正、削除する場合は「修正」をクリックして下さい。<br><br>\n";
	print "<table bordercolor=\"$frame_color\" border=1 cellspacing=0 cellpadding=1 style=\"border-collapse: collapse;\">\n";
	$k = 0;
	open (IN,"$catfile") || &error("OPEN ERROR");
	while (<IN>) {
		($cno,$cat) = split(/<>/);
		if (!$k) {print "<tr>";}
		print "<form action=\"$script\" method=POST>\n";
		if ($cno eq $catno) {print "<td width=$perw% bgcolor=\"#ffff00\">";} else {print "<td width=$perw%>";}
		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 == $cols) {print "</tr>\n"; $k = 0;}
	}
	close IN;
	if ($k) {
		for ($k+1 .. $cols) {print "<td width=$perw%></td>";}
		print "</tr>";
	}
	print "</table>\n";

	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 "<div style=\"width:$dspw px\">\n";
	&detdsp;
	print "</div></form>\n";

	print "<table cellpadding=0><tr>";
	if (0 <= $back) {
		print "<form action=\"$script\" method=POST><td width=60>\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=num value=\"$back\">\n";
		print "<input type=submit value=\"BACK\"></td></form>\n";
	}
	if ($next <= $m) {
		print "<form action=\"$script\" method=POST><td>\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=num value=\"$next\">\n";
		print "<input type=submit value=\"NEXT\"></td></form>\n";
	}
	print "</tr></table>\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";
	if ($in{'edt'}) {
		open (IN,"$voicefile") || &error("OPEN ERROR");
		while (<IN>) {
			($no,$cno,$imgt,$imgw,$imgh,$imgb,$year,$mon,$day,$sub,$com) = split(/<>/);
			if ($in{$no}) {last;}
		}
		close IN;
		print "<input type=hidden name=no value=\"$no\">\n";
		$com =~ s/<br>/\r/g;
	} else {
		$cno = $catno;
		$sub = $com = '';
	}
	print "<table bgcolor=\"#e6e4ce\" cellspacing=8><tr><td><table cellspacing=1 cellpadding=0>\n";
	print "<tr><td>分類&nbsp;</td><td><table width=100% cellspacing=0 cellpadding=0>\n";
	$k = 0;
	open (IN,"$catfile") || &error("OPEN ERROR");
	while (<IN>) {
		($cn,$cat) = split(/<>/);
		if (!$k) {print "<tr>";}
		if ($cn eq $cno) {$chk = ' checked';} else {$chk = '';}
		print "<td width=$perw%><input type=radio name=cno value=\"$cn\"$chk>$cat</td>\n";
		$k++;
		if ($k == $cols) {print "</tr>\n"; $k = 0;}
	}
	close IN;
	if ($k) {print "</tr>";}
	print "</table></td></tr>\n";
	print "<tr><td>題名</td><td><input type=text size=50 name=sub value=\"$sub\" style=\"ime-mode:active;\"></td></tr>\n";
	print "<tr><td valign=top><br>内容</td><td><textarea cols=70 rows=12 name=com style=\"ime-mode:active;\">$com</textarea></td></tr>\n";
	print "<tr><td>画像</td><td><input type=file size=60 name=img></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><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{'com'} =~ s/\r\n|\r|\n/<br>/g;
	($sec,$min,$hour,$day,$mon,$year) = localtime;
	$year += 1900;
	$mon++;

	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');
	open (IN,"$voicefile") || &error("OPEN ERROR");		@new = <IN>;		close IN;
	unshift(@new,"$no<>$catno<>$type<>$width<>$height<>$big<>$year<>$mon<>$day<>$in{'sub'}<>$in{'com'}<>\n");
	open (OUT,">$voicefile") || &error("OPEN ERROR");	print OUT @new;		close OUT;
}

###
sub edtwrt {
	$in{'com'} =~ s/\r\n|\r|\n/<br>/g;
	&img("$savedir/$in{'no'}",'img');
	@new = ();
	open (IN,"$voicefile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$cno,$imgt,$imgw,$imgh,$imgb,$year,$mon,$day) = split(/<>/);
		if ($no eq $in{'no'}) {
			if ($type) {$imgt = $type; $imgw = $width; $imgh = $height; $imgb = $big;}
			push(@new,"$no<>$catno<>$imgt<>$imgw<>$imgh<>$imgb<>$year<>$mon<>$day<>$in{'sub'}<>$in{'com'}<>\n");
		} else {push(@new,$_);}
	}
	close IN;
	open (OUT,">$voicefile") || &error("OPEN ERROR");	print OUT @new;		close OUT;
}

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

###
sub catset {
	if ($wrt) {
		@new = ();
		%num = ();
		$delflag = 0;
		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"}) {$delflag = 1;} else {push(@new,"$cno<>$in{\"cat$cno\"}<>\n");}
		}
		open (OUT,">$catfile") || &error("OPEN ERROR");		print OUT @new;		close OUT;

		if ($delflag) {
			@new = ();
			open (IN,"$voicefile") || &error("OPEN ERROR");
			while (<IN>) {
				($no,$cno,$imgt) = split(/<>/);
				if ($in{"del$cno"}) {if ($imgt) {unlink "$savedir/$no.$imgt";}} else {push(@new,$_);}
			}
			close IN;
			open (OUT,">$voicefile") || &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;
		open (OUT,">>$catfile") || &error("OPEN ERROR");	print OUT "$cno<><>\n";		close OUT;
	}
	print "分類を削除すると、その分類に属する全ての記事を削除します。<br><br>\n";
	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=1 style=\"border-collapse: collapse\"><col span=4 align=center>\n";
	print "<tr bgcolor=\"#e6edff\"><td width=60>表\示順</td><td>分類番号</td><td width=250>分類</td><td width=40>削除</td></tr>\n";
	$k = 0;
	open (IN,"$catfile") || &error("OPEN ERROR");
	while (<IN>) {
		($cno,$cat) = split(/<>/);
		print "<tr><td><input type=text size=3 name=num$k value=\"",$k+1,"\" style=\"text-align:right; ime-mode:disabled;\"></td>\n";
		print "<td>$cno</td><td><input type=text size=35 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'});}
		$tempfile = $in{'tempfile'};
		$savedir = $in{'savedir'};		$loaddir = $in{'loaddir'};
		$colors = $in{'colors'};		$colors =~ s/\0/,/g;
		$cols = $in{'cols'};
		$page = $in{'page'};			$dspw = $in{'dspw'};
		$max_w = $in{'max_w'};			$max_h = $in{'max_h'};

		open (OUT,">$opfile") || &error("OPEN ERROR");
		print OUT "$pass<>$tempfile<>$savedir<>$loaddir<>$colors<>$cols<>$page<>$dspw<>$max_w<>$max_h";
		close OUT;
	}
	print "下記に入力後、「設定する」を押して下さい。<br><br>\n";
	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 size=60 name=tempfile value=\"$tempfile\"></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=\"$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 size=10 name=colors 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=cols value=\"$cols\" style=\"text-align:right; ime-mode:disabled;\">列</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;\">件/ページ　　表\示幅（管理画面）<input type=text size=4 name=dspw value=\"$dspw\" style=\"text-align:right; ime-mode:disabled;\">px</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";
}

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