Skip to content
Snippets Groups Projects
Commit 91c7379e authored by Martin Stadelmann's avatar Martin Stadelmann
Browse files

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!1Issue #3359176 by martinstadelmann: PHP 8.1 compatibility and Drupal 10 readiness
......@@ -5,4 +5,4 @@ datalayer:
dependencies:
- core/jquery
- core/jquery.cookie
- core/drupalSettings
\ No newline at end of file
- core/drupalSettings
<?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;
}
<?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
}
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