#!/usr/bin/perl

###############################################
#   view41.cgi
#      V1.0 (2005.6.19)
#                     Copyright(C) CGI-design
###############################################

require './cgi-lib.pl';

eval 'use Image::Magick;';
if ($@) {$magick = 0;} else {$magick = 1;}

$script = 'gallery.cgi';
$base = './viewdata';				#データ格納ディレクトリ
$catfile = "$base/cat.txt";			#分類
$cnofile = "$base/cno.txt";			#分類番号
$nofile = "$base/no.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');
	chmod(0666,$opfile);	open (OUT,">$opfile") || &error("OPEN ERROR");
	print OUT "$pass<><>$base<>$base<><><>#FAFAEB<>#414F38<>#c9e0c9<>#ffffff<>#688A53<>150<>150<>4<>12<>22";
	close OUT;
	chmod(0666,$catfile);	chmod(0666,$cnofile);	chmod(0666,$nofile);
}

###　メイン処理　###
&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'};
$num = $in{'num'};

open (IN,"$opfile") || &error("OPEN ERROR");
($pass,$home,$savedir,$loaddir,$bg_img,$home_icon,$bg_color,$text_color,$cbg_color,$cat_color,$catbg_color,$max_w,$max_h,$cols,$page,$menurate) = split(/<>/,<IN>);
close IN;
if ($cols == 0) {$cols = 4;}
if ($page == 0) {$page = 12;}

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

if ($mode eq 'menu') {&menu;}
elsif ($mode eq 'main') {&main;}
elsif ($mode eq 'photo') {&photo;}
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>Photo</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=\"view21\" scrolling=\"auto\">\n";
	print "</frameset>\n";
	print "</html>\n";
	exit;
}

###
sub menu {
	&header;
	print "<body background=\"$loaddir/$bg_img\" bgcolor=\"$cbg_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 "<table width=100% cellspacing=5 cellpadding=0>\n";
	open (IN,"$catfile") || &error("OPEN ERROR");
	while (<IN>) {
		($cno,$cat) = split(/<>/);
		print "<tr><td style=\"border-bottom:1px dotted $text_color;\"><a href=\"$script?mode=main&cno=$cno\" target=view21>■$cat</a></td></tr>\n";
	}
	close IN;
	print "</table>\n";
	print "<table width=100%><tr><td height=20 valign=bottom><a href=\"$script?mode=admin\" target=\"_parent\"><font color=\"$text_color\">[編集]</font></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,$com) = split(/<>/);
		if ($cno eq $catno) {last;}
	}
	close IN;

	print "<table width=95% cellspacing=4 cellpadding=2>\n";
	print "<tr><td bgcolor=\"$catbg_color\" align=center><font color=\"$cat_color\" size=\"+1\"><b>$cat</b></font></td></tr>\n";
	print "<tr><td>$com</td></tr></table>\n";
	$mainrate = 95;
	&dsp;
	if ($page <= $m) {
		print "<table bordercolor=\"$catbg_color\" border=1 cellspacing=0 cellpadding=2 style=\"border-collapse: collapse\"><tr align=center>\n";
		$i = 1;
		for ($k=0; $k<=$m; $k+=$page) {
			if ($k == $num) {print "<td width=40 bgcolor=\"$catbg_color\"><font color=\"$cat_color\">Page$i</font></td>\n";}
			else {print "<td width=40><a href=\"$script?mode=$mode&cno=$catno&num=$k\">Page$i</a></td>\n";}
			$i++;
		}
		print "</tr></table><br>\n";
	}
}

###
sub dsp {
	if (!-e $viewfile) {return;}
	$next = $num + $page;
	$rate = int(100 / $cols);
	$k = 0;
	$m = -1;
	print "<table width=$mainrate% cellspacing=1 cellpadding=5>\n";
	if ($mode eq 'admin') {$tar = ' target="_blank"';} else {$tar = '';}
	if ($magick) {$is = '-s';} else {$is = '';}
	open (IN,"$viewfile") || &error("OPEN ERROR");
	while (<IN>) {
		$m++;
		if ($m < $num || $next <= $m) {next;}
		($no,$img_type,$img_w,$img_h) = split(/<>/);
		if (!$k) {print "<tr align=center>\n";}
		print "<td width=$rate%><a href=\"$script?mode=photo&cno=$catno&num=$num&sel=$m\"$tar><img src=\"$loaddir/$no$is.$img_type\" border=0 width=$img_w height=$img_h></a>\n";
		if ($mode eq 'admin') {print "<br><input type=checkbox name=del$no value=\"1\">削除\n";}
		print "</td>\n";
		$k++;
		if ($k == $cols) {print "</tr>\n"; $k = 0;}
	}
	close IN;
	if ($k) {
		for ($k+1 .. $cols) {print "<td></td>";}
		print "</tr>";
	}
	print "</table><br>\n";
}

