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