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