#///////////////////////////////////////////////////////////////////
# GMS_Perl サブモジュール Ver1.61b
# Copyright(C) by でいゆ工房
# http://www.deiyu-studio.net/
#///////////////////////////////////////////////////////////////////

sub set_counter()
{
	my($FileName,$con_c) = @_;

	open(IN,"$FileName") || &Error("Open Error : $FileName");
	my $count = <IN>;
	close(IN);

	my $chk_host = $ENV{'REMOTE_ADDR'};
	my ($counter,$IP_host) = split(/<>/,$count);

	if($chk_host ne $IP_host){
		$counter++; #///カウンターを増やす///
		#///ロック処理///
		if ($Lockkey) { &lock("$Lockkey","$con_lockfile"); }

		open(OUT,">$FileName") || &Error("Open Error : $FileName");
		print OUT "$counter<>$chk_host<>";
		close(OUT);

		#///ロック解除///
		if ($Lockkey) { &unlock("$Lockkey","$con_lockfile"); }
	}

	return sprintf(sprintf("%%0%dld",$con_c),$counter);
}


#///ファイル読み込み///
sub ReadFile {
	my @ReadData;
	open(IN,"@_[0]") || &Error("Open Error : @_[0]");
	@ReadData = <IN>;
	close(IN);
	return @ReadData;
}


#///ファイル書き込み///
sub WriteFile {
	my ($WriteFileName,@WriteData) = @_;

	#///ロック処理///
	if ($Lockkey) { &lock("$Lockkey","$lockfile"); }

	open(OUT,">$WriteFileName") || &Error("Open Error : $WriteFileName");
	print OUT @WriteData;
	close(OUT);

	#///ロック解除///
	if ($Lockkey) { &unlock("$Lockkey","$lockfile"); }
}


#///メンバーカウント///
sub Member_count {
	my (@CountMember) = @_;
	$mem = 0;
	foreach (@CountMember) {
		($No,$Name,$pass,$host,$view_flg,$regtime,$uptime,$upimg,$iw,$ih) = split(/<>/);
		if($view_flg){ $mem++; }
	}
	return $mem;
}


#画像アップロード
sub upload_ifile {
	my ($image_file,$image_keyname,$filename,$width,$height) = @_;
	my ($macbin,$flag,$tail,$fname,$fflag ) = (0,0,"","","");

	foreach (@in) {
		if ($_ =~ /(.*)name=\"(.*)\";/i){ $fflag = $2; }
		if ($fflag eq $image_keyname && $_ =~ /(.*)Content-type:(.*)/i) { $tail=$2; }
		if ($fflag eq $image_keyname && $_ =~ /(.*)filename=(.*)/i) { $fname=$2; }
		if ($fflag eq $image_keyname && $_ =~ /application\/x-macbinary/i) { $macbin=1; }
	}
	$tail =~ s/\r//g;
	$tail =~ s/\n//g;
	$fname =~ tr/\"\x0D\x0A//d;

	$flag=0;
	if ($tail =~ /image\/gif/i) { $tail=".gif"; $flag=1; }
	if ($tail =~ /image\/jpeg/i) { $tail=".jpg"; $flag=1; }
	if ($tail =~ /image\/(x-)?png/i)  { $tail=".png"; $flag=1; }

	if (!$flag) {
		if ($fname =~ /\.gif$/i) { $tail=".gif"; $flag=1; }
		if ($fname =~ /\.jpe?g$/i) { $tail=".jpg"; $flag=1; }
		if ($fname =~ /\.png$/i) { $tail=".png"; $flag=1; }
	}

	#アップ不可形式
	if (!$flag){ return (-1,$fname,$tail,0,0); }

	if ($macbin) {
		$length = substr($image_file,83,4);
		$length = unpack("%N",$length);
		$image_file = substr($image_file,128,$length);
	}

	$fname = "$filename$tail";

	if (!open(OUT,">$fname")) { 
		return (-2,$fname,$tail,0,0);
	}
	binmode(OUT);
	binmode(STDOUT);
	print OUT $image_file;
	close(OUT);

	my ($PrevImg1,$PrevImg2) = ("$filename","$filename");

	if ($tail eq ".jpg") { 
		$PrevImg1 .= ".gif";
		$PrevImg2 .= ".png";
		($W, $H) = &JpegSize($fname);
	} elsif ($tail eq ".gif") {
		$PrevImg1 .= ".jpg";
		$PrevImg2 .= ".png";
		($W, $H) = &GifSize($fname);
	} elsif ($tail eq ".png") {
		$PrevImg1 .= ".jpg";
		$PrevImg2 .= ".gif";
		($W, $H) = &PngSize($fname);
	}

	if ($W > $width || $H > $height ) {
		unlink($fname);
		return (-3,$fname,$tail,$W,$H);
	}

	if(-f $PrevImg1) { unlink($PrevImg1); }
	if(-f $PrevImg2) { unlink($PrevImg2); }

	return (0,$fname,$tail,$W,$H);
}


#比較
sub str_val_cmp {
	my ($str1,$str2) = @_;

	if($str1 =~ /[^0-9\+\-]/g || $str2 =~ /[^0-9\+\-]/g){
		return 0 if($str1 eq $str2);
		return ($str1 cmp $str2);
	}else{
		return 0 if($str1 == $str2);
		return 1 if($str1 > $str2);
		return -1 if($str1 < $str2);
	}
}



1; #return;