#!/usr/local/bin/perl
################################################################################################################
#
# $Id: redirect.cgi,v 1.2 1999/11/30 23:07:22 shetland Exp $
#
# $RCSfile: redirect.cgi,v $
#
# $Header: /cvs/stuffQuest/redirect.cgi,v 1.2 1999/11/30 23:07:22 shetland Exp $
#
################################################################################################################
#
# $Log: redirect.cgi,v $
# Revision 1.2 1999/11/30 23:07:22 shetland
# 1) Changed SELECT queries to reflect column-name changes in sql server.
# 2) Modified Distfile to reflect to source directory for dist'ing.
#
################################################################################################################
#
$|=1;
#
use DBI;
#
################################################################################################################
#
require "./index.config";
#
################################################################################################################
#
# This section shamelessly stolen (and modified for my own purposes) from CGI.pm
#
# unescape URL-encoded data
sub unescape {
shift() if ref($_[0]);
my $todecode = shift;
return undef unless defined($todecode);
$todecode =~ tr/+/ /; # pluses become spaces
$todecode =~ s/%([0-9a-fA-F][0-9a-fA-F])/pack("c",hex($1))/ge;
return $todecode;
}
#
# URL-encode data
sub escape {
shift() if ref($_[0]) || $_[0] eq $DefaultClass;
my $toencode = shift;
return undef unless defined($toencode);
$toencode =~ s/([^-._0-9a-zA-Z])/uc sprintf("%%%02x",ord($1))/eg;
return $toencode;
}
#
# Read data from a file handle
sub read_from_client {
my($fh, $buff, $len, $offset) = @_;
local $^W=0; # prevent a warning
return undef unless defined($fh);
return read($fh, $$buff, $len, $offset);
}
#
sub parse_params { my($tosplit) = @_;
my(@pairs) = split(/[&;]/,$tosplit);
my($param,$value);
foreach (@pairs) {
($param,$value) = split('=',$_,2);
$param = unescape($param);
if ($value) {
$value = unescape($value);
$in{$param} = $value;
} else {
$in{$param} = 1;
}
}
}
#
sub ReadParse {
my $meth = $ENV{'REQUEST_METHOD'} if defined($ENV{'REQUEST_METHOD'});
my $content_length = defined($ENV{'CONTENT_LENGTH'}) ? $ENV{'CONTENT_LENGTH'} : 0;
local $query_string;
METHOD: {
# If method is GET or HEAD, fetch the query from
# the environment.
if ($meth =~ /^(GET|HEAD)$/) {
$query_string = $ENV{'QUERY_STRING'} if defined $ENV{'QUERY_STRING'};
last METHOD;
}
if ($meth eq 'POST') { $query_string = read_from_client(\*STDIN,\$query_string,$content_length,0)
if $content_length > 0;
# Want to have their cake and eat it too?
# Uncomment this line to have the contents of the query string
# APPENDED to the POST data.
# $query_string .= (length($query_string) ? '&' : '') . $ENV{'QUERY_STRING'} if defined $ENV{'QUERY_STRING'};
last METHOD;
}
}
#
# We now have the query string in hand. Let's not dilly dally around if
# it's empty.
if ($query_string ne '') { &parse_params($query_string);
}
}
#
################################################################################################################
#
# The rest as they say is home grown..
#
sub debug_info {
print("
DEBUG OUTPUT: \n \n \nQuery String:
");
foreach $current_key (keys %in) {
print("
$current_key
-
$in{$current_key}
\n");
}
print("
");
print("
Environment:
");
foreach $current_env (keys %ENV) {
print("
$current_env
-
$ENV{$current_env}
\n");
}
print("
");
$self_url = $CGIURL . "\?" . $ENV{'QUERY_STRING'};
print(qq|Reference myself here \n|);
}
#
sub vs_print {
my($rvs_scalar,$filename) = @_; # take first param, ignore the rest.
open(FH,qq{>>$filename}); # We do several writes to the file.
eval "print FH qq{$$rvs_scalar}"; # This will change in the future.
close FH;
}
#
sub query_db {
my ($query) = @_; # take first param, ignore the rest.
if (!$dbh->{Active}) {
$dbh = DBI->connect('DBI:mysql:' . $db_database, $db_user, $db_pass);
&check_db_err(\$dbh);
}
my $db_query_sth = $dbh->prepare($query);
if (&check_db_err(\$db_query_sth)) {
$db_query_sth->execute;
if (&check_db_err(\$db_query_sth)) {
return $db_query_sth;
}
}
}
#
sub check_db_err {
my ($handle) = @_; # take first param, ignore the rest.
if ($errornum = $$handle->err) {
$errorstr = $$handle->errstr;
print "SQL error: ($errornum: $errorstr)\n";
die "MySQL error ($errornum: $errorstr)\n";
}
return 1;
}
#
################################################################################
#
# Script Execution begins here.
#
&ReadParse; #get my params
#
undef $query;
if ($in{'URL'}) {
$query = qq{SELECT URLs.URL FROM URLs WHERE URLs.ID = '$in{URL}'};
} elsif ($in{'OTHER'}) {
$query = qq{SELECT OtherSites.URL FROM OtherSites WHERE OtherSites.ID = '$in{OTHER}'};
}
if ($query) {
$sth = &query_db("$query");
if ($sth->rows >= 1) {
if(($LinkURL) = $sth->fetchrow) {
$LinkURL =~ s/^#|#$//go;
print qq{Location: $LinkURL\n\n};
}
} else {
print qq{Content-type: text/html\n\n};
print <stuffQuest - The Never Ending 'Quest' for More Stuff, ...and it's all FREE!!
An error has occured.
Your request can not be prcessed at this time. In a moment you will
be brought back to the page from which you came.
END_OF_HTML_BODY
}
undef $sth;
} else {
print <stuffQuest - The Never Ending 'Quest' for More Stuff, ...and it's all FREE!!
An error has occured.
You can not call this script directly! In a moment you will
be brought back to the stuffQuest home page.