[BUGFIX] Reset global ajaxID for ajax requests
[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 ->loadExtensionTables(TRUE)
82 ->initializeSpriteManager()
83 ->initializeBackendUser()
84 ->initializeBackendAuthentication()
85 ->initializeLanguageObject()
86 ->initializeBackendTemplate()
87 ->endOutputBufferingAndCleanPreviousOutput()
88 ->initializeOutputCompression()
89 ->sendHttpHeaders();
90
91 // Finding the script path from the registry
92 $ajaxRegistryEntry = isset($GLOBALS['TYPO3_CONF_VARS']['BE']['AJAX'][$ajaxID]) ? $GLOBALS['TYPO3_CONF_VARS']['BE']['AJAX'][$ajaxID] : NULL;
93 $ajaxScript = NULL;
94 $csrfTokenCheck = FALSE;
95 if ($ajaxRegistryEntry !== NULL) {
96 if (is_array($ajaxRegistryEntry)) {
97 if (isset($ajaxRegistryEntry['callbackMethod'])) {
98 $ajaxScript = $ajaxRegistryEntry['callbackMethod'];
99 $csrfTokenCheck = $ajaxRegistryEntry['csrfTokenCheck'];
100 }
101 } else {
102 // @deprecated since 6.2 will be removed two versions later
103 $ajaxScript = $ajaxRegistryEntry;
104 }
105 }
106
107 // Instantiating the AJAX object
108 $ajaxObj = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Http\AjaxRequestHandler::class, $ajaxID);
109 $ajaxParams = array();
110
111 // Evaluating the arguments and calling the AJAX method/function
112 if (empty($ajaxID)) {
113 $ajaxObj->setError('No valid ajaxID parameter given.');
114 } elseif (empty($ajaxScript)) {
115 $ajaxObj->setError('No backend function registered for ajaxID "' . $ajaxID . '".');
116 } else {
117 $success = TRUE;
118 $tokenIsValid = TRUE;
119 if ($csrfTokenCheck) {
120 $tokenIsValid = \TYPO3\CMS\Core\FormProtection\FormProtectionFactory::get()->validateToken(GeneralUtility::_GP('ajaxToken'), 'ajaxCall', $ajaxID);
121 }
122 if ($tokenIsValid) {
123 // Cleanup global variable space
124 unset($csrfTokenCheck, $ajaxRegistryEntry, $tokenIsValid, $success);
125 $success = GeneralUtility::callUserFunction($ajaxScript, $ajaxParams, $ajaxObj, FALSE, TRUE);
126 } else {
127 $ajaxObj->setError('Invalid CSRF token detected for ajaxID "' . $ajaxID . '"!');
128 }
129 if ($success === FALSE) {
130 $ajaxObj->setError('Registered backend function for ajaxID "' . $ajaxID . '" was not found.');
131 }
132 }
133
134 // Outputting the content (and setting the X-JSON-Header)
135 $ajaxObj->render();
136 }
137
138 /**
139 * This request handler can handle any backend request coming from ajax.php
140 *
141 * @return bool If the request is an AJAX backend request, TRUE otherwise FALSE
142 */
143 public function canHandleRequest() {
144 return TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX;
145 }
146
147 /**
148 * Returns the priority - how eager the handler is to actually handle the request.
149 *
150 * @return int The priority of the request handler.
151 */
152 public function getPriority() {
153 return 80;
154 }
155 }