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