Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 30
GeoipUpdater
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
72
0.00% covered (danger)
0.00%
0 / 30
 __construct
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getDatabase
0.00% covered (danger)
0.00%
0 / 1
56
0.00% covered (danger)
0.00%
0 / 28
1<?php
2
3namespace Miniframe\Statistics\Service;
4
5class 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_HTTPGET => true,
64            CURLOPT_HEADER => false,
65            CURLOPT_FILE => $fh,
66        ]);
67        curl_exec($ch);
68        curl_close($ch);
69        fclose($fh);
70
71        // Step 2: unzip .tar.gz file
72        if (file_exists($tarFile)) {
73            unlink($tarFile);
74        }
75        $ungzip = new \PharData($tarGzFile);
76        $ungzip->decompress();
77
78        // Step 3: unpack .tar file
79        $untar = new \PharData($tarFile);
80        $untar->extractTo($this->databasePath);
81        foreach (glob($this->databasePath . '/*', GLOB_ONLYDIR) as $dir) {
82            if (substr(pathinfo($dir, PATHINFO_BASENAME), 0, strlen($edition)) == $edition) {
83                foreach (glob($dir . '/*') as $file) {
84                    $dest = $this->databasePath . '/' . pathinfo($file, PATHINFO_BASENAME);
85                    if (file_exists($dest)) {
86                        unlink($dest);
87                    }
88                    rename($file, $dest);
89                }
90                rmdir($dir);
91                break;
92            }
93        }
94    }
95}