a93f0dd711c86d6feb060956d8b6c5e4887e2961
[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\Localization\LanguageService;
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
23 /**
24 * Tools for scripts using the eID feature of the TYPO3 Frontend.
25 * Included from \TYPO3\CMS\Frontend\Http\RequestHandler.
26 * Since scripts using the eID feature does not
27 * have a full FE environment initialized by default
28 * this class seeks to provide functions that can
29 * initialize parts of the FE environment as needed,
30 * eg. Frontend User session, Database connection etc.
31 */
32 class EidUtility
33 {
34 /**
35 * Load and initialize Frontend User. Note, this process is slow because
36 * it creates a calls many objects. Call this method only if necessary!
37 *
38 * @return FrontendUserAuthentication Frontend User object (usually known as TSFE->fe_user)
39 */
40 public static function initFeUser()
41 {
42 // Get TSFE instance. It knows how to initialize the user.
43 /** @var $tsfe \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController */
44 $tsfe = self::getTSFE();
45 $tsfe->initFEuser();
46 // Return FE user object:
47 return $tsfe->fe_user;
48 }
49
50 /**
51 * Initializes $GLOBALS['LANG'] for use in eID scripts.
52 *
53 * @param string $language TYPO3 language code
54 */
55 public static function initLanguage($language = 'default')
56 {
57 if (!is_object($GLOBALS['LANG'])) {
58 $GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageService::class);
59 $GLOBALS['LANG']->init($language);
60 }
61 }
62
63 /**
64 * Makes TCA available inside eID
65 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10 - this is not needed anymore as TCA is now always available
66 */
67 public static function initTCA()
68 {
69 GeneralUtility::logDeprecatedFunction();
70 // Some badly made extensions attempt to manipulate TCA in a wrong way
71 // (inside ext_localconf.php). Therefore $GLOBALS['TCA'] may become an array
72 // but in fact it is not loaded. The check below ensure that
73 // TCA is still loaded if such bad extensions are installed
74 if (!is_array($GLOBALS['TCA']) || !isset($GLOBALS['TCA']['pages'])) {
75 ExtensionManagementUtility::loadBaseTca();
76 }
77 }
78
79 /**
80 * Makes TCA for the extension available inside eID. Use this function if
81 * you need not to include the whole $GLOBALS['TCA'].
82 *
83 * @param string $extensionKey Extension key
84 */
85 public static function initExtensionTCA($extensionKey)
86 {
87 $extTablesPath = ExtensionManagementUtility::extPath($extensionKey, 'ext_tables.php');
88 if (file_exists($extTablesPath)) {
89 $GLOBALS['_EXTKEY'] = $extensionKey;
90 require_once $extTablesPath;
91 // We do not need to save restore the value of $GLOBALS['_EXTKEY']
92 // because it is not defined to anything real outside of
93 // ext_tables.php or ext_localconf.php scope.
94 unset($GLOBALS['_EXTKEY']);
95 }
96 }
97
98 /**
99 * Creating a single static cached instance of TSFE to use with this class.
100 *
101 * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController New instance of \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
102 */
103 private static function getTSFE()
104 {
105 // Cached instance
106 static $tsfe = null;
107 if (is_null($tsfe)) {
108 $tsfe = GeneralUtility::makeInstance(TypoScriptFrontendController::class, null, 0, 0);
109 }
110 return $tsfe;
111 }
112 }