Files
ltsecurityinc/app/code/Plumrocket/GeoIPLookup/Model/Data/Import/AbstractModel.php
T

394 lines
9.8 KiB
PHP
Executable File

<?php
/**
* Plumrocket Inc.
*
* NOTICE OF LICENSE
*
* This source file is subject to the End-user License Agreement
* that is available through the world-wide-web at this URL:
* http://wiki.plumrocket.net/wiki/EULA
* If you are unable to obtain it through the world-wide-web, please
* send an email to support@plumrocket.com so we can send you a copy immediately.
*
* @package Plumrocket_GeoIPLookup
* @copyright Copyright (c) 2018 Plumrocket Inc. (http://www.plumrocket.com)
* @license http://wiki.plumrocket.net/wiki/EULA End-user License Agreement
*/
namespace Plumrocket\GeoIPLookup\Model\Data\Import;
use Magento\Directory\Model\ResourceModel\Country\CollectionFactory;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\File\Csv;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Io\File;
use Magento\Framework\HTTP\Client\Curl;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Plumrocket\GeoIPLookup\Helper\Config;
use Plumrocket\GeoIPLookup\Model\MmdbReader;
class AbstractModel
{
/**
* @var int
*/
public $steps = 1;
/**
* @var int
*/
public $step = 1;
/**
* @var string
*/
public $dataName = '';
/**
* @var string
*/
public $fileName = '';
/**
* @var string
*/
public $dataLabel = '';
/**
* @var string
*/
public $pathSourceFile = '';
/**
* @var string
*/
public $pathVersionsFile = Config::PATH_VERSION_FILE;
/**
* @var string
*/
public $versionsFileName = 'versions.csv';
/**
* @var string
*/
public $progressFileName = 'progress.csv';
/**
* @var \Magento\Directory\Model\ResourceModel\Country\Collection/null
*/
public $countryCollection = null;
/**
* @var \Magento\Framework\Filesystem\Io\File
*/
private $ioFile;
/**
* @var \Magento\Framework\Filesystem
*/
private $filesystem;
/**
* @var \Magento\Framework\File\Csv
*/
private $csv;
/**
* @var \Magento\Framework\Stdlib\DateTime\DateTime
*/
private $date;
/**
* @var \Magento\Framework\HTTP\Client\Curl
*/
private $curl;
/**
* @var \Magento\Directory\Model\ResourceModel\Country\CollectionFactory
*/
public $countryCollectionFactory;
/**
* @var \Plumrocket\GeoIPLookup\Model\MmdbReader
*/
private $mmdbReader;
/**
* AbstractModel constructor.
*
* @param \Magento\Framework\Filesystem\Io\File $ioFile
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\Framework\File\Csv $csv
* @param \Magento\Framework\Stdlib\DateTime\DateTime $date
* @param \Magento\Framework\HTTP\Client\Curl $curl
* @param \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory
* @param \Plumrocket\GeoIPLookup\Model\MmdbReader $mmdbReader
*/
public function __construct(
File $ioFile,
Filesystem $filesystem,
Csv $csv,
DateTime $date,
Curl $curl,
CollectionFactory $countryCollectionFactory,
MmdbReader $mmdbReader
) {
$this->ioFile = $ioFile;
$this->filesystem = $filesystem;
$this->csv = $csv;
$this->date = $date;
$this->curl = $curl;
$this->countryCollectionFactory = $countryCollectionFactory;
$this->mmdbReader = $mmdbReader;
}
/**
* @param $url
* @return float|int
*/
public function retrieveRemoteFileSize($url)
{
$contentLength = 0;
$this->curl->setOptions([
CURLOPT_NOBODY => true,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true
]);
$this->curl->get($url);
$data = $this->curl->getBody();
if ($data) {
$status = 404;
if (preg_match("/^HTTP\/1\.[01] (\d\d\d)/", $data, $matches)) {
$status = (int)$matches[1];
}
if (($status > 300 && $status <= 308)
|| (preg_match("/Content-Length: (\d+)/", $data, $matches) && $status === 200)
) {
$contentLength = (int)$matches[1];
}
}
return $contentLength;
}
/**
* @return array
*/
public function autoImportData(): array
{
set_time_limit(0);
$copyFile = false;
$importProgress = [
'status' => 'process',
'total' => 100,
'exec' => 1,
'step' => $this->step,
'message' => __('Download in Progress...'),
'remote_file_size' => $this->retrieveRemoteFileSize($this->pathSourceFile),
'local_file_name' => $this->getDataFile()
];
$this->setProgress($importProgress);
if ($this->ioFile->fileExists($this->getDataFile(), true)) {
$copyFile = true;
} else {
$this->ioFile->setAllowCreateFolders(true);
if ($this->ioFile->createDestinationDir($this->getDataPath())) {
$copyFile = $this->ioFile->cp($this->getPathSourceFile(), $this->getDataFile());
}
}
$this->mmdbReader->refresh();
$result = [
'status' => 'success',
'message' => $this->getInstalledMessage()
];
if (! $copyFile) {
$lastError = error_get_last();
$result = ['status' => 'fail', 'message' => $lastError['message']];
}
return $result;
}
/**
* @return string
*/
private function getPathSourceFile(): string
{
if (!empty($this->pathSourceFile)) {
$this->pathSourceFile .= '?t=' . $this->date->gmtTimestamp();
}
return $this->pathSourceFile;
}
/**
* @param $importProgress
*/
public function setProgress($importProgress)
{
$this->ioFile->setAllowCreateFolders(true);
if ($this->ioFile->createDestinationDir($this->getDataPath())) {
$this->csv->saveData(
$this->getProgressFile(),
[$importProgress]
);
}
}
/**
* @return array
*/
public function getProgress()
{
if ($this->ioFile->fileExists($this->getProgressFile(), true)) {
$csvData = $this->csv->getData($this->getProgressFile());
if (isset($csvData[0][6])
&& isset($csvData[0][5])
&& $this->ioFile->fileExists($csvData[0][6])
) {
$total = $csvData[0][5];
$exec = filesize($csvData[0][6]);
} else {
$total = (int)$csvData[0][1];
$exec = (int)$csvData[0][2];
}
$importProgress = [
'status' => $csvData[0][0],
'total' => $total,
'exec' => $exec,
'step' => (int)$csvData[0][3],
'message' => $csvData[0][4]
];
} else {
$importProgress = [
'status' => 'process',
'total' => 100,
'exec' => 1,
'step' => $this->step,
'message' => __('Download in Progress...')
];
}
return $importProgress;
}
/**
* @return mixed
*/
public function getInstalledVersion()
{
return ($metadata = $this->mmdbReader->metadata()) ? $metadata['buildDate'] : null;
}
/**
* @return string
*/
public function getInstalledMessage(): string
{
$metadata = $this->mmdbReader->metadata();
return $metadata ? (string)__(
'Installed Database - %1 (Build Date: %2)',
$metadata['databaseType'],
$metadata['buildDate']
) : '';
}
/**
* @return bool|mixed
*/
public function isLatestInstalledVersion()
{
$result = false;
$installedBuildDate = $this->getInstalledVersion();
if (!$installedBuildDate) {
return false;
}
$existVersion = null;
$copyFile = false;
$this->ioFile->setAllowCreateFolders(true);
if ($this->ioFile->createDestinationDir($this->getDataPath())) {
$copyFile = $this->ioFile->cp($this->pathVersionsFile, $this->getVersionsFile());
}
if ($copyFile) {
$csvData = $this->csv->getData($this->getVersionsFile());
foreach ($csvData as $data) {
if ($data[0] == $this->getDataName()) {
$existVersion = [
'data_name' => $data[0],
'file_date' => $data[1]
];
break;
}
}
$this->ioFile->rm($this->getVersionsFile());
if ($existVersion) {
$result = strtotime($existVersion['file_date']) === strtotime($installedBuildDate);
}
}
return $result;
}
/**
* @return string
*/
public function getDataName(): string
{
return $this->dataName;
}
/**
* @return string
*/
public function getDataLabel()
{
return $this->dataLabel;
}
/**
* @return string
*/
public function getDataPath()
{
return $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)
->getAbsolutePath(Config::LOCAL_PATH);
}
/**
* @return string
*/
public function getDataFile()
{
return $this->getDataPath() . $this->fileName;
}
/**
* @return string
*/
public function getVersionsFile()
{
return $this->getDataPath() . $this->versionsFileName;
}
/**
* @return string
*/
public function getProgressFile()
{
return $this->getDataPath() . $this->progressFileName;
}
}