#!/usr/local/bin/perl
# Counter Script
# Version 1.1.1 (TB)
# Originally created by Matt Wright	mattw@misha.net. Created on: 10/27/95	
#	
# Matt's Scripts Archive:  http://www.worldwidemart.com/scripts/
# Consult the file README for more information and Installation Instructions.

#######################################################################
# Define Variables

	### FILE AND DIRECTORY LOCATIONS, REFERERS ###

$base_dir   = "/export/home/stryker/www" ;
$count_file = "$base_dir/counter/count.txt";
$digit_dir  = "$base_dir/counter/digits";
$access_log = "$base_dir/counter/access_log";
$error_log  = "$base_dir/counter/error_log";
$output_log  = "$base_dir/counter/output.gif";

$flyprog    = "$base_dir/counter/fly -q";
$fly_temp   = "$base_dir/counter/fly_temp.txt";

$bad_referer_img = "http://www.jeff-stryker.com/images/bad_referer.gif";

@referers = ("www.jeff-stryker.com","www.js-grand.com");

	### IMAGE SETTINGS ###

$width = "15";
$height = "20";

$tp = "X";  # 	This will set your transparency color.  If you leave this 
            #   variable at X, it will assume you don't want to make any color in your 
            #   images transparent. If you do want to make something transparent, put the 
            #   rgb color code in for this variable.  If you want to make black 
            #   transparent, you would set $tp = "0,0,0"; or if you wanted the white color 
            #   to be transparent you would set $tp = "255,255,255";


$il = "1";  # Set this variable to 1 if you want it interlaced and 0 if you do not.


$frame_width = "3";
$frame_color = "0,0,0";

$dot = "X";  # set to "X" if a tranparent dot is NOT desired, "1" if it is desired

# NOTE that the next line should be the URL of the logo that
# you want counted, or "0" if you do NOT want a logo counted.
# If you give a logo's URL, then the counter will not be displayed,
# and the logo will show instead.  (And, "X" should be set above.)

# $logo = "http://www.jeff-stryker.com/images/clearpixel.gif";

$logo = "0";

	### OPTIONS ###

$uselog = "0";	# 1 = YES; 0 = NO
$max_time = "10";

$DEBUG = 0 ;

# Done
#######################################################################

# Get the Date For Logging Purposes
if ($uselog == 1) 
{
   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
   
   $mon++;
   
   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 = "$hour\:$min\:$sec -- $mon/$mday/$year";
}

# 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;

# 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_dir/$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`;
##&log_output($output) ;
print "Content-type: image/gif\n\n";
print "$output";

# Remove Temp File
unlink($fly_temp);

# Log the Counter Access
if ($uselog == 1) 
{
   &log_access;
}

sub check_referer 
{
  my($ref) = 0 ;

  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) 
    {
       open(LOG,">>$error_log") || die "Can't Open User Error Log: $!\n";
       print LOG "$error: $ENV{'REMOTE_HOST'} [$date] $ENV{'HTTP_REFERER'} - $ENV{'HTTP_USER_AGENT'}\n";
       close(LOG);
    }

    exit;
  }
}

sub get_num 
{
  open(COUNT,"$count_file") || die "Can't Open Count Data File: $!\n"; 
  $count = <COUNT>;
  close(COUNT);
  if ($count =~ /\n$/) 
  {
     chop($count);
  }

  $count++;

  open(COUNT,">$count_file") || die "Can't Open Count Data File For Writing: $!\n";
  print COUNT "$count";
  close(COUNT);
}

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";

    exit;
  }
  elsif ($logo ne "X" && $logo =~ /.*tp:\/\//) 
  {
    print "Location: $logo\n\n";

    # Log The Access
    if ($uselog == 1) 
    {
         &log_access;
    }

    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: $!\n";
   print LOG "[$date] $ENV{'HTTP_REFERER'} - $ENV{'REMOTE_HOST'} -  $ENV{'HTTP_USER_AGENT'}\n";
   close(LOG);
}

sub log_output
{
   my($output) = @_ ;

   open(LOG,">>$output_log") || die "Can't Open User Access Log: $!\n";
   print LOG "$output" ;
   close(LOG);
}

