#!/usr/bin/perl
############################################
##                                        ##
##       Developed by Kaviraj Kodai       ##
##        www.eDesignParadigm.com         ##
##     (scripts@edesignparadigm.com)      ##
##                                        ##
##         Ultimate Member Manager        ##
##              version:  1.0             ##
##    last modified: 10/December/2002     ##
##           copyright (c) 2002           ##
##                                        ##
##                                        ##
##    For additional information visit    ##
##     http://www.eDesignParadigm.com     ##
##                                        ##
################################################################
#   COPYRIGHT (C) 2002 KAVIRAJ KODAI. ALL RIGHTS RESERVED      #
################################################################
# Copyright Notice                                             #
# ----------------                                             #
# This program is a commercial product and must NOT be copied, #
# modified, distributed, or installed without a user license   #
# from http://www.eDesignParadigm.com. Any modification of the #
# script without the written consent of Kaviraj Kodai is       #
# strictly prohibited.  You may not remove any of these header #
# notices. By using this code you agree to indemnify the author#
# from any liability that might arise from it's use.           #
################################################################

################################################################
#                  DO NOT EDIT ANYTHING BELOW                  #
################################################################

require "config.cgi";
&configure;

use CGI qw (:standard);
use CGI::Carp qw(fatalsToBrowser);

if (param(action) eq "add") {&add;}
if (param(action) eq "doadd") {&doadd;}


sub add{
$pass = param('pass');
if ($pass ne $accesspass) {&error("An unauthorized request was passed to the script.<br><br>  Please contact <a href=\"mailto:$adminemail\">$adminemail</a> if you think there's an error.<br><br>Thank You!");}

open (HEADERFILE, "$header");
@header= <HEADERFILE>;
close(HEADERFILE);

open (FOOTERFILE, "$footer");
@footer = <FOOTERFILE>;
close(FOOTERFILE);

open (STYLEFILE, "$style");
@style= <STYLEFILE>;
close(STYLEFILE);

	print "Content-type: text/html\n\n";
	print <<html_;
<html>
<head>
<title>Ultimate Member Manager</title>
@style
</head>
@header
<center>
<table border=0 cellspacing=1 cellpadding=0 bgcolor=#000000 width=600><tr><td>
<table border=0 width=100% bgcolor=#ffffff><tr><td>
<font class=tablefont>Please fill out the form below to create your <b>member account</b>. All fields marked with * are required.<BR><BR>

<form action="add.cgi" method="post"><input type=hidden name=action value=doadd>
<input type=hidden name="pass" value="$pass">
<table border=0 class="tablefont">
<tr><td>How Did You Hear About Us:</td><td><input type=text name=howdid size=60 class="text"> <font size=1 face="verdana, arial">optional</font></td></tr>
<tr><td>* Your Name:</td><td> <input type=text name=name size=40 class="text"></td></tr>
<tr><td>* Your Email address:</td><td><input type=text name=email size=30 class="text"></td></tr>
<tr><td>* Account ID:</td><td> <input type=text name=acctid size=30 class="text"> <font size=1 face="verdana, arial"> will give you access to the members area (max 15 characters).</font></td></tr>
<tr><td>* Password:</td><td> <input type=password name=password size=30 class="text"> <font size=1 face="verdana, arial">(max 15 characters)</font></td></tr>
<tr><td> </td><td><input type="submit" value="Create Account" class="button"> &nbsp;&nbsp;&nbsp;&nbsp; <input type="reset" value="Reset!" class="button"></td></tr></table></form></center>

If you are having any problems, <B>don't panic</B>. Email us at <A HREF="mailto:$adminemail">$adminemail</A> and we'll get back to you asap.<BR><BR></td></tr></table>
</td></tr></table></center>

@footer
</body>
</html>
html_
exit;
}

