-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from mageplaza/2.4-develop
v4.0.1
- Loading branch information
Showing
2 changed files
with
129 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,118 +1,128 @@ | ||
<?php | ||
/** | ||
* Mageplaza | ||
* | ||
* NOTICE OF LICENSE | ||
* | ||
* This source file is subject to the Mageplaza.com license that is | ||
* available through the world-wide-web at this URL: | ||
* https://www.mageplaza.com/LICENSE.txt | ||
* | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade this extension to newer | ||
* version in the future. | ||
* | ||
* @category Mageplaza | ||
* @package Mageplaza_GeoIP | ||
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/) | ||
* @license https://www.mageplaza.com/LICENSE.txt | ||
*/ | ||
|
||
namespace Mageplaza\GeoIP\Controller\Adminhtml\System\Config; | ||
|
||
use Exception; | ||
use Magento\Backend\App\Action; | ||
use Magento\Backend\App\Action\Context; | ||
use Magento\Framework\App\Filesystem\DirectoryList; | ||
use Magento\Framework\Controller\Result\Json; | ||
use Magento\Framework\Controller\Result\JsonFactory; | ||
use Mageplaza\GeoIP\Helper\Data as HelperData; | ||
use PharData; | ||
use function stream_get_wrappers; | ||
use function stream_wrapper_restore; | ||
|
||
/** | ||
* Class Geoip | ||
* @package Mageplaza\GeoIP\Controller\Adminhtml\System\Config | ||
*/ | ||
class Geoip extends Action | ||
{ | ||
/** | ||
* @var JsonFactory | ||
*/ | ||
protected $resultJsonFactory; | ||
|
||
/** | ||
* @var DirectoryList | ||
*/ | ||
protected $_directoryList; | ||
|
||
/** | ||
* @var HelperData | ||
*/ | ||
protected $_helperData; | ||
|
||
/** | ||
* Geoip constructor. | ||
* | ||
* @param Context $context | ||
* @param JsonFactory $resultJsonFactory | ||
* @param DirectoryList $directoryList | ||
* @param HelperData $helperData | ||
*/ | ||
public function __construct( | ||
Context $context, | ||
JsonFactory $resultJsonFactory, | ||
DirectoryList $directoryList, | ||
HelperData $helperData | ||
) { | ||
$this->resultJsonFactory = $resultJsonFactory; | ||
$this->_directoryList = $directoryList; | ||
$this->_helperData = $helperData; | ||
|
||
parent::__construct($context); | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function execute() | ||
{ | ||
$status = false; | ||
try { | ||
$path = $this->_directoryList->getPath('var') . '/Mageplaza/GeoIp/GeoIp'; | ||
if (!file_exists($path)) { | ||
mkdir($path, 0777, true); | ||
} | ||
$folder = scandir($path, true); | ||
$pathFile = $path . '/' . $folder[0] . '/GeoLite2-City.mmdb'; | ||
if (file_exists($pathFile)) { | ||
foreach (scandir($path . '/' . $folder[0], true) as $filename) { | ||
if ($filename == '..' || $filename == '.') { | ||
continue; | ||
} | ||
unlink($path . '/' . $folder[0] . '/' . $filename); | ||
} | ||
rmdir($path . '/' . $folder[0]); | ||
} | ||
|
||
file_put_contents($path . '/GeoLite2-City.tar.gz', fopen($this->_helperData->getDownloadPath(), 'r')); | ||
if (!in_array('phar', stream_get_wrappers(), true)) { | ||
stream_wrapper_restore('phar'); | ||
} | ||
|
||
$phar = new PharData($path . '/GeoLite2-City.tar.gz'); | ||
$phar->extractTo($path); | ||
$status = true; | ||
$message = __('Download library success!'); | ||
} catch (Exception $e) { | ||
$message = __('Can\'t download file. Please try again! %1', $e->getMessage()); | ||
} | ||
|
||
/** @var Json $result */ | ||
$result = $this->resultJsonFactory->create(); | ||
|
||
return $result->setData(['success' => $status, 'message' => $message]); | ||
} | ||
} | ||
<?php | ||
/** | ||
* Mageplaza | ||
* | ||
* NOTICE OF LICENSE | ||
* | ||
* This source file is subject to the Mageplaza.com license that is | ||
* available through the world-wide-web at this URL: | ||
* https://www.mageplaza.com/LICENSE.txt | ||
* | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade this extension to newer | ||
* version in the future. | ||
* | ||
* @category Mageplaza | ||
* @package Mageplaza_GeoIP | ||
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/) | ||
* @license https://www.mageplaza.com/LICENSE.txt | ||
*/ | ||
|
||
namespace Mageplaza\GeoIP\Controller\Adminhtml\System\Config; | ||
|
||
use Exception; | ||
use Magento\Backend\App\Action; | ||
use Magento\Backend\App\Action\Context; | ||
use Magento\Framework\App\Filesystem\DirectoryList; | ||
use Magento\Framework\Controller\Result\Json; | ||
use Magento\Framework\Controller\Result\JsonFactory; | ||
use Mageplaza\GeoIP\Helper\Data as HelperData; | ||
use Psr\Log\LoggerInterface; | ||
use PharData; | ||
use function stream_get_wrappers; | ||
use function stream_wrapper_restore; | ||
|
||
/** | ||
* Class Geoip | ||
* @package Mageplaza\GeoIP\Controller\Adminhtml\System\Config | ||
*/ | ||
class Geoip extends Action | ||
{ | ||
/** | ||
* @var JsonFactory | ||
*/ | ||
protected $resultJsonFactory; | ||
|
||
/** | ||
* @var DirectoryList | ||
*/ | ||
protected $_directoryList; | ||
|
||
/** | ||
* @var HelperData | ||
*/ | ||
protected $_helperData; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
protected $_logger; | ||
|
||
/** | ||
* Geoip constructor. | ||
* | ||
* @param Context $context | ||
* @param JsonFactory $resultJsonFactory | ||
* @param DirectoryList $directoryList | ||
* @param HelperData $helperData | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function __construct( | ||
Context $context, | ||
JsonFactory $resultJsonFactory, | ||
DirectoryList $directoryList, | ||
HelperData $helperData, | ||
LoggerInterface $logger | ||
) { | ||
$this->resultJsonFactory = $resultJsonFactory; | ||
$this->_directoryList = $directoryList; | ||
$this->_helperData = $helperData; | ||
$this->_logger = $logger; | ||
|
||
parent::__construct($context); | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function execute() | ||
{ | ||
$status = false; | ||
try { | ||
$path = $this->_directoryList->getPath('var') . '/Mageplaza/GeoIp/GeoIp'; | ||
if (!file_exists($path)) { | ||
mkdir($path, 0777, true); | ||
} | ||
$folder = scandir($path, true); | ||
$pathFile = $path . '/' . $folder[0] . '/GeoLite2-City.mmdb'; | ||
if (file_exists($pathFile)) { | ||
foreach (scandir($path . '/' . $folder[0], true) as $filename) { | ||
if ($filename == '..' || $filename == '.') { | ||
continue; | ||
} | ||
unlink($path . '/' . $folder[0] . '/' . $filename); | ||
} | ||
rmdir($path . '/' . $folder[0]); | ||
} | ||
|
||
file_put_contents($path . '/GeoLite2-City.tar.gz', fopen($this->_helperData->getDownloadPath(), 'r')); | ||
if (!in_array('phar', stream_get_wrappers(), true)) { | ||
stream_wrapper_restore('phar'); | ||
} | ||
|
||
$phar = new PharData($path . '/GeoLite2-City.tar.gz'); | ||
$phar->extractTo($path); | ||
$status = true; | ||
$message = __('Download library success!'); | ||
} catch (Exception $e) { | ||
$this->_logger->info($e->getMessage()); | ||
$message = __('Can\'t download file. Please try again! <br> You can find out more in the error log.' ); | ||
} | ||
|
||
/** @var Json $result */ | ||
$result = $this->resultJsonFactory->create(); | ||
|
||
return $result->setData(['success' => $status, 'message' => $message]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters