[TASK] update the comments regarding ajax.php
[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
19 /**
20 * Class to encapsulate base setup of bootstrap.
21 *
22 * This class contains all code that must be executed by every entry script.
23 *
24 * It sets up all basic paths, constants, global variables and checks
25 * the basic environment TYPO3 runs in.
26 *
27 * This class does not use any TYPO3 instance specific configuration, it only
28 * sets up things based on the server environment and core code. Even with a
29 * missing typo3conf/localconf.php this script will be successful.
30 *
31 * The script aborts execution with an error message if
32 * some part fails or conditions are not met.
33 *
34 * This script is internal code and subject to change.
35 * DO NOT use it in own code, or be prepared your code might
36 * break in future versions of the core.
37 */
38 class SystemEnvironmentBuilder {
39
40 /**
41 * A list of supported CGI server APIs
42 * NOTICE: This is a duplicate of the SAME array in GeneralUtility!
43 * It is duplicated here as this information is needed early in bootstrap
44 * and GeneralUtility is not available yet.
45 * @var array
46 */
47 static protected $supportedCgiServerApis = array(
48 'fpm-fcgi',
49 'cgi',
50 'isapi',
51 'cgi-fcgi',
52 'srv', // HHVM with fastcgi
53 );
54
55 /**
56 * An array of disabled methods
57 *
58 * @var string[]
59 */
60 static protected $disabledFunctions = NULL;
61
62 /**
63 * Run base setup.
64 * This entry method is used in all scopes (FE, BE, eid, ajax, ...)
65 *
66 * @internal This method should not be used by 3rd party code. It will change without further notice.
67 * @param string $relativePathPart Relative path of the entry script back to document root
68 * @return void
69 */
70 static public function run($relativePathPart = '') {
71 self::defineBaseConstants();
72 self::definePaths($relativePathPart);
73 self::checkMainPathsExist();
74 self::initializeGlobalVariables();
75 self::initializeGlobalTimeTrackingVariables();
76 self::initializeBasicErrorReporting();
77 }
78
79 /**
80 * Define all simple constants that have no dependency to local configuration
81 *
82 * @return void
83 */
84 static protected function defineBaseConstants() {
85 // This version, branch and copyright
86 define('TYPO3_version', '7.6.0-dev');
87 define('TYPO3_branch', '7.6');
88 define('TYPO3_copyright_year', '1998-2015');
89
90 // TYPO3 external links
91 define('TYPO3_URL_GENERAL', 'https://typo3.org/');
92 define('TYPO3_URL_LICENSE', 'https://typo3.org/typo3-cms/overview/licenses/');
93 define('TYPO3_URL_EXCEPTION', 'https://typo3.org/go/exception/CMS/');
94 define('TYPO3_URL_MAILINGLISTS', 'http://lists.typo3.org/cgi-bin/mailman/listinfo');
95 define('TYPO3_URL_DOCUMENTATION', 'https://typo3.org/documentation/');
96 define('TYPO3_URL_DOCUMENTATION_TSREF', 'https://docs.typo3.org/typo3cms/TyposcriptReference/');
97 define('TYPO3_URL_DOCUMENTATION_TSCONFIG', 'https://docs.typo3.org/typo3cms/TSconfigReference/');
98 define('TYPO3_URL_CONSULTANCY', 'https://typo3.org/support/professional-services/');
99 define('TYPO3_URL_CONTRIBUTE', 'https://typo3.org/contribute/');
100 define('TYPO3_URL_SECURITY', 'https://typo3.org/teams/security/');
101 define('TYPO3_URL_DOWNLOAD', 'https://typo3.org/download/');
102 define('TYPO3_URL_SYSTEMREQUIREMENTS', 'https://typo3.org/typo3-cms/overview/requirements/');
103 define('TYPO3_URL_DONATE', 'https://typo3.org/donate/online-donation/');
104 define('TYPO3_URL_WIKI_OPCODECACHE', 'https://wiki.typo3.org/Opcode_Cache');
105
106 // A null, a tabulator, a linefeed, a carriage return, a substitution, a CR-LF combination
107 define('NUL', chr(0));
108 define('TAB', chr(9));
109 define('LF', chr(10));
110 define('CR', chr(13));
111 define('SUB', chr(26));
112 define('CRLF', CR . LF);
113
114 // Security related constant: Default value of fileDenyPattern
115 define('FILE_DENY_PATTERN_DEFAULT', '\\.(php[3-6]?|phpsh|phtml)(\\..*)?$|^\\.htaccess$');
116 // Security related constant: List of file extensions that should be registered as php script file extensions
117 define('PHP_EXTENSIONS_DEFAULT', 'php,php3,php4,php5,php6,phpsh,inc,phtml');
118
119 // Operating system identifier
120 // Either "WIN" or empty string
121 define('TYPO3_OS', self::getTypo3Os());
122
123 // Service error constants
124 // General error - something went wrong
125 define('T3_ERR_SV_GENERAL', -1);
126 // During execution it showed that the service is not available and should be ignored. The service itself should call $this->setNonAvailable()
127 define('T3_ERR_SV_NOT_AVAIL', -2);
128 // Passed subtype is not possible with this service
129 define('T3_ERR_SV_WRONG_SUBTYPE', -3);
130 // Passed subtype is not possible with this service
131 define('T3_ERR_SV_NO_INPUT', -4);
132 // File not found which the service should process
133 define('T3_ERR_SV_FILE_NOT_FOUND', -20);
134 // File not readable
135 define('T3_ERR_SV_FILE_READ', -21);
136 // File not writable
137 define('T3_ERR_SV_FILE_WRITE', -22);
138 // Passed subtype is not possible with this service
139 define('T3_ERR_SV_PROG_NOT_FOUND', -40);
140 // Passed subtype is not possible with this service
141 define('T3_ERR_SV_PROG_FAILED', -41);
142 }
143
144 /**
145 * Calculate all required base paths and set as constants.
146 *
147 * @param string $relativePathPart Relative path of the entry script back to document root
148 * @return void
149 */
150 static protected function definePaths($relativePathPart = '') {
151 // Relative path from document root to typo3/ directory
152 // Hardcoded to "typo3/"
153 define('TYPO3_mainDir', 'typo3/');
154 // Absolute path of the entry script that was called
155 // All paths are unified between Windows and Unix, so the \ of Windows is substituted to a /
156 // Example "/var/www/instance-name/htdocs/typo3conf/ext/wec_map/mod1/index.php"
157 // Example "c:/var/www/instance-name/htdocs/typo3/index.php?M=main" for a path in Windows
158 if (!defined('PATH_thisScript')) {
159 define('PATH_thisScript', self::getPathThisScript());
160 }
161 // Absolute path of the document root of the instance with trailing slash
162 // Example "/var/www/instance-name/htdocs/"
163 if (!defined('PATH_site')) {
164 define('PATH_site', self::getPathSite($relativePathPart));
165 }
166 // Absolute path of the typo3 directory of the instance with trailing slash
167 // Example "/var/www/instance-name/htdocs/typo3/"
168 define('PATH_typo3', PATH_site . TYPO3_mainDir);
169 // Absolute path to the typo3conf directory with trailing slash
170 // Example "/var/www/instance-name/htdocs/typo3conf/"
171 define('PATH_typo3conf', PATH_site . 'typo3conf/');
172 }
173
174 /**
175 * Check if path and script file name calculation was successful, exit if not.
176 *
177 * @return void
178 */
179 static protected function checkMainPathsExist() {
180 if (!is_file(PATH_thisScript)) {
181 static::dieWithMessage('Unable to determine path to entry script.');
182 }
183 if (!is_dir(PATH_typo3 . 'sysext')) {
184 static::dieWithMessage('Calculated absolute path to typo3/sysext directory does not exist.' . LF . LF
185 . 'Something in the main file, folder and link structure is wrong and must be fixed! A typical document root contains a couple of symbolic links:' . LF
186 . '* A symlink "typo3_src" pointing to the TYPO3 CMS core.' . LF
187 . '* A symlink "typo3" - the backend entry point - pointing to "typo3_src/typo3"' . LF
188 . '* A symlink "index.php" - the frontend entry point - points to "typo3_src/index.php"');
189 }
190 }
191
192 /**
193 * Set up / initialize several globals variables
194 *
195 * @return void
196 */
197 static protected function initializeGlobalVariables() {
198 // Unset variable(s) in global scope (security issue #13959)
199 unset($GLOBALS['error']);
200 $GLOBALS['TYPO3_MISC'] = array();
201 $GLOBALS['T3_VAR'] = array();
202 $GLOBALS['T3_SERVICES'] = array();
203 }
204
205 /**
206 * Initialize global time tracking variables.
207 * These are helpers to for example output script parsetime at the end of a script.
208 *
209 * @return void
210 */
211 static protected function initializeGlobalTimeTrackingVariables() {
212 // Set PARSETIME_START to the system time in milliseconds.
213 $GLOBALS['PARSETIME_START'] = GeneralUtility::milliseconds();
214 // Microtime of (nearly) script start
215 $GLOBALS['TYPO3_MISC']['microtime_start'] = microtime(TRUE);
216 // EXEC_TIME is set so that the rest of the script has a common value for the script execution time
217 $GLOBALS['EXEC_TIME'] = time();
218 // $ACCESS_TIME is a common time in minutes for access control
219 $GLOBALS['ACCESS_TIME'] = $GLOBALS['EXEC_TIME'] - $GLOBALS['EXEC_TIME'] % 60;
220 // $SIM_EXEC_TIME is set to $EXEC_TIME but can be altered later in the script if we want to
221 // simulate another execution-time when selecting from eg. a database
222 $GLOBALS['SIM_EXEC_TIME'] = $GLOBALS['EXEC_TIME'];
223 // If $SIM_EXEC_TIME is changed this value must be set accordingly
224 $GLOBALS['SIM_ACCESS_TIME'] = $GLOBALS['ACCESS_TIME'];
225 }
226
227 /**
228 * Initialize basic error reporting.
229 *
230 * There are a lot of extensions that have no strict / notice / deprecated free
231 * ext_localconf or ext_tables. Since the final error reporting must be set up
232 * after those extension files are read, a default configuration is needed to
233 * suppress error reporting meanwhile during further bootstrap.
234 *
235 * @return void
236 */
237 static protected function initializeBasicErrorReporting() {
238 // Core should be notice free at least until this point ...
239 error_reporting(E_ALL & ~(E_STRICT | E_NOTICE | E_DEPRECATED));
240 }
241
242 /**
243 * Determine the operating system TYPO3 is running on.
244 *
245 * @return string Either 'WIN' if running on Windows, else empty string
246 */
247 static protected function getTypo3Os() {
248 $typoOs = '';
249 if (!stristr(PHP_OS, 'darwin') && !stristr(PHP_OS, 'cygwin') && stristr(PHP_OS, 'win')) {
250 $typoOs = 'WIN';
251 }
252 return $typoOs;
253 }
254
255 /**
256 * Calculate PATH_thisScript
257 *
258 * First step in path calculation: Goal is to find the absolute path of the entry script
259 * that was called without resolving any links. This is important since the TYPO3 entry
260 * points are often linked to a central core location, so we can not use the php magic
261 * __FILE__ here, but resolve the called script path from given server environments.
262 *
263 * This path is important to calculate the document root (PATH_site). The strategy is to
264 * find out the script name that was called in the first place and to subtract the local
265 * part from it to find the document root.
266 *
267 * @return string Absolute path to entry script
268 */
269 static protected function getPathThisScript() {
270 if (defined('TYPO3_cliMode') && TYPO3_cliMode === TRUE) {
271 return self::getPathThisScriptCli();
272 } else {
273 return self::getPathThisScriptNonCli();
274 }
275 }
276
277 /**
278 * Calculate path to entry script if not in cli mode.
279 *
280 * Depending on the environment, the script path is found in different $_SERVER variables.
281 *
282 * @return string Absolute path to entry script
283 */
284 static protected function getPathThisScriptNonCli() {
285 $cgiPath = '';
286 if (isset($_SERVER['ORIG_PATH_TRANSLATED'])) {
287 $cgiPath = $_SERVER['ORIG_PATH_TRANSLATED'];
288 } elseif (isset($_SERVER['PATH_TRANSLATED'])) {
289 $cgiPath = $_SERVER['PATH_TRANSLATED'];
290 }
291 if ($cgiPath && in_array(PHP_SAPI, self::$supportedCgiServerApis, TRUE)) {
292 $scriptPath = $cgiPath;
293 } else {
294 if (isset($_SERVER['ORIG_SCRIPT_FILENAME'])) {
295 $scriptPath = $_SERVER['ORIG_SCRIPT_FILENAME'];
296 } else {
297 $scriptPath = $_SERVER['SCRIPT_FILENAME'];
298 }
299 }
300 // Replace \ to / for Windows
301 $scriptPath = str_replace('\\', '/', $scriptPath);
302 // Replace double // to /
303 $scriptPath = str_replace('//', '/', $scriptPath);
304 return $scriptPath;
305 }
306
307 /**
308 * Calculate path to entry script if in cli mode.
309 *
310 * First argument of a cli script is the path to the script that was called. If the script does not start
311 * with / (or A:\ for Windows), the path is not absolute yet, and the current working directory is added.
312 *
313 * @return string Absolute path to entry script
314 */
315 static protected function getPathThisScriptCli() {
316 // Possible relative path of the called script
317 if (isset($_SERVER['argv'][0])) {
318 $scriptPath = $_SERVER['argv'][0];
319 } elseif (isset($_ENV['_'])) {
320 $scriptPath = $_ENV['_'];
321 } else {
322 $scriptPath = $_SERVER['_'];
323 }
324 // Find out if path is relative or not
325 $isRelativePath = FALSE;
326 if (TYPO3_OS === 'WIN') {
327 if (!preg_match('/^([a-zA-Z]:)?\\\\/', $scriptPath)) {
328 $isRelativePath = TRUE;
329 }
330 } else {
331 if ($scriptPath[0] !== '/') {
332 $isRelativePath = TRUE;
333 }
334 }
335 // Concatenate path to current working directory with relative path and remove "/./" constructs
336 if ($isRelativePath) {
337 if (isset($_SERVER['PWD'])) {
338 $workingDirectory = $_SERVER['PWD'];
339 } else {
340 $workingDirectory = getcwd();
341 }
342 $scriptPath = $workingDirectory . '/' . preg_replace('/\\.\\//', '', $scriptPath);
343 }
344 return $scriptPath;
345 }
346
347 /**
348 * Calculate the document root part to the instance from PATH_thisScript
349 *
350 * We have two main scenarios for entry points:
351 * - Directly called documentRoot/index.php (-> FE call or eiD include): index.php sets $relativePathPart to
352 * empty string to hint this code that the document root is identical to the directory the script is located at.
353 * - An indirect include of any Backend related script (-> typo3/index.php or the install tool).
354 * - A Backend script: This is the case for the index.php dispatcher and other entry scripts like 'cli_dispatch.phpsh'
355 * or 'typo3/index.php' that are located inside typo3/ directly. In this case the Bootstrap->run() command sets
356 * 'typo3/' as $relativePathPart as base to calculate the document root.
357 *
358 * @param string $relativePathPart Relative directory part from document root to script path
359 * @return string Absolute path to document root of installation
360 */
361 static protected function getPathSite($relativePathPart) {
362 $entryScriptDirectory = self::getUnifiedDirectoryNameWithTrailingSlash(PATH_thisScript);
363 if ($relativePathPart !== '') {
364 $pathSite = substr($entryScriptDirectory, 0, -strlen($relativePathPart));
365 } else {
366 $pathSite = $entryScriptDirectory;
367 }
368 return $pathSite;
369 }
370
371 /**
372 * Remove file name from script path and unify for Windows and Unix
373 *
374 * @param string $absolutePath Absolute path to script
375 * @return string Directory name of script file location, unified for Windows and Unix
376 */
377 static protected function getUnifiedDirectoryNameWithTrailingSlash($absolutePath) {
378 $directory = dirname($absolutePath);
379 if (TYPO3_OS === 'WIN') {
380 $directory = str_replace('\\', '/', $directory);
381 }
382 return $directory . '/';
383 }
384
385 /**
386 * Echo out a text message and die
387 *
388 * @param string $message
389 */
390 static protected function dieWithMessage($message) {
391 header('Content-type: text/plain');
392 die($message);
393 }
394
395 /**
396 * Check if the given function is disabled in the system
397 *
398 * @param string $function
399 * @return bool
400 */
401 static public function isFunctionDisabled($function) {
402 if (static::$disabledFunctions === NULL) {
403 static::$disabledFunctions = GeneralUtility::trimExplode(',', ini_get('disable_functions'));
404 }
405 if (!empty(static::$disabledFunctions)) {
406 return in_array($function, static::$disabledFunctions, TRUE);
407 }
408
409 return FALSE;
410 }
411
412 }