087de775664224c599f1da9eae70df92dc636265
[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 TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * Script Class, creating object of \TYPO3\CMS\Core\DataHandling\DataHandler and
21 * sending the posted data to the object.
22 *
23 * Used by many smaller forms/links in TYPO3, including the QuickEdit module.
24 * Is not used by alt_doc.php though (main form rendering script) - that uses the same class (TCEmain) but makes its own initialization (to save the redirect request).
25 * For all other cases than alt_doc.php 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 alt_doc.php, that will give you easy form-rendering as well.
26 *
27 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
28 */
29 class SimpleDataHandlerController {
30
31 /**
32 * Array. Accepts options to be set in TCE object. Currently it supports "reverseOrder" (bool).
33 *
34 * @var array
35 */
36 public $flags;
37
38 /**
39 * Data array on the form [tablename][uid][fieldname] = value
40 *
41 * @var array
42 */
43 public $data;
44
45 /**
46 * Command array on the form [tablename][uid][command] = value.
47 * This array may get additional data set internally based on clipboard commands send in CB var!
48 *
49 * @var array
50 */
51 public $cmd;
52
53 /**
54 * Array passed to ->setMirror.
55 *
56 * @var array
57 */
58 public $mirror;
59
60 /**
61 * Cache command sent to ->clear_cacheCmd
62 *
63 * @var string
64 */
65 public $cacheCmd;
66
67 /**
68 * Redirect URL. Script will redirect to this location after performing operations (unless errors has occurred)
69 *
70 * @var string
71 */
72 public $redirect;
73
74 /**
75 * 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.
76 *
77 * @var int
78 */
79 public $prErr;
80
81 /**
82 * Clipboard command array. May trigger changes in "cmd"
83 *
84 * @var array
85 */
86 public $CB;
87
88 /**
89 * Verification code
90 *
91 * @var string
92 */
93 public $vC;
94
95 /**
96 * Boolean. Update Page Tree Trigger. If set and the manipulated records are pages then the update page tree signal will be set.
97 *
98 * @var int
99 */
100 public $uPT;
101
102 /**
103 * String, general comment (for raising stages of workspace versions)
104 *
105 * @var string
106 */
107 public $generalComment;
108
109 /**
110 * TYPO3 Core Engine
111 *
112 * @var \TYPO3\CMS\Core\DataHandling\DataHandler
113 */
114 public $tce;
115
116 /**
117 * Constructor
118 */
119 public function __construct() {
120 $GLOBALS['SOBE'] = $this;
121 $this->init();
122 }
123
124 /**
125 * Initialization of the class
126 *
127 * @return void
128 */
129 public function init() {
130 // GPvars:
131 $this->flags = GeneralUtility::_GP('flags');
132 $this->data = GeneralUtility::_GP('data');
133 $this->cmd = GeneralUtility::_GP('cmd');
134 $this->mirror = GeneralUtility::_GP('mirror');
135 $this->cacheCmd = GeneralUtility::_GP('cacheCmd');
136 $this->redirect = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('redirect'));
137 $this->prErr = GeneralUtility::_GP('prErr');
138 $this->_disableRTE = GeneralUtility::_GP('_disableRTE');
139 $this->CB = GeneralUtility::_GP('CB');
140 $this->vC = GeneralUtility::_GP('vC');
141 $this->uPT = GeneralUtility::_GP('uPT');
142 $this->generalComment = GeneralUtility::_GP('generalComment');
143 // Creating TCEmain object
144 $this->tce = GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
145 $this->tce->stripslashes_values = 0;
146 $this->tce->generalComment = $this->generalComment;
147 // Configuring based on user prefs.
148 if ($GLOBALS['BE_USER']->uc['recursiveDelete']) {
149 // TRUE if the delete Recursive flag is set.
150 $this->tce->deleteTree = 1;
151 }
152 if ($GLOBALS['BE_USER']->uc['copyLevels']) {
153 // Set to number of page-levels to copy.
154 $this->tce->copyTree = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($GLOBALS['BE_USER']->uc['copyLevels'], 0, 100);
155 }
156 if ($GLOBALS['BE_USER']->uc['neverHideAtCopy']) {
157 $this->tce->neverHideAtCopy = 1;
158 }
159 $TCAdefaultOverride = $GLOBALS['BE_USER']->getTSConfigProp('TCAdefaults');
160 if (is_array($TCAdefaultOverride)) {
161 $this->tce->setDefaultsFromUserTS($TCAdefaultOverride);
162 }
163 // Reverse order.
164 if ($this->flags['reverseOrder']) {
165 $this->tce->reverseOrder = 1;
166 }
167 }
168
169 /**
170 * Clipboard pasting and deleting.
171 *
172 * @return void
173 */
174 public function initClipboard() {
175 if (is_array($this->CB)) {
176 $clipObj = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Clipboard\Clipboard::class);
177 $clipObj->initializeClipboard();
178 if ($this->CB['paste']) {
179 $clipObj->setCurrentPad($this->CB['pad']);
180 $this->cmd = $clipObj->makePasteCmdArray(
181 $this->CB['paste'],
182 $this->cmd,
183 isset($this->CB['update']) ? $this->CB['update'] : NULL
184 );
185 }
186 if ($this->CB['delete']) {
187 $clipObj->setCurrentPad($this->CB['pad']);
188 $this->cmd = $clipObj->makeDeleteCmdArray($this->cmd);
189 }
190 }
191 }
192
193 /**
194 * Executing the posted actions ...
195 *
196 * @return void
197 */
198 public function main() {
199 // LOAD TCEmain with data and cmd arrays:
200 $this->tce->start($this->data, $this->cmd);
201 if (is_array($this->mirror)) {
202 $this->tce->setMirror($this->mirror);
203 }
204 // Checking referer / executing
205 $refInfo = parse_url(GeneralUtility::getIndpEnv('HTTP_REFERER'));
206 $httpHost = GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY');
207 if ($httpHost != $refInfo['host'] && $this->vC != $GLOBALS['BE_USER']->veriCode() && !$GLOBALS['TYPO3_CONF_VARS']['SYS']['doNotCheckReferer']) {
208 $this->tce->log('', 0, 0, 0, 1, 'Referer host "%s" and server host "%s" did not match and veriCode was not valid either!', 1, array($refInfo['host'], $httpHost));
209 } else {
210 // Register uploaded files
211 $this->tce->process_uploads($_FILES);
212 // Execute actions:
213 $this->tce->process_datamap();
214 $this->tce->process_cmdmap();
215 // Clearing cache:
216 if (!empty($this->cacheCmd)) {
217 $this->tce->clear_cacheCmd($this->cacheCmd);
218 }
219 // Update page tree?
220 if ($this->uPT && (isset($this->data['pages']) || isset($this->cmd['pages']))) {
221 \TYPO3\CMS\Backend\Utility\BackendUtility::setUpdateSignal('updatePageTree');
222 }
223 }
224 }
225
226 /**
227 * Redirecting the user after the processing has been done.
228 * Might also display error messages directly, if any.
229 *
230 * @return void
231 */
232 public function finish() {
233 // Prints errors, if...
234 if ($this->prErr) {
235 $this->tce->printLogErrorMessages($this->redirect);
236 }
237 if ($this->redirect && !$this->tce->debug) {
238 \TYPO3\CMS\Core\Utility\HttpUtility::redirect($this->redirect);
239 }
240 }
241
242 /**
243 * Processes all AJAX calls and returns a JSON formatted string
244 *
245 * @param array $parameters
246 * @param \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxRequestHandler
247 */
248 public function processAjaxRequest($parameters, \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxRequestHandler) {
249 // do the regular / main logic
250 $this->initClipboard();
251 $this->main();
252
253 $flashMessageService = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessageService::class);
254
255 $content = array(
256 'redirect' => $this->redirect,
257 'messages' => array(),
258 'hasErrors' => FALSE
259 );
260
261 // Prints errors (= write them to the message queue)
262 if ($this->prErr) {
263 $content['hasErrors'] = TRUE;
264 $this->tce->printLogErrorMessages($this->redirect);
265 }
266
267 $messages = $flashMessageService->getMessageQueueByIdentifier()->getAllMessagesAndFlush();
268 if (!empty($messages)) {
269 foreach ($messages as $message) {
270 $content['messages'][] = array(
271 'title' => $message->getTitle(),
272 'message' => $message->getMessage(),
273 'severity' => $message->getSeverity()
274 );
275 }
276 }
277 $ajaxRequestHandler->setContentFormat('json');
278 $ajaxRequestHandler->setContent($content);
279 }
280
281 }