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