#!/usr/bin/perl -w
use strict;
use CGI::Carp qw(fatalsToBrowser);
use CGI;
use POSIX;
require SDBM_File;
#
# Define our constants
#
my $sendmail = "/usr/lib/sendmail";
my $adminmail = "test\@test.com";
my($verified, $unverified, $ID);
my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw(! @ $ % ^ & * ) );
#
# Define our dynamic input
#
my $query = CGI->new;
print $query->header;
my %form = %{$query->Vars};
my $accountID = $query->url_param('accountID');
my $accountAD = $query->url_param('accountAD');
my (%dbm1, %dbm2);
my $dbm1 = "unverified.dbm";
my $dbm2 = "verified.dbm";
tie (%dbm1, 'SDBM_File', $dbm1, O_CREAT|O_RDWR, 0644)
|| die "Died tying database\nReason: $!\n";
tie (%dbm2, 'SDBM_File', $dbm2, O_CREAT|O_RDWR, 0644)
|| die "Died tying database\nReason: $!\n";
#
# If form was completed generate an ID, store them to database, email user
#
if ($form{'usermail'}) {
&generate_id;
&email;
print "An email has been sent to $form{'usermail'} for verification.
\n";
$dbm1{$form{'usermail'}} = "$accountID";
}
#
# Or if url param's are present and checked add them to other DB and remove them from $unverified
#
else {
my $unverified = $accountID;
my $verified = "$accountAD";
if ($dbm1{"$accountAD"} && $dbm1{"$accountID"} =~ /^$accountAD$/) {
$dbm2{"$verified"} = "$accountAD";
print "You have been added to the mailing list successfully!\n";
} else {
print "Registration failed!
\n";
print "\$accountID: $accountID .
\n";
print "\$accountAD: $accountAD .
\n";
}
}
sub email {
$accountAD = "$form{'usermail'}";
open (MAIL, "|$sendmail -t") or die "Cannot access mail";
print MAIL "To: $form{'usermail'}\n";
print MAIL "From: $adminmail\n";
print MAIL "Subject: Verify your Email Address\n\n";
print MAIL "http://sulfericacid.perlmonk.org/evs/revised.pl?accountID=$accountID&accountAD=$accountAD\n";
close (MAIL);
}
sub generate_id {
do { $accountID = join '', map { $chars[ rand @chars ] } 1..17; }
while grep {$_ eq $ID} my @used;
print $ID;
# my @unverified_emails=($form{'usermail'}, $accountID);
# $unverified = join "::",@unverified_emails;
#$dbm{"$unverified"}= join "::",@unverified_emails;
#foreach my $mail (split /::/, $dbm{'notverified'}) {
# print "$mail is not verified!\n";
#}
}
untie(%dbm1);
untie(%dbm2);