1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace jeremykenedy\LaravelBlocker\App\Traits; |
4
|
|
|
|
5
|
|
|
trait IpAddressDetails |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Get the Location of the IP Address. |
9
|
|
|
* |
10
|
|
|
* @param string $ip (optional, no value will always return NULL) |
11
|
|
|
* @param string $purpose (optional) |
12
|
|
|
* @param bool $deep_detect (optional) |
13
|
|
|
* |
14
|
|
|
* @return string |
15
|
|
|
*/ |
16
|
|
|
public static function checkIP($ip = null, $purpose = 'location', $deep_detect = true) |
17
|
|
|
{ |
18
|
|
|
$output = null; |
19
|
|
|
if (filter_var($ip, FILTER_VALIDATE_IP) === false) { |
20
|
|
|
$ip = $_SERVER['REMOTE_ADDR']; |
21
|
|
|
if ($deep_detect) { |
22
|
|
|
if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP)) { |
23
|
|
|
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; |
24
|
|
|
} |
25
|
|
|
if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP)) { |
26
|
|
|
$ip = $_SERVER['HTTP_CLIENT_IP']; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
$purpose = str_replace(['name', "\n", "\t", ' ', '-', '_'], null, strtolower(trim($purpose))); |
31
|
|
|
$support = ['country', 'countrycode', 'state', 'region', 'city', 'location', 'address']; |
32
|
|
|
$continents = [ |
33
|
|
|
'AF' => 'Africa', |
34
|
|
|
'AN' => 'Antarctica', |
35
|
|
|
'AS' => 'Asia', |
36
|
|
|
'EU' => 'Europe', |
37
|
|
|
'OC' => 'Australia (Oceania)', |
38
|
|
|
'NA' => 'North America', |
39
|
|
|
'SA' => 'South America', |
40
|
|
|
]; |
41
|
|
|
if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) { |
42
|
|
|
$ipdat = @json_decode(file_get_contents('http://www.geoplugin.net/json.gp?ip='.$ip)); |
43
|
|
|
if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) { |
44
|
|
|
switch ($purpose) { |
45
|
|
|
case 'location': |
46
|
|
|
$output = [ |
47
|
|
|
'city' => @$ipdat->geoplugin_city, |
48
|
|
|
'state' => @$ipdat->geoplugin_regionName, |
49
|
|
|
'country' => @$ipdat->geoplugin_countryName, |
50
|
|
|
'countryCode' => @$ipdat->geoplugin_countryCode, |
51
|
|
|
'continent' => @$continents[strtoupper($ipdat->geoplugin_continentCode)], |
52
|
|
|
'continent_code' => @$ipdat->geoplugin_continentCode, |
53
|
|
|
'latitude' => @$ipdat->geoplugin_latitude, |
54
|
|
|
'longitude' => @$ipdat->geoplugin_longitude, |
55
|
|
|
'currencyCode' => @$ipdat->geoplugin_currencyCode, |
56
|
|
|
'areaCode' => @$ipdat->geoplugin_areaCode, |
57
|
|
|
'dmaCode' => @$ipdat->geoplugin_dmaCode, |
58
|
|
|
'region' => @$ipdat->geoplugin_region, |
59
|
|
|
]; |
60
|
|
|
break; |
61
|
|
|
case 'address': |
62
|
|
|
$address = [$ipdat->geoplugin_countryName]; |
63
|
|
|
if (@strlen($ipdat->geoplugin_regionName) >= 1) { |
64
|
|
|
$address[] = $ipdat->geoplugin_regionName; |
65
|
|
|
} |
66
|
|
|
if (@strlen($ipdat->geoplugin_city) >= 1) { |
67
|
|
|
$address[] = $ipdat->geoplugin_city; |
68
|
|
|
} |
69
|
|
|
$output = implode(', ', array_reverse($address)); |
70
|
|
|
break; |
71
|
|
|
case 'city': |
72
|
|
|
$output = @$ipdat->geoplugin_city; |
73
|
|
|
break; |
74
|
|
|
case 'state': |
75
|
|
|
$output = @$ipdat->geoplugin_regionName; |
76
|
|
|
break; |
77
|
|
|
case 'region': |
78
|
|
|
$output = @$ipdat->geoplugin_regionName; |
79
|
|
|
break; |
80
|
|
|
case 'country': |
81
|
|
|
$output = @$ipdat->geoplugin_countryName; |
82
|
|
|
break; |
83
|
|
|
case 'countrycode': |
84
|
|
|
$output = @$ipdat->geoplugin_countryCode; |
85
|
|
|
break; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $output; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|