6cfb1368b9928f438dadd5dd0ff0360076e3db04
[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 * Boolean. If set, errors will be printed on screen instead of redirection. Should always be used, otherwise you will see no errors if they happen.
82 *
83 * @var int
84 */
85 public $prErr;
86
87 /**
88 * Clipboard command array. May trigger changes in "cmd"
89 *
90 * @var array
91 */
92 public $CB;
93
94 /**
95 * Boolean. Update Page Tree Trigger. If set and the manipulated records are pages then the update page tree signal will be set.
96 *
97 * @var int
98 */
99 public $uPT;
100
101 /**
102 * TYPO3 Core Engine
103 *
104 * @var \TYPO3\CMS\Core\DataHandling\DataHandler
105 */
106 public $tce;
107
108 /**
109 * Constructor
110 */
111 public function __construct()
112 {
113 $GLOBALS['SOBE'] = $this;
114 $this->init();
115 }
116
117 /**
118 * Initialization of the class
119 */
120 public function init()
121 {
122 $beUser = $this->getBackendUser();
123 // GPvars:
124 $this->flags = GeneralUtility::_GP('flags');
125 $this->data = GeneralUtility::_GP('data');
126 $this->cmd = GeneralUtility::_GP('cmd');
127 $this->mirror = GeneralUtility::_GP('mirror');
128 $this->cacheCmd = GeneralUtility::_GP('cacheCmd');
129 $this->redirect = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('redirect'));
130 $this->prErr = GeneralUtility::_GP('prErr');
131 $this->CB = GeneralUtility::_GP('CB');
132 $this->uPT = GeneralUtility::_GP('uPT');
133 // Creating DataHandler object
134 $this->tce = GeneralUtility::makeInstance(DataHandler::class);
135 // Configuring based on user prefs.
136 if ($beUser->uc['recursiveDelete']) {
137 // TRUE if the delete Recursive flag is set.
138 $this->tce->deleteTree = 1;
139 }
140 if ($beUser->uc['copyLevels']) {
141 // Set to number of page-levels to copy.
142 $this->tce->copyTree = MathUtility::forceIntegerInRange($beUser->uc['copyLevels'], 0, 100);
143 }
144 if ($beUser->uc['neverHideAtCopy']) {
145 $this->tce->neverHideAtCopy = 1;
146 }
147 $TCAdefaultOverride = $beUser->getTSConfigProp('TCAdefaults');
148 if (is_array($TCAdefaultOverride)) {
149 $this->tce->setDefaultsFromUserTS($TCAdefaultOverride);
150 }
151 // Reverse order.
152 if ($this->flags['reverseOrder']) {
153 $this->tce->reverseOrder = 1;
154 }
155 }
156
157 /**
158 * Clipboard pasting and deleting.
159 */
160 public function initClipboard()
161 {
162 if (is_array($this->CB)) {
163 $clipObj = GeneralUtility::makeInstance(Clipboard::class);
164 $clipObj->initializeClipboard();
165 if ($this->CB['paste']) {
166 $clipObj->setCurrentPad($this->CB['pad']);
167 $this->cmd = $clipObj->makePasteCmdArray(
168 $this->CB['paste'],
169 $this->cmd,
170 isset($this->CB['update']) ? $this->CB['update'] : null
171 );
172 }
173 if ($this->CB['delete']) {
174 $clipObj->setCurrentPad($this->CB['pad']);
175 $this->cmd = $clipObj->makeDeleteCmdArray($this->cmd);
176 }
177 }
178 }
179
180 /**
181 * Executing the posted actions ...
182 */
183 public function main()
184 {
185 // LOAD DataHandler with data and cmd arrays:
186 $this->tce->start($this->data, $this->cmd);
187 if (is_array($this->mirror)) {
188 $this->tce->setMirror($this->mirror);
189 }
190 // Checking referer / executing
191 $refInfo = parse_url(GeneralUtility::getIndpEnv('HTTP_REFERER'));
192 $httpHost = GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY');
193 if ($httpHost != $refInfo['host'] && !$GLOBALS['TYPO3_CONF_VARS']['SYS']['doNotCheckReferer']) {
194 $this->tce->log('', 0, 0, 0, 1, 'Referer host "%s" and server host "%s" did not match!', 1, [$refInfo['host'], $httpHost]);
195 } else {
196 // Register uploaded files
197 $this->tce->process_uploads($_FILES);
198 // Execute actions:
199 $this->tce->process_datamap();
200 $this->tce->process_cmdmap();
201 // Clearing cache:
202 if (!empty($this->cacheCmd)) {
203 $this->tce->clear_cacheCmd($this->cacheCmd);
204 }
205 // Update page tree?
206 if ($this->uPT && (isset($this->data['pages']) || isset($this->cmd['pages']))) {
207 BackendUtility::setUpdateSignal('updatePageTree');
208 }
209 }
210 }
211
212 /**
213 * Injects the request object for the current request or subrequest
214 * As this controller goes only through the main() method, it just redirects to the given URL afterwards.
215 *
216 * @param ServerRequestInterface $request the current request
217 * @param ResponseInterface $response
218 * @return ResponseInterface the response with the content
219 */
220 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
221 {
222 $this->initClipboard();
223 $this->main();
224
225 // Write errors to flash message queue
226 if ($this->prErr) {
227 $this->tce->printLogErrorMessages($this->redirect);
228 }
229 if ($this->redirect) {
230 $response = $response
231 ->withHeader('Location', GeneralUtility::locationHeaderUrl($this->redirect))
232 ->withStatus(303);
233 }
234 return $response;
235 }
236
237 /**
238 * Processes all AJAX calls and returns a JSON formatted string
239 *
240 * @param ServerRequestInterface $request
241 * @param ResponseInterface $response
242 * @return ResponseInterface
243 */
244 public function processAjaxRequest(ServerRequestInterface $request, ResponseInterface $response)
245 {
246 // do the regular / main logic
247 $this->initClipboard();
248 $this->main();
249
250 /** @var \TYPO3\CMS\Core\Messaging\FlashMessageService $flashMessageService */
251 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
252
253 $content = [
254 'redirect' => $this->redirect,
255 'messages' => [],
256 'hasErrors' => false
257 ];
258
259 // Prints errors (= write them to the message queue)
260 if ($this->prErr) {
261 $content['hasErrors'] = true;
262 $this->tce->printLogErrorMessages($this->redirect);
263 }
264
265 $messages = $flashMessageService->getMessageQueueByIdentifier()->getAllMessagesAndFlush();
266 if (!empty($messages)) {
267 foreach ($messages as $message) {
268 $content['messages'][] = [
269 'title' => $message->getTitle(),
270 'message' => $message->getMessage(),
271 'severity' => $message->getSeverity()
272 ];
273 if ($message->getSeverity() === AbstractMessage::ERROR) {
274 $content['hasErrors'] = true;
275 }
276 }
277 }
278
279 $response->getBody()->write(json_encode($content));
280 return $response;
281 }
282
283 /**
284 * Returns the current BE user.
285 *
286 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
287 */
288 protected function getBackendUser()
289 {
290 return $GLOBALS['BE_USER'];
291 }
292 }