#! /usr/bin/perl -w
use strict ;

###########################################################
# Written by Guy Malachi http://guymal.com
# 18 August, 2002
###########################################################

# This function generates random strings of a given length
sub generate_random_string
{
    my $length_of_randomstring=shift;# the length of 
	 # the random string to generate

	my @chars=('a'..'z','A'..'Z','0'..'9','_');
    my $random_string;
    foreach (1..$length_of_randomstring) 
    {
	# rand @chars will generate a random 
	# number between 0 and scalar @chars
	$random_string.=$chars[rand @chars];
    }
    return $random_string;
}

my $newCookie = &generate_random_string(32) ;
my $oldCookie ;
if (exists $ENV{'HTTP_COOKIE'}) {
    # This is rather pathetic
    ( $oldCookie ) = ($ENV{'HTTP_COOKIE'}
		      =~ /^.*tracker=(................................).*$/) ;
}
if (defined $oldCookie) {
    $newCookie = $oldCookie ;
}

print <<EOHEAD;
Content-type: text/html
Set-Cookie: tracker=${newCookie}; domain=cs.unca.edu; path=/; expires=Wed, 01-Jan-2020 20:20:20 GMT

<html>
<head>
<link href="http://www.cs.unca.edu/~brock/classes/style/handout.css"
      rel="stylesheet" type="text/css" />
<title>CSCI 107 Cookie Injector</title>
</head>
<body>
<h1>CSCI 107 Cookie injector</h1>
<h2>Your location</h2>
EOHEAD

my $remoteAddr ;
if (exists $ENV{'REMOTE_ADDR'}) {
    $remoteAddr = $ENV{'REMOTE_ADDR'} ;
} else {
    $remoteAddr = '<strong>unknown</strong>' ;
}
print "<p>Hi there. I see you are connecting from IP address ${remoteAddr}.</p>\n" ;
my %IPhash = (
    '24.179.26.168'   => 'at the Brock/Bruce house',
    '152.18.69.227'   => "in Dean's office",
    '152.18.69.231'   => "in Dean's office",
    '152.18.27.214'   => "in Charles' office",
    '152.18.33.201'   => 'using a front row PC in KH037',
    '152.18.33.202'   => 'using a front row PC in KH037',
    '152.18.33.203'   => 'using a front row PC in KH037',
    '152.18.33.204'   => 'using a front row PC in KH037',
    '152.18.33.204'   => 'using a front row PC in KH037',
    '152.18.33.206'   => 'using a 2nd row PC in KH037',
    '152.18.33.207'   => 'using a 2nd row PC in KH037',
    '152.18.33.208'   => 'using a 2nd row PC in KH037',
    '152.18.33.209'   => 'using a 2nd row PC in KH037',
    '152.18.33.210'   => 'using a 2nd row PC in KH037',
    '152.18.33.211'   => 'using a 2nd row PC in KH037',
    '152.18.33.215'   => 'using a 3rd row PC in KH037',
    '152.18.33.216'   => 'using a 3rd row PC in KH037',
    '152.18.33.217'   => 'using a 3rd row PC in KH037',
    '152.18.33.212'   => 'using a back row PC in KH037',
    '152.18.33.213'   => 'using a back row PC in KH037',
    '152.18.33.214'   => 'using a back row PC in KH037',
    '152.18.33.218'   => 'using a back row PC in KH037',
    '152.18.33.219'   => 'using a back row PC in KH037',
    '152.18.33.220'   => 'using a back row PC in KH037',
    '152.18.33.227'   => 'using the instructor PC in KH037',
) ;
if (exists $IPhash{$remoteAddr}) {
    print "<p>Are you ${IPhash{$remoteAddr}}?</p>\n" ;
}

if (defined $oldCookie) {
print <<EOCOOKIEOLD;
<h2>Your cookie</h2>
<p>
I see you've been here before and were given
a <kbd>tracker</kbd> cookie with <em>content</em>
<kbd>${oldCookie}</kbd>.
</p>
<p>
I know you're thinking that I promised not to remember your cookie.
I didn't!  Your browser remembered it and passed it back to me.
</p>
<p>
Believe me I know nothing about <kbd>${oldCookie}</kbd>.
</p>
EOCOOKIEOLD

} else {

print <<EOCOOKIENEW;
<h2>Your cookie</h2>
<p>
I've given you a cookie for the <kbd>cs.unca.edu</kbd> web server.
Its name is <kbd>tracker</kbd> and its <em>content</em>
is <kbd>${newCookie}</kbd>.
The long "word", <kbd>${newCookie}</kbd>, was generated just for you.
It <em>could</em> be stored in a database, along with other information
that could be obtained by having you fill out some forms.
</p>
<p>
I could even share information with computers to get
a pretty good idea of your interests.  However, I'm just going
to forget all about you and <kbd>${newCookie}</kbd>.
</p>
EOCOOKIENEW
}

print <<EOTAIL;
</body>
</html>
EOTAIL
