meta/geolite2.php

49 lines
2.9 KiB
PHP

<?php
$ip = substr($_SERVER['REQUEST_URI'], 1);
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
$ipFormat = "IPv4";
else if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6))
$ipFormat = "IPv6";
else
exit("This is not a valid IP address!");
define("GEOLITE2_DATE", "20220215");
require('db-reader/autoload.php');
use MaxMind\Db\Reader;
$reader = new Reader("geolite2/GeoLite2-ASN_" . GEOLITE2_DATE . "/GeoLite2-ASN.mmdb");
$asnData = $reader->get($ip);
$reader->close();
$reader = new Reader("geolite2/GeoLite2-City_" . GEOLITE2_DATE . "/GeoLite2-City.mmdb");
$cityData = $reader->get($ip);
$reader->close();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Informations about <?= $ipFormat ?> address <?= htmlspecialchars($ip) ?></title>
</head>
<body>
<h1>Informations about <?= $ipFormat ?> address <code><?= htmlspecialchars($ip) ?></code></h1>
<main>
<abbr title="Autonomous System Number">ASN</abbr>: <code><?= htmlspecialchars($asnData['autonomous_system_number']) ?></code><br>
<abbr title="Autonomous System">AS</abbr> name: <?= htmlspecialchars($asnData['autonomous_system_organization']) ?><br>
Location: <a href="geo:<?= htmlspecialchars($cityData['location']['latitude']) ?>,<?= htmlspecialchars($cityData['location']['longitude']) ?>"><?= htmlspecialchars($cityData['location']['latitude']) ?>,<?= htmlspecialchars($cityData['location']['longitude']) ?></a> (accuracy radius: <?= htmlspecialchars($cityData['location']['accuracy_radius']) ?> km)<br>
Continent: <?php if (isset($cityData['continent']['names']['en'])) echo htmlspecialchars($cityData['continent']['names']['en']) . " (" . htmlspecialchars($cityData['continent']['code']) . ")"; else echo "unknown"; ?><br>
Time zone: <?php if (isset($cityData['location']['time_zone'])) echo htmlspecialchars($cityData['location']['time_zone']); else echo "unknown"; ?><br>
Country: <?php if (isset($cityData['country']['names']['en'])) echo htmlspecialchars($cityData['country']['names']['en']) . " (" . htmlspecialchars($cityData['country']['iso_code']) . ")"; else echo "unknown"; ?><br>
Subdivision: <?php if (isset($cityData['subdivisions']['0']['names']['en'])) echo htmlspecialchars($cityData['subdivisions']['0']['names']['en']) . " (" . htmlspecialchars($cityData['subdivisions']['0']['iso_code']) . ")"; else echo "unknown"; ?><br>
Postal code: <?php if (isset($cityData['postal']['code'])) echo htmlspecialchars($cityData['postal']['code']); else echo "unknown"; ?><br>
City: <?php if (isset($cityData['city']['names']['en'])) echo htmlspecialchars($cityData['city']['names']['en']); else echo "unknown"; ?><br>
</main>
<footer>
<small>This page queries a local GeoLite2 database dating from <?= GEOLITE2_DATE ?>.</small>
<br>
<a href="https://code.antopie.org/miraty/meta">Source code</a>
</footer>
</body>
</html>