-
Notifications
You must be signed in to change notification settings - Fork 0
/
user-agent.php
47 lines (40 loc) · 1.3 KB
/
user-agent.php
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
<?php
/**
* Return a JSON response containing user agent info, including geographic location.
*/
$config = include('./src/config.php');
require_once("./src/utils.php");
require_once("./libs/geoip/geoip2.phar");
use GeoIp2\Database\Reader;
$GeoIPCity = new Reader("./libs/geoip/GeoLite2-City.mmdb");
$GeoIPASN = new Reader("./libs/geoip/GeoLite2-ASN.mmdb");
// Build response headers
if(!authorize($config)) return;
header('Content-Type: application/json; charset=utf-8');
// Build JSON response
$origin_ip = get_ip($config);
try {
$city_record = $GeoIPCity->city($origin_ip);
$asn_record = $GeoIPASN->asn($origin_ip);
$whois = $city_record->location;
$isp = $asn_record->autonomousSystemOrganization;
$country = $city_record->country->name;
}
catch (Exception $e) {
// Not much to do... IP is most likely not in DB.
}
$response = array(
"ip" => get_ip($config),
"origin" => get_origin(),
"user-agent" => $_SERVER['HTTP_USER_AGENT'],
"lang" => get_lang(),
"whois" => $whois,
"isp" => $isp,
"country" => $country,
"browser" => getBrowser(),
"OS" => getOS(),
"port" => $_SERVER['SERVER_PORT'],
"host" => $_SERVER['HTTP_HOST'],
"secret" => $config['user-secret'],
);
echo json_encode($response);