3048e230d89124983a0f64e8a42e84ef86bf5070
[Packages/TYPO3.CMS.git] / typo3 / classes / Bootstrap / BaseSetup.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2012 Christian Kuhn <lolli@schwarzbu.ch>
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 /**
29 * Class to encapsulate base setup of bootstrap.
30 *
31 * This class contains all code that must be executed by every
32 * entry script without exceptions.
33 *
34 * It sets up all basic paths, constants, global variables and checks
35 * the basic environment TYPO3 runs in.
36 *
37 * This class does not use any TYPO3 instance specific configuration, it only
38 * sets up things based on the server environment and core code. Even with a
39 * missing typo3conf/localconf.php this script will be successful.
40 *
41 * The script aborts execution with an error message if
42 * some part fails or conditions are not met.
43 */
44 class Typo3_Bootstrap_BaseSetup {
45
46 /**
47 * Run base setup.
48 *
49 * @param string $relativePathPart Relative path of the entry script back to document root
50 * @return void
51 */
52 public static function run($relativePathPart = '') {
53 self::checkPhpVersionOrDie();
54 self::checkGlobalsAreNotSetViaPostOrGet();
55 self::defineBaseConstants();
56 self::definePaths($relativePathPart);
57 self::checkMainPathsExist();
58 self::requireBaseClasses();
59 self::handleMagicQuotesGpc();
60 self::addCorePearPathToIncludePath();
61 self::initializeGlobalVariables();
62 self::loadDefaultConfiguration();
63 self::initializeGlobalTimeTrackingVariables();
64 self::initializeBasicErrorReporting();
65 }
66
67 /**
68 * Check php version requirement or exit script
69 *
70 * @return void
71 */
72 protected static function checkPhpVersionOrDie() {
73 if (version_compare(phpversion(), '5.3', '<')) {
74 die('TYPO3 requires PHP 5.3.0 or higher.');
75 }
76 }
77
78 /**
79 * Exit script if globals are set via post or get
80 *
81 * @return void
82 */
83 protected static function checkGlobalsAreNotSetViaPostOrGet() {
84 if (isset($_POST['GLOBALS']) || isset($_GET['GLOBALS'])) {
85 die('You cannot set the GLOBALS array from outside the script.');
86 }
87 }
88
89 /**
90 * Define all simple constants that have no dependency to local configuration
91 *
92 * @return void
93 */
94 protected static function defineBaseConstants() {
95 // This version, branch and copyright
96 define('TYPO3_version', '6.0-dev');
97 define('TYPO3_branch', '6.0');
98 define('TYPO3_copyright_year', '1998-2012');
99
100 // TYPO3 external links
101 define('TYPO3_URL_GENERAL', 'http://typo3.org/');
102 define('TYPO3_URL_ORG', 'http://typo3.org/');
103 define('TYPO3_URL_LICENSE', 'http://typo3.org/licenses');
104 define('TYPO3_URL_EXCEPTION', 'http://typo3.org/go/exception/v4/');
105 define('TYPO3_URL_MAILINGLISTS', 'http://lists.typo3.org/cgi-bin/mailman/listinfo');
106 define('TYPO3_URL_DOCUMENTATION', 'http://typo3.org/documentation/');
107 define('TYPO3_URL_DOCUMENTATION_TSREF', 'http://typo3.org/documentation/document-library/core-documentation/doc_core_tsref/current/view/');
108 define('TYPO3_URL_DOCUMENTATION_TSCONFIG', 'http://typo3.org/documentation/document-library/core-documentation/doc_core_tsconfig/current/view/');
109 define('TYPO3_URL_CONSULTANCY', 'http://typo3.org/support/professional-services/');
110 define('TYPO3_URL_CONTRIBUTE', 'http://typo3.org/contribute/');
111 define('TYPO3_URL_SECURITY', 'http://typo3.org/teams/security/');
112 define('TYPO3_URL_DOWNLOAD', 'http://typo3.org/download/');
113 define('TYPO3_URL_SYSTEMREQUIREMENTS', 'http://typo3.org/about/typo3-the-cms/system-requirements/');
114 define('TYPO3_URL_DONATE', 'http://typo3.org/donate/online-donation/');
115
116 // A tabulator, a linefeed, a carriage return, a CR-LF combination
117 define('TAB', chr(9));
118 define('LF', chr(10));
119 define('CR', chr(13));
120 define('CRLF', CR . LF);
121
122 // Security related constant: Default value of fileDenyPattern
123 define('FILE_DENY_PATTERN_DEFAULT', '\.(php[3-6]?|phpsh|phtml)(\..*)?$|^\.htaccess$');
124
125 // Security related constant: List of file extensions that should be registered as php script file extensions
126 define('PHP_EXTENSIONS_DEFAULT', 'php,php3,php4,php5,php6,phpsh,inc,phtml');
127
128 // List of extensions required to run the core
129 define('REQUIRED_EXTENSIONS', 'cms,lang,sv,extensionmanager,recordlist,extbase,fluid');
130
131 // Operating system identifier
132 // Either "WIN" or empty string
133 define('TYPO3_OS', self::getTypo3Os());
134 }
135
136 /**
137 * Calculate all required base paths and set as constants.
138 *
139 * @param string $relativePathPart Relative path of the entry script back to document root
140 * @return void
141 */
142 protected static function definePaths($relativePathPart = '') {
143 // Relative path from document root to typo3/ directory
144 // Hardcoded to "typo3/"
145 define('TYPO3_mainDir', 'typo3/');
146
147 // Absolute path of the entry script that was called
148 // All paths are unified between Windows and Unix, so the \ of Windows is substituted to a /
149 // Example "/var/www/instance-name/htdocs/typo3conf/ext/wec_map/mod1/index.php"
150 // Example "c:/var/www/instance-name/htdocs/typo3/backend.php" for a path in Windows
151 define('PATH_thisScript', self::getPathThisScript());
152
153 // Absolute path of the document root of the instance with trailing slash
154 // Example "/var/www/instance-name/htdocs/"
155 define('PATH_site', self::getPathSite($relativePathPart));
156
157 // Absolute path of the typo3 directory of the instance with trailing slash
158 // Example "/var/www/instance-name/htdocs/typo3/"
159 define('PATH_typo3', PATH_site . TYPO3_mainDir);
160
161 // Relative path (from the PATH_typo3) to a BE module NOT using mod.php dispatcher with trailing slash
162 // Example "sysext/perms/mod/" for an extension installed in typo3/sysext/
163 // Example "install/" for the install tool entry script
164 // Example "../typo3conf/ext/templavoila/mod2/ for an extension installed in typo3conf/ext/
165 define('PATH_typo3_mod', defined('TYPO3_MOD_PATH') ? TYPO3_MOD_PATH : '');
166
167 // Absolute path to the t3lib directory with trailing slash
168 // Example "/var/www/instance-name/htdocs/t3lib/"
169 define('PATH_t3lib', PATH_site . 't3lib/');
170
171 // Absolute path to the typo3conf directory with trailing slash
172 // Example "/var/www/instance-name/htdocs/typo3conf/"
173 define('PATH_typo3conf', PATH_site . 'typo3conf/');
174
175 // Absolute path to the tslib directory with trailing slash
176 // Example "/var/www/instance-name/htdocs/typo3/sysext/cms/tslib/"
177 define('PATH_tslib', PATH_typo3 . 'sysext/cms/tslib/');
178 }
179
180 /**
181 * Check if path and script file name calculation was successful, exit if not.
182 *
183 * @return void
184 */
185 protected static function checkMainPathsExist() {
186 if (!is_file(PATH_thisScript)) {
187 die('Unable to determine path to entry script.');
188 }
189 if (!is_dir(PATH_t3lib)) {
190 die('Calculated absolute path to t3lib directory does not exist.');
191 }
192 if (!is_dir(PATH_tslib)) {
193 die('Calculated absolute path to tslib directory does not exist.');
194 }
195 if (!is_dir(PATH_typo3conf)) {
196 die('Calculated absolute path to typo3conf directory does not exist');
197 }
198 }
199
200 /**
201 * Load several base classes during bootstrap
202 *
203 * @return void
204 */
205 protected static function requireBaseClasses() {
206 /** @define "PATH_t3lib" "../../../t3lib/" */
207 require_once(PATH_t3lib . 'class.t3lib_div.php');
208
209 require_once(PATH_t3lib . 'utility/class.t3lib_utility_array.php');
210 require_once(PATH_t3lib . 'class.t3lib_configuration.php');
211 require_once(PATH_t3lib . 'class.t3lib_extmgm.php');
212
213 require_once(PATH_t3lib . 'class.t3lib_cache.php');
214 require_once(PATH_t3lib . 'cache/class.t3lib_cache_exception.php');
215 require_once(PATH_t3lib . 'cache/exception/class.t3lib_cache_exception_nosuchcache.php');
216 require_once(PATH_t3lib . 'cache/exception/class.t3lib_cache_exception_invaliddata.php');
217 require_once(PATH_t3lib . 'interfaces/interface.t3lib_singleton.php');
218 require_once(PATH_t3lib . 'cache/class.t3lib_cache_factory.php');
219 require_once(PATH_t3lib . 'cache/class.t3lib_cache_manager.php');
220 require_once(PATH_t3lib . 'cache/frontend/interfaces/interface.t3lib_cache_frontend_frontend.php');
221 require_once(PATH_t3lib . 'cache/frontend/class.t3lib_cache_frontend_abstractfrontend.php');
222 require_once(PATH_t3lib . 'cache/frontend/class.t3lib_cache_frontend_stringfrontend.php');
223 require_once(PATH_t3lib . 'cache/frontend/class.t3lib_cache_frontend_phpfrontend.php');
224 require_once(PATH_t3lib . 'cache/backend/interfaces/interface.t3lib_cache_backend_backend.php');
225 require_once(PATH_t3lib . 'cache/backend/interfaces/interface.t3lib_cache_backend_taggablebackend.php');
226 require_once(PATH_t3lib . 'cache/backend/class.t3lib_cache_backend_abstractbackend.php');
227 require_once(PATH_t3lib . 'cache/backend/interfaces/interface.t3lib_cache_backend_phpcapablebackend.php');
228 require_once(PATH_t3lib . 'cache/backend/class.t3lib_cache_backend_simplefilebackend.php');
229 require_once(PATH_t3lib . 'cache/backend/class.t3lib_cache_backend_nullbackend.php');
230 require_once(PATH_t3lib . 'log/class.t3lib_log_level.php');
231
232 require_once(PATH_t3lib . 'class.t3lib_autoloader.php');
233 }
234
235 /**
236 * Compatibility layer for magic quotes
237 *
238 * @return void
239 */
240 protected static function handleMagicQuotesGpc() {
241 if (!get_magic_quotes_gpc()) {
242 t3lib_div::addSlashesOnArray($_GET);
243 t3lib_div::addSlashesOnArray($_POST);
244 $GLOBALS['HTTP_GET_VARS'] = $_GET;
245 $GLOBALS['HTTP_POST_VARS'] = $_POST;
246 }
247 }
248
249 /**
250 * Add typo3/contrib/pear/ as first include folder in
251 * include path, because the shipped PEAR packages use
252 * relative paths to include their files.
253 *
254 * This is required for t3lib_http_Request to work.
255 *
256 * Having the TYPO3 folder first will make sure that the
257 * shipped version is loaded before any local PEAR package,
258 * thus avoiding any incompatibilities with newer or older
259 * versions.
260 *
261 * @return void
262 */
263 protected static function addCorePearPathToIncludePath() {
264 set_include_path(PATH_typo3 . 'contrib/pear/' . PATH_SEPARATOR . get_include_path());
265 }
266
267 /**
268 * Set up / initialize several globals variables
269 *
270 * @return void
271 */
272 protected static function initializeGlobalVariables() {
273 // Unset variable(s) in global scope (security issue #13959)
274 unset($GLOBALS['error']);
275
276 // Set up base information about browser/user-agent
277 $GLOBALS['CLIENT'] = t3lib_div::clientInfo();
278
279 $GLOBALS['TYPO3_MISC'] = array();
280 $GLOBALS['T3_VAR'] = array();
281 $GLOBALS['T3_SERVICES'] = array();
282 }
283
284 /**
285 * Load default TYPO3_CONF_VARS to globals
286 *
287 * @return void
288 */
289 protected static function loadDefaultConfiguration() {
290 $GLOBALS['TYPO3_CONF_VARS'] = t3lib_Configuration::getDefaultConfiguration();
291 }
292
293 /**
294 * Initialize global time tracking variables.
295 * These are helpers to for example output script parsetime at the end of a script.
296 *
297 * @return void
298 */
299 protected static function initializeGlobalTimeTrackingVariables() {
300 // Set PARSETIME_START to the system time in milliseconds.
301 $GLOBALS['PARSETIME_START'] = t3lib_div::milliseconds();
302 // Microtime of (nearly) script start
303 $GLOBALS['TYPO3_MISC']['microtime_start'] = microtime(TRUE);
304
305 // EXEC_TIME is set so that the rest of the script has a common value for the script execution time
306 $GLOBALS['EXEC_TIME'] = time();
307 // $ACCESS_TIME is a common time in minutes for access control
308 $GLOBALS['ACCESS_TIME'] = $GLOBALS['EXEC_TIME'] - ($GLOBALS['EXEC_TIME'] % 60);
309
310 // $SIM_EXEC_TIME is set to $EXEC_TIME but can be altered later in the script if we want to
311 // simulate another execution-time when selecting from eg. a database
312 $GLOBALS['SIM_EXEC_TIME'] = $GLOBALS['EXEC_TIME'];
313 // If $SIM_EXEC_TIME is changed this value must be set accordingly
314 $GLOBALS['SIM_ACCESS_TIME'] = $GLOBALS['ACCESS_TIME'];
315 }
316
317 /**
318 * Initialize basic error reporting.
319 *
320 * There are a lot of extensions that have no strict / notice / deprecated free
321 * ext_localconf or ext_tables. Since the final error reporting must be set up
322 * after those extension files are read, a default configuration is needed to
323 * suppress error reporting meanwhile during further bootstrap.
324 *
325 * @return void
326 */
327 protected static function initializeBasicErrorReporting() {
328 // Core should be notice free at least until this point ...
329 error_reporting(E_ALL & ~(E_STRICT | E_NOTICE | E_DEPRECATED));
330 }
331
332 /**
333 * Determine the operating system TYPO3 is running on.
334 *
335 * @return string Either 'WIN' if running on Windows, else empty string
336 */
337 protected static function getTypo3Os() {
338 $typoOs = '';
339 if (!stristr(PHP_OS, 'darwin') && stristr(PHP_OS, 'win')) {
340 $typoOs = 'WIN';
341 }
342 return $typoOs;
343 }
344
345 /**
346 * Calculate PATH_thisScript
347 *
348 * First step in path calculation: Goal is to find the absolute path of the entry script
349 * that was called without resolving any links. This is important since the TYPO3 entry
350 * points are often linked to a central core location, so we can not use the php magic
351 * __FILE__ here, but resolve the called script path from given server environments.
352 *
353 * This path is important to calculate the document root (PATH_site). The strategy is to
354 * find out the script name that was called in the first place and to subtract the local
355 * part from it to find the document root.
356 *
357 * @return string Absolute path to entry script
358 */
359 protected static function getPathThisScript() {
360 if (defined('TYPO3_cliMode') && TYPO3_cliMode === TRUE) {
361 return self::getPathThisScriptCli();
362 } else {
363 return self::getPathThisScriptNonCli();
364 }
365 }
366
367 /**
368 * Calculate path to entry script if not in cli mode.
369 *
370 * Depending on the environment, the script path is found in different $_SERVER variables.
371 *
372 * @return string Absolute path to entry script
373 */
374 protected static function getPathThisScriptNonCli() {
375 $cgiPath = '';
376 if (isset($_SERVER['ORIG_PATH_TRANSLATED'])) {
377 $cgiPath = $_SERVER['ORIG_PATH_TRANSLATED'];
378 } elseif (isset($_SERVER['PATH_TRANSLATED'])) {
379 $cgiPath = $_SERVER['PATH_TRANSLATED'];
380 }
381 if ($cgiPath && (PHP_SAPI === 'fpm-fcgi' || PHP_SAPI === 'cgi' || PHP_SAPI === 'isapi' || PHP_SAPI === 'cgi-fcgi')) {
382 $scriptPath = $cgiPath;
383 } else {
384 if (isset($_SERVER['ORIG_SCRIPT_FILENAME'])) {
385 $scriptPath = $_SERVER['ORIG_SCRIPT_FILENAME'];
386 } else {
387 $scriptPath = $_SERVER['SCRIPT_FILENAME'];
388 }
389 }
390 // Replace \ to / for Windows
391 $scriptPath = str_replace('\\', '/', $scriptPath);
392 // Replace double // to /
393 $scriptPath = str_replace('//', '/', $scriptPath);
394
395 return $scriptPath;
396 }
397
398 /**
399 * Calculate path to entry script if in cli mode.
400 *
401 * First argument of a cli script is the path to the script that was called. If the script does not start
402 * with / (or A:\ for Windows), the path is not absolute yet, and the current working directory is added.
403 *
404 * @return string Absolute path to entry script
405 */
406 protected static function getPathThisScriptCli() {
407 // Possible relative path of the called script
408 if (isset($_SERVER['argv'][0])) {
409 $scriptPath = $_SERVER['argv'][0];
410 } elseif (isset($_ENV['_'])) {
411 $scriptPath = $_ENV['_'];
412 } else {
413 $scriptPath = $_SERVER['_'];
414 }
415
416 // Find out if path is relative or not
417 $isRelativePath = FALSE;
418 if (TYPO3_OS === 'WIN') {
419 if (!preg_match('/^([A-Z]:)?\\\/', $scriptPath)) {
420 $isRelativePath = TRUE;
421 }
422 } else {
423 if (substr($scriptPath, 0, 1) !== '/') {
424 $isRelativePath = TRUE;
425 }
426 }
427
428 // Concatenate path to current working directory with relative path and remove "/./" constructs
429 if ($isRelativePath) {
430 if (isset($_SERVER['PWD'])) {
431 $workingDirectory = $_SERVER['PWD'];
432 } else {
433 $workingDirectory = getcwd();
434 }
435 $scriptPath = $workingDirectory . '/' . preg_replace('/\.\//', '', $scriptPath);
436 }
437
438 return $scriptPath;
439 }
440
441 /**
442 * Calculate the document root part to the instance from PATH_thisScript
443 *
444 * There are two ways to hint correct calculation:
445 * Either an explicit specified sub path or the defined constant TYPO3_MOD_PATH. Which one is
446 * used depends on which entry script was called in the first place.
447 *
448 * We have two main scenarios for entry points:
449 * - Directly called documentRoot/index.php (-> FE call or eiD include): index.php sets $relativePathPart to
450 * empty string to hint this code that the document root is identical to the directory the script is located at.
451 * - An indirect include of typo3/init.php (-> a backend module, the install tool, or scripts like thumbs.php).
452 * If init.php is included we distinguish two cases:
453 * -- A backend module defines 'TYPO3_MOD_PATH': This is the case for "old" modules that are not called through
454 * "mod.php" dispatcher, and in the install tool. The TYPO3_MOD_PATH defines the relative path to the typo3/
455 * directory. This is taken as base to calculate the document root.
456 * -- A script includes init.php and does not define 'TYPO3_MOD_PATH': This is the case for the mod.php dispatcher
457 * and other entry scripts like 'cli_dispatch.phpsh' or 'thumbs.php' that are located parallel to init.php. In
458 * this case init.php sets 'typo3/' as $relativePathPart as base to calculate the document root.
459 *
460 * This basically boils down to the following code:
461 * If TYPO3_MOD_PATH is defined, subtract this 'local' part from the entry point directory, else use
462 * $relativePathPart to subtract this from the the script entry point to find out the document root.
463 *
464 * @param string $relativePathPart Relative directory part from document root to script path if TYPO3_MOD_PATH is not used
465 * @return string Absolute path to document root of installation
466 */
467 protected static function getPathSite($relativePathPart) {
468 // If end of path is not "typo3/" and TYPO3_MOD_PATH is given
469 if (defined('TYPO3_MOD_PATH')) {
470 return self::getPathSiteByTypo3ModulePath();
471 } else {
472 return self::getPathSiteByRelativePathPart($relativePathPart);
473 }
474 }
475
476 /**
477 * Calculate document root by TYPO3_MOD_PATH
478 *
479 * TYPO3_MOD_PATH can have the following values:
480 * - "sysext/extensionName/path/entryScript.php" -> extension is below 'docRoot'/typo3/sysext
481 * - "ext/extensionName/path/entryScript.php" -> extension is below 'docRoot'/typo3/ext
482 * - "../typo3conf/ext/extensionName/path/entryScript.php" -> extension is below 'docRoot'/typo3conf/ext
483 *- "install/index.php" -> install tool in 'docRoot'/typo3/install/
484 *
485 * The method unifies the above and subtracts the calculated path part from PATH_thisScript
486 *
487 * @return string Absolute path to document root of installation
488 */
489 protected static function getPathSiteByTypo3ModulePath() {
490 if (
491 substr(TYPO3_MOD_PATH, 0, strlen('sysext/')) === 'sysext/'
492 || substr(TYPO3_MOD_PATH, 0, strlen('ext/')) === 'ext/'
493 || substr(TYPO3_MOD_PATH, 0, strlen('install/')) === 'install/'
494 ) {
495 $pathPartRelativeToDocumentRoot = TYPO3_mainDir . TYPO3_MOD_PATH;
496 } elseif (substr(TYPO3_MOD_PATH, 0, strlen('../typo3conf/')) === '../typo3conf/') {
497 $pathPartRelativeToDocumentRoot = substr(TYPO3_MOD_PATH, 3);
498 } else {
499 die('Unable to determine TYPO3 document root.');
500 }
501
502 $entryScriptDirectory = self::getUnifiedDirectoryNameWithTrailingSlash(PATH_thisScript);
503
504 return substr($entryScriptDirectory, 0, -strlen($pathPartRelativeToDocumentRoot));
505 }
506
507 /**
508 * Find out document root by subtracting $relativePathPart from PATH_thisScript
509 *
510 * @param string $relativePathPart Relative part of script from document root
511 * @return string Absolute path to document root of installation
512 */
513 protected static function getPathSiteByRelativePathPart($relativePathPart) {
514 $entryScriptDirectory = self::getUnifiedDirectoryNameWithTrailingSlash(PATH_thisScript);
515 if (strlen($relativePathPart) > 0) {
516 $pathSite = substr($entryScriptDirectory, 0, -strlen($relativePathPart));
517 } else {
518 $pathSite = $entryScriptDirectory;
519 }
520 return $pathSite;
521 }
522
523 /**
524 * Remove file name from script path and unify for Windows and Unix
525 *
526 * @param string $absolutePath Absolute path to script
527 * @return string Directory name of script file location, unified for Windows and Unix
528 */
529 protected static function getUnifiedDirectoryNameWithTrailingSlash($absolutePath) {
530 $directory = dirname($absolutePath);
531 if (TYPO3_OS === 'WIN') {
532 $directory = str_replace('\\', '/', $directory);
533 }
534 return $directory . '/';
535 }
536 }
537 ?>