Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 30 |
GeoipUpdater | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
72 | |
0.00% |
0 / 30 |
__construct | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
getDatabase | |
0.00% |
0 / 1 |
56 | |
0.00% |
0 / 28 |
1 | <?php |
2 | |
3 | namespace Miniframe\Statistics\Service; |
4 | |
5 | class GeoipUpdater |
6 | { |
7 | /** |
8 | * Maxmind license key. |
9 | * |
10 | * @var string |
11 | */ |
12 | protected $licenseKey; |
13 | /** |
14 | * Storage folder for database files. |
15 | * |
16 | * @var string |
17 | */ |
18 | protected $databasePath; |
19 | /** |
20 | * URL for the API |
21 | * |
22 | * @var string |
23 | */ |
24 | protected $urlApi = 'https://download.maxmind.com/app/geoip_download'; |
25 | |
26 | /** |
27 | * Initiates the GeoIP database Updater |
28 | * |
29 | * @param string $licenseKey Maxmind license key. |
30 | * @param string $databasePath Storage folder for database files. |
31 | */ |
32 | public function __construct(string $licenseKey, string $databasePath) |
33 | { |
34 | $this->licenseKey = $licenseKey; |
35 | $this->databasePath = $databasePath; |
36 | } |
37 | |
38 | /** |
39 | * Makes sure we've got a database and returns the full path to the database |
40 | * |
41 | * @return void |
42 | */ |
43 | public function getDatabase(): void |
44 | { |
45 | if (!is_dir($this->databasePath)) { |
46 | mkdir($this->databasePath, 0777, true); |
47 | } |
48 | $edition = 'GeoLite2-Country'; |
49 | $suffix = 'tar.gz'; |
50 | $tarGzFile = $this->databasePath . '/' . $edition . '.' . $suffix; |
51 | $tarFile = $this->databasePath . '/' . $edition . '.tar'; |
52 | $mmdbFile = $this->databasePath . '/' . $edition . '.mmdb'; |
53 | |
54 | // Step 1: Download .tar.gz file |
55 | $fh = fopen($tarGzFile, 'wb'); |
56 | $ch = curl_init(); |
57 | curl_setopt_array($ch, [ |
58 | CURLOPT_URL => $this->urlApi . '?' . http_build_query([ |
59 | 'edition_id' => $edition, |
60 | 'suffix' => $suffix, |
61 | 'license_key' => $this->licenseKey, |
62 | ]), |
63 | CURLOPT_FOLLOWLOCATION => true, |
64 | CURLOPT_HTTPGET => true, |
65 | CURLOPT_HEADER => false, |
66 | CURLOPT_FILE => $fh, |
67 | ]); |
68 | curl_exec($ch); |
69 | curl_close($ch); |
70 | fclose($fh); |
71 | |
72 | // Step 2: unzip .tar.gz file |
73 | if (file_exists($tarFile)) { |
74 | unlink($tarFile); |
75 | } |
76 | $ungzip = new \PharData($tarGzFile); |
77 | $ungzip->decompress(); |
78 | |
79 | // Step 3: unpack .tar file |
80 | $untar = new \PharData($tarFile); |
81 | $untar->extractTo($this->databasePath); |
82 | foreach (glob($this->databasePath . '/*', GLOB_ONLYDIR) as $dir) { |
83 | if (substr(pathinfo($dir, PATHINFO_BASENAME), 0, strlen($edition)) == $edition) { |
84 | foreach (glob($dir . '/*') as $file) { |
85 | $dest = $this->databasePath . '/' . pathinfo($file, PATHINFO_BASENAME); |
86 | if (file_exists($dest)) { |
87 | unlink($dest); |
88 | } |
89 | rename($file, $dest); |
90 | } |
91 | rmdir($dir); |
92 | break; |
93 | } |
94 | } |
95 | } |
96 | } |