###
sub photo {
	&header;
	print "<body background=\"$loaddir/$bg_img\" bgcolor=\"$bg_color\" text=\"$text_color\" link=\"$text_color\" vlink=\"$text_color\" alink=\"$text_color\"><center>\n";
	$sel = $in{'sel'};
	open (IN,"$viewfile") || &error("OPEN ERROR");		@data = <IN>;		close IN;
	($no,$img_type) = split(/<>/,$data[$sel]);

	$sum = @data;
	$next = $sel + 1;
	$back = $sel - 1;
	if ($next < $sum) {$nidx = "&mode=photo&sel=$next&num=$num";} else {$nidx = '&mode=main';}
	if (0 <= $back) {$bidx = "&mode=photo&sel=$back&num=$num";} else {$bidx = '&mode=main';}
	print "<table width=100% height=95%><tr valign=top><td width=20% height=18>　<a href=\"$script?mode=main&cno=$catno&num=$num\">[Return]</a></td>\n";
	print "<td width=60% align=center><a href=\"$script?cno=$catno$bidx\">＜＜</a> $next/$sum <a href=\"$script?cno=$catno$nidx\">＞＞</a></td><td width=20%></td></tr>\n";
	print "<tr><td colspan=3 align=center><a href=\"$script?cno=$catno$nidx\"><img src=\"$loaddir/$no.$img_type\" border=0></a></td></tr></table>\n";
}

###
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 ($wrt) {&edtwrt;}
	&catidx;
	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";
	print "<input type=submit name=wrt value=\"設定する\"><br><br>\n";

	print "<table bgcolor=\"#e6e4ce\" cellspacing=8><tr><td>画像 <input type=file size=60 name=img></td></tr></table><br>\n";
	$mainrate = 95 - $menurate;
	&dsp;
	print "</form>\n";
	if ($m < $page) {return;}

	print "<table bgcolor=\"#aaaaaa\" cellspacing=1 cellpadding=0><tr bgcolor=\"#ffffff\" align=center>\n";
	$i = 1;
	for ($k=0; $k<=$m; $k+=$page) {
		if ($k == $num) {print "<td width=50 bgcolor=\"#ffff00\">Page$i</td>\n";}
		else {
			print "<td width=50><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=num value=\"$k\">\n";
			print "<input type=submit value=\"Page$i\"></td></form>\n";
		}
		$i++;
	}
	print "</tr></table><br>\n";
}

###
sub catidx {
	$catcols = int(100 / $menurate);
	$perw = int(100 / $catcols);
	print "<table width=95% bgcolor=\"#ffffff\" bordercolor=\"#aaaaaa\" 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>";}
		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=\"選択\">&nbsp;$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 edtwrt {
	@new = ();
	open (IN,"$viewfile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$img_type) = split(/<>/);
		if ($in{"del$no"}) {
			unlink "$savedir/$no.$img_type";
			if ($magick) {unlink "$savedir/$no-s.$img_type";}
		} else {push(@new,$_);}
	}
	close IN;

	if ($in{'img'}) {
		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');
		push(@new,"$no<>$type<>$width<>$height<>\n");
	}
	open (OUT,">$viewfile") || &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"}) {
				$viewfile = "$base/$cno.txt";
				open (IN,"$viewfile") || &error("OPEN ERROR");
				while (<IN>) {
					($no,$img_type) = split(/<>/);
					unlink "$savedir/$no.$img_type";
					if ($magick) {unlink "$savedir/$no-s.$img_type";}
				}
				close IN;
				unlink "$viewfile";
			} 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;
		$viewfile = "$base/$cno.txt";
		open (OUT,">$viewfile") || &error("$viewfileファイルを作成できません");	close OUT;	chmod(0666,$viewfile);
		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=\"#cccccc\" border=1 cellspacing=0 cellpadding=1 style=\"border-collapse: collapse\"><col span=4 align=center>\n";
	print "<tr bgcolor=\"#e6edff\"><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><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 name=cat$cno size=30 value=\"$cat\" style=\"ime-mode:active;\"></td>\n";
		print "<td><textarea cols=50 rows=4 name=com$cno style=\"ime-mode:active;\">$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 ($wrt) {
		if ($in{'newpass'} ne '') {$pass = &crypt($in{'newpass'});}
		$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'};
		$cbg_color = $in{'color2'};
		$cat_color = $in{'color3'};
		$catbg_color = $in{'color4'};

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

		open (OUT,">$opfile") || &error("OPEN ERROR");
		print OUT "$pass<>$home<>$savedir<>$loaddir<>$bg_img<>$home_icon<>$bg_color<>$text_color<>$cbg_color<>$cat_color<>$catbg_color<>$max_w<>$max_h<>$cols<>$page<>$menurate";
		close OUT;
	}
	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>ホーム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 name=img size=60></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,$cbg_color,$cat_color,$catbg_color);
	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=color$_ 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 name=max_w size=4 value=\"$max_w\" style=\"text-align:right; ime-mode:disabled;\">px　縦max<input type=text name=max_h size=4 value=\"$max_h\" style=\"text-align:right; ime-mode:disabled;\">px\n";
	print "　　<input type=text name=cols size=3 value=\"$cols\" style=\"text-align:right; ime-mode:disabled;\">列　<input type=text name=page size=3 value=\"$page\" style=\"text-align:right; ime-mode:disabled;\">枚/ページ</td></tr>\n";
	print "<tr><td><b>分類フレーム幅</b></td><td><input type=text name=menurate size=4 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);
	}
	if ($magick) {
		$im = Image::Magick->new;
		$im->Read("$img_file");
		$im->Scale(width=>$width,height=>$height);
		$im->Write("$_[0]-s.$type");
		chmod (0666,"$_[0]-s.$type");
	}
}

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