#!/usr/bin/perl

use DBI;
use CGI qw(:standard);
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;

print "Content-type: text/html\n\n";

$| = 1;
$sleeptime = 1;
$maxretries = 20;
$myname = "NS1.DOLLARDOMAINNAME.COM";

$affid = param("affid");

($affid ne "") || &msgout("lostpassword.html","");

$driver = "mysql";
$database = "dd1";
$hostname = "198.50.218.97";
$port = 3306;
$dsn = "DBI:$driver:database=$database;host=$hostname;port=$port";

$dbh = DBI->connect($dsn, "dd1", "rep\@rtition");
$drh = DBI->install_driver($driver);

$sth = $dbh->prepare("SELECT * from affiliates WHERE AffID=\"$affid\"");
$sth->execute;
$affid = '';
if (($numrows = $sth->rows)){
    $row = $sth->fetchrow_hashref;
    $contactemail = $row->{'Email'};
    $affid = $row->{'AffID'};
    $recpassword= $row->{'Password'};
}
$msg = "Your password has been emailed to '$contactemail'. Please note that it may take some time for you to receive it.";
if ($affid eq ""){
    $msg = "<FONT color=red>We do not have that Affiliate ID in our database.</font><BR><BR>";
} elsif ($contactemail eq ""){
    $msg = "<FONT color=red>We do not have any email address listed in our database for that Affiliate.</font><BR><BR>";
} else {
    &emailpass($affid, $contactemail, $recpassword);
}
&msgout("lostpassword.html", $msg);

$sth->finish;
$dbh->disconnect();
print "<!-- All done -->\n";
# END MAIN

sub mycrypt{
    local ($pass) = shift;
    local ($salt) = shift;
    
    local ($res, $pass2);
    ($salt ne "") || ($salt = localtime);
    $res = crypt($pass, $salt);
    if (length($pass) > 8){
	$pass2 = substr($pass, 8);
	$res = crypt($res, $pass2);
    }
    return $res;
}
# end mycrypt()

sub msgout{
    local ($filename) = shift;
    local ($msg) = shift;

    open(LF, "<$filename");
    while(<LF>){	
	$line = $_;
	$line =~ s/\<\!--MESSAGE--\>/$msg/;
	print "$line";
    }
    close(LF);
    exit;
}
# end msgout()

sub error{
    local ($msg) = shift;

    open(LF, "<std_error.html");
    while(<LF>){	
	$line = $_;
	$line =~ s/\<\!--ERRORMESSAGE--\>/$msg/;
	print "$line";
    }
    close(LF);
    exit;
}
# end error()


sub emailpass {
    local($affid) = shift;
    local($to_email) = shift;
    local($pass) = shift;

    my $mailprog = "/usr/sbin/sendmail -t -O ErrorMode=q";
    my $whoisprog = "/usr/bin/whois.actual -h whois.registrars.com";

    ########################################################
    # Specify email addresses below
    ########################################################

#	my $from_email_address = "info\@$aff_dom.$aff_ext";
#	my $replyto_email_address = "info\@$aff_dom.$aff_ext";
    my $from_email_address = "info\@dollardomainname.com";
    my $replyto_email_address = "info\@dollardomainname.com";

    if ($affid eq "" || $to_email eq "") {
	return;
    }
    open(MF, "| $mailprog");
    print MF <<ENDPRINT;
From: $from_email_address
To: $to_email
Reply-to: $replyto_email_address
Subject: Lost Password Retrieval

As requested, here is the password to access your Affiliate Panel.
Affiliate ID : $affid
Password     : $pass

You may access your Affiliate Panel at http://www.dollardomainname.com/affiliate

Thank you for your business!

ENDPRINT
	close(MF);
}
# end emailpass()

