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