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