#!/usr/bin/perl
###########################################################################
# Password Protection                                       # Version 1.1 #
###########################################################################
# FORM SETUP:                                                             #
#  - The form can use both a GET and POST method.                         #
#  - The input for the password must be named "pw".                       #
#  - The input for the submit button can not have a name.                 #
#  - There can be no other inputs inside the form.                        #
# EXAMPLE:                                                                #
#    <FORM ACTION="password.cgi" METHOD="post">                           #
#    <INPUT NAME="pw" TYPE="password">                                    #
#    <INPUT TYPE="submit">                                                #
#    </FORM>                                                              #
###########################################################################
# SCRIPT SETUP:                                                           #
#  - This script must be uploaded in ASCII mode.                          #
#  - The password is case insensitive.                                    #
#  - Your $page1 and $page2 pages must exist.                             #
# EXAMPLE:                                                                #
#    $password = "the site rules";                                        #
#    $page1    = "members_only.html";                                     #
#    $page2    = "bad_password.html";                                     #
###########################################################################
$password = "OMFGOMFGOMFG";
$page1    = "http://www.demohq.com/asd98as0d8asdsad7as9d87as9d79as87d97asd6as8d6asd3.shtml";
$page2    = "http://www.demohq.com/noaccess.html";
###########################################################################
if ($ENV{'REQUEST_METHOD'} eq "POST") {
	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	($name, $value) = split(/=/, $buffer);
}
elsif ($ENV{'REQUEST_METHOD'} eq "GET") {
	($name, $value) = split(/=/, $ENV{'QUERY_STRING'});
}
else {
	print "Content-type:text/html\n\n";
	print "The form method must be \"GET\" or \"POST\".";
	exit;
}
$password =~ tr/[A-Z]/[a-z]/;          # Delete these two lines if you want
$value =~ tr/[A-Z]/[a-z]/;             # this script to be case sensitive.
$value =~ tr/+/ /;
$value =~ s/%([a-f0-9][a-f0-9])/pack("C", hex($1))/eg;
if ($value eq $password) {
	print "Location: $page1\n\n";
}
else {
	print "Location: $page2\n\n";
}
exit;