#!/usr/local/bin/perl
#########################################################
#                                                       #
#                     RedirectPro                       #
#                                                       #
#########################################################
#                                                       #
#                      script.pl                        #
#                                                       #
#   Configure this script by filling in the variables   #
#   below. After that place both scripts into your      #
#   CGI-BIN and CHMOD them 755. Put the datafile into   #
#   into a non-web accessable directory (if you don't   #
#   have access to one a web accessable directory will  #
#   work but is much less secure) and CHMOD it 777.     #
#   CHMOD the directory your users accounts will be     #
#   stored in 777.                                      #
#                                                       #
#   If you have any further questions, direct them to   #
#   the support forum at http://www.go2cgi.com/         #
#                                                       #
#########################################################
#                                                       #
#         Copyright © 1999 W. Dustin Alligood           #
#                                                       #
#########################################################

# The path to the file you want displayed at the top
# of all the CGI generated pages (sign-up/modify)
$header='/data1/hypermart.net/go2/redirect/header.txt';

# The path to the file you want displayed at the bottom
# of all the CGI generated pages (sign-up/modify)
$footer='/data1/hypermart.net/go2/redirect/footer.txt';

# The path to your sendmail (or equivalent) program. 
$mail_prog='/var/qmail/bin/qmail-inject'; 

# The reply address that will go out on the email
$emailadd='webmaster@go2.com.py';

# The text you want to appear at the top of the email
# (use "&&" for line breakes, this can be left blank)
$mailhead='Welcome to the Go2! forwarding service!&&&&';

# The text you want to appear at the bottom of the email
# (use "&&" for line breakes, this can be left blank)
$mailfoot='If you have any problems, email webmaster@go2.com.py.';

# The path to the datafile
$datafile='/data1/hypermart.net/go2/redirect/data.txt';

# The path to the directory the user's accounts are
# placed in
$redirect='/data1/hypermart.net/go2/redirect';

# The URL that the path above points to
$reurl='http://go2.hypermart.net/redirect';

# Your domain name. Should be the same as $reurl less
# the http:// and www. 
$domain='go2.hypermart.net';

# Allow users to put the ad on their page (yes/no)
$placeban='yes';

# Width of username box (adjust until it fills the line)
$width='11';

# Extention used to tell the server to parse for SSI
$ext='shtml';

# The path to the redirect script relative to the
# user's subdirectory
$rescript='../redirect.pl';

# Your redirection service's title
$title='Go2! - Redirection Service';

# The URL to this script
$script='/redirect/script.pl';

# The URL the user should be taken to after they have
# modified their account
$leave='http://www.go2cgi.com/';

# What is the page the URL above points to?
$whatis='home page';

# The path to the countries data file
$countries='/data1/hypermart.net/go2/redirect/countries.txt';

# If there is a problem with the countries data file
# this HTML is what the user will be told to put on
# his/her page is he/she opts to put the ad on his/her
# page
$html='<a href="domain.pl"><img src="banner.gif"></a>';

# Change nothing below this line
#########################################################

$mailhead=~s/&&/\n/g;
$mailfoot=~s/&&/\n/g;

if ($ENV{'QUERY_STRING'} =~ /do=step2/)
{
	&steptwo;
}
elsif($ENV{'QUERY_STRING'} =~ /do=forgotpass2/){
	&stepseven;
}
elsif($ENV{'QUERY_STRING'} =~ /do=links/){
	&stepeight;
}
elsif($ENV{'QUERY_STRING'} =~ /do=forgotpass/){
	&stepsix;
}
elsif ($ENV{'QUERY_STRING'} =~ /do=leave/)
{
	print "Location: $leave\n\n";
}
elsif ($ENV{'QUERY_STRING'} =~ /do=modify/)
{
	&stepthree;
}
elsif ($ENV{'QUERY_STRING'} =~ /do=step4/)
{
	&stepfour;
}
elsif ($ENV{'QUERY_STRING'} =~ /do=step5/)
{
	&stepfive;
}
elsif ($ENV{'QUERY_STRING'} =~ /do=step1/)
{
	&stepone;
}
else{
	&into;
}


