[BUGFIX] Fix resource loading in composer mode
[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\StringUtility;
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 = null;
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 self::definePaths($entryPointLevel);
89 self::checkMainPathsExist();
90 self::initializeGlobalVariables();
91 self::initializeGlobalTimeTrackingVariables();
92 self::initializeBasicErrorReporting();
93 }
94
95 /**
96 * Define all simple constants that have no dependency to local configuration
97 */
98 protected static function defineBaseConstants()
99 {
100 // This version, branch and copyright
101 define('TYPO3_version', '9.2.0-dev');
102 define('TYPO3_branch', '9.2');
103 define('TYPO3_copyright_year', '1998-2018');
104
105 // TYPO3 external links
106 define('TYPO3_URL_GENERAL', 'https://typo3.org/');
107 define('TYPO3_URL_LICENSE', 'https://typo3.org/typo3-cms/overview/licenses/');
108 define('TYPO3_URL_EXCEPTION', 'https://typo3.org/go/exception/CMS/');
109 define('TYPO3_URL_MAILINGLISTS', 'http://lists.typo3.org/cgi-bin/mailman/listinfo');
110 define('TYPO3_URL_DOCUMENTATION', 'https://typo3.org/documentation/');
111 define('TYPO3_URL_DOCUMENTATION_TSREF', 'https://docs.typo3.org/typo3cms/TyposcriptReference/');
112 define('TYPO3_URL_DOCUMENTATION_TSCONFIG', 'https://docs.typo3.org/typo3cms/TSconfigReference/');
113 define('TYPO3_URL_CONSULTANCY', 'https://typo3.org/support/professional-services/');
114 define('TYPO3_URL_CONTRIBUTE', 'https://typo3.org/contribute/');
115 define('TYPO3_URL_SECURITY', 'https://typo3.org/teams/security/');
116 define('TYPO3_URL_DOWNLOAD', 'https://typo3.org/download/');
117 define('TYPO3_URL_SYSTEMREQUIREMENTS', 'https://typo3.org/typo3-cms/overview/requirements/');
118 define('TYPO3_URL_DONATE', 'https://typo3.org/donate/online-donation/');
119 define('TYPO3_URL_WIKI_OPCODECACHE', 'https://wiki.typo3.org/Opcode_Cache');
120
121 // A null, a tabulator, a linefeed, a carriage return, a substitution, a CR-LF combination
122 defined('NUL') ?: define('NUL', chr(0));
123 defined('TAB') ?: define('TAB', chr(9));
124 defined('LF') ?: define('LF', chr(10));
125 defined('CR') ?: define('CR', chr(13));
126 defined('SUB') ?: define('SUB', chr(26));
127 defined('CRLF') ?: define('CRLF', CR . LF);
128
129 // Security related constant: Default value of fileDenyPattern
130 define('FILE_DENY_PATTERN_DEFAULT', '\\.(php[3-7]?|phpsh|phtml|pht)(\\..*)?$|^\\.htaccess$');
131 // Security related constant: List of file extensions that should be registered as php script file extensions
132 define('PHP_EXTENSIONS_DEFAULT', 'php,php3,php4,php5,php6,php7,phpsh,inc,phtml,pht');
133
134 // Operating system identifier
135 // Either "WIN" or empty string
136 defined('TYPO3_OS') ?: define('TYPO3_OS', self::getTypo3Os());
137
138 // Service error constants
139 // General error - something went wrong
140 define('T3_ERR_SV_GENERAL', -1);
141 // During execution it showed that the service is not available and should be ignored. The service itself should call $this->setNonAvailable()
142 define('T3_ERR_SV_NOT_AVAIL', -2);
143 // Passed subtype is not possible with this service
144 define('T3_ERR_SV_WRONG_SUBTYPE', -3);
145 // Passed subtype is not possible with this service
146 define('T3_ERR_SV_NO_INPUT', -4);
147 // File not found which the service should process
148 define('T3_ERR_SV_FILE_NOT_FOUND', -20);
149 // File not readable
150 define('T3_ERR_SV_FILE_READ', -21);
151 // File not writable
152 define('T3_ERR_SV_FILE_WRITE', -22);
153 // Passed subtype is not possible with this service
154 define('T3_ERR_SV_PROG_NOT_FOUND', -40);
155 // Passed subtype is not possible with this service
156 define('T3_ERR_SV_PROG_FAILED', -41);
157 }
158
159 /**
160 * Calculate all required base paths and set as constants.
161 *
162 * @param int $entryPointLevel Number of subdirectories where the entry script is located under the document root
163 */
164 protected static function definePaths($entryPointLevel = 0)
165 {
166 $isCli = PHP_SAPI === 'cli';
167 // Absolute path of the entry script that was called
168 $scriptPath = GeneralUtility::fixWindowsFilePath(self::getPathThisScript($isCli));
169 $rootPath = self::getRootPathFromScriptPath($scriptPath, $entryPointLevel);
170 // Check if the root path has been set in the environment (e.g. by the composer installer)
171 if (getenv('TYPO3_PATH_ROOT')) {
172 if ($isCli && self::usesComposerClassLoading() && StringUtility::endsWith($scriptPath, 'typo3')) {
173 // PATH_thisScript is used for various path calculations based on the document root
174 // Therefore we assume it is always a subdirectory of the document root, which is not the case
175 // in composer mode on cli, as the binary is in the composer bin directory.
176 // Because of that, we enforce the document root path of this binary to be set
177 $scriptName = '/typo3/sysext/core/bin/typo3';
178 } else {
179 // Base the script path on the path taken from the environment
180 // to make relative path calculations work in case only one of both is symlinked
181 // or has the real path
182 $scriptName = substr($scriptPath, strlen($rootPath));
183 }
184 $rootPath = GeneralUtility::fixWindowsFilePath(getenv('TYPO3_PATH_ROOT'));
185 $scriptPath = $rootPath . $scriptName;
186 }
187
188 if (!defined('PATH_thisScript')) {
189 define('PATH_thisScript', $scriptPath);
190 }
191 // Absolute path of the document root of the instance with trailing slash
192 if (!defined('PATH_site')) {
193 define('PATH_site', $rootPath . '/');
194 }
195 // Relative path from document root to typo3/ directory
196 // Hardcoded to "typo3/"
197 define('TYPO3_mainDir', 'typo3/');
198 // Absolute path of the typo3 directory of the instance with trailing slash
199 // Example "/var/www/instance-name/htdocs/typo3/"
200 define('PATH_typo3', PATH_site . TYPO3_mainDir);
201 // Absolute path to the typo3conf directory with trailing slash
202 // Example "/var/www/instance-name/htdocs/typo3conf/"
203 define('PATH_typo3conf', PATH_site . 'typo3conf/');
204 }
205
206 /**
207 * Check if path and script file name calculation was successful, exit if not.
208 */
209 protected static function checkMainPathsExist()
210 {
211 if (!is_file(PATH_thisScript)) {
212 static::exitWithMessage('Unable to determine path to entry script.');
213 }
214 }
215
216 /**
217 * Set up / initialize several globals variables
218 */
219 protected static function initializeGlobalVariables()
220 {
221 // Unset variable(s) in global scope (security issue #13959)
222 $GLOBALS['TYPO3_MISC'] = [];
223 $GLOBALS['T3_VAR'] = [];
224 $GLOBALS['T3_SERVICES'] = [];
225 }
226
227 /**
228 * Initialize global time tracking variables.
229 * These are helpers to for example output script parsetime at the end of a script.
230 */
231 protected static function initializeGlobalTimeTrackingVariables()
232 {
233 // Microtime of (nearly) script start
234 $GLOBALS['TYPO3_MISC']['microtime_start'] = microtime(true);
235 // EXEC_TIME is set so that the rest of the script has a common value for the script execution time
236 $GLOBALS['EXEC_TIME'] = time();
237 // $ACCESS_TIME is a common time in minutes for access control
238 $GLOBALS['ACCESS_TIME'] = $GLOBALS['EXEC_TIME'] - $GLOBALS['EXEC_TIME'] % 60;
239 // $SIM_EXEC_TIME is set to $EXEC_TIME but can be altered later in the script if we want to
240 // simulate another execution-time when selecting from eg. a database
241 $GLOBALS['SIM_EXEC_TIME'] = $GLOBALS['EXEC_TIME'];
242 // If $SIM_EXEC_TIME is changed this value must be set accordingly
243 $GLOBALS['SIM_ACCESS_TIME'] = $GLOBALS['ACCESS_TIME'];
244 }
245
246 /**
247 * Initialize the Environment class
248 *
249 * @param ApplicationContext $context
250 */
251 public static function initializeEnvironment(ApplicationContext $context)
252 {
253 $sitePath = rtrim(PATH_site, '/');
254 $projectRootPath = GeneralUtility::fixWindowsFilePath(getenv('TYPO3_PATH_APP'));
255 $isDifferentRootPath = ($projectRootPath && $projectRootPath !== $sitePath);
256 Environment::initialize(
257 $context,
258 PHP_SAPI === 'cli',
259 self::usesComposerClassLoading(),
260 $isDifferentRootPath ? $projectRootPath : $sitePath,
261 $sitePath,
262 $isDifferentRootPath ? $projectRootPath . '/var' : $sitePath . '/typo3temp/var',
263 $isDifferentRootPath ? $projectRootPath . '/config' : $sitePath . '/typo3conf',
264 PATH_thisScript,
265 self::getTypo3Os() === 'WIN' ? 'WINDOWS' : 'UNIX'
266 );
267 }
268
269 /**
270 * Initialize basic error reporting.
271 *
272 * There are a lot of extensions that have no strict / notice / deprecated free
273 * ext_localconf or ext_tables. Since the final error reporting must be set up
274 * after those extension files are read, a default configuration is needed to
275 * suppress error reporting meanwhile during further bootstrap.
276 */
277 protected static function initializeBasicErrorReporting()
278 {
279 // Core should be notice free at least until this point ...
280 error_reporting(E_ALL & ~(E_STRICT | E_NOTICE | E_DEPRECATED));
281 }
282
283 /**
284 * Determine the operating system TYPO3 is running on.
285 *
286 * @return string Either 'WIN' if running on Windows, else empty string
287 */
288 protected static function getTypo3Os()
289 {
290 $typoOs = '';
291 if (!stristr(PHP_OS, 'darwin') && !stristr(PHP_OS, 'cygwin') && stristr(PHP_OS, 'win')) {
292 $typoOs = 'WIN';
293 }
294 return $typoOs;
295 }
296
297 /**
298 * Calculate PATH_thisScript
299 *
300 * First step in path calculation: Goal is to find the absolute path of the entry script
301 * that was called without resolving any links. This is important since the TYPO3 entry
302 * points are often linked to a central core location, so we can not use the php magic
303 * __FILE__ here, but resolve the called script path from given server environments.
304 *
305 * This path is important to calculate the document root (PATH_site). The strategy is to
306 * find out the script name that was called in the first place and to subtract the local
307 * part from it to find the document root.
308 *
309 * @param bool $isCli
310 * @return string Absolute path to entry script
311 */
312 protected static function getPathThisScript(bool $isCli)
313 {
314 if ($isCli) {
315 return self::getPathThisScriptCli();
316 }
317 return self::getPathThisScriptNonCli();
318 }
319
320 /**
321 * Calculate path to entry script if not in cli mode.
322 *
323 * Depending on the environment, the script path is found in different $_SERVER variables.
324 *
325 * @return string Absolute path to entry script
326 */
327 protected static function getPathThisScriptNonCli()
328 {
329 $cgiPath = '';
330 if (isset($_SERVER['ORIG_PATH_TRANSLATED'])) {
331 $cgiPath = $_SERVER['ORIG_PATH_TRANSLATED'];
332 } elseif (isset($_SERVER['PATH_TRANSLATED'])) {
333 $cgiPath = $_SERVER['PATH_TRANSLATED'];
334 }
335 if ($cgiPath && in_array(PHP_SAPI, self::$supportedCgiServerApis, true)) {
336 $scriptPath = $cgiPath;
337 } else {
338 if (isset($_SERVER['ORIG_SCRIPT_FILENAME'])) {
339 $scriptPath = $_SERVER['ORIG_SCRIPT_FILENAME'];
340 } else {
341 $scriptPath = $_SERVER['SCRIPT_FILENAME'];
342 }
343 }
344 return $scriptPath;
345 }
346
347 /**
348 * Calculate path to entry script if in cli mode.
349 *
350 * First argument of a cli script is the path to the script that was called. If the script does not start
351 * with / (or A:\ for Windows), the path is not absolute yet, and the current working directory is added.
352 *
353 * @return string Absolute path to entry script
354 */
355 protected static function getPathThisScriptCli()
356 {
357 // Possible relative path of the called script
358 if (isset($_SERVER['argv'][0])) {
359 $scriptPath = $_SERVER['argv'][0];
360 } elseif (isset($_ENV['_'])) {
361 $scriptPath = $_ENV['_'];
362 } else {
363 $scriptPath = $_SERVER['_'];
364 }
365 // Find out if path is relative or not
366 $isRelativePath = false;
367 if (TYPO3_OS === 'WIN') {
368 if (!preg_match('/^([a-zA-Z]:)?\\\\/', $scriptPath)) {
369 $isRelativePath = true;
370 }
371 } else {
372 if ($scriptPath[0] !== '/') {
373 $isRelativePath = true;
374 }
375 }
376 // Concatenate path to current working directory with relative path and remove "/./" constructs
377 if ($isRelativePath) {
378 if (isset($_SERVER['PWD'])) {
379 $workingDirectory = $_SERVER['PWD'];
380 } else {
381 $workingDirectory = getcwd();
382 }
383 $scriptPath = $workingDirectory . '/' . preg_replace('/\\.\\//', '', $scriptPath);
384 }
385 return $scriptPath;
386 }
387
388 /**
389 * Calculate the document root part to the instance from PATH_thisScript.
390 * This is based on the amount of subdirectories "under" PATH_site where PATH_thisScript is located.
391 *
392 * The following main scenarios for entry points exist by default in the TYPO3 core:
393 * - Directly called documentRoot/index.php (-> FE call or eiD include): index.php is located in the same directory
394 * as the main project. The document root is identical to the directory the script is located at.
395 * - The install tool, located under typo3/install.php.
396 * - A Backend script: This is the case for the typo3/index.php dispatcher and other entry scripts like 'typo3/sysext/core/bin/typo3'
397 * or 'typo3/index.php' that are located inside typo3/ directly.
398 *
399 * @param string $scriptPath Calculated path to the entry script
400 * @param int $entryPointLevel Number of subdirectories where the entry script is located under the document root
401 * @return string Absolute path to document root of installation
402 */
403 protected static function getRootPathFromScriptPath($scriptPath, $entryPointLevel)
404 {
405 $entryScriptDirectory = dirname($scriptPath);
406 if ($entryPointLevel > 0) {
407 list($rootPath) = GeneralUtility::revExplode('/', $entryScriptDirectory, $entryPointLevel + 1);
408 } else {
409 $rootPath = $entryScriptDirectory;
410 }
411 return $rootPath;
412 }
413
414 /**
415 * Send http headers, echo out a text message and exit with error code
416 *
417 * @param string $message
418 */
419 protected static function exitWithMessage($message)
420 {
421 $headers = [
422 \TYPO3\CMS\Core\Utility\HttpUtility::HTTP_STATUS_500,
423 'Content-Type: text/plain'
424 ];
425 if (!headers_sent()) {
426 foreach ($headers as $header) {
427 header($header);
428 }
429 }
430 echo $message . LF;
431 exit(1);
432 }
433
434 /**
435 * Check if the given function is disabled in the system
436 *
437 * @param string $function
438 * @return bool
439 */
440 public static function isFunctionDisabled($function)
441 {
442 if (static::$disabledFunctions === null) {
443 static::$disabledFunctions = GeneralUtility::trimExplode(',', ini_get('disable_functions'));
444 }
445 if (!empty(static::$disabledFunctions)) {
446 return in_array($function, static::$disabledFunctions, true);
447 }
448
449 return false;
450 }
451
452 /**
453 * @return bool
454 */
455 protected static function usesComposerClassLoading(): bool
456 {
457 return defined('TYPO3_COMPOSER_MODE') && TYPO3_COMPOSER_MODE;
458 }
459
460 /**
461 * Define TYPO3_REQUESTTYPE* constants that can be used for developers to see if any context has been hit
462 * also see setRequestType(). Is done at the very beginning so these parameters are always available.
463 */
464 protected static function defineTypo3RequestTypes()
465 {
466 if (defined('TYPO3_REQUESTTYPE_FE')) { // @todo remove once Bootstrap::getInstance() is dropped
467 return;
468 }
469 define('TYPO3_REQUESTTYPE_FE', self::REQUESTTYPE_FE);
470 define('TYPO3_REQUESTTYPE_BE', self::REQUESTTYPE_BE);
471 define('TYPO3_REQUESTTYPE_CLI', self::REQUESTTYPE_CLI);
472 define('TYPO3_REQUESTTYPE_AJAX', self::REQUESTTYPE_AJAX);
473 define('TYPO3_REQUESTTYPE_INSTALL', self::REQUESTTYPE_INSTALL);
474 }
475
476 /**
477 * Defines the TYPO3_REQUESTTYPE constant so the environment knows which context the request is running.
478 *
479 * @param int $requestType
480 */
481 protected static function setRequestType(int $requestType)
482 {
483 if (defined('TYPO3_REQUESTTYPE')) { // @todo remove once Bootstrap::getInstance() is dropped
484 return;
485 }
486 define('TYPO3_REQUESTTYPE', $requestType);
487 }
488
489 /**
490 * Define constants and variables
491 *
492 * @param string
493 */
494 protected static function defineLegacyConstants(string $mode)
495 {
496 if (defined('TYPO3_MODE')) { // @todo remove once Bootstrap::getInstance()
497 return;
498 }
499 define('TYPO3_MODE', $mode);
500 }
501 }