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