#!/usr/bin/perl

###############################################
#   inf.cgi改造
#      V1.0 (2012.1.15)
#                     Copyright(C) CGI-design
###############################################

require './cgi-lib.pl';

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

open (IN,"$opfile") || &error("OPEN ERROR");	$opdata = <IN>;		close IN;
if (!$opdata) {
	$pass = &crypt('cgi');
	open(OUT,">$opfile") || &error("OPEN ERROR");
	print OUT "$pass<><><>$base<>$base<>#ECF0EC,#000000,#CAD8CB,#5A6D45,#869D7D,#000000,#CAD8CB,#ffffff<>25<>5<>700<>1<>200";
	close OUT;
}

###　メイン処理　###
&ReadParse;
while (($n,$val) = each %in) {
	if ($n eq 'img') {next;}
	$val =~ s/&/&amp;/g;	$val =~ s/</&lt;/g;		$val =~ s/>/&gt;/g;		$val =~ s/"/&quot;/g;	$val =~ s/\r\n|\r|\n/<br>/g;
	$in{$n} = $val;
}
$mode = $in{'mode'};
$catno = $in{'cno'};
$num = $in{'num'};

open (IN,"$opfile") || &error("OPEN ERROR");
($pass,$home,$bg_img,$savedir,$loaddir,$colors,$menurate,$page,$dspw,$newsort,$max_wh) = split(/<>/,<IN>);
close IN;
($bg_color,$text_color,$mbg_color,$cat_color,$frame_color,$sub_color,$subbg_color,$combg_color) = split(/,/,$colors);
if ($page == 0) {$page = 5;}

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 "<link rel=\"stylesheet\" type=\"text/css\" href=\"./resource/lightbox.css\" media=\"screen,tv\">\n";
	print "<script type=\"text/javascript\" charset=\"UTF-8\" src=\"./resource/spica.js\"></script>\n";
	print "<script type=\"text/javascript\" charset=\"UTF-8\" src=\"./resource/lightbox_plus.js\"></script>\n";
	print "<title>SVCF参考資料アーカイブ</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=\"inf\" scrolling=\"auto\">\n";
	print "</frameset>\n";
	print "</html>\n";
	exit;
}

###
sub menu {
	&header;
	print "<body background=\"$bg_img\" bgcolor=\"$mbg_color\" text=\"$text_color\">\n";
	if ($home) {print "<a href=\"$home\" target=\"_parent\">SVCF福島原発行動隊 HOME</a>\n";}

	open (IN,"$catfile") || &error("OPEN ERROR");
	while (<IN>) {
		($cno,$cat) = split(/<>/);
		print "<div style=\"border-bottom:1px dotted $text_color; margin-top:5px;\"><a href=\"$script?mode=main&cno=$cno\" target=\"inf\">■$cat</a></div>\n";
	}
	close IN;
	print "<br><a href=\"$script?mode=admin\" target=\"_parent\">[管理]</a>\n";
	print "</body></html>\n";
	exit;
}

###
sub main {
	&header;
	print "<body background=\"$bg_img\" bgcolor=\"$bg_color\" text=\"$text_color\"><center>\n";
	if (!$catno) {return;}

	open (IN,"$catfile") || &error("OPEN ERROR");
	while (<IN>) {
		($cno,$cat,$com) = split(/<>/);
		if ($cno eq $catno) {last;}
	}
	close IN;
	print "<font color=\"$cat_color\" size=\"+1\"><b>$cat</b></font>\n";
	print "<table><tr><td>$com</td></tr></table>\n";
	&dsp;
	print "<table width=$dspw><tr>\n";
	if (0 <= $back) {print "<td><a href=\"$script?mode=main&cno=$catno&num=$back\">&lt;&lt; 前ページ</a></td>";}
	if ($next <= $m) {print "<td align=right><a href=\"$script?mode=main&cno=$catno&num=$next\">次ページ &gt;&gt;</a></td>";}
	print "</td></tr></table>\n";
	# 次の行は著作権表示ですので削除しないで下さい。#
	print "<a href=\"http://cgi-design.net\" target=\"_blank\">CGI-design</a>\n";
}

###
sub dsp {
	$back = $num - $page;
	$next = $num + $page;
	$m = -1;
	open (IN,"$inffile") || &error("OPEN ERROR");
	while (<IN>) {
		$m++;
		if ($m < $num) {next;}
		if ($next <= $m) {last;}
		($no,$sub,$com,$imgt,$imgw,$imgh) = split(/<>/);
		$com =~ s/([^=^\"]|^)(https?\:[\w\.\~\-\/\?\&\+\=\:\@\%\;\#\%]+)/$1<a href="$2" target="link">$2<\/a>/g;

		print "<table width=$dspw bordercolor=\"$frame_color\" border=1 cellspacing=0 cellpadding=1 style=\"border-collapse: collapse;\">\n";
		print "<tr><td bgcolor=\"$subbg_color\"><table width=100%><tr><td>　<font color=\"$sub_color\"><b>$sub</b></font></td>\n";
		if ($mode eq 'admin') {print "<td align=right><input type=text size=3 name=num$m value=\"",$m+1,"\" style=\"text-align:right; ime-mode:disabled;\"> <input type=submit name=$no value=\"修正\"></td>";}
		print "</tr></table></td></tr>\n";
		print "<tr><td bgcolor=\"$combg_color\"><table width=100% cellspacing=4><tr><td>\n";
		if ($imgt) {print "<a href=\"$loaddir/$no.$imgt\" rel=\"lightbox\"><img src=\"$loaddir/$no.$imgt\" width=$imgw height=$imgh border=0 hspace=8 align=right></a>";}
		print "$com</td></tr></table></td></tr></table><br>\n";
	}
	close IN;
}

###
sub admin {
	&header;
	print "<body><center>\n";
	$inpass = $in{'pass'};
	if ($inpass eq '') {
		print "<table width=97%><tr><td><a href=\"$script\">戻る</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=\"$script\"><font color=\"#ffffff\"><b>Return</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";

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

###
sub edtin {
	if (!$catno) {print "<br><br>「分類設定」を設定して下さい\n"; return;}
	if ($in{'newwrt'}) {&newwrt;}
	elsif ($in{'edtwrt'}) {&edtwrt;}
	elsif ($in{'delwrt'}) {&delwrt;}
	elsif ($in{'edtsort'}) {&edtsort;}

	&catidx;
	&in_form;
	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 "<input type=hidden name=num value=\"$num\">\n";
	print "<hr width=700><input type=submit name=edtsort value=\"並び替え\"> 記事の並び替えは順番を入力後、「並び替え」を押して下さい。<br>　　記事を修正、削除する場合は「修正」を押して下さい。<br><br>\n";
	&dsp;
	print "</form>\n";

	print "<table><tr>\n";
	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 catidx {
	if ($menurate == 0) {$menurate = 25;}
	$catcols = int(100 / $menurate);
	$perw = int(100 / $catcols);
	$k = 0;
	print "<table width=95% bordercolor=\"#aaaaaa\" border=1 cellspacing=0 cellpadding=1 style=\"border-collapse: collapse;\">\n";
	open (IN,"$catfile") || &error("OPEN ERROR");
	while (<IN>) {
		($cno,$cat) = split(/<>/);
		if ($cno eq $catno) {$bc = ' bgcolor="#ffff00"';} else {$bc = '';}
		if (!$k) {print "<tr>";}
		print "<form action=\"$script\" method=POST><td width=$perw%$bc>\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) = 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";
		$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=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";
	print "<tr><td>画像</td><td><input type=file size=60 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><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 = ();
	%num = ();
	open (IN,"$inffile") || &error("OPEN ERROR");		@data = <IN>;		close IN;
	for (0 .. $#data) {
		if ($in{"num$_"} eq '') {$num{$_} = $_ + 1;} else {$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 {
	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<>$in{'sub'}<>$in{'com'}<>$type<>$width<>$height<>\n";

	open (IN,"$inffile") || &error("OPEN ERROR");		@new = <IN>;	 	close IN;
	if ($newsort) {unshift(@new,$newdata);} else {push(@new,$newdata);}
	open (OUT,">$inffile") || &error("OPEN ERROR");		print OUT @new;		close OUT;
}

###
sub edtwrt {
	&img("$savedir/$in{'no'}",'img');
	@new = ();
	open (IN,"$inffile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$sub,$com,$imgt,$imgw,$imgh) = split(/<>/);
		if ($no eq $in{'no'}) {
			if ($type) {$imgt=$type; $imgw=$width; $imgh=$height;}
			push(@new,"$no<>$in{'sub'}<>$in{'com'}<>$imgt<>$imgw<>$imgh<>\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,$imgt) = split(/<>/);
		if ($no eq $in{'no'}) {if ($imgt) {unlink "$savedir/$no.$imgt";}} else {push(@new,$_);}
	}
	close IN;
	open (OUT,">$inffile") || &error("OPEN ERROR");		print OUT @new;		close OUT;
}

###
sub catset {
	if ($in{'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,$imgt) = split(/<>/);
					if ($imgt) {unlink "$savedir/$no.$imgt";}
				}
				close IN;
				unlink "$inffile";
			} else {
				push(@new,"$cno<>$in{\"cat$cno\"}<>$in{\"com$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);
		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=\"#cccccc\" border=1 cellspacing=0 cellpadding=2 style=\"border-collapse: collapse;\">\n";
	print "<tr bgcolor=\"#e6edff\" align=center><td width=50>表\示順</td><td width=190>分類名</td><td width=400>コメント</td><td>削除</td></tr>\n";
	$k = 0;
	open (IN,"$catfile") || &error("OPEN ERROR");
	while (<IN>) {
		($cno,$cat,$com) = split(/<>/);
		$com =~ s/<br>/\r/g;
		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=30 name=cat$cno value=\"$cat\"></td>\n";
		print "<td><textarea cols=50 rows=4 name=com$cno>$com</textarea></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 ($in{'wrt'}) {
		if ($in{'newpass'} ne '') {$pass = &crypt($in{'newpass'});}
		$home = $in{'home'};		$bg_img = $in{'bg_img'};
		$savedir = $in{'savedir'};	$loaddir = $in{'loaddir'};
		$colors = $in{'colors'};	$colors =~ s/\0/,/g;
		$menurate = $in{'menurate'};
		$page = $in{'page'};		$dspw = $in{'dspw'};		$newsort = $in{'newsort'};
		$max_wh = $in{'max_wh'};

		open (OUT,">$opfile") || &error("OPEN ERROR");
		print OUT "$pass<>$home<>$bg_img<>$savedir<>$loaddir<>$colors<>$menurate<>$page<>$dspw<>$newsort<>$max_wh";
		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>ホーム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=bg_img value=\"$bg_img\"></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 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=4 name=menurate value=\"$menurate\" style=\"text-align:right; ime-mode:disabled;\">%</td></tr>\n";
	if ($newsort) {$chk0 = ' checked'; $chk1 = '';} else {$chk0 = ''; $chk1 = ' checked';}
	print "<tr><td><b>記事表\示</b></td><td><input type=text size=4 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　　<input type=radio name=newsort value=\"1\"$chk0>新しい順　<input type=radio name=newsort value=\"0\"$chk1>古い順</td></tr>\n";
	print "<tr><td><b>画像表\示</b></td><td><input type=text size=4 name=max_wh value=\"$max_wh\" 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=$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("ファイルを認識できません");}

	if ($max_wh && ($max_wh < $width || $max_wh < $height)) {
		if ($height < $width) {$height = int($height * $max_wh / $width); $width = $max_wh;}
		else {$width = int($width * $max_wh / $height); $height = $max_wh;}
	}
}

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