#! /usr/bin/perl -w

use DBI; 

$| = 1; 

$db = DBI->connect('DBI:mysql:database=elvis;user=elvis;password=fidodido'); 

$getnewhits = $db->prepare(q{
    select referer, firstseen, lastseen, count, description from tubgirl
	where unix_timestamp(now()) - firstseen < 900 order by firstseen
    }); 

$getoldhits = $db->prepare(q{ 
    select referer, firstseen, lastseen, count, description from tubgirl
	where unix_timestamp(now()) - firstseen > 900 order by firstseen
    }); 

$getnewhits->execute; 
$getoldhits->execute; 

print <<EOF; 
Content-type: text/html

<html><head><title>automated tubgirl defense system</title></head><body>

<hr>
<B>WARNING</b>: The tubgirl image is SO NOT SAFE FOR WORK it's not even funny.
If you're easily offended or traumatized, do not click on any of the links on this
page - close your browser and forget you ever came here.  A safe explanation of
tubgirl and other similar images can be found in
<a href="http://en.wikipedia.org/wiki/Tubgirl" target="wikipedia">the Wikipedia
article on shock sites</a>.
<p>
<hr>

I hate it when lazy people can't be bothered to copy the image off onto their own
web site and instead just link directly to my site.  It's just plain rude.  It uses
my bandwidth and my resources to display my imagery on their site.  In the past, I'd 
happen to notice them in the referrer logs of my web sites, and if I was feeling 
particularly vindictive, I'd give them the tubgirl treatment - I'd substitute 
the <a href="http://www.tubgirl.com/" target="tubgirl">tubgirl.jpg</a> image for whatever
image they were linking to, causing tubgirl to appear in all her glory on the poor
schmuck's web site.  It was fun, and gratifying, and usually got the message across
quite clearly and quickly, but the one drawback was that it was a lot of work.  
<p>

After reading about a similar situation on <a href="http://jeremy.zawodny.com/blog/archives/000826.html" target="zawodny">Jeremy Zawodny's blog</a>, I realized that
by combining his mod_rewrite approach with a little perl, the entire process 
could be automated.  
<p>

An hour of light coding and apache configuration later, and the Automated
Tubgirl Defense System was born.  Requests for images hosted on ent.ity.org are checked
to make sure the referer looks right before serving the image.  If the referer indicates
that the image is being hijacked, it's checked against a SQL database to see if it's a 
fresh hijacking or an existing one.  If it's new, the correct image is served for a small
period of time.  After that initial grace period expires, the image is transparently replaced
with tubgirl.  
<p>

Yes, this approach is essentially a bait-and-switch for asshats that hijack images from
my site.  It's designed that way.  It wouldn't be any fun if I just gave them tubgirl right
from the get-go, since they'd just get squicked right off the bat and remove the image, 
preventing other people from seeing tubgirl.  The fun part of this approach comes after
the initial hijacking, when tubgirl is substituted for visitors to the hijacking site, 
who then react in various amusing ways.  

EOF

print "<p>\n"; 

$site=0;

if ($getnewhits->rows) { 

    print "<b><u>new sites that have not yet been tubgirled</b></u><p>\n"; 

    while ( ($referer, $firstseen, $lastseen, $count) = $getnewhits->fetchrow_array ) { 
	
	$site++; 
	
	print "<a href='$referer' target='tubgirl-$site'>$referer</a><br>\n"; 
	print "first hijacked ", scalar localtime($firstseen), "<br>\n"; 
	print "last visited ", scalar localtime($lastseen), "<br>\n"; 
	print "count : $count<br>\n"; 
	print "<p>\n"; 
    }

}    

if ($getoldhits->rows) { 

    print "<b><u>sites that are actively being tubgirled</b></u><p>\n"; 

    while ( ($referer, $firstseen, $lastseen, $count) = $getoldhits->fetchrow_array ) { 

	$site++; 
	
	print "<a href='$referer' target='tubgirl-$site'>$referer</a><br>\n"; 
	print "first hijacked ", scalar localtime($firstseen), "<br>\n"; 
	print "last visited ", scalar localtime($lastseen), "<br>\n"; 
	print "count : $count<br>\n"; 
	print "<p>\n"; 
    }

}    

print "</body></html>\n"; 
