c13c860b390712845223221ea3c011ad07da2495
[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 pathes, 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.0alpha3');
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,em,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/class.t3lib_cache_backend_abstractbackend.php');
226 require_once(PATH_t3lib . 'cache/backend/interfaces/interface.t3lib_cache_backend_phpcapablebackend.php');
227 require_once(PATH_t3lib . 'cache/backend/class.t3lib_cache_backend_filebackend.php');
228 require_once(PATH_t3lib . 'cache/backend/class.t3lib_cache_backend_nullbackend.php');
229
230 require_once(PATH_t3lib . 'class.t3lib_autoloader.php');
231 }
232
233 /**
234 * Compatibility layer for magic quotes
235 *
236 * @return void
237 */
238 protected static function handleMagicQuotesGpc() {
239 if (!get_magic_quotes_gpc()) {
240 t3lib_div::addSlashesOnArray($_GET);
241 t3lib_div::addSlashesOnArray($_POST);
242 $GLOBALS['HTTP_GET_VARS'] = $_GET;
243 $GLOBALS['HTTP_POST_VARS'] = $_POST;
244 }
245 }
246
247 /**
248 * Add typo3/contrib/pear/ as first include folder in
249 * include path, because the shipped PEAR packages use
250 * relative paths to include their files.
251 *
252 * This is required for t3lib_http_Request to work.
253 *
254 * Having the TYPO3 folder first will make sure that the
255 * shipped version is loaded before any local PEAR package,
256 * thus avoiding any incompatibilities with newer or older
257 * versions.
258 *
259 * @return void
260 */
261 protected static function addCorePearPathToIncludePath() {
262 set_include_path(PATH_typo3 . 'contrib/pear/' . PATH_SEPARATOR . get_include_path());
263 }
264
265 /**
266 * Set up / initialize several globals variables
267 *
268 * @return void
269 */
270 protected static function initializeGlobalVariables() {
271 // Unset variable(s) in global scope (security issue #13959)
272 unset($GLOBALS['error']);
273
274 // Set up base information about browser/user-agent
275 $GLOBALS['CLIENT'] = t3lib_div::clientInfo();
276
277 $GLOBALS['TYPO3_MISC'] = array();
278 $GLOBALS['T3_VAR'] = array();
279 $GLOBALS['T3_SERVICES'] = array();
280 }
281
282 /**
283 * Load default TYPO3_CONF_VARS to globals
284 *
285 * @return void
286 */
287 protected static function loadDefaultConfiguration() {
288 $GLOBALS['TYPO3_CONF_VARS'] = t3lib_Configuration::getDefaultConfiguration();
289 }
290
291 /**
292 * Initialize global time tracking variables.
293 * These are helpers to for example output script parsetime at the end of a script.
294 *
295 * @return void
296 */
297 protected static function initializeGlobalTimeTrackingVariables() {
298 // Set PARSETIME_START to the system time in milliseconds.
299 $GLOBALS['PARSETIME_START'] = t3lib_div::milliseconds();
300 // Microtime of (nearly) script start
301 $GLOBALS['TYPO3_MISC']['microtime_start'] = microtime(TRUE);
302
303 // EXEC_TIME is set so that the rest of the script has a common value for the script execution time
304 $GLOBALS['EXEC_TIME'] = time();
305 // $ACCESS_TIME is a common time in minutes for access control
306 $GLOBALS['ACCESS_TIME'] = $GLOBALS['EXEC_TIME'] - ($GLOBALS['EXEC_TIME'] % 60);
307
308 // $SIM_EXEC_TIME is set to $EXEC_TIME but can be altered later in the script if we want to
309 // simulate another execution-time when selecting from eg. a database
310 $GLOBALS['SIM_EXEC_TIME'] = $GLOBALS['EXEC_TIME'];
311 // If $SIM_EXEC_TIME is changed this value must be set accordingly
312 $GLOBALS['SIM_ACCESS_TIME'] = $GLOBALS['ACCESS_TIME'];
313 }
314
315 /**
316 * Initialize basic error reporting.
317 *
318 * There are a lot of extensions that have no strict / notice / deprecated free
319 * ext_localconf or ext_tables. Since the final error reporting must be set up
320 * after those extension files are read, a default configuration is needed to
321 * suppress error reporting meanwhile during further bootstrap.
322 *
323 * @return void
324 */
325 protected static function initializeBasicErrorReporting() {
326 // Core should be notice free at least until this point ...
327 error_reporting(E_ALL & ~(E_STRICT | E_NOTICE | E_DEPRECATED));
328 }
329
330 /**
331 * Determine the operating system TYPO3 is running on.
332 *
333 * @return string Either 'WIN' if running on Windows, else empty string
334 */
335 protected static function getTypo3Os() {
336 $typoOs = '';
337 if (!stristr(PHP_OS, 'darwin') && stristr(PHP_OS, 'win')) {
338 $typoOs = 'WIN';
339 }
340 return $typoOs;
341 }
342
343 /**
344 * Calculate PATH_thisScript
345 *
346 * First step in path calculation: Goal is to find the absolute path of the entry script
347 * that was called without resolving any links. This is important since the TYPO3 entry
348 * points are often linked to a central core location, so we can not use the php magic
349 * __FILE__ here, but resolve the called script path from given server environments.
350 *
351 * This path is important to calculate the document root (PATH_site). The strategy is to
352 * find out the script name that was called in the first place and to subtract the local
353 * part from it to find the document root.
354 *
355 * @return string Absolute path to entry script
356 */
357 protected static function getPathThisScript() {
358 if (defined('TYPO3_cliMode') && TYPO3_cliMode === TRUE) {
359 return self::getPathThisScriptCli();
360 } else {
361 return self::getPathThisScriptNonCli();
362 }
363 }
364
365 /**
366 * Calculate path to entry script if not in cli mode.
367 *
368 * Depending on the environment, the script path is found in different $_SERVER variables.
369 *
370 * @return string Absolute path to entry script
371 */
372 protected static function getPathThisScriptNonCli() {
373 $cgiPath = '';
374 if (isset($_SERVER['ORIG_PATH_TRANSLATED'])) {
375 $cgiPath = $_SERVER['ORIG_PATH_TRANSLATED'];
376 } elseif (isset($_SERVER['PATH_TRANSLATED'])) {
377 $cgiPath = $_SERVER['PATH_TRANSLATED'];
378 }
379 if ($cgiPath && (PHP_SAPI === 'fpm-fcgi' || PHP_SAPI === 'cgi' || PHP_SAPI === 'isapi' || PHP_SAPI === 'cgi-fcgi')) {
380 $scriptPath = $cgiPath;
381 } else {
382 if (isset($_SERVER['ORIG_SCRIPT_FILENAME'])) {
383 $scriptPath = $_SERVER['ORIG_SCRIPT_FILENAME'];
384 } else {
385 $scriptPath = $_SERVER['SCRIPT_FILENAME'];
386 }
387 }
388 // Replace \ to / for Windows
389 $scriptPath = str_replace('\\', '/', $scriptPath);
390 // Replace double // to /
391 $scriptPath = str_replace('//', '/', $scriptPath);
392
393 return $scriptPath;
394 }
395
396 /**
397 * Calculate path to entry script if in cli mode.
398 *
399 * First argument of a cli script is the path to the script that was called. If the script does not start
400 * with / (or A:\ for Windows), the path is not absolute yet, and the current working directory is added.
401 *
402 * @return string Absolute path to entry script
403 */
404 protected static function getPathThisScriptCli() {
405 // Possible relative path of the called script
406 if (isset($_SERVER['argv'][0])) {
407 $scriptPath = $_SERVER['argv'][0];
408 } elseif (isset($_ENV['_'])) {
409 $scriptPath = $_ENV['_'];
410 } else {
411 $scriptPath = $_SERVER['_'];
412 }
413
414 // Find out if path is relative or not
415 $isRelativePath = FALSE;
416 if (TYPO3_OS === 'WIN') {
417 if (!preg_match('/^([A-Z]:)?\\\/', $scriptPath)) {
418 $isRelativePath = TRUE;
419 }
420 } else {
421 if (substr($scriptPath, 0, 1) !== '/') {
422 $isRelativePath = TRUE;
423 }
424 }
425
426 // Concatenate path to current working directory with relative path and remove "/./" constructs
427 if ($isRelativePath) {
428 if (isset($_SERVER['PWD'])) {
429 $workingDirectory = $_SERVER['PWD'];
430 } else {
431 $workingDirectory = getcwd();
432 }
433 $scriptPath = $workingDirectory . '/' . preg_replace('/\.\//', '', $scriptPath);
434 }
435
436 return $scriptPath;
437 }
438
439 /**
440 * Calculate the document root part to the instance from PATH_thisScript
441 *
442 * There are two ways to hint correct calculation:
443 * Either an explicit specified sub path or the defined constant TYPO3_MOD_PATH. Which one is
444 * used depends on which entry script was called in the first place.
445 *
446 * We have two main scenarios for entry points:
447 * - Directly called documentRoot/index.php (-> FE call or eiD include): index.php sets $relativePathPart to
448 * empty string to hint this code that the document root is identical to the directory the script is located at.
449 * - An indirect include of typo3/init.php (-> a backend module, the install tool, or scripts like thumbs.php).
450 * If init.php is included we distinguish two cases:
451 * -- A backend module defines 'TYPO3_MOD_PATH': This is the case for "old" modules that are not called through
452 * "mod.php" dispatcher, and in the install tool. The TYPO3_MOD_PATH defines the relative path to the typo3/
453 * directory. This is taken as base to calculate the document root.
454 * -- A script includes init.php and does not define 'TYPO3_MOD_PATH': This is the case for the mod.php dispatcher
455 * and other entry scripts like 'cli_dispatch.phpsh' or 'thumbs.php' that are located parallel to init.php. In
456 * this case init.php sets 'typo3/' as $relativePathPart as base to calculate the document root.
457 *
458 * This basically boils down to the following code:
459 * If TYPO3_MOD_PATH is defined, subtract this 'local' part from the entry point directory, else use
460 * $relativePathPart to subtract this from the the script entry point to find out the document root.
461 *
462 * @param string $relativePathPart Relative directory part from document root to script path if TYPO3_MOD_PATH is not used
463 * @return string Absolute path to document root of installation
464 */
465 protected static function getPathSite($relativePathPart) {
466 // If end of path is not "typo3/" and TYPO3_MOD_PATH is given
467 if (defined('TYPO3_MOD_PATH')) {
468 return self::getPathSiteByTypo3ModulePath();
469 } else {
470 return self::getPathSiteByRelativePathPart($relativePathPart);
471 }
472 }
473
474 /**
475 * Calculate document root by TYPO3_MOD_PATH
476 *
477 * TYPO3_MOD_PATH can have the following values:
478 * - "sysext/extensionName/path/entryScript.php" -> extension is below 'docRoot'/typo3/sysext
479 * - "ext/extensionName/path/entryScript.php" -> extension is below 'docRoot'/typo3/ext
480 * - "../typo3conf/ext/extensionName/path/entryScript.php" -> extension is below 'docRoot'/typo3conf/ext
481 *- "install/index.php" -> install tool in 'docRoot'/typo3/install/
482 *
483 * The method unifies the above and subtracts the calculated path part from PATH_thisScript
484 *
485 * @return string Absolute path to document root of installation
486 */
487 protected static function getPathSiteByTypo3ModulePath() {
488 if (
489 substr(TYPO3_MOD_PATH, 0, strlen('sysext/')) === 'sysext/'
490 || substr(TYPO3_MOD_PATH, 0, strlen('ext/')) === 'ext/'
491 || substr(TYPO3_MOD_PATH, 0, strlen('install/')) === 'install/'
492 ) {
493 $pathPartRelativeToDocumentRoot = TYPO3_mainDir . TYPO3_MOD_PATH;
494 } elseif (substr(TYPO3_MOD_PATH, 0, strlen('../typo3conf/')) === '../typo3conf/') {
495 $pathPartRelativeToDocumentRoot = substr(TYPO3_MOD_PATH, 3);
496 } else {
497 die('Unable to determine TYPO3 document root.');
498 }
499
500 $entryScriptDirectory = self::getUnifiedDirectoryNameWithTrailingSlash(PATH_thisScript);
501
502 return substr($entryScriptDirectory, 0, -strlen($pathPartRelativeToDocumentRoot));
503 }
504
505 /**
506 * Find out document root by subtracting $relativePathPart from PATH_thisScript
507 *
508 * @param string $relativePathPart Relative part of script from document root
509 * @return string Absolute path to document root of installation
510 */
511 protected static function getPathSiteByRelativePathPart($relativePathPart) {
512 $entryScriptDirectory = self::getUnifiedDirectoryNameWithTrailingSlash(PATH_thisScript);
513 if (strlen($relativePathPart) > 0) {
514 $pathSite = substr($entryScriptDirectory, 0, -strlen($relativePathPart));
515 } else {
516 $pathSite = $entryScriptDirectory;
517 }
518 return $pathSite;
519 }
520
521 /**
522 * Remove file name from script path and unify for Windows and Unix
523 *
524 * @param string $absolutePath Absolute path to script
525 * @return string Directory name of script file location, unified for Windows and Unix
526 */
527 protected static function getUnifiedDirectoryNameWithTrailingSlash($absolutePath) {
528 $directory = dirname($absolutePath);
529 if (TYPO3_OS === 'WIN') {
530 $directory = str_replace('\\', '/', $directory);
531 }
532 return $directory . '/';
533 }
534 }
535 ?>