44namespace GeorgRinger \Uri2Link \Service ;
55
66use TYPO3 \CMS \Core \Context \Context ;
7+ use TYPO3 \CMS \Core \Domain \Repository \PageRepository ;
78use TYPO3 \CMS \Core \Error \Http \ServiceUnavailableException ;
8- use TYPO3 \CMS \Core \Http \ImmediateResponseException ;
99use TYPO3 \CMS \Core \Http \ServerRequest ;
10+ use TYPO3 \CMS \Core \Http \ServerRequestFactory ;
1011use TYPO3 \CMS \Core \LinkHandling \PageLinkHandler ;
1112use TYPO3 \CMS \Core \Routing \PageArguments ;
12- use TYPO3 \CMS \Core \Routing \RouteNotFoundException ;
1313use TYPO3 \CMS \Core \Routing \SiteMatcher ;
1414use TYPO3 \CMS \Core \Routing \SiteRouteResult ;
1515use TYPO3 \CMS \Core \SingletonInterface ;
1616use TYPO3 \CMS \Core \Site \Entity \Site ;
17- use TYPO3 \CMS \Core \Site \Entity \SiteInterface ;
1817use TYPO3 \CMS \Core \Utility \GeneralUtility ;
1918use TYPO3 \CMS \Frontend \Authentication \FrontendUserAuthentication ;
2019use TYPO3 \CMS \Frontend \Controller \TypoScriptFrontendController ;
2120use TYPO3 \CMS \Frontend \Service \TypoLinkCodecService ;
2221use TYPO3 \CMS \Frontend \Typolink \PageLinkBuilder ;
23- use TYPO3 \CMS \Frontend \Typolink \UnableToLinkException ;
2422
2523class UrlParser implements SingletonInterface
2624{
27- /** @var PageLinkHandler */
28- protected $ pageLinkHandler ;
29-
30- /** @var TypoLinkCodecService */
31- protected $ typoLinkCodecService ;
25+ protected PageLinkHandler $ pageLinkHandler ;
26+ protected TypoLinkCodecService $ typoLinkCodecService ;
3227
3328 public function __construct ()
3429 {
3530 $ this ->pageLinkHandler = GeneralUtility::makeInstance (PageLinkHandler::class);
3631 $ this ->typoLinkCodecService = GeneralUtility::makeInstance (TypoLinkCodecService::class);
3732 }
3833
39- /**
40- * @param string $uri
41- * @return string
42- * @throws UnableToLinkException
43- * @throws RouteNotFoundException
44- */
4534 public function parse (string $ uri ): string
4635 {
4736 $ uriParts = $ this ->typoLinkCodecService ->decode ($ uri );
@@ -59,11 +48,8 @@ public function parse(string $uri): string
5948 $ parameters = $ this ->buildLinkParameters ($ routeResult , $ pageArguments );
6049
6150 if ($ this ->validateUrl ($ uri , $ parameters , $ site )) {
62- $ uriParts ['url ' ] = $ this ->pageLinkHandler ->asString ($ parameters );;
51+ $ uriParts ['url ' ] = $ this ->pageLinkHandler ->asString ($ parameters );
6352 return $ this ->typoLinkCodecService ->encode ($ uriParts );
64- } else {
65- // print_r($parameters);
66- // die;
6753 }
6854
6955 return $ uri ;
@@ -90,26 +76,16 @@ protected function buildLinkParameters(SiteRouteResult $routeResult, PageArgumen
9076 return $ parameters ;
9177 }
9278
93- /**
94- * @param string $uri
95- * @param array $parameters
96- * @param Site $site
97- * @return bool
98- * @throws UnableToLinkException
99- */
10079 protected function validateUrl (string $ uri , array $ parameters , Site $ site ): bool
10180 {
102-
10381 $ queryParams = [];
10482
10583 $ controller = $ this ->bootFrontendController ($ site , $ queryParams );
10684 $ pageLinkBuilder = GeneralUtility::makeInstance (PageLinkBuilder::class, $ controller ->cObj , $ controller );
10785 $ newUrlResult = $ pageLinkBuilder ->build ($ parameters , 'fake ' , '' , []);
108- if ($ newUrlResult [0 ] === $ uri ) {
109- return true ;
110- }
86+ $ newUrlResultAbsolute = $ pageLinkBuilder ->build ($ parameters , 'fake ' , '' , ['forceAbsoluteUrl ' => true ]);
11187
112- return false ;
88+ return $ newUrlResult -> getUrl () === $ uri || $ newUrlResultAbsolute -> getUrl () === $ uri ;
11389 }
11490
11591 /**
@@ -129,24 +105,28 @@ protected function validateUrl(string $uri, array $parameters, Site $site): bool
129105 * @param array $queryParams
130106 * @return TypoScriptFrontendController
131107 * @throws ServiceUnavailableException
132- * @throws ImmediateResponseException
133108 */
134109 protected function bootFrontendController (Site $ site , array $ queryParams ): TypoScriptFrontendController
135110 {
136- $ pageId = $ site ? $ site -> getRootPageId () : 0 ;
111+ $ originalRequest = $ GLOBALS [ ' TYPO3_REQUEST ' ] ?? ServerRequestFactory:: fromGlobals () ;
137112 $ controller = GeneralUtility::makeInstance (
138113 TypoScriptFrontendController::class,
139114 GeneralUtility::makeInstance (Context::class),
140115 $ site ,
141116 $ site ->getDefaultLanguage (),
142- new PageArguments ((int )$ pageId , '0 ' , [])
117+ new PageArguments ($ site ->getRootPageId (), '0 ' , []),
118+ GeneralUtility::makeInstance (FrontendUserAuthentication::class)
143119 );
144- $ controller ->fe_user = GeneralUtility::makeInstance (FrontendUserAuthentication::class);;
145- $ controller ->fetch_the_id ();
120+ $ controller ->determineId ($ originalRequest );
146121 $ controller ->calculateLinkVars ($ queryParams );
147122 $ controller ->getConfigArray ();
148- $ controller ->settingLanguage ();
149- $ controller ->newCObj ();
123+ $ controller ->newCObj ($ originalRequest );
124+ if (!isset ($ GLOBALS ['TSFE ' ]) || !$ GLOBALS ['TSFE ' ] instanceof TypoScriptFrontendController) {
125+ $ GLOBALS ['TSFE ' ] = $ controller ;
126+ }
127+ if (!$ GLOBALS ['TSFE ' ]->sys_page instanceof PageRepository) {
128+ $ GLOBALS ['TSFE ' ]->sys_page = GeneralUtility::makeInstance (PageRepository::class);
129+ }
150130 return $ controller ;
151131 }
152132}
0 commit comments