f7237932415686ccfc78f2b051077e3019e6622e
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Core / SystemEnvironmentBuilder.php
1 <?php
2 namespace TYPO3\CMS\Core\Core;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012 Christian Kuhn <lolli@schwarzbu.ch>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Class to encapsulate base setup of bootstrap.
31 *
32 * This class contains all code that must be executed by every
33 * entry script without exceptions.
34 *
35 * It sets up all basic paths, constants, global variables and checks
36 * the basic environment TYPO3 runs in.
37 *
38 * This class does not use any TYPO3 instance specific configuration, it only
39 * sets up things based on the server environment and core code. Even with a
40 * missing typo3conf/localconf.php this script will be successful.
41 *
42 * The script aborts execution with an error message if
43 * some part fails or conditions are not met.
44 */
45 class SystemEnvironmentBuilder {
46
47 /**
48 * Run base setup.
49 *
50 * @param string $relativePathPart Relative path of the entry script back to document root
51 * @return void
52 */
53 static public function run($relativePathPart = '') {
54 self::ensureRequiredEnvironment();
55 self::checkGlobalsAreNotSetViaPostOrGet();
56 self::defineBaseConstants();
57 self::definePaths($relativePathPart);
58 self::checkMainPathsExist();
59 self::requireBaseClasses();
60 self::handleMagicQuotesGpc();
61 self::addCorePearPathToIncludePath();
62 self::initializeGlobalVariables();
63 self::loadDefaultConfiguration();
64 self::initializeGlobalTimeTrackingVariables();
65 self::initializeBasicErrorReporting();
66 }
67
68 /**
69 * Check php version requirement or exit script
70 *
71 * @return void
72 */
73 static protected function ensureRequiredEnvironment() {
74 if (version_compare(phpversion(), '5.3', '<')) {
75 die('TYPO3 requires PHP 5.3.0 or higher.');
76 }
77 if (self::getPhpIniValueBoolean('register_globals')) {
78 die('TYPO3 requires PHP setting "register_globals" set to Off. (Error: #1345284320)');
79 }
80 }
81
82 /**
83 * Cast a on/off php ini value to boolean
84 *
85 * @param string $configOption
86 * @return boolean TRUE if the given option is enabled, FALSE if disabled
87 * @see t3lib_utility_PhpOptions::getIniValueBoolean
88 */
89 static protected function getPhpIniValueBoolean($configOption) {
90 return filter_var(ini_get($configOption), FILTER_VALIDATE_BOOLEAN, array(FILTER_REQUIRE_SCALAR, FILTER_NULL_ON_FAILURE));
91 }
92
93 /**
94 * Exit script if globals are set via post or get
95 *
96 * @return void
97 */
98 static protected function checkGlobalsAreNotSetViaPostOrGet() {
99 if (isset($_POST['GLOBALS']) || isset($_GET['GLOBALS'])) {
100 die('You cannot set the GLOBALS array from outside the script.');
101 }
102 }
103
104 /**
105 * Define all simple constants that have no dependency to local configuration
106 *
107 * @return void
108 */
109 static protected function defineBaseConstants() {
110 // This version, branch and copyright
111 define('TYPO3_version', '6.0-dev');
112 define('TYPO3_branch', '6.0');
113 define('TYPO3_copyright_year', '1998-2012');
114 // TYPO3 external links
115 define('TYPO3_URL_GENERAL', 'http://typo3.org/');
116 define('TYPO3_URL_ORG', 'http://typo3.org/');
117 define('TYPO3_URL_LICENSE', 'http://typo3.org/licenses');
118 define('TYPO3_URL_EXCEPTION', 'http://typo3.org/go/exception/v4/');
119 define('TYPO3_URL_MAILINGLISTS', 'http://lists.typo3.org/cgi-bin/mailman/listinfo');
120 define('TYPO3_URL_DOCUMENTATION', 'http://typo3.org/documentation/');
121 define('TYPO3_URL_DOCUMENTATION_TSREF', 'http://typo3.org/documentation/document-library/core-documentation/doc_core_tsref/current/view/');
122 define('TYPO3_URL_DOCUMENTATION_TSCONFIG', 'http://typo3.org/documentation/document-library/core-documentation/doc_core_tsconfig/current/view/');
123 define('TYPO3_URL_CONSULTANCY', 'http://typo3.org/support/professional-services/');
124 define('TYPO3_URL_CONTRIBUTE', 'http://typo3.org/contribute/');
125 define('TYPO3_URL_SECURITY', 'http://typo3.org/teams/security/');
126 define('TYPO3_URL_DOWNLOAD', 'http://typo3.org/download/');
127 define('TYPO3_URL_SYSTEMREQUIREMENTS', 'http://typo3.org/about/typo3-the-cms/system-requirements/');
128 define('TYPO3_URL_DONATE', 'http://typo3.org/donate/online-donation/');
129 // A tabulator, a linefeed, a carriage return, a CR-LF combination
130 define('TAB', chr(9));
131 define('LF', chr(10));
132 define('CR', chr(13));
133 define('CRLF', CR . LF);
134 // Security related constant: Default value of fileDenyPattern
135 define('FILE_DENY_PATTERN_DEFAULT', '\\.(php[3-6]?|phpsh|phtml)(\\..*)?$|^\\.htaccess$');
136 // Security related constant: List of file extensions that should be registered as php script file extensions
137 define('PHP_EXTENSIONS_DEFAULT', 'php,php3,php4,php5,php6,phpsh,inc,phtml');
138 // List of extensions required to run the core
139 define('REQUIRED_EXTENSIONS', 'core,backend,frontend,cli,integrity,cms,lang,sv,extensionmanager,recordlist,extbase,fluid,cshmanual');
140 // Operating system identifier
141 // Either "WIN" or empty string
142 define('TYPO3_OS', self::getTypo3Os());
143 }
144
145 /**
146 * Calculate all required base paths and set as constants.
147 *
148 * @param string $relativePathPart Relative path of the entry script back to document root
149 * @return void
150 */
151 static protected function definePaths($relativePathPart = '') {
152 // Relative path from document root to typo3/ directory
153 // Hardcoded to "typo3/"
154 define('TYPO3_mainDir', 'typo3/');
155 // Absolute path of the entry script that was called
156 // All paths are unified between Windows and Unix, so the \ of Windows is substituted to a /
157 // Example "/var/www/instance-name/htdocs/typo3conf/ext/wec_map/mod1/index.php"
158 // Example "c:/var/www/instance-name/htdocs/typo3/backend.php" for a path in Windows
159 define('PATH_thisScript', self::getPathThisScript());
160 // Absolute path of the document root of the instance with trailing slash
161 // Example "/var/www/instance-name/htdocs/"
162 define('PATH_site', self::getPathSite($relativePathPart));
163 // Absolute path of the typo3 directory of the instance with trailing slash
164 // Example "/var/www/instance-name/htdocs/typo3/"
165 define('PATH_typo3', PATH_site . TYPO3_mainDir);
166 // Relative path (from the PATH_typo3) to a BE module NOT using mod.php dispatcher with trailing slash
167 // Example "sysext/perms/mod/" for an extension installed in typo3/sysext/
168 // Example "install/" for the install tool entry script
169 // Example "../typo3conf/ext/templavoila/mod2/ for an extension installed in typo3conf/ext/
170 define('PATH_typo3_mod', defined('TYPO3_MOD_PATH') ? TYPO3_MOD_PATH : '');
171 // Absolute path to the t3lib directory with trailing slash
172 // Example "/var/www/instance-name/htdocs/t3lib/"
173 define('PATH_t3lib', PATH_site . 't3lib/');
174 // Absolute path to the typo3conf directory with trailing slash
175 // Example "/var/www/instance-name/htdocs/typo3conf/"
176 define('PATH_typo3conf', PATH_site . 'typo3conf/');
177 // Absolute path to the tslib directory with trailing slash
178 // Example "/var/www/instance-name/htdocs/typo3/sysext/cms/tslib/"
179 define('PATH_tslib', PATH_typo3 . 'sysext/cms/tslib/');
180 }
181
182 /**
183 * Check if path and script file name calculation was successful, exit if not.
184 *
185 * @return void
186 */
187 static protected function checkMainPathsExist() {
188 if (!is_file(PATH_thisScript)) {
189 die('Unable to determine path to entry script.');
190 }
191 if (!is_dir(PATH_t3lib)) {
192 die('Calculated absolute path to t3lib directory does not exist.');
193 }
194 if (!is_dir(PATH_tslib)) {
195 die('Calculated absolute path to tslib directory does not exist.');
196 }
197 if (!is_dir(PATH_typo3conf)) {
198 die('Calculated absolute path to typo3conf directory does not exist');
199 }
200 }
201
202 /**
203 * Load several base classes during bootstrap
204 *
205 * @return void
206 */
207 static protected function requireBaseClasses() {
208 require_once __DIR__ . '/../Utility/GeneralUtility.php';
209 require_once __DIR__ . '/../Utility/ArrayUtility.php';
210 require_once __DIR__ . '/../Configuration/ConfigurationManager.php';
211 require_once __DIR__ . '/../Extension/ExtensionManager.php';
212 require_once __DIR__ . '/../Cache/Cache.php';
213 require_once __DIR__ . '/../Cache/Exception.php';
214 require_once __DIR__ . '/../Cache/Exception/NoSuchCacheException.php';
215 require_once __DIR__ . '/../Cache/Exception/InvalidDataException.php';
216 require_once __DIR__ . '/../SingletonInterface.php';
217 require_once __DIR__ . '/../Cache/CacheFactory.php';
218 require_once __DIR__ . '/../Cache/CacheManager.php';
219 require_once __DIR__ . '/../Cache/Frontend/FrontendInterface.php';
220 require_once __DIR__ . '/../Cache/Frontend/AbstractFrontend.php';
221 require_once __DIR__ . '/../Cache/Frontend/StringFrontend.php';
222 require_once __DIR__ . '/../Cache/Frontend/PhpFrontend.php';
223 require_once __DIR__ . '/../Cache/Backend/BackendInterface.php';
224 require_once __DIR__ . '/../Cache/Backend/TaggableBackendInterface.php';
225 require_once __DIR__ . '/../Cache/Backend/AbstractBackend.php';
226 require_once __DIR__ . '/../Cache/Backend/PhpCapableBackendInterface.php';
227 require_once __DIR__ . '/../Cache/Backend/SimpleFileBackend.php';
228 require_once __DIR__ . '/../Cache/Backend/NullBackend.php';
229 require_once __DIR__ . '/../Log/LogLevel.php';
230 require_once __DIR__ . '/../Utility/MathUtility.php';
231 require_once __DIR__ . '/ClassLoader.php';
232 if (PHP_VERSION_ID < 50307) {
233 require_once __DIR__ . '/../Compatibility/CompatbilityClassLoaderPhpBelow50307.php';
234 }
235 }
236
237 /**
238 * Compatibility layer for magic quotes
239 *
240 * @return void
241 */
242 static protected function handleMagicQuotesGpc() {
243 if (!get_magic_quotes_gpc()) {
244 \TYPO3\CMS\Core\Utility\GeneralUtility::addSlashesOnArray($_GET);
245 \TYPO3\CMS\Core\Utility\GeneralUtility::addSlashesOnArray($_POST);
246 $GLOBALS['HTTP_GET_VARS'] = $_GET;
247 $GLOBALS['HTTP_POST_VARS'] = $_POST;
248 }
249 }
250
251 /**
252 * Add typo3/contrib/pear/ as first include folder in
253 * include path, because the shipped PEAR packages use
254 * relative paths to include their files.
255 *
256 * This is required for t3lib_http_Request to work.
257 *
258 * Having the TYPO3 folder first will make sure that the
259 * shipped version is loaded before any local PEAR package,
260 * thus avoiding any incompatibilities with newer or older
261 * versions.
262 *
263 * @return void
264 */
265 static protected function addCorePearPathToIncludePath() {
266 set_include_path(PATH_typo3 . 'contrib/pear/' . PATH_SEPARATOR . get_include_path());
267 }
268
269 /**
270 * Set up / initialize several globals variables
271 *
272 * @return void
273 */
274 static protected function initializeGlobalVariables() {
275 // Unset variable(s) in global scope (security issue #13959)
276 unset($GLOBALS['error']);
277 // Set up base information about browser/user-agent
278 $GLOBALS['CLIENT'] = \TYPO3\CMS\Core\Utility\GeneralUtility::clientInfo();
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 static protected function loadDefaultConfiguration() {
290 $GLOBALS['TYPO3_CONF_VARS'] = \TYPO3\CMS\Core\Configuration\ConfigurationManager::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 static protected function initializeGlobalTimeTrackingVariables() {
300 // Set PARSETIME_START to the system time in milliseconds.
301 $GLOBALS['PARSETIME_START'] = \TYPO3\CMS\Core\Utility\GeneralUtility::milliseconds();
302 // Microtime of (nearly) script start
303 $GLOBALS['TYPO3_MISC']['microtime_start'] = microtime(TRUE);
304 // EXEC_TIME is set so that the rest of the script has a common value for the script execution time
305 $GLOBALS['EXEC_TIME'] = time();
306 // $ACCESS_TIME is a common time in minutes for access control
307 $GLOBALS['ACCESS_TIME'] = $GLOBALS['EXEC_TIME'] - $GLOBALS['EXEC_TIME'] % 60;
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 static protected 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 static protected 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 static protected 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 static protected 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 return $scriptPath;
393 }
394
395 /**
396 * Calculate path to entry script if in cli mode.
397 *
398 * First argument of a cli script is the path to the script that was called. If the script does not start
399 * with / (or A:\ for Windows), the path is not absolute yet, and the current working directory is added.
400 *
401 * @return string Absolute path to entry script
402 */
403 static protected function getPathThisScriptCli() {
404 // Possible relative path of the called script
405 if (isset($_SERVER['argv'][0])) {
406 $scriptPath = $_SERVER['argv'][0];
407 } elseif (isset($_ENV['_'])) {
408 $scriptPath = $_ENV['_'];
409 } else {
410 $scriptPath = $_SERVER['_'];
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 // Concatenate path to current working directory with relative path and remove "/./" constructs
424 if ($isRelativePath) {
425 if (isset($_SERVER['PWD'])) {
426 $workingDirectory = $_SERVER['PWD'];
427 } else {
428 $workingDirectory = getcwd();
429 }
430 $scriptPath = $workingDirectory . '/' . preg_replace('/\\.\\//', '', $scriptPath);
431 }
432 return $scriptPath;
433 }
434
435 /**
436 * Calculate the document root part to the instance from PATH_thisScript
437 *
438 * There are two ways to hint correct calculation:
439 * Either an explicit specified sub path or the defined constant TYPO3_MOD_PATH. Which one is
440 * used depends on which entry script was called in the first place.
441 *
442 * We have two main scenarios for entry points:
443 * - Directly called documentRoot/index.php (-> FE call or eiD include): index.php sets $relativePathPart to
444 * empty string to hint this code that the document root is identical to the directory the script is located at.
445 * - An indirect include of typo3/init.php (-> a backend module, the install tool, or scripts like thumbs.php).
446 * If init.php is included we distinguish two cases:
447 * -- A backend module defines 'TYPO3_MOD_PATH': This is the case for "old" modules that are not called through
448 * "mod.php" dispatcher, and in the install tool. The TYPO3_MOD_PATH defines the relative path to the typo3/
449 * directory. This is taken as base to calculate the document root.
450 * -- A script includes init.php and does not define 'TYPO3_MOD_PATH': This is the case for the mod.php dispatcher
451 * and other entry scripts like 'cli_dispatch.phpsh' or 'thumbs.php' that are located parallel to init.php. In
452 * this case init.php sets 'typo3/' as $relativePathPart as base to calculate the document root.
453 *
454 * This basically boils down to the following code:
455 * If TYPO3_MOD_PATH is defined, subtract this 'local' part from the entry point directory, else use
456 * $relativePathPart to subtract this from the the script entry point to find out the document root.
457 *
458 * @param string $relativePathPart Relative directory part from document root to script path if TYPO3_MOD_PATH is not used
459 * @return string Absolute path to document root of installation
460 */
461 static protected function getPathSite($relativePathPart) {
462 // If end of path is not "typo3/" and TYPO3_MOD_PATH is given
463 if (defined('TYPO3_MOD_PATH')) {
464 return self::getPathSiteByTypo3ModulePath();
465 } else {
466 return self::getPathSiteByRelativePathPart($relativePathPart);
467 }
468 }
469
470 /**
471 * Calculate document root by TYPO3_MOD_PATH
472 *
473 * TYPO3_MOD_PATH can have the following values:
474 * - "sysext/extensionName/path/entryScript.php" -> extension is below 'docRoot'/typo3/sysext
475 * - "ext/extensionName/path/entryScript.php" -> extension is below 'docRoot'/typo3/ext
476 * - "../typo3conf/ext/extensionName/path/entryScript.php" -> extension is below 'docRoot'/typo3conf/ext
477 * - "install/index.php" -> install tool in 'docRoot'/typo3/install/
478 *
479 * The method unifies the above and subtracts the calculated path part from PATH_thisScript
480 *
481 * @return string Absolute path to document root of installation
482 */
483 static protected function getPathSiteByTypo3ModulePath() {
484 if (substr(TYPO3_MOD_PATH, 0, strlen('sysext/')) === 'sysext/' || substr(TYPO3_MOD_PATH, 0, strlen('ext/')) === 'ext/' || substr(TYPO3_MOD_PATH, 0, strlen('install/')) === 'install/') {
485 $pathPartRelativeToDocumentRoot = TYPO3_mainDir . TYPO3_MOD_PATH;
486 } elseif (substr(TYPO3_MOD_PATH, 0, strlen('../typo3conf/')) === '../typo3conf/') {
487 $pathPartRelativeToDocumentRoot = substr(TYPO3_MOD_PATH, 3);
488 } else {
489 die('Unable to determine TYPO3 document root.');
490 }
491 $entryScriptDirectory = self::getUnifiedDirectoryNameWithTrailingSlash(PATH_thisScript);
492 return substr($entryScriptDirectory, 0, -strlen($pathPartRelativeToDocumentRoot));
493 }
494
495 /**
496 * Find out document root by subtracting $relativePathPart from PATH_thisScript
497 *
498 * @param string $relativePathPart Relative part of script from document root
499 * @return string Absolute path to document root of installation
500 */
501 static protected function getPathSiteByRelativePathPart($relativePathPart) {
502 $entryScriptDirectory = self::getUnifiedDirectoryNameWithTrailingSlash(PATH_thisScript);
503 if (strlen($relativePathPart) > 0) {
504 $pathSite = substr($entryScriptDirectory, 0, -strlen($relativePathPart));
505 } else {
506 $pathSite = $entryScriptDirectory;
507 }
508 return $pathSite;
509 }
510
511 /**
512 * Remove file name from script path and unify for Windows and Unix
513 *
514 * @param string $absolutePath Absolute path to script
515 * @return string Directory name of script file location, unified for Windows and Unix
516 */
517 static protected function getUnifiedDirectoryNameWithTrailingSlash($absolutePath) {
518 $directory = dirname($absolutePath);
519 if (TYPO3_OS === 'WIN') {
520 $directory = str_replace('\\', '/', $directory);
521 }
522 return $directory . '/';
523 }
524
525 }
526
527
528 ?>