66
77namespace Magefan \GeoIp \Model \GeoIpDatabase ;
88
9+ use Magefan \GeoIp \Model \Config ;
10+ use Magento \Framework \Archive \Gz ;
11+ use Magento \Framework \Archive \Tar ;
912/**
1013 * Class MaxMind
1114 * @package Magefan\GeoIp\Model\GeoIpDatabase
@@ -16,6 +19,7 @@ class MaxMind
1619 * Url
1720 */
1821 const URL = 'https://magefan.com/media/geoip/GeoLite2-Country.mmdb ' ;
22+ const URL_API = 'https://download.maxmind.com/app/geoip_download ' ;
1923 /**
2024 * @var \Magento\Framework\Filesystem\DirectoryList
2125 */
@@ -29,20 +33,44 @@ class MaxMind
2933 */
3034 protected $ _logger ;
3135
36+ /**
37+ * @var Config
38+ */
39+ private $ config ;
40+
41+ /**
42+ * @var Gz
43+ */
44+ private $ gz ;
45+
46+ /**
47+ * @var Tar
48+ */
49+ private $ tar ;
50+
3251 /**
3352 * MaxMind constructor.
3453 * @param \Magento\Framework\Filesystem\DirectoryList $dir
3554 * @param \Magento\Framework\Filesystem\Io\File $file
3655 * @param \Psr\Log\LoggerInterface $logger
56+ * @param Config $config
57+ * @param Gz $gz
58+ * @param Tar $tar
3759 */
3860 public function __construct (
3961 \Magento \Framework \Filesystem \DirectoryList $ dir ,
4062 \Magento \Framework \Filesystem \Io \File $ file ,
41- \Psr \Log \LoggerInterface $ logger
63+ \Psr \Log \LoggerInterface $ logger ,
64+ Config $ config ,
65+ Gz $ gz ,
66+ Tar $ tar
4267 ) {
4368 $ this ->_dir = $ dir ;
4469 $ this ->_file = $ file ;
4570 $ this ->_logger = $ logger ;
71+ $ this ->config = $ config ;
72+ $ this ->gz = $ gz ;
73+ $ this ->tar = $ tar ;
4674 }
4775
4876 /**
@@ -67,6 +95,19 @@ protected function createDir($dirPath)
6795 * @throws \Magento\Framework\Exception\LocalizedException
6896 */
6997 public function update ()
98+ {
99+ if ($ this ->config ->getLicenseKey ()) {
100+ return $ this ->updateByAPI ();
101+ } else {
102+ return $ this ->updateByMagefanServer ();
103+ }
104+ }
105+ /**
106+ * @return bool
107+ * @throws \Magento\Framework\Exception\FileSystemException
108+ * @throws \Magento\Framework\Exception\LocalizedException
109+ */
110+ public function updateByMagefanServer ()
70111 {
71112 $ dbPath = $ this ->_dir ->getPath ('var ' ) . '/magefan/geoip ' ;
72113 $ this ->createDir ($ dbPath );
@@ -78,19 +119,88 @@ public function update()
78119 //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
79120 $ result = curl_exec ($ ch );
80121 if (!$ result ) {
81- throw new \Magento \Framework \Exception \LocalizedException (__ ('Can not download file GeoLite2-Country.mmdb ' ));
122+ throw new \Magento \Framework \Exception \LocalizedException (__ ('Can not download GeoLite2-Country.mmdb file. ' ));
82123 }
83124 $ http_code = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
84125 if ($ http_code != 200 ) {
85- throw new \Magento \Framework \Exception \LocalizedException (__ ('Fail download file . Http code: %1 ' , $ http_code ) );
126+ throw new \Magento \Framework \Exception \LocalizedException (__ ('File download failed . Http code: %1. ' , $ http_code ) );
86127 }
87128 curl_close ($ ch );
88129
89130 $ output_filename = $ dbPath . '/ ' . 'GeoLite2-Country.mmdb ' ;
90131 $ fp = fopen ($ output_filename , 'w ' );
91132 if (!fwrite ($ fp , $ result )) {
92- throw new \Magento \Framework \Exception \LocalizedException (__ ('Can not save or overwrite file GeoLite2-Country.mmdb ' ));
133+ throw new \Magento \Framework \Exception \LocalizedException (__ ('Can not save or overwrite GeoLite2-Country.mmdb file. ' ));
134+ }
135+ fclose ($ fp );
136+
137+ return true ;
138+ }
139+
140+ /**
141+ * Get GeoIP Databse via MaxMind API
142+ *
143+ * @return bool
144+ * @throws \Magento\Framework\Exception\FileSystemException
145+ * @throws \Magento\Framework\Exception\LocalizedException
146+ */
147+ private function updateByAPI ()
148+ {
149+ $ dbPath = $ this ->_dir ->getPath ('var ' ) . '/magefan/geoip ' ;
150+ $ 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+ );
93175 }
176+
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+ }
183+
184+ curl_close ($ ch );
185+
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 ' );
190+
191+ $ this ->_file ->open (['path ' => $ unpackTar ]);
192+ $ list = $ this ->_file ->ls ();
193+ $ this ->_file ->close ();
194+
195+ foreach ($ list as $ info ) {
196+ if ($ info ['text ' ] !== 'GeoLite2-Country.mmdb ' ) {
197+ if (isset ($ info ['id ' ])) {
198+ $ this ->_file ->rmdirRecursive ($ info ['id ' ]);
199+ }
200+ $ this ->_file ->rm ($ info ['text ' ]);
201+ }
202+ }
203+
94204 fclose ($ fp );
95205
96206 return true ;
0 commit comments