[TASK] Bootstrap: Disentangle cli and install from init.php
[Packages/TYPO3.CMS.git] / typo3 / Bootstrap_Cli.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2012 Christian Kuhn <lolli@schwarzbu.ch>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * This class encapsulates cli specific bootstrap methods.
30 *
31 * @author Christian Kuhn <lolli@schwarzbu.ch>
32 * @package TYPO3
33 * @subpackage core
34 */
35 class Typo3_Bootstrap_Cli {
36
37 /**
38 * Check the script is called from a cli environment.
39 *
40 * @return void
41 */
42 public static function checkEnvironmentOrDie() {
43 if (substr(php_sapi_name(), 0, 3) === 'cgi') {
44 self::initializeCgiCompatibilityLayerOrDie();
45 } elseif (php_sapi_name() !== 'cli') {
46 die('Not called from a command line interface (e.g. a shell or scheduler).' . chr(10));
47 }
48 }
49
50 /**
51 * Check and define cli parameters
52 *
53 * @return void
54 */
55 public static function initializeCliKeyOrDie() {
56 if (!isset($_SERVER['argv'][1])) {
57 fwrite(STDERR, 'The first argument must be a valid key.' . chr(10));
58 exit(1);
59 }
60
61 // First argument is a key that points to the script configuration
62 define('TYPO3_cliKey', $_SERVER['argv'][1]);
63
64 if (!is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys'][TYPO3_cliKey])) {
65 $message = "The supplied 'cliKey' was not valid. Please use one of the available from this list:\n\n";
66 $message .= var_export(array_keys($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys']), TRUE);
67 fwrite(STDERR, $message . LF);
68 exit(1);
69 }
70
71 define('TYPO3_cliInclude', t3lib_div::getFileAbsFileName($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys'][TYPO3_cliKey][0]));
72 $GLOBALS['MCONF']['name'] = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys'][TYPO3_cliKey][1];
73
74 // This is a compatibility layer: Some cli scripts rely on this, like ext:phpunit cli
75 $GLOBALS['temp_cliScriptPath'] = array_shift($_SERVER['argv']);
76 $GLOBALS['temp_cliKey'] = array_shift($_SERVER['argv']);
77 array_unshift($_SERVER['argv'],$GLOBALS['temp_cliScriptPath']);
78 }
79
80 /**
81 * Set up cgi sapi as de facto cli, but check no HTTP
82 * environment variables are set.
83 *
84 * @return void
85 */
86 protected static function initializeCgiCompatibilityLayerOrDie() {
87 // Sanity check: Ensure we're running in a shell or cronjob (and NOT via HTTP)
88 $checkEnvVars = array('HTTP_USER_AGENT', 'HTTP_HOST', 'SERVER_NAME', 'REMOTE_ADDR', 'REMOTE_PORT', 'SERVER_PROTOCOL');
89 foreach ($checkEnvVars as $var) {
90 if (array_key_exists($var, $_SERVER)) {
91 echo 'SECURITY CHECK FAILED! This script cannot be used within your browser!' . chr(10);
92 echo 'If you are sure that we run in a shell or cronjob, please unset' . chr(10);
93 echo 'environment variable ' . $var . ' (usually using \'unset ' . $var . '\')' . chr(10);
94 echo 'before starting this script.' . chr(10);
95 exit;
96 }
97 }
98
99 // Mimic CLI API in CGI API (you must use the -C/-no-chdir and the -q/--no-header switches!)
100 ini_set('html_errors', 0);
101 ini_set('implicit_flush', 1);
102 ini_set('max_execution_time', 0);
103 define(STDIN, fopen('php://stdin', 'r'));
104 define(STDOUT, fopen('php://stdout', 'w'));
105 define(STDERR, fopen('php://stderr', 'w'));
106 }
107 }
108 ?>