#!/usr/bin/perl
# $Id: irasputin.cgi,v 1.8 2007/07/13 18:37:18 giolla Exp $
# $Revision: 1.8 $
#
# Rasputin google salter
# Released under GPL V2
#
# This expects the word files to be in the same directory
# they should be named words.X 
# Where X is 0 to the max value of $Wrnd.
# Defiane location of word files
$WrdFiles='./words';

# Define search engine to salt:
$SearchURL='http://www.google.com/search?q=';
$ImgURL='http://images.google.co.uk/images?q=';

# If we want to disguise the referer and have a list of servers to
# cycle between define it here
$UrlList='';

# As some of the search terms are fun lets log them
$srchLog='./rasputin.log';

# Define range of number of words to use in search
# Default is 1 to 5
srand;
$random = int( rand(5)) + 1;

# Define range of rnadom time to wait between search
# Default is 10 to 600 seconds
$RTime = int( rand(585)) + 15;

# Define GSalt as null
$GSalt='';
$clqte = 0;
$rndqt = 1;

# Get the current time
($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOfYear, $IsDST) = gmtime(time);

$Year += 1900;
$Month++;
$Time = "$Year/$Month/$Day $Hour:$Minute:$Second";

if ( $random == 6  ) {

$dsx = int( rand(5)) + 1;

	if ( $dsx == 6 ) {

		$random = int( rand(5)) + 7;

	};
};

if ( $random > 1 ) {
$rndqt = int( rand(20));
};

if ( $random < 4 ) {

	$imgrndm = int (rand(4));

		if ( $imgrndm < 2 ) {

			$SearchURL=$ImgURL;
			$Img='Image:';
		}
}

# Loop through the word selection until we have enough words.

for ($count = $random; $count >= 1; $count -- ) {

# Pick the word file to use at random

$Wrnd = int( rand(6));

# Open the words file, slect a line and remove the new line.

open FILE, "<$WrdFiles.$Wrnd" or die "Could not open filename: $!\n";
rand($.) <1 and ($line=$_) while <FILE>;
close FILE;
chop ($line );

# Add the new word to GSalt with a %20 for a space
$qte = int( rand(6));
if (( $qte > 4 ) && ( $count >= 2 ) && ( $rndqt > 0 )) {

	if ( $clqte == 1 ){

		$GSalt .= "$line%22%20";
		$clqte = 0;

	} else {

		$GSalt .= "%22$line%20";
		$clqte = 1;

	};
	
} else {

	$GSalt .= "$line%20";
};
}

if ( $rndqt == 0 ) {

	$GSalt = "%22$GSalt%22";

};

if ( $clqte == 1 ){

	$GSalt = "$GSalt%22";
};

# Log the search term then generate the salting page

if ( $srchLog ne '' ){

	if ( open ( LOG, ">>" . $srchLog )){
		print LOG <<EOW;
$Img $GSalt
EOW
		close (LOG);
	};

};

# if there's a url list pick the next url at random else use ourself
# as the url

if ( $UrlList ne '' ) {

	open UFILE, "<$UrlList" or die "Could not open filename: $!\n";
	rand($.) <1 and ($url=$_) while <UFILE>;
	close UFILE;
	chop ($url );
}  else {

$url= $ENV{'REQUEST_URI'};

};

# Now print the page with suitable subsitutions

print <<EOT;
Cache-Control: max-age=0
Pragma: no-cache
Content-type: text/html; charset=iso-8859-1

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Rasputin!</title>
        <meta http-equiv="refresh" content="$RTime;url=$url" />
	<meta http-equiv="Pragma" content="no-cache" />
    </head>
    <body style="background:#99ccff;text-align:center;color:#000033;">
       <div>
        <h1>Rasputin is working!</h1>
        <p>Close this window to stop Rasputin salting your google data
mine.</p>
        <p><a
href="http://www.untrusted.ca/rasputin/">Rasputin</a></p>
<p>Current refresh interval: $RTime seconds<br/>
Rasputin last refreshed at $Time GMT </p>
        </div>
        <div style="color:#ffffff;font-size:smaller;">
            <form action="http://www.google.com/search" method="get" >
                <p>
Normal Google search:
                <input name="q" type="text" size="40" />
                <input type="submit" name="sa" value="Search" />
                </p>
            </form>
<p>N.B. This navigates away from Rasputin, 
or just search below though Rasputin will still refresh.</p>
            </div>
        <div>
        <p>
        <object height="300" data="$SearchURL$GSalt" width="95%"
        type="text/html">
        Rasputin google salter.
       </object>
        </p>
     </div>
	<div style="color:#ffffff;">
	<p>
	Rasputin Version: $Revision: 1.8 $ -
	</p>
	</div>
    </body>
</html>
EOT
