#!/usr/bin/perl
###############################################################################
# JxDownload V 2.01a                       Copyright Johan Johansson 2002-05-27
# johan@jojoxx.net - http://www.jojoxx.net
###############################################################################
#
use POSIX;

# Variabler.
$allownew = '1';                                 # Skall nya poster få skapas? '1'=ja, '0'=nej.
$require_password_for_stats = '0';               # Krävs lösenord för att visa statistik? '1'=ja, '0'=nej.
$statspassword = 'jxdownload';                   # Lösenord om ovanstående variabel är satt till '1'.
$require_password_for_download = '0';            # Krävs lösenord för att ladda ner filer? '1'=ja, '0'=nej.
$downloadpassword = '';                          # Lösenord om ovanstående variabel är satt till '1'.
$hitsmode=1;                                     # Tillgänliga mode 1,2.
$allowed_referer = '';                           # Tillåten referer (inkl. undersidor).
$redirect_if_refused = '';                       # Var skall man skickas om hämtningen inte tillåts?

# Script.
%query = getQuery();
$version = '2.01a';
$scriptname='jxdownload20.pl';
if($allowed_referer && (lc(substr($ENV{'HTTP_REFERER'},0,length($allowed_referer))) ne lc($allowed_referer))){
	if($redirect_if_refused){
		print "Location: $redirect_if_refused\n\n";
	} else {
		print "Content-type:text/html\n\n<h4>Access denied</h4>";
	}
	exit;
}
open(INDATA,getScriptDir()."/jxdownload20.txt");
@file=<INDATA>;
close(INDATA);
for($n=0;$n<=$#file;$n++){
	chomp $file[$n]; ($thisurl,$thisid,$thishits,$thisdesc) = split(/;/,$file[$n]);
	if($query{'sort'} eq 'hits'){ $file[$n]=$thishits.";".$file[$n]; } elsif($query{'sort'} eq 'desc'){ $file[$n]=$thisdesc.";".$file[$n]; } else { $file[$n]=$thisurl.";".$file[$n]; }
}
if($query{'sort'} eq 'hits'){
	@file=sort {$a <=> $b} @file;
	@file=reverse @file;
} else {
	@file=sort @file;
}
for($n=0;$n<=$#file;$n++){
	$file[$n]=substr($file[$n],index($file[$n],";")+1);
}
if(!$query{'url'} && !$query{'id'}){
	if($require_password_for_stats eq '1' && $query{'statspassword'} ne $statspassword){
		print "Content-type: text/html\n\n";
		print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">'."\n".'<html><head><title>Ogiltigt lösenord</title></head><body><h4>Ogiltigt lösenord!</h4></body></html>';
		CORE::exit;
	}
	# Visa statistik.
	$totalhits=0; $mosthits=0;
	for($n=0;$n<=$#file;$n++){
		($thisurl,$thisid,$thishits,$thisdesc) = split(/;/,$file[$n]);
		$totalhits+=$thishits;
		$mosthits=$thishits if($thishits>$mosthits);
	}
	$result = '<tr><td width="100" bgcolor="#dddddd"><b><a href="'.$scriptname.'">Fil</a></b></td><td width="300" bgcolor="#dddddd"><b><a href="'.$scriptname.'?sort=desc">Beskrivning</b></a></td><td bgcolor="#dddddd"><b><a href="'.$scriptname.'?sort=hits">Statistik</a></b></td></tr>'."\n";
	for($n=0;$n<=$#file;$n++){
		($thisurl,$thisid,$thishits,$thisdesc) = split(/;/,$file[$n]);
		$thisid = $thisurl if(!$thisid);
		if($thishits>0 && $totalhits>0){
			$procent = floor($thishits/$totalhits*100);
			if($hitsmode eq 1){
				$stapelwidth = floor($thishits/$mosthits*200);
			} else {
				$stapelwidth = floor($thishits/$totalhits*200);
			}
		} else {
			$procent = 0;
			$stapelwidth = 0;
		}
		$whitewidth = 200-$stapelwidth;
		$result .= <<End_of_html
<tr><td><a href="$thisurl">$thisid</a></td><td>$thisdesc &nbsp;</td><td>
<table border=0 cellpadding=0 cellspacing=0>
<tr><td width="$stapelwidth" bgcolor="green" height="10"></td><td width="$whitewidth" bgcolor="#dddddd" height="10"></td></tr><tr><td colspan=2>$procent \% - $thishits träff(ar)</td></tr>
</table>
</td></tr>
End_of_html
	}
	print "Content-type: text/html\n\n";
	print <<End_of_page;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
	<title>Statistik</title>
<style type="text/css">
td { font-family: Arial,Sans-Serif; font-size: 10pt; }
a { color: blue; text-decoration:none; }
a:hover { text-decoration:underline; }
</style>
</head>
<body>
<h2>JxDownload $version</h2>
<h4>Statistik</h4>
<table border=1 cellpadding=0 cellspacing=0>
$result
</table>
Totalt $totalhits filer nerladdade.<br>
<span style="font-size: 8pt;">&copy; <a href="http://www.jojoxx.net" style="text-decoration: none;">Jojoxx</a> - 2000</span>
</body>
</html>
End_of_page
	CORE::exit;
}
if($require_password_for_download eq '1' && $query{'downloadpassword'} ne $downloadpassword){
	print "Content-type: text/html\n\n";
	print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">'."\n".'<html><head><title>Ogiltigt lösenord</title></head><body><h4>Ogiltigt lösenord!</h4></body></html>';
	CORE::exit;
}
for($n=0;$n<=$#file;$n++){
	chomp $file[$n];
	($thisurl,$thisid,$thishits,$thisdesc) = split(/;/,$file[$n]);
	if(($query{'url'} && $query{'url'} eq $thisurl) || ($query{'id'} && $query{'id'} eq $thisid)){
		$thishits++;
		$redirect = $thisurl;
		$file[$n] = join(';',$thisurl,$thisid,$thishits,$thisdesc);
	}
}
if(!$redirect && $allownew eq '1' && $query{'url'}){
	$redirect = $query{'url'};
	push(@file,"$query{'url'};;1;");
}
open(OUTDATA,">".getScriptDir()."/jxdownload20.txt");
for($n=0;$n<=$#file;$n++){
	print OUTDATA "$file[$n]\n";
}
close(OUTDATA);
if(!$redirect){
	print "Content-type: text/html\n\n";
	print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">'."\n".'<html><head><title>Ogiltigt anrop</title></head><body><h4>Ogiltigt anrop!</h4></body></html>';
	CORE::exit;
}
print "Location: $redirect\n\n";



sub getScriptDir{
	my $path = $ENV{'PATH_TRANSLATED'};
	$path = $ENV{'SCRIPT_FILENAME'} if(!$path);
	$path =~ s/\\/\//g;
	$path = substr($path,0,rindex($path,'/'));
	$path =~ s/\/+$//g;
	return $path;
}
sub getQuery{
	my ($name,$value,$pair);
	my (%query);
	my @pairs = split(/&/, $ENV{'QUERY_STRING'});
	if ($ENV{'QUERY_STRING'}) {
		foreach $pair (@pairs){
			($name,$value) = split(/=/, $pair);
			$value =~ tr/+/ /;
			$name =~ tr/+/ /;
			$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
			$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
			$query{$name} = $value;
		}
	}
	return %query;
}
