[BUGFIX] Minor adjustments to wordings, variables and docs
[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 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Core\Utility\PathUtility;
19 use TYPO3\CMS\Core\Utility\StringUtility;
20
21 /**
22 * Class to encapsulate base setup of bootstrap.
23 *
24 * This class contains all code that must be executed by every entry script.
25 *
26 * It sets up all basic paths, constants, global variables and checks
27 * the basic environment TYPO3 runs in.
28 *
29 * This class does not use any TYPO3 instance specific configuration, it only
30 * sets up things based on the server environment and core code. Even with a
31 * missing typo3conf/localconf.php this script will be successful.
32 *
33 * The script aborts execution with an error message if
34 * some part fails or conditions are not met.
35 *
36 * This script is internal code and subject to change.
37 * DO NOT use it in own code, or be prepared your code might
38 * break in future versions of the core.
39 */
40 class SystemEnvironmentBuilder
41 {
42 /** @internal */
43 const REQUESTTYPE_FE = 1;
44 /** @internal */
45 const REQUESTTYPE_BE = 2;
46 /** @internal */
47 const REQUESTTYPE_CLI = 4;
48 /** @internal */
49 const REQUESTTYPE_AJAX = 8;
50 /** @internal */
51 const REQUESTTYPE_INSTALL = 16;
52
53 /**
54 * A list of supported CGI server APIs
55 * NOTICE: This is a duplicate of the SAME array in GeneralUtility!
56 * It is duplicated here as this information is needed early in bootstrap
57 * and GeneralUtility is not available yet.
58 * @var array
59 */
60 protected static $supportedCgiServerApis = [
61 'fpm-fcgi',
62 'cgi',
63 'isapi',
64 'cgi-fcgi',
65 'srv', // HHVM with fastcgi
66 ];
67
68 /**
69 * An array of disabled methods
70 *
71 * @var string[]
72 */
73 protected static $disabledFunctions;
74
75 /**
76 * Run base setup.
77 * This entry method is used in all scopes (FE, BE, eid, ajax, ...)
78 *
79 * @internal This method should not be used by 3rd party code. It will change without further notice.
80 * @param int $entryPointLevel Number of subdirectories where the entry script is located under the document root
81 * @param int $requestType
82 */
83 public static function run(int $entryPointLevel = 0, int $requestType = self::REQUESTTYPE_FE)
84 {
85 self::defineBaseConstants();
86 self::defineTypo3RequestTypes();
87 self::setRequestType($requestType | ($requestType === self::REQUESTTYPE_BE && strpos($_REQUEST['route'] ?? '', '/ajax/') === 0 ? TYPO3_REQUESTTYPE_AJAX : 0));
88 self::defineLegacyConstants($requestType === self::REQUESTTYPE_FE ? 'FE' : 'BE');
89 self::definePaths($entryPointLevel, $requestType);
90 self::checkMainPathsExist();
91 self::initializeGlobalVariables();
92 self::initializeGlobalTimeTrackingVariables();
93 self::initializeBasicErrorReporting();
94
95 $applicationContext = static::createApplicationContext();
96 self::initializeEnvironment($applicationContext, $requestType);
97 GeneralUtility::presetApplicationContext($applicationContext);
98 }
99
100 protected static function createApplicationContext(): ApplicationContext
101 {
102 $applicationContext = getenv('TYPO3_CONTEXT') ?: (getenv('REDIRECT_TYPO3_CONTEXT') ?: 'Production');
103
104 return new ApplicationContext($applicationContext);
105 }
106
107 /**
108 * Define all simple constants that have no dependency to local configuration
109 */
110 protected static function defineBaseConstants()
111 {
112 // Check one of the constants and return early if defined already
113 if (defined('TYPO3_version')) {
114 return;
115 }
116
117 // This version, branch and copyright
118 define('TYPO3_version', '9.5.0-dev');
119 define('TYPO3_branch', '9.5');
120 define('TYPO3_copyright_year', '1998-2018');
121
122 // TYPO3 external links
123 define('TYPO3_URL_GENERAL', 'https://typo3.org/');
124 define('TYPO3_URL_LICENSE', 'https://typo3.org/typo3-cms/overview/licenses/');
125 define('TYPO3_URL_EXCEPTION', 'https://typo3.org/go/exception/CMS/');
126 define('TYPO3_URL_DONATE', 'https://typo3.org/community/contribute/donate/');
127 define('TYPO3_URL_WIKI_OPCODECACHE', 'https://wiki.typo3.org/Opcode_Cache');
128
129 // @deprecated since TYPO3 v9.4 and will be removed in TYPO3 v10
130 define('TYPO3_URL_MAILINGLISTS', 'http://lists.typo3.org/cgi-bin/mailman/listinfo');
131 define('TYPO3_URL_DOCUMENTATION', 'https://typo3.org/documentation/');
132 define('TYPO3_URL_DOCUMENTATION_TSREF', 'https://docs.typo3.org/typo3cms/TyposcriptReference/');
133 define('TYPO3_URL_DOCUMENTATION_TSCONFIG', 'https://docs.typo3.org/typo3cms/TSconfigReference/');
134 define('TYPO3_URL_CONSULTANCY', 'https://typo3.org/support/professional-services/');
135 define('TYPO3_URL_CONTRIBUTE', 'https://typo3.org/contribute/');
136 define('TYPO3_URL_SECURITY', 'https://typo3.org/teams/security/');
137 define('TYPO3_URL_DOWNLOAD', 'https://typo3.org/download/');
138 define('TYPO3_URL_SYSTEMREQUIREMENTS', 'https://typo3.org/typo3-cms/overview/requirements/');
139
140 // A linefeed, a carriage return, a CR-LF combination
141 defined('LF') ?: define('LF', chr(10));
142 defined('CR') ?: define('CR', chr(13));
143 defined('CRLF') ?: define('CRLF', CR . LF);
144
145 // @deprecated since TYPO3 v9.4 and will be removed in TYPO3 v10
146 defined('NUL') ?: define('NUL', "\0");
147 defined('TAB') ?: define('TAB', "\t");
148 defined('SUB') ?: define('SUB', chr(26));
149
150 // Security related constant: Default value of fileDenyPattern
151 define('FILE_DENY_PATTERN_DEFAULT', '\\.(php[3-7]?|phpsh|phtml|pht)(\\..*)?$|^\\.htaccess$');
152 // Security related constant: List of file extensions that should be registered as php script file extensions
153 define('PHP_EXTENSIONS_DEFAULT', 'php,php3,php4,php5,php6,php7,phpsh,inc,phtml,pht');
154
155 // Operating system identifier
156 // Either "WIN" or empty string
157 defined('TYPO3_OS') ?: define('TYPO3_OS', self::getTypo3Os());
158
159 // Service error constants
160 // @deprecated since TYPO3 v9.3, will be removed in TYPO3 v10.0, use the class constants in AbstractService instead.
161 // General error - something went wrong
162 define('T3_ERR_SV_GENERAL', -1);
163 // During execution it showed that the service is not available and should be ignored. The service itself should call $this->setNonAvailable()
164 define('T3_ERR_SV_NOT_AVAIL', -2);
165 // Passed subtype is not possible with this service
166 define('T3_ERR_SV_WRONG_SUBTYPE', -3);
167 // Passed subtype is not possible with this service
168 define('T3_ERR_SV_NO_INPUT', -4);
169 // File not found which the service should process
170 define('T3_ERR_SV_FILE_NOT_FOUND', -20);
171 // File not readable
172 define('T3_ERR_SV_FILE_READ', -21);
173 // File not writable
174 define('T3_ERR_SV_FILE_WRITE', -22);
175 // Passed subtype is not possible with this service
176 define('T3_ERR_SV_PROG_NOT_FOUND', -40);
177 // Passed subtype is not possible with this service
178 define('T3_ERR_SV_PROG_FAILED', -41);
179 }
180
181 /**
182 * Calculate all required base paths and set as constants.
183 *
184 * @param int $entryPointLevel Number of subdirectories where the entry script is located under the document root
185 * @param int $requestType
186 */
187 protected static function definePaths(int $entryPointLevel, int $requestType)
188 {
189 $isCli = self::isCliRequestType($requestType);
190 // Absolute path of the entry script that was called
191 $scriptPath = GeneralUtility::fixWindowsFilePath(self::getPathThisScript($isCli));
192 $rootPath = self::getRootPathFromScriptPath($scriptPath, $entryPointLevel);
193 // Check if the root path has been set in the environment (e.g. by the composer installer)
194 if (getenv('TYPO3_PATH_ROOT')) {
195 if ($isCli && self::usesComposerClassLoading() && StringUtility::endsWith($scriptPath, 'typo3')) {
196 // PATH_thisScript is used for various path calculations based on the document root
197 // Therefore we assume it is always a subdirectory of the document root, which is not the case
198 // in composer mode on cli, as the binary is in the composer bin directory.
199 // Because of that, we enforce the document root path of this binary to be set
200 $scriptName = '/typo3/sysext/core/bin/typo3';
201 } else {
202 // Base the script path on the path taken from the environment
203 // to make relative path calculations work in case only one of both is symlinked
204 // or has the real path
205 $scriptName = substr($scriptPath, strlen($rootPath));
206 }
207 $rootPath = GeneralUtility::fixWindowsFilePath(getenv('TYPO3_PATH_ROOT'));
208 $scriptPath = $rootPath . $scriptName;
209 }
210
211 if (!defined('PATH_thisScript')) {
212 // @deprecated since v9, will be removed in v10
213 define('PATH_thisScript', $scriptPath);
214 }
215 // Absolute path of the document root of the instance with trailing slash
216 if (!defined('PATH_site')) {
217 // @deprecated since v9, will be removed in v10
218 define('PATH_site', $rootPath . '/');
219 }
220 // Relative path from document root to typo3/ directory
221 // Hardcoded to "typo3/"
222 if (!defined('TYPO3_mainDir')) {
223 define('TYPO3_mainDir', 'typo3/');
224 }
225 // Absolute path of the typo3 directory of the instance with trailing slash
226 // Example "/var/www/instance-name/htdocs/typo3/"
227 if (!defined('PATH_typo3')) {
228 // @deprecated since v9, will be removed in v10
229 define('PATH_typo3', PATH_site . TYPO3_mainDir);
230 }
231 // Absolute path to the typo3conf directory with trailing slash
232 // Example "/var/www/instance-name/htdocs/typo3conf/"
233 if (!defined('PATH_typo3conf')) {
234 // @deprecated since v9, will be removed in v10
235 define('PATH_typo3conf', PATH_site . 'typo3conf/');
236 }
237 }
238
239 /**
240 * Check if path and script file name calculation was successful, exit if not.
241 */
242 protected static function checkMainPathsExist()
243 {
244 if (!is_file(PATH_thisScript)) {
245 static::exitWithMessage('Unable to determine path to entry script.');
246 }
247 }
248
249 /**
250 * Set up / initialize several globals variables
251 */
252 protected static function initializeGlobalVariables()
253 {
254 // Unset variable(s) in global scope (security issue #13959)
255 $GLOBALS['TYPO3_MISC'] = [];
256 $GLOBALS['T3_VAR'] = [];
257 $GLOBALS['T3_SERVICES'] = [];
258 }
259
260 /**
261 * Initialize global time tracking variables.
262 * These are helpers to for example output script parsetime at the end of a script.
263 */
264 protected static function initializeGlobalTimeTrackingVariables()
265 {
266 // Microtime of (nearly) script start
267 $GLOBALS['TYPO3_MISC']['microtime_start'] = microtime(true);
268 // EXEC_TIME is set so that the rest of the script has a common value for the script execution time
269 $GLOBALS['EXEC_TIME'] = time();
270 // $ACCESS_TIME is a common time in minutes for access control
271 $GLOBALS['ACCESS_TIME'] = $GLOBALS['EXEC_TIME'] - $GLOBALS['EXEC_TIME'] % 60;
272 // $SIM_EXEC_TIME is set to $EXEC_TIME but can be altered later in the script if we want to
273 // simulate another execution-time when selecting from eg. a database
274 $GLOBALS['SIM_EXEC_TIME'] = $GLOBALS['EXEC_TIME'];
275 // If $SIM_EXEC_TIME is changed this value must be set accordingly
276 $GLOBALS['SIM_ACCESS_TIME'] = $GLOBALS['ACCESS_TIME'];
277 }
278
279 /**
280 * Initialize the Environment class
281 *
282 * @param ApplicationContext $context
283 * @param int|null $requestType
284 */
285 public static function initializeEnvironment(ApplicationContext $context, int $requestType = null)
286 {
287 // Absolute path of the entry script that was called
288 $scriptPath = PATH_thisScript;
289 $sitePath = rtrim(PATH_site, '/');
290
291 if (getenv('TYPO3_PATH_ROOT')) {
292 $rootPathFromEnvironment = GeneralUtility::fixWindowsFilePath(getenv('TYPO3_PATH_ROOT'));
293 if ($sitePath !== $rootPathFromEnvironment) {
294 // This means, that we re-initialized the environment during a single request
295 // This currently only happens in custom code or during functional testing
296 // Once the constants are removed, we might be able to remove this code here as well and directly pass an environment to the application
297 $scriptPath = $rootPathFromEnvironment . substr($scriptPath, strlen($sitePath));
298 $sitePath = $rootPathFromEnvironment;
299 }
300 }
301
302 $projectRootPath = GeneralUtility::fixWindowsFilePath(getenv('TYPO3_PATH_APP'));
303 $isDifferentRootPath = ($projectRootPath && $projectRootPath !== $sitePath);
304 Environment::initialize(
305 $context,
306 self::isCliRequestType($requestType),
307 self::usesComposerClassLoading(),
308 $isDifferentRootPath ? $projectRootPath : $sitePath,
309 $sitePath,
310 $isDifferentRootPath ? $projectRootPath . '/var' : $sitePath . '/typo3temp/var',
311 $isDifferentRootPath ? $projectRootPath . '/config' : $sitePath . '/typo3conf',
312 $scriptPath,
313 self::getTypo3Os() === 'WIN' ? 'WINDOWS' : 'UNIX'
314 );
315 }
316
317 /**
318 * Initialize basic error reporting.
319 *
320 * There are a lot of extensions that have no strict / notice / deprecated free
321 * ext_localconf or ext_tables. Since the final error reporting must be set up
322 * after those extension files are read, a default configuration is needed to
323 * suppress error reporting meanwhile during further bootstrap.
324 */
325 protected static function initializeBasicErrorReporting()
326 {
327 // Core should be notice free at least until this point ...
328 error_reporting(E_ALL & ~(E_STRICT | E_NOTICE | E_DEPRECATED));
329 }
330
331 /**
332 * Determine the operating system TYPO3 is running on.
333 *
334 * @return string Either 'WIN' if running on Windows, else empty string
335 */
336 protected static function getTypo3Os()
337 {
338 $typoOs = '';
339 if (!stristr(PHP_OS, 'darwin') && !stristr(PHP_OS, 'cygwin') && stristr(PHP_OS, 'win')) {
340 $typoOs = 'WIN';
341 }
342 return $typoOs;
343 }
344
345 /**
346 * Calculate PATH_thisScript
347 *
348 * First step in path calculation: Goal is to find the absolute path of the entry script
349 * that was called without resolving any links. This is important since the TYPO3 entry
350 * points are often linked to a central core location, so we can not use the php magic
351 * __FILE__ here, but resolve the called script path from given server environments.
352 *
353 * This path is important to calculate the document root (PATH_site). The strategy is to
354 * find out the script name that was called in the first place and to subtract the local
355 * part from it to find the document root.
356 *
357 * @param bool $isCli
358 * @return string Absolute path to entry script
359 */
360 protected static function getPathThisScript(bool $isCli)
361 {
362 if ($isCli) {
363 return self::getPathThisScriptCli();
364 }
365 return self::getPathThisScriptNonCli();
366 }
367
368 /**
369 * Calculate path to entry script if not in cli mode.
370 *
371 * Depending on the environment, the script path is found in different $_SERVER variables.
372 *
373 * @return string Absolute path to entry script
374 */
375 protected static function getPathThisScriptNonCli()
376 {
377 $cgiPath = '';
378 if (isset($_SERVER['ORIG_PATH_TRANSLATED'])) {
379 $cgiPath = $_SERVER['ORIG_PATH_TRANSLATED'];
380 } elseif (isset($_SERVER['PATH_TRANSLATED'])) {
381 $cgiPath = $_SERVER['PATH_TRANSLATED'];
382 }
383 if ($cgiPath && in_array(PHP_SAPI, self::$supportedCgiServerApis, true)) {
384 $scriptPath = $cgiPath;
385 } else {
386 if (isset($_SERVER['ORIG_SCRIPT_FILENAME'])) {
387 $scriptPath = $_SERVER['ORIG_SCRIPT_FILENAME'];
388 } else {
389 $scriptPath = $_SERVER['SCRIPT_FILENAME'];
390 }
391 }
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 protected static function getPathThisScriptCli()
404 {
405 // Possible relative path of the called script
406 if (isset($_SERVER['argv'][0])) {
407 $scriptPath = $_SERVER['argv'][0];
408 } elseif (isset($_ENV['_'])) {
409 $scriptPath = $_ENV['_'];
410 } else {
411 $scriptPath = $_SERVER['_'];
412 }
413 // Find out if path is relative or not
414 $isRelativePath = false;
415 if (self::getTypo3Os() === 'WIN') {
416 if (!preg_match('/^([a-zA-Z]:)?\\\\/', $scriptPath)) {
417 $isRelativePath = true;
418 }
419 } else {
420 if ($scriptPath[0] !== '/') {
421 $isRelativePath = true;
422 }
423 }
424 // Concatenate path to current working directory with relative path and remove "/./" constructs
425 if ($isRelativePath) {
426 if (isset($_SERVER['PWD'])) {
427 $workingDirectory = $_SERVER['PWD'];
428 } else {
429 $workingDirectory = getcwd();
430 }
431 $scriptPath = $workingDirectory . '/' . preg_replace('/\\.\\//', '', $scriptPath);
432 }
433 return $scriptPath;
434 }
435
436 /**
437 * Calculate the document root part to the instance from PATH_thisScript.
438 * This is based on the amount of subdirectories "under" PATH_site where PATH_thisScript is located.
439 *
440 * The following main scenarios for entry points exist by default in the TYPO3 core:
441 * - Directly called documentRoot/index.php (-> FE call or eiD include): index.php is located in the same directory
442 * as the main project. The document root is identical to the directory the script is located at.
443 * - The install tool, located under typo3/install.php.
444 * - A Backend script: This is the case for the typo3/index.php dispatcher and other entry scripts like 'typo3/sysext/core/bin/typo3'
445 * or 'typo3/index.php' that are located inside typo3/ directly.
446 *
447 * @param string $scriptPath Calculated path to the entry script
448 * @param int $entryPointLevel Number of subdirectories where the entry script is located under the document root
449 * @return string Absolute path to document root of installation
450 */
451 protected static function getRootPathFromScriptPath($scriptPath, $entryPointLevel)
452 {
453 $entryScriptDirectory = PathUtility::dirnameDuringBootstrap($scriptPath);
454 if ($entryPointLevel > 0) {
455 list($rootPath) = GeneralUtility::revExplode('/', $entryScriptDirectory, $entryPointLevel + 1);
456 } else {
457 $rootPath = $entryScriptDirectory;
458 }
459 return $rootPath;
460 }
461
462 /**
463 * Send http headers, echo out a text message and exit with error code
464 *
465 * @param string $message
466 */
467 protected static function exitWithMessage($message)
468 {
469 $headers = [
470 \TYPO3\CMS\Core\Utility\HttpUtility::HTTP_STATUS_500,
471 'Content-Type: text/plain'
472 ];
473 if (!headers_sent()) {
474 foreach ($headers as $header) {
475 header($header);
476 }
477 }
478 echo $message . LF;
479 exit(1);
480 }
481
482 /**
483 * Check if the given function is disabled in the system
484 *
485 * @param string $function
486 * @return bool
487 */
488 public static function isFunctionDisabled($function)
489 {
490 if (static::$disabledFunctions === null) {
491 static::$disabledFunctions = GeneralUtility::trimExplode(',', ini_get('disable_functions'));
492 }
493 if (!empty(static::$disabledFunctions)) {
494 return in_array($function, static::$disabledFunctions, true);
495 }
496
497 return false;
498 }
499
500 /**
501 * @return bool
502 */
503 protected static function usesComposerClassLoading(): bool
504 {
505 return defined('TYPO3_COMPOSER_MODE') && TYPO3_COMPOSER_MODE;
506 }
507
508 /**
509 * Define TYPO3_REQUESTTYPE* constants that can be used for developers to see if any context has been hit
510 * also see setRequestType(). Is done at the very beginning so these parameters are always available.
511 */
512 protected static function defineTypo3RequestTypes()
513 {
514 if (defined('TYPO3_REQUESTTYPE_FE')) { // @todo remove once Bootstrap::getInstance() is dropped
515 return;
516 }
517 define('TYPO3_REQUESTTYPE_FE', self::REQUESTTYPE_FE);
518 define('TYPO3_REQUESTTYPE_BE', self::REQUESTTYPE_BE);
519 define('TYPO3_REQUESTTYPE_CLI', self::REQUESTTYPE_CLI);
520 define('TYPO3_REQUESTTYPE_AJAX', self::REQUESTTYPE_AJAX);
521 define('TYPO3_REQUESTTYPE_INSTALL', self::REQUESTTYPE_INSTALL);
522 }
523
524 /**
525 * Defines the TYPO3_REQUESTTYPE constant so the environment knows which context the request is running.
526 *
527 * @param int $requestType
528 */
529 protected static function setRequestType(int $requestType)
530 {
531 if (defined('TYPO3_REQUESTTYPE')) { // @todo remove once Bootstrap::getInstance() is dropped
532 return;
533 }
534 define('TYPO3_REQUESTTYPE', $requestType);
535 }
536
537 /**
538 * Define constants and variables
539 *
540 * @param string
541 */
542 protected static function defineLegacyConstants(string $mode)
543 {
544 if (defined('TYPO3_MODE')) { // @todo remove once Bootstrap::getInstance() is dropped
545 return;
546 }
547 define('TYPO3_MODE', $mode);
548 }
549
550 /**
551 * Checks if request type is cli.
552 * Falls back to check PHP_SAPI in case request type is not provided
553 *
554 * @param int|null $requestType
555 * @return bool
556 */
557 protected static function isCliRequestType(?int $requestType): bool
558 {
559 if ($requestType === null) {
560 $requestType = PHP_SAPI === 'cli' ? self::REQUESTTYPE_CLI : self::REQUESTTYPE_FE;
561 }
562
563 return ($requestType & self::REQUESTTYPE_CLI) === self::REQUESTTYPE_CLI;
564 }
565 }