SystemEnvironmentBuilder.php 15.4 KB
Newer Older
1
<?php
2

3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
7
8
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
9
 *
10
11
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
14
 * The TYPO3 project - inspiring people to share!
 */
15

16
17
namespace TYPO3\CMS\Core\Core;

18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
use TYPO3\CMS\Core\Utility\PathUtility;
20

21
22
23
/**
 * Class to encapsulate base setup of bootstrap.
 *
24
 * This class contains all code that must be executed by every entry script.
25
26
27
28
29
30
 *
 * It sets up all basic paths, constants, global variables and checks
 * the basic environment TYPO3 runs in.
 *
 * This class does not use any TYPO3 instance specific configuration, it only
 * 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
34
 *
 * The script aborts execution with an error message if
 * some part fails or conditions are not met.
35
36
37
38
 *
 * This script is internal code and subject to change.
 * DO NOT use it in own code, or be prepared your code might
 * break in future versions of the core.
39
 */
40
41
class SystemEnvironmentBuilder
{
42
43
44
45
46
47
48
49
50
51
52
    /** @internal */
    const REQUESTTYPE_FE = 1;
    /** @internal */
    const REQUESTTYPE_BE = 2;
    /** @internal */
    const REQUESTTYPE_CLI = 4;
    /** @internal */
    const REQUESTTYPE_AJAX = 8;
    /** @internal */
    const REQUESTTYPE_INSTALL = 16;

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
65
66
        $scriptPath = self::calculateScriptPath($entryPointLevel, $requestType);
        $rootPath = self::calculateRootPath($entryPointLevel, $requestType);

67
68
        self::initializeGlobalVariables();
        self::initializeGlobalTimeTrackingVariables();
69
        self::initializeEnvironment($requestType, $scriptPath, $rootPath);
70
71
    }

72
73
74
75
76
77
78
79
    /**
     * Some notes:
     *
     * HTTP_TYPO3_CONTEXT -> used with Apache suexec support
     * REDIRECT_TYPO3_CONTEXT -> used under some circumstances when value is set in the webserver and proxying the values to FPM
     * @return ApplicationContext
     * @throws \TYPO3\CMS\Core\Exception
     */
80
81
    protected static function createApplicationContext(): ApplicationContext
    {
82
        $applicationContext = getenv('TYPO3_CONTEXT') ?: (getenv('REDIRECT_TYPO3_CONTEXT') ?: (getenv('HTTP_TYPO3_CONTEXT') ?: 'Production'));
83
        return new ApplicationContext($applicationContext);
84
    }
85

86
87
88
89
90
    /**
     * Define all simple constants that have no dependency to local configuration
     */
    protected static function defineBaseConstants()
    {
91
        // A linefeed, a carriage return, a CR-LF combination
92
93
94
        defined('LF') ?: define('LF', chr(10));
        defined('CR') ?: define('CR', chr(13));
        defined('CRLF') ?: define('CRLF', CR . LF);
95

96
97
98
99
100
101
        // A generic constant to state we are in TYPO3 scope. This is especially used in script files
        // like ext_localconf.php that run in global scope without class encapsulation: "defined('TYPO3') or die();"
        // This is a security measure to prevent script output if those files are located within document root and
        // called directly without bootstrap and error handling setup.
        defined('TYPO3') ?: define('TYPO3', true);

102
103
104
105
        // Relative path from document root to typo3/ directory, hardcoded to "typo3/"
        if (!defined('TYPO3_mainDir')) {
            define('TYPO3_mainDir', 'typo3/');
        }
106
    }
107

108
    /**
109
110
111
     * Calculate script path. This is the absolute path to the entry script.
     * Can be something like '.../public/index.php' or '.../public/typo3/index.php' for
     * web calls, or '.../bin/typo3' or similar for cli calls.
112
     *
113
     * @param int $entryPointLevel Number of subdirectories where the entry script is located under the document root
114
     * @param int $requestType
115
     * @return string Absolute path to entry script
116
     */
117
    protected static function calculateScriptPath(int $entryPointLevel, int $requestType): string
118
    {
119
        $isCli = self::isCliRequestType($requestType);
120
        // Absolute path of the entry script that was called
121
        $scriptPath = GeneralUtility::fixWindowsFilePath(self::getPathThisScript($isCli));
122
123
124
        $rootPath = self::getRootPathFromScriptPath($scriptPath, $entryPointLevel);
        // Check if the root path has been set in the environment (e.g. by the composer installer)
        if (getenv('TYPO3_PATH_ROOT')) {
125
            if ($isCli && self::usesComposerClassLoading()) {
126
                // $scriptPath is used for various path calculations based on the document root
127
128
129
                // Therefore we assume it is always a subdirectory of the document root, which is not the case
                // in composer mode on cli, as the binary is in the composer bin directory.
                // Because of that, we enforce the document root path of this binary to be set
130
                $scriptName = 'typo3/sysext/core/bin/typo3';
131
132
133
134
            } else {
                // Base the script path on the path taken from the environment
                // to make relative path calculations work in case only one of both is symlinked
                // or has the real path
135
                $scriptName = ltrim(substr($scriptPath, strlen($rootPath)), '/');
136
            }
137
            $rootPath = rtrim(GeneralUtility::fixWindowsFilePath((string)getenv('TYPO3_PATH_ROOT')), '/');
138
            $scriptPath = $rootPath . '/' . $scriptName;
139
        }
140
141
        return $scriptPath;
    }
