|
2 | 2 |
|
3 | 3 | namespace Torann\GeoIP\Services; |
4 | 4 |
|
| 5 | +use PharData; |
5 | 6 | use Exception; |
6 | 7 | use GeoIp2\Database\Reader; |
7 | 8 |
|
@@ -66,27 +67,97 @@ public function update() |
66 | 67 | throw new Exception('Database path not set in config file.'); |
67 | 68 | } |
68 | 69 |
|
69 | | - // Get settings |
70 | | - $url = $this->config('update_url'); |
71 | | - $path = $this->config('database_path'); |
| 70 | + $this->withTemporaryDirectory(function ($directory) { |
| 71 | + $tarFile = sprintf('%s/maxmind.tar.gz', $directory); |
72 | 72 |
|
73 | | - // Get header response |
74 | | - $headers = get_headers($url); |
| 73 | + file_put_contents($tarFile, fopen($this->config('update_url'), 'r')); |
75 | 74 |
|
76 | | - if (substr($headers[0], 9, 3) != '200') { |
77 | | - throw new Exception('Unable to download database. (' . substr($headers[0], 13) . ')'); |
| 75 | + $archive = new PharData($tarFile); |
| 76 | + |
| 77 | + $file = $this->findDatabaseFile($archive); |
| 78 | + |
| 79 | + $relativePath = "{$archive->getFilename()}/{$file->getFilename()}"; |
| 80 | + |
| 81 | + $archive->extractTo($directory, $relativePath); |
| 82 | + |
| 83 | + file_put_contents($this->config('database_path'), fopen("{$directory}/{$relativePath}", 'r')); |
| 84 | + }); |
| 85 | + |
| 86 | + return "Database file ({$this->config('database_path')}) updated."; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Provide a temporary directory to perform operations in and and ensure |
| 91 | + * it is removed afterwards. |
| 92 | + * |
| 93 | + * @param callable $callback |
| 94 | + * @return void |
| 95 | + */ |
| 96 | + protected function withTemporaryDirectory(callable $callback) |
| 97 | + { |
| 98 | + $directory = tempnam(sys_get_temp_dir(), 'maxmind'); |
| 99 | + |
| 100 | + if (file_exists($directory)) { |
| 101 | + unlink($directory); |
| 102 | + } |
| 103 | + |
| 104 | + mkdir($directory); |
| 105 | + |
| 106 | + try { |
| 107 | + $callback($directory); |
| 108 | + } finally { |
| 109 | + $this->deleteDirectory($directory); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Recursively search the given archive to find the .mmdb file. |
| 115 | + * |
| 116 | + * @param \PharData $archive |
| 117 | + * @return mixed |
| 118 | + * @throws \Exception |
| 119 | + */ |
| 120 | + protected function findDatabaseFile($archive) |
| 121 | + { |
| 122 | + foreach ($archive as $file) { |
| 123 | + if ($file->isDir()) { |
| 124 | + return $this->findDatabaseFile(new PharData($file->getPathName())); |
| 125 | + } |
| 126 | + |
| 127 | + if (pathinfo($file, PATHINFO_EXTENSION) === 'mmdb') { |
| 128 | + return $file; |
| 129 | + } |
78 | 130 | } |
79 | 131 |
|
80 | | - // Download zipped database to a system temp file |
81 | | - $tmpFile = tempnam(sys_get_temp_dir(), 'maxmind'); |
82 | | - file_put_contents($tmpFile, fopen($url, 'r')); |
| 132 | + throw new Exception('Database file could not be found within archive.'); |
| 133 | + } |
83 | 134 |
|
84 | | - // Unzip and save database |
85 | | - file_put_contents($path, gzopen($tmpFile, 'r')); |
| 135 | + /** |
| 136 | + * Recursively delete the given directory. |
| 137 | + * |
| 138 | + * @param string $directory |
| 139 | + * @return mixed |
| 140 | + */ |
| 141 | + protected function deleteDirectory(string $directory) |
| 142 | + { |
| 143 | + if (!file_exists($directory)) { |
| 144 | + return true; |
| 145 | + } |
86 | 146 |
|
87 | | - // Remove temp file |
88 | | - @unlink($tmpFile); |
| 147 | + if (!is_dir($directory)) { |
| 148 | + return unlink($directory); |
| 149 | + } |
| 150 | + |
| 151 | + foreach (scandir($directory) as $item) { |
| 152 | + if ($item == '.' || $item == '..') { |
| 153 | + continue; |
| 154 | + } |
| 155 | + |
| 156 | + if (!$this->deleteDirectory($directory . DIRECTORY_SEPARATOR . $item)) { |
| 157 | + return false; |
| 158 | + } |
| 159 | + } |
89 | 160 |
|
90 | | - return "Database file ({$path}) updated."; |
| 161 | + return rmdir($directory); |
91 | 162 | } |
92 | 163 | } |
0 commit comments