[TASK] Change "true" into "TRUE"
[Packages/TYPO3.CMS.git] / typo3 / sysext / cms / tslib / class.tslib_eidtools.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2006-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Tools for scripts using the eID feature of index.php
29 * Included from index_ts.php
30 * Since scripts using the eID feature does not
31 * have a full FE environment initialized by default
32 * this class seeks to provide functions that can
33 * initialize parts of the FE environment as needed,
34 * eg. Frontend User session, Database connection etc.
35 *
36 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
37 */
38 /**
39 * [CLASS/FUNCTION INDEX of SCRIPT]
40 *
41 * 71: public static function initFeUser()
42 * 98: public static function connectDB()
43 * 117: public static function initLanguage($language = 'default')
44 * 131: public static function initTCA()
45 * 150: public static function initExtensionTCA($extensionKey)
46 * 167: private static function getTSFE()
47 *
48 * TOTAL FUNCTIONS: 6
49 * (This index is automatically created/updated by the extension "extdeveval")
50 *
51 */
52
53 /**
54 * Tools for scripts using the eID feature of index.php
55 *
56 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
57 * @author Dmitry Dulepov <dmitry@typo3.org>
58 * @package TYPO3
59 * @subpackage tslib
60 */
61 final class tslib_eidtools {
62
63 /**
64 * Load and initialize Frontend User. Note, this process is slow because
65 * it creates a calls many objects. Call this method only if necessary!
66 *
67 * @return object Frontend User object (usually known as TSFE->fe_user)
68 */
69 public static function initFeUser() {
70 // Initialize the database. Do not use TSFE method as it may redirect to
71 // Install tool and call hooks, which do not expect to be called from eID
72 self::connectDB();
73
74 // Get TSFE instance. It knows how to initialize the user. We also
75 // need TCA because services may need extra tables!
76 self::initTCA();
77 $tsfe = self::getTSFE();
78 /* @var $tsfe tslib_fe */
79
80 $tsfe->initFEuser();
81
82 // Return FE user object:
83 return $tsfe->fe_user;
84 }
85
86 /**
87 * Connecting to database. If the function fails, last error message
88 * can be retrieved using $GLOBALS['TYPO3_DB']->sql_error().
89 *
90 * @return boolean TRUE if connection was successful
91 */
92 public static function connectDB() {
93 static $dbConnected = false;
94
95 if (!$dbConnected) {
96 // Attempt to connect to the database
97 if ($GLOBALS['TYPO3_DB']->sql_pconnect(TYPO3_db_host, TYPO3_db_username, TYPO3_db_password) &&
98 $GLOBALS['TYPO3_DB']->sql_select_db(TYPO3_db)) {
99 $dbConnected = TRUE;
100 }
101 }
102 return $dbConnected;
103 }
104
105 /**
106 * Initializes $GLOBALS['LANG'] for use in eID scripts.
107 *
108 * @param string $language TYPO3 language code
109 * @return void
110 */
111 public static function initLanguage($language = 'default') {
112 if (!is_object($GLOBALS['LANG'])) {
113 $GLOBALS['LANG'] = t3lib_div::makeInstance('language');
114 $GLOBALS['LANG']->init($language);
115 }
116 }
117
118 /**
119 * Makes TCA available inside eID
120 *
121 * @return void
122 */
123 public static function initTCA() {
124 // Some badly made extensions attempt to manipulate TCA in a wrong way
125 // (inside ext_localconf.php). Therefore $GLOBALS['TCA'] may become an array
126 // but in fact it is not loaded. The check below ensure that
127 // TCA is still loaded if such bad extensions are installed
128 if (!is_array($GLOBALS['TCA']) || !isset($GLOBALS['TCA']['pages'])) {
129 // Load TCA using TSFE
130 self::getTSFE()->includeTCA(false);
131 }
132 }
133
134 /**
135 * Makes TCA for the extension available inside eID. Use this function if
136 * you need not to include the whole $GLOBALS['TCA']. However, you still need to call
137 * t3lib_div::loadTCA() if you want to access column array!
138 *
139 * @param string $extensionKey Extension key
140 * @return void
141 */
142 public static function initExtensionTCA($extensionKey) {
143 $extTablesPath = t3lib_extMgm::extPath($extensionKey, 'ext_tables.php');
144 if (file_exists($extTablesPath)) {
145 $GLOBALS['_EXTKEY'] = $extensionKey;
146 require_once($extTablesPath);
147 unset($GLOBALS['_EXTKEY']);
148 // We do not need to save restore the value of $GLOBALS['_EXTKEY']
149 // because it is not defined to anything real outside of
150 // ext_tables.php or ext_localconf.php scope.
151 }
152 }
153
154 /**
155 * Creating a single static cached instance of TSFE to use with this class.
156 *
157 * @return tslib_fe New instance of tslib_fe
158 */
159 private static function getTSFE() {
160 // Cached instance
161 static $tsfe = null;
162
163 if (is_null($tsfe)) {
164 $tsfe = t3lib_div::makeInstance('tslib_fe', $GLOBALS['TYPO3_CONF_VARS'], 0, 0);
165 }
166
167 return $tsfe;
168 }
169 }
170
171 ?>