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