/home/desid573/public_html/tollster
Edit: /home/desid573/public_html/tollster/11.php (28082B)
query($sql);
if(!$result) {
error_log($conn->error);
}
if ($result->num_rows > 0)
{
$row = $result->fetch_assoc();
$sql = "INSERT INTO key_usage (id, cid, kid, ip_address, date_accessed) VALUES (null, '".$row['cid']."', '".$row['id']."', '".$_SERVER['REMOTE_ADDR']."', NOW())";
$result = $conn->query($sql);
if(!$result) {
error_log($conn->error);
}
$responseArray = processData();
} else {
$responseArray["status"] = "error";
$responseArray["message"] = "API Key not found";
}
} else if(isset($_REQUEST['token']) && $_REQUEST['token'] != "") {
$url = "http://bookings.perthdesignateddrivers.com.au/tollster/google_log_api.php?sku=".$_REQUEST['sku']."&token=".$_REQUEST['token'];
$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);
if($response_a["token_status"] == "OK") {
$responseArray = processData();
} else {
//Swap this when up and running
$responseArray = processData();
//$responseArray["status"] = "error";
//$responseArray["message"] = "Purchase authority not found";
}
} else {
$ip_address = $_SERVER['REMOTE_ADDR'];
$time_limit = strtotime("-24 hours");
$time_now = strtotime("now");
$sql = "SELECT * FROM ip_log WHERE ip_address = '".$ip_address."' AND date_last_accessed > ".$time_limit;
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
$responseArray = processData(); //get rid of this
//$responseArray["status"] = "error";
//$responseArray["message"] = "Reached Maximum Usage Today";
} else {
$sql = "INSERT INTO ip_log (id, ip_address, date_last_accessed) VALUES (null, '".$ip_address."', '".$time_now."')";
$result = $conn->query($sql);
if(!$result) {
error_log($conn->error);
}
$responseArray = processData();
}
}
function processData() {
global $conn, $point1, $point2, $alt, $routes, $state;
$responseArray = [];
$data = [];
$tolls = [];
$entrys = [];
$exits = [];
$route = "";
if(isset($_REQUEST["lineArray"])) {
$tollways = [];
$tollsfound = [];
$sql = "SELECT * FROM tollways WHERE city = '".$state."'";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
if($row['search_string'] != "") {
$tollways[$row['id']] = $row['search_string'];
}
}
}
foreach($routes as $j => $paths) {
$Entrys = [];
$Exits = [];
$Tolls = [];
$polyline_array = [];
foreach($paths as $i => $r) {
array_push($polyline_array, $r["route"]);
foreach($tollways as $key => $value) {
$tollway_strings = explode(",",$value);
foreach($tollway_strings as $tollway_searchString) {
if(strpos(strtolower($r["text"]), strtolower($tollway_searchString)) !== false){
//error_log("found ".$tollway_searchString);
if(array_search($value, $tollsfound) == false) {
$sql = "SELECT * FROM tollways WHERE id = ".$key;
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$name = $row["name"];
}
}
$tollsfound["$key"] = $name;
}
}
}
}
}
error_log(json_encode($tollsfound, true));
foreach($tollsfound as $key => $value) {
$returnData = checkPrice2($polyline_array, $key, $value);
if($returnData != null) {
error_log("returnData ".json_encode($returnData, true));
array_push($Entrys, reset($returnData["entrys"]));
array_push($Exits, reset($returnData["exits"]));
if(count($Tolls) > 0) {
$notFound = true;
foreach($Tolls as $key => $toll) {
error_log($returnData["tolls"]["name"]." ".$toll["name"]);
if($returnData["tolls"]["name"] == $toll["name"]) {
$notFound = false;
$cost = $toll["cost"] + + $returnData["tolls"]["cost"];
if($toll["name"] == "CityLink" && $cost > 9.23) {
$cost = 9.23;
}
$Tolls[$key] = ["name" => $toll["name"], "cost" => $cost];
} else {
}
}
if($notFound) {
array_push($Tolls, $returnData["tolls"]);
}
} else {
array_push($Tolls, $returnData["tolls"]);
}
}
}
if(count($Tolls) > 0){
$data[$j]["tolls"] = $Tolls;
$data[$j]["entrys"] = $Entrys;
$data[$j]["exits"] = $Exits;
} else {
$data[$j]["tolls"] = [];
$data[$j]["entrys"] = [];
$data[$j]["exits"] = [];
}
$Tolls = array();
$Exits = array();
$Entrys = array();
}
$responseArray["status"] = "OK";
$responseArray["message"] = "Success";
$responseArray["data"] = $data;
return $responseArray;
} else if(!empty($point1) && !empty($point2))
{
if($_REQUEST['directions'] == "" OR $alt == "true") {
$dist=GetDirections($point1, $point2);
$searchString = "html_instructions";
} else {
$dist = $_REQUEST['directions'];
$searchString = "instructions";
}
$status = json_decode($dist, true);
$state1 = checkState($status, "start");
$state2 = checkState($status, "end");
if($state1 == "none" OR $state2 == "none") {
$responseArray["status"] = "error";
$responseArray["message"] = "Tolls only available in NSW, VIC and QLD";
echo json_encode($responseArray);
die;
}
if($state1 != $state2) {
$responseArray["status"] = "error";
$responseArray["message"] = "Cannot calculate tolls between states";
//error_log(json_encode($responseArray, true));
echo json_encode($responseArray);
die;
}
$tollways = [];
$tollsfound = [];
$sql = "SELECT * FROM tollways WHERE city = '".$state1."'";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
if($row['search_string'] != "") {
$tollways[$row['id']] = $row['search_string'];
}
}
}
//echo json_encode($tollways, true);
$foundToll = false;
$polyline_array = [];
$globalEntrys = [];
$globalExits = [];
$globalToll = [];
foreach($status['routes'] as $i => $routes) {
$this_polyline = "";
$this_instruction = "";
$polyline_array = [];
$globalEntrys = [];
$globalExits = [];
$globalToll = [];
foreach($routes['legs'][0]['steps'] as $array) {
$last_polyline = $this_polyline;
$last_instruction = $this_instruction;
if(strpos(strtolower($array[$searchString]), 'toll road') !== false){
if($foundToll == false) {
array_push($polyline_array, $last_polyline);
}
$foundToll = true;
array_push($polyline_array, $array['polyline']['points']);
foreach($tollways as $key => $value) {
$tollway_strings = explode(",",$value);
foreach($tollway_strings as $tollway_searchString) {
if(strpos(strtolower($array[$searchString]), strtolower($tollway_searchString)) !== false OR strpos(strtolower($last_instruction), strtolower($tollway_searchString)) !== false){
//error_log("found ".$tollway_searchString);
if(array_search($value, $tollsfound) == false) {
$sql = "SELECT * FROM tollways WHERE id = ".$key;
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$name = $row["name"];
}
}
$tollsfound["$key"] = $name;
}
}
}
}
} else {
if($foundToll) {
array_push($polyline_array, $last_polyline);
}
$foundToll = false;
}
//$responseArray["tollsFound"] = $tollsfound;
//$responseArray["polylines"] = $polyline_array;
$this_polyline = $array['polyline']['points'];
$this_instruction = $array[$searchString];
}
// echo "
";
// echo json_encode($tollsfound, true);
foreach($tollsfound as $key => $value) {
checkPrice($polyline_array, $conn, $key, $globalEntrys, $globalExits, $globalToll, $value, $i);
if($state1 == "NSW" && $key == 4) {
//error_log("checking tunnel");
//checkPrice($polyline_array, $conn, 5, $globalEntrys, $globalExits, $globalToll, "Lane Cove Tunnel", $i);
}
}
$tolls[$i] = $globalToll;
$entrys[$i] = $globalEntrys;
$exits[$i] = $globalExits;
$globalEntrys = array();
$globalExits = array();
$globalToll = array();
}
//echo $status;
}
$responseArray["status"] = "OK";
$responseArray["message"] = "Success";
$responseArray["entrys"] = $entrys;
$responseArray["exits"] = $exits;
$responseArray["tolls"] = $tolls;
$responseArray["routes"] = "";
return $responseArray;
}
function checkState($routes, $type) {
global $statesArray;
global $conn, $point1, $point2, $alt;
if($type == "start") {
$typeString = "start_address";
} else if($type == "end"){
$typeString = "end_address";
}
foreach ($statesArray as $stateString) {
if(strpos(strtolower($routes["routes"][0]["legs"][0][$typeString]), strtolower($stateString)) !== false) {
return $stateString;;
}
}
return "none";
}
//calculate distance
function GetDrivingDistance($marker1, $marker2)
{
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".urlencode($marker1)."&destinations=".urlencode($marker2)
."&mode=driving&sensor=false";
$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);
$dist = $response_a['rows'][0]['elements'][0]['distance']['text'];
$time = $response_a['rows'][0]['elements'][0]['duration']['text'];
return array('distance' => $dist, 'time' => $time);
}
function GetDirections($marker1, $marker2)
{
global $alt;
$url = "https://maps.googleapis.com/maps/api/directions/json?origin=".urlencode($marker1)."&destination=".urlencode($marker2)."&alternatives=".$alt."&key=AIzaSyCq_JZBDbrE5qma5SgF06M2dVAqbiZBZ-o";
$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);
return $response;
}
function doesRouteIntercept($polylines, $lat, $lng) {
foreach($polylines as $line) {
$decodedPoints = PolylineEncoder::decodeValue($line);
$response = \GeometryLibrary\PolyUtil::isLocationOnEdge(['lat' => $lat, 'lng' => $lng], $decodedPoints, 20, true);
if($response) {
//error_log("found ".$lat." ".$lng);
return true; // false
}
}
return false;
}
function checkPrice($polylines, $conn, $tollWay, &$globalEntrys, &$globalExits, &$globalToll, $tollName, $i) {
$entrysArray = [];
$exitsArray = [];
$entryID = 0;
$exitID = 0;
$sql = "SELECT * FROM entrys WHERE tollway = ".$tollWay;
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
if(doesRouteIntercept($polylines, $row['lat'], $row['lng'])) {
array_push($entrysArray, ["lat" => $row['lat'], "lng" => $row['lng'], "description" => $row['description']]);
$entryID = $row['id'];
//echo $row['description'];
}
}
if($entrysArray != null) {
array_push($globalEntrys, array_shift($entrysArray));
}
}
$sql = "SELECT * FROM exits WHERE tollway = ".$tollWay;
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
if(doesRouteIntercept($polylines, $row['lat'], $row['lng'])) {
array_push($exitsArray, ["lat" => $row['lat'], "lng" => $row['lng'], "description" => $row['description']]);
//error_log("exitArray ".json_encode($exitsArray, true));
$exitID = $row['id'];
}
}
if($exitsArray != null) {
array_push($globalExits, array_shift($exitsArray));
//error_log("globalArray ".json_encode($globalExits, true));
}
}
$sql = "SELECT * FROM priceMatrix WHERE entryID = '".$entryID."' and exitID = '".$exitID."'";
error_log($sql);
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
$row = $result->fetch_assoc();
if(strpos($row['priceA'], ",") !== false) {
$priceArray = $row['priceA'];
$priceSplit = explode(",", $priceArray);
foreach($priceSplit as $key => $p) {
if(strpos($p, "[") !== false) {
$p = str_replace("[", "", $p);
$p = str_replace("]", "", $p);
$addSplit = explode("-", $p);
$priceSplit[$key] = 0;
foreach($addSplit as $toll) {
$sql = "SELECT * FROM priceConstants WHERE ref = '".$toll."'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$priceSplit[$key] = $priceSplit[$key] + $row['value'];
error_log($priceSplit[$key]);
}
} else if(strpos($p, "S") !== false OR strpos($p, "M") !== false) {
$sql = "SELECT * FROM priceConstants WHERE ref = '".$p."'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$priceSplit[$key] = $row['value'];
}
}
error_log(json_encode($priceSplit, true));
$price = $priceSplit[0] * $priceSplit[1] + $priceSplit[2];
$price = number_format((float)$price, 2, '.', '');
if($price > $priceSplit[3] && $priceSplit[3] != 0) {
$price = $priceSplit[3];
}
} else {
$price = $row['priceA'];
}
foreach($globalToll as $key => $toll) {
if(array_search($tollName, $toll) !== false) {
$globalToll["cost"] = $globalToll["cost"] + + $price;
if($globalToll["name"] == "CityLink" && $globalToll["cost"] > 9.23) {
$globalToll["cost"] = "9.23";
}
return;
}
}
array_push($globalToll, ["name" => $tollName, "cost" => $price]);
}
return array("tolls" => $globalToll, "entrys" => $globalEntrys, "exits" => $globalExits);
}
function checkPrice2($polylines, $tollWay, $tollName) {
$entrysArray = [];
$exitsArray = [];
$entryID = 0;
$exitID = 0;
global $conn;
$sql = "SELECT * FROM entrys WHERE tollway = ".$tollWay;
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
if(doesRouteIntercept($polylines, $row['lat'], $row['lng'])) {
array_push($entrysArray, ["lat" => $row['lat'], "lng" => $row['lng'], "description" => $row['description']]);
$entryID = $row['id'];
//echo $row['description'];
}
}
}
$sql = "SELECT * FROM exits WHERE tollway = ".$tollWay;
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
if(doesRouteIntercept($polylines, $row['lat'], $row['lng'])) {
array_push($exitsArray, ["lat" => $row['lat'], "lng" => $row['lng'], "description" => $row['description']]);
//error_log("exitArray ".json_encode($exitsArray, true));
$exitID = $row['id'];
}
}
}
error_log($entryID." ".$exitID);
if($entryID != 0 OR $exitID != 0) {
$sql = "SELECT * FROM priceMatrix WHERE entryID = '".$entryID."' and exitID = '".$exitID."'";
error_log($sql);
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
$row = $result->fetch_assoc();
if(strpos($row['priceA'], ",") !== false) {
$priceArray = $row['priceA'];
$priceSplit = explode(",", $priceArray);
foreach($priceSplit as $key => $p) {
if(strpos($p, "[") !== false) {
$p = str_replace("[", "", $p);
$p = str_replace("]", "", $p);
$addSplit = explode("-", $p);
$priceSplit[$key] = 0;
foreach($addSplit as $toll) {
$sql = "SELECT * FROM priceConstants WHERE ref = '".$toll."'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$priceSplit[$key] = $priceSplit[$key] + $row['value'];
error_log($priceSplit[$key]);
}
} else if(strpos($p, "S") !== false OR strpos($p, "M") !== false) {
$sql = "SELECT * FROM priceConstants WHERE ref = '".$p."'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$priceSplit[$key] = $row['value'];
}
}
error_log(json_encode($priceSplit, true));
$price = $priceSplit[0] * $priceSplit[1] + $priceSplit[2];
$price = number_format((float)$price, 2, '.', '');
if($price > $priceSplit[3] && $priceSplit[3] != 0) {
$price = $priceSplit[3];
}
} else {
$price = $row['priceA'];
}
}
return array("tolls" => ["name" => $tollName, "cost" => $price], "entrys" => $entrysArray, "exits" => $exitsArray);
} else {
return null;
}
}
//error_log(json_encode($responseArray, true));
header("Content-type: application/json");
echo json_encode($responseArray, true);