@@ -19,7 +19,11 @@ class MaxMind
1919 * Url
2020 */
2121 const URL = 'https://magefan.com/media/geoip/GeoLite2-Country.mmdb ' ;
22+
23+ const URL_CITY = 'https://magefan.com/media/geoip/GeoLite2-City.mmdb ' ;
24+
2225 const URL_API = 'https://download.maxmind.com/app/geoip_download ' ;
26+
2327 /**
2428 * @var \Magento\Framework\Filesystem\DirectoryList
2529 */
@@ -111,28 +115,33 @@ public function updateByMagefanServer()
111115 {
112116 $ dbPath = $ this ->_dir ->getPath ('var ' ) . '/magefan/geoip ' ;
113117 $ this ->createDir ($ dbPath );
114- $ url = self ::URL ;
115-
116- $ ch = curl_init ();
117- curl_setopt ($ ch , CURLOPT_URL , $ url );
118- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
119- //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
120- $ result = curl_exec ($ ch );
121- if (!$ result ) {
122- throw new \Magento \Framework \Exception \LocalizedException (__ ('Can not download GeoLite2-Country.mmdb file. ' ));
123- }
124- $ http_code = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
125- if ($ http_code != 200 ) {
126- throw new \Magento \Framework \Exception \LocalizedException (__ ('File download failed. Http code: %1. ' , $ http_code ) );
127- }
128- curl_close ($ ch );
129118
130- $ output_filename = $ dbPath . '/ ' . 'GeoLite2-Country.mmdb ' ;
131- $ fp = fopen ($ output_filename , 'w ' );
132- if (!fwrite ($ fp , $ result )) {
133- throw new \Magento \Framework \Exception \LocalizedException (__ ('Can not save or overwrite GeoLite2-Country.mmdb file. ' ));
119+ foreach ([self ::URL , self ::URL_CITY ] as $ url ) {
120+ $ ch = curl_init ();
121+ curl_setopt ($ ch , CURLOPT_URL , $ url );
122+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
123+
124+ $ result = curl_exec ($ ch );
125+ if (!$ result ) {
126+ throw new \Magento \Framework \Exception \LocalizedException (__ ('Can not download GeoLite2-Country.mmdb file. ' ));
127+ }
128+
129+ $ http_code = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
130+ if ($ http_code != 200 ) {
131+ throw new \Magento \Framework \Exception \LocalizedException (__ ('File download failed. Http code: %1. ' , $ http_code ) );
132+ }
133+
134+ curl_close ($ ch );
135+
136+ $ urlArray = explode ('/ ' , $ url );
137+ $ output_filename = $ dbPath . '/ ' . end ($ urlArray );
138+
139+ $ fp = fopen ($ output_filename , 'w ' );
140+ if (!fwrite ($ fp , $ result )) {
141+ throw new \Magento \Framework \Exception \LocalizedException (__ ('Can not save or overwrite GeoLite2-Country.mmdb file. ' ));
142+ }
143+ fclose ($ fp );
134144 }
135- fclose ($ fp );
136145
137146 return true ;
138147 }
@@ -148,60 +157,63 @@ private function updateByAPI()
148157 {
149158 $ dbPath = $ this ->_dir ->getPath ('var ' ) . '/magefan/geoip ' ;
150159 $ this ->createDir ($ dbPath );
151- $ url = self ::URL_API . '? ' . http_build_query ([
152- 'edition_id ' => 'GeoLite2-Country ' ,
153- 'suffix ' => 'tar.gz ' ,
154- 'license_key ' => $ this ->config ->getLicenseKey ()
155- ]);
156-
157- $ ch = curl_init ($ url );
158-
159- $ outputFilename = $ dbPath . DIRECTORY_SEPARATOR . 'GeoLite2-Country.tar.gz ' ;
160- $ fp = fopen ($ outputFilename , 'wb ' );
161-
162- curl_setopt_array ($ ch , array (
163- CURLOPT_HTTPGET => true ,
164- CURLOPT_BINARYTRANSFER => true ,
165- CURLOPT_HEADER => false ,
166- CURLOPT_FILE => $ fp ,
167- ));
168-
169- $ response = curl_exec ($ ch );
170-
171- if (!$ response ) {
172- throw new \Magento \Framework \Exception \LocalizedException (
173- __ ('Can not download GeoLite2-Country.tar.gz archive. ' )
174- );
175- }
176160
177- $ http_code = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
178- if ($ http_code != 200 ) {
179- throw new \Magento \Framework \Exception \LocalizedException (
180- __ ('File download failed. Http code: %1. Please check the license key. ' , $ http_code )
181- );
182- }
161+ foreach (['GeoLite2-Country ' , 'GeoLite2-City ' ] as $ file ) {
162+ $ url = self ::URL_API . '? ' . http_build_query ([
163+ 'edition_id ' => $ file ,
164+ 'suffix ' => 'tar.gz ' ,
165+ 'license_key ' => $ this ->config ->getLicenseKey ()
166+ ]);
167+
168+ $ ch = curl_init ($ url );
169+
170+ $ outputFilename = $ dbPath . DIRECTORY_SEPARATOR . $ file . '.tar.gz ' ;
171+ $ fp = fopen ($ outputFilename , 'wb ' );
172+
173+ curl_setopt_array ($ ch , array (
174+ CURLOPT_HTTPGET => true ,
175+ CURLOPT_BINARYTRANSFER => true ,
176+ CURLOPT_HEADER => false ,
177+ CURLOPT_FILE => $ fp ,
178+ ));
179+
180+ $ response = curl_exec ($ ch );
183181
184- curl_close ($ ch );
182+ if (!$ response ) {
183+ throw new \Magento \Framework \Exception \LocalizedException (
184+ __ ('Can not download ' . $ file . '.tar.gz archive. ' )
185+ );
186+ }
187+
188+ $ http_code = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
189+ if ($ http_code != 200 ) {
190+ throw new \Magento \Framework \Exception \LocalizedException (
191+ __ ('File download failed. Http code: %1. Please check the license key. ' , $ http_code )
192+ );
193+ }
185194
186- $ unpackGz = $ this ->gz ->unpack ($ outputFilename , $ dbPath . DIRECTORY_SEPARATOR );
187- $ unpackTar = $ this ->tar ->unpack ($ unpackGz , $ dbPath . DIRECTORY_SEPARATOR );
188- $ dir = $ this ->_file ->getDirectoriesList ($ unpackTar );
189- $ this ->_file ->mv ($ dir [0 ] . '/GeoLite2-Country.mmdb ' , $ unpackTar . 'GeoLite2-Country.mmdb ' );
195+ curl_close ($ ch );
190196
191- $ this ->_file ->open (['path ' => $ unpackTar ]);
192- $ list = $ this ->_file ->ls ();
193- $ this ->_file ->close ();
197+ $ unpackGz = $ this ->gz ->unpack ($ outputFilename , $ dbPath . DIRECTORY_SEPARATOR );
198+ $ unpackTar = $ this ->tar ->unpack ($ unpackGz , $ dbPath . DIRECTORY_SEPARATOR );
199+ $ dir = $ this ->_file ->getDirectoriesList ($ unpackTar );
200+ $ this ->_file ->mv ($ dir [0 ] . '/ ' . $ file . '.mmdb ' , $ unpackTar . $ file . '.mmdb ' );
194201
195- foreach ($ list as $ info ) {
196- if ($ info ['text ' ] !== 'GeoLite2-Country.mmdb ' ) {
197- if (isset ($ info ['id ' ])) {
198- $ this ->_file ->rmdirRecursive ($ info ['id ' ]);
202+ $ this ->_file ->open (['path ' => $ unpackTar ]);
203+ $ list = $ this ->_file ->ls ();
204+ $ this ->_file ->close ();
205+
206+ foreach ($ list as $ info ) {
207+ if (!in_array ($ info ['text ' ], ['GeoLite2-Country.mmdb ' , 'GeoLite2-City.mmdb ' ])) {
208+ if (isset ($ info ['id ' ])) {
209+ $ this ->_file ->rmdirRecursive ($ info ['id ' ]);
210+ }
211+ $ this ->_file ->rm ($ info ['text ' ]);
199212 }
200- $ this ->_file ->rm ($ info ['text ' ]);
201213 }
202- }
203214
204- fclose ($ fp );
215+ fclose ($ fp );
216+ }
205217
206218 return true ;
207219 }
0 commit comments