Skip to content

Commit

Permalink
[Bugfix] Fix TYPO3 user session issues and missing excludeIp
Browse files Browse the repository at this point in the history
* Store/cache the detected geolocation in native PHP $_SESSION instead of TYPO3 user session
* Add missing statement so excluded IPs are respected again
  • Loading branch information
kftw committed Aug 21, 2020
1 parent 67b1e69 commit e1ca6ed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
12 changes: 3 additions & 9 deletions Classes/Utility/GeoUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,25 +216,21 @@ 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) {
// Store posision in session
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;
}

Expand All @@ -244,11 +240,9 @@ public function getGeoLocation(): ?Position
$position = $geolocationService->getGeolocation();
if ($position instanceof Position) {
$sessionUtility->set('geoLocation', $position);

return $position;
}
}

return null;
}

Expand Down
20 changes: 11 additions & 9 deletions Classes/Utility/SessionUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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] = [];
}
}

Expand All @@ -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;
}

Expand All @@ -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]);
}
}

0 comments on commit e1ca6ed

Please sign in to comment.