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