62d4a39a3879a70cc8d40fcde80257e4d1b2e60b
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Console / CliRequestHandler.php
1 <?php
2 namespace TYPO3\CMS\Backend\Console;
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 use TYPO3\CMS\Core\Core\Bootstrap;
18 use TYPO3\CMS\Core\Core\RequestHandlerInterface;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Command Line Interface Request Handler dealing with "cliKey"-based Commands from the cli_dispatch.phpsh script.
23 * Picks up requests only when coming from the CLI mode.
24 * Resolves the "cliKey" which is registered inside $TYPO3_CONF_VARS[SC_OPTIONS][GLOBAL][cliKeys]
25 * and includes the CLI-based script or exits if no valid "cliKey" is found.
26 */
27 class CliRequestHandler implements RequestHandlerInterface {
28
29 /**
30 * Instance of the current TYPO3 bootstrap
31 * @var Bootstrap
32 */
33 protected $bootstrap;
34
35 /**
36 * Constructor handing over the bootstrap
37 *
38 * @param Bootstrap $bootstrap
39 */
40 public function __construct(Bootstrap $bootstrap) {
41 $this->bootstrap = $bootstrap;
42 }
43
44 /**
45 * Handles any commandline request
46 *
47 * @return void
48 */
49 public function handleRequest() {
50 $commandLineKey = $this->getCommandLineKeyOrDie();
51 $commandLineScript = $this->getIncludeScriptByCommandLineKey($commandLineKey);
52
53 $this->boot();
54
55 try {
56 include($commandLineScript);
57 } catch (\Exception $e) {
58 fwrite(STDERR, $e->getMessage() . LF);
59 exit(99);
60 }
61 }
62
63 /**
64 * Execute TYPO3 bootstrap
65 */
66 protected function boot() {
67 // Evaluate the constant for skipping the BE user check for the bootstrap
68 if (defined('TYPO3_PROCEED_IF_NO_USER') && TYPO3_PROCEED_IF_NO_USER) {
69 $proceedIfNoUserIsLoggedIn = TRUE;
70 } else {
71 $proceedIfNoUserIsLoggedIn = FALSE;
72 }
73
74 $this->bootstrap
75 ->loadExtensionTables(TRUE)
76 ->initializeBackendUser()
77 ->initializeBackendAuthentication($proceedIfNoUserIsLoggedIn)
78 ->initializeLanguageObject();
79
80 // Make sure output is not buffered, so command-line output and interaction can take place
81 GeneralUtility::flushOutputBuffers();
82 }
83
84 /**
85 * Check CLI parameters.
86 * First argument is a key that points to the script configuration.
87 * If it is not set or not valid, the script exits with an error message.
88 *
89 * @return string the CLI key in use
90 */
91 protected function getCommandLineKeyOrDie() {
92 $cliKey = $_SERVER['argv'][1];
93 $errorMessage = '';
94 if (empty($cliKey)) {
95 $errorMessage = 'This script must have a \'cliKey\' as first argument.';
96 } elseif (!is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys'][$cliKey])) {
97 $errorMessage = 'The supplied \'cliKey\' is not valid.';
98 }
99
100 // exit with an error message
101 if (!empty($errorMessage)) {
102 $errorMessage .= ' Valid keys are:
103
104 ';
105 $cliKeys = array_keys($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys']);
106 asort($cliKeys);
107 foreach ($cliKeys as $key => $value) {
108 $errorMessage .= ' ' . $value . LF;
109 }
110 fwrite(STDERR, $errorMessage . LF);
111 die(1);
112 }
113
114 return $cliKey;
115 }
116
117 /**
118 * Define cli-related parameters and return the include script.
119 *
120 * @param string $cliKey the CLI key
121 * @return string the absolute path to the include script
122 */
123 protected function getIncludeScriptByCommandLineKey($cliKey) {
124 list($commandLineScript, $commandLineName) = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys'][$cliKey];
125 $commandLineScript = GeneralUtility::getFileAbsFileName($commandLineScript);
126 // Note: These constants are not in use anymore
127 define('TYPO3_cliKey', $cliKey);
128 define('TYPO3_cliInclude', $commandLineScript);
129 $GLOBALS['MCONF']['name'] = $commandLineName;
130 // This is a compatibility layer: Some cli scripts rely on this, like ext:phpunit cli
131 $GLOBALS['temp_cliScriptPath'] = array_shift($_SERVER['argv']);
132 $GLOBALS['temp_cliKey'] = array_shift($_SERVER['argv']);
133 array_unshift($_SERVER['argv'], $GLOBALS['temp_cliScriptPath']);
134 return $commandLineScript;
135 }
136
137 /**
138 * This request handler can handle any CLI request .
139 *
140 * @return bool If the request is a CLI request, TRUE otherwise FALSE
141 */
142 public function canHandleRequest() {
143 return defined('TYPO3_cliMode') && (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_BE) && (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI);
144 }
145
146 /**
147 * Returns the priority - how eager the handler is to actually handle the request.
148 *
149 * @return int The priority of the request handler.
150 */
151 public function getPriority() {
152 return 50;
153 }
154 }