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