sub into
{
	print "Content-type: text/html\n\n";
	print "<html><head><title>$title</title></head><body>\n";
	open(HEAD,"<$header");
	@top=<HEAD>;
	close(HEAD);
	print "@top\n";
	print "<form action=\"$script\" method=\"get\">\n";
	print "<P><CENTER>\n";
	print "<table border=1 cellpadding=1 cellspacing=0 bgcolor=white><tr><td bgcolor=blue>\n";
	print "<font face=arial color=white>$title</font>\n";
	print "</td></tr><tr><td>\n";
	print "<TABLE PADDING=5>\n";
	print "<TR><TD>\n";
	print "<table board=0><tr><td width=75>\n";
	print "<input type=hidden value=\"step1\" name=\"do\">\n";
	print "<p><center><input type=Submit value=\"Sign-up\"></center></p>\n";
	print "</td><td width=200>Sign-up for a new redirecting URL account</td></tr><tr><td></form><form action=\"$script\" method=\"get\">\n";
	print "<input type=hidden value=\"modify\" name=\"do\">\n";
	print "<p><center><input type=Submit value=\"Modify\"></center></p>\n";
	print "</td><td width=200>Modify your existing redirecting URL account</td></tr><tr><td></form><form action=\"$script\" method=\"get\">\n";
	print "<input type=hidden value=\"links\" name=\"do\">\n";
	print "<p><center><input type=Submit value=\"Links\"></center></p>\n";
	print "</td><td width=200>Index of links to our member's sites</td></tr><tr><td></form><form action=\"$script\" method=\"get\">\n";
	print "<input type=hidden value=\"leave\" name=\"do\">\n";
	print "<p><center><input type=Submit value=\"Exit\"></center></p>\n";
	print "</td><td width=200>Leave our redirection service and return to our $whatis</td></tr></table>\n";
	print "</td></tr></table>\n";
	print "</td></tr></table>\n";
	print "</form>\n";
	open(FOOT,"<$footer");
	@bottom=<FOOT>;
	close(FOOT);
	print "@bottom\n";
	print "</body></html>\n";
}
sub stepeight
{
 	@fval=split(/&/,$ENV{'QUERY_STRING'});
	foreach $i (0 .. $#fval){
		($name,$val)=split (/=/,$fval[$i],2);
		$val=~tr/+/ /;
		$val=~ s/%(..)/pack("c",hex($1))/ge;
		$name=~tr/+/ /;
		$name=~ s/%(..)/pack("c",hex($1))/ge;
		$field{$name}=$val;
	}
	$mx=$field{'linkmx'};
	$mn=$field{'linkmn'}-1;

	print "Content-type: text/html\n\n";
	print "<html><head><title>$title</title></head><body>\n";
	open(HEAD,"<$header");
	@top=<HEAD>;
	close(HEAD);
	print "@top\n";
	print "<form action=\"$script\" method=\"get\"><input type=hidden name=do value=links>\n";
	print "<P><CENTER>\n";
	print "<table border=1 cellpadding=1 cellspacing=0 bgcolor=white width=400><tr><td bgcolor=blue>\n";
	print "<font face=arial color=white>Member's Links:</font>\n";
	print "</td></tr><tr><td>\n";
	print "<TABLE PADDING=5 width=100%>\n";
	print "<TR><TD width=100%>\n";

	open(FILE,"<$datafile");
	@data=<FILE>;
	close(FILE);
	$nm=@data;
	if($mx==0){
		if($nm<10){
			$mx=$nm;
			$mn=0;
		}else{
			$mx=10;
		}
	}
	if($mn<0){
		$mn=0;
	}
	$pnm=$nm+1;
	$pmn=$mn+1;
	$pmx=$mx;
	print "<p><center>Currently viewing links <input type=text name=\"linkmn\" value=\"$pmn\" size=3> through <input type=text name=\"linkmx\" value=\"$pmx\" size=3> of $pnm &nbsp; <input type=submit value=\"Change\"></center></p>\n";

	@data=sort(@data);
	foreach $elink (@data){
		@mbr=split(/\|/,"$elink");
		if($elink ne "\n"){
			push(@res,"<p><ul><li><b><a href=\"$reurl/@mbr[0]\">@mbr[0]</a></b><br>@mbr[7]</ul></p>\n");
		}
	}
	for ($n=$mn; $n<$mx; $n++){
		print "@res[$n]";
	}

	print "</td></tr></table>\n";
	print "</td></tr></table>\n";
	print "</form>\n";
	open(FOOT,"<$footer");
	@bottom=<FOOT>;
	close(FOOT);
	print "@bottom\n";
	print "</body></html>\n";

}
sub stepseven
{
 	@fval=split(/&/,$ENV{'QUERY_STRING'});
	foreach $i (0 .. $#fval){
		($name,$val)=split (/=/,$fval[$i],2);
		$val=~tr/+/ /;
		$val=~ s/%(..)/pack("c",hex($1))/ge;
		$name=~tr/+/ /;
		$name=~ s/%(..)/pack("c",hex($1))/ge;
		$field{$name}=$val;
	}

	$message = "" ;
	$found_err = "" ;

	$errmsg = "<p>There is no account with the username \"$field{'username'}\".</p>";
	open(DATA,"<$datafile");
		@subs=<DATA>;
	close(DATA);

	$found_err = 1 ;
	foreach $sub(@subs){
		@subdata = split(/\|/,$sub);
		if(@subdata[0] eq $field{'username'}){
			$un=@subdata[0];
			$pass=@subdata[2];
			$em=@subdata[4];
			$found_err = 0;
		}
	}
	if($found_err==1){
		$message = $message.$errmsg ;
		$found_err = 1 ;
	}

	if ($found_err) {
		print "Content-type:text/html\n\n";
		print "<html><head><title>$title</title></head><body $bodytag>\n";
		open(HEAD,"<$header");
		@top=<HEAD>;
		close(HEAD);
		print "@top\n";
		print "<form action=\"$script\" method=\"get\"><input type=hidden name=do value=forgotpass>\n";
		print "<P><CENTER>\n";
		print "<table border=1 cellpadding=1 cellspacing=0 bgcolor=white><tr><td bgcolor=blue>\n";
		print "<font face=arial color=white>Forgot your password? - Error</font>\n";
		print "</td></tr><tr><td>\n";
		print "<TABLE PADDING=5>\n";
		print "<TR><TD>\n";

		print "$message\n";

		print "<p><center><input type=Submit value=\"<- Back\" onClick=go.history(-1);></center></p>\n";
		print "</td></tr></table>\n";
		print "</td></tr></table>\n";
		print "</form>\n";
		open(FOOT,"<$footer");
		@bottom=<FOOT>;
		close(FOOT);
		print "@bottom\n";
		print "</body></html>\n";
	} else {
	
		open (MAIL, "|$mail_prog -t");
			print MAIL "To: $em\n";
			print MAIL "Reply-to: $emailadd\n";
			print MAIL "From: $emailadd\n";
			print MAIL "Subject: $title\n";
			print MAIL "\n\n";
			print MAIL "$mailhead";
			print MAIL "The account \"$un\" signed-up with the password \"$pass\".\n";
			print MAIL "$mailfoot";
			print MAIL "\n\n";
		close (MAIL);
	
	print "Content-type:text/html\n\n";
	print "<html><head><title>$title</title></head><body $bodytag>\n";
	open(HEAD,"<$header");
	@top=<HEAD>;
	close(HEAD);
	print "@top\n";
	print "<form action=\"$script\" method=\"get\">\n";
	print "<P><CENTER>\n";
	print "<table border=1 cellpadding=1 cellspacing=0 bgcolor=white><tr><td bgcolor=blue>\n";
	print "<font face=arial color=white>Forgot your password? - Step 2 of 2</font>\n";
	print "</td></tr><tr><td>\n";
	print "<TABLE PADDING=5>\n";
	print "<TR><TD>\n";
	print "Your password has been emailed to $em.\n";
	print "<input type=hidden name=\"do\" value=\"modify\">\n";
	print "<p><center><input type=Submit value=\"Okay\"></center></p>\n";
	print "</td></tr></table>\n";
	print "</td></tr></table>\n";
	print "</form>\n";
	open(FOOT,"<$footer");
	@bottom=<FOOT>;
	close(FOOT);
	print "@bottom\n";
	print "</body></html>\n";

	}

}
sub stepsix
{

	print "Content-type: text/html\n\n";
	print "<html><head><title>$title</title></head><body>\n";
	open(HEAD,"<$header");
	@top=<HEAD>;
	close(HEAD);
	print "@top\n";
	print "<form action=\"$script\" method=\"get\">\n";
	print "<P><CENTER>\n";
	print "<table border=1 cellpadding=1 cellspacing=0 bgcolor=white><tr><td bgcolor=blue>\n";
	print "<font face=arial color=white>Forgot your Password? - Step 1 of 2</font>\n";
	print "</td></tr><tr><td>\n";
	print "<TABLE PADDING=5>\n";
	print "<TR><TD><input type=\"hidden\" name=\"do\" value=\"forgotpass2\">\n";

	print "<p><table border=0><tr><td>Username:</td><td>\n";
	print "<input type=\"text\" name=\"username\" width=30></td>\n";
	print "<tr><td colspan=2 align=right><input type=Submit value=\"Next...\"></td></tr></table></p>\n";

	print "</td></tr></table>\n";
	print "</td></tr></table>\n";
	print "</form>\n";
	open(FOOT,"<$footer");
	@bottom=<FOOT>;
	close(FOOT);
	print "@bottom\n";
	print "</body></html>\n";

}
sub stepfive
{

 	@fval=split(/&/,$ENV{'QUERY_STRING'});
	foreach $i (0 .. $#fval){
		($name,$val)=split (/=/,$fval[$i],2);
		$val=~tr/+/ /;
		$val=~ s/%(..)/pack("c",hex($1))/ge;
		$name=~tr/+/ /;
		$name=~ s/%(..)/pack("c",hex($1))/ge;
		$fild{$name}=$val;
	}


	$message = "" ;
	$found_err = "" ;

	$adt=$fild{'ad'};
	if($adt eq ""){
		$adt="adsoff";
	}

	$errmsg = "<p>You didn't tell us what your URL is. Please do so.</p>\n" ;

	if ($fild{'url'} eq "http://") {
		$message = $message.$errmsg ;
		$found_err = 1 ;
	}
	if ($fild{'url'} eq "") {
		$message = $message.$errmsg ;
		$found_err = 1 ;
	}

	$errmsg = "<p>You need a valid email address. Please enter one.</p>\n" ;

	if ($fild{'email'} !~ /.+\@.+\..+/) {
		$message = $message.$errmsg ;
		$found_err = 1 ; 
	}

	$errmsg = "<p>You didn't enter your name. Please tell us what it is.</p>\n" ;

	if ($fild{'name'} eq "") {
		$message = $message.$errmsg ;
		$found_err = 1 ;
	}

	$errmsg = "<p>You must enter a password, you'll need it to edit your account.</p>\n" ;

	if ($fild{'password'} eq "") {
		$message = $message.$errmsg ;
		$found_err = 1 ;
	}

	if ($found_err) {
		&PrintError;
	} else {

	print "Content-type: text/html\n\n";
	print "<html><head><title>$title</title></head><body>\n";
	open(HEAD,"<$header");
	@top=<HEAD>;
	close(HEAD);
	print "@top\n";
	print "<form action=\"$script\" method=\"get\">\n";
	print "<input type=hidden value=\"leave\" name=\"do\">\n";
	print "<P><CENTER>\n";
	print "<table border=1 cellpadding=1 cellspacing=0 bgcolor=white><tr><td bgcolor=blue>\n";
	print "<font face=arial color=white>Modify - Step 3 of 3</font>\n";
	print "</td></tr><tr><td>\n";
	print "<TABLE PADDING=5>\n";
	print "<TR><TD>\n";

	open(INDEX,">$redirect/$fild{'username'}/index.$ext");
		if($fild{'ad'} eq "popup"){
			print INDEX "<script language=\"JavaScript\"><!--\n";
			print INDEX "<!--#include virtual=\"$rescript?ad=popup&ct=$fild{'cnty'}\"-->\n";
			print INDEX "//--></script>\n\n";
			print INDEX "<html><head><title>$fild{'wtitle'}</title><meta description=\"$fild{'desc'}\"><meta keyword=\"$fild{'keywords'}\"></head>\n";
			print INDEX "<frameset rows=\"100%,*\" FRAMESPACING=0 FRAMEBORDER=0 BORDER=0>\n";
			print INDEX "<frame src=\"$fild{'url'}\" NAME=\"MAIN_FRAME\" SCROLLING=\"AUTO\" NORESIZE FRAMEBORDER=0 BORDER=0 MARGINHEIGHT=3 MARGINWIDTH=3>\n";
			print INDEX "</frameset></html>\n\n";
		}elsif($fild{'ad'} eq "delay"){
			print INDEX "<html><head><title>$fild{'wtitle'}</title><meta description=\"$fild{'desc'}\"><meta keyword=\"$fild{'keywords'}\"><meta http-Equiv=\"refresh\" content=\"8; url=$fild{'url'}\"></head><body>\n";
			print INDEX "<table border=0 width=100% height=95%><tr><td valign=middle><!--#include virtual=\"$rescript?ad=delay&ct=$fild{'cnty'}\"-->\n";
			print INDEX "<p><center>Please wait while you're taken to <a href=\"$fild{'u'}\">$fild{'wtitle'}</a>.</center></p>\n";
			print INDEX "</td></tr></table></body></html>\n\n";
		}elsif($fild{'ad'} eq "frame"){

			print INDEX "<html><head><title>$fild{'wtitle'}</title><meta description=\"$fild{'desc'}\"><meta keyword=\"$fild{'keywords'}\"></head>\n";
			print INDEX "<frameset rows=\"62,*\" FRAMESPACING=0 FRAMEBORDER=0 BORDER=0>\n";
			print INDEX "<frame src=\"<!--#include virtual=\"$rescript?ad=frame&ct=$fild{'cnty'}\"-->\" NAME=\"AD_FRAME\" SCROLLING=\"NO\" NORESIZE FRAMEBORDER=0 BORDER=0 MARGINHEIGHT=0 MARGINWIDTH=0>\n";
			print INDEX "<frame src=\"$fild{'url'}\" NAME=\"MAIN_FRAME\" SCROLLING=\"AUTO\" NORESIZE FRAMEBORDER=0 BORDER=0 MARGINHEIGHT=3 MARGINWIDTH=3>\n";
			print INDEX "</frameset></html>\n\n";
		}else{
			print INDEX "<html><head><title>$fild{'wtitle'}</title><meta description=\"$fild{'desc'}\"><meta keyword=\"$fild{'keywords'}\"></head>\n";
			print INDEX "<frameset rows=\"100%,*\" FRAMESPACING=0 FRAMEBORDER=0 BORDER=0>\n";
			print INDEX "<frame src=\"$fild{'url'}\" NAME=\"MAIN_FRAME\" SCROLLING=\"AUTO\" NORESIZE FRAMEBORDER=0 BORDER=0 MARGINHEIGHT=3 MARGINWIDTH=3>\n";
			print INDEX "</frameset></html>\n\n";
		}
	close(INDEX);
	chmod("$redirect/$fild{'username'}/index.$ext",0777);

	open(DATA,"<$datafile");
		@subs=<DATA>;
	close(DATA);
	$i=-1;
	foreach $sub(@subs){
		$i=$i+1;
		@subdata = split(/\|/,$sub);
		if(@subdata[0] eq $fild{'username'}){
			@subs[$i]="$fild{'username'}|$fild{'url'}|$fild{'password'}|$adt|$fild{'email'}|$fild{'name'}|$fild{'wtitle'}|$fild{'desc'}|$fild{'keywords'}|$fild{'cnty'}\n";
		}	
	}
	$ii=-1;
	open(DATA,">$datafile");
	while($ii<=$i+1){
		$ii=$ii+1;
		$temp="@subs[$ii]";
		print DATA "$temp";
	}
	close(DATA);

	print "<tableborder=0 cellpadding=5><tr><td><center>Your account has been updated<br></center>\n";

	if($fild{'ad'} eq 'onpage'){
		open (FILE,"<$countries");
			@cntes=<FILE>;
		close(FILE);
		foreach $cnty (@cntes){
			($c,$p,$d,$h)=split(/\|/,$cnty);
			if($fild{'cnty'} eq $c){
				$html=$h;
			}
		}
		$html =~ s/\&/\&amp\;/g;
		$html =~ s/"/\&quot\;/g;
		$html =~ s/</\&lt\;/g;
		$html =~ s/>/\&gt\;/g;
		print "<p>Since you wanted to put the ad on your page you'll need to copy this code on to it.</p>\n";
		print "<p><blockquote><tt>$html</tt></blockquote></p>\n";
	}

	print "<br><center><input type=Submit value=\"Okay\"></center></td></tr></table>\n";

	print "</td></tr></table>\n";
	print "</td></tr></table>\n";
	print "</form>\n";
	open(FOOT,"<$footer");
	@bottom=<FOOT>;
	close(FOOT);
	print "@bottom\n";
	print "</body></html>\n";
	}

}
sub stepfour
{
 	@fval=split(/&/,$ENV{'QUERY_STRING'});
	foreach $i (0 .. $#fval){
		($name,$val)=split (/=/,$fval[$i],2);
		$val=~tr/+/ /;
		$val=~ s/%(..)/pack("c",hex($1))/ge;
		$name=~tr/+/ /;
		$name=~ s/%(..)/pack("c",hex($1))/ge;
		$fild{$name}=$val;
	}


	$message = "" ;
	$found_err = "" ;

	$errmsg = "<p>Wrong username/password combination. Try again.</p>\n" ;

	$errtype='Modify';
	$errscript="modify";

	if ($fild{'username'} eq "") {
		$message = $message.$errmsg ;
		$found_err = 1 ;
	}
	if ($fild{'password'} eq "") {
		$message = $message.$errmsg ;
		$found_err = 1 ;
	}
	open(DATA,"<$datafile");
		@subs=<DATA>;
	close(DATA);

	$found_err = 1 ;
	foreach $sub(@subs){
		@subdata = split(/\|/,$sub);
		if(@subdata[0] eq $fild{'username'}){
			if(@subdata[2] ne $fild{'password'}){
				$message = $message.$errmsg ;
			}else{
				$un=@subdata[0];
				$url=@subdata[1];
				$pass=@subdata[2];
				$ad=@subdata[3];
				$em=@subdata[4];
				$nm=@subdata[5];
				$tl=@subdata[6];
				$de=@subdata[7];
				$ke=@subdata[8];
				$ct=@subdata[9];
				$found_err = 0;
			}
		}	
	}
	if ($found_err) {
		if($message eq ''){
			$message=$errmsg;
		}	
		&PrintError;
	} else {
	print "Content-type: text/html\n\n";
	print "<html><head><title>$title</title></head><body>\n";
	open(HEAD,"<$header");
	@top=<HEAD>;
	close(HEAD);
	print "@top\n";
	print "<form action=\"$script\" method=\"get\">\n";
	print "<input type=hidden value=\"step5\" name=\"do\">\n";
	print "<P><CENTER>\n";
	print "<table border=1 cellpadding=1 cellspacing=0 bgcolor=white><tr><td bgcolor=blue>\n";
	print "<font face=arial color=white>Modify - Step 2 of 3</font>\n";
	print "</td></tr><tr><td>\n";
	print "<TABLE PADDING=5>\n";
	print "<TR><TD>\n";
	print "<p>Here's what you signed-up with. You can edit anything you wish<br>except for your username. If you want to change it sign-up for a<br>new account.</p>\n";

	print "<P><TABLE BORDER=0>\n";
	print "   <TR>\n";
	print "      <TD>\n";
	print "         <P>Username:\n";
	print "      </TD><TD>\n";
	print "         <P>http://$domain/$un<input type=hidden value=\"$un\" name=\"username\">\n";
	print "      </TD></TR>\n";
	print "   <TR>\n";
	print "      <TD>\n";
	print "         <P>Your Current URL:\n";
	print "      </TD><TD>\n";
	print "         <P><INPUT TYPE=\"text\" NAME=\"url\" VALUE=\"$url\" SIZE=30>\n";
	print "      </TD></TR>\n";
	print "   <TR>\n";
	print "      <TD>\n";
	print "         <P>Your Email Address:\n";
	print "      </TD><TD>\n";
	print "         <P><INPUT TYPE=\"text\" NAME=\"email\" VALUE=\"$em\" SIZE=30>\n";
	print "      </TD></TR>\n";
	print "   <TR>\n";
	print "      <TD>\n";
	print "         <P>Your Name:\n";
	print "      </TD><TD>\n";
	print "         <P><INPUT TYPE=\"text\" NAME=\"name\" VALUE=\"$nm\" SIZE=30>\n";
	print "      </TD></TR>\n";
	print "   <TR>\n";
	print "      <TD>\n";
	print "         <P>Website Title:\n";
	print "      </TD><TD>\n";
	print "         <P><INPUT TYPE=\"text\" NAME=\"wtitle\" VALUE=\"$tl\" SIZE=30>\n";
	print "      </TD></TR>\n";
	print "   <TR>\n";
	print "      <TD>\n";
	print "        <P>Your Desired Password:\n";
	print "      </TD><TD>\n";
	print "         <P><INPUT TYPE=\"text\" NAME=\"password\" VALUE=\"$pass\" SIZE=30>\n";
	print "      </TD></TR>\n";
	print "   <TR>\n";
	print "      <TD>\n";
	print "        <P>Website Description:\n";
	print "      </TD><TD>\n";
	print "         <P><INPUT TYPE=\"text\" NAME=\"desc\" VALUE=\"$de\" SIZE=30>\n";
	print "      </TD></TR>\n";
	print "   <TR>\n";
	print "      <TD>\n";
	print "        <P>Website Keywords:\n";
	print "      </TD><TD>\n";
	print "         <P><INPUT TYPE=\"text\" NAME=\"keywords\" VALUE=\"$ke\" SIZE=30>\n";
	print "      </TD></TR>\n";
	print "   <TR>\n";
	print "      <TD>\n";
	print "        <P>Your Country:\n";
	print "      </TD><TD>\n";
	chop($ct);
	print "         <P><SELECT NAME=\"cnty\">\n";
	print "            <OPTION VALUE=\"other\">------------------------------------------------------\n";
	open (FILE,"<$countries");
		@cntes=<FILE>;
	close(FILE);
	foreach $cnty (@cntes){
		($c,$p,$d,$h)=split(/\|/,$cnty);
		print "            <OPTION VALUE=\"$c\"";
		if($c eq $ct){
			print " SELECTED ";
		}
		print ">$c\n";
	}
	print "         </SELECT>\n";
	print "      </TD></TR>\n";
	print "   <TR>\n";
	print "      <TD>\n";
	print "        <P>Ad Preference:\n";
	print "      </TD><TD>\n";
	if($ad eq 'adsoff'){
		print "         <P><INPUT TYPE=\"radio\" NAME=\"ad\" VALUE=\"adsoff\" CHECKED>No Ads";
	}else{
		if($ad eq 'popup'){
			print "         <P><INPUT TYPE=\"radio\" NAME=\"ad\" VALUE=\"popup\" CHECKED>Ad in a popup";
		}else{
			print "         <P><INPUT TYPE=\"radio\" NAME=\"ad\" VALUE=\"popup\">Ad in a popup";
		}
		if($ad eq 'frame'){
			print "         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<INPUT TYPE=\"radio\" NAME=\"ad\" VALUE=\"frame\" CHECKED>Ad in ad-frame\n";
		}else{
			print "         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<INPUT TYPE=\"radio\" NAME=\"ad\" VALUE=\"frame\">Ad in ad-frame\n";
		}
		if($ad eq 'delay'){
			print "         <BR><INPUT TYPE=\"radio\" NAME=\"ad\" VALUE=\"delay\" CHECKED>Ad on delay page\n";
		}else{
			print "         <BR><INPUT TYPE=\"radio\" NAME=\"ad\" VALUE=\"delay\">Ad on delay page\n";
		}
		if($placeban eq "yes"){
			if($ad eq 'onpage'){
				print "         <INPUT TYPE=\"radio\" NAME=\"ad\" VALUE=\"onpage\" CHECKED>Ad on your page\n";
			}else{
				print "         <INPUT TYPE=\"radio\" NAME=\"ad\" VALUE=\"onpage\">Ad on your page\n";
			}
		}
	}
	print "      </TD></TR>\n";
	print "   <TR>\n";
	print "      <TD COLSPAN=2 ALIGN=RIGHT>\n";
	print "         <INPUT TYPE=\"Submit\" VALUE=\"Next...\">\n";
	print "      </TD></TR>\n";
	print "</TABLE>\n";
	
	print "</td></tr></table>\n";
	print "</td></tr></table>\n";
	print "</form>\n";
	open(FOOT,"<$footer");
	@bottom=<FOOT>;
	close(FOOT);
	print "@bottom\n";
	print "</body></html>\n";
	}
}
sub stepthree
{
	print "Content-type: text/html\n\n";
	print "<html><head><title>$title</title></head><body>\n";
	open(HEAD,"<$header");
	@top=<HEAD>;
	close(HEAD);
	print "@top\n";
	print "<form action=\"$script\" method=\"get\">\n";
	print "<input type=hidden value=\"step4\" name=\"do\">\n";
	print "<P><CENTER>\n";
	print "<table border=1 cellpadding=1 cellspacing=0 bgcolor=white><tr><td bgcolor=blue>\n";
	print "<font face=arial color=white>Modify - Step 1 of 3</font>\n";
	print "</td></tr><tr><td>\n";
	print "<TABLE PADDING=5>\n";
	print "<TR><TD>\n";
	
	print "<table border=0><tr><td>\n";
	print "Username:</td><td><input type=text size=30 name=username></td></tr><tr><td valign=top>\n";
	print "Password:</td><td valign=top><input type=password size=30 name=password>\n";
	print "</td></tr><tr><td colspan=2 align=\"right\"><p><font size=-2><a href=\"$script?do=forgotpass\">Forgot Your Password?</a></font>&nbsp;<input type=Submit value=\"Next...\"></p></td></tr></table>\n";
	
	print "</td></tr></table>\n";
	print "</td></tr></table>\n";
	print "</form>\n";
	open(FOOT,"<$footer");
	@bottom=<FOOT>;
	close(FOOT);
	print "@bottom\n";
	print "</body></html>\n";
}

