Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
gtl
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
gtl
Commits
91c7379e
Commit
91c7379e
authored
1 year ago
by
Martin Stadelmann
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3359176
by martinstadelmann: PHP 8.1 compatibility and Drupal 10 readiness
parent
16704d85
No related branches found
No related tags found
1 merge request
!1
Issue #3359176 by martinstadelmann: PHP 8.1 compatibility and Drupal 10 readiness
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
gtl.libraries.yml
+1
-1
1 addition, 1 deletion
gtl.libraries.yml
gtl.module
+6
-4
6 additions, 4 deletions
gtl.module
src/Plugin/rest/resource/GeoTrackingResource.php
+28
-24
28 additions, 24 deletions
src/Plugin/rest/resource/GeoTrackingResource.php
with
35 additions
and
29 deletions
gtl.libraries.yml
+
1
−
1
View file @
91c7379e
...
...
@@ -5,4 +5,4 @@ datalayer:
dependencies
:
-
core/jquery
-
core/jquery.cookie
-
core/drupalSettings
\ No newline at end of file
-
core/drupalSettings
This diff is collapsed.
Click to expand it.
gtl.module
+
6
−
4
View file @
91c7379e
<?php
use
\Drupal\Core\Entity\EntityInterface
;
use
\Drupal\Core\Entity\Display\EntityViewDisplayInterface
;
/**
* @file
* Contains Geo Tracking Limit module.
*/
/**
* Implements hook_page_attachments().
...
...
@@ -10,5 +12,5 @@ function gtl_page_attachments(array &$build) {
$build
[
'#cache'
][
'contexts'
][]
=
'ip'
;
$build
[
'#attached'
][
'library'
][]
=
'gtl/datalayer'
;
$build
[
'#attached'
][
'drupalSettings'
][
'gtl'
][
'check'
]
=
true
;
}
\ No newline at end of file
$build
[
'#attached'
][
'drupalSettings'
][
'gtl'
][
'check'
]
=
TRUE
;
}
This diff is collapsed.
Click to expand it.
src/Plugin/rest/resource/GeoTrackingResource.php
+
28
−
24
View file @
91c7379e
<?php
namespace
Drupal\gtl\Plugin\rest\resource
;
use
Drupal\rest\Plugin\ResourceBase
;
...
...
@@ -18,47 +19,49 @@ use Drupal\rest\ResourceResponse;
class
GeoTrackingResource
extends
ResourceBase
{
/**
* Sets a cookie and returns true if tracking is allowed
* in the geographic region of the visitor (CH)
*
* Sets a cookie and returns true if tracking is allowed.
*
* Depends on the geographic region of the visitor (CH).
*
* @return \Drupal\rest\ResourceResponse
* The data for serialization before sending the response.
*/
public
function
get
()
{
\Drupal
::
service
(
'page_cache_kill_switch'
)
->
trigger
();
$cookies
=
\Drupal
::
request
()
->
cookies
;
// Only check geolocation if a cookie is not present
// Only check geolocation if a cookie is not present
.
if
(
$cookies
->
has
(
'gtl'
))
{
if
(
$cookies
->
get
(
'gtl'
)
>
0
)
{
$response
=
[
TRUE
];
}
else
{
}
else
{
$response
=
[
FALSE
];
}
}
else
{
// Geolocate visitor
}
else
{
// Geolocate visitor.
/** @var \Drupal\smart_ip\SmartIpLocation $location */
$location
=
\Drupal
::
service
(
'smart_ip.smart_ip_location'
);
// \Drupal::logger('gtl')->notice('Smart IP Country: ' . $location->get('countryCode'));
if
(
$location
->
get
(
'countryCode'
)
==
'CH'
)
{
// Set a cookie for trackable visitors
// Set a cookie for trackable visitors
.
$this
->
setGeoTrackingCookie
(
'1'
);
$response
=
[
TRUE
];
}
else
{
// Set a cookie for non-trackable visitors
}
else
{
// Set a cookie for non-trackable visitors.
$this
->
setGeoTrackingCookie
(
'-1'
);
$response
=
[
FALSE
];
}
// Remove smart_ip data from user
// Remove smart_ip data from user
.
$location
->
delete
();
// Remove smart_ip data from Drupal session
// Prevents creation of session for anonymous visitors
// Prevents creation of session for anonymous visitors
.
$request
=
\Drupal
::
request
();
$session
=
$request
->
getSession
();
$session
->
remove
(
'smart_ip'
);
...
...
@@ -68,16 +71,17 @@ class GeoTrackingResource extends ResourceBase {
}
/**
* Sets the geo tracking cookie
* Sets the geo tracking cookie
.
*/
protected
function
setGeoTrackingCookie
(
$value
)
{
setcookie
(
'gtl'
,
$value
,
[
'expires'
=>
0
,
'path'
=>
'/'
,
'domain'
=>
"."
.
str_replace
(
"www."
,
""
,
\Drupal
::
request
()
->
getHost
()),
'secure'
=>
true
,
'httponly'
=>
false
,
'samesite'
=>
'None'
]);
'secure'
=>
TRUE
,
'httponly'
=>
FALSE
,
'samesite'
=>
'None'
,
]);
}
}
\ No newline at end of file
}
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