/home/desid573/public_html/angel
Edit: /home/desid573/public_html/angel/hook.php (21267B)
query($sql);
if(!$result) {
error_log("find booking error: ".$conn->error);
}
if($result->num_rows > 0) {
if($progress == 1) {
$event = "pickup";
} else {
$event = $json_data['items'][0]['dispatches']['items'][0]['type'];
$audit = $json_data['items'][0]['dispatches']['items'][0]["audit"]["type"];
}
$type = $json_data['type'];
$driver = $json_data['items'][0]['dispatches']['items'][0]['vehicle']['id'];
if($ratings != "") {
$id = $json_data['items'][0]['id'];
$stars = round($json_data['items'][0]['ratings']['items'][0]['ratio'] * 5);
error_log("called ".$stars);
$comment = $json_data['items'][0]['ratings']['items'][0]['comment'];
}
if ($type == "update"){
$row = mysqli_fetch_assoc($result);
$cid = $row['cid'];
$bid = $row['id'];
$spatula_link = $row['spatula_link'];
$ratingsdriver = $row['driver'];
$escapedratingsdriver = mysqli_real_escape_string($conn, $ratingsdriver);
if($row['cid'] != "") {
// $company = "FamCab";
// $authkey = "AAAAqi4vwgY:APA91bF2FTZV_AmYn6tI3rUZtQEejPY7F3E6LZcKuw20s1R3xWZr7AcnHQTOiGBFKMyaOAUJBeGlqxvpc8HYnWClwpw-AWwwKZOa0bsNJp9kW5JYVrF8g_LlnRJU0zNpt_Pc-DUv0uYH";
error_log("Booking found");
$result = mysqli_query($conn, "SELECT name, mobile, firebase_token FROM customers WHERE id='".$cid."' ORDER BY name DESC LIMIT 0,1");
if(!$result) {
error_log("FA customer error: ".$conn->error);
}
$row = mysqli_fetch_assoc($result);
$custname = $row['name'];
error_log($custname);
$custphone = $row['mobile'];
$firebase_token = $row['firebase_token'];
$names = explode(" ",$custname);
$custfirstname = $names[0];
if($driver != "") {
$result = mysqli_query($conn, "SELECT driver, driver_phone FROM drivers WHERE id='".$driver."'");
} else {
$result = mysqli_query($conn, "SELECT driver, driver_phone FROM drivers WHERE driver='".$escapedratingsdriver."'");
}
$row = mysqli_fetch_assoc($result);
$drivername = $row['driver'];
$drivername = mysqli_real_escape_string($conn, $drivername);
$driverphone = $row['driver_phone'];
}
$slackURL = 'https://hooks.slack.com/services/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';
if ($event == "dispatched"){
$slackmessage = "$drivername has been assigned $custname";
error_log("operator worked");
$result = mysqli_query($conn, "UPDATE booking SET driver='".$drivername."', status='dispatched' WHERE spatula_id='".$id."'");
if($firebase_token != "") {
$title = "Driver has been assigned";
$body = "Driver is ".$drivername;
$gcm = sendGCM($authkey, $title, $body, $firebase_token, $bid);
}
if($audit == "offer") {
$slackmessage = "Offered job ".$custname." has been accepted by ".$drivername;
sendToSlack($slackURL, $slackmessage);
}
}
if ($event == "returned"){
$slackmessage = "$drivername has been removed from $custname";
$result = mysqli_query($conn, "UPDATE booking SET driver='', status='confirmed' WHERE spatula_id='".$id."'");
if($audit == "driver") {
$slackmessage = "Returned job ".$custname." from ".$drivername;
sendToSlack($slackURL, $slackmessage);
}
}
if ($event == "underway"){
$extra_message = "";
if($firebase_token != ""){
$result = mysqli_query($conn, "UPDATE booking SET driver='".$drivername."', status='underway' WHERE spatula_id='".$id."'");
//-- Look up driver coordinates
$result = mysqli_query($conn, "SELECT pickup_lat, pickup_long FROM booking WHERE spatula_id='".$id."' ");
$get = mysqli_fetch_assoc($result);
$lat2 = $get['pickup_lat'];
$lng2 = $get['pickup_long'];
$destination = "$lat2,$lng2";
$time = getETA($driver, $destination, false);
$title = "Your Driver is enroute";
$body = "ETA is ".$time;
$gcm = sendGCM($authkey, $title, $body, $firebase_token, $bid);
if(!$gcm) {
$message = $title."\n".$body;
// sendSMS($custphone, $company, $message);
}
$response = curl_exec($curlObj);
curl_close($curlObj);
$short = "";
$query = "SELECT * FROM customer_tokens WHERE cid='".$cid."'";
$result = $conn->query($query);
if(!$result) {
error_log("Token error: ".$conn->error);
} else {
$row = $result->fetch_assoc();
$modified = $row['modified'];
$modified = strtotime($modified);
$twoWeeks = strtotime("-3 weeks");
if($modified < $twoWeeks) {
$return = token_check($row['access_token_value'], $conn);
$extra_message = $return[0];
$short = $return[1];
} else {
$extra_message = "credit card on file used in last 3 weeks";
$short = "CCA";
}
}
} else {
$time = getETA($driver, $pickup, true);
$message = "Hi ".$custfirstname.",\nYour Driver is en route. ETA is ".$time."\nYou can track their arrival using this link ".$spat_link;
sendSMS($custphone, $company, $message);
if($booking_token != "" && $booking_token != "account") {
$query = "SELECT * FROM customer_tokens WHERE access_token_value='".$booking_token."'";
$result = $conn->query($query);
if(!$result) {
error_log("Token error: ".$conn->error);
} else {
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$modified = $row['modified'];
if($modified != null) {
$modified = strtotime($modified);
$twoWeeks = strtotime("-3 weeks");
if($modified < $twoWeeks) {
$return = token_check($booking_token, $conn);
$short = $return[1];
$extra_message = $return[0];
} else {
$short = "CCA";
$extra_message = "credit card on file used in last 3 weeks";
}
} else {
$return = token_check($booking_token, $conn);
$short = $return[1];
$extra_message = $return[0];
}
} else {
$query = "INSERT INTO customer_tokens (`id`, `cid`, `access_token_value`, `status`, `created`, `modified`) VALUES (NULL, '', ".$booking_token.", 1, NOW(), NULL)";
$result = $conn->query($query);
if(!$result) {
error_log("Token insert error: ".$conn->error);
} else {
$return = token_check($booking_token, $conn);
$short = $return[1];
$extra_message = $return[0];
}
}
}
}
}
$drivermessage = $short." You are on your way to $custname, ".$extra_message;
messageDriver($id, $drivermessage);
//sendSMS($driverphone, "DesiDrivers", $drivermessage);
$slackmessage = $short." ".$drivername." is enroute (".$time.") to ".$custname." ".$extra_message;
sendToSlack($slackURL, $slackmessage);
}
if ($event == "closed"){
if($firebase_token != ""){
$result = mysqli_query($conn, "UPDATE booking SET driver='".$drivername."', status='completed' WHERE spatula_id='".$id."'");
$title = "Fare is completed";
$body = "You can rate and leave feedback in the Closed Bookings tab";
$gcm = sendGCM($authkey, $title, $body, $firebase_token, $bid);
if(!$gcm) {
$message = sprintf($message, $custfirstname);
$sms = sendSMS($custphone, $company, $body);
}
} else {
if($message != ""){
$message = sprintf($message, $custfirstname);
sendSMS($custphone, $company, $message);
if($welcome != "yes"){
$result = mysqli_query($conn, $query4);
}
$query1 = "UPDATE jobs SET driver='".$drivername."' WHERE spat_ref='".$id."'";
$result = mysqli_query($conn, $query1);
}
}
$slackmessage = "$drivername has finished $custname";
sendToSlack($slackURL, $slackmessage);
//send_mail('lewis@desid573ignateddrivers.com.au','info@desid573ignateddrivers.com.au','Webhook', $emailmessage);
}
if($event == "pickup") {
if($firebase_token != ""){
$title = "Driver has arrived";
$body = "You can contact them from the Open Bookings Tab";
$gcm = sendGCM($authkey, $title, $body, $firebase_token, $bid);
if(!$gcm) {
$message = $title."\n".$body;
sendSMS($custphone, $company, $message);
}
} else {
$message = "Hi ".$custfirstname.", your driver ".$drivername." has arrived. You can contact them on ".$driverphone.". Thanks ".$company;
sendSMS($custphone, $company, $message);
}
}
if($ratings != "") {
if($comment == "") {
$comment = "No Comment";
}
$slackmessage = "New Rating for ".$custname."\nDriven By ".$ratingsdriver."\n".$stars." Stars\nComment: ".$comment;
error_log($slackmessage);
$ratingsURL = 'https://hooks.slack.com/services/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';
sendToSlack($ratingsURL, $slackmessage);
}
}
}
function getETA($driver, $destination, $lats) {
require("../vromo.php");
$url = "https://api.fleeteng.com.au/v2/graph/role/vehicles.json?access_token=".$vromo;
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, $url);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($curlObj);
curl_close($curlObj);
$json = json_decode($response, true);
$array = array_filter($json['items'], function($v) use ($driver) {
return $v['id'] == $driver; });
$array = reset($array);
$lat1 = $array['points']['items'][0]['location']['lat'];
$lng1 = $array['points']['items'][0]['location']['lon'];
$origin = "$lat1,$lng1";
//-- check driving time from driver to customer
error_log("destination: ".$destination);
if($lats) {
$dbhost = "localhost";
$dbuser = "desid573_famcab";
$dbpass = "C]+6wQySxov(";
$dbname = "desid573_familycab";
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname) or die($conn->connect_error);
$sql = "SELECT * FROM `desid573_familycab`.`terminal_details` WHERE `name` = '".$destination."'";
$result = $conn->query($sql);
if($result->num_rows > 0) {
$row = $result->fetch_assoc();
$destination = $row["lat"].",".$row["long"];
} else {
$pickplace = getplaceid($destination);
if($pickplace) {
$pickCoords = getCoords($pickplace);
$destination = $pickCoords["lat"].",".$pickCoords["lon"];
} else {
$destination = urlencode($destination);
}
}
}
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=$origin&destinations=$destination&mode=driving&sensor=false&key=AIzaSyCmVfJqsA5tW1ZckILcxa865tWBblxxnzA";
error_log($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response, true);
error_log($response);
$dist = $response_a['rows'][0]['elements'][0]['distance']['text'];
$time = $response_a['rows'][0]['elements'][0]['duration']['text'];
return $time;
}
function send_mail($to, $from, $title, $message)
{
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "From: $from \n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html;charset=ISO-8859-1 \r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: smail-PHP " . phpversion() . "\r\n";
return mail($to, $title, $message, $headers);
}
function sendToSlack($url, $message) {
$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_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, '{"text": "'.$message.'"}');
$response = curl_exec($curlObj);
curl_close($curlObj);
}
function sendSMS($to, $from, $body) {
$account_sid = 'AC7d734c856c0eb7054cfedbb40fc0c6e0';
$auth_token = 'd86defbe663b673bd12ad7f8baf79454';
$client = new Client($account_sid, $auth_token);
try {
$messages = $client->messages->create($to, array('From' => "AngelDrivers", 'body' => $body));
} catch (Exception $e) {
error_log($e);
}
error_log($messages->sid);
if($messages->sid) {
return true;
} else {
try {
$messages = $client->messages->create($to, array('From' => "+61417128124", 'body' => $body));
} catch (Exception $e) {
error_log($e);
}
if($messages->sid) {
return true;
} else {
send_mail("lewis@desid573ignateddrivers.com.au", "error_log@perthdesignateddrivers.com.au", "New Error sending SMS", "Error sending ".$body." to ".$to);
return false;
}
}
}
function sendGCM($authkey, $title, $body, $firebase_token, $bid) {
$headers = array
(
'Authorization: key='.$authkey,
'Content-Type: application/json'
);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, '{ "notification": {
"title": "'.$title.'",
"body": "'.$body.'",
"icon": "myicon",
"sound": "mySound"
},
"to" : "'.$firebase_token.'"
"data": {
"status": "underway"
"bid": "'.$bid.'",
"spatula_link": "'.$spatula_link.'",
"quote": "0"
}
}');
$response = curl_exec($curlObj);
error_log($response);
curl_close($curlObj);
$result = json_decode($response, true);
if($result["success"] == 1) {
return true;
} else {
return false;
}
}
// function token_check($token, $conn)
// {
// require_once 'eway-rapid-php-master/include_eway.php';
// $client_key = '4T7y1vuKPkdhRYUQdH6msueW9hqV2wpcwnSk0Qwxkvwf7TjQn40qymPW7IJb66gUC1w1505cDMkj9IR82juQEnIp4/8nd0nDhwYDSDoqROtNBWmkqtqYmF6F4qVGMUq3HpJrY3y2kdSljCFfWzWc7LIEn4G5XY2IZAPgAO5+mhwvYrricmqwLhqzV4FBUII5AwTvJZl4awrdul4vLWkS73vJuW/XrXHseGK/nqB3oSRluZ1jKp2qDgVTwrXYEbx4rwwS42X2w0C9tLKywxf27WD2CCK8ftSeQBimVqFI0r6VuRXFVaOn6SQTOD9V/TsCfPk5Zh6/PWcfapyVKS+l1w==';
// $apiKey = '44DD7APNUxR8/4sDLhEVT11OS00fmBo4Haxj4ZGJGHvtLBP0rC/ayqNhbVu9tEJrjlJPJm';
// $apiPassword = 'y2b7QMpd';
// $apiEndpoint = \Eway\Rapid\Client::MODE_PRODUCTION;
// $client = \Eway\ Rapid::createClient($apiKey, $apiPassword, $apiEndpoint);
// $transaction = [
// 'Customer' => [
// 'TokenCustomerID' => $token,
// ],
// 'Payment' => [
// 'TotalAmount' => '1',
// ],
// 'TransactionType' => \Eway\Rapid\Enum\TransactionType::RECURRING,
// 'Capture' => false,
// ];
// $response = $client->createTransaction(\Eway\Rapid\Enum\ApiMethod::DIRECT, $transaction);
// if (isset($response->TransactionStatus) && $response->TransactionStatus) {
// $extra_message = "Credit card on file authorised today";
// $query = "SELECT * FROM customer_tokens WHERE access_token_value='".$token."'";
// $result = $conn->query($query);
// if(!$result) {
// error_log("Token error: ".$conn->error);
// } else {
// if ($result->num_rows > 0)
// {
// $row = $result->fetch_assoc();
// $tokencheck = $row['access_token_value'];
// $query = "UPDATE customer_tokens SET modified = NOW() WHERE access_token_value = ".$tokencheck;
// $result = $conn->query($query);
// if(!$result) {
// error_log("Token update error: ".$conn->error);
// }
// } else {
// error_log("No token check required");
// }
// }
// return array($extra_message, "CCA");
// } else {
// $query = "SELECT * FROM desid573_eway_codes.responses WHERE code = '".$response->ResponseMessage."'";
// $result = $conn->query($query);
// if ($result->num_rows > 0)
// {
// $row = $result->fetch_assoc();
// $responseMessage = $row['message'];
// $errormessage = $responseMessage;
// }
// return array("Credit card on file failed ".$errormessage, "CCD");
// }
// }
function getplaceid($place) {
$url = "https://maps.googleapis.com/maps/api/place/findplacefromtext/json?key=AIzaSyCmVfJqsA5tW1ZckILcxa865tWBblxxnzA&inputtype=textquery&input=".rawurlencode($place);
error_log($url);
$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_HTTPHEADER, array('Content-type:application/json'));
$response = curl_exec($curlObj);
error_log($response);
$json = json_decode($response, true);
curl_close($curlObj);
if($json["status"] == "OK") {
return $json["candidates"][0]["place_id"];
} else {
return false;
}
}
function getCoords($placeid) {
$url = "https://maps.googleapis.com/maps/api/place/details/json?fields=name,geometry,place_id,formatted_address&key=AIzaSyCmVfJqsA5tW1ZckILcxa865tWBblxxnzA&placeid=".$placeid;
$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_HTTPHEADER, array('Content-type:application/json'));
$response = curl_exec($curlObj);
//error_log($response);
$json = json_decode($response, true);
curl_close($curlObj);
if($json["status"] == "OK") {
return array("address" => $json["result"]["formatted_address"], "lat" => $json["result"]["geometry"]["location"]["lat"], "lon" => $json["result"]["geometry"]["location"]["lng"]);
} else {
return false;
}
}
function messageDriver($job, $message) {
require("../vromo.php");
$payload = "{'chat': [{'message': '".$message."'}]}";
error_log($payload);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, 'https://api.fleeteng.com.au/v2/graph/role/jobs/'.$job.'?access_token='.$vromo);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $payload);
$response = curl_exec($curlObj);
error_log($response);
$json = json_decode($response, true);
curl_close($curlObj);
}