-
Notifications
You must be signed in to change notification settings - Fork 377
Expand file tree
/
Copy pathMaxMindDatabase.php
More file actions
169 lines (139 loc) · 4.18 KB
/
MaxMindDatabase.php
File metadata and controls
169 lines (139 loc) · 4.18 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace Torann\GeoIP\Services;
use PharData;
use Exception;
use GeoIp2\Database\Reader;
class MaxMindDatabase extends AbstractService
{
/**
* Service reader instance.
*
* @var \GeoIp2\Database\Reader
*/
protected $reader;
/**
* The "booting" method of the service.
*
* @return void
*/
public function boot()
{
$path = $this->config('database_path');
// Copy test database for now
if (is_file($path) === false) {
@mkdir(dirname($path));
copy(__DIR__ . '/../../resources/geoip.mmdb', $path);
}
$this->reader = new Reader(
$path, $this->config('locales', ['en'])
);
}
/**
* {@inheritdoc}
*/
public function locate($ip)
{
$record = $this->reader->city($ip);
return $this->hydrate([
'ip' => $ip,
'iso_code' => $record->country->isoCode,
'country' => $record->country->name,
'city' => $record->city->name,
'state' => $record->mostSpecificSubdivision->isoCode,
'state_name' => $record->mostSpecificSubdivision->name,
'postal_code' => $record->postal->code,
'lat' => $record->location->latitude,
'lon' => $record->location->longitude,
'timezone' => $record->location->timeZone,
'continent' => $record->continent->code,
]);
}
/**
* Update function for service.
*
* @return string
* @throws Exception
*/
public function update()
{
if ($this->config('database_path', false) === false) {
throw new Exception('Database path not set in config file.');
}
$this->withTemporaryDirectory(function ($directory) {
$tarFile = sprintf('%s/maxmind.tar.gz', $directory);
@file_put_contents($tarFile, fopen($this->config('update_url'), 'r'));
$archive = new PharData($tarFile);
$file = $this->findDatabaseFile($archive);
$relativePath = "{$archive->getFilename()}/{$file->getFilename()}";
$archive->extractTo($directory, $relativePath);
file_put_contents($this->config('database_path'), fopen("{$directory}/{$relativePath}", 'r'));
});
return "Database file ({$this->config('database_path')}) updated.";
}
/**
* Provide a temporary directory to perform operations in and and ensure
* it is removed afterwards.
*
* @param callable $callback
*
* @return void
*/
protected function withTemporaryDirectory(callable $callback)
{
$directory = tempnam(sys_get_temp_dir(), 'maxmind');
if (file_exists($directory)) {
unlink($directory);
}
mkdir($directory);
try {
$callback($directory);
} finally {
$this->deleteDirectory($directory);
}
}
/**
* Recursively search the given archive to find the .mmdb file.
*
* @param \PharData $archive
*
* @return mixed
* @throws \Exception
*/
protected function findDatabaseFile($archive)
{
foreach ($archive as $file) {
if ($file->isDir()) {
return $this->findDatabaseFile(new PharData($file->getPathName()));
}
if (pathinfo($file, PATHINFO_EXTENSION) === 'mmdb') {
return $file;
}
}
throw new Exception('Database file could not be found within archive.');
}
/**
* Recursively delete the given directory.
*
* @param string $directory
*
* @return mixed
*/
protected function deleteDirectory(string $directory)
{
if (! file_exists($directory)) {
return true;
}
if (! is_dir($directory)) {
return unlink($directory);
}
foreach (scandir($directory) as $item) {
if ($item == '.' || $item == '..') {
continue;
}
if (! $this->deleteDirectory($directory . DIRECTORY_SEPARATOR . $item)) {
return false;
}
}
return rmdir($directory);
}
}