#!/usr/bin/perl



require "config.idx";



$long = $ENV{'QUERY_STRING'};

if ($long) {
	@pairs=split(/&/,$long);
	foreach $item(@pairs) {
		($name,$content)=split (/=/,$item,2);
		$content=~tr/+/ /;
		$content=~ s/%(..)/pack("c",hex($1))/ge;
		if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$content; }
		else { $INPUT{$name} = $content; }
	}
}
else {
	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	@pairs = split(/&/, $buffer);
	foreach $pair (@pairs) {
		($name, $value) = split(/=/, $pair);
		$value =~ tr/+/ /;
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$value; }
		else { $INPUT{$name} = $value; }
	}
}





$d_name = "muncieml_db1";
$d_username = "muncieml_db1";
$d_pass = "\@rockdb1";

unless ($INPUT{FirstName} && $INPUT{LastName} && $INPUT{Email} && $INPUT{Password}) {
&header;
print "First name, last name, email address, and password required.";
&footer;
exit 0;
}

use DBI;
my $dsn = "DBI:mysql:database=$d_name;host=localhost";
my $dbh = DBI->connect($dsn, $d_username, $d_pass, {RaiseError => 1}) || die $DBI::errstr;

$INPUT{Email} =~ s/\"/CHR2/gi;
$INPUT{Email} =~ s/\'/CHR1/gi;

my $uasth = $dbh->prepare("select email from users where email = '$INPUT{Email}");
$uasth->execute;
while (($email) = $uasth->fetchrow_array()) {
}

if ($email eq "$INPUT{Email}") {
print "Sorry, you have already registered for a MuncieMLS.com MyHomes Account";
exit 0;
}
else {

$INPUT{FirstName} =~ s/\"/CHR2/gi;
$INPUT{FirstName} =~ s/\'/CHR1/gi;
$INPUT{LastName} =~ s/\"/CHR2/gi;
$INPUT{LastName} =~ s/\'/CHR1/gi;
$INPUT{Address} =~ s/\"/CHR2/gi;
$INPUT{Address} =~ s/\'/CHR1/gi;
$INPUT{City} =~ s/\"/CHR2/gi;
$INPUT{City} =~ s/\'/CHR1/gi;
$INPUT{State} =~ s/\"/CHR2/gi;
$INPUT{State} =~ s/\'/CHR1/gi;
$INPUT{Zip} =~ s/\"/CHR2/gi;
$INPUT{Zip} =~ s/\'/CHR1/gi;
$INPUT{Password} =~ s/\"/CHR2/gi;
$INPUT{Password} =~ s/\'/CHR1/gi;
$INPUT{TimeToMove} =~ s/\"/CHR2/gi;
$INPUT{TimeToMove} =~ s/\'/CHR1/gi;
$INPUT{realtor} =~ s/\"/CHR2/gi;
$INPUT{realtor} =~ s/\'/CHR1/gi;

$oasth = $dbh->do("insert into users values ('','$INPUT{Email}',PASSWORD('$INPUT{Password}'),'$INPUT{FirstName}','$INPUT{LastName}','$INPUT{Address}','$INPUT{City}','$INPUT{State}','$INPUT{Zip}','$INPUT{TimeToMove}','$INPUT{realtor}',now(),now())");

my $pasth = $dbh->prepare("select last_insert_id()");
$pasth->execute();

while (($lastnumber) = $pasth->fetchrow_array()) {
$thelastnumber = $lastnumber;
}

$dbh->disconnect();


open(MAIL,"|/usr/sbin/sendmail -t");
print MAIL "To: ryan.orr\@lazula.net\n";
print MAIL "From: $INPUT{Email}\n";
print MAIL "Subject: **** NEW MuncieMLS.com User! ****\n\n";
print MAIL "The following user has joined MuncieMLS.com\n\n\n$INPUT{FirstName} $INPUT{LastName},\n$INPUT{Address}\n$INPUT{City}, $INPUT{State} $INPUT{Zip}\nTime To Move: $INPUT{TimeToMove}\nWorking With A Realtor? $INPUT{realtor}\n\n\nThis e-mail was automatically generated for the MuncieMLS.com Registration System.";
close(MAIL);

open(MAIL,"|/usr/sbin/sendmail -t");
print MAIL "To: patrealtor\@aol.com\n";
print MAIL "From: $INPUT{Email}\n";
print MAIL "Subject: **** NEW MuncieMLS.com User! ****\n\n";
print MAIL "The following user has joined MuncieMLS.com\n\n\n$INPUT{FirstName} $INPUT{LastName},\n$INPUT{Address}\n$INPUT{City}, $INPUT{State} $INPUT{Zip}\nTime To Move: $INPUT{TimeToMove}\nWorking With A Realtor? $INPUT{realtor}\n\n\nThis e-mail was automatically generated for the MuncieMLS.com Registration System.";
close(MAIL);

open(MAIL,"|/usr/sbin/sendmail -t");
print MAIL "To: ryano\@page-us.com\n";
print MAIL "From: $INPUT{Email}\n";
print MAIL "Subject: ...";
print MAIL "$INPUT{FirstName} $INPUT{LastName} Has Joined MuncieMLS.com!";
close(MAIL);

print "Set-cookie: userid=$thelastnumber\-$INPUT{Email}\n";
print "Content-type: text/html\n\n";

print qq~
<html><head><title>Logging in...</title>
<meta http-equiv="Refresh" content="2; URL=http://www.munciemls.com/myhomes.idx">
</head>
<body>
<br><br><br><center><font face=verdana size=4>Logging in...</font><br><font face=Tahoma size=2>If you are not redirected in 5 seconds, <a href="myhomes.idx">click here</a>.
~;
}