-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookup.php
34 lines (26 loc) · 946 Bytes
/
lookup.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
<?php
declare(strict_types=1);
header('Content-type: application/json');
if (is_file(__DIR__ . "/vendor/autoload.php")) {
// Dependencies installed via composer
require(__DIR__ . "/vendor/autoload.php");
} elseif (is_file('/usr/share/php/GeoIP2/autoload.php')) {
// Dependencies installed via Fedora autoloader
require('/usr/share/php/GeoIP2/autoload.php');
} else {
http_response_code(501);
exit;
}
use GeoIp2\Database\Reader as GeoIP2;
$Return = [];
$GeoIP2 = new GeoIP2('/usr/share/GeoIP/GeoLite2-Country.mmdb');
if (isset($_POST['ip']) && is_array($_POST['ip'])) {
foreach ($_POST['ip'] as $IP) {
try {
$Return[] = ['ip' => $IP, 'info' => ['country' => strtolower($GeoIP2->country($IP)->country->isoCode), 'host' => $IP]];
} catch (Exception $e) {
$Return[] = ['ip' => $IP, 'info' => ['country' => 'un', 'host' => $IP]];
}
}
}
echo json_encode($Return);