Compare commits

...

2 Commits

2 changed files with 62 additions and 4 deletions

View File

@@ -1,9 +1,63 @@
<?php
$latitude = $_GET["latitude"];
$longitude = $_GET["longitude"];
// FUNCTION TO CALCULATE HAVERSINE DISTANCE GIVEN COORDINATES OF
// TWO POINTS, AND RADIUS (IN KM)
function haversine_distance($lat1, $long1, $lat2, $long2, $radius) {
$term_1 = $lat2 - $lat1;
$term_1 = $term_1 / 2;
$term_1 = sin($term_1);
$term_1 = pow($term_1, 2);
$term_2 = $long2 - $long1;
$term_2 = $term_2 / 2;
$term_2 = sin($term_2);
$term_2 = pow($term_2, 2);
$term_2 = $term_2 * cos($lat1);
$term_2 = $term_2 * cos($lat2);
$distance = $term_1 + $term_2;
$distance = sqrt($distance);
$distance = asin($distance);
$distance = $distance * 2;
$distance = $distance * $radius;
return $distance;
}
function km_to_miles($distance) {
return $distance * 0.62137119;
}
$user_lat = $_GET["latitude"];
$user_lat = $user_lat * (M_PI / 180);
$user_long = $_GET["longitude"];
$user_long = $user_long * (M_PI / 180);
$glink = $_GET["glink"];
printf("Lat is %s, Long is %s, and glink is %s",$latitude,$longitude,$glink);
$cluster = Cassandra::cluster()->withPersistentSessions(true)->build();
$keyspace = 'glink';
$session = $cluster->connect($keyspace);
$statement = $session->prepare('SELECT latitude,longitude,radius,url FROM data WHERE shortlink=? ALLOW FILTERING;');
$result = $session->execute($statement,array('arguments' => array($glink)));
$link_lat = floatval($result[0]['latitude']);
$link_lat = $link_lat * (M_PI / 180);
$link_long = floatval($result[0]['longitude']);
$link_long = $link_long * (M_PI / 180);
$target_radius = intval($result[0]['radius']);
$url = $result[0]['url'];
$distance = haversine_distance($link_lat, $link_long, $user_lat, $user_long, 6371.009);
$distance = km_to_miles($distance);
if ($distance <= $target_radius) {
printf("%s",$url);
} else {
printf("B");
}
// Check the database to see if user is allowed to access the URL. If they are, respond 'Yes' (for the time being), if they are not, respond 'No' (for the time being).

View File

@@ -17,7 +17,11 @@ var request;
function requestHandler() {
if (request.readyState === XMLHttpRequest.DONE) {
if (request.status === 200) {
alert(request.responseText);
if (request.responseText == "B") {
alert("You are not authorized to access this GLink, as you are outside the permitted radius. If you think this is an error, please try again on a mobile device.");
} else {
window.location.replace(request.responseText);
}
} else {
console.log("Error sending data to server.");
}