Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
webform_geoip_restriction
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
webform_geoip_restriction
Commits
a7f35dff
Commit
a7f35dff
authored
7 months ago
by
Sylvain MICHEL
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3462846
by netsliver: tronovav/geoip2-update seems doens't work anymore
parent
fa1b36a4
No related branches found
Branches containing commit
Tags
1.0.0-alpha19
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Form/SettingsForm.php
+6
-0
6 additions, 0 deletions
src/Form/SettingsForm.php
src/UpdateMaxmindManager.php
+42
-20
42 additions, 20 deletions
src/UpdateMaxmindManager.php
with
48 additions
and
20 deletions
src/Form/SettingsForm.php
+
6
−
0
View file @
a7f35dff
...
...
@@ -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
);
...
...
This diff is collapsed.
Click to expand it.
src/UpdateMaxmindManager.php
+
42
−
20
View file @
a7f35dff
...
...
@@ -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
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment