Skip to content
Snippets Groups Projects
Commit a7f35dff authored by Sylvain MICHEL's avatar Sylvain MICHEL
Browse files

Issue #3462846 by netsliver: tronovav/geoip2-update seems doens't work anymore

parent fa1b36a4
No related branches found
Tags 1.0.0-alpha19
No related merge requests found
......@@ -30,6 +30,11 @@ final class SettingsForm extends ConfigFormBase {
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$form['maxmind_account'] = [
'#type' => 'textfield',
'#title' => $this->t('Maxmind Account ID'),
'#default_value' => $this->config('webform_geoip_restriction.settings')->get('maxmind_account'),
];
$form['maxmind_license'] = [
'#type' => 'textfield',
'#title' => $this->t('Maxmind License Key'),
......@@ -43,6 +48,7 @@ final class SettingsForm extends ConfigFormBase {
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$this->config('webform_geoip_restriction.settings')
->set('maxmind_account', $form_state->getValue('maxmind_account'))
->set('maxmind_license', $form_state->getValue('maxmind_license'))
->save();
parent::submitForm($form, $form_state);
......
......@@ -6,13 +6,14 @@ namespace Drupal\webform_geoip_restriction;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\File\FileSystemInterface;
use tronovav\GeoIP2Update\Client as GeoIP2Update;
/**
* Service to update Maxmind Database.
*/
final class UpdateMaxmindManager implements UpdateMaxmindManagerInterface {
private const MAXMIND_URL = 'https://download.maxmind.com/geoip/databases/';
/**
* Constructs an UpdateMaxmindManager object.
*/
......@@ -27,28 +28,49 @@ final class UpdateMaxmindManager implements UpdateMaxmindManagerInterface {
public function updMaxmind(): bool|array {
$editions = ['GeoLite2-Country'];
$publicDir = $this->fileSystem->realpath('public://');
// Configuration.
$client = new GeoIP2Update([
'license_key' => $this->configFactory->get('webform_geoip_restriction.settings')->get('maxmind_license'),
'dir' => $publicDir,
'editions' => $editions,
]);
// Run update.
$client->run();
if ($client->errors()) {
return $client->errors();
}
else {
foreach ($editions as $edition) {
rename($publicDir . '/' . $edition . '/' . $edition . '.mmdb', $publicDir . '/' . $edition . '.mmdb');
$files = array_diff(scandir($publicDir . '/' . $edition), ['.', '..']);
foreach ($files as $file) {
unlink($publicDir . '/' . $edition . '/' . $file);
foreach ($editions as $edition) {
$ch = curl_init(self::MAXMIND_URL . $edition . '/download?' . http_build_query([
'suffix' => 'tar.gz',
]));
$archiveFile = $publicDir . DIRECTORY_SEPARATOR . $edition . '.tar.gz';
$fh = fopen($archiveFile, 'wb');
curl_setopt_array($ch, [
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_USERPWD => $this->configFactory->get('webform_geoip_restriction.settings')->get('maxmind_account') . ":" . $this->configFactory->get('webform_geoip_restriction.settings')->get('maxmind_license'),
CURLOPT_FILE => $fh,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
fclose($fh);
if ($response === FALSE || $httpCode !== 200) {
if (is_file($archiveFile)) {
unlink($archiveFile);
}
return FALSE;
}
else {
$phar = new \PharData($archiveFile);
if ($phar->current()->isDir()) {
$dir = $phar->current()->getPathname();
$dir = basename($dir);
$phar->extractTo($publicDir, NULL, TRUE);
rename($publicDir . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . $edition . '.mmdb', $publicDir . DIRECTORY_SEPARATOR . $edition . '.mmdb');
$files = array_diff(scandir($publicDir . DIRECTORY_SEPARATOR . $dir), [
'.',
'..',
]);
foreach ($files as $file) {
unlink($publicDir . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . $file);
}
rmdir($publicDir . DIRECTORY_SEPARATOR . $dir);
unlink($publicDir . DIRECTORY_SEPARATOR . $edition . '.tar.gz');
}
rmdir($publicDir . '/' . $edition);
}
return TRUE;
}
return TRUE;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment