#!/usr/bin/perl

$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; }
	}
}

require "config.idx";

@rawCookies = split(/;/,$ENV{'HTTP_COOKIE'});
foreach (@rawCookies) {
	($cookieName, $cookieValue) = split(/=/,$_);
	$cookieName =~ s/ //gi;
	$Cookies{$cookieName}=$cookieValue;
}

if ($Cookies{userid} =~ /\|/) {
($Cook{userid},$Cook{email}) = split(/\|/,$Cookies{userid});
}
else {
($Cook{userid},$Cook{email}) = split(/\-/,$Cookies{userid});
}

if ($Cook{userid} eq "" || $Cook{userid} eq "none") {


print qq~

<html>
<head>
<title>Login to Connect.EagleSold.com</title>
<link rel="stylesheet" TYPE="text/css" href="http://www.munciemls.com/includes/style.css">
<meta http-equiv="set-cookie" content="userid=; expires=; path=/"> 
</head>
<body bgcolor="white">
<center>
<table border="0" cellspacing="1" cellpadding="3" bgcolor="#CC0000">
	<form method=post action=index.idx>
	<input type=hidden name=action value=login>
<tr>
<td valign="top" colspan="2" bgcolor="white"><center><font size="3" color="maroon" face="Arial"><b>Connect.EagleSold.com</b></font><br><b><i>Access Center</i></b></center></td>
</tr>
<tr>
<td valign="middle" align="right" bgcolor="white"><b>Login</b></td>
<td valign="middle" bgcolor="white"><input type="text" name="Email" size="30" class="form"></td>
</tr>
<tr>
<td valign="middle" align="right" bgcolor="white"><b>Password</b></td>
<td valign="middle" bgcolor="white"><input type="password" name="Password" size="30" class="form"></td>
</tr>
<tr>
<td valign="top">&nbsp;</td>
<td valign="top"><input type="submit" value="Login" class="form"><br></td>
</tr>
</table>
</center>
</body>
</html>
~;
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;

my $basth = $dbh->prepare("select * from documents where did = '$INPUT{did}' ORDER BY dateadded DESC");
$basth->execute;
while ($data_ref = $basth->fetchrow_hashref()) {
%documents = %$data_ref;
if ($Cook{agentid} ne $documents{agent}) {
print qq~
This is not your document!
~;
exit 0;
}

@variables = ($INPUT{did},$Cook{agentid});
$sql_query = "DELETE FROM documents WHERE did = ? AND agent = ?";
my $aasth = $dbh->do($sql_query, undef, @variables);


if ($INPUT{matchcat} eq "t") {
print qq~Location: http://connect.eaglesold.com/transactions.idx?action=overview_documents&transid=$matchid\n\n~;
}
elsif ($INPUT{matchcat} eq "p") {
print qq~Location: http://connect.eaglesold.com/prospects.idx?action=overview_documents_prospect&prospectid=$matchid\n\n~;
}
else {
print qq~Location: http://connect.eaglesold.com/transactions.idx?action=overview_documents_contact&contactid=$matchid\n\n~;
}

exit 0;
}