#!/usr/local/bin/perl

###############################################
#   inf10.cgi
#      V2.0 (2005.6.14)
#                     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 = 400000;			#入力最大容量（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\">\n";
	print "<title>$title</title><link rel=\"stylesheet\" type=\"text/css\" href=\"$loaddir/style.css\"></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 background=\"$loaddir/$bg_img\" bgcolor=\"$mbg_color\" text=\"$text_color\" link=\"$text_color\" vlink=\"$text_color\" alink=\"$text_color\"><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 "<hr color=\"$text_color\" size=2><table width=100%><col span=1 valign=top>\n";
	open(IN,"$catfile") || &error("OPEN ERROR");
	while (<IN>) {
		($cno,$cat) = split(/<>/);
		print "<tr><td width=5>■</td><td><a href=\"$script?mode=main&cno=$cno\" target=inf10>$cat</a></td></tr>\n";
	}
	close IN;
	print "</table><hr color=\"$text_color\" size=2>\n";
	print "<table width=100%><tr><td><a href=\"$script?mode=admin\" target=\"_parent\">[編集]</a></td></tr></table>\n";
}

###
sub main {
	&header;
	print "<body background=\"$loaddir/$bg_img\" bgcolor=\"$bg_color\" text=\"$text_color\" link=\"$text_color\" vlink=\"$text_color\" alink=\"$text_color\"><center>\n";
	open(IN,"$catfile") || &error("OPEN ERROR");
	while (<IN>) {
		($cno,$cat) = split(/<>/);
		if ($cno eq $catno) {last;}
	}
	close IN;
	print "<table><tr><td><font color=\"$cat_color\" size=\"+1\"><b>$cat</b></font></td></tr></table>\n";

	if ($catno) {
		open(IN,"$inffile") || &error("OPEN ERROR");
		while (<IN>) {
			($no,$sub,$com,$side,$img) = split(/<>/);
			&infdsp;
		}
		close IN;
	}
	print "<hr width=100% color=\"$subbg_color\">\n";
	# 次の行は著作権表示ですので削除しないで下さい。#
	print "<a href=\"http://merlion.cool.ne.jp/cgi/\" target=\"_blank\">CGI-design</a>\n";
}

###
sub infdsp {
	$com =~ s/([^=^\"]|^)(http\:[\w\.\~\-\/\?\&\+\=\:\@\%\;\#\%]+)/$1<a href=\"$2\" target=\"_blank\">$2<\/a>/g;
	if ($sub) {print "<table width=100% cellspacing=2 cellpadding=3><tr><td bgcolor=\"$subbg_color\" align=center><font color=\"$sub_color\"><b>$sub</b></font></td></tr></table>\n";}
	print "<table width=100%><tr><td>\n";
	@img = split(/:/,$img);
	$n = 0;
	for ($i=0; $i<16; $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 valign=bottom>\n";
		&imgdsp('');
	}
	print "</td></tr></table>\n";
}

###
sub imgdsp {
	$j = 0;
	for ($i=0; $i<16; $i+=4) {
		if ($img[$i]) {
			$imgfile = "$loaddir/$no-$j.$img[$i]";
			$imgsrc = "<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\">$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=\"$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=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;}

	&cat_idx;
	&in_form;
	print "<hr width=95%>記事を修正、削除する場合は[修正]をクリックして下さい。\n";
	$k = 0;
	$rate = 114 - $menurate;
	print "<table width=$rate% cellspacing=0 cellpadding=12><col span=3 valign=top>\n";
	print "<tr><td colspan=3><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 "<input type=submit name=sort value=\"並び替え\"><br><br></td></tr>\n";

	open (IN,"$inffile") || &error("OPEN ERROR");	@data = <IN>;	close IN;
	foreach (@data) {
		($no,$sub,$com,$side,$img) = split(/<>/);
		print "<tr><td><select name=num$k>\n";
		for (0 .. $#data) {
			if ($_ == $k) {$sel = ' selected';} else {$sel = '';}
			print "<option value=\"$_\"$sel>",$_+1,"</option>\n";
		}
		print "</select></td><td><input type=submit name=$no value=\"修正\"></td><td width=88% bgcolor=\"$bg_color\">\n";
		&infdsp;
		print "</td></tr>\n";
		$k++;
	}
	print "<tr><td></td><td></td><td bgcolor=\"$bg_color\"><hr width=100% color=\"$subbg_color\"></td></tr></table></form><br>\n";
}

###
sub cat_idx {
	$catcols = int(100 / $menurate);
	$perw = int(100 / $catcols);
	open (IN,"$catfile") || &error("OPEN ERROR");		@cat = <IN>;		close IN;
	print "<table width=90% bgcolor=\"#aaaaaa\" cellspacing=1 cellpadding=1>\n";
	$k = 0;
	foreach (@cat) {
		($cno,$cat) = split(/<>/);
		if (!$k) {print "<tr bgcolor=\"#ffffff\">";}
		if ($cno eq $catno) {$bc = ' bgcolor="#ffff00"';} else {$bc = '';}
		print "<td width=$perw%$bc><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=\"選択\">&nbsp;$cat</td></form>\n";
		$k++;
		if ($k == $catcols) {print "</tr>\n"; $k = 0;}
	}
	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";
		print "<b>*****　修正・削除　*****</b><br>\n";
		$com =~ s/<br>/\r/g;
		@img = split(/:/,$img);
	} else {
		print "<b>*****　新規登録　*****</b><br>\n";
		$sub=$com=$side='';
		@img = ();
	}
	print "<table bgcolor=\"#e6e4ce\" cellspacing=10><tr><td><table cellspacing=1 cellpadding=0>\n";
	print "<tr><td>題名</td><td><input type=text size=60 name=sub value=\"$sub\" style=\"ime-mode:active;\"></td></tr>\n";
	print "<tr><td valign=top><br>内容</td><td><textarea cols=80 rows=12 name=com style=\"ime-mode:active;\">$com</textarea></td></tr>\n";
	for (0 .. 3) {
		print "<tr><td>画像",$_+1,"</td><td><input type=file size=60 name=img$_>\n";
		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>\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) {
				$img_type = $img[4*$_];
				if ($img_type) {unlink "$savedir/$no-$_.$img_type";}
			}
		} 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,$cat) = 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) {
						$img_type = $img[4*$_];
						if ($img_type) {unlink "$savedir/$no-$_.$img_type";}
					}
				}
				close IN;
				unlink "$inffile";
			} else {
				push(@new,"$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("$inffileファイルを作成できません");		close OUT;	chmod(0666,$inffile);
		open (OUT,">>$catfile") || &error("OPEN ERROR");	print OUT "$cno<><>\n";	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\"><col span=3 align=center>\n";
	print "<tr bgcolor=\"#e6edff\"><td width=70>表\示順</td><td width=250>分類</td><td width=40>削除</td></tr>\n";
	open (IN,"$catfile") || &error("OPEN ERROR");		@cat = <IN>;		close IN;
	$k = 0;
	foreach (@cat) {
		($cno,$cat) = split(/<>/);
		print "<tr><td><select name=num$k>\n";
		for (0 .. $#cat) {
			if ($_ == $k) {$sel = ' selected';} else {$sel = '';}
			print "<option value=\"$_\"$sel>",$_+1,"</option>\n";
		}
		print "</select></td>\n";
		print "<td><input type=text size=40 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++;
	}
	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\" style=\"ime-mode:active;\"></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 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;
}
