[!!!][TASK] Remove tce_db options "prErr" and "uPT"
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / SimpleDataHandlerController.php
1 <?php
2 namespace TYPO3\CMS\Backend\Controller;
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 Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Backend\Clipboard\Clipboard;
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
21 use TYPO3\CMS\Core\DataHandling\DataHandler;
22 use TYPO3\CMS\Core\Messaging\AbstractMessage;
23 use TYPO3\CMS\Core\Messaging\FlashMessageService;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\CMS\Core\Utility\MathUtility;
26
27 /**
28 * Script Class, creating object of \TYPO3\CMS\Core\DataHandling\DataHandler and
29 * sending the posted data to the object.
30 *
31 * Used by many smaller forms/links in TYPO3, including the QuickEdit module.
32 * Is not used by FormEngine though (main form rendering script) - that uses the same class (DataHandler) but makes its own initialization (to save the redirect request).
33 * For all other cases than FormEngine it is recommended to use this script for submitting your editing forms - but the best solution in any case would probably be to link your application to FormEngine, that will give you easy form-rendering as well.
34 */
35 class SimpleDataHandlerController
36 {
37 /**
38 * Array. Accepts options to be set in TCE object. Currently it supports "reverseOrder" (bool).
39 *
40 * @var array
41 */
42 public $flags;
43
44 /**
45 * Data array on the form [tablename][uid][fieldname] = value
46 *
47 * @var array
48 */
49 public $data;
50
51 /**
52 * Command array on the form [tablename][uid][command] = value.
53 * This array may get additional data set internally based on clipboard commands send in CB var!
54 *
55 * @var array
56 */
57 public $cmd;
58
59 /**
60 * Array passed to ->setMirror.
61 *
62 * @var array
63 */
64 public $mirror;
65
66 /**
67 * Cache command sent to ->clear_cacheCmd
68 *
69 * @var string
70 */
71 public $cacheCmd;
72
73 /**
74 * Redirect URL. Script will redirect to this location after performing operations (unless errors has occurred)
75 *
76 * @var string
77 */
78 public $redirect;
79
80 /**
81 * Clipboard command array. May trigger changes in "cmd"
82 *
83 * @var array
84 */
85 public $CB;
86
87 /**
88 * TYPO3 Core Engine
89 *
90 * @var \TYPO3\CMS\Core\DataHandling\DataHandler
91 */
92 public $tce;
93
94 /**
95 * Constructor
96 */
97 public function __construct()
98 {
99 $GLOBALS['SOBE'] = $this;
100 $this->init();
101 }
102
103 /**
104 * Initialization of the class
105 */
106 public function init()
107 {
108 $beUser = $this->getBackendUser();
109 // GPvars:
110 $this->flags = GeneralUtility::_GP('flags');
111 $this->data = GeneralUtility::_GP('data');
112 $this->cmd = GeneralUtility::_GP('cmd');
113 $this->mirror = GeneralUtility::_GP('mirror');
114 $this->cacheCmd = GeneralUtility::_GP('cacheCmd');
115 $this->redirect = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('redirect'));
116 $this->CB = GeneralUtility::_GP('CB');
117 // Creating DataHandler object
118 $this->tce = GeneralUtility::makeInstance(DataHandler::class);
119 // Configuring based on user prefs.
120 if ($beUser->uc['recursiveDelete']) {
121 // TRUE if the delete Recursive flag is set.
122 $this->tce->deleteTree = 1;
123 }
124 if ($beUser->uc['copyLevels']) {
125 // Set to number of page-levels to copy.
126 $this->tce->copyTree = MathUtility::forceIntegerInRange($beUser->uc['copyLevels'], 0, 100);
127 }
128 if ($beUser->uc['neverHideAtCopy']) {
129 $this->tce->neverHideAtCopy = 1;
130 }
131 $TCAdefaultOverride = $beUser->getTSConfigProp('TCAdefaults');
132 if (is_array($TCAdefaultOverride)) {
133 $this->tce->setDefaultsFromUserTS($TCAdefaultOverride);
134 }
135 // Reverse order.
136 if ($this->flags['reverseOrder']) {
137 $this->tce->reverseOrder = 1;
138 }
139 }
140
141 /**
142 * Clipboard pasting and deleting.
143 */
144 public function initClipboard()
145 {
146 if (is_array($this->CB)) {
147 $clipObj = GeneralUtility::makeInstance(Clipboard::class);
148 $clipObj->initializeClipboard();
149 if ($this->CB['paste']) {
150 $clipObj->setCurrentPad($this->CB['pad']);
151 $this->cmd = $clipObj->makePasteCmdArray(
152 $this->CB['paste'],
153 $this->cmd,
154 isset($this->CB['update']) ? $this->CB['update'] : null
155 );
156 }
157 if ($this->CB['delete']) {
158 $clipObj->setCurrentPad($this->CB['pad']);
159 $this->cmd = $clipObj->makeDeleteCmdArray($this->cmd);
160 }
161 }
162 }
163
164 /**
165 * Executing the posted actions ...
166 */
167 public function main()
168 {
169 // LOAD DataHandler with data and cmd arrays:
170 $this->tce->start($this->data, $this->cmd);
171 if (is_array($this->mirror)) {
172 $this->tce->setMirror($this->mirror);
173 }
174 // Checking referer / executing
175 $refInfo = parse_url(GeneralUtility::getIndpEnv('HTTP_REFERER'));
176 $httpHost = GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY');
177 if ($httpHost != $refInfo['host'] && !$GLOBALS['TYPO3_CONF_VARS']['SYS']['doNotCheckReferer']) {
178 $this->tce->log('', 0, 0, 0, 1, 'Referer host "%s" and server host "%s" did not match!', 1, [$refInfo['host'], $httpHost]);
179 } else {
180 // Register uploaded files
181 $this->tce->process_uploads($_FILES);
182 // Execute actions:
183 $this->tce->process_datamap();
184 $this->tce->process_cmdmap();
185 // Clearing cache:
186 if (!empty($this->cacheCmd)) {
187 $this->tce->clear_cacheCmd($this->cacheCmd);
188 }
189 // Update page tree?
190 if (isset($this->data['pages']) || isset($this->cmd['pages'])) {
191 BackendUtility::setUpdateSignal('updatePageTree');
192 }
193 }
194 }
195
196 /**
197 * Injects the request object for the current request or subrequest
198 * As this controller goes only through the main() method, it just redirects to the given URL afterwards.
199 *
200 * @param ServerRequestInterface $request the current request
201 * @param ResponseInterface $response
202 * @return ResponseInterface the response with the content
203 */
204 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
205 {
206 $this->initClipboard();
207 $this->main();
208
209 // Write errors to flash message queue
210 $this->tce->printLogErrorMessages();
211 if ($this->redirect) {
212 $response = $response
213 ->withHeader('Location', GeneralUtility::locationHeaderUrl($this->redirect))
214 ->withStatus(303);
215 }
216 return $response;
217 }
218
219 /**
220 * Processes all AJAX calls and returns a JSON formatted string
221 *
222 * @param ServerRequestInterface $request
223 * @param ResponseInterface $response
224 * @return ResponseInterface
225 */
226 public function processAjaxRequest(ServerRequestInterface $request, ResponseInterface $response)
227 {
228 // do the regular / main logic
229 $this->initClipboard();
230 $this->main();
231
232 /** @var \TYPO3\CMS\Core\Messaging\FlashMessageService $flashMessageService */
233 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
234
235 $content = [
236 'redirect' => $this->redirect,
237 'messages' => [],
238 'hasErrors' => false
239 ];
240
241 // Prints errors (= write them to the message queue)
242 $this->tce->printLogErrorMessages();
243
244 $messages = $flashMessageService->getMessageQueueByIdentifier()->getAllMessagesAndFlush();
245 if (!empty($messages)) {
246 foreach ($messages as $message) {
247 $content['messages'][] = [
248 'title' => $message->getTitle(),
249 'message' => $message->getMessage(),
250 'severity' => $message->getSeverity()
251 ];
252 if ($message->getSeverity() === AbstractMessage::ERROR) {
253 $content['hasErrors'] = true;
254 }
255 }
256 }
257
258 $response->getBody()->write(json_encode($content));
259 return $response;
260 }
261
262 /**
263 * Returns the current BE user.
264 *
265 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
266 */
267 protected function getBackendUser()
268 {
269 return $GLOBALS['BE_USER'];
270 }
271 }