TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
SystemEnvironmentBuilder.php
Go to the documentation of this file.
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 
18 
39 {
47  protected static $supportedCgiServerApis = [
48  'fpm-fcgi',
49  'cgi',
50  'isapi',
51  'cgi-fcgi',
52  'srv', // HHVM with fastcgi
53  ];
54 
60  protected static $disabledFunctions = null;
61 
70  public static function run($entryPointLevel = 0)
71  {
72  self::defineBaseConstants();
73  self::definePaths($entryPointLevel);
74  self::checkMainPathsExist();
75  self::initializeGlobalVariables();
76  self::initializeGlobalTimeTrackingVariables();
77  self::initializeBasicErrorReporting();
78  }
79 
85  protected static function defineBaseConstants()
86  {
87  // This version, branch and copyright
88  define('TYPO3_version', '8.5.2-dev');
89  define('TYPO3_branch', '8.5');
90  define('TYPO3_copyright_year', '1998-2016');
91 
92  // TYPO3 external links
93  define('TYPO3_URL_GENERAL', 'https://typo3.org/');
94  define('TYPO3_URL_LICENSE', 'https://typo3.org/typo3-cms/overview/licenses/');
95  define('TYPO3_URL_EXCEPTION', 'https://typo3.org/go/exception/CMS/');
96  define('TYPO3_URL_MAILINGLISTS', 'http://lists.typo3.org/cgi-bin/mailman/listinfo');
97  define('TYPO3_URL_DOCUMENTATION', 'https://typo3.org/documentation/');
98  define('TYPO3_URL_DOCUMENTATION_TSREF', 'https://docs.typo3.org/typo3cms/TyposcriptReference/');
99  define('TYPO3_URL_DOCUMENTATION_TSCONFIG', 'https://docs.typo3.org/typo3cms/TSconfigReference/');
100  define('TYPO3_URL_CONSULTANCY', 'https://typo3.org/support/professional-services/');
101  define('TYPO3_URL_CONTRIBUTE', 'https://typo3.org/contribute/');
102  define('TYPO3_URL_SECURITY', 'https://typo3.org/teams/security/');
103  define('TYPO3_URL_DOWNLOAD', 'https://typo3.org/download/');
104  define('TYPO3_URL_SYSTEMREQUIREMENTS', 'https://typo3.org/typo3-cms/overview/requirements/');
105  define('TYPO3_URL_DONATE', 'https://typo3.org/donate/online-donation/');
106  define('TYPO3_URL_WIKI_OPCODECACHE', 'https://wiki.typo3.org/Opcode_Cache');
107 
108  // A null, a tabulator, a linefeed, a carriage return, a substitution, a CR-LF combination
109  defined('NUL') ?: define('NUL', chr(0));
110  defined('TAB') ?: define('TAB', chr(9));
111  defined('LF') ?: define('LF', chr(10));
112  defined('CR') ?: define('CR', chr(13));
113  defined('SUB') ?: define('SUB', chr(26));
114  defined('CRLF') ?: define('CRLF', CR . LF);
115 
116  // Security related constant: Default value of fileDenyPattern
117  define('FILE_DENY_PATTERN_DEFAULT', '\\.(php[3-7]?|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,php7,phpsh,inc,phtml');
120 
121  // Operating system identifier
122  // Either "WIN" or empty string
123  defined('TYPO3_OS') ?: 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 
152  protected static function definePaths($entryPointLevel = 0)
153  {
154  // Relative path from document root to typo3/ directory
155  // Hardcoded to "typo3/"
156  define('TYPO3_mainDir', 'typo3/');
157  // Absolute path of the entry script that was called
158  // All paths are unified between Windows and Unix, so the \ of Windows is substituted to a /
159  // Example "/var/www/instance-name/htdocs/typo3conf/ext/wec_map/mod1/index.php"
160  // Example "c:/var/www/instance-name/htdocs/typo3/index.php?M=main" for a path in Windows
161  if (!defined('PATH_thisScript')) {
162  define('PATH_thisScript', self::getPathThisScript());
163  }
164  // Absolute path of the document root of the instance with trailing slash
165  // Example "/var/www/instance-name/htdocs/"
166  if (!defined('PATH_site')) {
167  define('PATH_site', self::getPathSite($entryPointLevel));
168  }
169  // Absolute path of the typo3 directory of the instance with trailing slash
170  // Example "/var/www/instance-name/htdocs/typo3/"
171  define('PATH_typo3', PATH_site . TYPO3_mainDir);
172  // Absolute path to the typo3conf directory with trailing slash
173  // Example "/var/www/instance-name/htdocs/typo3conf/"
174  define('PATH_typo3conf', PATH_site . 'typo3conf/');
175  }
176 
182  protected static function checkMainPathsExist()
183  {
184  if (!is_file(PATH_thisScript)) {
185  static::exitWithMessage('Unable to determine path to entry script.');
186  }
187  if (!is_dir(PATH_typo3 . 'sysext')) {
188  static::exitWithMessage('Calculated absolute path to typo3/sysext directory does not exist.' . LF . LF
189  . '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
190  . '* A symlink "typo3_src" pointing to the TYPO3 CMS core.' . LF
191  . '* A symlink "typo3" - the backend entry point - pointing to "typo3_src/typo3"' . LF
192  . '* A symlink "index.php" - the frontend entry point - points to "typo3_src/index.php"');
193  }
194  }
195 
201  protected static function initializeGlobalVariables()
202  {
203  // Unset variable(s) in global scope (security issue #13959)
204  unset($GLOBALS['error']);
205  $GLOBALS['TYPO3_MISC'] = [];
206  $GLOBALS['T3_VAR'] = [];
207  $GLOBALS['T3_SERVICES'] = [];
208  }
209 
216  protected static function initializeGlobalTimeTrackingVariables()
217  {
218  // Set PARSETIME_START to the system time in milliseconds.
219  $GLOBALS['PARSETIME_START'] = GeneralUtility::milliseconds();
220  // Microtime of (nearly) script start
221  $GLOBALS['TYPO3_MISC']['microtime_start'] = microtime(true);
222  // EXEC_TIME is set so that the rest of the script has a common value for the script execution time
223  $GLOBALS['EXEC_TIME'] = time();
224  // $ACCESS_TIME is a common time in minutes for access control
225  $GLOBALS['ACCESS_TIME'] = $GLOBALS['EXEC_TIME'] - $GLOBALS['EXEC_TIME'] % 60;
226  // $SIM_EXEC_TIME is set to $EXEC_TIME but can be altered later in the script if we want to
227  // simulate another execution-time when selecting from eg. a database
228  $GLOBALS['SIM_EXEC_TIME'] = $GLOBALS['EXEC_TIME'];
229  // If $SIM_EXEC_TIME is changed this value must be set accordingly
230  $GLOBALS['SIM_ACCESS_TIME'] = $GLOBALS['ACCESS_TIME'];
231  }
232 
243  protected static function initializeBasicErrorReporting()
244  {
245  // Core should be notice free at least until this point ...
246  error_reporting(E_ALL & ~(E_STRICT | E_NOTICE | E_DEPRECATED));
247  }
248 
254  protected static function getTypo3Os()
255  {
256  $typoOs = '';
257  if (!stristr(PHP_OS, 'darwin') && !stristr(PHP_OS, 'cygwin') && stristr(PHP_OS, 'win')) {
258  $typoOs = 'WIN';
259  }
260  return $typoOs;
261  }
262 
277  protected static function getPathThisScript()
278  {
279  if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) {
280  return self::getPathThisScriptCli();
281  } else {
282  return self::getPathThisScriptNonCli();
283  }
284  }
285 
293  protected static function getPathThisScriptNonCli()
294  {
295  $cgiPath = '';
296  if (isset($_SERVER['ORIG_PATH_TRANSLATED'])) {
297  $cgiPath = $_SERVER['ORIG_PATH_TRANSLATED'];
298  } elseif (isset($_SERVER['PATH_TRANSLATED'])) {
299  $cgiPath = $_SERVER['PATH_TRANSLATED'];
300  }
301  if ($cgiPath && in_array(PHP_SAPI, self::$supportedCgiServerApis, true)) {
302  $scriptPath = $cgiPath;
303  } else {
304  if (isset($_SERVER['ORIG_SCRIPT_FILENAME'])) {
305  $scriptPath = $_SERVER['ORIG_SCRIPT_FILENAME'];
306  } else {
307  $scriptPath = $_SERVER['SCRIPT_FILENAME'];
308  }
309  }
310  // Replace \ to / for Windows
311  $scriptPath = str_replace('\\', '/', $scriptPath);
312  // Replace double // to /
313  $scriptPath = str_replace('//', '/', $scriptPath);
314  return $scriptPath;
315  }
316 
325  protected static function getPathThisScriptCli()
326  {
327  // Possible relative path of the called script
328  if (isset($_SERVER['argv'][0])) {
329  $scriptPath = $_SERVER['argv'][0];
330  } elseif (isset($_ENV['_'])) {
331  $scriptPath = $_ENV['_'];
332  } else {
333  $scriptPath = $_SERVER['_'];
334  }
335  // Find out if path is relative or not
336  $isRelativePath = false;
337  if (TYPO3_OS === 'WIN') {
338  if (!preg_match('/^([a-zA-Z]:)?\\\\/', $scriptPath)) {
339  $isRelativePath = true;
340  }
341  } else {
342  if ($scriptPath[0] !== '/') {
343  $isRelativePath = true;
344  }
345  }
346  // Concatenate path to current working directory with relative path and remove "/./" constructs
347  if ($isRelativePath) {
348  if (isset($_SERVER['PWD'])) {
349  $workingDirectory = $_SERVER['PWD'];
350  } else {
351  $workingDirectory = getcwd();
352  }
353  $scriptPath = $workingDirectory . '/' . preg_replace('/\\.\\//', '', $scriptPath);
354  }
355  return $scriptPath;
356  }
357 
372  protected static function getPathSite($entryPointLevel)
373  {
374  $entryScriptDirectory = self::getUnifiedDirectoryName(PATH_thisScript);
375  if ($entryPointLevel > 0) {
376  list($pathSite) = GeneralUtility::revExplode('/', $entryScriptDirectory, $entryPointLevel+1);
377  } else {
378  $pathSite = $entryScriptDirectory;
379  }
380  return $pathSite . '/';
381  }
382 
389  protected static function getUnifiedDirectoryName($absolutePath)
390  {
391  $directory = dirname($absolutePath);
392  if (TYPO3_OS === 'WIN') {
393  $directory = str_replace('\\', '/', $directory);
394  }
395  return $directory;
396  }
397 
403  protected static function exitWithMessage($message)
404  {
405  $headers = [
407  'Content-Type: text/plain'
408  ];
409  if (!headers_sent()) {
410  foreach ($headers as $header) {
411  header($header);
412  }
413  }
414  echo $message . LF;
415  exit(1);
416  }
417 
424  public static function isFunctionDisabled($function)
425  {
426  if (static::$disabledFunctions === null) {
427  static::$disabledFunctions = GeneralUtility::trimExplode(',', ini_get('disable_functions'));
428  }
429  if (!empty(static::$disabledFunctions)) {
430  return in_array($function, static::$disabledFunctions, true);
431  }
432 
433  return false;
434  }
435 }
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static revExplode($delimiter, $string, $count=0)