[TASK] Avoid ObjectManager in ext:form FormRuntime
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Core / SystemEnvironmentBuilder.php
1 <?php
2
3 /*
4 * This file is part of the TYPO3 CMS project.
5 *
6 * It is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License, either version 2
8 * of the License, or any later version.
9 *
10 * For the full copyright and license information, please read the
11 * LICENSE.txt file that was distributed with this source code.
12 *
13 * The TYPO3 project - inspiring people to share!
14 */
15
16 namespace TYPO3\CMS\Core\Core;
17
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Core\Utility\PathUtility;
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/LocalConfiguration.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 * Run base setup.
55 * This entry method is used in all scopes (FE, BE, Install Tool and CLI)
56 *
57 * @internal This method should not be used by 3rd party code. It will change without further notice.
58 * @param int $entryPointLevel Number of subdirectories where the entry script is located under the document root
59 * @param int $requestType
60 */
61 public static function run(int $entryPointLevel = 0, int $requestType = self::REQUESTTYPE_FE)
62 {
63 self::defineBaseConstants();
64 self::defineTypo3RequestTypes();
65 self::setRequestType($requestType | ($requestType === self::REQUESTTYPE_BE && (strpos($_SERVER['REQUEST_URI'] ?? '', '/typo3/ajax/') !== false || strpos($_REQUEST['route'] ?? '', '/ajax/') === 0) ? TYPO3_REQUESTTYPE_AJAX : 0));
66 self::defineLegacyConstants($requestType === self::REQUESTTYPE_FE ? 'FE' : 'BE');
67 $scriptPath = self::calculateScriptPath($entryPointLevel, $requestType);
68 $rootPath = self::calculateRootPath($entryPointLevel, $requestType);
69
70 self::initializeGlobalVariables();
71 self::initializeGlobalTimeTrackingVariables();
72 self::initializeEnvironment($requestType, $scriptPath, $rootPath);
73 }
74
75 /**
76 * Some notes:
77 *
78 * HTTP_TYPO3_CONTEXT -> used with Apache suexec support
79 * REDIRECT_TYPO3_CONTEXT -> used under some circumstances when value is set in the webserver and proxying the values to FPM
80 * @return ApplicationContext
81 * @throws \TYPO3\CMS\Core\Exception
82 */
83 protected static function createApplicationContext(): ApplicationContext
84 {
85 $applicationContext = getenv('TYPO3_CONTEXT') ?: (getenv('REDIRECT_TYPO3_CONTEXT') ?: (getenv('HTTP_TYPO3_CONTEXT') ?: 'Production'));
86 return new ApplicationContext($applicationContext);
87 }
88
89 /**
90 * Define all simple constants that have no dependency to local configuration
91 */
92 protected static function defineBaseConstants()
93 {
94 // A linefeed, a carriage return, a CR-LF combination
95 defined('LF') ?: define('LF', chr(10));
96 defined('CR') ?: define('CR', chr(13));
97 defined('CRLF') ?: define('CRLF', CR . LF);
98
99 // A generic constant to state we are in TYPO3 scope. This is especially used in script files
100 // like ext_localconf.php that run in global scope without class encapsulation: "defined('TYPO3') or die();"
101 // This is a security measure to prevent script output if those files are located within document root and
102 // called directly without bootstrap and error handling setup.
103 defined('TYPO3') ?: define('TYPO3', true);
104
105 // Relative path from document root to typo3/ directory, hardcoded to "typo3/"
106 if (!defined('TYPO3_mainDir')) {
107 define('TYPO3_mainDir', 'typo3/');
108 }
109 }
110
111 /**
112 * Calculate script path. This is the absolute path to the entry script.
113 * Can be something like '.../public/index.php' or '.../public/typo3/index.php' for
114 * web calls, or '.../bin/typo3' or similar for cli calls.
115 *
116 * @param int $entryPointLevel Number of subdirectories where the entry script is located under the document root
117 * @param int $requestType
118 * @return string Absolute path to entry script
119 */
120 protected static function calculateScriptPath(int $entryPointLevel, int $requestType): string
121 {
122 $isCli = self::isCliRequestType($requestType);
123 // Absolute path of the entry script that was called
124 $scriptPath = GeneralUtility::fixWindowsFilePath(self::getPathThisScript($isCli));
125 $rootPath = self::getRootPathFromScriptPath($scriptPath, $entryPointLevel);
126 // Check if the root path has been set in the environment (e.g. by the composer installer)
127 if (getenv('TYPO3_PATH_ROOT')) {
128 if ($isCli && self::usesComposerClassLoading()) {
129 // $scriptPath is used for various path calculations based on the document root
130 // Therefore we assume it is always a subdirectory of the document root, which is not the case
131 // in composer mode on cli, as the binary is in the composer bin directory.
132 // Because of that, we enforce the document root path of this binary to be set
133 $scriptName = 'typo3/sysext/core/bin/typo3';
134 } else {
135 // Base the script path on the path taken from the environment
136 // to make relative path calculations work in case only one of both is symlinked
137 // or has the real path
138 $scriptName = ltrim(substr($scriptPath, strlen($rootPath)), '/');
139 }
140 $rootPath = rtrim(GeneralUtility::fixWindowsFilePath((string)getenv('TYPO3_PATH_ROOT')), '/');
141 $scriptPath = $rootPath . '/' . $scriptName;
142 }
143 return $scriptPath;
144 }
145
146 /**
147 * Absolute path to the "classic" site root of the TYPO3 application.
148 * This semantically refers to the directory where executable server-side code, configuration
149 * and runtime files are located (e.g. typo3conf/ext, typo3/sysext, typo3temp/var).
150 * In practice this is always identical to the public web document root path which contains
151 * files that are served by the webserver directly (fileadmin/ and public resources).
152 *
153 * This is not to be confused with the app-path that is used in composer-mode installations (by default).
154 * Resources in app-path are located outside the document root.
155 *
156 * @param int $entryPointLevel Number of subdirectories where the entry script is located under the document root
157 * @param int $requestType
158 * @return string Absolute path without trailing slash
159 */
160 protected static function calculateRootPath(int $entryPointLevel, int $requestType): string
161 {
162 // Check if the root path has been set in the environment (e.g. by the composer installer)
163 if (getenv('TYPO3_PATH_ROOT')) {
164 return rtrim(GeneralUtility::fixWindowsFilePath((string)getenv('TYPO3_PATH_ROOT')), '/');
165 }
166 $isCli = self::isCliRequestType($requestType);
167 // Absolute path of the entry script that was called
168 $scriptPath = GeneralUtility::fixWindowsFilePath(self::getPathThisScript($isCli));
169 return self::getRootPathFromScriptPath($scriptPath, $entryPointLevel);
170 }
171
172 /**
173 * Set up / initialize several globals variables
174 */
175 protected static function initializeGlobalVariables()
176 {
177 // Unset variable(s) in global scope (security issue #13959)
178 $GLOBALS['T3_SERVICES'] = [];
179 }
180
181 /**
182 * Initialize global time tracking variables.
183 * These are helpers to for example output script parsetime at the end of a script.
184 */
185 protected static function initializeGlobalTimeTrackingVariables()
186 {
187 // EXEC_TIME is set so that the rest of the script has a common value for the script execution time
188 $GLOBALS['EXEC_TIME'] = time();
189 // $ACCESS_TIME is a common time in minutes for access control
190 $GLOBALS['ACCESS_TIME'] = $GLOBALS['EXEC_TIME'] - $GLOBALS['EXEC_TIME'] % 60;
191 // $SIM_EXEC_TIME is set to $EXEC_TIME but can be altered later in the script if we want to
192 // simulate another execution-time when selecting from eg. a database
193 $GLOBALS['SIM_EXEC_TIME'] = $GLOBALS['EXEC_TIME'];
194 // If $SIM_EXEC_TIME is changed this value must be set accordingly
195 $GLOBALS['SIM_ACCESS_TIME'] = $GLOBALS['ACCESS_TIME'];
196 }
197
198 /**
199 * Initialize the Environment class
200 *
201 * @param int $requestType
202 * @param string $scriptPath
203 * @param string $sitePath
204 */
205 protected static function initializeEnvironment(int $requestType, string $scriptPath, string $sitePath)
206 {
207 if (getenv('TYPO3_PATH_ROOT')) {
208 $rootPathFromEnvironment = rtrim(GeneralUtility::fixWindowsFilePath((string)getenv('TYPO3_PATH_ROOT')), '/');
209 if ($sitePath !== $rootPathFromEnvironment) {
210 // This means, that we re-initialized the environment during a single request
211 // This currently only happens in custom code or during functional testing
212 // Once the constants are removed, we might be able to remove this code here as well and directly pass an environment to the application
213 $scriptPath = $rootPathFromEnvironment . substr($scriptPath, strlen($sitePath));
214 $sitePath = $rootPathFromEnvironment;
215 }
216 }
217
218 $projectRootPath = GeneralUtility::fixWindowsFilePath((string)getenv('TYPO3_PATH_APP'));
219 $isDifferentRootPath = ($projectRootPath && $projectRootPath !== $sitePath);
220 Environment::initialize(
221 static::createApplicationContext(),
222 self::isCliRequestType($requestType),
223 self::usesComposerClassLoading(),
224 $isDifferentRootPath ? $projectRootPath : $sitePath,
225 $sitePath,
226 $isDifferentRootPath ? $projectRootPath . '/var' : $sitePath . '/typo3temp/var',
227 $isDifferentRootPath ? $projectRootPath . '/config' : $sitePath . '/typo3conf',
228 $scriptPath,
229 self::isRunningOnWindows() ? 'WINDOWS' : 'UNIX'
230 );
231 }
232
233 /**
234 * Determine if the operating system TYPO3 is running on is windows.
235 */
236 protected static function isRunningOnWindows(): bool
237 {
238 return stripos(PHP_OS, 'darwin') === false
239 && stripos(PHP_OS, 'cygwin') === false
240 && stripos(PHP_OS, 'win') !== false;
241 }
242
243 /**
244 * Calculate script path.
245 *
246 * First step in path calculation: Goal is to find the absolute path of the entry script
247 * that was called without resolving any links. This is important since the TYPO3 entry
248 * points are often linked to a central core location, so we can not use the php magic
249 * __FILE__ here, but resolve the called script path from given server environments.
250 *
251 * This path is important to calculate the document root. The strategy is to
252 * find out the script name that was called in the first place and to subtract the local
253 * part from it to find the document root.
254 *
255 * @param bool $isCli
256 * @return string Absolute path to entry script
257 */
258 protected static function getPathThisScript(bool $isCli)
259 {
260 if ($isCli) {
261 return self::getPathThisScriptCli();
262 }
263 return self::getPathThisScriptNonCli();
264 }
265
266 /**
267 * Calculate path to entry script if not in cli mode.
268 *
269 * Depending on the environment, the script path is found in different $_SERVER variables.
270 *
271 * @return string Absolute path to entry script
272 */
273 protected static function getPathThisScriptNonCli()
274 {
275 $cgiPath = $_SERVER['ORIG_PATH_TRANSLATED'] ?? $_SERVER['PATH_TRANSLATED'] ?? '';
276 if ($cgiPath && Environment::isRunningOnCgiServer()) {
277 return $cgiPath;
278 }
279 return $_SERVER['ORIG_SCRIPT_FILENAME'] ?? $_SERVER['SCRIPT_FILENAME'];
280 }
281
282 /**
283 * Calculate path to entry script if in cli mode.
284 *
285 * First argument of a cli script is the path to the script that was called. If the script does not start
286 * with / (or A:\ for Windows), the path is not absolute yet, and the current working directory is added.
287 *
288 * @return string Absolute path to entry script
289 */
290 protected static function getPathThisScriptCli()
291 {
292 // Possible relative path of the called script
293 $scriptPath = $_SERVER['argv'][0] ?? $_ENV['_'] ?? $_SERVER['_'];
294 // Find out if path is relative or not
295 $isRelativePath = false;
296 if (self::isRunningOnWindows()) {
297 if (!preg_match('/^([a-zA-Z]:)?\\\\/', $scriptPath)) {
298 $isRelativePath = true;
299 }
300 } elseif ($scriptPath[0] !== '/') {
301 $isRelativePath = true;
302 }
303 // Concatenate path to current working directory with relative path and remove "/./" constructs
304 if ($isRelativePath) {
305 $workingDirectory = $_SERVER['PWD'] ?? getcwd();
306 $scriptPath = $workingDirectory . '/' . preg_replace('/\\.\\//', '', $scriptPath);
307 }
308 return $scriptPath;
309 }
310
311 /**
312 * Calculate the document root part to the instance from $scriptPath.
313 * This is based on the amount of subdirectories "under" root path where $scriptPath is located.
314 *
315 * The following main scenarios for entry points exist by default in the TYPO3 core:
316 * - Directly called documentRoot/index.php (-> FE call or eiD include): index.php is located in the same directory
317 * as the main project. The document root is identical to the directory the script is located at.
318 * - The install tool, located under typo3/install.php.
319 * - A Backend script: This is the case for the typo3/index.php dispatcher and other entry scripts like 'typo3/sysext/core/bin/typo3'
320 * or 'typo3/index.php' that are located inside typo3/ directly.
321 *
322 * @param string $scriptPath Calculated path to the entry script
323 * @param int $entryPointLevel Number of subdirectories where the entry script is located under the document root
324 * @return string Absolute path to document root of installation without trailing slash
325 */
326 protected static function getRootPathFromScriptPath($scriptPath, $entryPointLevel)
327 {
328 $entryScriptDirectory = PathUtility::dirnameDuringBootstrap($scriptPath);
329 if ($entryPointLevel > 0) {
330 [$rootPath] = GeneralUtility::revExplode('/', $entryScriptDirectory, $entryPointLevel + 1);
331 } else {
332 $rootPath = $entryScriptDirectory;
333 }
334 return $rootPath;
335 }
336
337 /**
338 * @return bool
339 */
340 protected static function usesComposerClassLoading(): bool
341 {
342 return defined('TYPO3_COMPOSER_MODE') && TYPO3_COMPOSER_MODE;
343 }
344
345 /**
346 * Define TYPO3_REQUESTTYPE* constants that can be used for developers to see if any context has been hit
347 * also see setRequestType(). Is done at the very beginning so these parameters are always available.
348 *
349 * @deprecated since v11, method can be removed in v12
350 */
351 protected static function defineTypo3RequestTypes()
352 {
353 // Check one of the constants and return early if already defined,
354 // needed if multiple requests are handled in one process, for instance in functional testing.
355 if (defined('TYPO3_REQUESTTYPE_FE')) {
356 return;
357 }
358 /** @deprecated since v11, will be removed in v12. */
359 define('TYPO3_REQUESTTYPE_FE', self::REQUESTTYPE_FE);
360 /** @deprecated since v11, will be removed in v12. */
361 define('TYPO3_REQUESTTYPE_BE', self::REQUESTTYPE_BE);
362 /** @deprecated since v11, will be removed in v12. */
363 define('TYPO3_REQUESTTYPE_CLI', self::REQUESTTYPE_CLI);
364 /** @deprecated since v11, will be removed in v12. */
365 define('TYPO3_REQUESTTYPE_AJAX', self::REQUESTTYPE_AJAX);
366 /** @deprecated since v11, will be removed in v12. */
367 define('TYPO3_REQUESTTYPE_INSTALL', self::REQUESTTYPE_INSTALL);
368 }
369
370 /**
371 * Defines the TYPO3_REQUESTTYPE constant so the environment knows which context the request is running.
372 *
373 * @param int $requestType
374 * @deprecated since v11, method can be removed in v12
375 */
376 protected static function setRequestType(int $requestType)
377 {
378 // Return early if already defined,
379 // needed if multiple requests are handled in one process, for instance in functional testing.
380 if (defined('TYPO3_REQUESTTYPE')) {
381 return;
382 }
383 /** @deprecated since v11, will be removed in v12. Use Core\Http\ApplicationType API or $request->getAttribute('applicationType') instead */
384 define('TYPO3_REQUESTTYPE', $requestType);
385 }
386
387 /**
388 * Define constants and variables
389 *
390 * @param string $mode
391 * @deprecated since v11, method can be removed in v12
392 */
393 protected static function defineLegacyConstants(string $mode)
394 {
395 // Return early if already defined,
396 // needed if multiple requests are handled in one process, for instance in functional testing.
397 if (defined('TYPO3_MODE')) {
398 return;
399 }
400 /** @deprecated since v11, will be removed in v12. Use Core\Http\ApplicationType API instead */
401 define('TYPO3_MODE', $mode);
402 }
403
404 /**
405 * Checks if request type is cli.
406 * Falls back to check PHP_SAPI in case request type is not provided
407 *
408 * @param int|null $requestType
409 * @return bool
410 */
411 protected static function isCliRequestType(?int $requestType): bool
412 {
413 if ($requestType === null) {
414 $requestType = PHP_SAPI === 'cli' ? self::REQUESTTYPE_CLI : self::REQUESTTYPE_FE;
415 }
416
417 return ($requestType & self::REQUESTTYPE_CLI) === self::REQUESTTYPE_CLI;
418 }
419 }