# Blosxom Plugin: counter # Author: Iwata # 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 --- my $cnt = "$blosxom::plugin_state_dir/counter.cnt"; my $log = "$blosxom::plugin_state_dir/counter.log"; my $log_bot = "$blosxom::plugin_state_dir/counter-bot.log"; # hosts to ignore $ip_list = '192.168.0.1'; $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|YahooFeedSeeker|Bulkfeeds|Rojo|Feedster|BlogPulse|Syndic8|MyRSS.jp|Terrar'; # file size to backup current log and to make the new $size_log = 10000; $debug_level = 0 unless defined $debug_level; sub debug { my ($level, @msg) = @_; if ($debug_level >= $level) { print STDERR "counter: $package debug $level: @msg\n"; } } # ------------------------------ 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 get_tm { @MoY = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'); $tz = defined($ENV{'TZ'}) ? ( $ENV{'TZ'} ? $ENV{'TZ'} : 'GMT' ) : ''; ($tm_sec, $tm_min, $tm_hour, $tm_mday, $tm_mon, $tm_year, $tm_wday, $tm_yday, $tm_isdst) = ($tz eq 'GMT') ? gmtime(time) : localtime(time); if ($tz =~ /^([A-Z]+)([-+])?(\d+)(:(\d\d)(:\d\d)?)?([A-Z]+)?(([-+])?(\d+)(:( \d\d)(:\d\d)?)?)?/) { $tm_tz = $1; $off = $3 * 60 + $4; $off = -$off if ($2 ne '-'); if ($tm_isdst && $5 ne '') { $tm_tz = $5; if ($7 ne '') { $off = $7 * 60 + $8; $off = -$off if ($6 ne '-'); } else { $off += 60; } } } else { ($gm_sec, $gm_min, $gm_hour, $gm_mday, $gm_mon, $gm_year, $gm_wk, $gm_yday) = gmtime(time); $off = ($tm_hour - $gm_hour) * 60 + $tm_min - $gm_min; if ($tm_year < $gm_year) { $off -= 24 * 60; } elsif ($tm_year > $gm_year) { $off += 24 * 60; } elsif ($tm_yday < $gm_yday) { $off -= 24 * 60; } elsif ($tm_yday > $gm_yday) { $off += 24 * 60; } } $tzc = " ($tm_tz)" if ($tm_tz ne ''); if ($off == 0) { $tm_tz = 'GMT'; } elsif ($off > 0) { $tm_tz = sprintf("+%02d%02d%s", $off/60, $off%60, $tzc); } else { $off = -$off; $tm_tz = sprintf("-%02d%02d%s", $off/60, $off%60, $tzc); } # "DD/MMM/YYYY:HH:MM:SS TZ" (mainly for apache) sprintf("[%02d/%s/%d:%02d:%02d:%02d %s]", $tm_mday, $MoY[$tm_mon], $tm_year+1900, $tm_hour, $tm_min, $tm_sec, $tm_tz); } # ------------------------------ sub filter { my $n; my $remote_host = $ENV{'REMOTE_HOST'}; my $remote_addr = $ENV{'REMOTE_ADDR'}; my $user_agent = $ENV{'HTTP_USER_AGENT'}; my $referer = $ENV{'HTTP_REFERER'}; $remote_host = $remote_addr if $remote_host eq ''; # if (($remote_host eq $remote_addr) || ($remote_host eq '')) { # $remote_host = gethostbyaddr(pack('C4', split(/\./, $remote_addr)), 2) || $remote_addr; # } $remote_host =~ s/\"/\\$&/g; $remote_addr =~ s/\"/\\$&/g; $user_agent =~ s/\"/\\$&/g; $referer =~ s/\"/\\$&/g; my $output = &get_tm() . " \"$remote_host\" \"$remote_addr\" \"$user_agent\" \"$referer\"\n"; debug(1, "filter() called, enabled"); if (-e $cnt) { open (CNT, "< $cnt"); flock(CNT, 2); $n = ; close (CNT); } else { # $n = 0; return 1; # not suck } if ($remote_addr !~ /($ip_list)/ && $ENV{'REQUEST_URI'} !~ /(1&PIXEL&SPACER&TRACKING&THING)/) { debug(1, "normal $cnt/$n"); if ($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 $output; close(LOG); check_size ($log); } else { debug(1, "bot $log_bot"); if (-e $log_bot) { open (LOG, ">> $log_bot"); } else { open (LOG, "> $log_bot") or die "cannot write to file: $!."; } flock(LOG, 2); print LOG $output; close(LOG); check_size ($log_bot); } } # generate counter $counter = $n; debug(1, "counter is $counter"); } 1;