sub steptwo
{

 	@fval=split(/&/,$ENV{'QUERY_STRING'});
	foreach $i (0 .. $#fval){
		($name,$val)=split (/=/,$fval[$i],2);
		$val=~tr/+/ /;
		$val=~ s/%(..)/pack("c",hex($1))/ge;
		$name=~tr/+/ /;
		$name=~ s/%(..)/pack("c",hex($1))/ge;
		$fild{$name}=$val;
	}


	$message = "" ;
	$found_err = "" ;

	$errmsg = "<p>You have not chosen a username. Please pick one.</p>\n" ;

	if ($fild{'username'} eq "") {
		$message = $message.$errmsg ;
		$found_err = 1 ;
	}

	$errmsg = "<p>You didn't tell us what your URL is. Please do so.</p>\n" ;

	if ($fild{'url'} eq "http://") {
		$message = $message.$errmsg ;
		$found_err = 1 ;
	}
	if ($fild{'url'} eq "") {
		$message = $message.$errmsg ;
		$found_err = 1 ;
	}

	$errmsg = "<p>You need a valid email address. Please enter one.</p>\n" ;

	if ($fild{'email'} !~ /.+\@.+\..+/) {
		$message = $message.$errmsg ;
		$found_err = 1 ; 
	}

	$errmsg = "<p>You didn't enter your name. Please tell us what it is.</p>\n" ;

	if ($fild{'name'} eq "") {
		$message = $message.$errmsg ;
		$found_err = 1 ;
	}

	$errmsg = "<p>You must enter a password, you'll need it to edit your account.</p>\n" ;

	if ($fild{'password'} eq "") {
		$message = $message.$errmsg ;
		$found_err = 1 ;
	}

	$errmsg = "<p>Sorry, that username is taken. Pick another.</p>\n" ;

	open(DATA,"<$datafile");
		@subs=<DATA>;
	close(DATA);

	foreach $sub(@subs){
		@subdata = split(/\|/,$sub);
		if(@subdata[0] eq $fild{'username'}){
			$message = $message.$errmsg ;
			$found_err = 1 ;
		}	
	}

	$errtype='Sign-up';
	$errscript="step1";

	if ($found_err) {
		&PrintError;
	} else {

	mkdir("$redirect/$fild{'username'}", 0777);

	open(INDEX,">$redirect/$fild{'username'}/index.$ext");
		if($fild{'ad'} eq "popup"){
			print INDEX "<script language=\"JavaScript\"><!--\n";
			print INDEX "<!--#include virtual=\"$rescript?ad=popup&ct=$fild{'cnty'}\"-->\n";
			print INDEX "//--></script>\n\n";
			print INDEX "<html><head><title>$fild{'wtitle'}</title><meta description=\"$fild{'desc'}\"><meta keyword=\"$fild{'keywords'}\"></head>\n";
			print INDEX "<frameset rows=\"100%,*\" FRAMESPACING=0 FRAMEBORDER=0 BORDER=0>\n";
			print INDEX "<frame src=\"$fild{'url'}\" NAME=\"MAIN_FRAME\" SCROLLING=\"AUTO\" NORESIZE FRAMEBORDER=0 BORDER=0 MARGINHEIGHT=3 MARGINWIDTH=3>\n";
			print INDEX "</frameset></html>\n\n";
		}elsif($fild{'ad'} eq "delay"){
			print INDEX "<html><head><title>$fild{'wtitle'}</title><meta description=\"$fild{'desc'}\"><meta keyword=\"$fild{'keywords'}\"><meta http-Equiv=\"refresh\" content=\"8; url=$fild{'url'}\"></head><body>\n";
			print INDEX "<table border=0 width=100% height=95%><tr><td valign=middle><!--#include virtual=\"$rescript?ad=delay&ct=$fild{'cnty'}\"-->\n";
			print INDEX "<p><center>Please wait while you're taken to <a href=\"$fild{'u'}\">$fild{'wtitle'}</a>.</center></p>\n";
			print INDEX "</td></tr></table></body></html>\n\n";
		}elsif($fild{'ad'} eq "frame"){
			print INDEX "<html><head><title>$fild{'wtitle'}</title><meta description=\"$fild{'desc'}\"><meta keyword=\"$fild{'keywords'}\"></head>\n";
			print INDEX "<frameset rows=\"62,*\" FRAMESPACING=0 FRAMEBORDER=0 BORDER=0>\n";
			print INDEX "<frame src=\"<!--#include virtual=\"$rescript?ad=frame&ct=$fild{'cnty'}\"-->\" NAME=\"AD_FRAME\" SCROLLING=\"NO\" NORESIZE FRAMEBORDER=0 BORDER=0 MARGINHEIGHT=0 MARGINWIDTH=0>\n";
			print INDEX "<frame src=\"$fild{'url'}\" NAME=\"MAIN_FRAME\" SCROLLING=\"AUTO\" NORESIZE FRAMEBORDER=0 BORDER=0 MARGINHEIGHT=3 MARGINWIDTH=3>\n";
			print INDEX "</frameset></html>\n\n";
		}else{
			print INDEX "<html><head><title>$fild{'wtitle'}</title><meta description=\"$fild{'desc'}\"><meta keyword=\"$fild{'keywords'}\"></head>\n";
			print INDEX "<frameset rows=\"100%,*\" FRAMESPACING=0 FRAMEBORDER=0 BORDER=0>\n";
			print INDEX "<frame src=\"$fild{'url'}\" NAME=\"MAIN_FRAME\" SCROLLING=\"AUTO\" NORESIZE FRAMEBORDER=0 BORDER=0 MARGINHEIGHT=3 MARGINWIDTH=3>\n";
			print INDEX "</frameset></html>\n\n";
		}
	close(INDEX);
	chmod("$redirect/$fild{'username'}/index.$ext",0777);

	open(DATA,">>$datafile");
		print DATA "$fild{'username'}|$fild{'url'}|$fild{'password'}|$fild{'ad'}|$fild{'email'}|$fild{'name'}|$fild{'wtitle'}|$fild{'desc'}|$fild{'keywords'}|$fild{'cnty'}\n";
	close(DATA);

	print "Content-type: text/html\n\n";
	print "<html><head><title>$title</title></head><body>\n";
	open(HEAD,"<$header");
	@top=<HEAD>;
	close(HEAD);
	print "@top\n";

	print "<form action=\"$script\" method=\"get\">\n";
	print "<input type=hidden value=\"modify\" name=\"do\">\n";
	print "<P><CENTER>\n";
	print "<table border=1 cellpadding=1 cellspacing=0 bgcolor=white width=90%><tr><td bgcolor=blue>\n";
	print "<font face=arial color=white>Sign-up - Step 2 of 2</font>\n";
	print "</td></tr><tr><td>\n";
	print "<TABLE PADDING=5>\n";
	print "<TR><TD>\n";

	open (MAIL, "|$mail_prog -t");
		print MAIL "To: $fild{'email'}\n";
		print MAIL "Reply-to: $emailadd\n";
		print MAIL "From: $emailadd\n";
		print MAIL "Subject: $title\n";
		print MAIL "\n\n";
		print MAIL "Hello $fild{'name'},\n\n";
		print MAIL "$mailhead";
		print MAIL "Your new URL is http://$usrnm.$domain/, it forwards to $fild{'url'}. Your password is \"$fild{'password'}\", you can use it to modify your account with us.\n\n";


	print "<p><center><b><tt><a href=\"$reurl/$fild{'username'}\" target=\"_new\">$reurl/$fild{'username'}</a> created!</tt></b></center></p>\n";

	print "<p>Your Website should now be accessible from your new redirecting URL. If it isn't perhaps you misstyped your URL when signing-up. Don't worry, you can change your setup by clicking the button below.</p>\n";
	if($fild{'ad'} eq 'onpage'){
		open (FILE,"<$countries");
			@cntes=<FILE>;
		close(FILE);
		foreach $cnty (@cntes){
			($c,$p,$d,$h)=split(/\|/,$cnty);
			if($fild{'cnty'} eq $c){
				$html=$h;
			}
		}
		$mhtml=$html;
		$html =~ s/\&/\&amp\;/g;
		$html =~ s/"/\&quot\;/g;
		$html =~ s/</\&lt\;/g;
		$html =~ s/>/\&gt\;/g;
			print MAIL "Since you wanted to put the ad on your page you'll need to copy this code on to it:\n\n";
			print MAIL "$mhtml\n\n";
		print "<p>Since you wanted to put the ad on your page you'll need to copy this code on to it. If you've changed your mind, click the button below to change your setup.</p>\n";
		print "<blockquote><p><tt>$html</tt></p></blockquote>\n";
	}
		print MAIL "$mailfoot";
	close (MAIL);
	print "<p><center><input type=Submit value=\"Change your Setup\"></center></p>\n";

	print "</td></tr></table>\n";
	print "</td></tr></table>\n";
	print "</form>\n";
	open(FOOT,"<$footer");
	@bottom=<FOOT>;
	close(FOOT);
	print "@bottom\n";
	print "</body></html>\n";
	}

}

