-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIpLocation.php
76 lines (65 loc) · 1.89 KB
/
IpLocation.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
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
<?php
declare(strict_types=1);
namespace B13\Magnets;
/*
* This file is part of TYPO3 CMS-based extension "magnets" by b13.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*/
use GeoIp2\Database\Reader;
use GeoIp2\Model\City;
use GeoIp2\Model\Country;
/**
* Fetches Geo Location information from a given IP address
* This should be later refactored to have different Backends / Adapters
* to hook in php-module geoip, geoip2 or headers.
*/
class IpLocation
{
protected string $ip;
private City $cityRecord;
private Country $countryRecord;
public function __construct(string $ip)
{
// We could do some nice evaluation if the IP address is valid later-on
$this->ip = $ip;
$reader = new Reader($GLOBALS['TYPO3_CONF_VARS']['SYS']['GeoIPPath'] . '/GeoLite2-City.mmdb');
$this->cityRecord = $reader->city($ip);
$reader = new Reader($GLOBALS['TYPO3_CONF_VARS']['SYS']['GeoIPPath'] . '/GeoLite2-Country.mmdb');
$this->countryRecord = $reader->country($ip);
}
public function getCityRecord(): City
{
return $this->cityRecord;
}
public function getCountryRecord(): Country
{
return $this->countryRecord;
}
/**
* Get two-letter country code.
*/
public function getCountryCode(): ?string
{
return $this->countryRecord->country->isoCode;
}
/**
* Get two letter continent code
*/
public function getContinentCode(): ?string
{
return $this->countryRecord->continent->code;
}
/**
* Get location record.
*/
public function getLocation(): array
{
return [
'lat' => $this->cityRecord->location->latitude,
'lng' => $this->cityRecord->location->longitude,
];
}
}