|
1
|
|
|
<?php namespace Arcanedev\LaravelTracker\Detectors; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LaravelTracker\Contracts\Detectors\GeoIpDetector as GeoIpDetectorContract; |
|
4
|
|
|
use GeoIp2\Database\Reader as GeoIpReader; |
|
5
|
|
|
use GeoIp2\Exception\AddressNotFoundException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class GeoIpDetector |
|
9
|
|
|
* |
|
10
|
|
|
* @package Arcanedev\LaravelTracker\Detectors |
|
11
|
|
|
* @author ARCANEDEV <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class GeoIpDetector implements GeoIpDetectorContract |
|
14
|
|
|
{ |
|
15
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
16
|
|
|
| Properties |
|
17
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
18
|
|
|
*/ |
|
19
|
|
|
private $reader; |
|
20
|
|
|
|
|
21
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
22
|
|
|
| Constructor |
|
23
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct() |
|
26
|
|
|
{ |
|
27
|
|
|
$this->reader = new GeoIpReader($this->getGeoliteFileName()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
31
|
|
|
| Main Functions |
|
32
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
33
|
|
|
*/ |
|
34
|
|
|
/** |
|
35
|
|
|
* Get the geoip data. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $ipAddress |
|
38
|
|
|
* |
|
39
|
|
|
* @return array|null |
|
40
|
|
|
*/ |
|
41
|
|
|
public function search($ipAddress) |
|
42
|
|
|
{ |
|
43
|
|
|
try { |
|
44
|
|
|
if ($cityModel = $this->reader->city($ipAddress)) { |
|
45
|
|
|
return $this->renderData($cityModel); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
catch (AddressNotFoundException $e) { |
|
49
|
|
|
// do nothing |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return null; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
56
|
|
|
| Other Functions |
|
57
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
58
|
|
|
*/ |
|
59
|
|
|
/** |
|
60
|
|
|
* Render the data. |
|
61
|
|
|
* |
|
62
|
|
|
* @param \GeoIp2\Model\City $cityModel |
|
63
|
|
|
* |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
|
|
private function renderData($cityModel) |
|
67
|
|
|
{ |
|
68
|
|
|
return [ |
|
69
|
|
|
'latitude' => $cityModel->location->latitude, |
|
70
|
|
|
'longitude' => $cityModel->location->longitude, |
|
71
|
|
|
'country_code' => $cityModel->country->isoCode, |
|
72
|
|
|
'country_code3' => null, |
|
73
|
|
|
'country_name' => $cityModel->country->name, |
|
74
|
|
|
'region' => $cityModel->continent->code, |
|
75
|
|
|
'city' => $cityModel->city->name, |
|
76
|
|
|
'postal_code' => $cityModel->postal->code, |
|
77
|
|
|
'area_code' => null, |
|
78
|
|
|
'dma_code' => null, |
|
79
|
|
|
'metro_code' => $cityModel->location->metroCode, |
|
80
|
|
|
'continent_code' => $cityModel->continent->code, |
|
81
|
|
|
]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get the GeoLiteFileName. |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
private function getGeoliteFileName() |
|
90
|
|
|
{ |
|
91
|
|
|
return __DIR__ . '/../../data/GeoLite2-City.mmdb'; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|