-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
66 lines (56 loc) · 2.28 KB
/
index.php
File metadata and controls
66 lines (56 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
define('GOOGLE_API_KEY', 'enter-your-key');
class Locator
{
public static function isInsideRadius($addressString, $IPaddress, $distanceInKm = 100)
{
if (!$addressString || !$IPaddress || $distanceInKm <= 0) {
return null;
}
$record = geoip_record_by_name($IPaddress);
if ($record) {
if (!empty($record['latitude']) && !empty($record['longitude'])) {
try {
$addressString = str_replace(' ', '+', $addressString);
$url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . $addressString . '&key=' . GOOGLE_API_KEY;
$get_content = file_get_contents($url);
$coordination = json_decode($get_content, true);
if ($coordination['status'] != 'OK') {
return null;
}
$LatLng = array();
foreach ($coordination['results'] as $coord) {
$LatLng = array(
'latitude' => $coord['geometry']['location']['lat'],
'longitude' => $coord['geometry']['location']['lng']
);
}
if (empty($LatLng)) {
return null;
}
$distance = static::distance($record['latitude'], $record['longitude'], $LatLng['latitude'], $LatLng['longitude'], false);
return $distance < $distanceInKm;
} catch (Exception $e) {
return null;
}
}
}
return null;
}
private static function distance($lat1, $lng1, $lat2, $lng2, $miles = true)
{
$pi80 = M_PI / 180;
$lat1 *= $pi80;
$lng1 *= $pi80;
$lat2 *= $pi80;
$lng2 *= $pi80;
$r = 6372.797; // mean radius of Earth in km
$dlat = $lat2 - $lat1;
$dlng = $lng2 - $lng1;
$a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2);
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
$km = $r * $c;
return ($miles ? ($km * 0.621371192) : $km);
}
}
$result = Locator::isInsideRadius('Paris', '90.100.120.130');