include('configfile.php');
require_once('recaptchalib.php');
$sent = 'false';
if(isset($_POST['submit']))
{
//Check email
$email = trim(strip_tags($_POST['email']));
if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
{
//Check name
$name = trim(strip_tags($_POST['name']));
$nlength = strlen($name);
if($nlength > 0 and $nlength < 100)
{
//Check Phone
$phone = trim(strip_tags($_POST['phone']));
if(strlen($phone) != 0 and is_numeric($phone) == false)
{
$errmsg = 'Invalid phone number, please check';
$captchacode = recaptcha_get_html($publickey);
}else{
//Check Time of Preferable Showing
$timeofps = trim(strip_tags($_POST['timeofps']));
$timeofpslength = strlen($timeofps);
if($timeofpslength == 0 or $timeofpslength > 500)
{
$errmsg = 'You need to especify time of preferable showing';
$captchacode = recaptcha_get_html($publickey);
}else{
//Check Captcha
$resp = recaptcha_check_answer ($privatekey,$_SERVER["REMOTE_ADDR"],$_POST["recaptcha_challenge_field"],$_POST["recaptcha_response_field"]);
if ($resp->is_valid) {
if($phone == ''){$phone = 'Not provided';}
//Get date
$date = date("Y-m-d H:i:s");
//Send to DB
mysql_connect($mysql_server,$mysql_username,$mysql_password);
mysql_select_db($mysql_database);
mysql_query("INSERT INTO $bookapp_tablename (name, email, phone,time_of_preferable_showing, mls, date) values ('$name','$email','$phone','$timeofps','$_POST[mls]','$date')");
//Send email notification
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->Host = $emailhost;
$mail->From = $ba_from_email;
$mail->FromName = $ba_from_name;
$mail->Subject = $ba_email_subject;
$mail->AddAddress($email,$name);
$body = str_replace('[##NAME##]',$name,$ba_notifmessage_html);
$mail->Body = $body;
$altbody = str_replace('[##NAME##]',$name,$ba_notifmessage_plaintext);
$mail->AltBody = $altbody;
$mail->Send();
$sent = 'true';
//Notify Realtor
send_notification('book_appointment',$_POST[mls],$name,$_POST['email'],$phone);
}else{
$errmsg = 'Invalid captcha, please try again';
$captchacode = recaptcha_get_html($publickey);
}
}
}
}else{
$errmsg = 'Invalid name, please check';
$captchacode = recaptcha_get_html($publickey);
}
}else{
$errmsg = 'Invalid email, please check';
$captchacode = recaptcha_get_html($publickey);
}
$errmsg = '
'.$errmsg.'
';
$mls = $_POST['mls'];
$pic = $_POST['cp'];
}else{
$mls = $_GET['mls'];
$pic = $_GET['cp'];
//Recaptcha
$captchacode = recaptcha_get_html($publickey);
}
function send_notification($type,$mls,$name,$email,$phone)
{
include('simple_html_dom.php');
$enlace = 'http://www.realtor.ca/PropertyResults.aspx?Mode=5&id='.$mls;
$html = str_get_html(get_file($enlace,'http://www.realtor.ca/PropertyResults.aspx'));
$table = $html->find('table[class=SubTable]',0);
if($table)
{
foreach($table->find('a') as $a)
{
if(strpos($a->plaintext,'Email REALTOR') !== false)
{
//Get Key
$aux = explode(',',$a->href);
$key = str_replace("'",'',$aux[1]);
//Get ID
$aux2 = explode('(',$aux[0]);
$id = $aux2[1];
//Get Mode
$mode = $aux[3];
//Get PropertyID
$illegalchars = array("'",')',';');
$pid = trim(str_replace($illegalchars,'',$aux[4]));
//Create email page link
$html->clear();unset($html);
$emailink = 'http://www.realtor.ca/EmailRealtor.aspx?ID='.$id.'&Key='.$key.'&RefererenceNumber='.$mls.'&PropertyID='.$pid.'&Mode='.$mode.'&Title=EmailRealtor&Type=1';
//Go to email page
$html = str_get_html(get_file($emailink,$enlace));
//Get VIEWSTATE code
$viewstate = $html->find('input[id=__VIEWSTATE]',0)->value;
//Get EVENTVALIDATION
$eventvalidation = $html->find('input[id=__EVENTVALIDATION]',0)->value;
//Set option [Contact realtor or Book Appointment]
if($type == 'contact_realtor'){$option = '1';}else{$option = '2';}
//Prepare post data
$post_data = '__VIEWSTATE='.urlencode($viewstate).'&_ctl0%3AtxtYourName='.urlencode($name).'&_ctl0%3AtxtYourEmail='.urlencode($email).'&_ctl0%3AtxtAreaCode=&_ctl0%3AtxtContactPhoneNumber='.urlencode($phone).'&_ctl0%3AchkOption'.$option.'=on&_ctl0%3AtxtComments=&_ctl0%3AtxtCaptchaCode=&_ctl0%3AimgSubmit.x=27&_ctl0%3AimgSubmit.y=3&__EVENTVALIDATION='.urlencode($eventvalidation);
//Send data
$postlink = 'http://www.realtor.ca/EmailRealtor.aspx?ID='.urlencode($id).'&Key='.urlencode($key).'&RefererenceNumber='.urlencode($mls).'&PropertyID='.urlencode($pid).'&Mode=3&Title=EmailRealtor&Type=1';
$result = postdata($postlink,$post_data,$emailink);
break;
}
}
}else{
//The property is not on Realtor anymore. Notify user
unset($mail);
global $emailhost;
global $np_from_email;
global $np_from_name;
global $np_email_subject;
global $np_notifmessage_html;
global $np_notifmessage_plaintext;
$mail = new PHPMailer();
$mail->Host = $emailhost;
$mail->From = $np_from_email;
$mail->FromName = $np_from_name;
$mail->Subject = $np_email_subject;
$mail->AddAddress($email,$name);
$body = str_replace('[##NAME##]',$name,$np_notifmessage_html);
$body = str_replace('[##MLS##]',$mls,$body);
$mail->Body = $body;
$altbody = str_replace('[##NAME##]',$name,$np_notifmessage_plaintext);
$altbody = str_replace('[##MLS##]',$mls,$altbody);
$mail->AltBody = $altbody;
$mail->Send();
//Delete row from database
if($type == 'contact_realtor')
{
$id = mysql_insert_id();
mysql_query("DELETE FROM $contactr_tablename WHERE id LIKE '$id'");
}else{
mysql_query("DELETE FROM $bookapp_tablename WHERE id LIKE '$id'");
}
}
}
function get_file($url,$ref){
$url = str_replace(' ','%20',$url);
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt ($ch, CURLOPT_COOKIE, 'LegalDisclaimer=1');//Bypass disclaimer page
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
if($ref != ''){curl_setopt($ch,CURLOPT_REFERER,$ref); }
$result = curl_exec ($ch);
curl_close ($ch);
return $result;
}
function postdata($where,$what,$ref)
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $where);
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_REFERER,$ref);
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt ($ch, CURLOPT_POSTFIELDS, $what);
$result = curl_exec ($ch);
return $result;
}
?>
Book Appointment (MLS: echo $mls;?>)