#!/usr/bin/perl
##############################################################################
# Cliff's 404 Finder Version 1.01                                            # 
# Copyright 1998 Shaven Ferret Productions                                   #
# Created 7/9/98        Last Modified 9/13/98                                #
# Available at http://www.shavenferret.com/scripts                           #
##############################################################################
# COPYRIGHT NOTICE                                                           #
# Copyright 1998 Shaven Ferret Productions All Rights Reserved.              #
#                                                                            #
# This script can be used\modified free of charge as long as you don't       #
# change this header thing.  By using this script you agree to indemnify     #
# me from any liability that might arise from its use.  In simple English,   #
# if this script somehow makes your computer run amuck and kill the pope,    #
# it's not my fault.                                                         #
#                                                                            #
# Redistributing\selling the code for this program without prior written     #
# consent is expressly forbidden.                                            #
##############################################################################

# if the data file is not in the same directory as this script, enter it's
# path.
$datafile = "404.txt";

# Enter the URL of the page you would like people to be forwarded to when they
# try to view a file that doesn't exist.
$go_to = "/404.html";

# Enter the password you want to use.
$password = "shavenferret.com";

##############################################################################
# Congratulations!  You've finished defining the variables.  If you want to, #
# you can continue screwing with the script, but it isn't necessary.         #
##############################################################################

open (FILE,"$datafile");
@urls = <FILE>;
close(FILE);


if ($ENV{'QUERY_STRING'}) {
        if ($ENV{'HTTP_REFERER'}) {
                $i = 0;
                for ($i = 0; $i <= $#urls; $i++) {
                        chomp($urls[$i]);
                        ($num,$url) = split(/\|/, $urls[$i]);
                        if ($ENV{'HTTP_REFERER'} eq $url) {
                                $num++;
                                $urls[$i] = "$num|$url";
                                $found = 1;                        
                        }
                }
                unless ($found) { push(@urls,"1|$ENV{'HTTP_REFERER'}"); }
                if ($#urls > 1) {
                        for($i = 0; $i < $#urls; $i++) {
                                for($j = 0; $j <= $i; $j++) {
                                        ($num1,$x) = split(/\|/,$urls[$j]);
                                        ($num2,$x) = split(/\|/,$urls[$j+1]);
                                        if ($num1 < $num2) {
                                                $temp = $urls[$j];
                                                $urls[$j] = $urls[$j+1];
                                                $urls[$j+1] = $temp;
                                        }
                                }
                        }
                }       
                open(FILE,">$datafile");
                foreach $line(@urls) { print FILE "$line\n"; }
                close(FILE);
        }
        print "Location: $go_to\n\n";
}
elsif ($ENV{"REQUEST_METHOD"} eq 'POST') {
        print "Content-type: text/html\n\n";
        read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
        @pairs = split(/&/, $buffer);
        foreach $pair (@pairs) {
                ($name, $value) = split(/=/, $pair);
                $value =~ tr/+/ /;
                $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
                $FORM{$name} = $value;
        }
        if ($FORM{'password'} eq $password) {
                open (FILE,">$datafile");
                close(FILE);
                print "<html><head><title>Deleted</title></head>\n";
                print "<body>The data has been deleted</body></html>\n";
                exit;
        }
        else {
                print "<html><head><title>Bad Password</title></head><body>\n";
                print "The password you entered is incorrect.</body></html>\n";
                exit;
        }
}
else {
        print "Content-type: text/html\n\n";
        print "<html><head><title>404 Report</title></head><body>\n";
        print "<center><a href=\"http://www.cgi-resources.com/rate?01975\">Rate\n";
        print "this Script @ The CGI Resource Index</a></center>\n";
        print "<h1>404 Report</h1>\n";
        print "<table cols=\"2\"><tr><td width=\"30\">Hits</td><td>Referring URL</td></tr>\n";                
        foreach $line(@urls) {
                chomp($line);
                ($num,$url) = split(/\|/,$line);
                print "<tr><td width=\"30\">$num</td><td><a href=\"$url\" ";
                print "target=\"$url\">$url</a></td></tr>\n";
        }
        print "</table><h1>Delete Data</h1><form method=\"POST\">\n";
        print "Password:<input type=\"password\" name=\"password\"><br>\n";
        print "<input type=\"submit\" value=\"Delete\"></form></body></html>\n";
}
