[!!!][FEATURE] Introduce Backend Routing
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / AjaxRequestHandler.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 * Base class for all AJAX-related calls for the TYPO3 Backend run through typo3/ajax.php.
23 * Before doing the basic BE-related set up of this request (see the additional calls on $this->bootstrap inside
24 * handleRequest()), some AJAX-calls can be made without a valid user, which is determined here.
25 *
26 * Due to legacy reasons, the actual logic is in EXT:core/Http/AjaxRequestHandler which will eventually
27 * be moved into this class.
28 * In the future, the logic for "TYPO3_PROCEED_IF_NO_USER" will be moved in here as well.
29 */
30 class AjaxRequestHandler implements RequestHandlerInterface {
31
32 /**
33 * Instance of the current TYPO3 bootstrap
34 * @var Bootstrap
35 */
36 protected $bootstrap;
37
38 /**
39 * Constructor handing over the bootstrap
40 *
41 * @param Bootstrap $bootstrap
42 */
43 public function __construct(Bootstrap $bootstrap) {
44 $this->bootstrap = $bootstrap;
45 }
46
47 /**
48 * Handles any AJAX request in the TYPO3 Backend
49 *
50 * @return void
51 */
52 public function handleRequest() {
53
54 // This is a list of requests that don't necessarily need a valid BE user
55 $noUserAjaxIDs = array(
56 'BackendLogin::login',
57 'BackendLogin::logout',
58 'BackendLogin::refreshLogin',
59 'BackendLogin::isTimedOut',
60 'BackendLogin::getChallenge',
61 'BackendLogin::getRsaPublicKey',
62 );
63
64 // First get the ajaxID
65 $ajaxID = isset($_POST['ajaxID']) ? $_POST['ajaxID'] : $_GET['ajaxID'];
66 if (isset($ajaxID)) {
67 $ajaxID = (string)stripslashes($ajaxID);
68 }
69
70 // If we're trying to do an ajax login, don't require a user.
71 if (in_array($ajaxID, $noUserAjaxIDs)) {
72 define('TYPO3_PROCEED_IF_NO_USER', 2);
73 }
74
75 $GLOBALS['ajaxID'] = $ajaxID;
76 $this->bootstrap
77 ->checkLockedBackendAndRedirectOrDie()
78 ->checkBackendIpOrDie()
79 ->checkSslBackendAndRedirectIfNeeded()
80 ->checkValidBrowserOrDie()
81 ->initializeBackendRouter()
82 ->loadExtensionTables(TRUE)
83 ->initializeSpriteManager()
84 ->initializeBackendUser()
85 ->initializeBackendAuthentication()
86 ->initializeLanguageObject()
87 ->initializeBackendTemplate()
88 ->endOutputBufferingAndCleanPreviousOutput()
89 ->initializeOutputCompression()
90 ->sendHttpHeaders();
91
92 // Finding the script path from the registry
93 $ajaxRegistryEntry = isset($GLOBALS['TYPO3_CONF_VARS']['BE']['AJAX'][$ajaxID]) ? $GLOBALS['TYPO3_CONF_VARS']['BE']['AJAX'][$ajaxID] : NULL;
94 $ajaxScript = NULL;
95 $csrfTokenCheck = FALSE;
96 if ($ajaxRegistryEntry !== NULL) {
97 if (is_array($ajaxRegistryEntry)) {
98 if (isset($ajaxRegistryEntry['callbackMethod'])) {
99 $ajaxScript = $ajaxRegistryEntry['callbackMethod'];
100 $csrfTokenCheck = $ajaxRegistryEntry['csrfTokenCheck'];
101 }
102 } else {
103 // @deprecated since 6.2 will be removed two versions later
104 $ajaxScript = $ajaxRegistryEntry;
105 }
106 }
107
108 // Instantiating the AJAX object
109 $ajaxObj = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Http\AjaxRequestHandler::class, $ajaxID);
110 $ajaxParams = array();
111
112 // Evaluating the arguments and calling the AJAX method/function
113 if (empty($ajaxID)) {
114 $ajaxObj->setError('No valid ajaxID parameter given.');
115 } elseif (empty($ajaxScript)) {
116 $ajaxObj->setError('No backend function registered for ajaxID "' . $ajaxID . '".');
117 } else {
118 $success = TRUE;
119 $tokenIsValid = TRUE;
120 if ($csrfTokenCheck) {
121 $tokenIsValid = \TYPO3\CMS\Core\FormProtection\FormProtectionFactory::get()->validateToken(GeneralUtility::_GP('ajaxToken'), 'ajaxCall', $ajaxID);
122 }
123 if ($tokenIsValid) {
124 // Cleanup global variable space
125 unset($csrfTokenCheck, $ajaxRegistryEntry, $tokenIsValid, $success);
126 $success = GeneralUtility::callUserFunction($ajaxScript, $ajaxParams, $ajaxObj, FALSE, TRUE);
127 } else {
128 $ajaxObj->setError('Invalid CSRF token detected for ajaxID "' . $ajaxID . '"!');
129 }
130 if ($success === FALSE) {
131 $ajaxObj->setError('Registered backend function for ajaxID "' . $ajaxID . '" was not found.');
132 }
133 }
134
135 // Outputting the content (and setting the X-JSON-Header)
136 $ajaxObj->render();
137 }
138
139 /**
140 * This request handler can handle any backend request coming from ajax.php
141 *
142 * @return bool If the request is an AJAX backend request, TRUE otherwise FALSE
143 */
144 public function canHandleRequest() {
145 return TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX;
146 }
147
148 /**
149 * Returns the priority - how eager the handler is to actually handle the request.
150 *
151 * @return int The priority of the request handler.
152 */
153 public function getPriority() {
154 return 80;
155 }
156 }