#!/usr/bin/perl
#########################################################################
#  Top Downloads v2.0  (SSI)                                            #
#  Copyright (c)2000 Chi Kien Uong                                      #
#  URL: http://www.proxy2.de                                            #
#                                                                       #
# This Software is distributed under the GNU General Public             #
# License. For more details see license.txt                             #
#                                                                       #                                                                        #
# To include the table into a HTML document use:                        #
# <!--#include virtual="cgi-bin/status.pl?top=xx" -->                   #
#                                                                       #
# To include a count number into a HTML document use:                   #
# <!--#include virtual="cgi-bin/status.pl?stat=file_id" -->             #
#                                                                       #
# Total downloads:                                                      #
# <!--#include virtual="cgi-bin/status.pl?total=1" -->                  #
#                                                                       #
#########################################################################

# base url to the *.gif files without trailing "/"
$base_gif = "http://www.proxy2.de/gif";

# path to the log file - relative to script location or absolute
$basedat = "log.txt";

$table_width = 560;	# width of hole table
$table_text = 250;	# width reserve for the file description
$table_bg = "#F7F7F7";	# table background color
$table_top = "#E1A8A8";	# top table background color
$show_default = 15;	# 10=TOP_10  20=TOP_20,etc.
$font_size = 'face=Arial size=1';	# font style and size
# End Setup
#####################

@querys = split(/&/, $ENV{'QUERY_STRING'});
foreach $query_str (@querys) {
  ($name, $value) = split(/=/, $query_str);
  $query{$name} = $value;    
}

print "Content-type: text/html\n\n";

if ($query{'stat'} || $query{'total'}) {

open(DATA,"$basedat");
@lines = <DATA>;
close(DATA);
shift(@lines);
if ($query{'stat'}) {
  foreach $line (@lines)  {
    ($file_id,$count,$location,$stamp,$descript) = split(/\|/,$line);
    if ($query{'stat'} eq $file_id) {
      print ("$count");
      last;
    }
  }
}
else {
  foreach $line (@lines)  {
    ($file_id,$count,$location,$stamp,$descript) = split(/\|/,$line);
    $total += $count;
  }
  print ("$total");
}
}

else {

$query{'top'} = $show_default if ($query{'top'} !~ /^\d+/);

unless (open(DATA,"$basedat")) {
  if (-e "$basedat") {
    die ("File exists, but cannot be opened.\n");
  } 
  else {	
    open(DATA,">$basedat");
  }	
}
@sort_array = <DATA>;
chop (@sort_array);
close(DATA);
$last_download = shift(@sort_array);

print "<table bgcolor=\"$table_bg\" border=0 cellpadding=2 cellspacing=1 width=$table_width align=center>\n"; 
print "  <tr>\n    <td colspan=2 bgcolor=\"$table_top\"><b><font $font_size color=\"#990000\">Top $query{'top'} Downloads</font></b></td>\n  </tr>\n";
$total=0;
foreach $input (@sort_array) {
  ($filenum, $countnum, $location, $stamp, $description) = split(/\|/,$input);
  $name_array{$filenum}=$countnum;
  $descript_array{$filenum}=$description;
  $total += $countnum;
}
if ($total>0) {
 $show=0;
 $flag=0;
 foreach $file_id (sort {$name_array{$b} <=> $name_array{$a}} (keys(%name_array))) {
   if ($show < $query{'top'}) {
         $top_pos = $name_array{$file_id} if ($flag == 0);
         $percent = ($name_array{$file_id} *100/ $total);
 	 if ($percent <= 1) {$img = 'blue.gif';}
     	 elsif ($percent > 1 && $percent <= 2) {$img = 'green.gif';}
     	 elsif ($percent > 2 && $percent <= 5) {$img = 'aqua.gif';}
     	 elsif ($percent > 5 && $percent <= 8) {$img = 'pink.gif';}
     	 elsif ($percent > 8 && $percent <= 11) {$img = 'yellow.gif';}
     	 elsif ($percent > 11 && $percent <= 14) {$img = 'purple.gif';}
     	 elsif ($percent > 14 && $percent <= 17) {$img = 'brown.gif';}
     	 elsif ($percent > 17 && $percent <= 20) {$img = 'darkgreen.gif';}
     	 elsif ($percent > 20 && $percent <= 23) {$img = 'red.gif';}
     	 elsif ($percent > 23) {$img = 'gold.gif';}
     	 $img_width = int($name_array{$file_id} *($table_width-$table_text)/$top_pos);
     	 if ($descript_array{$file_id} ne "---") {
	 print "    <tr>\n      <td><font $font_size>$descript_array{$file_id}</font></td>\n      <td><font $font_size><img src=\"$base_gif/$img\" width=$img_width height=10> $name_array{$file_id}</font></td>\n    </tr>\n";
         }
         else {
         print "    <tr>\n      <td><font $font_size>$file_id</font></td>\n      <td><font $font_size><img src=\"$base_gif/$img\" width=$img_width height=10> $name_array{$file_id}</font></td>\n    </tr>\n";
	 }
         $show++;
         $flag=1;
   }
   else {
      last;
   }
 }
}
print "    <tr>\n      <td><font $font_size>Statistic</font></td>\n      <td><font $font_size>Last Download: </font><font $font_size color=\"#660066\">$last_download</font><font $font_size> Total\n";
print "        Downloads: </font><font $font_size color=\"#FF0000\">$total</font></td>\n    </tr>\n</table>"; 
}
exit(0);
