1ade5eb52bb170cbe500bbce2867d65b36b86a94
[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 /**
40 * Tools for scripts using the eID feature of index.php
41 *
42 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
43 * @author Dmitry Dulepov <dmitry@typo3.org>
44 * @package TYPO3
45 * @subpackage tslib
46 */
47 final class tslib_eidtools {
48
49 /**
50 * Load and initialize Frontend User. Note, this process is slow because
51 * it creates a calls many objects. Call this method only if necessary!
52 *
53 * @return object Frontend User object (usually known as TSFE->fe_user)
54 */
55 public static function initFeUser() {
56 // Initialize the database. Do not use TSFE method as it may redirect to
57 // Install tool and call hooks, which do not expect to be called from eID
58 self::connectDB();
59
60 // Get TSFE instance. It knows how to initialize the user. We also
61 // need TCA because services may need extra tables!
62 self::initTCA();
63 $tsfe = self::getTSFE();
64 /* @var $tsfe tslib_fe */
65
66 $tsfe->initFEuser();
67
68 // Return FE user object:
69 return $tsfe->fe_user;
70 }
71
72 /**
73 * Connecting to database. If the function fails, last error message
74 * can be retrieved using $GLOBALS['TYPO3_DB']->sql_error().
75 *
76 * @return boolean TRUE if connection was successful
77 */
78 public static function connectDB() {
79 if (!$GLOBALS['TYPO3_DB']->isConnected()) {
80 // Attempt to connect to the database
81 $GLOBALS['TYPO3_DB']->connectDB();
82 }
83 // connectDB() throws exceptions if something went wrong,
84 // so we are sure that connect was successful here.
85 return TRUE;
86 }
87
88 /**
89 * Initializes $GLOBALS['LANG'] for use in eID scripts.
90 *
91 * @param string $language TYPO3 language code
92 * @return void
93 */
94 public static function initLanguage($language = 'default') {
95 if (!is_object($GLOBALS['LANG'])) {
96 $GLOBALS['LANG'] = t3lib_div::makeInstance('language');
97 $GLOBALS['LANG']->init($language);
98 }
99 }
100
101 /**
102 * Makes TCA available inside eID
103 *
104 * @return void
105 */
106 public static function initTCA() {
107 // Some badly made extensions attempt to manipulate TCA in a wrong way
108 // (inside ext_localconf.php). Therefore $GLOBALS['TCA'] may become an array
109 // but in fact it is not loaded. The check below ensure that
110 // TCA is still loaded if such bad extensions are installed
111 if (!is_array($GLOBALS['TCA']) || !isset($GLOBALS['TCA']['pages'])) {
112 // Load TCA using TSFE
113 self::getTSFE()->includeTCA(FALSE);
114 }
115 }
116
117 /**
118 * Makes TCA for the extension available inside eID. Use this function if
119 * you need not to include the whole $GLOBALS['TCA']. However, you still need to call
120 * t3lib_div::loadTCA() if you want to access column array!
121 *
122 * @param string $extensionKey Extension key
123 * @return void
124 */
125 public static function initExtensionTCA($extensionKey) {
126 $extTablesPath = t3lib_extMgm::extPath($extensionKey, 'ext_tables.php');
127 if (file_exists($extTablesPath)) {
128 $GLOBALS['_EXTKEY'] = $extensionKey;
129 require_once($extTablesPath);
130 unset($GLOBALS['_EXTKEY']);
131 // We do not need to save restore the value of $GLOBALS['_EXTKEY']
132 // because it is not defined to anything real outside of
133 // ext_tables.php or ext_localconf.php scope.
134 }
135 }
136
137 /**
138 * Creating a single static cached instance of TSFE to use with this class.
139 *
140 * @return tslib_fe New instance of tslib_fe
141 */
142 private static function getTSFE() {
143 // Cached instance
144 static $tsfe = NULL;
145
146 if (is_null($tsfe)) {
147 $tsfe = t3lib_div::makeInstance('tslib_fe', $GLOBALS['TYPO3_CONF_VARS'], 0, 0);
148 }
149
150 return $tsfe;
151 }
152 }
153
154 ?>