#!/usr/bin/perl

use strict;
use warnings;
use DBI;
use CGI;

my $TABLE = 'thread';
my $DSN = 'DBI:mysql:database=vbf;host=w6.tks.ru;port=3306';
my $DB_USER = 'dummy';
my $DB_PASS = '';
my $BASE_LOCATION = 'http://www.tks.ru/forum/';
my $LOCATION = 'http://www.tks.ru/forum/showthread.php?t=<%T%>';

my $co = new CGI;
my $forum = $co->param('f') ? $co->param('f') : '';
my $thread = $co->param('t') ? $co->param('t') : '';

redirect($BASE_LOCATION) if $forum !~ /^\d+$/ or $thread!~ /^\d+$/;

my $dbh = DBI->connect($DSN, $DB_USER, $DB_PASS)
		or warn $!, redirect($BASE_LOCATION);
my $query = qq~
				select threadid from $TABLE
				where 
        importthreadid = $thread
        and importforumid = $forum
        ~;
my $data = $dbh->selectrow_hashref($query);

redirect($BASE_LOCATION) if !$data or !$data->{threadid};
$LOCATION =~ s/<%T%>/$data->{threadid}/;
redirect($LOCATION);







sub redirect {
  my $location = shift;
  
  print "Location: $location\n\n";
  print qq~Перенаправление на новый адрес: <a href="$location">$location</a>...~;

  $dbh->disconnect if $dbh;  
  exit();
}
