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