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