Skip to content

Commit

Permalink
Merge pull request #2 from Sebobo/master
Browse files Browse the repository at this point in the history
[BUGFIX] Homepage redirect to page with empty redirect URL + PSR-2
  • Loading branch information
aertmann committed Oct 1, 2015
2 parents e8a77fa + 113de2f commit c9d1a5c
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 94 deletions.
34 changes: 19 additions & 15 deletions Classes/MOC/Redirects/Controller/RedirectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@
namespace MOC\Redirects\Controller;

use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Mvc\Controller\ActionController;
use TYPO3\Neos\Service\LinkingService;
use TYPO3\TYPO3CR\Domain\Model\Node;

class RedirectController extends \TYPO3\Flow\Mvc\Controller\ActionController {
class RedirectController extends ActionController
{

/**
* @Flow\Inject
* @var LinkingService
*/
protected $linkingService;
/**
* @Flow\Inject
* @var LinkingService
*/
protected $linkingService;

/**
* @param Node $node
* @return void
*
* @Flow\IgnoreValidation("node")
*/
public function redirectAction(Node $node) {
$this->redirectToUri($this->linkingService->createNodeUri($this->controllerContext, $node, NULL, NULL, TRUE, array(), '', TRUE), 0, 301);
}
/**
* @param Node $node
* @return void
*
* @Flow\IgnoreValidation("node")
*/
public function redirectAction(Node $node)
{
$this->redirectToUri($this->linkingService->createNodeUri($this->controllerContext, $node, null, null, true,
array(), '', true), 0, 301);
}

}
167 changes: 88 additions & 79 deletions Classes/MOC/Redirects/Routing/RedirectFrontendNodeRoutePartHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,84 +11,93 @@
/**
* A route part handler for finding nodes specifically in the website's frontend.
*/
class RedirectFrontendNodeRoutePartHandler extends DynamicRoutePart {

/**
* @Flow\Inject
* @var \Doctrine\Common\Persistence\ObjectManager
*/
protected $entityManager;

/**
* @Flow\Inject
* @var Bootstrap
*/
protected $bootstrap;

/**
* @param string $requestPath The request path to be matched
* @return string value to match
*/
protected function findValueToMatch($requestPath) {
return $requestPath;
}

/**
* @param string $requestPath
* @return boolean TRUE if the $requestPath could be matched, otherwise FALSE
*/
protected function matchValue($requestPath) {
/** @var Uri $uri */
$uri = $this->bootstrap->getActiveRequestHandler()->getHttpRequest()->getUri();
$relativeUrl = rtrim($uri->getPath(), '/');
$relativeUrlWithQueryString = $relativeUrl . ($uri->getQuery() ? '?' . $uri->getQuery() : '');
$absoluteUrl = $uri->getHost() . $relativeUrl;
$absoluteUrlWithQueryString = $uri->getHost() . $relativeUrlWithQueryString;

/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->entityManager->createQueryBuilder();

$queryBuilder->select('n')
->distinct()
->from('TYPO3\TYPO3CR\Domain\Model\NodeData', 'n')
->where('n.workspace = :workspace')
->setParameter('workspace', 'live')
->andWhere('n.properties LIKE :relativeUrl')
->setParameter('relativeUrl', '%"redirectUrl"%' . rtrim($uri->getPath(), '/') . '%');

$query = $queryBuilder->getQuery();
$nodes = $query->getResult();

if (empty($nodes)) {
return FALSE;
}

foreach ($nodes as $node) {
/** @var NodeData $node */
// Prevent partial matches
$redirectUrl = 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;
}

/**
* @param string $value value to resolve
* @return boolean TRUE if value could be resolved successfully, otherwise FALSE.
*/
protected function resolveValue($value) {
return FALSE;
}
class RedirectFrontendNodeRoutePartHandler extends DynamicRoutePart
{

/**
* @Flow\Inject
* @var \Doctrine\Common\Persistence\ObjectManager
*/
protected $entityManager;

/**
* @Flow\Inject
* @var Bootstrap
*/
protected $bootstrap;

/**
* @param string $requestPath The request path to be matched
* @return string value to match
*/
protected function findValueToMatch($requestPath)
{
return $requestPath;
}

/**
* @param string $requestPath
* @return boolean TRUE if the $requestPath could be matched, otherwise FALSE
*/
protected function matchValue($requestPath)
{
/** @var Uri $uri */
$uri = $this->bootstrap->getActiveRequestHandler()->getHttpRequest()->getUri();
$relativeUrl = rtrim($uri->getPath(), '/');
$relativeUrlWithQueryString = $relativeUrl . ($uri->getQuery() ? '?' . $uri->getQuery() : '');
$absoluteUrl = $uri->getHost() . $relativeUrl;
$absoluteUrlWithQueryString = $uri->getHost() . $relativeUrlWithQueryString;

if (empty($relativeUrl)) {
return false;
}

/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->entityManager->createQueryBuilder();

$queryBuilder->select('n')
->distinct()
->from('TYPO3\TYPO3CR\Domain\Model\NodeData', 'n')
->where('n.workspace = :workspace')
->setParameter('workspace', 'live')
->andWhere('n.properties LIKE :relativeUrl')
->setParameter('relativeUrl', '%"redirectUrl"%' . $relativeUrl . '%');

$query = $queryBuilder->getQuery();
$nodes = $query->getResult();

if (empty($nodes)) {
return false;
}

foreach ($nodes as $node) {
/** @var NodeData $node */
// Prevent partial matches
$redirectUrl = 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;
}

/**
* @param string $value value to resolve
* @return boolean TRUE if value could be resolved successfully, otherwise FALSE.
*/
protected function resolveValue($value)
{
return false;
}

}

0 comments on commit c9d1a5c

Please sign in to comment.