Skip to content

Use new Maxmind GeoLite2-Country URL and added option for License key #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 66 additions & 11 deletions Classes/Command/UpdateDB.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace UrbanTrout\SiteLanguageRedirection\Command;

use Archive_Tar;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\Console\Command\Command;
Expand All @@ -11,6 +12,7 @@
use TYPO3\CMS\Core\Error\Exception;
use TYPO3\CMS\Core\Http\RequestFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use UrbanTrout\SiteLanguageRedirection\Middleware\RedirectionMiddleware;

class UpdateDB extends Command implements LoggerAwareInterface
{
Expand All @@ -27,26 +29,77 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$requireLicenseKey = false;
$licenseKey = null;
$io = new SymfonyStyle($input, $output);
$io->title('Fetching database');
$path = Environment::getVarPath() . '/sitelanguageredirection/';
$tarFilename = 'GeoLite2-Country.tar';
$filename = 'GeoLite2-Country.mmdb';

/** @var RequestFactory $requestFactory */
$requestFactory = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Http\\RequestFactory');
$url = 'https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz';
/** @var \TYPO3\CMS\Core\Site\SiteFinder $siteFinder */
$siteFinder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Site\SiteFinder::class);

// Return a PSR-7 compliant response object.
$response = $requestFactory->request($url, 'GET');
$sites = $siteFinder->getAllSites();

// Get the content as a stream on a successful request.
if ($response->getStatusCode() === 200) {
if (strpos($response->getHeaderLine('Content-Type'), 'application/octet-stream') === 0) {
foreach ($sites as $site) {
$method = $site->getConfiguration()['SiteLanguageRedirectionMethod'];
if ($method !== RedirectionMiddleware::REDIRECT_METHOD_IPADDRESS) {
continue; //Skip this site
}
$requireLicenseKey = true;
$licenseKey = $site->getConfiguration()['SiteLanguageMaxmindLicenseKey'];
if (!empty($licenseKey)) {
break;
}
}

if ($requireLicenseKey) {
if (empty($licenseKey)) {
$logMessage = 'Maxmind license key not given.';
$io->error($logMessage);
$this->logger->error($logMessage);
throw new Exception($logMessage);
}
/** @var RequestFactory $requestFactory */
$requestFactory = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Http\\RequestFactory');
$url = 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key=' . $licenseKey . '&suffix=tar.gz';
// Return a PSR-7 compliant response object.
$response = $requestFactory->request($url, 'GET');

// Get the content as a stream on a successful request.
if ($response->getStatusCode() === 200 && strpos($response->getHeaderLine('Content-Type'), 'application/gzip') === 0) {
$content = gzdecode($response->getBody()->getContents());
$result = GeneralUtility::writeFileToTypo3tempDir($path . $filename, $content);
$result = GeneralUtility::writeFileToTypo3tempDir($path . $tarFilename, $content);

if (!empty($result)) {
$logMessage = 'Couldn\'t save DB file.';
$logMessage = 'Couldn\'t extract GZIP file.';
$io->error($logMessage);
$this->logger->error($logMessage);
throw new Exception($logMessage);
}

$tar = new Archive_Tar($path . $tarFilename);
$tarContent = $tar->listContent();
$dbFiles = array_filter($tarContent, function ($content) use ($filename) {
$baseName = pathinfo($content['filename'], PATHINFO_BASENAME);
return $baseName === $filename;
});

if (empty($dbFiles)) {
$logMessage = "Couldn\'t find file '{$filename}' TAR file.";
$io->error($logMessage);
$this->logger->error($logMessage);
throw new Exception($logMessage);
}
$dbFile = reset($dbFiles);
/** @var string $dbFilePath Contains the directory path containing the database file */
$dbFilePath = pathinfo($dbFile['filename'], PATHINFO_DIRNAME);

$result = $tar->extractList([$dbFile['filename']], $path, $dbFilePath, false, false);

if (!$result) {
$logMessage = 'Couldn\'t extract TAR file.';
$io->error($logMessage);
$this->logger->error($logMessage);
throw new Exception($logMessage);
Expand All @@ -56,10 +109,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->logger->info($logMessage);
$io->success($logMessage);
} else {
$logMessage = 'Couldn\'t fetch file from geolite.maxmind.com.';
$logMessage = 'Couldn\'t fetch file from download.maxmind.com.';
$io->error($logMessage);
$this->logger->error($logMessage);
throw new Exception($logMessage);
}
}
return 0;
}
}
10 changes: 9 additions & 1 deletion Configuration/SiteConfiguration/Overrides/sites.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@
],
];

$GLOBALS['SiteConfiguration']['site']['types']['0']['showitem'] = $GLOBALS['SiteConfiguration']['site']['types']['0']['showitem'] . ',--div--;Site Language Redirection,SiteLanguageRedirectionMethod';
$GLOBALS['SiteConfiguration']['site']['columns']['SiteLanguageMaxmindLicenseKey'] = [
'label' => 'Maxmind license key Method',
'description' => 'Enter license key for Maxmind GeoLite2-Country database when using IP address',
'config' => [
'type' => 'input'
],
];

$GLOBALS['SiteConfiguration']['site']['types']['0']['showitem'] = $GLOBALS['SiteConfiguration']['site']['types']['0']['showitem'] . ',--div--;Site Language Redirection,SiteLanguageRedirectionMethod,SiteLanguageMaxmindLicenseKey';
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
}
],
"require": {
"pear/archive_tar": "^1.4"
"typo3/cms-core": "^9.5.0||^10.4||^11",
"geoip2/geoip2": "^2.9.0"
},
Expand All @@ -33,7 +34,11 @@
},
"config": {
"vendor-dir": ".Build/vendor/",
"bin-dir": ".Build/bin/"
"bin-dir": ".Build/bin/",
"allow-plugins": {
"typo3/cms-composer-installers": true,
"typo3/class-alias-loader": true
}
},
"extra": {
"typo3/cms": {
Expand Down
Loading