#!/usr/bin/perl

  prepare();

  $id=$FORM{'id'};
  $url=$FORM{'url'};

#if ($id =~ /^(\d+)$/) {
#  $sth = $dbh->query("update `inout` set outcat=outcat+1 where id=$id and day=date_format(now()-interval (weekday(now())) day,\"%Y-%m-%d\")")
#}

#&cleardb;

%ids=();
open (ddd, "/www/akavita/include/topsites_list.txt") || die "no file";
while (<ddd>) {
  $s=$_;
  chop $s;
  my ($site_id,$site_url)=split(/\t/,$s);
#print "1: $site_id,$site_url\n";
  $ids{$site_id}=$site_url;
}
close ddd;

$tim=time;
open(ddd,">>/www/adlik/tmp/tophref.txt");
print ddd "$id\t$tim\t$ENV{REMOTE_ADDR}\t$url\t$ENV{HTTP_USER_AGENT}\t$ENV{HTTP_REFERER}\n";
close ddd;

if (exists $ids{$id}) {
  print "Status: 302 Found\n";
  print "Location: http://$ids{$id}\n\n";
}
else {
  print "Status: 400 Bad Request\n\n";
  print "Wrong URI\n";
}

exit(0);


############################################################
sub prepare {

  %FORM=();
  $qs = $ENV{'QUERY_STRING'};
 
  @pairs = split(/&/, $qs);
  foreach $pair (@pairs){
	my ($name, $value) = split(/=/, $pair);
	if ($FORM{$name}) {
		$FORM{$name} = "$FORM{$name}, $value";
	}
	else {
		$FORM{$name} = $value;
	}
  }
}
