1
|
|
|
<?php |
2
|
|
|
namespace MOC\Redirects\Routing; |
3
|
|
|
|
4
|
|
|
use Doctrine\ORM\QueryBuilder; |
5
|
|
|
use TYPO3\Flow\Annotations as Flow; |
6
|
|
|
use TYPO3\Flow\Core\Bootstrap; |
7
|
|
|
use TYPO3\Flow\Http\Uri; |
8
|
|
|
use TYPO3\Flow\Mvc\Routing\DynamicRoutePart; |
9
|
|
|
use TYPO3\TYPO3CR\Domain\Model\NodeData; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* A route part handler for finding nodes specifically in the website's frontend. |
13
|
|
|
*/ |
14
|
|
|
class RedirectFrontendNodeRoutePartHandler extends DynamicRoutePart |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @Flow\Inject |
19
|
|
|
* @var \Doctrine\Common\Persistence\ObjectManager |
20
|
|
|
*/ |
21
|
|
|
protected $entityManager; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @Flow\Inject |
25
|
|
|
* @var Bootstrap |
26
|
|
|
*/ |
27
|
|
|
protected $bootstrap; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $requestPath The request path to be matched |
31
|
|
|
* @return string value to match |
32
|
|
|
*/ |
33
|
|
|
protected function findValueToMatch($requestPath) |
34
|
|
|
{ |
35
|
|
|
return $requestPath; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $haystack |
40
|
|
|
* @param string $needle |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
|
|
protected function endsWith($haystack, $needle) { |
44
|
|
|
return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== FALSE); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $haystack |
49
|
|
|
* @param string $needle |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
protected function startsWith($haystack, $needle) { |
53
|
|
|
return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $requestPath |
58
|
|
|
* @return boolean TRUE if the $requestPath could be matched, otherwise FALSE |
59
|
|
|
*/ |
60
|
|
|
protected function matchValue($requestPath) |
61
|
|
|
{ |
62
|
|
|
/** @var Uri $uri */ |
63
|
|
|
$uri = $this->bootstrap->getActiveRequestHandler()->getHttpRequest()->getUri(); |
|
|
|
|
64
|
|
|
$relativeUrl = rtrim($uri->getPath(), '/'); |
65
|
|
|
if($this->startsWith($relativeUrl, '/')){ |
66
|
|
|
$tempRelativeUrl = substr($relativeUrl, 1); |
67
|
|
|
} |
68
|
|
|
$relativeUrlWithQueryString = $relativeUrl . ($uri->getQuery() ? '?' . $uri->getQuery() : ''); |
69
|
|
|
$absoluteUrl = $uri->getHost() . $relativeUrl; |
70
|
|
|
$absoluteUrlWithQueryString = $uri->getHost() . $relativeUrlWithQueryString; |
71
|
|
|
|
72
|
|
|
if (empty($relativeUrl)) { |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** @var QueryBuilder $queryBuilder */ |
77
|
|
|
$queryBuilder = $this->entityManager->createQueryBuilder(); |
|
|
|
|
78
|
|
|
$queryBuilder->select('n') |
79
|
|
|
->distinct() |
80
|
|
|
->from('TYPO3\TYPO3CR\Domain\Model\NodeData', 'n') |
81
|
|
|
->where('n.workspace = :workspace') |
82
|
|
|
->setParameter('workspace', 'live') |
83
|
|
|
->andWhere('n.properties LIKE :relativeUrl') |
84
|
|
|
->setParameter('relativeUrl', '%"redirectUrl"%' . str_replace('/', '\\\\\\/', $tempRelativeUrl) . '%'); |
|
|
|
|
85
|
|
|
$query = $queryBuilder->getQuery(); |
86
|
|
|
$nodes = $query->getResult(); |
87
|
|
|
if (empty($nodes)) { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
foreach ($nodes as $node) { |
92
|
|
|
/** @var NodeData $node */ |
93
|
|
|
// Prevent partial matches |
94
|
|
|
$redirectUrl = preg_replace('#^https?://#', '', $node->getProperty('redirectUrl')); |
95
|
|
|
if($this->endsWith($redirectUrl, '/') === TRUE){ |
96
|
|
|
$redirectUrl = substr($redirectUrl, 0, -1); |
97
|
|
|
} |
98
|
|
|
if($this->startsWith($relativeUrl, '/') === TRUE){ |
99
|
|
|
$relativeUrl = substr($relativeUrl, 1); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (in_array($redirectUrl, |
103
|
|
|
array($relativeUrl, $relativeUrlWithQueryString, $absoluteUrl, $absoluteUrlWithQueryString), true)) { |
104
|
|
|
|
105
|
|
|
$matchingNode = $node; |
106
|
|
|
break; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
if (!isset($matchingNode)) { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->setName('node'); |
114
|
|
|
$this->value = $matchingNode->getPath(); |
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $value value to resolve |
120
|
|
|
* @return boolean TRUE if value could be resolved successfully, otherwise FALSE. |
121
|
|
|
*/ |
122
|
|
|
protected function resolveValue($value) |
123
|
|
|
{ |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: