Skip to content

Commit

Permalink
Merge pull request #14 from magefan/6959-maxmindapi
Browse files Browse the repository at this point in the history
Add download GeoIp Database via MaxMind API
  • Loading branch information
magefan authored Aug 30, 2022
2 parents 6c72324 + 643b1f7 commit d5f7f37
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 8 deletions.
9 changes: 7 additions & 2 deletions Block/Adminhtml/System/Config/Form/MaxMindInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $ele
{
$dirList = $this->_dir->getPath('var'). '/magefan/geoip/GeoLite2-Country.mmdb';

if (!file_exists($dirList)) {
try {
$this->maxMind->update();
} catch (\Exception $e) {
}
}

if (file_exists($dirList)) {
$modified = date("F d, Y.", filemtime($dirList));
} elseif ($this->maxMind->update()) {
$modified = date("F d, Y.", filemtime($dirList));
} else {
$modified = __('Can not download DB.');
}
Expand Down
8 changes: 6 additions & 2 deletions Controller/Adminhtml/Maxmind/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ public function __construct(
public function execute()
{
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
if ($this->maxMind->update()) {

try {
$this->maxMind->update();
$this->messageManager->addSuccessMessage('MaxMind GeoIP Database has been updated successfully.');
} else {
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addErrorMessage('Something went wrong while updating the GeoIP database.');
}

Expand Down
56 changes: 56 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
*/

namespace Magefan\GeoIp\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

class Config
{
const XML_PATH_LICENSE_KEY = 'mfgeoip/update_geoip/key';

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* Config constructor.
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
ScopeConfigInterface $scopeConfig
) {
$this->scopeConfig = $scopeConfig;
}

/**
* @param $path
* @param null $storeId
* @return mixed
*/
public function getConfig($path, $storeId = null)
{
return $this->scopeConfig->getValue(
$path,
ScopeInterface::SCOPE_STORE,
$storeId
);
}

/**
* @param null $storeId
* @return bool
*/
public function getLicenseKey($storeId = null)
{
return (string)$this->getConfig(
self::XML_PATH_LICENSE_KEY,
$storeId
);
}
}
118 changes: 114 additions & 4 deletions Model/GeoIpDatabase/MaxMind.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

namespace Magefan\GeoIp\Model\GeoIpDatabase;

use Magefan\GeoIp\Model\Config;
use Magento\Framework\Archive\Gz;
use Magento\Framework\Archive\Tar;
/**
* Class MaxMind
* @package Magefan\GeoIp\Model\GeoIpDatabase
Expand All @@ -16,6 +19,7 @@ class MaxMind
* Url
*/
const URL = 'https://magefan.com/media/geoip/GeoLite2-Country.mmdb';
const URL_API = 'https://download.maxmind.com/app/geoip_download';
/**
* @var \Magento\Framework\Filesystem\DirectoryList
*/
Expand All @@ -29,20 +33,44 @@ class MaxMind
*/
protected $_logger;

/**
* @var Config
*/
private $config;

/**
* @var Gz
*/
private $gz;

/**
* @var Tar
*/
private $tar;

/**
* MaxMind constructor.
* @param \Magento\Framework\Filesystem\DirectoryList $dir
* @param \Magento\Framework\Filesystem\Io\File $file
* @param \Psr\Log\LoggerInterface $logger
* @param Config $config
* @param Gz $gz
* @param Tar $tar
*/
public function __construct(
\Magento\Framework\Filesystem\DirectoryList $dir,
\Magento\Framework\Filesystem\Io\File $file,
\Psr\Log\LoggerInterface $logger
\Psr\Log\LoggerInterface $logger,
Config $config,
Gz $gz,
Tar $tar
) {
$this->_dir = $dir;
$this->_file = $file;
$this->_logger = $logger;
$this->config = $config;
$this->gz = $gz;
$this->tar = $tar;
}

/**
Expand All @@ -67,6 +95,19 @@ protected function createDir($dirPath)
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function update()
{
if ($this->config->getLicenseKey()) {
return $this->updateByAPI();
} else {
return $this->updateByMagefanServer();
}
}
/**
* @return bool
* @throws \Magento\Framework\Exception\FileSystemException
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function updateByMagefanServer()
{
$dbPath = $this->_dir->getPath('var') . '/magefan/geoip';
$this->createDir($dbPath);
Expand All @@ -78,19 +119,88 @@ public function update()
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
if (!$result) {
throw new \Magento\Framework\Exception\LocalizedException(__('Can not download file GeoLite2-Country.mmdb'));
throw new \Magento\Framework\Exception\LocalizedException(__('Can not download GeoLite2-Country.mmdb file.'));
}
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code != 200) {
throw new \Magento\Framework\Exception\LocalizedException(__('Fail download file. Http code: %1', $http_code) );
throw new \Magento\Framework\Exception\LocalizedException(__('File download failed. Http code: %1.', $http_code) );
}
curl_close($ch);

$output_filename = $dbPath . '/' . 'GeoLite2-Country.mmdb';
$fp = fopen($output_filename, 'w');
if (!fwrite($fp, $result)) {
throw new \Magento\Framework\Exception\LocalizedException(__('Can not save or overwrite file GeoLite2-Country.mmdb'));
throw new \Magento\Framework\Exception\LocalizedException(__('Can not save or overwrite GeoLite2-Country.mmdb file.'));
}
fclose($fp);

return true;
}

/**
* Get GeoIP Databse via MaxMind API
*
* @return bool
* @throws \Magento\Framework\Exception\FileSystemException
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function updateByAPI()
{
$dbPath = $this->_dir->getPath('var') . '/magefan/geoip';
$this->createDir($dbPath);
$url = self::URL_API . '?' . http_build_query([
'edition_id' => 'GeoLite2-Country',
'suffix' => 'tar.gz',
'license_key' => $this->config->getLicenseKey()
]);

$ch = curl_init($url);

$outputFilename = $dbPath . DIRECTORY_SEPARATOR . 'GeoLite2-Country.tar.gz';
$fp = fopen($outputFilename, 'wb');

curl_setopt_array($ch, array(
CURLOPT_HTTPGET => true,
CURLOPT_BINARYTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FILE => $fp,
));

$response = curl_exec($ch);

if (!$response) {
throw new \Magento\Framework\Exception\LocalizedException(
__('Can not download GeoLite2-Country.tar.gz archive.')
);
}

$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code != 200) {
throw new \Magento\Framework\Exception\LocalizedException(
__('File download failed. Http code: %1. Please check the license key.', $http_code)
);
}

curl_close($ch);

$unpackGz = $this->gz->unpack($outputFilename, $dbPath . DIRECTORY_SEPARATOR);
$unpackTar = $this->tar->unpack($unpackGz, $dbPath . DIRECTORY_SEPARATOR);
$dir = $this->_file->getDirectoriesList($unpackTar);
$this->_file->mv($dir[0] . '/GeoLite2-Country.mmdb', $unpackTar . 'GeoLite2-Country.mmdb');

$this->_file->open(['path' => $unpackTar]);
$list = $this->_file->ls();
$this->_file->close();

foreach ($list as $info) {
if ($info['text'] !== 'GeoLite2-Country.mmdb') {
if (isset($info['id'])) {
$this->_file->rmdirRecursive($info['id']);
}
$this->_file->rm($info['text']);
}
}

fclose($fp);

return true;
Expand Down
7 changes: 7 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
<field id="maxmind" translate="label comment" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<frontend_model>Magefan\GeoIp\Block\Adminhtml\System\Config\Form\MaxMindInfo</frontend_model>
</field>
<field id="key" translate="label comment" type="text" sortOrder="15" showInDefault="1" showInWebsite="0" showInStore="0">
<label>License Key (optional)</label>
<comment><![CDATA[
Enter <a title="Generate a License Key" target="_blank" rel="nofollow noopener" href="https://support.maxmind.com/hc/en-us/articles/4407111582235-Generate-a-License-Key">MaxMind License Key</a> and save config to update the MaxMind GeoIP Database directly from MaxMind and not via the Magefan server.
]]>
</comment>
</field>
<field id="listbutton" translate="label" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<frontend_model>Magefan\GeoIp\Block\Adminhtml\System\Config\Form\Button</frontend_model>
</field>
Expand Down

0 comments on commit d5f7f37

Please sign in to comment.