142

143
    /**
144
145
146
147
148
149
150
151
     * Absolute path to the "classic" site root of the TYPO3 application.
     * This semantically refers to the directory where executable server-side code, configuration
     * and runtime files are located (e.g. typo3conf/ext, typo3/sysext, typo3temp/var).
     * In practice this is always identical to the public web document root path which contains
     * files that are served by the webserver directly (fileadmin/ and public resources).
     *
     * This is not to be confused with the app-path that is used in composer-mode installations (by default).
     * Resources in app-path are located outside the document root.
152
153
154
155
156
157
158
159
160
     *
     * @param int $entryPointLevel Number of subdirectories where the entry script is located under the document root
     * @param int $requestType
     * @return string Absolute path without trailing slash
     */
    protected static function calculateRootPath(int $entryPointLevel, int $requestType): string
    {
        // Check if the root path has been set in the environment (e.g. by the composer installer)
        if (getenv('TYPO3_PATH_ROOT')) {
161
            return rtrim(GeneralUtility::fixWindowsFilePath((string)getenv('TYPO3_PATH_ROOT')), '/');
162
        }
163
164
165
166
        $isCli = self::isCliRequestType($requestType);
        // Absolute path of the entry script that was called
        $scriptPath = GeneralUtility::fixWindowsFilePath(self::getPathThisScript($isCli));
        return self::getRootPathFromScriptPath($scriptPath, $entryPointLevel);
167
    }
168

169
170
171
172
173
174
    /**
     * Set up / initialize several globals variables
     */
    protected static function initializeGlobalVariables()
    {
        // Unset variable(s) in global scope (security issue #13959)
175
        $GLOBALS['T3_SERVICES'] = [];
176
    }
177

178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
    /**
     * Initialize global time tracking variables.
     * These are helpers to for example output script parsetime at the end of a script.
     */
    protected static function initializeGlobalTimeTrackingVariables()
    {
        // EXEC_TIME is set so that the rest of the script has a common value for the script execution time
        $GLOBALS['EXEC_TIME'] = time();
        // $ACCESS_TIME is a common time in minutes for access control
        $GLOBALS['ACCESS_TIME'] = $GLOBALS['EXEC_TIME'] - $GLOBALS['EXEC_TIME'] % 60;
        // $SIM_EXEC_TIME is set to $EXEC_TIME but can be altered later in the script if we want to
        // simulate another execution-time when selecting from eg. a database
        $GLOBALS['SIM_EXEC_TIME'] = $GLOBALS['EXEC_TIME'];
        // If $SIM_EXEC_TIME is changed this value must be set accordingly
        $GLOBALS['SIM_ACCESS_TIME'] = $GLOBALS['ACCESS_TIME'];
    }
194

195
196
197
    /**
     * Initialize the Environment class
     *
198
199
     * @param int $requestType
     * @param string $scriptPath
200
     * @param string $sitePath
201
     */
202
    protected static function initializeEnvironment(int $requestType, string $scriptPath, string $sitePath)
203
    {
204
        if (getenv('TYPO3_PATH_ROOT')) {
205
            $rootPathFromEnvironment = rtrim(GeneralUtility::fixWindowsFilePath((string)getenv('TYPO3_PATH_ROOT')), '/');
206
207
208
209
210
211
            if ($sitePath !== $rootPathFromEnvironment) {
                // This means, that we re-initialized the environment during a single request
                // This currently only happens in custom code or during functional testing
                // Once the constants are removed, we might be able to remove this code here as well and directly pass an environment to the application
                $scriptPath = $rootPathFromEnvironment . substr($scriptPath, strlen($sitePath));
                $sitePath = $rootPathFromEnvironment;
212
213
214
            }
        }

215
        $projectRootPath = GeneralUtility::fixWindowsFilePath((string)getenv('TYPO3_PATH_APP'));
216
217
        $isDifferentRootPath = ($projectRootPath && $projectRootPath !== $sitePath);
        Environment::initialize(
218
            static::createApplicationContext(),
219
            self::isCliRequestType($requestType),
220
            static::usesComposerClassLoading(),
221
222
223
224
            $isDifferentRootPath ? $projectRootPath : $sitePath,
            $sitePath,
            $isDifferentRootPath ? $projectRootPath . '/var'    : $sitePath . '/typo3temp/var',
            $isDifferentRootPath ? $projectRootPath . '/config' : $sitePath . '/typo3conf',
225
            $scriptPath,
226
            self::isRunningOnWindows() ? 'WINDOWS' : 'UNIX'
227
228
229
        );
    }

230
    /**
231
     * Determine if the operating system TYPO3 is running on is windows.
232
     */
233
    protected static function isRunningOnWindows(): bool
234
    {
235
236
237
        return stripos(PHP_OS, 'darwin') === false
            && stripos(PHP_OS, 'cygwin') === false
            && stripos(PHP_OS, 'win') !== false;
238
    }