sub stepone
{

	print "Content-type: text/html\n\n";
	print "<html><head><title>$title</title></head><body>\n";
	open(HEAD,"<$header");
	@top=<HEAD>;
	close(HEAD);
	print "@top\n";
	print "<form action=\"$script\" method=\"get\">\n";
	print "<input type=hidden value=\"step2\" name=\"do\">\n";
	print "<P><CENTER>\n";
	print "<table border=1 cellpadding=1 cellspacing=0 bgcolor=white><tr><td bgcolor=blue>\n";
	print "<font face=arial color=white>Sign-up - Step 1 of 2</font>\n";
	print "</td></tr><tr><td>\n";
	print "<TABLE PADDING=5>\n";
	print "<TR><TD>\n";
	
	print "<P><TABLE BORDER=0>\n";
	print "   <TR>\n";
	print "      <TD>\n";
	print "         <P>Username:\n";
	print "      </TD><TD>\n";
	print "         <P>http://$domain/<INPUT TYPE=\"text\"\n";
	print "         NAME=\"username\" VALUE=\"\" SIZE=$width>\n";
	print "      </TD></TR>\n";
	print "   <TR>\n";
	print "      <TD>\n";
	print "         <P>Your Current URL:\n";
	print "      </TD><TD>\n";
	print "         <P><INPUT TYPE=\"text\" NAME=\"url\" VALUE=\"http://\" SIZE=30>\n";
	print "      </TD></TR>\n";
	print "   <TR>\n";
	print "      <TD>\n";
	print "         <P>Your Email Address:\n";
	print "      </TD><TD>\n";
	print "         <P><INPUT TYPE=\"text\" NAME=\"email\" VALUE=\"\" SIZE=30>\n";
	print "      </TD></TR>\n";
	print "   <TR>\n";
	print "      <TD>\n";
	print "         <P>Your Name:\n";
	print "      </TD><TD>\n";
	print "         <P><INPUT TYPE=\"text\" NAME=\"name\" VALUE=\"\" SIZE=30>\n";
	print "      </TD></TR>\n";
	print "   <TR>\n";
	print "      <TD>\n";
	print "         <P>Website Title:\n";
	print "      </TD><TD>\n";
	print "         <P><INPUT TYPE=\"text\" NAME=\"wtitle\" VALUE=\"\" SIZE=30>\n";
	print "      </TD></TR>\n";
	print "   <TR>\n";
	print "      <TD>\n";
	print "        <P>Your Desired Password:\n";
	print "      </TD><TD>\n";
	print "         <P><INPUT TYPE=\"text\" NAME=\"password\" VALUE=\"\" SIZE=30>\n";
	print "      </TD></TR>\n";
	print "   <TR>\n";
	print "      <TD>\n";
	print "        <P>Website Description:\n";
	print "      </TD><TD>\n";
	print "         <P><INPUT TYPE=\"text\" NAME=\"desc\" VALUE=\"\" SIZE=30>\n";
	print "      </TD></TR>\n";
	print "   <TR>\n";
	print "      <TD>\n";
	print "        <P>Website Keywords:\n";
	print "      </TD><TD>\n";
	print "         <P><INPUT TYPE=\"text\" NAME=\"keywords\" VALUE=\"\" SIZE=30>\n";
	print "      </TD></TR>\n";
	print "   <TR>\n";
	print "      <TD>\n";
	print "        <P>Your Country:\n";
	print "      </TD><TD>\n";
	print "         <P><SELECT NAME=\"cnty\">\n";
	print "            <OPTION VALUE=\"other\">------------------------------------------------------\n";
	open (FILE,"<$countries");
		@cntes=<FILE>;
	close(FILE);
	foreach $cnty (@cntes){
		($c,$p,$d,$h)=split(/\|/,$cnty);
		print "            <OPTION VALUE=\"$c\">$c\n";
	}
	print "         </SELECT>\n";
	print "      </TD></TR>\n";
	print "   <TR>\n";
	print "      <TD>\n";
	print "        <P>Ad Preference:\n";
	print "      </TD><TD>\n";
	print "         <P><INPUT TYPE=\"radio\" NAME=\"ad\" VALUE=\"popup\" CHECKED>Ad in a popup&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<INPUT TYPE=\"radio\" NAME=\"ad\" VALUE=\"frame\">Ad in ad-frame\n";
	print "         <BR><INPUT TYPE=\"radio\" NAME=\"ad\" VALUE=\"delay\">Ad on delay page";
	if($placeban eq "yes"){
		print "         <INPUT TYPE=\"radio\" NAME=\"ad\" VALUE=\"onpage\">Ad on your page\n";
	}
	print "      </TD></TR>\n";
	print "   <TR>\n";
	print "      <TD COLSPAN=2 ALIGN=RIGHT>\n";
	print "         <INPUT TYPE=\"Submit\" VALUE=\"Next...\">\n";
	print "      </TD></TR>\n";
	print "</TABLE>\n";

	print "</td></tr></table>\n";
	print "</td></tr></table>\n";
	print "</form>\n";
	open(FOOT,"<$footer");
	@bottom=<FOOT>;
	close(FOOT);
	print "@bottom\n";
	print "</body></html>\n";
}