sub doadd{
$pass = param('pass');

if ($pass ne $accesspass) {&error("An unauthorized request was passed to the script.<br><br>  Please contact <a href=\"mailto:$adminemail\">$adminemail</a> if you think there's an error.<br><br>Thank You!");}

$name = param('name');
$name =~s/'/''/g;
$email = param('email');
$email =~s/'/''/g;
$acctid = param('acctid');
$acctid =~s/'/''/g;
$password = param('password');
$password =~s/'/''/g;
$howdid = param('howdid');
$howdid =~s/'/''/g;


$ip = $ENV{'REMOTE_ADDR'};

my $tstamp = time();
my ($y, $m, $d, $ss, $mm, $hh) = (localtime($tstamp))[5,4,3,0,1,2];
$y += 1900;
$y = sprintf("%02d", $y % 100); 
$m += 1;
if (length($m) < 2) {$m = "0" . $m}
if (length($d) < 2) {$d = "0" . $d}
$date = "$m/$d/$y";

$time = time;

if ($name eq ""){ 
		&error("Please enter your name!");
} 

unless ($email =~ /.*\@.*\..*/)  {

	error("The email is invalid!");		
}

if ($acctid eq ""){ 
		&error("Account ID field is empty!");
} 

if ($acctid =~ /[^a-zA-Z_0-9]/) {&error("Only letters and numbers for <b>account name</b>!<br><br>No space or special characters please!");}

if ($password eq ""){ 
		&error("Password field is empty!");
} 

$accountlength = length($acctid);

if ($accountlength > 15) {
	&error("Please limit your \"<b>account name</b>\" to under 15 characters.");
}

$passlength = length($password);

if ($passlength > 15) {
	&error("Please limit your \"<b>account password</b>\" to under 15 characters.");
}
chomp($email);

$cursor=$dbh->prepare("select email from memdata where email='$email'");
$cursor->execute;
$email2 = $cursor->fetchrow_array;
chomp($email2);
if ($email2 eq $email)
{
        &error("It looks like email address <b>$email</b> is already registered.<br><br>Please go back and enter a different email address.<br><br>");
}

$cursor=$dbh->prepare("select acctid from memdata where acctid='$acctid'");
$cursor->execute;
$account = $cursor->fetchrow_array;
if ($account eq $acctid)
{
        &error("It looks like account <b>$acctid</b> is already registered.<br><br>Please go back and select a new account name.<br><br>");
}
    $cursor->finish;

$cursor=$dbh->prepare("select doubleoptin from settings where acctid='99'");
$cursor->execute;
$doubleoptin = $cursor->fetchrow_array;
if ($doubleoptin eq "Y")
{
&doubleoptin;
}


$cursor=$dbh->prepare("INSERT INTO memdata (acctid,password,name,email,howdid,date,time,ip) VALUES ('$acctid','$password','$name','$email','$howdid','$date','$time','$ip')");
$cursor->execute;
if ($cursor->err != 0)
{
        &error("Cannot connect to database to save parameters - error 1");
}
$cursor->finish;

$newpassword = crypt($password, yo);
$newline = join
("\:",$acctid,$newpassword);
$newline .= "\n";

open(DB, ">>$memfile") or print"unable to open htpasswd";
print DB $newline;
close (DB);

$cursor1=$dbh->prepare("select introduction,adminnotify,sitename,protecturl from settings where acctid='99'");
$cursor1->execute;
($introlet,$adminlet,$sitename,$protecturl) = $cursor1->fetchrow_array;
$cursor1->finish;
 $dbh->disconnect;

@welcomelet = $introlet;
@adminnotify = $adminlet;

   foreach $welcomelet(@welcomelet) { 
   $welcomelet =~ s/\[NAME\]/$name/gi;
   $welcomelet =~ s/\[EMAIL\]/$email/gi;
   $welcomelet =~ s/\[ACCOUNT\]/$acctid/gi;
   $welcomelet =~ s/\[PASSWORD\]/$password/gi;
   $welcomelet =~ s/\[DATE\]/$date/gi;
   $welcomelet =~ s/\[IP\]/$ip/gi;
   $welcomelet =~ s/\[PROTECTURL\]/$protecturl/gi;
   $welcomelet =~ s/\[SITENAME\]/$sitename/gi;
   $welcomelet =~ s/\[ADMINEMAIL\]/$adminemail/gi;
   }

open (MAIL, "|$mailprog -t") || die "Can't open $mailprog!\n";

   print MAIL "To: $email\n";
   print MAIL "From: $adminemail\n";
   print MAIL "Subject: $name, Thank you for joining.\n\n";
   foreach $welcomelet(@welcomelet) {
   print MAIL "$welcomelet";
   }
close(MAIL);

   foreach $adminnotify(@adminnotify) { 
   $adminnotify =~ s/\[NAME\]/$name/gi;
   $adminnotify =~ s/\[EMAIL\]/$email/gi;
   $adminnotify =~ s/\[ACCOUNT\]/$acctid/gi;
   $adminnotify =~ s/\[PASSWORD\]/$password/gi;
   $adminnotify =~ s/\[DATE\]/$date/gi;
   $adminnotify =~ s/\[IP\]/$ip/gi;
   $adminnotify =~ s/\[HOWDID\]/$howdid/gi;
   }

open (MAIL1, "|$mailprog -t") || die "Can't open $mailprog!\n";

   print MAIL1 "To: $adminemail\n";
   print MAIL1 "From: $adminemail\n";
   print MAIL1 "Subject: New Member Joined.\n\n";
   foreach $adminnotify(@adminnotify) {
   print MAIL1 "$adminnotify";
   }
close(MAIL1);

open (HEADERFILE, "$header");
@header= <HEADERFILE>;
close(HEADERFILE);

open (FOOTERFILE, "$footer");
@footer = <FOOTERFILE>;
close(FOOTERFILE);

open (STYLEFILE, "$style");
@style= <STYLEFILE>;
close(STYLEFILE);

print "Content-type: text/html\n\n";
print <<html_;
<html>
<head>
<title>$name, Your Account has been created!</title>
@style
</head>
@header

<center><table border=0 cellspacing=1 cellpadding=0 width=600 bgcolor=#000000><tr><td><table border=0 cellspacing=0 cellpadding=5 width=100% bgcolor=#ffffff><tr><td class="tablefont"><b><Center><font color=#cc0000>Success! Account Created Successfully</font></center></b><br>
Congratulations <b>$name</b>!  You have been successfully registered.<br><br>

<CENTER><B><FONT SIZE=+1><A HREF="http://$protecturl" target="_blank"><u>Click here to Enter into the $sitename</u></a></FONT></B></CENTER><BR>
Your username and password are:<BR><BR>
username: <B>$acctid</B><BR>
password: <B>$password</B><BR><BR>(note: these are case sensitive.  Copy and paste for best result)<BR><B>Make Sure that you write your username and password and store it in a safe place.</B><BR><BR>
Or <CENTER><B><FONT SIZE=3><A HREF="http://$acctid:$password\@$protecturl" target="_blank"><u>Click here to Enter if you're having trouble with the above link.</u></a></FONT></B></CENTER><BR>
If you have any questions, feel free to email us at <A HREF="mailto:$adminemail">$adminemail</A>

</td></tr></table></td></tr></table></center>

@footer
</body>
</html>
html_
exit;
}

