diff --git a/Classes/Utility/GeoUtility.php b/Classes/Utility/GeoUtility.php index da947d4..b798da9 100644 --- a/Classes/Utility/GeoUtility.php +++ b/Classes/Utility/GeoUtility.php @@ -216,7 +216,9 @@ public function getGeoLocation(): ?Position $sessionUtility = GeneralUtility::makeInstance(SessionUtility::class); // If excluded ip, return null - + if($this->excluded) { + return null; + } // If debug position, return it if ($this->debugPosition) { @@ -224,17 +226,11 @@ public function getGeoLocation(): ?Position return $this->debugPosition; } - // Return null if frontend or frontend user is not initialized yet - if(!$GLOBALS['TSFE'] || !$GLOBALS['TSFE']->fe_user) { - return null; - } - // If geolocation was already stored in session, return it if ($sessionUtility->get('geoLocation')) { /** @var Position $position */ $position = $sessionUtility->get('geoLocation'); $position->setFromSession(true); - return $position; } @@ -244,11 +240,9 @@ public function getGeoLocation(): ?Position $position = $geolocationService->getGeolocation(); if ($position instanceof Position) { $sessionUtility->set('geoLocation', $position); - return $position; } } - return null; } diff --git a/Classes/Utility/SessionUtility.php b/Classes/Utility/SessionUtility.php index 2af3725..20fd867 100644 --- a/Classes/Utility/SessionUtility.php +++ b/Classes/Utility/SessionUtility.php @@ -52,9 +52,7 @@ class SessionUtility implements SingletonInterface const KEY = 'tw_geo'; /** - * The frontend user session for key 'tw_geo' - * - * @var array|null + * @var array */ protected $session = null; @@ -63,8 +61,9 @@ class SessionUtility implements SingletonInterface */ public function __construct() { - if (!empty($GLOBALS['TSFE']->fe_user)) { - $this->session = $GLOBALS['TSFE']->fe_user->getKey('ses', self::KEY) ?: []; + session_start(); + if (!array_key_exists(self::KEY, $_SESSION)) { + $_SESSION[self::KEY] = []; } } @@ -78,12 +77,11 @@ public function __construct() */ public function set(string $key, $value): bool { - if (!is_array($this->session)) { + if(!$_SESSION || !is_array($_SESSION[self::KEY])) { return false; } - $this->session[$key] = $value; - $GLOBALS['TSFE']->fe_user->setKey('ses', self::KEY, $this->session); + $_SESSION[self::KEY][$key] = serialize($value); return true; } @@ -96,6 +94,10 @@ public function set(string $key, $value): bool */ public function get(string $key) { - return ($this->session && isset($this->session[$key])) ? $this->session[$key] : null; + if (!is_array($_SESSION) || !isset($_SESSION[self::KEY][$key])) { + return null; + } + + return unserialize($_SESSION[self::KEY][$key]); } }