#!/usr/local/bin/perl

use CGI;
use DBI;
use Net::SMTP;

use strict;

REQUEST:
foreach my $q (new CGI)
{

   if ($ENV{REQUEST_METHOD} eq 'POST')
   {
      my $dbh = DBI->connect('dbi:Oracle:geekdb', 'robf', 'ycgsfn', { AutoCommit => 1 } );

      my $email = $q->param('email');
      my $name = $q->param('name');

      my $done = 0;

      if ($email)
      {
         my ($exists) = $dbh->selectrow_array("
		select count(*)
		from genealogy_users
		where lower(username) = :1
		", undef, lc($email) );

         my ($password, $status);

         if ($exists)
         {
            ($password, $name, $status) = $dbh->selectrow_array("
		select password, fullname, status
		from genealogy_users
		where lower(username) = :1
		", undef, lc($email) );

            if ($status eq 'expired')
            {
               printf STDERR "GEN: UnExpiring user: %s\n", $email;
               my $result = $dbh->do("
		update genealogy_users
		set status = 'active'
		where lower(email) = lower(:1)
		", undef, $email);

            }

            &send_email($email, $name, $password);
            $done = 1;
         }
         elsif ($name)
         {
            $password = &random_password;

            $dbh->do("
		insert into genealogy_users
			(username, email, fullname, password, status, create_date)
		values (:1, :1, :2, :3, 'active', sysdate)
		", undef, $email, $name, $password);

            printf STDERR "GEN: New user: %s\n", $email;

            &send_email($email, $name, $password);
            $done = 1;
         }

      }

      if ($done)
      {
         print $q->header, $q->start_html(
		-title=>'Private Database Access Application',
		-author=>'robf@geekthing.com',
		);

         print "
<center>
<h1>Application Accepted</h1>

Thank you for your interest.  Please be sure to contribute
any data you might have.
<p>

You should receive your login info soon.
<p>

Click <a href=/~robf/genealogy/>here</a> to return to my Genealogy page.
<p>

";

         print "</center><hr>", $q->end_html, "\n";

         next REQUEST;
      }
   }

   print $q->header, $q->start_html(
	-title=>'Private Database Access Application',
	-author=>'robf@geekthing.com',
	);

   printf "
<center>
<h1>Private Database Access Application</h1>

Use this form to gain access to my private genalogy database.

<p>

Enter your email address and full name below, and an account will be
created for you.  <b>Both fields are required.</b>  Instructions on
logging in will be emailed to you.  You must therefore be sure you enter
your email address correctly!

<p>

If you already have access, but have forgotten your password, enter
just your email address below, and your password will be emailed to
you.

<p>

<form method=post>
<table>
<tr>
   <td align=right>Email address:</td>
   <td><input type=text name=email size=30 maxlength=100 value=\"%s\"></td>
</tr>
<tr>
   <td align=right>Full Name:</td>
   <td><input type=text name=name size=30 maxlength=100 value=\"%s\"></td>
</tr>
<tr>
   <td colspan=2 align=center><input type=submit value=\" Submit \"><input type=reset></td>
</tr>
</table>

</form>
",
	$q->param('email'),
	$q->param('name'),
	;

   print "<hr></center>\n", $q->end_html, "\n";


}



sub send_email
{
   my $email = shift;
   my $fullname = shift;
   my $password = shift;

   my $sender = 'genealogy@geekthing.com';

   my $message = sprintf "To: %s <%s>
From: Fugina Genealogy Database <%s>
Reply-To: %s
Errors-To: %s
Subject: Genealogy Access

An account has been created to allow you access to Rob Fugina's private
genealogy data.  Start by directing your browser to the following URL:

http://www.geekthing.com/~robf/genealogy/

When you select something that requires authentication, you will be
prompted by your browser for a username and password.  Use the following
information:

Username: %s
Password: %s

This account will be disabled after a period of inactivity.  I have not
yet determined how long that period will be, but I'll give you at least
several months.

If you can provide any additional information or corrections to my
data, or if you have any problems with my site, you may contact me at
genealogy\@geekthing.com.

Thanx,
Rob

",
	$fullname, $email,
	$sender,
	$sender,
	$sender,
	$email,
	$password,
	;

   my $smtp = Net::SMTP->new('mail.geekthing.com',
	Hello => 'www.geekthing.com'
	);							# make connection
   $smtp->mail($sender);					# sender
   $smtp->recipient($email, $sender);				# recipients
   $smtp->data();						# start sending data...
   $smtp->datasend($message);					# message
   $smtp->dataend();						# end
   $smtp->quit;							# disconnect

   printf STDERR "GEN: Sent email to %s\n", $email;
}

sub random_password
{
   my @chars = ('.', '/', 0..9, 'a'..'z');
   my $cnt = @chars;
   join '', @chars[rand $cnt, rand $cnt, rand $cnt, rand $cnt, rand $cnt, rand $cnt];
}
