[RELEASE] Release of TYPO3 7.3.0
[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::handleMagicQuotesGpc();
75 self::initializeGlobalVariables();
76 self::initializeGlobalTimeTrackingVariables();
77 self::initializeBasicErrorReporting();
78 }
79
80 /**
81 * Define all simple constants that have no dependency to local configuration
82 *
83 * @return void
84 */
85 static protected function defineBaseConstants() {
86 // This version, branch and copyright
87 define('TYPO3_version', '7.3.0');
88 define('TYPO3_branch', '7.3');
89 define('TYPO3_copyright_year', '1998-2015');
90
91 // TYPO3 external links
92 define('TYPO3_URL_GENERAL', 'http://typo3.org/');
93 define('TYPO3_URL_ORG', 'http://typo3.org/');
94 define('TYPO3_URL_LICENSE', 'http://typo3.org/licenses');
95 define('TYPO3_URL_EXCEPTION', 'http://typo3.org/go/exception/CMS/');
96 define('TYPO3_URL_MAILINGLISTS', 'http://lists.typo3.org/cgi-bin/mailman/listinfo');
97 define('TYPO3_URL_DOCUMENTATION', 'http://typo3.org/documentation/');
98 define('TYPO3_URL_DOCUMENTATION_TSREF', 'http://docs.typo3.org/typo3cms/TyposcriptReference/');
99 define('TYPO3_URL_DOCUMENTATION_TSCONFIG', 'http://docs.typo3.org/typo3cms/TSconfigReference/');
100 define('TYPO3_URL_CONSULTANCY', 'http://typo3.org/support/professional-services/');
101 define('TYPO3_URL_CONTRIBUTE', 'http://typo3.org/contribute/');
102 define('TYPO3_URL_SECURITY', 'http://typo3.org/teams/security/');
103 define('TYPO3_URL_DOWNLOAD', 'http://typo3.org/download/');
104 define('TYPO3_URL_SYSTEMREQUIREMENTS', 'http://typo3.org/typo3-cms/overview/requirements/');
105 define('TYPO3_URL_DONATE', 'http://typo3.org/donate/online-donation/');
106 define('TYPO3_URL_WIKI_OPCODECACHE', 'http://wiki.typo3.org/Opcode_Cache');
107
108 // A null, a tabulator, a linefeed, a carriage return, a substitution, a CR-LF combination
109 define('NUL', chr(0));
110 define('TAB', chr(9));
111 define('LF', chr(10));
112 define('CR', chr(13));
113 define('SUB', chr(26));
114 define('CRLF', CR . LF);
115
116 // Security related constant: Default value of fileDenyPattern
117 define('FILE_DENY_PATTERN_DEFAULT', '\\.(php[3-6]?|phpsh|phtml)(\\..*)?$|^\\.htaccess$');
118 // Security related constant: List of file extensions that should be registered as php script file extensions
119 define('PHP_EXTENSIONS_DEFAULT', 'php,php3,php4,php5,php6,phpsh,inc,phtml');
120
121 // Operating system identifier
122 // Either "WIN" or empty string
123 define('TYPO3_OS', self::getTypo3Os());
124
125 // Service error constants
126 // General error - something went wrong
127 define('T3_ERR_SV_GENERAL', -1);
128 // During execution it showed that the service is not available and should be ignored. The service itself should call $this->setNonAvailable()
129 define('T3_ERR_SV_NOT_AVAIL', -2);
130 // Passed subtype is not possible with this service
131 define('T3_ERR_SV_WRONG_SUBTYPE', -3);
132 // Passed subtype is not possible with this service
133 define('T3_ERR_SV_NO_INPUT', -4);
134 // File not found which the service should process
135 define('T3_ERR_SV_FILE_NOT_FOUND', -20);
136 // File not readable
137 define('T3_ERR_SV_FILE_READ', -21);
138 // File not writable
139 define('T3_ERR_SV_FILE_WRITE', -22);
140 // Passed subtype is not possible with this service
141 define('T3_ERR_SV_PROG_NOT_FOUND', -40);
142 // Passed subtype is not possible with this service
143 define('T3_ERR_SV_PROG_FAILED', -41);
144 }
145
146 /**
147 * Calculate all required base paths and set as constants.
148 *
149 * @param string $relativePathPart Relative path of the entry script back to document root
150 * @return void
151 */
152 static protected function definePaths($relativePathPart = '') {
153 // Relative path from document root to typo3/ directory
154 // Hardcoded to "typo3/"
155 define('TYPO3_mainDir', 'typo3/');
156 // Absolute path of the entry script that was called
157 // All paths are unified between Windows and Unix, so the \ of Windows is substituted to a /
158 // Example "/var/www/instance-name/htdocs/typo3conf/ext/wec_map/mod1/index.php"
159 // Example "c:/var/www/instance-name/htdocs/typo3/backend.php" for a path in Windows
160 if (!defined('PATH_thisScript')) {
161 define('PATH_thisScript', self::getPathThisScript());
162 }
163 // Absolute path of the document root of the instance with trailing slash
164 // Example "/var/www/instance-name/htdocs/"
165 if (!defined('PATH_site')) {
166 define('PATH_site', self::getPathSite($relativePathPart));
167 }
168 // Absolute path of the typo3 directory of the instance with trailing slash
169 // Example "/var/www/instance-name/htdocs/typo3/"
170 define('PATH_typo3', PATH_site . TYPO3_mainDir);
171 // Relative path (from the PATH_typo3) to a BE module NOT using mod.php dispatcher with trailing slash
172 // Example "sysext/perms/mod/" for an extension installed in typo3/sysext/
173 // Example "install/" for the install tool entry script
174 // Example "../typo3conf/ext/templavoila/mod2/ for an extension installed in typo3conf/ext/
175 define('PATH_typo3_mod', defined('TYPO3_MOD_PATH') ? TYPO3_MOD_PATH : '');
176 // Absolute path to the typo3conf directory with trailing slash
177 // Example "/var/www/instance-name/htdocs/typo3conf/"
178 define('PATH_typo3conf', PATH_site . 'typo3conf/');
179 }
180
181 /**
182 * Check if path and script file name calculation was successful, exit if not.
183 *
184 * @return void
185 */
186 static protected function checkMainPathsExist() {
187 if (!is_file(PATH_thisScript)) {
188 static::dieWithMessage('Unable to determine path to entry script.');
189 }
190 if (!is_dir(PATH_typo3 . 'sysext')) {
191 static::dieWithMessage('Calculated absolute path to typo3/sysext directory does not exist.' . LF . LF
192 . '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
193 . '* A symlink "typo3_src" pointing to the TYPO3 CMS core.' . LF
194 . '* A symlink "typo3" - the backend entry point - pointing to "typo3_src/typo3"' . LF
195 . '* A symlink "index.php" - the frontend entry point - points to "typo3_src/index.php"');
196 }
197 }
198
199 /**
200 * Compatibility layer for magic quotes
201 *
202 * @return void
203 */
204 static protected function handleMagicQuotesGpc() {
205 if (!get_magic_quotes_gpc()) {
206 GeneralUtility::addSlashesOnArray($_GET);
207 GeneralUtility::addSlashesOnArray($_POST);
208 $GLOBALS['HTTP_GET_VARS'] = $_GET;
209 $GLOBALS['HTTP_POST_VARS'] = $_POST;
210 }
211 }
212
213 /**
214 * Set up / initialize several globals variables
215 *
216 * @return void
217 */
218 static protected function initializeGlobalVariables() {
219 // Unset variable(s) in global scope (security issue #13959)
220 unset($GLOBALS['error']);
221 // Set up base information about browser/user-agent
222 $GLOBALS['CLIENT'] = GeneralUtility::clientInfo();
223 $GLOBALS['TYPO3_MISC'] = array();
224 $GLOBALS['T3_VAR'] = array();
225 $GLOBALS['T3_SERVICES'] = array();
226 }
227
228 /**
229 * Initialize global time tracking variables.
230 * These are helpers to for example output script parsetime at the end of a script.
231 *
232 * @return void
233 */
234 static protected function initializeGlobalTimeTrackingVariables() {
235 // Set PARSETIME_START to the system time in milliseconds.
236 $GLOBALS['PARSETIME_START'] = GeneralUtility::milliseconds();
237 // Microtime of (nearly) script start
238 $GLOBALS['TYPO3_MISC']['microtime_start'] = microtime(TRUE);
239 // EXEC_TIME is set so that the rest of the script has a common value for the script execution time
240 $GLOBALS['EXEC_TIME'] = time();
241 // $ACCESS_TIME is a common time in minutes for access control
242 $GLOBALS['ACCESS_TIME'] = $GLOBALS['EXEC_TIME'] - $GLOBALS['EXEC_TIME'] % 60;
243 // $SIM_EXEC_TIME is set to $EXEC_TIME but can be altered later in the script if we want to
244 // simulate another execution-time when selecting from eg. a database
245 $GLOBALS['SIM_EXEC_TIME'] = $GLOBALS['EXEC_TIME'];
246 // If $SIM_EXEC_TIME is changed this value must be set accordingly
247 $GLOBALS['SIM_ACCESS_TIME'] = $GLOBALS['ACCESS_TIME'];
248 }
249
250 /**
251 * Initialize basic error reporting.
252 *
253 * There are a lot of extensions that have no strict / notice / deprecated free
254 * ext_localconf or ext_tables. Since the final error reporting must be set up
255 * after those extension files are read, a default configuration is needed to
256 * suppress error reporting meanwhile during further bootstrap.
257 *
258 * @return void
259 */
260 static protected function initializeBasicErrorReporting() {
261 // Core should be notice free at least until this point ...
262 error_reporting(E_ALL & ~(E_STRICT | E_NOTICE | E_DEPRECATED));
263 }
264
265 /**
266 * Determine the operating system TYPO3 is running on.
267 *
268 * @return string Either 'WIN' if running on Windows, else empty string
269 */
270 static protected function getTypo3Os() {
271 $typoOs = '';
272 if (!stristr(PHP_OS, 'darwin') && !stristr(PHP_OS, 'cygwin') && stristr(PHP_OS, 'win')) {
273 $typoOs = 'WIN';
274 }
275 return $typoOs;
276 }
277
278 /**
279 * Calculate PATH_thisScript
280 *
281 * First step in path calculation: Goal is to find the absolute path of the entry script
282 * that was called without resolving any links. This is important since the TYPO3 entry
283 * points are often linked to a central core location, so we can not use the php magic
284 * __FILE__ here, but resolve the called script path from given server environments.
285 *
286 * This path is important to calculate the document root (PATH_site). The strategy is to
287 * find out the script name that was called in the first place and to subtract the local
288 * part from it to find the document root.
289 *
290 * @return string Absolute path to entry script
291 */
292 static protected function getPathThisScript() {
293 if (defined('TYPO3_cliMode') && TYPO3_cliMode === TRUE) {
294 return self::getPathThisScriptCli();
295 } else {
296 return self::getPathThisScriptNonCli();
297 }
298 }
299
300 /**
301 * Calculate path to entry script if not in cli mode.
302 *
303 * Depending on the environment, the script path is found in different $_SERVER variables.
304 *
305 * @return string Absolute path to entry script
306 */
307 static protected function getPathThisScriptNonCli() {
308 $cgiPath = '';
309 if (isset($_SERVER['ORIG_PATH_TRANSLATED'])) {
310 $cgiPath = $_SERVER['ORIG_PATH_TRANSLATED'];
311 } elseif (isset($_SERVER['PATH_TRANSLATED'])) {
312 $cgiPath = $_SERVER['PATH_TRANSLATED'];
313 }
314 if ($cgiPath && in_array(PHP_SAPI, self::$supportedCgiServerApis, TRUE)) {
315 $scriptPath = $cgiPath;
316 } else {
317 if (isset($_SERVER['ORIG_SCRIPT_FILENAME'])) {
318 $scriptPath = $_SERVER['ORIG_SCRIPT_FILENAME'];
319 } else {
320 $scriptPath = $_SERVER['SCRIPT_FILENAME'];
321 }
322 }
323 // Replace \ to / for Windows
324 $scriptPath = str_replace('\\', '/', $scriptPath);
325 // Replace double // to /
326 $scriptPath = str_replace('//', '/', $scriptPath);
327 return $scriptPath;
328 }
329
330 /**
331 * Calculate path to entry script if in cli mode.
332 *
333 * First argument of a cli script is the path to the script that was called. If the script does not start
334 * with / (or A:\ for Windows), the path is not absolute yet, and the current working directory is added.
335 *
336 * @return string Absolute path to entry script
337 */
338 static protected function getPathThisScriptCli() {
339 // Possible relative path of the called script
340 if (isset($_SERVER['argv'][0])) {
341 $scriptPath = $_SERVER['argv'][0];
342 } elseif (isset($_ENV['_'])) {
343 $scriptPath = $_ENV['_'];
344 } else {
345 $scriptPath = $_SERVER['_'];
346 }
347 // Find out if path is relative or not
348 $isRelativePath = FALSE;
349 if (TYPO3_OS === 'WIN') {
350 if (!preg_match('/^([a-zA-Z]:)?\\\\/', $scriptPath)) {
351 $isRelativePath = TRUE;
352 }
353 } else {
354 if ($scriptPath[0] !== '/') {
355 $isRelativePath = TRUE;
356 }
357 }
358 // Concatenate path to current working directory with relative path and remove "/./" constructs
359 if ($isRelativePath) {
360 if (isset($_SERVER['PWD'])) {
361 $workingDirectory = $_SERVER['PWD'];
362 } else {
363 $workingDirectory = getcwd();
364 }
365 $scriptPath = $workingDirectory . '/' . preg_replace('/\\.\\//', '', $scriptPath);
366 }
367 return $scriptPath;
368 }
369
370 /**
371 * Calculate the document root part to the instance from PATH_thisScript
372 *
373 * There are two ways to hint correct calculation:
374 * Either an explicit specified sub path or the defined constant TYPO3_MOD_PATH. Which one is
375 * used depends on which entry script was called in the first place.
376 *
377 * We have two main scenarios for entry points:
378 * - Directly called documentRoot/index.php (-> FE call or eiD include): index.php sets $relativePathPart to
379 * empty string to hint this code that the document root is identical to the directory the script is located at.
380 * - An indirect include of typo3/init.php (-> a backend module, the install tool, or scripts like ajax.php).
381 * If init.php is included we distinguish two cases:
382 * -- A backend module defines 'TYPO3_MOD_PATH': This is the case for "old" modules that are not called through
383 * "mod.php" dispatcher, and in the install tool. The TYPO3_MOD_PATH defines the relative path to the typo3/
384 * directory. This is taken as base to calculate the document root.
385 * -- A script includes init.php and does not define 'TYPO3_MOD_PATH': This is the case for the mod.php dispatcher
386 * and other entry scripts like 'cli_dispatch.phpsh' or 'ajax.php' that are located parallel to init.php. In
387 * this case init.php sets 'typo3/' as $relativePathPart as base to calculate the document root.
388 *
389 * This basically boils down to the following code:
390 * If TYPO3_MOD_PATH is defined, subtract this 'local' part from the entry point directory, else use
391 * $relativePathPart to subtract this from the the script entry point to find out the document root.
392 *
393 * @param string $relativePathPart Relative directory part from document root to script path if TYPO3_MOD_PATH is not used
394 * @return string Absolute path to document root of installation
395 */
396 static protected function getPathSite($relativePathPart) {
397 // If end of path is not "typo3/" and TYPO3_MOD_PATH is given
398 if (defined('TYPO3_MOD_PATH')) {
399 return self::getPathSiteByTypo3ModulePath();
400 } else {
401 return self::getPathSiteByRelativePathPart($relativePathPart);
402 }
403 }
404
405 /**
406 * Calculate document root by TYPO3_MOD_PATH
407 *
408 * TYPO3_MOD_PATH can have the following values:
409 * - "sysext/extensionName/path/entryScript.php" -> extension is below 'docRoot'/typo3/sysext
410 * - "ext/extensionName/path/entryScript.php" -> extension is below 'docRoot'/typo3/ext
411 * - "../typo3conf/ext/extensionName/path/entryScript.php" -> extension is below 'docRoot'/typo3conf/ext
412 * - "install/index.php" -> install tool in 'docRoot'/typo3/install/
413 *
414 * The method unifies the above and subtracts the calculated path part from PATH_thisScript
415 *
416 * @return string Absolute path to document root of installation
417 */
418 static protected function getPathSiteByTypo3ModulePath() {
419 if (substr(TYPO3_MOD_PATH, 0, 7) === 'sysext/' || substr(TYPO3_MOD_PATH, 0, 4) === 'ext/' || substr(TYPO3_MOD_PATH, 0, 8) === 'install/') {
420 $pathPartRelativeToDocumentRoot = TYPO3_mainDir . TYPO3_MOD_PATH;
421 } elseif (substr(TYPO3_MOD_PATH, 0, strlen('../typo3conf/')) === '../typo3conf/') {
422 $pathPartRelativeToDocumentRoot = substr(TYPO3_MOD_PATH, 3);
423 } else {
424 static::dieWithMessage('Unable to determine TYPO3 document root.');
425 }
426 $entryScriptDirectory = self::getUnifiedDirectoryNameWithTrailingSlash(PATH_thisScript);
427 return substr($entryScriptDirectory, 0, -strlen($pathPartRelativeToDocumentRoot));
428 }
429
430 /**
431 * Find out document root by subtracting $relativePathPart from PATH_thisScript
432 *
433 * @param string $relativePathPart Relative part of script from document root
434 * @return string Absolute path to document root of installation
435 */
436 static protected function getPathSiteByRelativePathPart($relativePathPart) {
437 $entryScriptDirectory = self::getUnifiedDirectoryNameWithTrailingSlash(PATH_thisScript);
438 if ($relativePathPart !== '') {
439 $pathSite = substr($entryScriptDirectory, 0, -strlen($relativePathPart));
440 } else {
441 $pathSite = $entryScriptDirectory;
442 }
443 return $pathSite;
444 }
445
446 /**
447 * Remove file name from script path and unify for Windows and Unix
448 *
449 * @param string $absolutePath Absolute path to script
450 * @return string Directory name of script file location, unified for Windows and Unix
451 */
452 static protected function getUnifiedDirectoryNameWithTrailingSlash($absolutePath) {
453 $directory = dirname($absolutePath);
454 if (TYPO3_OS === 'WIN') {
455 $directory = str_replace('\\', '/', $directory);
456 }
457 return $directory . '/';
458 }
459
460 /**
461 * Echo out a text message and die
462 *
463 * @param string $message
464 */
465 static protected function dieWithMessage($message) {
466 header('Content-type: text/plain');
467 die($message);
468 }
469
470 /**
471 * Check if the given function is disabled in the system
472 *
473 * @param string $function
474 * @return bool
475 */
476 static public function isFunctionDisabled($function) {
477 if (static::$disabledFunctions === NULL) {
478 static::$disabledFunctions = GeneralUtility::trimExplode(',', ini_get('disable_functions'));
479 }
480 if (!empty(static::$disabledFunctions)) {
481 return in_array($function, static::$disabledFunctions, TRUE);
482 }
483
484 return FALSE;
485 }
486 }