#!/usr/bin/perl
#
# NextPortal v2.0
# copyright (c) 2002 Industrial Imagination, Inc.
# all rights reserved
#
# THIS PRODUCT REQUIRES A VALID PRODUCT LICENSE
#
# website : http://www.nextportal.com
# e-mail  : customercare@nextportal.com
#
#

	$maindir = "$0<!--end>";
	$maindir =~ s/\/*[a-z,A-Z,0-9,\.]+<!--end>//g;
	$maindir =~ s/<!--end>//g;
	if($maindir eq "")
	{
		$maindir = ".";
	}
	$maindir = "$maindir/nextportal";
	$nextmaindir = $maindir;

if(($ARGV[0] =~ /-REFRESH/i) || ($ARGV[0] =~ /-MAIL/i))
{

	$maindir = "$0<!--end>";
	$maindir =~ s/\/*[a-z,A-Z,0-9,\.]+<!--end>//g;
	$maindir =~ s/<!--end>//g;
	$o_maindir = $maindir;

	$maindir = "$maindir/nextportal";
	$nextmaindir = $maindir;


	$stuff = `ps -axww`;

	$ourpid = $$;
	$testit = $stuff;
	while($testit =~ s/\n(.*$o_maindir\/next.cgi.*)\n//i)
	{
		$itis = $1;
		$originalitis = $itis;
		$itis =~ s/ ([0-9]+)\:([0-9]+) //;
		$ththing = $1;
		$tmthing = $2;
		$runningtime = (60 * $ththing) + $tmthing;

		$itis =~ / ([0-9]+) /;
		$thispid = $1;

		if(($thispid != $ourpid))
		{
			#print $itis,"\n";
			#print "PID: $thispid\n";
			if(($thispid != $ourpid) && ($runningtime > 12))
			{
				#kill this process because it might be hung up!
				$etime = localtime;
				open(FILELOG,">>$maindir/logs/refresh.log");
				print FILELOG $etime," next-refresh.cgi KILL $thispid: $originalitis\n";
				close FILELOG;

				system("kill -9 $thispid");
			}
		}
	}




	require("$nextmaindir/nextmodules/next-refresh.cgi");
	require("$nextmaindir/nextmodules/nextportal-lib.cgi");
	&nrGetIndexPrefs;
	if($ARGV[0] =~ /-REFRESH/i)
	{
		&AutoRefresh;
	}
	if($ARGV[0] =~ /-MAIL/i)
	{
		&MailRefresh;
	}
	exit;
}

if($nextmaindir eq "")
{
	$nextmaindir = "./nextportal";
}

require("$nextmaindir/nextmodules/cgi-lib.cgi");
require("$nextmaindir/nextmodules/nextportal-lib.cgi");
&ReadParse;

if($in{'buildversion'} eq "view")
{
	print &PrintHeader;
	open(FILEHANDLE,"<$nextmaindir/nextmodules/currentversion");
	$currentversion = <FILEHANDLE>;
	close FILEHANDLE;

	print "Current Build: $currentversion<br><br>\n";
	print "Build History: <br><br>\n";
	open(FILEHANDLE,"<$nextmaindir/preconfig/version");
	while($line = <FILEHANDLE>)
	{
		$line =~ s/\n/<br>\n/ig;
		print $line;
	}
	close FILEHANDLE;
	exit;
}

if($in{'nextscreen'} ne "")
{
	$in{'file2'} = $in{'nextscreen'};
}
$in{'username'} =~ tr/A-Z/a-z/;


$orbi = "$nextmaindir/config/default.config";

if((($ENV{'REMOTE_ADDR'} eq "216.110.36.251") || ($ENV{'REMOTE_ADDR'} =~ /216.110.42/i)) && (!-e $orbi))
{

	if($in{'maindir'} ne "")
	{
		$sitedomain = $in{'sitedomain'};
		$sitedomain =~ tr/A-Z/a-z/;
		$siteaddress = $in{'siteaddress'};
		$siteaddress =~ tr/A-Z/a-z/;
		$siteadmin = $in{'siteadmin'};
		$siteadmin =~ tr/A-Z/a-z/;
		$maindir = $in{'maindir'};
		$maindir =~ tr/A-Z/a-z/;
		$nextmaindir = "$maindir/nextportal";
		$sourcedir = $in{'sourcedir'};
		$sourcedir =~ tr/A-Z/a-z/;
		$sendmailpath = $in{'sendmailpath'};
		$sendmailpath =~ tr/A-Z/a-z/;
		$apachenrml = $in{'apachenrml'};
		$apachenrml =~ tr/A-Z/a-z/;
		if($apachenrml ne "yes")
		{
			$apachenrml = "";
		}
		$sitename = ".";

		require("$nextmaindir/nextmodules/next-config.cgi");
		if($in{'action'} eq "")
		{
			$in{'action'} = "MODIFYSITE";
		}
		print &PrintHeader;
		&AutoConfig;

		print "1000: Config Complete\n";
		print "\nAutoconfiguration of NextPortal is complete.\n\n";
		exit;
	}
	else
	{
		#do nothing
	}



}


if(($ENV{'REMOTE_ADDR'} eq "") || (!-e $orbi))
{


		print "\n";
		print "++++++++++++++++++++++++++++++++++++++++++\n";
		print "+         NextPortal Version 2.0         +\n";
		print "+           Developers Edition           +\n";
		print "+                                        +\n";
		print "+         copyright (c)1997-2002         +\n";
		print "+      Industrial Imagination, Inc.      +\n";
		print "+          all rights reserved.          +\n";
		print "+                                        +\n";
		print "+        http://www.NextPortal.com       +\n";
		print "+                                        +\n";
		print "+      e-mail: support\@netroamer.com     +\n";
		print "+                                        +\n";
		print "+        http://www.NetRoamer.com        +\n";
		print "+  http://www.industrialimagination.com  +\n";
		print "+                                        +\n";
		print "++++++++++++++++++++++++++++++++++++++++++\n";
		print "\n";
		print "\n";
	if(-e $orbi)
	{
		print "NEXTPORTAL IS ALREADY CONFIGURED!\n\n";
		exit;
	}
	if($ENV{'REMOTE_ADDR'} ne "")
	{
		print "TO CONFIGURE NEXTPORTAL:\n";
		print "\n";
		print "Go to http://www.nextportal.com/autoinstall\n";
		print "\n";
		exit;
	}
	if(($ARGV[0] eq "") && (!-e $orbi))
	{
		print "TO CONFIGURE NEXTPORTAL:\n";
		print "\n";
		print "Go to http://www.nextportal.com/autoinstall\n";
		print "\n";
		exit;
	}
	else
	{
		$foo = 0;
		while(($foo < 10) && ($ARGV[$foo] ne ""))
		{
	
			if($ARGV[$foo] =~ /domain\=([^ ]+)/i)
			{
				$sitedomain = $1;
				$sitedomain =~ tr/A-Z/a-z/;
			}
			if($ARGV[$foo] =~ /url\=([^ ]+)/i)
			{
				$siteaddress = $1;
				$siteaddress =~ tr/A-Z/a-z/;
			}
			if($ARGV[$foo] =~ /siteadmin\=([^ ]+)/i)
			{
				$siteadmin = $1;
				$siteadmin =~ tr/A-Z/a-z/;
			}
			if($ARGV[$foo] =~ /maindir\=([^ ]+)/i)
			{
				$maindir = $1;
				$maindir =~ tr/A-Z/a-z/;
				$nextmaindir = "$maindir/nextportal";
			}
			if($ARGV[$foo] =~ /sourcedir\=([^ ]+)/i)
			{
				$sourcedir = $1;
				$sourcedir =~ tr/A-Z/a-z/;
			}
			if($ARGV[$foo] =~ /sendmail\=([^ ]+)/i)
			{
				$sendmailpath = $1;
				$sendmailpath =~ tr/A-Z/a-z/;
			}
			if($ARGV[$foo] =~ /apachenrml\=([^ ]+)/i)
			{
				$apachenrml = $1;
				$apachenrml =~ tr/A-Z/a-z/;
				if($apachenrml ne "yes")
				{
					$apachenrml = "";
				}
			}
			$foo = $foo + 1;

		}
		$sitename = ".";
		require("$nextmaindir/nextmodules/next-config.cgi");
		$in{'action'} = "MODIFYSITE";
		&AutoConfig;

		print "\nAutoconfiguration of NextPortal is complete.\n\n";
		exit;
	}

	exit;
}


#$duetsite = "$ENV{'HTTP_HOST'}<!--endgo>";
$duetsite = "";
$dueturi = $ENV{'REQUEST_URI'};



if($in{'action'} eq "REGISTERUSER")
{

	$originalinfile = $in{'file'}; #dont know if this is used for anything
	require("$nextmaindir/nextmodules/nrregister.cgi");
	&DoRegisterUser;

}

#if(($in{'dbaction'} eq "ADD2") && (($in{'datafirstname'} ne "") || ($in{'datamiddlename'} ne "") || ($in{'datalastname'} ne "")))
if( (($in{'datafirstname'} ne "") || ($in{'datamiddlename'} ne "") || ($in{'datalastname'} ne "")))
{
	if($in{'datafullname'} eq "")
	{
		$in = "$in&datafullname=$in{'datalastname'}+$in{'datafirstname'}+$in{'datamiddlename'}";
		$in{'datafullname'} = "$in{'datalastname'} $in{'datafirstname'} $in{'datamiddlename'}";
	}
}
if(($in{'datafullname'} ne "") && (($in{'datafirstname'} eq "") || ($in{'datamiddlename'} eq "") || ($in{'datalastname'} eq "")) )
{

	$fooname = "<!last>$in{'datafullname'}\n";
	$fooname =~ s/<!last>([^ ]*) //;
	$in{'datalastname'} = $1;
	$fooname =~ s/([^ ]*) //;
	$in{'datafirstname'} = $1;
	$fooname =~ s/\n//g;
	$in{'datamiddlename'} = $fooname;

}

$originalinfile = $in{'file'};


$goo = $ENV{'REMOTE_ADDR'};

&SetDir;
&nrGetIndexPrefs;

&CheckVirtualSubdomain;

&WhoIsThis;
&GetInterface;


#if($in{'action'} ne "")
{
	&ActionAccess;
}

if($accessgroups =~ /admin/)
{
	require("$nextmaindir/nextmodules/nextadmin-lib.cgi");
	&DoOps;
}



#&nrGetIndexPrefs;
if($in{'chatroom'} ne "")
{
	require("$nextmaindir/nextmodules/nrtalk-lib.cgi");
	&DoChat;
	exit;
}

if($in{'action'} eq "SETPERSONALIZATION")
{
	&UpdatePersonalization;
}

if($in{'action'} eq "DYNAMICDATASAVE")
{
	&DynamicDataSave;
}


####GROUP STUFF###########

&GroupDomainCheck;
if(($in{'setcategory'} eq "SET") && ($in{'username'} ne "marquee"))
{
	&LoadNRgroup;
	&SetCategory;

}
if(($in{'action'} eq "SUBMITSUBSCRIBERSTATUS") && ($in{'username'} ne "marquee"))
{
	&LoadNRgroup;
	&ProcessSubscribers;
	if($in{'file'} eq "")
	{
		$in{'file'} = "groupadmin.nrml";
	}
}
if(($in{'action'} eq "SUBMITPENDINGSTATUS") && ($in{'username'} ne "marquee"))
{
	&LoadNRgroup;
	&ProcessPending;
	if($in{'file'} eq "")
	{
		$in{'file'} = "groupadmin.nrml";
	}
}

if($in{'action'} eq "SUBSCRIBE")
{
	&LoadNRgroup;
	if(($in{'username'} eq "marquee") && ($in{'subscriber'} eq ""))
	{
		$in{'file'} = "subscribeemail.nrml";
	}
	else
	{
		if($in{'subscriber'} ne "")
		{
			$subscriber = $in{'subscriber'};
			$groupname = $in{'groupname'};
			&SendConfirmation;
			$in{'file'} = "confirm.nrml";
		}
		else
		{
			$subscriber = $in{'username'};
			$groupname = $in{'groupname'};
			&UserSubscribes;
		}
	}
}
if(($in{'action'} eq "UNSUBSCRIBE") || ($in{'action'} eq "DELETESUBSCRIPTION"))
{
	&LoadNRgroup;
	if(($in{'username'} eq "marquee") && ($in{'subscriber'} eq ""))
	{
		$in{'file'} = "unsubscribeemail.nrml";
	}
	else
	{
		if($in{'subscriber'} ne "")
		{
			$subscriber = $in{'subscriber'};
		}
		else
		{
			$subscriber = $in{'username'};
		}
		$groupname = $in{'groupname'};
		&UserUnsubscribes;
	}
}

if($in{'confirmcode'} ne "")
{
	&LoadNRgroup;
	&ConfirmSubscription;
}

if(($in{'action'} eq "") && ($in{'groupname'} ne ""))
{

	&LoadNRgroup;
	&ReadGroup;

}
if(($in{'action'} eq "POSTGROUP") && ($in{'username'} ne "marquee"))
{
	&LoadNRgroup;
	&WriteGroup;
}
if(($in{'action'} eq "DELETEGROUP") && ($in{'username'} ne "marquee"))
{
	&LoadNRgroup;
	&DeleteGroup;
}

##########################


###SUPPORT TICKET STUFF###

if($in{'supportticket'} ne "")
{
	require("$nextmaindir/nextmodules/support.cgi");
	require("$nextmaindir/nextmodules/support-lib.cgi");
	&SupportTicket;
	$SupportTicket = "loaded";
}

##########################

if($in{'unsubscribeemail'} ne "")
{

	if($in{'unsubscribeemail'} =~ /([a-z,A-Z,0-9,\.,\_,\-]*\@[a-z,A-Z,0-9,\.,\_,\-]*)/i)
	{

		open(FILEHANDLE,">>$maindir/nrsubscribe/nrsubscribe.queue");
		print FILEHANDLE "<!unsubscribe>\n";
		print FILEHANDLE $in{'unsubscribeemail'},"\n";
		print FILEHANDLE "<!endunsubscribe>\n";
		close FILEHANDLE;

		if($in{'username'} eq "marquee")
		{
			$thingtoerase = "$maindir/temp_users/$actual_username/marquee.subscribe";
		}
		else
		{
			$thingtoerase = "$maindir/users/$actual_username/marquee.subscribe";
		}
		$cnt = unlink($thingtoerase);
	}
	else
	{
		$in{'file'} = "errors/unsubscribeerror.nrml";
	}


}

if($in{'subscribeemail'} ne "")
{

	if($in{'subscribeemail'} =~ /([a-z,A-Z,0-9,\.,\_,\-]*\@[a-z,A-Z,0-9,\.,\_,\-]*)/i)
	{
		if($in{'subscribetype'} eq "")
		{
			$in{'subscribetype'} = "email";
		}
		open(FILEHANDLE,">>$maindir/nrsubscribe/nrsubscribe.queue");
			print FILEHANDLE "<!subscribe>\n";
			print FILEHANDLE $in{'username'},"\n";
			print FILEHANDLE $actual_realname,"\n";
			print FILEHANDLE $ENV{'REMOTE_ADDR'},"\n";
			print FILEHANDLE $in{'subscribetype'},"\n";
			print FILEHANDLE $in{'subscribeemail'},"\n";
			print FILEHANDLE "<!endsubscribe>\n";
		close FILEHANDLE;
		if($in{'username'} eq "marquee")
		{
			$thingtowrite = "$maindir/temp_users/$actual_username/marquee.subscribe";
		}
		else
		{
			$thingtowrite = "$maindir/users/$actual_username/marquee.subscribe";
		}
		open(FILEHANDLE,">$thingtowrite");
		close FILEHANDLE;
	}
	else
	{
		$in{'file'} = "errors/subscribeerror.nrml";
	}


}

if($in{'action'} eq "POSTSUBDOMAIN")
{
	if($in{'location'} eq "")
	{
		$delorbi = "$maindir/users/$in{'username'}/redirect.url";
		unlink($delorbi);
		$delorbi = "$maindir/users/$in{'username'}/redirect.type";
		unlink($delorbi);
		if($in{'file'} eq "")
		{
			$in{'file'} eq "subdomainremoved.nrml";
		}
	}
	else
	{
		$in{'location'} = "<!--feego>$in{'location'}";
		$in{'location'} =~ s/<!--feego>http:\/\///i;
		$in{'location'} =~ s/<!--feego>//g;
		$in{'location'} = "http://$in{'location'}";

		open(FILEHANDLE,">$maindir/users/$in{'username'}/redirect.url");
		print FILEHANDLE $in{'location'},"\n";
		close FILEHANDLE;

		open(FILEHANDLE,">$maindir/users/$in{'username'}/redirect.type");
		print FILEHANDLE $in{'redirecttype'},"\n";
		close FILEHANDLE;


		if($in{'file'} eq "")
		{
			$in{'file'} = "subdomainsaved.nrml";
		}
	}

}

if($in{'action'} eq "ADDATTACHE")
{

	if($in{'username'} eq "marquee")
	{
		#use TEMP_USERNAME;
		$attacheuser = $in{'tempusername'};
		$attachefile = "$maindir/temp_users/$attacheuser";
	}
	else
	{
		$attacheuser = $in{'username'};
		$attachefile = "$maindir/users/$attacheuser";
	}
	$in{'thisscreenid'} =~ s/\///g;
	$in{'thisscreenid'} =~ s/\.\.//g;
	open(FILEHANDLE,"<$maindir/cache/$in{'thisscreenid'}.acache");
	while($line = <FILEHANDLE>)
	{
		$line =~ s/\n//g;
		if($line =~ /<!--attache_$in{'attacheid'}>/)
		{
			$addattachecategory = <FILEHANDLE>;
				$addattachecategory =~ s/\n//g;
			$addattachename = <FILEHANDLE>;
				$addattachename =~ s/\n//g;
			$line = <FILEHANDLE>;
				$line =~ s/\n//g;
			
			$addattachecontent = "";
			while($line ne "<!--endattache>")
			{
				$addattachecontent = "$addattachecontent$line\n";
				$line = <FILEHANDLE>;
					$line =~ s/\n//g;
				if(eof(FILEHANDLE))
				{
					$line = "<!--endattache>";
				}
			}

		}
	}
	close FILEHANDLE;

	open(FILEHANDLE,">>$attachefile/marquee.attache");
		print FILEHANDLE "<!--attache>\n";
		print FILEHANDLE $in{'attacheid'},"\n";
		print FILEHANDLE $addattachecategory,"\n";
		print FILEHANDLE $addattachename,"\n";
		print FILEHANDLE $addattachecontent;
		print FILEHANDLE "<!--endattache>\n";
	close FILEHANDLE;

	if($in{'file'} eq "")
	{
		$in{'file'} = "attacheadded.nrml";
	}

}

if($in{'action'} eq "REMOVEATTACHE")
{

	if($in{'username'} eq "marquee")
	{
		#use TEMP_USERNAME;
		$attacheuser = $in{'tempusername'};
		$attachefile = "$maindir/temp_users/$attacheuser";
	}
	else
	{
		$attacheuser = $in{'username'};
		$attachefile = "$maindir/users/$attacheuser";
	}

	open(FILEHANDLE,"<$attachefile/marquee.attache");
	open(FILEHANDLEB,">$attachefile/marquee.attache.foo");
	while($line = <FILEHANDLE>)
	{
		$line =~ s/\n//g;
		if($line eq "<!--attache>")
		{
			$fooattache = <FILEHANDLE>;
				$fooattache =~ s/\n//g;
			if($fooattache eq $in{'attacheid'})
			{
				$line = <FILEHANDLE>;
					$line =~ s/\n//g;
				while($line ne "<!--endattache>")
				{
					$line = <FILEHANDLE>;
						$line =~ s/\n//g;
					if(eof(FILEHANDLE))
					{
						$line = "<!--endattache>";
					}
				}
			}
			else
			{
				print FILEHANDLEB $line,"\n";
				print FILEHANDLEB $fooattache,"\n";
			}
		}
		else
		{
			print FILEHANDLEB $line,"\n";
		}

	}
	close FILEHANDLEB;
	close FILEHANDLE;

	open(FILEHANDLE,"<$attachefile/marquee.attache.foo");
	open(FILEHANDLEB,">$attachefile/marquee.attache");
	while($line = <FILEHANDLE>)
	{
		print FILEHANDLEB $line;
	}
	close FILEHANDLEB;
	close FILEHANDLE;

	$thingtoerase = "$attachefile/marquee.attache.foo";
	$cnt = unlink($thingtoerase);


}

if($in{'action'} eq "CHANGEPASSWORD")
{

	$in{'newpassword'} =~ s/\n//g;

	if(($in{'newpassword'} eq $in{'newpassword2'}) && ($in{'newpassword'} ne ""))
	{
		open(FILEHANDLE,"<$maindir/users/$in{'username'}/nrmail.safe");
		$la[0] = <FILEHANDLE>;
		$la[1] = <FILEHANDLE>;
		$la[2] = <FILEHANDLE>;
		$la[3] = <FILEHANDLE>;
		$la[4] = <FILEHANDLE>;
		$la[4] = "$in{'newpassword'}\n";
		$coo = 5;

		while($line = <FILEHANDLE>)
		{
			$la[$coo] = $line;
			$coo = $coo + 1;
		}
		close FILEHANDLE;

		$foo = 0;
		open(FILEHANDLEB,">$maindir/users/$in{'username'}/nrmail.safe");
		while($foo < $coo)
		{
			print FILEHANDLEB $la[$foo];
			$foo = $foo + 1;
		}
		close FILEHANDLEB;
	}
	else
	{
		$in{'file'} = "changepassworderror.nrml";
	}

}

if($in{'action'} eq "logout")
{

		open(FILEHANDLE,">$maindir/users/$in{'username'}/nrmail.code");
		print FILEHANDLE "\n";
		print FILEHANDLE "\n";
		close FILEHANDLE;
		open(FILEHANDLE,">$maindir/users/$in{'username'}/nruser.code");
		print FILEHANDLE "\n";
		print FILEHANDLE "\n";
		close FILEHANDLE;
		$thingtoerase = "$maindir/nralert/online/$in{'username'}.online";
		$cnt = unlink($thingtoerase);

		if($in{'username'} ne "marquee")
		{
			$thingtoerase = "$maindir/logs/online/$in{'username'}.online";
			unlink($thingtoerase);
		}
	$in{'file'} = "logout.nrml";
}


if($in{'action'} eq "SENDPASSWORD")
{

	open(FILEHANDLE,"<$maindir/users/$in{'baduser'}/marquee.misc");
	while($line = <FILEHANDLE>)
	{
		$line =~ s/\n//g;
		
		if($line eq "<!sendpasscode>")
		{
			$sendpasscode = <FILEHANDLE>;
				$sendpasscode =~ s/\n//g;
		}
	}
	close FILEHANDLE;

if($sendpasscode =~ /\@/i)
{


	open(MAIL,"|$mailprog $sendpasscode") || die "Can't open $mailprog";
	print MAIL "From: $lostpasscodeemail ($lostpasscodename)\n";
	print MAIL "Reply-To: $adminemail\n";
	print MAIL "To: $sendpasscode ($sendpasscode)\n";
	print MAIL "Subject: $lostpasscodesubject\n\n";


	open(FILEHANDLE,"<$maindir/users/$in{'baduser'}/nrmail.safe");
			$blah = <FILEHANDLE>;
			$pre_realname = <FILEHANDLE>;
				$pre_realname =~ s/\n//g;
			$pre_username = <FILEHANDLE>;
				$pre_username =~ s/\n//g;
			$pre_usercode = <FILEHANDLE>;
				$pre_usercode =~ s/\n//g;
			$pre_passcode = <FILEHANDLE>;
				$pre_passcode =~ s/\n//g;
			$pre_forward = <FILEHANDLE>;
				$pre_forward =~ s/\n//g;
			$pre_forwardaddr = <FILEHANDLE>;
				$pre_forwardaddr =~ s/\n//g;
	close FILEHANDLE;

	open(FILEHANDLE,"<$maindir/config/lostpasscode.mail");
	while($line = <FILEHANDLE>)
	{	#URF.14627
	
					#$line =~ s/\$[a,A][u,U][t,T][h,H][c,C][o,O][d,D][e,E]/$code/g;
					$line =~ s/\$[u,U][s,S][e,E][r,R][n,N][a,A][m,M][e,E]/$pre_username/g;
					$line =~ s/\$[d,D][o,O][m,M][a,A][i,I][n,N]/$domainname/g;
					$line =~ s/\$[p,P][a,A][s,S][s,S][c,C][o,O][d,D][e,E]/$pre_passcode/g;
					$line =~ s/\$[r,R][e,E][a,A][l,L][n,N][a,A][m,M][e,E]/$pre_realname/g;
					$boo = $pre_realname;
					$boo =~ s/([a-z,A-Z,\-,\_]*)  *([a-z,A-Z,\-,\_]*)//;
					$fname = $1;
					$lname = $2;
					$line =~ s/\$[f,F][i,I][r,R][s,S][t,T] *[n,N][a,A][m,M][e,E]/$fname/g;
					$line =~ s/\$[l,L][a,A][s,S][t,T] *[n,N][a,A][m,M][e,E]/$lname/g;

					$line =~ s/\$[m,M][o,O][n,N][t,T][h,H]/$monthname[$newmon]/g;
					$line =~ s/\$[d,D][a,A][t,T][e,E]/$newmday/g;
					$line =~ s/\$[d,D][a,A][y,Y]/$dayname[$newyday]/g;
					$line =~ s/\$[y,Y][e,E][a,A][r,R]/19$newyear/g;
					$line =~ s/\$[t,T][i,I][m,M][e,E]/$pagetime/g;
					$line =~ s/\$SENDPASSWORDADDRESS/$sendpasscode/ig;

		print MAIL $line;
	}	#URF.14641

	close FILEHANDLE;
	close MAIL;

	$in{'file'} = "sentpassword.nrml";
}
###########
	else
	{
		$in{'file'} = "sendpasserror.nrml";
	}

}


#print &PrintHeader;

$goo = $ENV{'REMOTE_HOST'};
#$goo = $ENV{'REMOTE_ADDR'};

$mainpage = "";

if($in{'file'} eq "")
{
	#$in{'file'} = "index.nrml";
}

#
# FILE DETERMINE - NRFORUM
#

if($in{'nrforum'} eq "yes")
{

	require("$nextmaindir/nextmodules/nrforum-main.cgi");
	require("$nextmaindir/nextmodules/nrforum-lib.cgi");
	&MarqueeForum;

	#&ForumCarmen;
	$didforum = "yes";
}

#if($mainpage =~ /SEARCH RESULTS/i)
if(($in{'searchterm'} ne "") || ($in{'nrsearchtype'} ne "") || ($in{'keyword'} ne ""))
{
	$godestination = "";
	if($in{'keyword'} ne "")
	{

		#check keywords first
		$foundit = "";
		$godestination = "";
		open(FILEHANDLE,"<$maindir/config/keywords.config");
		while(($line = <FILEHANDLE>) && ($foundit eq ""))
		{
			$line =~ s/\n//g;
			($checkkeywords,$checkcomment,$checkdestination) = split(/###/,$line);
			if($checkkeywords =~ /$in{'keyword'}/i)
			{
				$foundit = "yes";
				$godestination = $checkdestination;
			}
		}
		close FILEHANDLE;
		if($godestination ne "")
		{

			$thetime = localtime;
			open(FILELOG,">>$maindir/logs/keywords.log");
			print FILELOG "$ENV{'REMOTE_ADDR'}###$in{'keyword'}###$godestination###$in{'username'}###$thetime\n";
			close FILELOG;
			if($godestination =~ /http:\/\//i)
			{
				print "Location: $godestination\n\n";
				exit;
			}
			$in{'file'} = $godestination;
		}
		else
		{
		}

		$foundit = "";
	}
	if($godestination eq "")
	{
		if($in{'keyword'} ne "")
		{
			$in{'searchterm'} = $in{'keyword'};
		}
		require("$nextmaindir/nextmodules/nrsearch-lib.cgi");
		require("$nextmaindir/nextmodules/nrsearch.cgi");
		&DoSearch;
	}
}

if($mainurl eq "")
{
	&nrGetIndexPrefs;
}
&OpenTheScreen;
&ProcessIncludeTags;

if($mainpage =~ /< *AUTO *SEARCH *([^>]*)>/i)
{
	$autosearch = $1;
	$autosearch =~ s/\"//g;

	if($in{'searchterm'} eq "")
	{
		require("$nextmaindir/nextmodules/nrsearch-lib.cgi");
		require("$nextmaindir/nextmodules/nrsearch.cgi");
		$in{'searchterm'} = $autosearch;
		&DoSearch;
	}

}

#get time/date stuff together
&nrGetTime;

if(($in{'searchterm'} ne "") || ($in{'nrsearchtype'} ne ""))
{

&ClearExcess;

&GetCache;
&SearchZy;
&cacheclear;
}
if(($mainpage =~ /TRINKET/i) || ($in{'dbpreviousscreen'} ne "") || ($in{'dbaction'} ne ""))
{
	require("$nextmaindir/nextmodules/trinketdb.cgi");
}

if($mainpage =~ /NRREVIEW/i)
{

	require("$nextmaindir/nextmodules/nrreview.cgi");
	&PrepReview;

}

if($mainpage =~ /NRNEWS/i)
{
	require("$nextmaindir/nextmodules/nrnews.cgi");
	#prepare the news (retrieve it!)
	&prepZy;
}

#NetRoamer PlusPaks
&GUIJoe;

if($SupportTicket eq "loaded")
{
	&TicketNRML;
}

if($mainpage =~ /NRNEWS/i)
{
	&Zy;
}

if($mainpage =~ /< *!* *USER *REGISTRATION *>/i)
{
	require("$nextmaindir/nextmodules/nrregister-lib.cgi");
	&RegisterCarmen;
}

if(($mainpage =~ /FORUM/i) || ($in{'forumname'} ne ""))
{

	if($didforum eq "")
	{
		require("$nextmaindir/nextmodules/nrforum-main.cgi");
		require("$nextmaindir/nextmodules/nrforum-lib.cgi");
	}
		&nrFGetTime;
	if($didforum eq "")
	{
		&MarqueeForum;
	}
		&ForumCarmen;
		$didforum = "yes";
}

if($mainpage =~ /CALENDAR/i)
{
	require("$nextmaindir/nextmodules/calendar.cgi");
	#&PrepCalendar;
	&DoCalendar;

}

if($in{'chatroom'} ne "")
{
	require("$nextmaindir/nextmodules/nrtalk-lib.cgi");
	&DoChat;

}

&ClearExcess;

$mainpage =~ s/<!--dollarsign>/\$/ig;

if($in{'nralertstuff'} eq "")
{
	#print $mainpage;
	print &PrintHeader;
	&Display;
}

if($nraffiliate ne "")
{
	return $nraffiliate;
}


sub CheckVirtualSubdomain
{

	$incomingdomain = "$ENV{'HTTP_HOST'}\n";
	$incomingdomain =~ s/(.*)\.([^\.]*)\.([^\n|^\.]*)\n//;
	$incomingdomain = $1;
	$incominguser = $2;
	$incomingdomain =~ tr/A-Z/a-z/;
	$incominguser =~ tr/A-Z/a-z/;

	if(($incomingdomain eq "admin") && (($ENV{'REQUEST_URI'} eq "") || ($ENV{'REQUEST_URI'} eq "/")))
	{
		#BOOP
		&WhoIsThis;
		if($accessgroups =~ /admin/i)
		{
			$foundfuser = "yes";
			$in{'file'} = "nextportal/admin/index.nrml";
		}

	}

	if($incomingdomain =~ /affiliate\./)
	{
		$incomingdomain =~ /(.*)\.(.*)/;
		$incominguser = $2;

		foreach (split(/; /, $ENV{'HTTP_COOKIE'}))
		{
			($chip,$value) = split(/=/);
			$cookie{$chip} = $value;
		}
		if($cookie{'affiliatename'} eq "")
		{
			print "Set-Cookie: affiliatename=$incominguser;","path=/;","domain=.$duetdomain;";
			print "\n";
			$thetime = localtime;
			open(FILEAFF,">>$affiliatedir/$incominguser/affiliate.log");
			print FILEAFF "$ENV{'REMOTE_ADDR'}###$thetime###$incominguser###$in{'username'}\n";
			close FILEAFF;
			open(FILEAFF,">>$maindir/logs/affiliate.log");
			print FILEAFF "$ENV{'REMOTE_ADDR'}###$thetime###$incominguser###$in{'username'}\n";
			close FILEAFF;
		}
	}
	if($incomingdomain =~ /forum\./)
	{
		$incomingdomain =~ /(.*)\.(.*)/;
		$incominguser = $2;
		$in{'forumname'} = $incominguser;

		$in{'forumname'} =~ tr/A-Z/a-z/;
		$someforumplace = "$maindir/forums/$in{'forumname'}/forumlist.nrml";
		if(-e $someforumplace)
		{
			
		}
		else
		{
			$foundfuser = "";

			&WhoIsThis;
			if($in{'forumname'} eq $in{'username'})
			{
				if($accessgroups =~ /forum/i)
				{
					$foundfuser = "yes";
				}
			}
			if($in{'forumname'} eq "demo")
			{
				$foundfuser = "yes";
			}

			if($foundfuser ne "")
			{
				umask("000");
				mkdir("$maindir/forums/$in{'forumname'}",0777);

				system("cp $maindir/default/forums/forumcensor.nrml $maindir/forums/$in{'forumname'}/forumcensor.nrml");
				system("cp $maindir/default/forums/forumlist.nrml $maindir/forums/$in{'forumname'}/forumlist.nrml");
				system("cp $maindir/default/forums/forummessages.nrml $maindir/forums/$in{'forumname'}/forummessages.nrml");
				system("cp $maindir/default/forums/forumnopost.nrml $maindir/forums/$in{'forumname'}/forumnopost.nrml");
				system("cp $maindir/default/forums/forumpost.nrml $maindir/forums/$in{'forumname'}/forumpost.nrml");
				system("cp $maindir/default/forums/forumremove.nrml $maindir/forums/$in{'forumname'}/forumremove.nrml");
				system("cp $maindir/default/forums/forumreply.nrml $maindir/forums/$in{'forumname'}/forumreply.nrml");
				system("cp $maindir/default/forums/forumthankyou.nrml $maindir/forums/$in{'forumname'}/forumthankyou.nrml");
				system("cp $maindir/default/forums/forumwait.nrml $maindir/forums/$in{'forumname'}/forumwait.nrml");
				system("cp $maindir/default/forums/index.nrml $maindir/forums/$in{'forumname'}/index.nrml");


			}
			if($foundfuser eq "")
			{
				print &PrintHeader;
				&PreScreen;
				print "ERROR 6930115: No forum named \"$in{'forumname'}\"<br>\n";
				exit;
			}
		}




		return;
	}
	if($incomingdomain =~ /calendar\./)
	{
		$incomingdomain =~ /(.*)\.(.*)/;
		$incominguser = $2;
		$in{'calendarname'} = $incominguser;
		return;
	}
	if($incomingdomain =~ /profile\./)
	{
		$incomingdomain =~ /(.*)\.(.*)/;
		$incominguser = $2;
		#$in{'profilename'} = $incominguser;
		$profilename = $incominguser;
		return;
	}
	if($incomingdomain =~ /group\./)
	{
		return;
	}
	if($incomingdomain =~ /chat\./)
	{
		$o_chatroom = $in{'chatroom'};
		$incomingdomain =~ /(.*)\.(.*)/;
		$incominguser = $2;
		$in{'chatroom'} = $incominguser;

		$in{'chatroom'} =~ tr/A-Z/a-z/;
		$somechatplace = "$maindir/chatrooms/$in{'chatroom'}/chatstart.html";
		if(-e $somechatplace)
		{
			
		}
		else
		{
			$foundcuser = "";

			&WhoIsThis;
			if($in{'chatroom'} eq $in{'username'})
			{
				if($accessgroups =~ /chat/i)
				{
					$foundcuser = "yes";
				}
			}
			if($in{'chatroom'} eq "demo")
			{
				$foundcuser = "yes";
			}
			if($foundcuser ne "")
			{
				umask("000");
				mkdir("$maindir/chatrooms/$in{'chatroom'}",0777);
				umask("000");
				mkdir("$maindir/chatrooms/$in{'chatroom'}/alerts",0777);
				mkdir("$maindir/chatrooms/$in{'chatroom'}/conversation",0777);
				umask("000");
				mkdir("$maindir/chatrooms/$in{'chatroom'}/files",0777);

				system("cp $maindir/preconfig/.htaccess $maindir/chatrooms/$in{'chatroom'}/conversation/.htaccess");
				system("cp $maindir/preconfig/.htaccess $maindir/chatrooms/$in{'chatroom'}/alerts/.htaccess");
				system("cp $maindir/default/chat/chatlogin.nrml $maindir/chatrooms/$in{'chatroom'}/chatlogin.nrml");
				system("cp $maindir/default/chat/chatalert.nrml $maindir/chatrooms/$in{'chatroom'}/chatalert.nrml");
				system("cp $maindir/default/chat/chatroom.nrml $maindir/chatrooms/$in{'chatroom'}/chatroom.nrml");
				system("cp $maindir/default/chat/chatmenu.nrml $maindir/chatrooms/$in{'chatroom'}/chatmenu.nrml");
				system("cp $maindir/default/chat/chatfilemenu.nrml $maindir/chatrooms/$in{'chatroom'}/chatfilemenu.nrml");
				system("cp $maindir/default/chat/chatupdate.html $maindir/chatrooms/$in{'chatroom'}/chatupdate.html");
				system("cp $maindir/default/chat/chatbanner.html $maindir/chatrooms/$in{'chatroom'}/chatbanner.html");
				system("cp $maindir/default/chat/chatstart.html $maindir/chatrooms/$in{'chatroom'}/chatstart.html");
				system("cp $maindir/default/chat/chatblank.html $maindir/chatrooms/$in{'chatroom'}/chatblank.html");
				system("cp $maindir/default/chat/.htaccess $maindir/chatrooms/$in{'chatroom'}/conversation/.htaccess");


			}
			if($foundcuser eq "")
			{
				print &PrintHeader;
				&PreScreen;
				print "ERROR 3830012: No chatroom named \"$in{'chatroom'}\"<br>\n";
				exit;
			}
		}


		if($o_chatroom eq "")
		{

			$o_mainurl = $mainurl;
			$o_mainurl =~ s/http:\/\/[^\/]+//i;

			$chaturl = "http://$ENV{'HTTP_HOST'}$o_mainurl";

			print &PrintHeader;
			print "<script language = \"javascript\">\n";
			print "window.open('$chaturl/next.cgi?chatroom=$in{'chatroom'}','NextportalChat','toolbar=no,status=no,directories=no,menubar=no,resizable=no,height=398,width=600');\n";
			print "history.go(-1);\n";
			print "</script>\n";
			exit;
		}

		return;
	}

	if(($incomingdomain ne "www") && ($incomingdomain ne ""))
	{
		$o_incoming = $incomingdomain;
		$o_incoming =~ tr/A-Z/a-z/;
		$orbi = "$maindir/sites/$o_incoming/index.html";
		$orbj = "$maindir/sites/$o_incoming/index.htm";
#POOP
		open(FILEHANDLE,"<$maindir/config/marquee.hosts");
		$done = "";
		while(($line = <FILEHANDLE>) && ($done eq ""))
		{
			$line =~ s/\n//g;
			if($line eq "<!marqueeredirect>")
			{
				$prefix = <FILEHANDLE>;
					$prefix =~ s/\n//g;
				$checkfor = <FILEHANDLE>;
					$checkfor =~ s/\n//g;
				$onerror = <FILEHANDLE>;
					$onerror =~ s/\n//g;
				$redirect = <FILEHANDLE>;
					$redirect =~ s/\n//g;
				if($redirect eq "<!start>")
				{
					$redirectstuff = "";
					$line = <FILEHANDLE>;
						$line =~ s/\n//g;
					while($line ne "<!endmarqueeredirect>")
					{
						$redirectstuff = "$redirectstuff$line\n";	

						$line = <FILEHANDLE>;
							$line =~ s/\n//g;
					}
				}
				else
				{
					$line = <FILEHANDLE>;
				}
				if($incomingdomain =~ /$prefix/i)
				{
					$done = "yes";
				}
			}
			else
			{
				$res_host = "";
				$res_goto = "";
				($res_host,$res_goto) = split(/###/,$line);
				$foodomain = "$incomingdomain<!>";
				if(($foodomain =~ /\.*$res_host\.*<!>/i) && ($res_host ne ""))
				{
					$done = $res_goto;
				}
			}


		}
		close FILEHANDLE;
		
		if($done ne "yes")
		{
			$incomingdomain =~ s/www\.//i;
			$incomingdomain =~ tr/A-Z/a-z/;
			#open(FILEHANDLE,"<$maindir/users/$in{'username'}/redirect.url");
			open(FILEHANDLE,"<$maindir/users/$incomingdomain/redirect.url");
			$location = <FILEHANDLE>;
				$location =~ s/\n//g;
			close FILEHANDLE;


			if($location ne "")
			{

				$orbi = "$maindir/../subdomainredirect.nrml";
				if(-e $orbi)
				{
					$thispage = "";
					open(FILEHANDLE,"<$orbi");
					while($line = <FILEHANDLE>)
					{
						$thispage = "$thispage$line";
					}
					close FILEHANDLE;

					$thispage =~ s/\$LOCATION/$location/ig;
					$mainpage = $thispage;
					&GUIJoe;

					print &PrintHeader;
					&PreScreen;
					print $thispage;
					exit;
				}
				print "Location: $location\n\n";
				exit;
			}
			else
			{
				print "Location: http://$domain/nextportal/sites/$incomingdomain\n\n";
				exit;
			}
			#otherwise do nothing
		}
		if($done eq "yes")
		{
			$foobar = "$incomingdomain<!>";
			$foobar =~ s/$prefix([a-z,A-Z,\_,\.,0-9]*)\.*<!>//i;
			$usersite = $1;
			$checkfor =~ s/\$USERSITE/$usersite/ig;
			$redirect =~ s/\$USERSITE/$usersite/ig;
			$redirectstuff =~ s/\$USERSITE/$usersite/ig;
			$onerror =~ s/\$USERSITE/$usersite/ig;

			if((($checkfor ne "") && (-e $checkfor)) || ($checkfor eq ""))
			{
				if($redirect eq "<!start>")
				{
					print &PrintHeader;
					print $redirectstuff;
					exit;
				}
				else
				{
					print "Location: $redirect\n\n";
					exit;
				}
			}
			else
			{
				if($checkfor ne "")
				{
					print "Location: $onerror\n\n";
					exit;
				}
			}
		}
		else
		{
			if($done ne "")
			{
				print "Location: $done\n\n";
				exit;
			}
		}

		if(($incomingdomain =~ /\.postcard/i) && ($in{'action'} ne "RETRIEVEPOSTCARD"))
		{
			$in{'action'} = "RETRIEVEPOSTCARD";
			$getpostid = $incomingdomain;
			$getpostid =~ s/([^\.]*)\.postcard//i;
			$in{'postcardid'} = $1;
			print &PrintHeader;
			print "<script language = \"javascript\">\n";
			print "window.open(\"$mainurl/next.cgi?action=RETRIEVEPOSTCARD&postcardid=$in{'postcardid'}&file=nrpostcard/postcard.nrml\",\"postcard$in{'postcardid'}\",\"toolbar=no,status=no,directories=no,menubar=no,resizable=yes,scrollbars=no,height=270,width=520\");\n";
			print "history.go(-1);\n";
			print "</script>\n";

			exit;
		}
		
	}
	return 1;
}



