#!/usr/bin/perl

require "config.idx";




use CGI;
my $query = new CGI;
my $notify = $query->param('notify');
my $shortdesc = $query->param('shortdesc');
my $action = $query->param('action');
my $name = $query->param('name');
my $matchid = $query->param('matchid');
my $matchcat = $query->param('matchcat');

my($file_query, $file_name, $size, $buff, $time, $bytes_count);
$size = $bytes_count = 0;

$_ = $file_query = $query->param('FILE1');

	s/\w://;
	s/([^\/\\]+)$//;
	$_ = $1;
	s/\.\.+//g;
	s/\s+//g;
	$file_name = $_;


$file_name =~ m/\.([^.]+)$/;
$file_ext = $1;
$file_ext = lc($file_ext);

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;

$file_data = "";

	my $usrdasth = $dbh->prepare("select firstname,lastname from users where userid = '$Cook{userid}'");
	$usrdasth->execute;
	while (($afirstname,$alastname) = $usrdasth->fetchrow_array()) {
	$generateaddedby = "$afirstname $alastname";
	}

while ($bytesread=read($query->param('FILE1'),$buffer,1024)) {
	$file_data .= $buffer;
}



		$sql_query = qq~insert into documents values ('',?,?,?,?,?,now(),?,?,?)~;
		@variables = ("$Cook{agentid}","$name","$file_name",$file_ext,"$file_data","$generateaddedby","$matchid","$matchcat");
		my $gasth = $dbh->do($sql_query, undef, @variables);

if ($matchcat eq "t") {
$matchcatname = "Transaction";
}
elsif ($matchcat eq "c") {
$matchcatname = "Contact";
}
elsif ($matchcat eq "p") {
$matchcatname = "Prospect";
}

if ($notify eq "on") {

		my $agentasth = $dbh->prepare("select firstname,lastname,email from agents where mlsid = '$Cook{agentid}' order by agentid ASC LIMIT 0,1");
		$agentasth->execute;
		while (($l3agent_firstname,$l3agent_lastname,$l3agent_email) = $agentasth->fetchrow_array()) {

					open(MAIL,"|/usr/sbin/sendmail -t");
					print MAIL "To: $l3agent_email\n";
					print MAIL "From: Info\@ORRHomes.com\n";
					print MAIL "Subject: Document '$name' Uploaded for $shortdesc\n";
					print MAIL "Content-type: text/html\n\n";
					print MAIL qq~
					<html>
					<head>
					<title>Eagle Real Estate, Inc.</title>
					<link rel="stylesheet" TYPE="text/css" href="http://www.munciemls.com/includes/style.css">
					</head>
					<body bgcolor="white" leftMargin="0" topMargin="0" marginwidth="0" marginheight="0">
					<br><br>
					Document '$name' was just uploaded for $matchcatname $shortdesc by $generateaddedby.<br><br>
                    Document can be downloaded and viewed at: <a href="http://Connect.EagleSold.com">http://Connect.EagleSold.com</a>.
					</body>
					</html>
					~;
					close(MAIL);

		}
}

if ($matchcat eq "t") {
print qq~Location: http://connect.eaglesold.com/transactions.idx?action=overview_documents&transid=$matchid\n\n~;
}
elsif ($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~;
}