239

240
    /**
241
     * Calculate script path.
242
243
244
245
246
247
     *
     * First step in path calculation: Goal is to find the absolute path of the entry script
     * that was called without resolving any links. This is important since the TYPO3 entry
     * points are often linked to a central core location, so we can not use the php magic
     * __FILE__ here, but resolve the called script path from given server environments.
     *
248
     * This path is important to calculate the document root. The strategy is to
249
250
251
     * find out the script name that was called in the first place and to subtract the local
     * part from it to find the document root.
     *
252
     * @param bool $isCli
253
254
     * @return string Absolute path to entry script
     */
255
    protected static function getPathThisScript(bool $isCli)
256
    {
257
        if ($isCli) {
258
259
            return self::getPathThisScriptCli();
        }
260
        return self::getPathThisScriptNonCli();
261
    }
262

263
264
265
266
267
268
269
270
271
    /**
     * Calculate path to entry script if not in cli mode.
     *
     * Depending on the environment, the script path is found in different $_SERVER variables.
     *
     * @return string Absolute path to entry script
     */
    protected static function getPathThisScriptNonCli()
    {
272
        $cgiPath = $_SERVER['ORIG_PATH_TRANSLATED'] ?? $_SERVER['PATH_TRANSLATED'] ?? '';
273
        if ($cgiPath && Environment::isRunningOnCgiServer()) {
274
            return $cgiPath;
275
        }
276
        return $_SERVER['ORIG_SCRIPT_FILENAME'] ?? $_SERVER['SCRIPT_FILENAME'];
277
    }
278

279
280
281
282
283
284
285
286
287
288
289
    /**
     * Calculate path to entry script if in cli mode.
     *
     * First argument of a cli script is the path to the script that was called. If the script does not start
     * with / (or A:\ for Windows), the path is not absolute yet, and the current working directory is added.
     *
     * @return string Absolute path to entry script
     */
    protected static function getPathThisScriptCli()
    {
        // Possible relative path of the called script
290
        $scriptPath = $_SERVER['argv'][0] ?? $_ENV['_'] ?? $_SERVER['_'];
291
292
        // Find out if path is relative or not
        $isRelativePath = false;
293
        if (self::isRunningOnWindows()) {
294
295
296
            if (!preg_match('/^([a-zA-Z]:)?\\\\/', $scriptPath)) {
                $isRelativePath = true;
            }
297
298
        } elseif ($scriptPath[0] !== '/') {
            $isRelativePath = true;
299
300
301
        }
        // Concatenate path to current working directory with relative path and remove "/./" constructs
        if ($isRelativePath) {
302
            $workingDirectory = $_SERVER['PWD'] ?? getcwd();
303
304
305
306
            $scriptPath = $workingDirectory . '/' . preg_replace('/\\.\\//', '', $scriptPath);
        }
        return $scriptPath;
    }
307

308
    /**
309
     * Calculate the document root part to the instance from $scriptPath.
310
     * This is based on the amount of subdirectories "under" root path where $scriptPath is located.
311
     *
312
313
314
     * The following main scenarios for entry points exist by default in the TYPO3 core:
     * - Directly called documentRoot/index.php (-> FE call or eiD include): index.php is located in the same directory
     * as the main project. The document root is identical to the directory the script is located at.
315
     * - The install tool, located under typo3/install.php.
316
     * - A Backend script: This is the case for the typo3/index.php dispatcher and other entry scripts like 'typo3/sysext/core/bin/typo3'
317
     * or 'typo3/index.php' that are located inside typo3/ directly.
318
     *
319
     * @param string $scriptPath Calculated path to the entry script
320
     * @param int $entryPointLevel Number of subdirectories where the entry script is located under the document root
321
     * @return string Absolute path to document root of installation without trailing slash
322
     */
323
    protected static function getRootPathFromScriptPath($scriptPath, $entryPointLevel)
324
    {
325
        $entryScriptDirectory = PathUtility::dirnameDuringBootstrap($scriptPath);
326
        if ($entryPointLevel > 0) {
327
            [$rootPath] = GeneralUtility::revExplode('/', $entryScriptDirectory, $entryPointLevel + 1);
328
        } else {
329
            $rootPath = $entryScriptDirectory;
330
        }
331
        return $rootPath;
332
    }
333

334
335
336
337
338
339
340
    /**
     * @return bool
     */
    protected static function usesComposerClassLoading(): bool
    {
        return defined('TYPO3_COMPOSER_MODE') && TYPO3_COMPOSER_MODE;
    }
341

342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
    /**
     * Checks if request type is cli.
     * Falls back to check PHP_SAPI in case request type is not provided
     *
     * @param int|null $requestType
     * @return bool
     */
    protected static function isCliRequestType(?int $requestType): bool
    {
        if ($requestType === null) {
            $requestType = PHP_SAPI === 'cli' ? self::REQUESTTYPE_CLI : self::REQUESTTYPE_FE;
        }

        return ($requestType & self::REQUESTTYPE_CLI) === self::REQUESTTYPE_CLI;
    }
357
}