sub PrintError { 

print "Content-type: text/html\n\n";
print "<html><head><title>$title</title></head><body>\n";

open(HEAD,"<$header");
@top=<HEAD>;
close(HEAD);
print "@top\n";

print "<form action=\"$script\" method=\"get\">\n";
print "<input type=hidden name=\"do\" value=\"$errscript\">\n";
print "<p><center><table border=1 cellpadding=1 cellspacing=0 bgcolor=white><tr><td bgcolor=blue>\n";
print "<font face=arial color=white>$errtype - Error</font>\n";
print "</td></tr><tr><td>\n";
print "<TABLE PADDING=5>\n";
print "<TR><TD>\n";

print $message ;

print "<p><center><input type=Submit value=\"<- Back\" onClick=history.go(-1)></center></p>\n";

print "</td></tr></table></td></tr></table></center></p>\n";

print "</form>\n";
open(FOOT,"<$footer");
@bottom=<FOOT>;
close(FOOT);
print "@bottom\n";
print "</body></html>\n";

exit 0 ;
return 1 ; 

}

sub GetFormInput {

	(*fval) = @_ if @_ ;

	local ($buf);
	if ($ENV{'REQUEST_METHOD'} eq 'POST') {
		read(STDIN,$buf,$ENV{'CONTENT_LENGTH'});
	}
	else {
		$buf=$ENV{'QUERY_STRING'};
	}
	if ($buf eq "") {
			return 0 ;
		}
	else {
 		@fval=split(/&/,$buf);
		foreach $i (0 .. $#fval){
			($name,$val)=split (/=/,$fval[$i],2);
			$val=~tr/+/ /;
			$val=~ s/%(..)/pack("c",hex($1))/ge;
			$name=~tr/+/ /;
			$name=~ s/%(..)/pack("c",hex($1))/ge;

			if (!defined($field{$name})) {
				$field{$name}=$val;
			}

		   }
		}
return 1;
}