1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\FrontendEnvironment; |
3
|
|
|
|
4
|
|
|
use TYPO3\CMS\Core\Context\Context; |
5
|
|
|
use TYPO3\CMS\Core\Context\LanguageAspectFactory; |
6
|
|
|
use TYPO3\CMS\Core\Exception\SiteNotFoundException; |
7
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
8
|
|
|
use TYPO3\CMS\Core\Site\SiteFinder; |
9
|
|
|
use TYPO3\CMS\Core\TypoScript\TemplateService; |
10
|
|
|
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; |
11
|
|
|
use TYPO3\CMS\Backend\Utility\BackendUtility; |
12
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
13
|
|
|
use TYPO3\CMS\Frontend\Page\PageRepository; |
14
|
|
|
use TYPO3\CMS\Core\Context\UserAspect; |
15
|
|
|
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication; |
16
|
|
|
use TYPO3\CMS\Core\Http\ServerRequest; |
17
|
|
|
use ApacheSolrForTypo3\Solr\Util; |
18
|
|
|
|
19
|
|
|
class Tsfe implements SingletonInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
private $tsfeCache = []; |
23
|
|
|
|
24
|
|
|
private $requestCache = []; |
25
|
|
|
|
26
|
168 |
|
public function changeLanguageContext(int $pageId, int $language): void |
27
|
|
|
{ |
28
|
168 |
|
$context = GeneralUtility::makeInstance(Context::class); |
29
|
168 |
|
if ($context->hasAspect('language')) { |
30
|
168 |
|
$hasRightLanguageId = $context->getPropertyFromAspect('language', 'id') === $language; |
31
|
168 |
|
$hasRightContentLanguageId = $context->getPropertyFromAspect('language', 'contentId') === $language; |
32
|
168 |
|
if ($hasRightLanguageId && $hasRightContentLanguageId) { |
33
|
168 |
|
return; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
17 |
|
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class); |
38
|
|
|
try { |
39
|
17 |
|
$site = $siteFinder->getSiteByPageId($pageId); |
40
|
17 |
|
$languageAspect = LanguageAspectFactory::createFromSiteLanguage($site->getLanguageById($language)); |
41
|
17 |
|
$context->setAspect('language', $languageAspect); |
42
|
|
|
} catch (SiteNotFoundException $e) { |
|
|
|
|
43
|
|
|
|
44
|
|
|
} |
45
|
17 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Initializes the TSFE for a given page ID and language. |
49
|
|
|
* |
50
|
|
|
* @param $pageId |
51
|
|
|
* @param int $language |
52
|
|
|
* @throws SiteNotFoundException |
53
|
|
|
* @throws \TYPO3\CMS\Core\Error\Http\ServiceUnavailableException |
54
|
|
|
* @throws \TYPO3\CMS\Core\Http\ImmediateResponseException |
55
|
|
|
*/ |
56
|
19 |
|
public function initializeTsfe($pageId, $language = 0) |
57
|
|
|
{ |
58
|
|
|
|
59
|
|
|
// resetting, a TSFE instance with data from a different page Id could be set already |
60
|
19 |
|
unset($GLOBALS['TSFE']); |
61
|
|
|
|
62
|
19 |
|
$cacheId = $pageId . '|' . $language; |
63
|
|
|
|
64
|
|
|
/** @var Context $context */ |
65
|
19 |
|
$context = GeneralUtility::makeInstance(Context::class); |
66
|
19 |
|
$this->changeLanguageContext((int)$pageId, (int)$language); |
67
|
|
|
|
68
|
19 |
|
if (!isset($this->requestCache[$cacheId])) { |
69
|
19 |
|
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class); |
70
|
19 |
|
$site = $siteFinder->getSiteByPageId($pageId); |
71
|
19 |
|
$siteLanguage = $site->getLanguageById($language); |
72
|
|
|
|
73
|
19 |
|
$request = GeneralUtility::makeInstance(ServerRequest::class); |
74
|
19 |
|
$request = $request->withAttribute('site', $site); |
75
|
19 |
|
$this->requestCache[$cacheId] = $request->withAttribute('language', $siteLanguage); |
76
|
|
|
} |
77
|
19 |
|
$GLOBALS['TYPO3_REQUEST'] = $this->requestCache[$cacheId]; |
78
|
|
|
|
79
|
19 |
|
if (!isset($this->tsfeCache[$cacheId])) { |
80
|
|
|
|
81
|
19 |
|
if (Util::getIsTYPO3VersionBelow10()) { |
82
|
|
|
$GLOBALS['TSFE'] = GeneralUtility::makeInstance(TypoScriptFrontendController::class, [], $pageId, 0); |
|
|
|
|
83
|
|
|
} else { |
84
|
19 |
|
$GLOBALS['TSFE'] = GeneralUtility::makeInstance(TypoScriptFrontendController::class, $context, $site, $siteLanguage); |
|
|
|
|
85
|
19 |
|
$GLOBALS['TSFE']->id = $pageId; |
86
|
19 |
|
$GLOBALS['TSFE']->type = 0; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// for certain situations we need to trick TSFE into granting us |
90
|
|
|
// access to the page in any case to make getPageAndRootline() work |
91
|
|
|
// see http://forge.typo3.org/issues/42122 |
92
|
19 |
|
$pageRecord = BackendUtility::getRecord('pages', $pageId, 'fe_group'); |
93
|
|
|
|
94
|
19 |
|
$feUser = GeneralUtility::makeInstance(FrontendUserAuthentication::class); |
95
|
19 |
|
$userGroups = [0, -1]; |
96
|
19 |
|
if (!empty($pageRecord['fe_group'])) { |
97
|
|
|
$userGroups = array_unique(array_merge($userGroups, explode(',', $pageRecord['fe_group']))); |
98
|
|
|
} |
99
|
19 |
|
$context->setAspect('frontend.user', GeneralUtility::makeInstance(UserAspect::class, $feUser, $userGroups)); |
|
|
|
|
100
|
|
|
|
101
|
|
|
// @extensionScannerIgnoreLine |
102
|
19 |
|
$GLOBALS['TSFE']->sys_page = GeneralUtility::makeInstance(PageRepository::class); |
103
|
19 |
|
$GLOBALS['TSFE']->getPageAndRootlineWithDomain($pageId); |
104
|
|
|
|
105
|
19 |
|
$template = GeneralUtility::makeInstance(TemplateService::class, $context); |
106
|
19 |
|
$GLOBALS['TSFE']->tmpl = $template; |
107
|
19 |
|
$GLOBALS['TSFE']->forceTemplateParsing = true; |
108
|
19 |
|
$GLOBALS['TSFE']->no_cache = true; |
109
|
19 |
|
$GLOBALS['TSFE']->tmpl->start($GLOBALS['TSFE']->rootLine); |
110
|
19 |
|
$GLOBALS['TSFE']->no_cache = false; |
111
|
19 |
|
$GLOBALS['TSFE']->getConfigArray(); |
112
|
19 |
|
$GLOBALS['TSFE']->settingLanguage(); |
113
|
|
|
|
114
|
19 |
|
$GLOBALS['TSFE']->newCObj(); |
115
|
19 |
|
$GLOBALS['TSFE']->absRefPrefix = self::getAbsRefPrefixFromTSFE($GLOBALS['TSFE']); |
|
|
|
|
116
|
19 |
|
$GLOBALS['TSFE']->calculateLinkVars([]); |
117
|
|
|
|
118
|
19 |
|
$this->tsfeCache[$cacheId] = $GLOBALS['TSFE']; |
119
|
|
|
} |
120
|
|
|
|
121
|
19 |
|
$GLOBALS['TSFE'] = $this->tsfeCache[$cacheId]; |
122
|
19 |
|
$GLOBALS['TSFE']->settingLocale(); |
123
|
19 |
|
$this->changeLanguageContext((int)$pageId, (int)$language); |
124
|
19 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Resolves the configured absRefPrefix to a valid value and resolved if absRefPrefix |
128
|
|
|
* is set to "auto". |
129
|
|
|
*/ |
130
|
19 |
|
private function getAbsRefPrefixFromTSFE(TypoScriptFrontendController $TSFE): string |
131
|
|
|
{ |
132
|
19 |
|
$absRefPrefix = ''; |
133
|
19 |
|
if (empty($TSFE->config['config']['absRefPrefix'])) { |
134
|
|
|
return $absRefPrefix; |
135
|
|
|
} |
136
|
|
|
|
137
|
19 |
|
$absRefPrefix = trim($TSFE->config['config']['absRefPrefix']); |
138
|
19 |
|
if ($absRefPrefix === 'auto') { |
139
|
19 |
|
$absRefPrefix = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH'); |
140
|
|
|
} |
141
|
|
|
|
142
|
19 |
|
return $absRefPrefix; |
143
|
|
|
} |
144
|
|
|
} |