Skip to content

Commit

Permalink
Geolocation Bugfixes and improvements
Browse files Browse the repository at this point in the history
* Fixed bug always returning debug geolocation because of empty debug ip value
* Fixed bug always return null geolocation because of empty excludeIp value
* Add full support for debug and exclude ip addresses for TYPO3 8
  • Loading branch information
kftw committed Jan 18, 2021
1 parent 92f3626 commit 07231d0
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 28 deletions.
19 changes: 13 additions & 6 deletions Classes/Controller/DebugController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,32 @@ class DebugController extends ActionController
/**
* @param GeoUtility $geoUtility
*/
public function injectGeoUtility(GeoUtility $geoUtility){
public function injectGeoUtility(GeoUtility $geoUtility)
{
$this->geoUtility = $geoUtility;
}

/**
* Test geolocation services
*
* @param string $queryString
* @param string $ip
*/
public function geolocationAction(){
$this->view->assign('geolocation', $this->geoUtility->getGeoLocation());
public function geolocationAction(string $ip = null)
{
$this->view->assignMultiple([
'ip' => $ip,
'geolocation' => $this->geoUtility->getGeoLocation(trim($ip))
]);
}

/**
* Test geocoding services
*
* @param string $queryString
*/
public function geocodingAction(String $queryString = null){
if($queryString){
public function geocodingAction(string $queryString = null)
{
if ($queryString) {
$this->view->assign('geocode', $this->geoUtility->geocode($queryString));
}
}
Expand Down
85 changes: 64 additions & 21 deletions Classes/Utility/GeoUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected function getServices(string $type, string $subtype = '')
$serviceChain = '';
/** @var AbstractService $serviceObject */
while (is_object($serviceObject = GeneralUtility::makeInstanceService($type, $subtype, $serviceChain))) {
$serviceChain .= ', '.$serviceObject->getServiceKey();
$serviceChain .= ', ' . $serviceObject->getServiceKey();
$serviceObject->init();
yield $serviceObject;
}
Expand All @@ -125,12 +125,23 @@ public function __construct()
$this->excludeIps = GeneralUtility::trimExplode(',', $backendConfiguration['debug']['excludeIp']);

// Check if geolocation should be disabled by excludeIPs
if($backendConfiguration['debug']['excludeIp'] === '*' || (strlen($_SERVER['REMOTE_ADDR']) && in_array($_SERVER['REMOTE_ADDR'], $this->excludeIps))) {
if ($backendConfiguration['debug']['excludeIp'] === '*'
|| (
strlen($_SERVER['REMOTE_ADDR'])
&& in_array($_SERVER['REMOTE_ADDR'], $this->excludeIps)
)
) {
$this->excluded = true;
}

// Check if geolocation should return the debug position because of IP
if ($backendConfiguration['debug']['ip'] === '*' || in_array($_SERVER['REMOTE_ADDR'], $this->debugIps)) {
if ($backendConfiguration['debug']['ip'] === '*'
|| (
strlen($backendConfiguration['debug']['ip'])
&& in_array($_SERVER['REMOTE_ADDR'],
$this->debugIps)
)
) {
// @extensionScannerIgnoreLine
$this->debug = true;
$this->debugPosition = new Position();
Expand All @@ -145,10 +156,30 @@ public function __construct()
}
} else {
// For TYPO3 v8
$backendConfiguration = GeneralUtility::makeInstance(ObjectManager::class)->get(ConfigurationUtility::class)
$backendConfiguration = GeneralUtility::makeInstance(ObjectManager::class)
->get(ConfigurationUtility::class)
->getCurrentConfiguration('tw_geo');
$this->debugIps = GeneralUtility::trimExplode(',', $backendConfiguration['debug.ip']['value']);
if (in_array($_SERVER['REMOTE_ADDR'], $this->debugIps)) {
$this->excludeIps = GeneralUtility::trimExplode(',', $backendConfiguration['debug.excludeIp']['value']);

// Check if geolocation should be disabled by excludeIPs
if ($backendConfiguration['debug.excludeIp']['value'] === '*'
|| (
strlen($_SERVER['REMOTE_ADDR'])
&& in_array($_SERVER['REMOTE_ADDR'], $this->excludeIps)
)
) {
$this->excluded = true;
}

// Check if geolocation should return the debug position because of IP
if ($backendConfiguration['debug.ip']['value'] === '*'
|| (
strlen($backendConfiguration['debug.ip']['value'])
&& in_array($_SERVER['REMOTE_ADDR'],
$this->debugIps)
)
) {
// @extensionScannerIgnoreLine
$this->debug = true;
$this->debugPosition = new Position();
Expand Down Expand Up @@ -210,29 +241,39 @@ public function getDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitu
* Determines and returns the current position
*
* @param string|null $ip
*
* @return null|Position
*/
public function getGeoLocation(string $ip = null): ?Position
{
$sessionUtility = GeneralUtility::makeInstance(SessionUtility::class);

// If excluded ip, return null
if($this->excluded) {
return null;
}
/*
* If an ip address is given, we always want to get the real geolocation for it.
* So checking for excluded IPs, debug or cache must only be performend when
* there is no given ip address.
*/
if ($ip === null) {
// 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;
}
// If debug position, return it
if ($this->debugPosition) {
// Store posision in session
return $this->debugPosition;
}

// If geolocation was already stored in session, return it
if ($sessionUtility->get('geoLocation')) {
die("cached geoLocation!");
/** @var Position $position */
$position = $sessionUtility->get('geoLocation');
$position->setFromSession(true);

// 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;
return $position;
}
}

// Try to get the real position
Expand All @@ -241,17 +282,19 @@ public function getGeoLocation(string $ip = null): ?Position
$position = $geolocationService->getGeolocation($ip);
if ($position instanceof Position) {
$sessionUtility->set('geoLocation', $position);

return $position;
}
}

return null;
}

/**
* Try to geocode an query string
*
* @param string $queryString
* @param int $limit If 0, return all
* @param int $limit If 0, return all
*
* @return null|Position|PositionList
*/
Expand Down
6 changes: 5 additions & 1 deletion Resources/Private/Templates/Debug/Geolocation.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
--></f:comment>
<html xmlns:f="https://xsd.helhum.io/ns/typo3/cms-fluid/master/ViewHelpers">
<strong>Geolocation</strong><br />
<strong>Geolocation</strong><br/>
<f:debug inline="1">{geolocation}</f:debug>
<f:form action="geolocation" controller="Debug">
<f:form.textfield name="ip" value="{ip}"/>
<f:form.submit value="Submit"/>
</f:form>
</html>

0 comments on commit 07231d0

Please sign in to comment.