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