TYPO3  7.6
CliRequestHandler.php
Go to the documentation of this file.
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 
23 
32 {
37  protected $bootstrap;
38 
44  public function __construct(Bootstrap $bootstrap)
45  {
46  $this->bootstrap = $bootstrap;
47  }
48 
55  public function handleRequest(InputInterface $input)
56  {
57  $output = GeneralUtility::makeInstance(ConsoleOutput::class);
58  $exitCode = 0;
59 
60  try {
61  $command = $this->validateCommandLineKeyFromInput($input);
62 
63  // try and look up if the CLI command user exists, throws an exception if the CLI
64  // user cannot be found
65  list($commandLineScript, $commandLineName) = $this->getIncludeScriptByCommandLineKey($command);
66  $this->boot($commandLineName);
67 
68  if (is_callable($commandLineScript)) {
69  call_user_func($commandLineScript);
70  } else {
71  // include the CLI script
72  include($commandLineScript);
73  }
74  } catch (\InvalidArgumentException $e) {
75  $output->writeln('<error>Oops, an error occurred: ' . $e->getMessage() . '</error>');
76  $output->writeln('');
77  $output->writeln('Valid keys are:');
78  $output->writeln('');
79  $cliKeys = array_keys($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys']);
80  asort($cliKeys);
81  foreach ($cliKeys as $key => $value) {
82  $output->writeln(' ' . $value);
83  }
84  $exitCode = $e->getCode();
85  } catch (\RuntimeException $e) {
86  $output->writeln('<error>Oops, an error occurred: ' . $e->getMessage() . '</error>');
87  $exitCode = $e->getCode();
88  } catch (\Exception $e) {
89  $output->writeln('<error>Oops, an error occurred: ' . $e->getMessage() . '</error>');
90  $exitCode = $e->getCode();
91  }
92 
93  exit($exitCode);
94  }
95 
101  protected function boot($commandLineName)
102  {
103  $this->bootstrap
104  ->loadExtensionTables(true)
105  ->initializeBackendUser();
106 
107  // Checks for a user called starting with _CLI_ e.g. "_CLI_lowlevel"
108  $this->loadCommandLineBackendUser($commandLineName);
109 
110  $this->bootstrap
111  ->initializeBackendAuthentication()
112  ->initializeLanguageObject();
113 
114  // Make sure output is not buffered, so command-line output and interaction can take place
116  }
117 
128  {
129  $cliKey = $input->getFirstArgument();
130  if (empty($cliKey)) {
131  throw new \InvalidArgumentException('This script must have a command as first argument.', 1);
132  } elseif (!is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys'][$cliKey])) {
133  throw new \InvalidArgumentException('This supplied command is not valid.', 1);
134  }
135  return $cliKey;
136  }
137 
145  protected function getIncludeScriptByCommandLineKey($cliKey)
146  {
147  list($commandLineScript, $commandLineName) = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys'][$cliKey];
148  if (!is_callable($commandLineScript)) {
149  $commandLineScript = GeneralUtility::getFileAbsFileName($commandLineScript);
150  // Note: These constants are not in use anymore, and marked for deprecation and will be removed in TYPO3 CMS 8
151  define('TYPO3_cliKey', $cliKey);
152  define('TYPO3_cliInclude', $commandLineScript);
153  }
154  // This is a compatibility layer: Some cli scripts rely on this, like ext:phpunit cli
155  // This layer will be removed in TYPO3 CMS 8
156  $GLOBALS['temp_cliScriptPath'] = array_shift($_SERVER['argv']);
157  $GLOBALS['temp_cliKey'] = array_shift($_SERVER['argv']);
158  array_unshift($_SERVER['argv'], $GLOBALS['temp_cliScriptPath']);
159  return array($commandLineScript, $commandLineName);
160  }
161 
168  protected function loadCommandLineBackendUser($commandLineName)
169  {
170  if ($GLOBALS['BE_USER']->user['uid']) {
171  throw new \RuntimeException('Another user was already loaded which is impossible in CLI mode!', 3);
172  }
173  if (!StringUtility::beginsWith($commandLineName, '_CLI_')) {
174  throw new \RuntimeException('Module name, "' . $commandLineName . '", was not prefixed with "_CLI_"', 3);
175  }
176  $userName = strtolower($commandLineName);
177  $GLOBALS['BE_USER']->setBeUserByName($userName);
178  if (!$GLOBALS['BE_USER']->user['uid']) {
179  throw new \RuntimeException('No backend user named "' . $userName . '" was found!', 3);
180  }
181  if ($GLOBALS['BE_USER']->isAdmin()) {
182  throw new \RuntimeException('CLI backend user "' . $userName . '" was ADMIN which is not allowed!', 3);
183  }
184  }
185 
192  public function canHandleRequest(InputInterface $input)
193  {
194  return true;
195  }
196 
202  public function getPriority()
203  {
204  return 20;
205  }
206 }