fe6f88313ff51ec4a3168805a851897563e4658d
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / CliRequestHandler.php
1 <?php
2 namespace TYPO3\CMS\Backend;
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->bootstrap
54 ->loadExtensionTables(TRUE)
55 ->initializeBackendUser()
56 ->initializeBackendAuthentication()
57 ->initializeLanguageObject();
58
59 // Make sure output is not buffered, so command-line output and interaction can take place
60 GeneralUtility::flushOutputBuffers();
61
62 try {
63 include($commandLineScript);
64 } catch (\Exception $e) {
65 fwrite(STDERR, $e->getMessage() . LF);
66 exit(99);
67 }
68 }
69
70 /**
71 * Check CLI parameters.
72 * First argument is a key that points to the script configuration.
73 * If it is not set or not valid, the script exits with an error message.
74 *
75 * @return string the CLI key in use
76 */
77 protected function getCommandLineKeyOrDie() {
78 $cliKey = $_SERVER['argv'][1];
79 $errorMessage = '';
80 if (empty($cliKey)) {
81 $errorMessage = 'This script must have a \'cliKey\' as first argument.';
82 } elseif (!is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys'][$cliKey])) {
83 $errorMessage = 'The supplied \'cliKey\' is not valid.';
84 }
85
86 // exit with an error message
87 if (!empty($errorMessage)) {
88 $errorMessage .= ' Valid keys are:
89
90 ';
91 $cliKeys = array_keys($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys']);
92 asort($cliKeys);
93 foreach ($cliKeys as $key => $value) {
94 $errorMessage .= ' ' . $value . LF;
95 }
96 fwrite(STDERR, $errorMessage . LF);
97 die(1);
98 }
99
100 return $cliKey;
101 }
102
103 /**
104 * Define cli-related parameters and return the include script.
105 *
106 * @param string $cliKey the CLI key
107 * @return string the absolute path to the include script
108 */
109 protected function getIncludeScriptByCommandLineKey($cliKey) {
110 list($commandLineScript, $commandLineName) = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys'][$cliKey];
111 $commandLineScript = GeneralUtility::getFileAbsFileName($commandLineScript);
112 // Note: These constants are not in use anymore
113 define('TYPO3_cliKey', $cliKey);
114 define('TYPO3_cliInclude', $commandLineScript);
115 $GLOBALS['MCONF']['name'] = $commandLineName;
116 // This is a compatibility layer: Some cli scripts rely on this, like ext:phpunit cli
117 $GLOBALS['temp_cliScriptPath'] = array_shift($_SERVER['argv']);
118 $GLOBALS['temp_cliKey'] = array_shift($_SERVER['argv']);
119 array_unshift($_SERVER['argv'], $GLOBALS['temp_cliScriptPath']);
120 return $commandLineScript;
121 }
122
123 /**
124 * This request handler can handle any CLI request .
125 *
126 * @return bool If the request is a CLI request, TRUE otherwise FALSE
127 */
128 public function canHandleRequest() {
129 return defined('TYPO3_cliMode') && (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_BE) && (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI);
130 }
131
132 /**
133 * Returns the priority - how eager the handler is to actually handle the request.
134 *
135 * @return int The priority of the request handler.
136 */
137 public function getPriority() {
138 return 50;
139 }
140 }