5f3ced9f1bad656bd80a48a6f4444ccf31e2b967
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Core / CliBootstrap.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 * This class encapsulates cli specific bootstrap methods.
19 *
20 * This script is internal code and subject to change.
21 * DO NOT use it in own code, or be prepared your code might
22 * break in future core versions.
23 *
24 * @author Christian Kuhn <lolli@schwarzbu.ch>
25 */
26 class CliBootstrap {
27
28 /**
29 * Check the script is called from a cli environment.
30 *
31 * @return void
32 * @internal This is not a public API method, do not use in own extensions
33 */
34 static public function checkEnvironmentOrDie() {
35 if (substr(php_sapi_name(), 0, 3) === 'cgi') {
36 self::initializeCgiCompatibilityLayerOrDie();
37 } elseif (php_sapi_name() !== 'cli') {
38 die('Not called from a command line interface (e.g. a shell or scheduler).' . LF);
39 }
40 }
41
42 /**
43 * Check and define cli parameters.
44 * First argument is a key that points to the script configuration.
45 * If it is not set or not valid, the script exits with an error message.
46 *
47 * @return void
48 * @internal This is not a public API method, do not use in own extensions
49 */
50 static public function initializeCliKeyOrDie() {
51 if (!isset($_SERVER['argv'][1]) || !is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys'][$_SERVER['argv'][1]])) {
52 if (!isset($_SERVER['argv'][1])) {
53 $message = 'This script must have a \'cliKey\' as first argument.';
54 } else {
55 $message = 'The supplied \'cliKey\' is not valid.';
56 }
57 $message .= ' Valid keys are:
58
59 ';
60 $cliKeys = array_keys($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys']);
61 asort($cliKeys);
62 foreach ($cliKeys as $key => $value) {
63 $message .= ' ' . $value . LF;
64 }
65 fwrite(STDERR, $message . LF);
66 die(1);
67 }
68 define('TYPO3_cliKey', $_SERVER['argv'][1]);
69 define('TYPO3_cliInclude', \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys'][TYPO3_cliKey][0]));
70 $GLOBALS['MCONF']['name'] = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys'][TYPO3_cliKey][1];
71 // This is a compatibility layer: Some cli scripts rely on this, like ext:phpunit cli
72 $GLOBALS['temp_cliScriptPath'] = array_shift($_SERVER['argv']);
73 $GLOBALS['temp_cliKey'] = array_shift($_SERVER['argv']);
74 array_unshift($_SERVER['argv'], $GLOBALS['temp_cliScriptPath']);
75 }
76
77 /**
78 * Set up cgi sapi as de facto cli, but check no HTTP
79 * environment variables are set.
80 *
81 * @return void
82 */
83 static protected function initializeCgiCompatibilityLayerOrDie() {
84 // Sanity check: Ensure we're running in a shell or cronjob (and NOT via HTTP)
85 $checkEnvVars = array('HTTP_USER_AGENT', 'HTTP_HOST', 'SERVER_NAME', 'REMOTE_ADDR', 'REMOTE_PORT', 'SERVER_PROTOCOL');
86 foreach ($checkEnvVars as $var) {
87 if (array_key_exists($var, $_SERVER)) {
88 echo 'SECURITY CHECK FAILED! This script cannot be used within your browser!' . LF;
89 echo 'If you are sure that we run in a shell or cronjob, please unset' . LF;
90 echo 'environment variable ' . $var . ' (usually using \'unset ' . $var . '\')' . LF;
91 echo 'before starting this script.' . LF;
92 die;
93 }
94 }
95 // Mimic CLI API in CGI API (you must use the -C/-no-chdir and the -q/--no-header switches!)
96 ini_set('html_errors', 0);
97 ini_set('implicit_flush', 1);
98 ini_set('max_execution_time', 0);
99 define('STDIN', fopen('php://stdin', 'r'));
100 define('STDOUT', fopen('php://stdout', 'w'));
101 define('STDERR', fopen('php://stderr', 'w'));
102 }
103
104 }