1
|
|
|
<?php namespace Arcanedev\GeoIP\Drivers; |
2
|
|
|
|
3
|
|
|
use Arcanedev\GeoIP\Tasks\DownloadMaxmindDatabaseTask; |
4
|
|
|
use GeoIp2\Database\Reader; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class MaxmindDatabaseDriver |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\GeoIP\Drivers |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class MaxmindDatabaseDriver extends AbstractDriver |
13
|
|
|
{ |
14
|
|
|
/* ------------------------------------------------------------------------------------------------ |
15
|
|
|
| Properties |
16
|
|
|
| ------------------------------------------------------------------------------------------------ |
17
|
|
|
*/ |
18
|
|
|
/** |
19
|
|
|
* Service reader instance. |
20
|
|
|
* |
21
|
|
|
* @var \GeoIp2\Database\Reader |
22
|
|
|
*/ |
23
|
|
|
protected $reader; |
24
|
|
|
|
25
|
|
|
/* ------------------------------------------------------------------------------------------------ |
26
|
|
|
| Getters & Setters |
27
|
|
|
| ------------------------------------------------------------------------------------------------ |
28
|
|
|
*/ |
29
|
|
|
/** |
30
|
|
|
* Get the database path. |
31
|
|
|
* |
32
|
|
|
* @return string|null |
33
|
|
|
*/ |
34
|
24 |
|
private function getDatabasePath() |
35
|
|
|
{ |
36
|
24 |
|
return $this->getOption('database-path'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the locales. |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
24 |
|
private function getLocales() |
45
|
|
|
{ |
46
|
24 |
|
return $this->getOption('locales', ['en']); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/* ------------------------------------------------------------------------------------------------ |
50
|
|
|
| Main Functions |
51
|
|
|
| ------------------------------------------------------------------------------------------------ |
52
|
|
|
*/ |
53
|
|
|
/** |
54
|
|
|
* Init the driver. |
55
|
|
|
*/ |
56
|
24 |
|
protected function init() |
57
|
|
|
{ |
58
|
24 |
|
$this->checkDatabaseFile(); |
59
|
|
|
|
60
|
24 |
|
$this->reader = new Reader( |
61
|
24 |
|
$this->getDatabasePath(), |
62
|
24 |
|
$this->getLocales() |
63
|
4 |
|
); |
64
|
24 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Locate the ip address. |
68
|
|
|
* |
69
|
|
|
* @param string $ipAddress |
70
|
|
|
* |
71
|
|
|
* @return \Arcanedev\GeoIP\Location |
72
|
|
|
*/ |
73
|
12 |
|
public function locate($ipAddress) |
74
|
|
|
{ |
75
|
12 |
|
$record = $this->reader->city($ipAddress); |
76
|
|
|
|
77
|
6 |
|
return $this->hydrate([ |
78
|
6 |
|
'ip' => $ipAddress, |
79
|
6 |
|
'iso_code' => $record->country->isoCode, |
80
|
6 |
|
'country' => $record->country->name, |
81
|
6 |
|
'city' => $record->city->name, |
82
|
6 |
|
'state' => $record->mostSpecificSubdivision->name, |
83
|
6 |
|
'state_code' => $record->mostSpecificSubdivision->isoCode, |
84
|
6 |
|
'postal_code' => $record->postal->code, |
85
|
6 |
|
'latitude' => $record->location->latitude, |
86
|
6 |
|
'longitude' => $record->location->longitude, |
87
|
6 |
|
'timezone' => $record->location->timeZone, |
88
|
6 |
|
'continent' => $record->continent->code, |
89
|
1 |
|
]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Update function for service. |
94
|
|
|
* |
95
|
|
|
* @return bool |
96
|
|
|
* |
97
|
|
|
* @throws \Exception |
98
|
|
|
*/ |
99
|
|
|
public function update() |
100
|
|
|
{ |
101
|
|
|
return DownloadMaxmindDatabaseTask::run( |
102
|
|
|
$this->getOption('update-url'), |
103
|
|
|
$this->getDatabasePath() |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/* ------------------------------------------------------------------------------------------------ |
108
|
|
|
| Other Functions |
109
|
|
|
| ------------------------------------------------------------------------------------------------ |
110
|
|
|
*/ |
111
|
|
|
/** |
112
|
|
|
* Check if the database exists. |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
24 |
|
private function isDatabaseExists() |
117
|
|
|
{ |
118
|
24 |
|
return file_exists($this->getDatabasePath()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Check if database file exists, otherwise copy the dummy one. |
123
|
|
|
*/ |
124
|
24 |
|
private function checkDatabaseFile() |
125
|
|
|
{ |
126
|
24 |
|
if ($this->isDatabaseExists()) return; |
127
|
|
|
|
128
|
6 |
|
$pathinfo = pathinfo($path = $this->getDatabasePath()); |
129
|
|
|
|
130
|
6 |
|
if ( ! file_exists($pathinfo['dirname'])) { |
131
|
6 |
|
mkdir($pathinfo['dirname'], 0777, true); |
132
|
1 |
|
} |
133
|
|
|
|
134
|
6 |
|
copy(realpath(__DIR__ . '/../../data/geoip.mmdb'), $path); |
135
|
6 |
|
} |
136
|
|
|
} |
137
|
|
|
|