#!/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; }
	}
}

if ($INPUT{action} eq "logoff" || $INPUT{action} eq "login") {
print "Content-type: text/html\n\n";
}


unless ($INPUT{action} eq "login") {

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

}


$dl_pass = "123";



if ($INPUT{action} eq "create") {
	unless ($INPUT{download_password} eq "$dl_pass") {
print "Content-type: text/html\n\n";
print "YOU HAVE AN INVALID PASSWORD NO DOWNLOAD RIGHTS";
exit 0;
}
else {
$divider = "$INPUT{divider}";
$fileextension = "$INPUT{fileextension}";

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


##############################################################



@days   = ('Sunday','Monday','Tuesday','Wednesday',
               'Thursday','Friday','Saturday');
    @months = ('January','February','March','April','May','June','July',
	         'August','September','October','November','December');


    ($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];
    $time = sprintf("%02d:%02d:%02d",$hour,$min,$sec);
if ($hour eq "13") {
$hour = "1";
$pm = "y";
}if ($hour eq "14") {
$hour = "2";
$pm = "y";
}if ($hour eq "15") {
$hour = "3";
$pm = "y";
}if ($hour eq "16") {
$hour = "4";
$pm = "y";
}if ($hour eq "17") {
$hour = "5";
$pm = "y";
}if ($hour eq "18") {
$hour = "6";
$pm = "y";
}if ($hour eq "19") {
$hour = "7";
$pm = "y";
}if ($hour eq "20") {
$hour = "8";
$pm = "y";
}if ($hour eq "21") {
$hour = "9";
$pm = "y";
}if ($hour eq "22") {
$hour = "10";
$pm = "y";
}if ($hour eq "23") {
$hour = "11";
$pm = "y";
}if ($hour eq "0" || $hour eq "" || $hour eq "24") {
$hour = "12";
}
$newyear = $year+1900;
$time = sprintf("%02d:%02d",$hour,$min);
$tdate = "$days[$wday], $months[$mon] $mday, $newyear";
$mon = $mon + 1;



if ($pm eq "y") {
$tm = "pm";
}
else {
$tm = "am";
}

if ($mon < "10") {
$mon = "0$mon";
}

if ($mday < "10") {
$mday = "0$mday";
}

$actdate = "$newyear\_$mon\_$mday";

print "Content-type: application/octetstream\n";
print "Content-Disposition: filename=\"eagle_transactions\_$actdate\.$fileextension\"\n";
print "Pragma: no-cache\n";
print "Expires: 0\n\n";

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;

####### GET TRANSACTIONS CLIENT IS PART OF #####################

my $aasth = $dbh->prepare("select id,agent,expiredate,mlsnumber,type,status,addressnumber,addressdirection,addressstreet,city,state,zip from trans_board WHERE type = 'Listing' AND (status = 'Active' or status = 'Pending' or status = 'BSP') ORDER BY type");
$aasth->execute;
while (($t_transid,$t_transagent,$t_expiredate,$t_mlsnumber,$t_type,$t_status,$t_addressnumber,$t_addressdirection,$t_addressstreet,$t_addresscity,$t_addresszip) = $aasth->fetchrow_array()) {


### GET AGENT NAME
my $agentasth = $dbh->prepare("select firstname,lastname from agents WHERE mlsid = '$t_transagent' order by agentid asc limit 0,1");
$agentasth->execute;
while (($agent_firstname,$agent_lastname) = $agentasth->fetchrow_array()) {





$t_type =~ s/\,//gi;
$t_type =~ s/\r\n/\;/g;
$t_status =~ s/\,//gi;
$t_status =~ s/\r\n/\;/g;
$t_addressnumber =~ s/\,//gi;
$t_addressnumber =~ s/\r\n/\;/g;
$t_addressdirection =~ s/\,//gi;
$t_addressdirection =~ s/\r\n/\;/g;
$t_addressstreet =~ s/\,//gi;
$t_addressstreet =~ s/\r\n/\;/g;
$t_addresscity =~ s/\,//gi;
$t_addresscity =~ s/\r\n/\;/g;
$t_addresszip =~ s/\,//gi;
$t_addresszip =~ s/\r\n/\;/g;

#### FIND SELLER OR SELLERS
my $cbbaasth = $dbh->prepare("select contactid from trans_boardtranstocontact WHERE transid = '$t_transid' and type = 'Seller'");
$cbbaasth->execute;
while (($tempcontid) = $cbbaasth->fetchrow_array()) {


my $uasth = $dbh->prepare("select firstname,lastname,address,spouse,city,state,zip,phone1type,phone1number,phone2type,phone2number,phone3type,phone3number,faxnumber,email,emailtwo from trans_boardcontact where id = '$tempcontid'");
$uasth->execute;
while (($c_firstname,$c_lastname,$c_address,$c_spouse,$c_city,$c_state,$c_zip,$c_phone1type,$c_phone1number,$c_phone2type,$c_phone2number,$c_phone3type,$c_phone3number,$c_faxnumber,$c_email,$c_emailtwo) = $uasth->fetchrow_array()) {

$c_firstname =~ s/\,//gi;
$c_lastname =~ s/\,//gi;
$c_spouse =~ s/\,//gi;
$c_address =~ s/\,//gi;
$c_city =~ s/\,//gi;
$c_state =~ s/\,//gi;
$c_zip =~ s/\,//gi;
$c_phone1type =~ s/\,//gi;
$c_phone1number =~ s/\,//gi;
$c_phone2type =~ s/\,//gi;
$c_phone2number =~ s/\,//gi;
$c_phone3type =~ s/\,//gi;
$c_phone3number =~ s/\,//gi;
$c_faxnumber =~ s/\,//gi;
$c_email =~ s/\,//gi;
$c_emailtwo =~ s/\,//gi;

if ($c_spouse ne "") {
$c_spouse = " and $c_spouse";
}
else {
$c_spouse = "";
}


$fieldformat = "$t_type$divider$t_status$divider$agent_firstname $agent_lastname$divider$t_expiredate$divider$t_mlsnumber$divider$t_addressnumber$divider$t_addressdirection$divider$t_addressstreet$divider$t_addresscity$divider$t_addresszip$divider$c_firstname$c_spouse$divider$c_lastname$divider$c_address$divider$c_city$divider$c_state$divider$c_zip$divider$c_phone1type$divider$c_phone1number$divider$c_phone2type$divider$c_phone2number$divider$c_phone3type$divider$c_phone3number$divider$c_faxnumber$divider$c_email$divider$c_emailtwo\r\n";

print "$fieldformat";


} # END CONTACT
} # END TRANS TO CONTACT
} # END AGENT INFO
} # END TRANSACTION

#########################


$dbh->disconnect;
}
}
else {

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


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;

print qq~
<html>
<head>
<title>Eagle Real Estate, Inc. - Export Transactions - Connect.EagleSold.com</title>
~;
&header;

print qq~
<td valign="top" width="581">
<img src="images/headers/transaction_manager.gif" height="33" width="581" alt="My Transaction Manager"><br><br>
<b><font size="4">Export Your Transactions</font></b><br><br>
&nbsp;&nbsp;&nbsp;&nbsp;This will allow you to export your transactions so you may import them into other contact management programs such as Microsoft Outlook.  Once you click on Build Export File Now button you will be asked to save or open the new file.  You may then save the file, load your contact management program and Import your information.
<form method="post" action="new_export_transactions.idx">
<input type="hidden" name="action" value="create">
<input type="hidden" name="agent" value="$INPUT{agent}">
<b>Disclaimer</b><br>
		<table border="1" cellpadding="3" cellspacing="1" width="100%" bgcolor="black">
		<tr>
		<td valign="top" bgcolor="white">
		<table border="0" cellpadding="1" cellspacing="0" width="100%">
		<tr>
		<td valign="top" bgcolor="white" width="100%">
		This file is property of Eagle Real Estate, Inc.   by clicking "I Agree Below" you agree you will not copy or distrubute this file without the express written permission from Eagle Real Estate, Inc.
 		</td>
		</tr>
		</table>
		</td>
		</tr>
		</table><br>
<b>Choose Search/Download Options</b><br>
		<table border="1" cellpadding="3" cellspacing="1" width="100%" bgcolor="black">
		<tr>
		<td valign="top" bgcolor="white">
		<table border="0" cellpadding="1" cellspacing="0" width="100%">
		<tr>
		<td valign="top" bgcolor="white" width="100%">
		No Options Available At This Time
 		</td>
		</tr>
		</table>
		</td>
		</tr>
		</table><br>
<b>Choose how you want your file created</b><br>
		<table border="1" cellpadding="3" cellspacing="1" width="100%" bgcolor="black">
		<tr>
		<td valign="top" bgcolor="white">
		<table border="0" cellpadding="1" cellspacing="0" width="100%">
		<tr>
		<td valign="top" bgcolor="white">
		<font size="2"><b>File Type</b> </font><select name="fileextension" class="form"><option value="csv">[ Excel ] *.csv</option><option value="txt">[ Text ] *.txt</option></select><br><br>
		<font size="2"><b>Divider Type</b> </font><select name="divider" class="form"><option value="\,">[ Comma Seperated Values ] Ex: firstname,lastname</option><option value="\|">[ Pipe Delimited ] Ex: firstname|lastname</option><option value="\\t">[ Tab Delimited ] Ex: firstname	lastname</option></select><br><br>
		</td>
		</tr>
		</table>
		</td>
		</tr>
		</table><br>
<b>Enter Download Password</b><br>
		<table border="1" cellpadding="3" cellspacing="1" width="100%" bgcolor="black">
		<tr>
		<td valign="top" bgcolor="white">
		<table border="0" cellpadding="1" cellspacing="0" width="100%">
		<tr>
		<td valign="top" bgcolor="white">
		<input type="password" name="download_password" class="form"><br>
		</td>
		</tr>
		</table>
		</td>
		</tr>
		</table><br>
<input type="submit" value="I Agree [Build Your Export File Now]" class="form">
</form>

</td>
~;
&footer;

}



