Skip to content

Commit

Permalink
[TASK] Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
aertmann committed Mar 9, 2016
1 parent 05a7a62 commit c5adec8
Showing 1 changed file with 6 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,6 @@ protected function findValueToMatch($requestPath)
return $requestPath;
}

/**
* @param string $haystack
* @param string $needle
* @return bool
*/
protected function endsWith($haystack, $needle) {
return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== FALSE);
}

/**
* @param string $haystack
* @param string $needle
* @return bool
*/
protected function startsWith($haystack, $needle) {
return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
}

/**
* @param string $requestPath
* @return boolean TRUE if the $requestPath could be matched, otherwise FALSE
Expand All @@ -62,9 +44,6 @@ protected function matchValue($requestPath)
/** @var Uri $uri */
$uri = $this->bootstrap->getActiveRequestHandler()->getHttpRequest()->getUri();
$relativeUrl = rtrim($uri->getPath(), '/');
if($this->startsWith($relativeUrl, '/')){
$tempRelativeUrl = substr($relativeUrl, 1);
}
$relativeUrlWithQueryString = $relativeUrl . ($uri->getQuery() ? '?' . $uri->getQuery() : '');
$absoluteUrl = $uri->getHost() . $relativeUrl;
$absoluteUrlWithQueryString = $uri->getHost() . $relativeUrlWithQueryString;
Expand All @@ -81,7 +60,8 @@ protected function matchValue($requestPath)
->where('n.workspace = :workspace')
->setParameter('workspace', 'live')
->andWhere('n.properties LIKE :relativeUrl')
->setParameter('relativeUrl', '%"redirectUrl"%' . str_replace('/', '\\\\\\/', $tempRelativeUrl) . '%');
->setParameter('relativeUrl', '%"redirectUrl"%' . str_replace('/', '\\\\\\/', ltrim($relativeUrl, '/')) . '%');

$query = $queryBuilder->getQuery();
$nodes = $query->getResult();
if (empty($nodes)) {
Expand All @@ -91,27 +71,20 @@ protected function matchValue($requestPath)
foreach ($nodes as $node) {
/** @var NodeData $node */
// Prevent partial matches
$redirectUrl = preg_replace('#^https?://#', '', $node->getProperty('redirectUrl'));
if($this->endsWith($redirectUrl, '/') === TRUE){
$redirectUrl = substr($redirectUrl, 0, -1);
}
if($this->startsWith($relativeUrl, '/') === TRUE){
$relativeUrl = substr($relativeUrl, 1);
}

if (in_array($redirectUrl,
array($relativeUrl, $relativeUrlWithQueryString, $absoluteUrl, $absoluteUrlWithQueryString), true)) {

$redirectUrl = trim(preg_replace('#^https?://#', '', $node->getProperty('redirectUrl')), '/');
if (in_array($redirectUrl, array($relativeUrl, $relativeUrlWithQueryString, $absoluteUrl, $absoluteUrlWithQueryString), true)) {
$matchingNode = $node;
break;
}
}

if (!isset($matchingNode)) {
return false;
}

$this->setName('node');
$this->value = $matchingNode->getPath();

return true;
}

Expand Down

0 comments on commit c5adec8

Please sign in to comment.