<?php
if(isset($_REQUEST['to'])) {$to= $_REQUEST['to'];} ;
if(isset($_REQUEST['from'])) {$from= $_REQUEST['from'];} ;
if(isset($_REQUEST['message'])) {$message= $_POST["message"];} ;
if(isset($_REQUEST['reference'])) {$reference= $_REQUEST['reference'];};
if(isset($_REQUEST['check'])) {$check = $_REQUEST['check'];};

if(!empty($check) && !empty($reference) && !empty($from)) {
if($from == "FamilyCab"){
			$dbhost = "localhost";
	    		$dbuser = "perthdes_famcab";
	    		$dbpass = "C]+6wQySxov(";
	    		$dbname = "perthdes_familycab";
	    		$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname) or die($conn->connect_error);	    		
			$result = mysqli_query($conn, "SELECT conf_sms_sent FROM jobs WHERE reference='".$reference."' ORDER BY conf_sms_sent DESC LIMIT 0,1");
			$row = mysqli_fetch_assoc($result);
        			$sent = $row['conf_sms_sent'];
        		if($sent == "yes") {
        		echo "yes";
        		} else {
        		echo "no";
        		}	
			
			}
if($from == "DesiDrivers"){
			$dbhost = "localhost";
	    		$dbuser = "perthdes_inquiri";
	    		$dbpass = "@G&{)0*&Ts^V";
	    		$dbname = "perthdes_inquiries";
	    		$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname) or die($conn->connect_error);	    		
	    		$result = mysqli_query($conn, "SELECT conf_sms_sent FROM jobs WHERE reference='".$reference."' ORDER BY conf_sms_sent DESC LIMIT 0,1");
			$row = mysqli_fetch_assoc($result);
        			$sent = $row['conf_sms_sent'];
        		if($sent == "yes") {
        		echo "yes";
        		} else {
        		echo "no";
        		}	
			
	    		}
}


if(!empty($to) && !empty($from) && !empty($message))
{
    
    $url = "https://api.smsbroadcast.com.au/api-adv.php?username=lewiscann&password=HNVcGTm5&to=$to&from=$from&message=$message&maxsplit=5";
	$curlObj = curl_init();
	curl_setopt($curlObj, CURLOPT_URL, $url);
	curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($curlObj, CURLOPT_HEADER, 0);
	curl_setopt($curlObj, CURLOPT_POST, 1);

	$response = curl_exec($curlObj);

	curl_close($curlObj);
    
    	list($status, $phone, $smsreference) = explode(":", $response);
    
    echo $status;
    
    if($status == "OK"){

if($from == "FamilyCab"){
			$dbhost = "localhost";
	    		$dbuser = "perthdes_famcab";
	    		$dbpass = "C]+6wQySxov(";
	    		$dbname = "perthdes_familycab";
	    		$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname) or die($conn->connect_error);	    		
			$query = mysqli_query($conn, "UPDATE jobs SET conf_sms_sent='yes' WHERE reference='".$reference."'");
			if (!$query) {
    		die("Couldn't enter data: ".$conn->error);
    		}
			
			}
if($from == "DesiDrivers"){
			$dbhost = "localhost";
	    		$dbuser = "perthdes_inquiri";
	    		$dbpass = "@G&{)0*&Ts^V";
	    		$dbname = "perthdes_inquiries";
	    		$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname) or die($conn->connect_error);	    		
	    		$query = mysqli_query($conn, "UPDATE jobs SET conf_sms_sent='yes' WHERE reference='".$reference."'");
	    		if (!$query) {
    		die("Couldn't enter data: ".$conn->error);
    		}
	    		}
}
    
    
}

	    		
	    		
	    		