-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathIPinfoCore.php
More file actions
276 lines (249 loc) · 8.54 KB
/
IPinfoCore.php
File metadata and controls
276 lines (249 loc) · 8.54 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
namespace ipinfo\ipinfo;
require_once __DIR__ . "/Const.php";
use Exception;
use ipinfo\ipinfo\cache\DefaultCache;
use GuzzleHttp\Client;
use Symfony\Component\HttpFoundation\IpUtils;
/**
* Exposes the IPinfo Core API to client code.
*/
class IPinfoCore
{
const API_URL = "https://api.ipinfo.io/lookup";
const COUNTRY_FLAG_URL = "https://cdn.ipinfo.io/static/images/countries-flags/";
const STATUS_CODE_QUOTA_EXCEEDED = 429;
const REQUEST_TIMEOUT_DEFAULT = 2; // seconds
const CACHE_MAXSIZE = 4096;
const CACHE_TTL = 86400; // 24 hours as seconds
const CACHE_KEY_VSN = "1"; // update when cache vals change for same key.
const COUNTRIES_DEFAULT = COUNTRIES;
const EU_COUNTRIES_DEFAULT = EU;
const COUNTRIES_FLAGS_DEFAULT = FLAGS;
const COUNTRIES_CURRENCIES_DEFAULT = CURRENCIES;
const CONTINENTS_DEFAULT = CONTINENTS;
public $access_token;
public $settings;
public $cache;
public $countries;
public $eu_countries;
public $countries_flags;
public $countries_currencies;
public $continents;
protected $http_client;
public function __construct($access_token = null, $settings = [])
{
$this->access_token = $access_token;
$this->settings = $settings;
/*
Support a timeout first-class, then a `guzzle_opts` key that can
override anything.
*/
$guzzle_opts = [
"http_errors" => false,
"headers" => $this->buildHeaders(),
"timeout" => $settings["timeout"] ?? self::REQUEST_TIMEOUT_DEFAULT,
];
if (isset($settings["guzzle_opts"])) {
$guzzle_opts = array_merge($guzzle_opts, $settings["guzzle_opts"]);
}
$this->http_client = new Client($guzzle_opts);
$this->countries = $settings["countries"] ?? self::COUNTRIES_DEFAULT;
$this->countries_flags =
$settings["countries_flags"] ?? self::COUNTRIES_FLAGS_DEFAULT;
$this->countries_currencies =
$settings["countries_currencies"] ??
self::COUNTRIES_CURRENCIES_DEFAULT;
$this->eu_countries =
$settings["eu_countries"] ?? self::EU_COUNTRIES_DEFAULT;
$this->continents = $settings["continents"] ?? self::CONTINENTS_DEFAULT;
if (
!array_key_exists("cache_disabled", $this->settings) ||
$this->settings["cache_disabled"] == false
) {
if (array_key_exists("cache", $settings)) {
$this->cache = $settings["cache"];
} else {
$maxsize = $settings["cache_maxsize"] ?? self::CACHE_MAXSIZE;
$ttl = $settings["cache_ttl"] ?? self::CACHE_TTL;
$this->cache = new DefaultCache($maxsize, $ttl);
}
} else {
$this->cache = null;
}
}
/**
* Get formatted details for an IP address.
* @param string|null $ip_address IP address to look up.
* @return DetailsCore Formatted IPinfo data.
* @throws IPinfoException
*/
public function getDetails($ip_address = null)
{
$response_details = $this->getRequestDetails((string) $ip_address);
return $this->formatDetailsObject($response_details);
}
public function formatDetailsObject($details = [])
{
// Enrich geo object if present
if (isset($details["geo"]) && isset($details["geo"]["country_code"])) {
$country_code = $details["geo"]["country_code"];
$details["geo"]["country_name"] = $this->countries[$country_code] ?? null;
$details["geo"]["is_eu"] = in_array($country_code, $this->eu_countries);
$details["geo"]["country_flag"] =
$this->countries_flags[$country_code] ?? null;
$details["geo"]["country_flag_url"] =
self::COUNTRY_FLAG_URL . $country_code . ".svg";
$details["geo"]["country_currency"] =
$this->countries_currencies[$country_code] ?? null;
$details["geo"]["continent_info"] =
$this->continents[$country_code] ?? null;
}
return new DetailsCore($details);
}
/**
* Get details for a specific IP address.
* @param string $ip_address IP address to query API for.
* @return array IP response data.
* @throws IPinfoException
*/
public function getRequestDetails(string $ip_address)
{
if (
// Avoid checking if bogon if the user provided no IP or explicitly
// set it to "me" to get its IP info
$ip_address &&
$ip_address != "me" &&
$this->isBogon($ip_address)
) {
return [
"ip" => $ip_address,
"bogon" => true,
];
}
if ($this->cache != null) {
$cachedRes = $this->cache->get($this->cacheKey($ip_address));
if ($cachedRes != null) {
return $cachedRes;
}
}
$url = self::API_URL;
if ($ip_address) {
$url .= "/$ip_address";
} else {
$url .= "/me";
}
try {
$response = $this->http_client->request("GET", $url);
} catch (GuzzleException $e) {
throw new IPinfoException($e->getMessage());
} catch (Exception $e) {
throw new IPinfoException($e->getMessage());
}
if ($response->getStatusCode() == self::STATUS_CODE_QUOTA_EXCEEDED) {
throw new IPinfoException("IPinfo request quota exceeded.");
} elseif ($response->getStatusCode() >= 400) {
throw new IPinfoException(
"Exception: " .
json_encode([
"status" => $response->getStatusCode(),
"reason" => $response->getReasonPhrase(),
])
);
}
$raw_details = json_decode($response->getBody(), true);
if ($this->cache != null) {
$this->cache->set($this->cacheKey($ip_address), $raw_details);
}
return $raw_details;
}
/**
* Build headers for API request.
* @return array Headers for API request.
*/
private function buildHeaders()
{
$headers = [
"user-agent" => "IPinfoClient/PHP/3.2.0",
"accept" => "application/json",
"content-type" => "application/json",
];
if ($this->access_token) {
$headers["authorization"] = "Bearer {$this->access_token}";
}
return $headers;
}
/**
* Check if IP address is bogon.
* @param string $ip_address IP address.
* @return bool true if bogon, else false.
*/
private function isBogon(string $ip_address)
{
return IpUtils::checkIp($ip_address, [
"0.0.0.0/8",
"10.0.0.0/8",
"100.64.0.0/10",
"127.0.0.0/8",
"169.254.0.0/16",
"172.16.0.0/12",
"192.0.0.0/24",
"192.0.2.0/24",
"192.168.0.0/16",
"198.18.0.0/15",
"198.51.100.0/24",
"203.0.113.0/24",
"224.0.0.0/4",
"240.0.0.0/4",
"255.255.255.255/32",
"::/128",
"::1/128",
"::ffff:0:0/96",
"::/96",
"100::/64",
"2001:10::/28",
"2001:db8::/32",
"fc00::/7",
"fe80::/10",
"fec0::/10",
"ff00::/8",
"2002::/24",
"2002:a00::/24",
"2002:7f00::/24",
"2002:a9fe::/32",
"2002:ac10::/28",
"2002:c000::/40",
"2002:c000:200::/40",
"2002:c0a8::/32",
"2002:c612::/31",
"2002:c633:6400::/40",
"2002:cb00:7100::/40",
"2002:e000::/20",
"2002:f000::/20",
"2002:ffff:ffff::/48",
"2001::/40",
"2001:0:a00::/40",
"2001:0:7f00::/40",
"2001:0:a9fe::/48",
"2001:0:ac10::/44",
"2001:0:c000::/56",
"2001:0:c000:200::/56",
"2001:0:c0a8::/48",
"2001:0:c612::/47",
"2001:0:c633:6400::/56",
"2001:0:cb00:7100::/56",
"2001:0:e000::/36",
"2001:0:f000::/36",
"2001:0:ffff:ffff::/64",
]);
}
/**
* Generate cache key for an IP address.
* @param string $ip IP address.
* @return string Cache key.
*/
private function cacheKey(string $ip)
{
return "core_" . self::CACHE_KEY_VSN . "_" . $ip;
}
}