[BUGFIX] Add Request Handling for Console
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Http / Application.php
1 <?php
2 namespace TYPO3\CMS\Backend\Http;
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 /**
21 * Entry point for the TYPO3 Backend (HTTP requests)
22 */
23 class Application implements ApplicationInterface {
24
25 /**
26 * @var Bootstrap
27 */
28 protected $bootstrap;
29
30 /**
31 * @var string
32 */
33 protected $entryPointPath = 'typo3/';
34
35 /**
36 * All available request handlers that can handle backend requests (non-CLI)
37 * @var array
38 */
39 protected $availableRequestHandlers = array(
40 \TYPO3\CMS\Backend\Http\RequestHandler::class,
41 \TYPO3\CMS\Backend\Http\BackendModuleRequestHandler::class,
42 \TYPO3\CMS\Backend\Http\AjaxRequestHandler::class
43 );
44
45 /**
46 * Constructor setting up legacy constant and register available Request Handlers
47 *
48 * @param \Composer\Autoload\ClassLoader|\Helhum\ClassAliasLoader\Composer\ClassAliasLoader $classLoader an instance of the class loader
49 */
50 public function __construct($classLoader) {
51 $this->defineLegacyConstants();
52
53 $this->bootstrap = Bootstrap::getInstance()
54 ->initializeClassLoader($classLoader)
55 ->baseSetup($this->entryPointPath);
56
57 // can be done here after the base setup is done
58 $this->defineAdditionalEntryPointRelatedConstants();
59
60 // Redirect to install tool if base configuration is not found
61 if (!$this->bootstrap->checkIfEssentialConfigurationExists()) {
62 $this->bootstrap->redirectToInstallTool($this->entryPointPath);
63 }
64
65 foreach ($this->availableRequestHandlers as $requestHandler) {
66 $this->bootstrap->registerRequestHandlerImplementation($requestHandler);
67 }
68
69 $this->bootstrap->configure();
70 }
71
72 /**
73 * Set up the application and shut it down afterwards
74 *
75 * @param callable $execute
76 * @return void
77 */
78 public function run(callable $execute = NULL) {
79 $this->bootstrap->handleRequest(\TYPO3\CMS\Core\Http\ServerRequestFactory::fromGlobals());
80
81 if ($execute !== NULL) {
82 if ($execute instanceof \Closure) {
83 $execute->bindTo($this);
84 }
85 call_user_func($execute);
86 }
87
88 $this->bootstrap->shutdown();
89 }
90
91 /**
92 * Define constants and variables
93 */
94 protected function defineLegacyConstants() {
95 define('TYPO3_MODE', 'BE');
96 }
97
98 /**
99 * Define values that are based on the current script
100 */
101 protected function defineAdditionalEntryPointRelatedConstants() {
102 $currentScript = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('SCRIPT_NAME');
103
104 // activate "AJAX" handler when called via ajax.php
105 if (substr($currentScript, -15) === '/typo3/ajax.php') {
106 $GLOBALS['TYPO3_AJAX'] = TRUE;
107 }
108 // allow backend login to work
109 if (substr($currentScript, -16) === '/typo3/index.php') {
110 define('TYPO3_PROCEED_IF_NO_USER', 1);
111 }
112 }
113 }