Skip to content

Commit

Permalink
Dev: implemented a simple Session stack to prevent navigation loops
Browse files Browse the repository at this point in the history
  • Loading branch information
markusfluer committed Jul 22, 2016
1 parent e63ebb7 commit ecde211
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions application/core/LSHttpRequest.php
Expand Up @@ -82,19 +82,23 @@ public function getUrlReferrer($sAlternativeUrl=null, $aForbiddenWordsInUrl=arra
{

$referrer = parent::getUrlReferrer();
$this->updateNavigationStack($referrer);
$baseReferrer = str_replace(Yii::app()->getBaseUrl(true), "", $referrer);
$baseRequestUri = str_replace(Yii::app()->getBaseUrl(), "", Yii::app()->request->requestUri);
$referrer = ($baseReferrer != $baseRequestUri)?$referrer:null;

App()->session['foo'] = 'bar';

// Checks if the alternative url should be used
if(isset($sAlternativeUrl))
{
// Use alternative url if the referrer is equal to current url.
if(is_null($referrer))
//Use alternative url if the $referrer is still available in the checkLoopInNavigationStack
if( ($this->checkLoopInNavigationStack($referrer)) || (is_null($referrer)) )
{
$referrer = $sAlternativeUrl;
}


// Use alternative url if a forbidden word appears in the referrer
foreach($aForbiddenWordsInUrl as $sForbiddenWord)
{
Expand All @@ -103,10 +107,32 @@ public function getUrlReferrer($sAlternativeUrl=null, $aForbiddenWordsInUrl=arra
$referrer = $sAlternativeUrl;
}
}

}
return $referrer;
}

/**
* Method to update the LimeSurvey Navigation Stack to prevent looping
*/
protected function updateNavigationStack($referrerURL){
$navStack = App()->session['LSNAVSTACK'];
array_unshift($navStack,$referrerURL);
if(count($navstack)>5){
array_pop($navstack);
}
App()->session['LSNAVSTACK'] = $navStack;
}

/**
* Method to check if an url is part of the stack
* Returns true, when an url is saved in the stack
*/
protected function checkLoopInNavigationStack($referrerURL){
$navStack = App()->session['LSNAVSTACK'];
return (array_search($referrerURL, $navStack) === false);
}

protected function normalizeRequest(){
parent::normalizeRequest();

Expand Down

0 comments on commit ecde211

Please sign in to comment.