#!/usr/local/bin/perl # Counter Script Version 1.1.1 # Created by Matt Wright mattw@misha.net # Created on: 10/27/95 Last Modified on: 1/11/96 # Scripts Archive: http://www.worldwidemart.com/scripts/ # Consult the file README for more information and Installation Instructions. # Re-written by SAWADA Takahiro # First Modified 04/12/96 ####################################################################### # Define Variables ### FILE AND DIRECTORY LOCATIONS, REFERERS ### $home_dir = "/home/usr03/saw"; $flyprog = "$home_dir/fly/fly -q"; $log_dir = "$home_dir/log"; $lock_file = "$log_dir/lock"; $digit_dir = "$home_dir/digits"; $fly_temp = "$digit_dir/temp.fly"; $null_img = "$digit_dir/null.gif"; $file_busy_img = "$digit_dir/busy!.gif"; $bad_referer_img = "$digit_dir/bad_referer.gif"; $no_username_img = "$digit_dir/no_username.gif"; $no_htmlname_img = "$digit_dir/no_htmlname.gif"; $no_digit_img = "$digit_dir/no_digit.gif"; $unregistered_username_img = "$digit_dir/unregistered_username.gif"; if(!open(LK,"$lock_file")) { print "Content-type: image/gif\n\n"; print `cat $file_busy_img`; exit; } flock(LK,2); $org_referer= "$home_dir/public_html/http_info"; $anyone = "$home_dir/public_html/anyone"; $jump_from = `cat $org_referer`; chop($jump_from); `cp $anyone $org_referer`; #@referers = ("www.st.rim.or.jp","st.rim.or.jp","202.255.181.2"); ### DEFAULT IMAGE SETTINGS ### $type = "98"; $width = "26"; $height = "31"; $tp = "255,255,255"; $il = "1"; $frame_width = "a"; $frame_color = "50,70,90"; $dot = "X"; $logo = "X"; $uselog = "1"; # 1 = YES; 0 = NO ### Setting ### $buffer = $ENV{'QUERY_STRING'}; @pairs = split(/&/,$buffer); foreach $pair (@pairs){ ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; } if($FORM{'user'}){ $user = $FORM{'user'}; } else { print "Content-type: image/gif\n\n"; print `cat $no_username_img`; close(LK); exit; } $count_file = "$log_dir/$user"; $access_log = "$log_dir/$user" . "_access_log"; $error_log = "$log_dir/$user" . "_error_log"; unless(-e $count_file){ print "Content-type: image/gif\n\n"; print `cat $unregistered_username_img`; close(LK); exit; } if($FORM{'file'}){ $file = $FORM{'file'}; } else { print "Content-type: image/gif\n\n"; print `cat $no_htmlname_img`; close(LK); exit; } $type = $FORM{'type'} ? $FORM{'type'} : $type; $digit_images = "$digit_dir/$type"; unless(-e $digit_images){ unless($type eq "null"){ print "Content-type: image/gif\n\n"; print `cat $no_digit_img`; close(LK); exit; } } $width = $FORM{'w'} ? $FORM{'w'} : $width; $height = $FORM{'h'} ? $FORM{'h'} : $height; $tp = $FORM{'tp'} ? $FORM{'tp'} : $tp; $il = $FORM{'il'} ? $FORM{'il'} : $il; $frame_width = $FORM{'fw'} ? $FORM{'fw'} : $frame_width; $frame_color = $FORM{'fc'} ? $FORM{'fc'} : $frame_color; $dot = $FORM{'dot'} ? $FORM{'dot'} : $dot; $logo = $FORM{'logo'} ? $FORM{'logo'} : $logo; $uselog = $FORM{'log'} ? $FORM{'log'} : $uselog; # Done ####################################################################### # Get the Date For Logging Purposes if ($uselog == 1) { ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); if ($sec < 10) { $sec = "0$sec"; } if ($min < 10) { $min = "0$min"; } if ($hour < 10) { $hour = "0$hour"; } if ($mday < 10) { $mday = "0$mday"; } if ($mon < 10) { $monc = "0$mon"; } $date = "$year/$mon/$mday $hour\:$min\:$sec"; } # Make Sure People Aren't Messing With the Counter From Other Web Pages # &check_referer; # Get the Counter Number And Write New One to File &get_num; # Return very-small image and exit if($type eq null){ if($uselog == 1){ &log_access; } print "Content-type: image/gif\n\n"; print `cat $null_img`; close(LK); exit; } # If they Just want a transparent dot or a logo, give them that. &check_dot; # Determine Length of Counter Number $num = $length = length($count); # Set Individual Counter Numbers Into Associative Array while ($num > 0) { $CHAR{$num} = chop($count); $num--; } # Determine the Height and Width of the Image $img_width = (($width * $length) + ($frame_width * 2)); $img_height = (($frame_width * 2) + $height); # Open the In-File for Commands open(FLY,">$fly_temp") || die "Can't Open In File For FLY Commands: $!\n"; # Create New Counter Image print FLY "new\n"; print FLY "size $img_width,$img_height\n"; # If User Wants Frame, Print Commands to the In-File &make_frame; # Copy Individual Counter Images Commands to In-File $j = 1; while ($j <= $length) { print FLY "copy $insert_width,$insert_height,-1,-1,-1,-1,$digit_images/$CHAR{$j}\.gif\n"; $insert_width = ($insert_width + $width); $j++; } # If they want a color transparent, make it transparent if ($tp ne "X" && $tp =~ /.*,.*,.*/) { print FLY "transparent $tp\n"; } # If they want the image interlaced, make it interlaced if ($il == 1) { print FLY "interlace\n"; } # Close FLY close(FLY); $output = `$flyprog -i $fly_temp`; print "Content-type: image/gif\n\n"; print "$output"; # Remove Temp File # unlink($fly_temp); # Log the Counter Access if ($uselog == 1) { &log_access; } ##### end sub check_referer { if (@referers && $ENV{'HTTP_REFERER'}) { foreach $referer (@referers) { if ($ENV{'HTTP_REFERER'} =~ /$referer/) { $ref = 1; last; } } } else { $ref = 1; } if ($ref != 1) { print "Location: $bad_referer_img\n\n"; if ($uselog == 1) { open(LOG,">>$error_log") || die "Can't Open User Error Log: $!\n"; print LOG "$error: [$date] $ENV{'HTTP_REFERER'} - $jump_from - $ENV{'REMOTE_HOST'} - $ENV{'HTTP_USER_AGENT'}\n"; close(LOG); } close(LK); exit; } } sub get_num { open(COUNT,"$count_file") || die "Can't Open Count Data File to Read ($count_file) : $!\n"; foreach (){ ($html, $ct) = split; $table{$html} = $ct; } $table{$file}++; $count = $table{$file}; close(COUNT); open(COUNT,">$count_file") || die "Can't Open Count Data File to Write ($count_file) : $!\n"; for(sort byval keys %table){ printf COUNT ("%s\t%0d\n",$_,$table{$_}); } close(COUNT); } sub byval {$table{$b} <=> $table{$a};}; sub check_dot { if ($dot == 1) { # Open the In-File for Commands open(FLY,">$fly_temp") || die "Can't Open In File For FLY Commands: $!\n"; # Create New Counter Image print FLY "new\n"; print FLY "size 1,1\n"; print FLY "fill x,y,0,0,0\n"; print FLY "transparent 0,0,0\n"; close(FLY); $output = `$flyprog -i $fly_temp`; print "Content-type: image/gif\n\n"; print "$output"; close(LK); exit; } elsif ($logo ne "X" && $logo =~ /.*tp:\/\//) { print "Location: $logo\n\n"; # Log The Access if ($uselog == 1) { &log_access; } close(LK); exit; } } sub make_frame { $insert_width = $insert_height = $frame_width; $insert_frame = 0; while ($insert_frame < $frame_width) { $current_width = ($img_width - $insert_frame); $current_height = ($img_height - $insert_frame); print FLY "line 0,$insert_frame,$img_width,$insert_frame,$frame_color\n"; print FLY "line $insert_frame,0,$insert_frame,$img_height,$frame_color\n"; print FLY "line $current_width,0,$current_width,$img_height,$frame_color\n"; print FLY "line $current_height,0,$current_height,$img_width,$frame_color\n"; $insert_frame++; } } sub log_access { open(LOG,">>$access_log") || die "Can't Open User Access Log ($access_log): $!\n"; print LOG "[$date] $ENV{'HTTP_REFERER'} - $jump_from - $ENV{'REMOTE_HOST'} - $ENV{'HTTP_USER_AGENT'}\n"; close(LOG); }