#!/usr/bin/perl

#┌─────────────────────────────────
#│ LimeCounter
#│ lime.cgi - 2007/08/28
#│ Copyright (c) KentWeb
#│ webmaster@kent-web.com
#│ http://www.kent-web.com/
#└─────────────────────────────────

# 外部ファイル取り込み
require './init.cgi';

# 外部データ受け取り
$buf = $ENV{'QUERY_STRING'};
$buf =~ s/\W//g;

# ファイル定義
$datafile = "$datadir/$buf.dat";

# チェックモード
if (!$buf || $buf eq "check") { &check; }

# カウント処理
&count_up;

#-------------------------------------------------
#  カウント処理
#-------------------------------------------------
sub count_up {
	# データが存在しない場合は終了
	unless (-e $datafile) { &error("Not Exist: $datafile"); }

	# IPアドレス取得
	my $addr = $ENV{'REMOTE_ADDR'};

	# データ読み取り
	open(DAT,"+< $datafile") || &error("Open Error: $datafile");
	eval "flock(DAT, 2);";
	my $data = <DAT>;

	# データ分解
	my ($count, $ip) = split(/:/, $data);

	# カウントアップ
	if (!$ip_chk || ($ip_chk && $addr ne $ip)) {
		$count++;

		seek(DAT, 0, 0);
		print DAT "$count:$addr";
		truncate(DAT, tell(DAT));
	}
	close(DAT);

	# ページカウンタのとき
	if ($type == 1) {

		# ダミー画像
		my @gif = (
			"47","49","46","38","39","61","02","00","02","00","80","00",
			"00","00","00","00","ff","ff","ff","21","f9","04","01","00",
			"00","01","00","2c","00","00","00","00","02","00","02","00",
			"00","02","02","8c","53","00","3b");

		# ダミー画像表示
		print "Content-type: image/gif\n\n";
		foreach (@gif) {
			print pack('C*',hex($_));
		}
		exit;

	# ダウンロードカウンタのとき
	} else {

		# index読み取り
		my ($flg, $jump);
		open(IN,"$idxfile");
		while (<IN>) {
			my ($id,$sub,$link,$file) = split(/<>/);

			if ($buf eq $id) {
				$flg++;
				$jump = $file;
				last;
			}
		}
		close(IN);

		if (!$flg) { &error("IDが不正です"); }

		# Locationヘッダ
		if ($type == 2) {

			# IISサーバ (PerlIS) 対応
			if ($ENV{'PERLXS'} eq "PerlIS") {
				print "HTTP/1.0 302 Temporary Redirection\r\n";
 				print "Content-type: text/html\n";
			}

			# ファイルへジャンプ
			print "Location: $jump\n\n";
			exit;

		# metaタグ
		} else {
			&header("<meta http-equiv=\"refresh\" content=\"1; url=$jump\">");
			print qq|<div align="center">自動でダウンロードできない場合は\n|;
			print qq|<a href="$jump">ここ</a> をクリックしてください。<br>\n|;
			print qq|<form><input type="button" value="閉じる" onclick="top.close();">\n|;
			print qq|</form></div>\n|;
			print qq|</body></html>\n|;
			exit;
		}
	}
}

#-------------------------------------------------
#  エラー処理
#-------------------------------------------------
sub error {
	# ダウンロードカウンタの場合
	if ($type) {
		&header();
		print "<h3>ERROR</h3>$_[0]\n";
		print "</body></html>\n";
		exit;

	# ページカウンタの場合
	} else {
		die "Error : $_[0]";
	}
}

#-------------------------------------------------
#  HTMLヘッダ
#-------------------------------------------------
sub header {
	local($meta) = @_;

	print "Content-type: text/html\n\n";
	print <<EOM;
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=shift_jis">
$meta
<title>$ver</title>
</head>
<body>
EOM
}

#-------------------------------------------------
#  チェックモード
#-------------------------------------------------
sub check {
	&header();
	print <<EOM;
<h3>Check Mode</h3>
<ul>
EOM

	# indexのパス確認
	if (-e $idxfile) {
		print "<li>インデックスファイルのパス: OK!\n";

		# パーミッション
		if (-r $idxfile && -w $idxfile) {
			print "<li>インデックスファイルのパーミッション: OK!\n";
		} else {
			print "<li>インデックスファイルのパーミッション: NG → $idxfile\n";
		}
	} else {
		print "<li>インデックスファイルのパス: NG → $idxfile\n";
	}

	# データディレクトリ
	if (-d $datadir) {
		print "<li>データディレクトリのパス: OK!\n";

		if (-r $datadir && -w $datadir && -x $datadir) {
			print "<li>データディレクトリのパーミッション: OK!\n";
		} else {
			print "<li>データディレクトリのパーミッション: NG → $datadir\n";
		}
	} else {
		print "<li>データディレクトリのパス: NG → $datadir\n";
	}

	# 著作権表示：削除禁止
	print <<EOM;
<li>バージョン: <a href="http://www.kent-web.com/">$ver</a>
</ul>
</body>
</html>
EOM
	exit;
}



