|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace yiicod\geo\adapters\geoIp2; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class GeoIp2Database |
|
9
|
|
|
* |
|
10
|
|
|
* @package yiicod\geo\adapters\geoIp2 |
|
11
|
|
|
* |
|
12
|
|
|
* @author Dmitry Turchanin |
|
13
|
|
|
*/ |
|
14
|
|
|
class GeoIp2Database implements GeoIpDatabaseInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Database alias |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
public $databaseAlias = '@app/runtime'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* GeoLite database download URL |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
public $databaseDownloadUrl = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Database alias |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
public $databaseFilename = 'GeoLite2-City.mmdb'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Database full file name |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
private $databaseFullFilename; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Checks if DB-file exists and it is actual. Ff not then it starts file update |
|
46
|
|
|
* |
|
47
|
|
|
* @return bool |
|
48
|
|
|
*/ |
|
49
|
|
|
public function hasDbFile(): bool |
|
50
|
|
|
{ |
|
51
|
|
|
$result = false; |
|
52
|
|
|
$needUpdateDbFile = true; |
|
53
|
|
|
|
|
54
|
|
|
if (file_exists($this->getFileName()) && $modifiedTime = filemtime($this->getFileName())) { |
|
55
|
|
|
$result = true; |
|
56
|
|
|
|
|
57
|
|
|
if ((time() - $modifiedTime) < 30 * 24 * 3600) { |
|
58
|
|
|
$needUpdateDbFile = false; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if (true === $needUpdateDbFile) { |
|
63
|
|
|
$this->updateDbFile(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $result; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Downloads and unpacks fresh IP database file in the background process |
|
71
|
|
|
*/ |
|
72
|
|
|
public function updateDbFile() |
|
73
|
|
|
{ |
|
74
|
|
|
$file = $this->getFileName(); |
|
75
|
|
|
|
|
76
|
|
|
$archivePath = str_replace(basename($file), 'GeoIPArchive', $file); |
|
77
|
|
|
$tmpPath = str_replace(basename($file), 'GeoIPTmp', $file); |
|
78
|
|
|
$downloadUrl = $this->databaseDownloadUrl; |
|
79
|
|
|
|
|
80
|
|
|
$cmd = "rm -f $archivePath" |
|
81
|
|
|
. " && wget $downloadUrl -O $archivePath" |
|
82
|
|
|
. " && gunzip -c $archivePath > $tmpPath" |
|
83
|
|
|
. " && mv -f $tmpPath $file" |
|
84
|
|
|
. " && rm -f $archivePath"; |
|
85
|
|
|
$cmd .= ' 2>&1'; |
|
86
|
|
|
|
|
87
|
|
|
exec($cmd); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Gets local database's file name |
|
92
|
|
|
* |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getFileName() |
|
96
|
|
|
{ |
|
97
|
|
|
if (is_null($this->databaseFullFilename)) { |
|
98
|
|
|
$this->databaseFullFilename = rtrim(Yii::getAlias($this->databaseAlias), '/') . '/' . $this->databaseFilename; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $this->databaseFullFilename; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|