070b54c8c4c8c37906b12075163474013d188ef4
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Console / Application.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 use TYPO3\CMS\Core\Core\ApplicationInterface;
17 use TYPO3\CMS\Core\Core\Bootstrap;
18
19 /**
20 * Entry point for the TYPO3 Command Line for Backend calls
21 *
22 * This class is @deprecated in favor of the Core-based CommandApplication, which has a different
23 * entry-point level, and has a different request handler out-of-the-box. This class will be removed
24 * in TYPO3 v9.
25 */
26 class Application implements ApplicationInterface
27 {
28 /**
29 * @var Bootstrap
30 */
31 protected $bootstrap;
32
33 /**
34 * Number of subdirectories where the entry script is located, relative to PATH_site
35 * @var int
36 */
37 protected $entryPointLevel = 1;
38
39 /**
40 * All available request handlers that can deal with a CLI Request
41 * @var array
42 */
43 protected $availableRequestHandlers = [
44 \TYPO3\CMS\Backend\Console\CliRequestHandler::class
45 ];
46
47 /**
48 * Constructor setting up legacy constants and register available Request Handlers
49 *
50 * @param \Composer\Autoload\ClassLoader $classLoader an instance of the class loader
51 */
52 public function __construct($classLoader)
53 {
54 $this->checkEnvironmentOrDie();
55
56 $this->defineLegacyConstants();
57
58 $this->bootstrap = Bootstrap::getInstance()
59 ->initializeClassLoader($classLoader)
60 ->setRequestType(TYPO3_REQUESTTYPE_BE | TYPO3_REQUESTTYPE_CLI)
61 ->baseSetup($this->entryPointLevel);
62
63 foreach ($this->availableRequestHandlers as $requestHandler) {
64 $this->bootstrap->registerRequestHandlerImplementation($requestHandler);
65 }
66
67 $this->bootstrap->configure();
68 }
69
70 /**
71 * Set up the application and shut it down afterwards
72 *
73 * @param callable $execute
74 */
75 public function run(callable $execute = null)
76 {
77 $this->bootstrap->handleRequest(new \Symfony\Component\Console\Input\ArgvInput());
78
79 if ($execute !== null) {
80 call_user_func($execute);
81 }
82
83 $this->bootstrap->shutdown();
84 }
85
86 /**
87 * Define constants and variables
88 */
89 protected function defineLegacyConstants()
90 {
91 define('TYPO3_MODE', 'BE');
92 }
93
94 /**
95 * Check the script is called from a cli environment.
96 */
97 protected function checkEnvironmentOrDie()
98 {
99 if (substr(php_sapi_name(), 0, 3) === 'cgi') {
100 $this->initializeCgiCompatibilityLayerOrDie();
101 } elseif (php_sapi_name() !== 'cli') {
102 die('Not called from a command line interface (e.g. a shell or scheduler).' . LF);
103 }
104 }
105
106 /**
107 * Set up cgi sapi as de facto cli, but check no HTTP
108 * environment variables are set.
109 */
110 protected function initializeCgiCompatibilityLayerOrDie()
111 {
112 // Sanity check: Ensure we're running in a shell or cronjob (and NOT via HTTP)
113 $checkEnvVars = ['HTTP_USER_AGENT', 'HTTP_HOST', 'SERVER_NAME', 'REMOTE_ADDR', 'REMOTE_PORT', 'SERVER_PROTOCOL'];
114 foreach ($checkEnvVars as $var) {
115 if (array_key_exists($var, $_SERVER)) {
116 echo 'SECURITY CHECK FAILED! This script cannot be used within your browser!' . LF;
117 echo 'If you are sure that we run in a shell or cronjob, please unset' . LF;
118 echo 'environment variable ' . $var . ' (usually using \'unset ' . $var . '\')' . LF;
119 echo 'before starting this script.' . LF;
120 die;
121 }
122 }
123
124 // Mimic CLI API in CGI API (you must use the -C/-no-chdir and the -q/--no-header switches!)
125 ini_set('html_errors', 0);
126 ini_set('implicit_flush', 1);
127 ini_set('max_execution_time', 0);
128 define('STDIN', fopen('php://stdin', 'r'));
129 define('STDOUT', fopen('php://stdout', 'w'));
130 define('STDERR', fopen('php://stderr', 'w'));
131 }
132 }