#!/usr/local/bin/perl
#       ↑あなたが加入しているプロバイダの「perl」言語が使用できる
#         パスを指定します。一般的に「#!/usr/local/bin/perl」で大丈夫

require '/usr/lib/perl-lib.pl';
#=======================================================================================
#			logcounter Version 2001.11	SSI専用
#=======================================================================================
#ログファイル
$datafile = 'logcnt.dat';
#----------------------------------------------------------
#メインカウンタに使用するカウント画像のフォルダ
$main_img_dir = 'counter/image6/';
#----------------------------------------------------------
#サブカウンタの表示=1/非表示=0
$subcounter = 0;
#サブカウンタに使用するカウント画像のフォルダ
#画像を使用しない場合はヌル''にする
$sub_img_dir = 'counter/image1/';
#----------------------------------------------------------
#ログを記録する日数 極端に大きくすると重くなる
$max = 31;
#----------------------------------------------------------
#世界標準時との時差 日本の場合は9時間
#日本時間に設定されているサーバは0にする
$timelag = 9;
#=======================================================================================
#			初期設定が必要なのはここまでです。
#=======================================================================================
$DATE_NOW = time + $timelag * 3600;

readparts();
@COUNT = data_read($datafile);
if ($QUERY{'action'} eq 'view') {
	html_head('#FFFFFF', '#000000', '#0000FF', '#FF0000', '#00FF00', '', 10, 10, 'SSI LogCounter');
	foreach (@COUNT) {
		($date, $count, $total) = split(/:/,$_);
		if ($count > $maxcount) { $maxcount = $count; }
	}
	print "<center>\n";
	print "<p align=center><font size=3><b><em>Log Counter Access</em></b></font></p>\n";
	print "<table width=90% border=1 cellspacing=0 cellspacing=0>\n";
	foreach (@COUNT) {
		($date, $count, $total) = split(/:/,$_);
		print "<tr><td align=center><font size=2>", jst_time($date, 1), "</font></td>\n";
		print "<td align=right><font size=2>", comma($count), "</font></td>\n";
		if ($maxcount == 0) { $chrlen = 0; }
		else { $chrlen = int($count / $maxcount * 200); }
		print "<td><hr width=$chrlen size=10 color=#0000FF align=left></td>\n";
		print "<td align=right><font size=2>", comma($total), "</td></tr>\n";
	}
	print "</table><p>\n";
	print "<p><font size=2><a href=http://www.tryhp.net/>LogCounter by Terra</a></font></p>\n";
	print "</center>\n";
	print "</body></html>\n";
} else {
	$now_str = jst_time($DATE_NOW, 5);
	($date, $now_cnt, $total) = split(/:/, $COUNT[0]);
	$date_str = jst_time($date, 5);
	$now_cnt++; $total++;
	if ($now_str eq $date_str) {
		$COUNT[0] = "$date:$now_cnt:$total\n";
	} else {
		unshift(@COUNT, "$DATE_NOW:1:$total\n");
		if (@COUNT > $max) { pop(@COUNT); }
	}
	print "Content-type: text/plain\n\n";
	print "<table border=0>\n";
		print "<tr><td colspan=4 align=center>\n";
		print "<table border=1 cellspacing=0 cellpadding=0><tr><td>\n";
			$length = length($total);
			for ($i = 0; $i < $length; $i++) {
				$c = substr($total, $i, 1);
				print "<img src=$main_img_dir", $c , ".gif border=0>";
			}
		print "</td></tr></table>\n";
		print "</td></tr>\n";
		if ($subcounter) {
			print "<tr><td align=right><font size=2>Yesterday:</font></td>\n";
			($a, $yesterday, $c) = split(/:/, $COUNT[1]);
			$yesterday += 0;
			print "<td>";
				if ($sub_img_dir) {
					$length = length($yesterday);
					for ($i = 0; $i < $length; $i++) {
						$c = substr($yesterday, $i, 1);
						print "<img src=$sub_img_dir", $c , ".gif>";
					}
				} else { print $yesterday; }
			print "</td>\n";
			print "<td align=right><font size=2>Today:</font></td>\n";
			print "<td>";
				if ($sub_img_dir) {
					$length = length($now_cnt);
					for ($i = 0; $i < $length; $i++) {
						$c = substr($now_cnt, $i, 1);
						print "<img src=$sub_img_dir", $c , ".gif>";
					}
				} else { print $now_cnt; }
			print "</td>\n";
			print "</tr>\n";
		}
	print "</table>\n";
	data_save($datafile, @COUNT);
}
exit;