[RELEASE] Release of TYPO3 6.2.11
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Core / SystemEnvironmentBuilder.php
1 <?php
2 namespace TYPO3\CMS\Core\Core;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Class to encapsulate base setup of bootstrap.
19 *
20 * This class contains all code that must be executed by every entry script.
21 *
22 * It sets up all basic paths, constants, global variables and checks
23 * the basic environment TYPO3 runs in.
24 *
25 * This class does not use any TYPO3 instance specific configuration, it only
26 * sets up things based on the server environment and core code. Even with a
27 * missing typo3conf/localconf.php this script will be successful.
28 *
29 * The script aborts execution with an error message if
30 * some part fails or conditions are not met.
31 *
32 * This script is internal code and subject to change.
33 * DO NOT use it in own code, or be prepared your code might
34 * break in future versions of the core.
35 */
36 class SystemEnvironmentBuilder {
37
38 /**
39 * A list of supported CGI server APIs
40 * NOTICE: This is a duplicate of the SAME array in GeneralUtility!
41 * It is duplicated here as this information is needed early in bootstrap
42 * and GeneralUtility is not available yet.
43 * @var array
44 */
45 static protected $supportedCgiServerApis = array(
46 'fpm-fcgi',
47 'cgi',
48 'isapi',
49 'cgi-fcgi',
50 'srv', // HHVM with fastcgi
51 );
52
53 /**
54 * Run base setup.
55 * This entry method is used in all scopes (FE, BE, eid, ajax, ...)
56 *
57 * @internal This method should not be used by 3rd party code. It will change without further notice.
58 * @param string $relativePathPart Relative path of the entry script back to document root
59 * @return void
60 */
61 static public function run($relativePathPart = '') {
62 self::defineBaseConstants();
63 self::definePaths($relativePathPart);
64 self::checkMainPathsExist();
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.2.11');
80 define('TYPO3_branch', '6.2');
81 define('TYPO3_copyright_year', '1998-2015');
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/typo3-cms/overview/requirements/');
97 define('TYPO3_URL_DONATE', 'http://typo3.org/donate/online-donation/');
98 define('TYPO3_URL_WIKI_OPCODECACHE', 'http://wiki.typo3.org/Opcode_Cache');
99
100 // A tabulator, a linefeed, a carriage return, a CR-LF combination
101 define('TAB', chr(9));
102 define('LF', chr(10));
103 define('CR', chr(13));
104 define('CRLF', CR . LF);
105
106 // Security related constant: Default value of fileDenyPattern
107 define('FILE_DENY_PATTERN_DEFAULT', '\\.(php[3-6]?|phpsh|phtml)(\\..*)?$|^\\.htaccess$');
108 // Security related constant: List of file extensions that should be registered as php script file extensions
109 define('PHP_EXTENSIONS_DEFAULT', 'php,php3,php4,php5,php6,phpsh,inc,phtml');
110
111 // Operating system identifier
112 // Either "WIN" or empty string
113 define('TYPO3_OS', self::getTypo3Os());
114
115 // Service error constants
116 // General error - something went wrong
117 define('T3_ERR_SV_GENERAL', -1);
118 // During execution it showed that the service is not available and should be ignored. The service itself should call $this->setNonAvailable()
119 define('T3_ERR_SV_NOT_AVAIL', -2);
120 // Passed subtype is not possible with this service
121 define('T3_ERR_SV_WRONG_SUBTYPE', -3);
122 // Passed subtype is not possible with this service
123 define('T3_ERR_SV_NO_INPUT', -4);
124 // File not found which the service should process
125 define('T3_ERR_SV_FILE_NOT_FOUND', -20);
126 // File not readable
127 define('T3_ERR_SV_FILE_READ', -21);
128 // File not writable
129 define('T3_ERR_SV_FILE_WRITE', -22);
130 // Passed subtype is not possible with this service
131 define('T3_ERR_SV_PROG_NOT_FOUND', -40);
132 // Passed subtype is not possible with this service
133 define('T3_ERR_SV_PROG_FAILED', -41);
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 static protected function definePaths($relativePathPart = '') {
143 // Relative path from document root to typo3/ directory
144 // Hardcoded to "typo3/"
145 define('TYPO3_mainDir', 'typo3/');
146 // Absolute path of the entry script that was called
147 // All paths are unified between Windows and Unix, so the \ of Windows is substituted to a /
148 // Example "/var/www/instance-name/htdocs/typo3conf/ext/wec_map/mod1/index.php"
149 // Example "c:/var/www/instance-name/htdocs/typo3/backend.php" for a path in Windows
150 if (!defined('PATH_thisScript')) {
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 if (!defined('PATH_site')) {
156 define('PATH_site', self::getPathSite($relativePathPart));
157 }
158 // Absolute path of the typo3 directory of the instance with trailing slash
159 // Example "/var/www/instance-name/htdocs/typo3/"
160 define('PATH_typo3', PATH_site . TYPO3_mainDir);
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 // Absolute path to the typo3conf directory with trailing slash
167 // Example "/var/www/instance-name/htdocs/typo3conf/"
168 define('PATH_typo3conf', PATH_site . 'typo3conf/');
169 // Absolute path to the tslib directory with trailing slash
170 // Example "/var/www/instance-name/htdocs/typo3/sysext/cms/tslib/"
171 define('PATH_tslib', PATH_typo3 . 'sysext/cms/tslib/');
172 }
173
174 /**
175 * Check if path and script file name calculation was successful, exit if not.
176 *
177 * @return void
178 */
179 static protected function checkMainPathsExist() {
180 if (!is_file(PATH_thisScript)) {
181 static::dieWithMessage('Unable to determine path to entry script.');
182 }
183 if (!is_dir(PATH_tslib)) {
184 static::dieWithMessage('Calculated absolute path to tslib directory does not exist.' . LF . LF
185 . 'Something in the main file, folder and link structure is wrong and must be fixed! A typical document root contains a couple of symbolic links:' . LF
186 . '* A symlink "typo3_src" pointing to the TYPO3 CMS core.' . LF
187 . '* A symlink "typo3" - the backend entry point - pointing to "typo3_src/typo3"' . LF
188 . '* A symlink "index.php" - the frontend entry point - points to "typo3_src/index.php"');
189 }
190 }
191
192 /**
193 * Compatibility layer for magic quotes
194 *
195 * @return void
196 */
197 static protected function handleMagicQuotesGpc() {
198 if (!get_magic_quotes_gpc()) {
199 \TYPO3\CMS\Core\Utility\GeneralUtility::addSlashesOnArray($_GET);
200 \TYPO3\CMS\Core\Utility\GeneralUtility::addSlashesOnArray($_POST);
201 $GLOBALS['HTTP_GET_VARS'] = $_GET;
202 $GLOBALS['HTTP_POST_VARS'] = $_POST;
203 }
204 }
205
206 /**
207 * Add typo3/contrib/pear/ as first include folder in
208 * include path, because the shipped PEAR packages use
209 * relative paths to include their files.
210 *
211 * This is required for \TYPO3\CMS\Core\Http\HttpRequest
212 * to work.
213 *
214 * Having the TYPO3 folder first will make sure that the
215 * shipped version is loaded before any local PEAR package,
216 * thus avoiding any incompatibilities with newer or older
217 * versions.
218 *
219 * @return void
220 */
221 static protected function addCorePearPathToIncludePath() {
222 set_include_path(PATH_typo3 . 'contrib/pear/' . PATH_SEPARATOR . get_include_path());
223 }
224
225 /**
226 * Set up / initialize several globals variables
227 *
228 * @return void
229 */
230 static protected function initializeGlobalVariables() {
231 // Unset variable(s) in global scope (security issue #13959)
232 unset($GLOBALS['error']);
233 // Set up base information about browser/user-agent
234 $GLOBALS['CLIENT'] = \TYPO3\CMS\Core\Utility\GeneralUtility::clientInfo();
235 $GLOBALS['TYPO3_MISC'] = array();
236 $GLOBALS['T3_VAR'] = array();
237 $GLOBALS['T3_SERVICES'] = array();
238 }
239
240 /**
241 * Initialize global time tracking variables.
242 * These are helpers to for example output script parsetime at the end of a script.
243 *
244 * @return void
245 */
246 static protected function initializeGlobalTimeTrackingVariables() {
247 // Set PARSETIME_START to the system time in milliseconds.
248 $GLOBALS['PARSETIME_START'] = \TYPO3\CMS\Core\Utility\GeneralUtility::milliseconds();
249 // Microtime of (nearly) script start
250 $GLOBALS['TYPO3_MISC']['microtime_start'] = microtime(TRUE);
251 // EXEC_TIME is set so that the rest of the script has a common value for the script execution time
252 $GLOBALS['EXEC_TIME'] = time();
253 // $ACCESS_TIME is a common time in minutes for access control
254 $GLOBALS['ACCESS_TIME'] = $GLOBALS['EXEC_TIME'] - $GLOBALS['EXEC_TIME'] % 60;
255 // $SIM_EXEC_TIME is set to $EXEC_TIME but can be altered later in the script if we want to
256 // simulate another execution-time when selecting from eg. a database
257 $GLOBALS['SIM_EXEC_TIME'] = $GLOBALS['EXEC_TIME'];
258 // If $SIM_EXEC_TIME is changed this value must be set accordingly
259 $GLOBALS['SIM_ACCESS_TIME'] = $GLOBALS['ACCESS_TIME'];
260 }
261
262 /**
263 * Initialize basic error reporting.
264 *
265 * There are a lot of extensions that have no strict / notice / deprecated free
266 * ext_localconf or ext_tables. Since the final error reporting must be set up
267 * after those extension files are read, a default configuration is needed to
268 * suppress error reporting meanwhile during further bootstrap.
269 *
270 * @return void
271 */
272 static protected function initializeBasicErrorReporting() {
273 // Core should be notice free at least until this point ...
274 error_reporting(E_ALL & ~(E_STRICT | E_NOTICE | E_DEPRECATED));
275 }
276
277 /**
278 * Determine the operating system TYPO3 is running on.
279 *
280 * @return string Either 'WIN' if running on Windows, else empty string
281 */
282 static protected function getTypo3Os() {
283 $typoOs = '';
284 if (!stristr(PHP_OS, 'darwin') && stristr(PHP_OS, 'win')) {
285 $typoOs = 'WIN';
286 }
287 return $typoOs;
288 }
289
290 /**
291 * Calculate PATH_thisScript
292 *
293 * First step in path calculation: Goal is to find the absolute path of the entry script
294 * that was called without resolving any links. This is important since the TYPO3 entry
295 * points are often linked to a central core location, so we can not use the php magic
296 * __FILE__ here, but resolve the called script path from given server environments.
297 *
298 * This path is important to calculate the document root (PATH_site). The strategy is to
299 * find out the script name that was called in the first place and to subtract the local
300 * part from it to find the document root.
301 *
302 * @return string Absolute path to entry script
303 */
304 static protected function getPathThisScript() {
305 if (defined('TYPO3_cliMode') && TYPO3_cliMode === TRUE) {
306 return self::getPathThisScriptCli();
307 } else {
308 return self::getPathThisScriptNonCli();
309 }
310 }
311
312 /**
313 * Calculate path to entry script if not in cli mode.
314 *
315 * Depending on the environment, the script path is found in different $_SERVER variables.
316 *
317 * @return string Absolute path to entry script
318 */
319 static protected function getPathThisScriptNonCli() {
320 $cgiPath = '';
321 if (isset($_SERVER['ORIG_PATH_TRANSLATED'])) {
322 $cgiPath = $_SERVER['ORIG_PATH_TRANSLATED'];
323 } elseif (isset($_SERVER['PATH_TRANSLATED'])) {
324 $cgiPath = $_SERVER['PATH_TRANSLATED'];
325 }
326 if ($cgiPath && in_array(PHP_SAPI, self::$supportedCgiServerApis, TRUE)) {
327 $scriptPath = $cgiPath;
328 } else {
329 if (isset($_SERVER['ORIG_SCRIPT_FILENAME'])) {
330 $scriptPath = $_SERVER['ORIG_SCRIPT_FILENAME'];
331 } else {
332 $scriptPath = $_SERVER['SCRIPT_FILENAME'];
333 }
334 }
335 // Replace \ to / for Windows
336 $scriptPath = str_replace('\\', '/', $scriptPath);
337 // Replace double // to /
338 $scriptPath = str_replace('//', '/', $scriptPath);
339 return $scriptPath;
340 }
341
342 /**
343 * Calculate path to entry script if in cli mode.
344 *
345 * First argument of a cli script is the path to the script that was called. If the script does not start
346 * with / (or A:\ for Windows), the path is not absolute yet, and the current working directory is added.
347 *
348 * @return string Absolute path to entry script
349 */
350 static protected function getPathThisScriptCli() {
351 // Possible relative path of the called script
352 if (isset($_SERVER['argv'][0])) {
353 $scriptPath = $_SERVER['argv'][0];
354 } elseif (isset($_ENV['_'])) {
355 $scriptPath = $_ENV['_'];
356 } else {
357 $scriptPath = $_SERVER['_'];
358 }
359 // Find out if path is relative or not
360 $isRelativePath = FALSE;
361 if (TYPO3_OS === 'WIN') {
362 if (!preg_match('/^([a-zA-Z]:)?\\\\/', $scriptPath)) {
363 $isRelativePath = TRUE;
364 }
365 } else {
366 if ($scriptPath[0] !== '/') {
367 $isRelativePath = TRUE;
368 }
369 }
370 // Concatenate path to current working directory with relative path and remove "/./" constructs
371 if ($isRelativePath) {
372 if (isset($_SERVER['PWD'])) {
373 $workingDirectory = $_SERVER['PWD'];
374 } else {
375 $workingDirectory = getcwd();
376 }
377 $scriptPath = $workingDirectory . '/' . preg_replace('/\\.\\//', '', $scriptPath);
378 }
379 return $scriptPath;
380 }
381
382 /**
383 * Calculate the document root part to the instance from PATH_thisScript
384 *
385 * There are two ways to hint correct calculation:
386 * Either an explicit specified sub path or the defined constant TYPO3_MOD_PATH. Which one is
387 * used depends on which entry script was called in the first place.
388 *
389 * We have two main scenarios for entry points:
390 * - Directly called documentRoot/index.php (-> FE call or eiD include): index.php sets $relativePathPart to
391 * empty string to hint this code that the document root is identical to the directory the script is located at.
392 * - An indirect include of typo3/init.php (-> a backend module, the install tool, or scripts like thumbs.php).
393 * If init.php is included we distinguish two cases:
394 * -- A backend module defines 'TYPO3_MOD_PATH': This is the case for "old" modules that are not called through
395 * "mod.php" dispatcher, and in the install tool. The TYPO3_MOD_PATH defines the relative path to the typo3/
396 * directory. This is taken as base to calculate the document root.
397 * -- A script includes init.php and does not define 'TYPO3_MOD_PATH': This is the case for the mod.php dispatcher
398 * and other entry scripts like 'cli_dispatch.phpsh' or 'thumbs.php' that are located parallel to init.php. In
399 * this case init.php sets 'typo3/' as $relativePathPart as base to calculate the document root.
400 *
401 * This basically boils down to the following code:
402 * If TYPO3_MOD_PATH is defined, subtract this 'local' part from the entry point directory, else use
403 * $relativePathPart to subtract this from the the script entry point to find out the document root.
404 *
405 * @param string $relativePathPart Relative directory part from document root to script path if TYPO3_MOD_PATH is not used
406 * @return string Absolute path to document root of installation
407 */
408 static protected function getPathSite($relativePathPart) {
409 // If end of path is not "typo3/" and TYPO3_MOD_PATH is given
410 if (defined('TYPO3_MOD_PATH')) {
411 return self::getPathSiteByTypo3ModulePath();
412 } else {
413 return self::getPathSiteByRelativePathPart($relativePathPart);
414 }
415 }
416
417 /**
418 * Calculate document root by TYPO3_MOD_PATH
419 *
420 * TYPO3_MOD_PATH can have the following values:
421 * - "sysext/extensionName/path/entryScript.php" -> extension is below 'docRoot'/typo3/sysext
422 * - "ext/extensionName/path/entryScript.php" -> extension is below 'docRoot'/typo3/ext
423 * - "../typo3conf/ext/extensionName/path/entryScript.php" -> extension is below 'docRoot'/typo3conf/ext
424 * - "install/index.php" -> install tool in 'docRoot'/typo3/install/
425 *
426 * The method unifies the above and subtracts the calculated path part from PATH_thisScript
427 *
428 * @return string Absolute path to document root of installation
429 */
430 static protected function getPathSiteByTypo3ModulePath() {
431 if (substr(TYPO3_MOD_PATH, 0, 7) === 'sysext/' || substr(TYPO3_MOD_PATH, 0, 4) === 'ext/' || substr(TYPO3_MOD_PATH, 0, 8) === 'install/') {
432 $pathPartRelativeToDocumentRoot = TYPO3_mainDir . TYPO3_MOD_PATH;
433 } elseif (substr(TYPO3_MOD_PATH, 0, strlen('../typo3conf/')) === '../typo3conf/') {
434 $pathPartRelativeToDocumentRoot = substr(TYPO3_MOD_PATH, 3);
435 } else {
436 static::dieWithMessage('Unable to determine TYPO3 document root.');
437 }
438 $entryScriptDirectory = self::getUnifiedDirectoryNameWithTrailingSlash(PATH_thisScript);
439 return substr($entryScriptDirectory, 0, -strlen($pathPartRelativeToDocumentRoot));
440 }
441
442 /**
443 * Find out document root by subtracting $relativePathPart from PATH_thisScript
444 *
445 * @param string $relativePathPart Relative part of script from document root
446 * @return string Absolute path to document root of installation
447 */
448 static protected function getPathSiteByRelativePathPart($relativePathPart) {
449 $entryScriptDirectory = self::getUnifiedDirectoryNameWithTrailingSlash(PATH_thisScript);
450 if (strlen($relativePathPart) > 0) {
451 $pathSite = substr($entryScriptDirectory, 0, -strlen($relativePathPart));
452 } else {
453 $pathSite = $entryScriptDirectory;
454 }
455 return $pathSite;
456 }
457
458 /**
459 * Remove file name from script path and unify for Windows and Unix
460 *
461 * @param string $absolutePath Absolute path to script
462 * @return string Directory name of script file location, unified for Windows and Unix
463 */
464 static protected function getUnifiedDirectoryNameWithTrailingSlash($absolutePath) {
465 $directory = dirname($absolutePath);
466 if (TYPO3_OS === 'WIN') {
467 $directory = str_replace('\\', '/', $directory);
468 }
469 return $directory . '/';
470 }
471
472 /**
473 * Echo out a text message and die
474 *
475 * @param string $message
476 */
477 static protected function dieWithMessage($message) {
478 header('Content-type: text/plain');
479 die($message);
480 }
481 }