# Blosxom Plugin: counter # Author: Kengo Ichiki # Version: $Id: counter,v 1.11 2004/06/22 00:33:54 ichiki Exp $ package counter; use Time::Local; $counter; # --- Configurable variables --- $cnt = "$blosxom::plugin_state_dir/counter.cnt"; $log = "$blosxom::plugin_state_dir/counter.log"; $blog_bot = "$blosxom::plugin_state_dir/counter-bot.log"; #$linkurl = ""; $linkurl = "http://kichiki.hp.infoseek.co.jp/BLOSXOM/index.html#counter"; # images should be at URL $imgdir/$imgname/[0-9]$imgname.gif $imgdir = "/images/digits"; $imgname = "kichiki"; $imgbracket = 1; #$imgname = "katt077"; #$imgbracket = 0; # width (pixel) of the counter #$counterwidth = 0; $counterwidth = 80; # the width-data file with full path on the server (not URL!) $imgwidths = "/www/images/digits/kichiki/widths.dat"; # give digits if you want zero-padding #$ndigits = 0; $ndigits = 8; # hosts to ignore $ip_list = '192.168.1.1|192.168.1.2'; $agent_list = 'Mediapartners-Google|Googlebot|Gigabot|msnbot|larbin|ia_archiver|Microsoft-ATL-Native|Microsoft URL Control|libwww-perl|Yahoo! Slurp|Ask Jeeves/Teoma|Hatena Antenna|Bloglines|Steeler'; # file size to backup current log and to make the new $size_log = 10000; # ------------------------------ sub cntimg { my $n = shift; my $result = ''; my @digits; my $x = 10; my $i = 0; my $j, $k; my %widths; my $w = 0; my $w2 = 0; unless ($linkurl eq "") { $result .= ""; } # set digits $i while ($n > 0) { $digits[$i] = ($n % 10); $n = int($n / 10); $i ++; } if ($counterwidth > 0) { open (WD, "< $imgwidths"); while () { if (/^#/) { # comment } elsif (/^(\w)\s+(\d+)/) { $widths{$1} = $2; } } close (WD); # calc total width $w = 0; if ($imgbracket) { $w += $widths{'l'}; $w += $widths{'r'}; } for ($j = 0; $j < ($ndigits - $i); $j ++) { $w += $widths{'0'}; } for ($j = 0; $j < $i; $j ++) { $k = $i - $j - 1; $w += $widths{$digits[$k]}; } if ($w < $counterwidth) { $w = $counterwidth - $w; $w2 = int($w / 2); } else { $w = 0; $w2 = 0; } } # prepare $result if ($imgbracket) { $result .= "\"[\""; } for ($j = 0; $j < $w2; $j ++) { $result .= "\"\""; } for ($j = 0; $j < ($ndigits - $i); $j ++) { $result .= "\"0\""; } for ($j = 0; $j < $i; $j ++) { $k = $i - $j - 1; $result .= "\"$digits[$k]\""; } for ($j = 0; $j < ($w - $w2); $j ++) { $result .= "\"\""; } if ($imgbracket) { $result .= "\"]\""; } unless ($linkurl eq "") { $result .= ""; } return $result; } # ------------------------------ sub start { 1; } # ------------------------------ # to turn-over the log file sub check_size { my $file = shift; my $n = 1; my $size; $size = -s $file; if ($size > $size_log) { while (-e $file.".".$n) { $n++; } rename ($file, $file.".".$n); } } # ------------------------------ sub filter { my $n; my ($year, $month, $day); my ($hour, $min, $sec); my @now = localtime(); $year = $now[5] + 1900; $month = $now[4] + 1; $day = $now[3] + 0; $hour = $now[2] + 0; $min = $now[1] + 0; $sec = $now[0] + 0; if (-e $cnt) { open (CNT, "< $cnt"); $n = ; close (CNT); } else { $n = 0; } if ($ENV{'REMOTE_ADDR'} !~ /($ip_list)/ && $ENV{'REQUEST_URI'} !~ /(1&PIXEL&SPACER&TRACKING&THING)/) { if ($ENV{'HTTP_USER_AGENT'} !~ /($agent_list)/) { $n ++; open (CNT, "> $cnt") or die "cannot open file to write : $!."; flock(CNT, 2); print CNT "$n"; close (CNT); if (-e $log) { open (LOG, ">> $log"); } else { open (LOG, "> $log") or die "cannot write to file: $!."; } flock(LOG, 2); print LOG "$n "; printf (LOG "%4d/%d/%d %02d:%02d:%02d ", $year, $month, $day, $hour, $min, $sec); print LOG "$ENV{'REQUEST_URI'} $ENV{'REMOTE_HOST'} $ENV{'REMOTE_ADDR'} $ENV{'REMOTE_USER'} $ENV{'HTTP_REFERER'} $ENV{'HTTP_USER_AGENT'}\n"; close(LOG); check_size ($log); } else { if (-e $log_bot) { open (LOG, ">> $log_bot"); } else { open (LOG, "> $log_bot") or die "cannot write to file: $!."; } flock(LOG, 2); print LOG "- "; printf (LOG "%4d/%d/%d %02d:%02d:%02d ", $year, $month, $day, $hour, $min, $sec); print LOG "$ENV{'REQUEST_URI'} $ENV{'REMOTE_HOST'} $ENV{'REMOTE_ADDR'} $ENV{'REMOTE_USER'} $ENV{'HTTP_REFERER'} $ENV{'HTTP_USER_AGENT'}\n"; close(LOG); check_size ($log_bot); } } # generate counter $counter = cntimg($n); } 1;