#!/usr/local/bin/perl # Download Counter - ファイルのダウンロード数をカウントする # Copyright (C) Takashi Utsunomiya. All Rights Reserved. # http://uttsu.com/ # 2002.08.17- # ver 1.00 2002.08.17 # ■init use strict; # カウントデータファイルを置くディレクトリ my $data_path = 'dat/'; # カウントデータファイル my $data_file = 'count.dat'; # ダウンロードするファイルを置くディレクトリ my $file_path = '../../work/'; my $target = $ARGV[0] or err('No argument'); my %count; my $time; $ENV{TZ} = 'JST-9'; main(); exit; # ■main sub main { err("Illegal filename: $target") if ($target =~ /\.\./ || $target =~ /^\//); not_found() if (!-e "$file_path$target"); mkdir($data_path) if (!-d $data_path); open(FILE, "> $_") if (!-e ($_ = "$data_path$data_file")); open(FILE, "+< $_") or die $!; eval { flock(FILE, 2); }; binmode(FILE); get_count(); seek(FILE, 0, 0); get_time(); do_count(); do_log(); put_count(); put_file(); close(FILE); } # ◆get_count sub get_count { foreach () { my ($file, $count) = split(/\s/); $count{$file} = $count; } } # ◆put_count sub put_count { foreach (sort keys %count) { print FILE "$_\t$count{$_}\n"; } } # ◆get_time sub get_time { my ($sec, $min, $hour, $day, $month, $year) = localtime(time); $sec = figure($sec); $min = figure($min); $hour = figure($hour); $day = figure($day); $month = figure($month+1); $year += 1900; $time = "$year.$month.$day $hour:$min:$sec"; } # ◆figure sub figure { my $num = shift; my $figure = (shift || 2); $num = substr(('0' x $figure) . $num, -$figure); return $num; } # ◆do_count sub do_count { $count{$target}++; } # ◆do_log sub do_log { my ($ip, $host, $ref); $ip = $ENV{REMOTE_ADDR}; $host = (gethostbyaddr(pack("C4", split(/\./, $ip)), 2) || $ip); $ref = $ENV{HTTP_REFERER}; $_ = $target; s/\//_/g; open(LOG, ">> $data_path$_\.log") or die $!; print LOG "$time $host $ref\n"; close(LOG); } # ◆put_file sub put_file { my $size = -s "$file_path$target"; $_ = $target; s/^(.*)\///g; print <); close(IN); } # ◆not_found sub not_found { print <