sub doubleoptin{
$cursor=$dbh->prepare("select acctid from doubleopt where acctid='$acctid'");
$cursor->execute;
$account = $cursor->fetchrow_array;
if ($account eq $acctid)
{
        &error("It looks like account <b>$acctid</b> is already queued.<br><br>Please go back and select a new account name.<br><br>");
}
    $cursor->finish;

$cursor=$dbh->prepare("INSERT INTO doubleopt (acctid,password,name,email,howdid,date,time,ip) VALUES ('$acctid','$password','$name','$email','$howdid','$date','$time','$ip')");
$cursor->execute;
if ($cursor->err != 0)
{
        &error("Cannot connect to database to save parameters - error 1");
}
$cursor->finish;

$cursor=$dbh->prepare("select optinnotify,sitename from settings where acctid='99'");
$cursor->execute;
($optinlet,$sitename) = $cursor->fetchrow_array;
$cursor->finish;
 $dbh->disconnect;

@optinnotify = $optinlet;

$confirmlink = "$cgilocation/confirm.cgi?id=$acctid";

   foreach $optinnotify(@optinnotify) { 
   $optinnotify =~ s/\[NAME\]/$name/gi;
   $optinnotify =~ s/\[EMAIL\]/$email/gi;
   $optinnotify =~ s/\[ACCOUNT\]/$acctid/gi;
   $optinnotify =~ s/\[WEBADDRESS\]/$webaddress/gi;
   $optinnotify =~ s/\[SITENAME\]/$sitename/gi;
   $optinnotify =~ s/\[ADMINEMAIL\]/$adminemail/gi;
   $optinnotify =~ s/\[CONFIRMLINK\]/$confirmlink/gi;
   }

open (MAIL, "|$mailprog -t") || die "Can't open $mailprog!\n";

   print MAIL "To: $email\n";
   print MAIL "From: $adminemail\n";
   print MAIL "Subject: $name, Please confirm your membership.\n\n";
   foreach $optinnotify(@optinnotify) {
   print MAIL "$optinnotify";
   }
close(MAIL);

open (HEADERFILE, "$header");
@header= <HEADERFILE>;
close(HEADERFILE);

open (FOOTERFILE, "$footer");
@footer = <FOOTERFILE>;
close(FOOTERFILE);

open (STYLEFILE, "$style");
@style= <STYLEFILE>;
close(STYLEFILE);

print "Content-type: text/html\n\n";
print <<html_;
<html>
<head>
<title>$name, Please confirm your registration!</title>
@style
</head>
@header

<center><table border=0 cellspacing=1 cellpadding=0 width=600 bgcolor=#000000><tr><td><table border=0 cellspacing=0 cellpadding=5 width=100% bgcolor=#ffffff><tr><td class="tablefont">
<b><Center><font color=#cc0000>Please confirm your registration!</font></center></b><br>

Dear <b>$name</b>!<br><br>  Your registration request has been successfully queued for verification.<br><br>

An email has been sent to <u>$email</u> containing a verification url that you should click on so as to register your account with our system.  Expect to receive that email within a few seconds or minutes.  If it takes more than 15 minutes, please contact us at <a href="mailto:$adminemail">$adminemail</a> and we'll do the needful for you.<br><br>

This confirmation process is our way of verifying that you're not using someone else's email address to register with us.<br><br>

<center><a href="$webaddress">Return to home page.</a></center>

</td></tr></table></td></tr></table></center>

@footer
</body>
</html>
html_
exit;
}

# end of script