eb5c21aec2d48b521190a42a206b838c5dc7767a
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Utility / EidUtility.php
1 <?php
2 namespace TYPO3\CMS\Frontend\Utility;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Core\Bootstrap;
18 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
21 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
22 use TYPO3\CMS\Lang\LanguageService;
23
24 /**
25 * Tools for scripts using the eID feature of index.php
26 * Included from index_ts.php
27 * Since scripts using the eID feature does not
28 * have a full FE environment initialized by default
29 * this class seeks to provide functions that can
30 * initialize parts of the FE environment as needed,
31 * eg. Frontend User session, Database connection etc.
32 */
33 class EidUtility {
34
35 /**
36 * Returns true if within an eID-request. False if not.
37 *
38 * @return bool
39 */
40 static public function isEidRequest() {
41 return (bool)GeneralUtility::_GP('eID');
42 }
43
44 /**
45 * Returns the script path associated with the requested eID identifier.
46 *
47 * @return string eID associated script path
48 * @throws \TYPO3\CMS\Core\Exception
49 */
50 static public function getEidScriptPath() {
51 $eID = GeneralUtility::_GP('eID');
52 if (!$eID || !isset($GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include'][$eID])) {
53 throw new \TYPO3\CMS\Core\Exception('eID not registered in $GLOBALS[\'TYPO3_CONF_VARS\'][\'FE\'][\'eID_include\'].', 1415714161);
54 }
55 $scriptPath = GeneralUtility::getFileAbsFileName($GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include'][$eID]);
56 if ($scriptPath === '') {
57 throw new \TYPO3\CMS\Core\Exception('Registered eID has invalid script path.', 1416391467);
58 }
59 return $scriptPath;
60 }
61
62 /**
63 * Load and initialize Frontend User. Note, this process is slow because
64 * it creates a calls many objects. Call this method only if necessary!
65 *
66 * @return FrontendUserAuthentication Frontend User object (usually known as TSFE->fe_user)
67 */
68 static public function initFeUser() {
69 // Get TSFE instance. It knows how to initialize the user. We also
70 // need TCA because services may need extra tables!
71 self::initTCA();
72 /** @var $tsfe \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController */
73 $tsfe = self::getTSFE();
74 $tsfe->initFEuser();
75 // Return FE user object:
76 return $tsfe->fe_user;
77 }
78
79 /**
80 * Initializes $GLOBALS['LANG'] for use in eID scripts.
81 *
82 * @param string $language TYPO3 language code
83 * @return void
84 */
85 static public function initLanguage($language = 'default') {
86 if (!is_object($GLOBALS['LANG'])) {
87 $GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageService::class);
88 $GLOBALS['LANG']->init($language);
89 }
90 }
91
92 /**
93 * Makes TCA available inside eID
94 *
95 * @return void
96 */
97 static public function initTCA() {
98 // Some badly made extensions attempt to manipulate TCA in a wrong way
99 // (inside ext_localconf.php). Therefore $GLOBALS['TCA'] may become an array
100 // but in fact it is not loaded. The check below ensure that
101 // TCA is still loaded if such bad extensions are installed
102 if (!is_array($GLOBALS['TCA']) || !isset($GLOBALS['TCA']['pages'])) {
103 Bootstrap::getInstance()->loadCachedTca();
104 }
105 }
106
107 /**
108 * Makes TCA for the extension available inside eID. Use this function if
109 * you need not to include the whole $GLOBALS['TCA'].
110 *
111 * @param string $extensionKey Extension key
112 * @return void
113 */
114 static public function initExtensionTCA($extensionKey) {
115 $extTablesPath = ExtensionManagementUtility::extPath($extensionKey, 'ext_tables.php');
116 if (file_exists($extTablesPath)) {
117 $GLOBALS['_EXTKEY'] = $extensionKey;
118 require_once $extTablesPath;
119 // We do not need to save restore the value of $GLOBALS['_EXTKEY']
120 // because it is not defined to anything real outside of
121 // ext_tables.php or ext_localconf.php scope.
122 unset($GLOBALS['_EXTKEY']);
123 }
124 }
125
126 /**
127 * Creating a single static cached instance of TSFE to use with this class.
128 *
129 * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController New instance of \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
130 */
131 static private function getTSFE() {
132 // Cached instance
133 static $tsfe = NULL;
134 if (is_null($tsfe)) {
135 $tsfe = GeneralUtility::makeInstance(TypoScriptFrontendController::class, $GLOBALS['TYPO3_CONF_VARS'], 0, 0);
136 }
137 return $tsfe;
138 }
139
140 }