[TASK] Frontend: BE User Initialization should be encapsulated
[Packages/TYPO3.CMS.git] / typo3 / sysext / cms / tslib / index_ts.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-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 * This is the MAIN DOCUMENT of the TypoScript driven standard front-end (from the "cms" extension)
29 * Basically put this is the "index.php" script which all requests for TYPO3 delivered pages goes to in the frontend (the website)
30 * The script configures constants, includes libraries and does a little logic here and there in order to instantiate the right classes to create the webpage.
31 * All the real data processing goes on in the "tslib/" classes which this script will include and use as needed.
32 *
33 * Revised for TYPO3 3.6 June/2003 by Kasper Skårhøj
34 *
35 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
36 * @package TYPO3
37 * @subpackage tslib
38 */
39
40 // *******************************
41 // Checking PHP version
42 // *******************************
43 if (version_compare(phpversion(), '5.2', '<')) die ('TYPO3 requires PHP 5.2.0 or higher.');
44
45 // *******************************
46 // Set error reporting
47 // *******************************
48 error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED);
49
50
51 // ******************
52 // Constants defined
53 // ******************
54 $TYPO3_MISC['microtime_start'] = microtime(TRUE);
55 define('TYPO3_OS', stristr(PHP_OS,'win')&&!stristr(PHP_OS,'darwin')?'WIN':'');
56 define('TYPO3_MODE','FE');
57
58 if(!defined('PATH_thisScript')) {
59 define('PATH_thisScript', str_replace('//', '/', str_replace('\\', '/',
60 (PHP_SAPI == 'fpm-fcgi' || PHP_SAPI == 'cgi' || PHP_SAPI == 'isapi' || PHP_SAPI == 'cgi-fcgi') &&
61 ($_SERVER['ORIG_PATH_TRANSLATED'] ? $_SERVER['ORIG_PATH_TRANSLATED'] : $_SERVER['PATH_TRANSLATED']) ?
62 ($_SERVER['ORIG_PATH_TRANSLATED'] ? $_SERVER['ORIG_PATH_TRANSLATED'] : $_SERVER['PATH_TRANSLATED']) :
63 ($_SERVER['ORIG_SCRIPT_FILENAME'] ? $_SERVER['ORIG_SCRIPT_FILENAME'] : $_SERVER['SCRIPT_FILENAME']))));
64 }
65
66 if (!defined('PATH_site')) define('PATH_site', dirname(PATH_thisScript).'/');
67 if (!defined('PATH_t3lib')) define('PATH_t3lib', PATH_site.'t3lib/');
68
69 define('TYPO3_mainDir', 'typo3/'); // This is the directory of the backend administration for the sites of this TYPO3 installation.
70 define('PATH_typo3', PATH_site.TYPO3_mainDir);
71 define('PATH_typo3conf', PATH_site.'typo3conf/');
72
73 if (!defined('PATH_tslib')) {
74 if (@is_dir(PATH_site.TYPO3_mainDir.'sysext/cms/tslib/')) {
75 define('PATH_tslib', PATH_site.TYPO3_mainDir.'sysext/cms/tslib/');
76 } elseif (@is_dir(PATH_site.'tslib/')) {
77 define('PATH_tslib', PATH_site.'tslib/');
78 }
79 }
80
81 if (!@is_dir(PATH_typo3conf)) die('Cannot find configuration. This file is probably executed from the wrong location.');
82
83 // *********************
84 // Unset variable(s) in global scope (fixes #13959)
85 // *********************
86 unset($error);
87
88 // *********************
89 // Prevent any output until AJAX/compression is initialized to stop
90 // AJAX/compression data corruption
91 // *********************
92 ob_start();
93
94 // *********************
95 // Timetracking started
96 // *********************
97 if ($_SESSION['TYPO3-TT-start']) {
98 require_once(PATH_t3lib.'class.t3lib_timetrack.php');
99 $TT = new t3lib_timeTrack;
100 } else {
101 require_once(PATH_t3lib.'class.t3lib_timetracknull.php');
102 $TT = new t3lib_timeTrackNull;
103 }
104
105 $TT->start();
106 $TT->push('','Script start');
107
108
109 // *********************
110 // Mandatory libraries included
111 // *********************
112 $TT->push('Include class t3lib_db, t3lib_div, t3lib_extmgm','');
113 require_once(PATH_t3lib.'class.t3lib_div.php');
114 require_once(PATH_t3lib.'class.t3lib_extmgm.php');
115 $TT->pull();
116
117
118
119 // **********************
120 // Include configuration
121 // **********************
122 $TT->push('Include config files','');
123 require(PATH_t3lib.'config_default.php');
124 if (!defined ('TYPO3_db')) die ('The configuration file was not included.'); // the name of the TYPO3 database is stored in this constant. Here the inclusion of the config-file is verified by checking if this var is set.
125 if (!t3lib_extMgm::isLoaded('cms')) die('<strong>Error:</strong> The main frontend extension "cms" was not loaded. Enable it in the extension manager in the backend.');
126
127 if (!defined('PATH_tslib')) {
128 define('PATH_tslib', t3lib_extMgm::extPath('cms').'tslib/');
129 }
130
131
132 // Restart time tracking if BE login exists
133 if (!$_SESSION['TYPO3-TT-start'] && $_COOKIE[t3lib_beUserAuth::getCookieName()]) {
134 $_SESSION['TYPO3-TT-start'] = TRUE;
135
136 $TT = new t3lib_timeTrack;
137 $TT->start();
138 $TT->push('', 'Script start (late)');
139 }
140
141
142 // *********************
143 // Error & Exception handling
144 // *********************
145 if ($TYPO3_CONF_VARS['SC_OPTIONS']['errors']['exceptionHandler'] !== '') {
146 $TT->push('Register Exceptionhandler', '');
147 if ($TYPO3_CONF_VARS['SYS']['errorHandler'] !== '') {
148 // register an error handler for the given errorHandlerErrors
149 $errorHandler = t3lib_div::makeInstance($TYPO3_CONF_VARS['SYS']['errorHandler'], $TYPO3_CONF_VARS['SYS']['errorHandlerErrors']);
150 // set errors which will be converted in an exception
151 $errorHandler->setExceptionalErrors($TYPO3_CONF_VARS['SC_OPTIONS']['errors']['exceptionalErrors']);
152 }
153 $exceptionHandler = t3lib_div::makeInstance($TYPO3_CONF_VARS['SC_OPTIONS']['errors']['exceptionHandler']);
154 $TT->pull();
155 }
156
157 $TYPO3_DB = t3lib_div::makeInstance('t3lib_DB');
158 $TYPO3_DB->debugOutput = $TYPO3_CONF_VARS['SYS']['sqlDebug'];
159
160 $CLIENT = t3lib_div::clientInfo(); // Set to the browser: net / msie if 4+ browsers
161 $TT->pull();
162
163
164 // *******************************
165 // Checking environment
166 // *******************************
167 if (isset($_POST['GLOBALS']) || isset($_GET['GLOBALS'])) {
168 throw new Exception('You cannot set the GLOBALS-array from outside the script.', 1294585200);
169 }
170 if (!get_magic_quotes_gpc()) {
171 $TT->push('Add slashes to GET/POST arrays','');
172 t3lib_div::addSlashesOnArray($_GET);
173 t3lib_div::addSlashesOnArray($_POST);
174 $HTTP_GET_VARS = $_GET;
175 $HTTP_POST_VARS = $_POST;
176 $TT->pull();
177 }
178
179
180 // Hook to preprocess the current request:
181 if (is_array($TYPO3_CONF_VARS['SC_OPTIONS']['tslib/index_ts.php']['preprocessRequest'])) {
182 foreach ($TYPO3_CONF_VARS['SC_OPTIONS']['tslib/index_ts.php']['preprocessRequest'] as $hookFunction) {
183 $hookParameters = array();
184 t3lib_div::callUserFunction($hookFunction, $hookParameters, $hookParameters);
185 }
186 unset($hookFunction);
187 unset($hookParameters);
188 }
189
190
191 // *********************
192 // Look for extension ID which will launch alternative output engine
193 // *********************
194 if ($temp_extId = t3lib_div::_GP('eID')) {
195 if ($classPath = t3lib_div::getFileAbsFileName($TYPO3_CONF_VARS['FE']['eID_include'][$temp_extId])) {
196 // Remove any output produced until now
197 ob_clean();
198
199 require($classPath);
200 }
201 exit;
202 }
203
204
205 // ***********************************
206 // Create $TSFE object (TSFE = TypoScript Front End)
207 // Connecting to database
208 // ***********************************
209 $TSFE = t3lib_div::makeInstance('tslib_fe',
210 $TYPO3_CONF_VARS,
211 t3lib_div::_GP('id'),
212 t3lib_div::_GP('type'),
213 t3lib_div::_GP('no_cache'),
214 t3lib_div::_GP('cHash'),
215 t3lib_div::_GP('jumpurl'),
216 t3lib_div::_GP('MP'),
217 t3lib_div::_GP('RDCT')
218 );
219 /** @var $TSFE tslib_fe */
220
221 if($TYPO3_CONF_VARS['FE']['pageUnavailable_force'] &&
222 !t3lib_div::cmpIP(t3lib_div::getIndpEnv('REMOTE_ADDR'), $TYPO3_CONF_VARS['SYS']['devIPmask'])) {
223 $TSFE->pageUnavailableAndExit('This page is temporarily unavailable.');
224 }
225
226
227 $TSFE->connectToDB();
228
229 // In case of a keyword-authenticated preview, re-initialize the TSFE object:
230 if ($temp_previewConfig = $TSFE->ADMCMD_preview()) {
231 $TSFE = t3lib_div::makeInstance('tslib_fe',
232 $TYPO3_CONF_VARS,
233 t3lib_div::_GP('id'),
234 t3lib_div::_GP('type'),
235 t3lib_div::_GP('no_cache'),
236 t3lib_div::_GP('cHash'),
237 t3lib_div::_GP('jumpurl'),
238 t3lib_div::_GP('MP'),
239 t3lib_div::_GP('RDCT')
240 );
241 $TSFE->ADMCMD_preview_postInit($temp_previewConfig);
242 }
243
244
245 $TSFE->sendRedirect();
246
247
248 // *******************
249 // Output compression
250 // *******************
251 // Remove any output produced until now
252 ob_clean();
253 if ($TYPO3_CONF_VARS['FE']['compressionLevel'] && extension_loaded('zlib')) {
254 if (t3lib_div::testInt($TYPO3_CONF_VARS['FE']['compressionLevel'])) {
255 // Prevent errors if ini_set() is unavailable (safe mode)
256 @ini_set('zlib.output_compression_level', $TYPO3_CONF_VARS['FE']['compressionLevel']);
257 }
258 ob_start(array(t3lib_div::makeInstance('tslib_fecompression'), 'compressionOutputHandler'));
259 }
260
261 // *********
262 // FE_USER
263 // *********
264 $TT->push('Front End user initialized','');
265 /* @var $TSFE tslib_fe */
266 $TSFE->initFEuser();
267 $TT->pull();
268
269
270 // *********
271 // BE_USER
272 // *********
273 /** @var $BE_USER t3lib_tsfeBeUserAuth */
274 $BE_USER = $TSFE->initializeBackendUser();
275
276
277 // ********************
278 // Workspace preview:
279 // ********************
280 $TSFE->workspacePreviewInit();
281
282
283 // *****************************************
284 // Process the ID, type and other parameters
285 // After this point we have an array, $page in TSFE, which is the page-record of the current page, $id
286 // *****************************************
287 $TT->push('Process ID','');
288 // Initialize admin panel since simulation settings are required here:
289 if ($TSFE->isBackendUserLoggedIn()) {
290 $BE_USER->initializeAdminPanel();
291 }
292
293 $TSFE->checkAlternativeIdMethods();
294 $TSFE->clear_preview();
295 $TSFE->determineId();
296
297 // Now, if there is a backend user logged in and he has NO access to this page, then re-evaluate the id shown!
298 if ($TSFE->isBackendUserLoggedIn() && (!$BE_USER->extPageReadAccess($TSFE->page) || t3lib_div::_GP('ADMCMD_noBeUser'))) { // t3lib_div::_GP('ADMCMD_noBeUser') is placed here because workspacePreviewInit() might need to know if a backend user is logged in!
299
300 // Remove user
301 unset($BE_USER);
302 $TSFE->beUserLogin = 0;
303
304 // Re-evaluate the page-id.
305 $TSFE->checkAlternativeIdMethods();
306 $TSFE->clear_preview();
307 $TSFE->determineId();
308 }
309 $TSFE->makeCacheHash();
310 $TT->pull();
311
312 // *****************************************
313 // Admin Panel & Frontend editing
314 // *****************************************
315 if ($TSFE->isBackendUserLoggedIn()) {
316 // if a BE User is present load, the sprite manager for frontend-editing
317 $spriteManager = t3lib_div::makeInstance('t3lib_SpriteManager', FALSE);
318 $spriteManager->loadCacheFile();
319
320 $BE_USER->initializeFrontendEdit();
321 if ($BE_USER->adminPanel instanceof tslib_AdminPanel) {
322 $LANG = t3lib_div::makeInstance('language');
323 $LANG->init($BE_USER->uc['lang']);
324 }
325 if ($BE_USER->frontendEdit instanceof t3lib_frontendedit) {
326 $BE_USER->frontendEdit->initConfigOptions();
327 }
328 }
329
330 // *******************************************
331 // Get compressed $TCA-Array();
332 // After this, we should now have a valid $TCA, though minimized
333 // *******************************************
334 $TSFE->getCompressedTCarray();
335
336
337 // ********************************
338 // Starts the template
339 // *******************************
340 $TT->push('Start Template','');
341 $TSFE->initTemplate();
342 $TT->pull();
343
344
345 // ********************************
346 // Get from cache
347 // *******************************
348 $TT->push('Get Page from cache','');
349 $TSFE->getFromCache();
350 $TT->pull();
351
352
353 // ******************************************************
354 // Get config if not already gotten
355 // After this, we should have a valid config-array ready
356 // ******************************************************
357 $TSFE->getConfigArray();
358
359 // ********************************
360 // Convert POST data to internal "renderCharset" if different from the metaCharset
361 // *******************************
362 $TSFE->convPOSTCharset();
363
364
365 // *******************************************
366 // Setting language and locale
367 // *******************************************
368 $TT->push('Setting language and locale','');
369 $TSFE->settingLanguage();
370 $TSFE->settingLocale();
371 $TT->pull();
372
373
374 // ********************************
375 // Check JumpUrl
376 // *******************************
377 $TSFE->setExternalJumpUrl();
378 $TSFE->checkJumpUrlReferer();
379
380
381 // ********************************
382 // Check Submission of data.
383 // This is done at this point, because we need the config values
384 // *******************************
385 switch($TSFE->checkDataSubmission()) {
386 case 'email':
387 $TSFE->sendFormmail();
388 break;
389 case 'fe_tce':
390 $TSFE->includeTCA();
391 $TT->push('fe_tce','');
392 $TSFE->fe_tce();
393 $TT->pull();
394 break;
395 }
396
397
398 // ********************************
399 // Generate page
400 // *******************************
401 $TSFE->setUrlIdToken();
402
403 $TT->push('Page generation','');
404 if ($TSFE->isGeneratePage()) {
405 $TSFE->generatePage_preProcessing();
406 $temp_theScript=$TSFE->generatePage_whichScript();
407
408 if ($temp_theScript) {
409 include($temp_theScript);
410 } else {
411 include(PATH_tslib.'pagegen.php');
412 }
413 $TSFE->generatePage_postProcessing();
414 } elseif ($TSFE->isINTincScript()) {
415 include(PATH_tslib.'pagegen.php');
416 }
417 $TT->pull();
418
419
420 // ********************************
421 // $TSFE->config['INTincScript']
422 // *******************************
423 if ($TSFE->isINTincScript()) {
424 $TT->push('Non-cached objects','');
425 $TSFE->INTincScript();
426 $TT->pull();
427 }
428
429 // ***************
430 // Output content
431 // ***************
432 $sendTSFEContent = FALSE;
433 if ($TSFE->isOutputting()) {
434 $TT->push('Print Content','');
435 $TSFE->processOutput();
436
437 // ***************************************
438 // Outputs content / Includes EXT scripts
439 // ***************************************
440 if ($TSFE->isEXTincScript()) {
441 $TT->push('External PHP-script','');
442 // Important global variables here are $EXTiS_*, they must not be overridden in include-scripts!!!
443 $EXTiS_config = $TSFE->config['EXTincScript'];
444 $EXTiS_splitC = explode('<!--EXT_SCRIPT.',$TSFE->content); // Splits content with the key
445
446 // Special feature: Include libraries
447 foreach ($EXTiS_config as $EXTiS_cPart) {
448 if (isset($EXTiS_cPart['conf']['includeLibs']) && $EXTiS_cPart['conf']['includeLibs']) {
449 $EXTiS_resourceList = t3lib_div::trimExplode(',',$EXTiS_cPart['conf']['includeLibs'], TRUE);
450 $TSFE->includeLibraries($EXTiS_resourceList);
451 }
452 }
453
454 foreach ($EXTiS_splitC as $EXTiS_c => $EXTiS_cPart) {
455 if (substr($EXTiS_cPart,32,3)=='-->') { // If the split had a comment-end after 32 characters it's probably a split-string
456 $EXTiS_key = 'EXT_SCRIPT.'.substr($EXTiS_cPart,0,32);
457 if (is_array($EXTiS_config[$EXTiS_key])) {
458 $REC = $EXTiS_config[$EXTiS_key]['data'];
459 $CONF = $EXTiS_config[$EXTiS_key]['conf'];
460 $content = '';
461 include($EXTiS_config[$EXTiS_key]['file']);
462 echo $content; // The script MAY return content in $content or the script may just output the result directly!
463 }
464 echo substr($EXTiS_cPart,35);
465 } else {
466 echo ($c?'<!--EXT_SCRIPT.':'').$EXTiS_cPart;
467 }
468 }
469
470 $TT->pull();
471 } else {
472 $sendTSFEContent = TRUE;
473 }
474 $TT->pull();
475 }
476
477
478 // ********************************
479 // Store session data for fe_users
480 // ********************************
481 $TSFE->storeSessionData();
482
483
484 // ***********
485 // Statistics
486 // ***********
487 $TYPO3_MISC['microtime_end'] = microtime(TRUE);
488 $TSFE->setParseTime();
489 if ($TSFE->isOutputting() && (!empty($TSFE->TYPO3_CONF_VARS['FE']['debug']) || !empty($TSFE->config['config']['debug']))) {
490 $TSFE->content .= LF . '<!-- Parsetime: ' . $TSFE->scriptParseTime . 'ms -->';
491 }
492 $TSFE->statistics();
493
494
495 // ***************
496 // Check JumpUrl
497 // ***************
498 $TSFE->jumpurl();
499
500
501 // *************
502 // Preview info
503 // *************
504 $TSFE->previewInfo();
505
506
507 // ******************
508 // Publishing static
509 // ******************
510 if (is_object($BE_USER) && ($BE_USER->adminPanel instanceof tslib_AdminPanel)) {
511 if ($BE_USER->adminPanel->isAdminModuleEnabled('publish') && $BE_USER->adminPanel->getExtPublishList()) {
512 include_once(PATH_tslib.'publish.php');
513 }
514 }
515
516
517 // ******************
518 // Hook for end-of-frontend
519 // ******************
520 $TSFE->hook_eofe();
521
522
523 // ********************
524 // Finish timetracking
525 // ********************
526 $TT->pull();
527
528
529 // ******************
530 // beLoginLinkIPList
531 // ******************
532 echo $TSFE->beLoginLinkIPList();
533
534
535 // *************
536 // Admin panel
537 // *************
538 if (is_object($BE_USER) && $BE_USER->isAdminPanelVisible() && $TSFE->isBackendUserLoggedIn()) {
539 $TSFE->content = str_ireplace('</head>', $BE_USER->adminPanel->getAdminPanelHeaderData() . '</head>', $TSFE->content);
540 $TSFE->content = str_ireplace('</body>', $BE_USER->displayAdminPanel() . '</body>', $TSFE->content);
541 }
542
543 if ($sendTSFEContent) {
544 echo $TSFE->content;
545 }
546
547 // *************
548 // Debugging Output
549 // *************
550 if(isset($error) && is_object($error) && @is_callable(array($error,'debugOutput'))) {
551 $error->debugOutput();
552 }
553 if (TYPO3_DLOG) {
554 t3lib_div::devLog('END of FRONTEND session', 'cms', 0, array('_FLUSH' => TRUE));
555 }
556
557 ?>