[BUGFIX] Make sure excludedTablesAndFields are empty in DataHandler
[Packages/TYPO3.CMS.git] / typo3 / sysext / setup / Classes / Controller / SetupModuleController.php
1 <?php
2 namespace TYPO3\CMS\Setup\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\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Core\Messaging\FlashMessage;
21
22 /**
23 * Script class for the Setup module
24 *
25 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
26 */
27 class SetupModuleController {
28
29 const PASSWORD_NOT_UPDATED = 0;
30 const PASSWORD_UPDATED = 1;
31 const PASSWORD_NOT_THE_SAME = 2;
32 const PASSWORD_OLD_WRONG = 3;
33
34 /**
35 * @var array
36 */
37 public $MOD_MENU = array();
38
39 /**
40 * @var array
41 */
42 public $MOD_SETTINGS = array();
43
44 /**
45 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
46 */
47 public $doc;
48
49 /**
50 * @var string
51 */
52 public $content;
53
54 /**
55 * @var array
56 */
57 public $overrideConf;
58
59 /**
60 * backend user object, set during simulate-user operation
61 *
62 * @var \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
63 */
64 public $OLD_BE_USER;
65
66 /**
67 * @var bool
68 */
69 public $languageUpdate;
70
71 /**
72 * @var bool
73 */
74 protected $pagetreeNeedsRefresh = FALSE;
75
76 /**
77 * @var bool
78 */
79 protected $isAdmin;
80
81 /**
82 * @var array
83 */
84 protected $tsFieldConf;
85
86 /**
87 * @var bool
88 */
89 protected $saveData = FALSE;
90
91 /**
92 * @var int
93 */
94 protected $passwordIsUpdated = self::PASSWORD_NOT_UPDATED;
95
96 /**
97 * @var bool
98 */
99 protected $passwordIsSubmitted = FALSE;
100
101 /**
102 * @var bool
103 */
104 protected $setupIsUpdated = FALSE;
105
106 /**
107 * @var bool
108 */
109 protected $tempDataIsCleared = FALSE;
110
111 /**
112 * @var bool
113 */
114 protected $settingsAreResetToDefault = FALSE;
115
116 /**
117 * Form protection instance
118 *
119 * @var \TYPO3\CMS\Core\FormProtection\BackendFormProtection
120 */
121 protected $formProtection;
122
123 /**
124 * @var string
125 */
126 protected $simulateSelector = '';
127
128 /**
129 * @var string
130 */
131 protected $simUser = '';
132
133 /**
134 * The name of the module
135 *
136 * @var string
137 */
138 protected $moduleName = 'user_setup';
139
140 /**
141 * Instantiate the form protection before a simulated user is initialized.
142 */
143 public function __construct() {
144 $this->formProtection = \TYPO3\CMS\Core\FormProtection\FormProtectionFactory::get();
145 }
146
147 /**
148 * Getter for the form protection instance.
149 *
150 * @return \TYPO3\CMS\Core\FormProtection\BackendFormProtection
151 */
152 public function getFormProtection() {
153 return $this->formProtection;
154 }
155
156 /**
157 * If settings are submitted to _POST[DATA], store them
158 * NOTICE: This method is called before the \TYPO3\CMS\Backend\Template\DocumentTemplate
159 * is included. See bottom of document.
160 *
161 * @see \TYPO3\CMS\Backend\Template\DocumentTemplate
162 */
163 public function storeIncomingData() {
164 // First check if something is submitted in the data-array from POST vars
165 $d = GeneralUtility::_POST('data');
166 $columns = $GLOBALS['TYPO3_USER_SETTINGS']['columns'];
167 $beUser = $this->getBackendUser();
168 $beUserId = $beUser->user['uid'];
169 $storeRec = array();
170 $fieldList = $this->getFieldsFromShowItem();
171 if (is_array($d) && $this->formProtection->validateToken((string)GeneralUtility::_POST('formToken'), 'BE user setup', 'edit')) {
172 // UC hashed before applying changes
173 $save_before = md5(serialize($beUser->uc));
174 // PUT SETTINGS into the ->uc array:
175 // Reload left frame when switching BE language
176 if (isset($d['lang']) && $d['lang'] != $beUser->uc['lang']) {
177 $this->languageUpdate = TRUE;
178 }
179 // Reload pagetree if the title length is changed
180 if (isset($d['titleLen']) && $d['titleLen'] !== $beUser->uc['titleLen']) {
181 $this->pagetreeNeedsRefresh = TRUE;
182 }
183 if ($d['setValuesToDefault']) {
184 // If every value should be default
185 $beUser->resetUC();
186 $this->settingsAreResetToDefault = TRUE;
187 } elseif ($d['clearSessionVars']) {
188 foreach ($beUser->uc as $key => $value) {
189 if (!isset($columns[$key])) {
190 unset($beUser->uc[$key]);
191 }
192 }
193 $this->tempDataIsCleared = TRUE;
194 } elseif ($d['save']) {
195 // Save all submitted values if they are no array (arrays are with table=be_users) and exists in $GLOBALS['TYPO3_USER_SETTINGS'][columns]
196 foreach ($columns as $field => $config) {
197 if (!in_array($field, $fieldList)) {
198 continue;
199 }
200 if ($config['table']) {
201 if ($config['table'] === 'be_users' && !in_array($field, array('password', 'password2', 'passwordCurrent', 'email', 'realName', 'admin'))) {
202 if (!isset($config['access']) || $this->checkAccess($config) && $beUser->user[$field] !== $d['be_users'][$field]) {
203 if ($config['type'] === 'check') {
204 $fieldValue = isset($d['be_users'][$field]) ? 1 : 0;
205 } else {
206 $fieldValue = $d['be_users'][$field];
207 }
208 $storeRec['be_users'][$beUserId][$field] = $fieldValue;
209 $beUser->user[$field] = $fieldValue;
210 }
211 }
212 }
213 if ($config['type'] === 'check') {
214 $beUser->uc[$field] = isset($d[$field]) ? 1 : 0;
215 } else {
216 $beUser->uc[$field] = htmlspecialchars($d[$field]);
217 }
218 }
219 // Personal data for the users be_user-record (email, name, password...)
220 // If email and name is changed, set it in the users record:
221 $be_user_data = $d['be_users'];
222 // Possibility to modify the transmitted values. Useful to do transformations, like RSA password decryption
223 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/setup/mod/index.php']['modifyUserDataBeforeSave'])) {
224 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/setup/mod/index.php']['modifyUserDataBeforeSave'] as $function) {
225 $params = array('be_user_data' => &$be_user_data);
226 GeneralUtility::callUserFunction($function, $params, $this);
227 }
228 }
229 $this->passwordIsSubmitted = (string)$be_user_data['password'] !== '';
230 $passwordIsConfirmed = $this->passwordIsSubmitted && $be_user_data['password'] === $be_user_data['password2'];
231 // Update the real name:
232 if ($be_user_data['realName'] !== $beUser->user['realName']) {
233 $beUser->user['realName'] = ($storeRec['be_users'][$beUserId]['realName'] = substr($be_user_data['realName'], 0, 80));
234 }
235 // Update the email address:
236 if ($be_user_data['email'] !== $beUser->user['email']) {
237 $beUser->user['email'] = ($storeRec['be_users'][$beUserId]['email'] = substr($be_user_data['email'], 0, 80));
238 }
239 // Update the password:
240 if ($passwordIsConfirmed) {
241 $currentPasswordHashed = $GLOBALS['BE_USER']->user['password'];
242 $saltFactory = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($currentPasswordHashed);
243 if ($saltFactory->checkPassword($be_user_data['passwordCurrent'], $currentPasswordHashed)) {
244 $this->passwordIsUpdated = self::PASSWORD_UPDATED;
245 $storeRec['be_users'][$beUserId]['password'] = $be_user_data['password'];
246 } else {
247 $this->passwordIsUpdated = self::PASSWORD_OLD_WRONG;
248 }
249 } else {
250 $this->passwordIsUpdated = self::PASSWORD_NOT_THE_SAME;
251 }
252 $this->saveData = TRUE;
253 }
254 // Inserts the overriding values.
255 $beUser->overrideUC();
256 $save_after = md5(serialize($beUser->uc));
257 // If something in the uc-array of the user has changed, we save the array...
258 if ($save_before != $save_after) {
259 $beUser->writeUC($beUser->uc);
260 $beUser->writelog(254, 1, 0, 1, 'Personal settings changed', array());
261 $this->setupIsUpdated = TRUE;
262 }
263 // If the temporary data has been cleared, lets make a log note about it
264 if ($this->tempDataIsCleared) {
265 $beUser->writelog(254, 1, 0, 1, $this->getLanguageService()->getLL('tempDataClearedLog'), array());
266 }
267 // Persist data if something has changed:
268 if (count($storeRec) && $this->saveData) {
269 // Make instance of TCE for storing the changes.
270 $tce = GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
271 $tce->stripslashes_values = 0;
272 // This is so the user can actually update his user record.
273 $isAdmin = $beUser->user['admin'];
274 $beUser->user['admin'] = 1;
275 $tce->start($storeRec, array(), $beUser);
276 // This is to make sure that the users record can be updated even if in another workspace. This is tolerated.
277 $tce->bypassWorkspaceRestrictions = TRUE;
278 $tce->process_datamap();
279 unset($tce);
280 if ($this->passwordIsUpdated === self::PASSWORD_NOT_UPDATED || count($storeRec['be_users'][$beUserId]) > 1) {
281 $this->setupIsUpdated = TRUE;
282 }
283 // Restore admin status after processing
284 $beUser->user['admin'] = $isAdmin;
285 }
286 }
287 }
288
289 /******************************
290 *
291 * Rendering module
292 *
293 ******************************/
294 /**
295 * Initializes the module for display of the settings form.
296 *
297 * @return void
298 */
299 public function init() {
300 $this->getLanguageService()->includeLLFile('EXT:setup/mod/locallang.xlf');
301
302 // Returns the script user - that is the REAL logged in user! ($GLOBALS[BE_USER] might be another user due to simulation!)
303 $scriptUser = $this->getRealScriptUserObj();
304
305 $this->isAdmin = $scriptUser->isAdmin();
306 // Getting the 'override' values as set might be set in User TSconfig
307 $this->overrideConf = $this->getBackendUser()->getTSConfigProp('setup.override');
308 // Getting the disabled fields might be set in User TSconfig (eg setup.fields.password.disabled=1)
309 $this->tsFieldConf = $this->getBackendUser()->getTSConfigProp('setup.fields');
310 // id password is disabled, disable repeat of password too (password2)
311 if (isset($this->tsFieldConf['password.']) && $this->tsFieldConf['password.']['disabled']) {
312 $this->tsFieldConf['password2.']['disabled'] = 1;
313 $this->tsFieldConf['passwordCurrent.']['disabled'] = 1;
314 }
315 // Create instance of object for output of data
316 $this->doc = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
317 $this->doc->backPath = $GLOBALS['BACK_PATH'];
318 $this->doc->setModuleTemplate('EXT:setup/Resources/Private/Templates/setup.html');
319 $this->doc->form = '<form action="' . BackendUtility::getModuleUrl('user_setup') . '" method="post" name="usersetup" enctype="application/x-www-form-urlencoded">';
320 $this->doc->addStyleSheet('module', 'sysext/setup/Resources/Public/Styles/styles.css');
321 $this->doc->JScode .= $this->getJavaScript();
322 }
323
324 /**
325 * Generate necessary JavaScript
326 *
327 * @return string
328 */
329 protected function getJavaScript() {
330 $javaScript = '';
331 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/setup/mod/index.php']['setupScriptHook'])) {
332 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/setup/mod/index.php']['setupScriptHook'] as $function) {
333 $params = array();
334 $javaScript .= GeneralUtility::callUserFunction($function, $params, $this);
335 }
336 }
337 return $javaScript;
338 }
339
340 /**
341 * Generate the main settings form:
342 *
343 * @return void
344 */
345 public function main() {
346 if ($this->languageUpdate) {
347 $this->doc->JScodeArray['languageUpdate'] .= '
348 if (top.refreshMenu) {
349 top.refreshMenu();
350 } else {
351 top.TYPO3ModuleMenu.refreshMenu();
352 }
353 ';
354 }
355 if ($this->pagetreeNeedsRefresh) {
356 BackendUtility::setUpdateSignal('updatePageTree');
357 }
358 // Start page:
359 $this->doc->loadJavascriptLib('sysext/backend/Resources/Public/JavaScript/md5.js');
360 // Use a wrapper div
361 $this->content .= '<div id="user-setup-wrapper">';
362 // Load available backend modules
363 $this->loadModules = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Module\ModuleLoader::class);
364 $this->loadModules->observeWorkspaces = TRUE;
365 $this->loadModules->load($GLOBALS['TBE_MODULES']);
366 $this->content .= $this->doc->header($this->getLanguageService()->getLL('UserSettings'));
367 // Show if setup was saved
368 if ($this->setupIsUpdated && !$this->tempDataIsCleared && !$this->settingsAreResetToDefault) {
369 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('setupWasUpdated'), $this->getLanguageService()->getLL('UserSettings'));
370 $this->content .= $flashMessage->render();
371 }
372 // Show if temporary data was cleared
373 if ($this->tempDataIsCleared) {
374 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('tempDataClearedFlashMessage'), $this->getLanguageService()->getLL('tempDataCleared'));
375 $this->content .= $flashMessage->render();
376 }
377 // Show if temporary data was cleared
378 if ($this->settingsAreResetToDefault) {
379 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('settingsAreReset'), $this->getLanguageService()->getLL('resetConfiguration'));
380 $this->content .= $flashMessage->render();
381 }
382 // Notice
383 if ($this->setupIsUpdated || $this->settingsAreResetToDefault) {
384 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('activateChanges'), '', FlashMessage::INFO);
385 $this->content .= $flashMessage->render();
386 }
387 // If password is updated, output whether it failed or was OK.
388 if ($this->passwordIsSubmitted) {
389 $flashMessage = NULL;
390 switch ($this->passwordIsUpdated) {
391 case self::PASSWORD_OLD_WRONG:
392 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('oldPassword_failed'), $this->getLanguageService()->getLL('newPassword'), FlashMessage::ERROR);
393 break;
394 case self::PASSWORD_NOT_THE_SAME:
395 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('newPassword_failed'), $this->getLanguageService()->getLL('newPassword'), FlashMessage::ERROR);
396 break;
397 case self::PASSWORD_UPDATED:
398 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('newPassword_ok'), $this->getLanguageService()->getLL('newPassword'));
399 break;
400 }
401 if ($flashMessage) {
402 $this->content .= $flashMessage->render();
403 }
404 }
405
406 // Render user switch
407 $this->content .= $this->renderSimulateUserSelectAndLabel();
408
409 // Render the menu items
410 $menuItems = $this->renderUserSetup();
411 $this->content .= $this->doc->getDynamicTabMenu($menuItems, 'user-setup', 1, FALSE, FALSE);
412 $formToken = $this->formProtection->generateToken('BE user setup', 'edit');
413 $this->content .= $this->doc->section('', '<input type="hidden" name="simUser" value="' . $this->simUser . '" />
414 <input type="hidden" name="formToken" value="' . $formToken . '" />
415 <input type="hidden" value="1" name="data[save]" />
416 <input type="hidden" name="data[setValuesToDefault]" value="0" id="setValuesToDefault" />
417 <input type="hidden" name="data[clearSessionVars]" value="0" id="clearSessionVars" />');
418 // End of wrapper div
419 $this->content .= '</div>';
420 // Setting up the buttons and markers for docheader
421 $docHeaderButtons = $this->getButtons();
422 $markers['CSH'] = $docHeaderButtons['csh'];
423 $markers['CONTENT'] = $this->content;
424 // Build the <body> for the module
425 $this->content = $this->doc->moduleBody($this->pageinfo, $docHeaderButtons, $markers);
426 // Renders the module page
427 $this->content = $this->doc->render($this->getLanguageService()->getLL('UserSettings'), $this->content);
428 }
429
430 /**
431 * Prints the content / ends page
432 *
433 * @return void
434 */
435 public function printContent() {
436 echo $this->content;
437 }
438
439 /**
440 * Create the panel of buttons for submitting the form or otherwise perform operations.
441 *
442 * @return array All available buttons as an assoc. array
443 */
444 protected function getButtons() {
445 $buttons = array(
446 'csh' => '',
447 'save' => '',
448 'shortcut' => ''
449 );
450 $buttons['csh'] = BackendUtility::cshItem('_MOD_user_setup', '');
451 $buttons['save'] = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-document-save', array('html' => '<input type="image" name="data[save]" class="c-inputButton" src="clear.gif" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:rm.saveDoc', TRUE) . '" />'));
452 if ($this->getBackendUser()->mayMakeShortcut()) {
453 $buttons['shortcut'] = $this->doc->makeShortcutIcon('', '', $this->moduleName);
454 }
455 return $buttons;
456 }
457
458 /******************************
459 *
460 * Render module
461 *
462 ******************************/
463
464 /**
465 * renders the data for all tabs in the user setup and returns
466 * everything that is needed with tabs and dyntab menu
467 *
468 * @return array Ready to use for the dyntabmenu itemarray
469 */
470 protected function renderUserSetup() {
471 $result = array();
472 $firstTabLabel = '';
473 $code = array();
474 $i = 0;
475 $fieldArray = $this->getFieldsFromShowItem();
476 $tabLabel = '';
477 foreach ($fieldArray as $fieldName) {
478 $more = '';
479 if (substr($fieldName, 0, 8) === '--div--;') {
480 if ($firstTabLabel === '') {
481 // First tab
482 $tabLabel = $this->getLabel(substr($fieldName, 8), '', FALSE);
483 $firstTabLabel = $tabLabel;
484 } else {
485 $result[] = array(
486 'label' => $tabLabel,
487 'content' => count($code) ? implode(LF, $code) : ''
488 );
489 $tabLabel = $this->getLabel(substr($fieldName, 8), '', FALSE);
490 $i = 0;
491 $code = array();
492 }
493 continue;
494 }
495 $config = $GLOBALS['TYPO3_USER_SETTINGS']['columns'][$fieldName];
496
497 // Field my be disabled in setup.fields
498 if (isset($this->tsFieldConf[$fieldName . '.']['disabled']) && $this->tsFieldConf[$fieldName . '.']['disabled'] == 1) {
499 continue;
500 }
501 if (isset($config['access']) && !$this->checkAccess($config)) {
502 continue;
503 }
504 $label = $this->getLabel($config['label'], $fieldName);
505 $label = $this->getCSH($config['csh'] ?: $fieldName, $label);
506 $type = $config['type'];
507 $class = $config['class'];
508
509 if ($type !== 'check') {
510 $class .= ' form-control';
511 }
512
513 $style = $config['style'];
514 if ($class) {
515 $more .= ' class="' . $class . '"';
516 }
517 if ($style) {
518 $more .= ' style="' . $style . '"';
519 }
520 if (isset($this->overrideConf[$fieldName])) {
521 $more .= ' disabled="disabled"';
522 }
523 $value = $config['table'] === 'be_users' ? $this->getBackendUser()->user[$fieldName] : $this->getBackendUser()->uc[$fieldName];
524 if (!$value && isset($config['default'])) {
525 $value = $config['default'];
526 }
527 $dataAdd = '';
528 if ($config['table'] === 'be_users') {
529 $dataAdd = '[be_users]';
530 }
531
532 switch ($type) {
533 case 'text':
534 case 'email':
535 case 'password': {
536 $noAutocomplete = '';
537 if ($type === 'password') {
538 $value = '';
539 $noAutocomplete = 'autocomplete="off" ';
540 }
541 $html = '<input id="field_' . $fieldName . '"
542 type="' . $type . '"
543 name="data' . $dataAdd . '[' . $fieldName . ']" ' .
544 $noAutocomplete .
545 'value="' . htmlspecialchars($value) . '" ' .
546 $more .
547 ' />';
548 break;
549 }
550 case 'check': {
551 $html = $label . '<div class="checkbox"><label><input id="field_' . $fieldName . '"
552 type="checkbox"
553 name="data' . $dataAdd . '[' . $fieldName . ']"' .
554 ($value ? ' checked="checked"' : '') .
555 $more .
556 ' /></label></div>';
557 $label = '';
558 break;
559 }
560 case 'select': {
561 if ($config['itemsProcFunc']) {
562 $html = GeneralUtility::callUserFunction($config['itemsProcFunc'], $config, $this, '');
563 } else {
564 $html = '<select id="field_' . $fieldName . '"
565 name="data' . $dataAdd . '[' . $fieldName . ']"' .
566 $more . '>' . LF;
567 foreach ($config['items'] as $key => $optionLabel) {
568 $html .= '<option value="' . $key . '"' . ($value == $key ? ' selected="selected"' : '') . '>' . $this->getLabel($optionLabel, '', FALSE) . '</option>' . LF;
569 }
570 $html .= '</select>';
571 }
572 break;
573 }
574 case 'user': {
575 $html = GeneralUtility::callUserFunction($config['userFunc'], $config, $this, '');
576 break;
577 }
578 case 'button': {
579 if ($config['onClick']) {
580 $onClick = $config['onClick'];
581 if ($config['onClickLabels']) {
582 foreach ($config['onClickLabels'] as $key => $labelclick) {
583 $config['onClickLabels'][$key] = $this->getLabel($labelclick, '', FALSE);
584 }
585 $onClick = vsprintf($onClick, $config['onClickLabels']);
586 }
587 $html = '<br><input class="btn btn-default" type="button"
588 value="' . $this->getLabel($config['buttonlabel'], '', FALSE) . '"
589 onclick="' . $onClick . '" />';
590 }
591 break;
592 }
593 default:
594 $html = '';
595 }
596
597 $code[] = '<div class="form-section"><div class="form-group">' .
598 $label .
599 $html .
600 '</div></div>';
601 }
602
603 $result[] = array(
604 'label' => $tabLabel,
605 'content' => count($code) ? implode(LF, $code) : ''
606 );
607 return $result;
608 }
609
610 /******************************
611 *
612 * Helper functions
613 *
614 ******************************/
615 /**
616 * Returns the backend user object, either the global OR the $this->OLD_BE_USER which is set during simulate-user operation.
617 * Anyway: The REAL user is returned - the one logged in.
618 *
619 * @return BackendUserAuthentication The REAL user is returned - the one logged in.
620 */
621 protected function getRealScriptUserObj() {
622 return is_object($this->OLD_BE_USER) ? $this->OLD_BE_USER : $this->getBackendUser();
623 }
624
625 /**
626 * Return a select with available languages
627 *
628 * @return string Complete select as HTML string or warning box if something went wrong.
629 */
630 public function renderLanguageSelect($params, $pObj) {
631 $languageOptions = array();
632 // Compile the languages dropdown
633 $langDefault = $this->getLanguageService()->getLL('lang_default', TRUE);
634 $languageOptions[$langDefault] = '<option value=""' . ($this->getBackendUser()->uc['lang'] === '' ? ' selected="selected"' : '') . '>' . $langDefault . '</option>';
635 // Traverse the number of languages
636 /** @var $locales \TYPO3\CMS\Core\Localization\Locales */
637 $locales = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\Locales::class);
638 $languages = $locales->getLanguages();
639 foreach ($languages as $locale => $name) {
640 if ($locale !== 'default') {
641 $defaultName = isset($GLOBALS['LOCAL_LANG']['default']['lang_' . $locale]) ? $GLOBALS['LOCAL_LANG']['default']['lang_' . $locale][0]['source'] : $name;
642 $localizedName = $this->getLanguageService()->getLL('lang_' . $locale, TRUE);
643 if ($localizedName === '') {
644 $localizedName = htmlspecialchars($name);
645 }
646 $localLabel = ' - [' . htmlspecialchars($defaultName) . ']';
647 $available = is_dir(PATH_typo3conf . 'l10n/' . $locale) ? TRUE : FALSE;
648 if ($available) {
649 $languageOptions[$defaultName] = '<option value="' . $locale . '"' . ($this->getBackendUser()->uc['lang'] === $locale ? ' selected="selected"' : '') . '>' . $localizedName . $localLabel . '</option>';
650 }
651 }
652 }
653 ksort($languageOptions);
654 $languageCode = '
655 <select id="field_lang" name="data[lang]" class="form-control">' . implode('', $languageOptions) . '
656 </select>';
657 if ($this->getBackendUser()->uc['lang'] && !@is_dir((PATH_typo3conf . 'l10n/' . $this->getBackendUser()->uc['lang']))) {
658 $languageUnavailableWarning = 'The selected language "' . $this->getLanguageService()->getLL(('lang_' . $this->getBackendUser()->uc['lang']), TRUE) . '" is not available before the language files are installed.<br />' . ($this->getBackendUser()->isAdmin() ? 'You can use the Language module to easily download new language files.' : 'Please ask your system administrator to do this.');
659 $languageUnavailableMessage = GeneralUtility::makeInstance(FlashMessage::class, $languageUnavailableWarning, '', FlashMessage::WARNING);
660 $languageCode = $languageUnavailableMessage->render() . $languageCode;
661 }
662 return $languageCode;
663 }
664
665 /**
666 * Returns a select with all modules for startup
667 *
668 * @return string Complete select as HTML string
669 */
670 public function renderStartModuleSelect($params, $pObj) {
671 // Start module select
672 if (empty($this->getBackendUser()->uc['startModule'])) {
673 $this->getBackendUser()->uc['startModule'] = $this->getBackendUser()->uc_default['startModule'];
674 }
675 $startModuleSelect = '<option value=""></option>';
676 foreach ($pObj->loadModules->modules as $mainMod => $modData) {
677 if (isset($modData['sub']) && is_array($modData['sub'])) {
678 $startModuleSelect .= '<option disabled="disabled">' . $this->getLanguageService()->moduleLabels['tabs'][($mainMod . '_tab')] . '</option>';
679 foreach ($modData['sub'] as $subKey => $subData) {
680 $modName = $subData['name'];
681 $startModuleSelect .= '<option value="' . $modName . '"' . ($this->getBackendUser()->uc['startModule'] == $modName ? ' selected="selected"' : '') . '>';
682 $startModuleSelect .= ' - ' . $this->getLanguageService()->moduleLabels['tabs'][($modName . '_tab')] . '</option>';
683 }
684 }
685 }
686 return '<select id="field_startModule" name="data[startModule]" class="form-control">' . $startModuleSelect . '</select>';
687 }
688
689 /**
690 * Will make the simulate-user selector if the logged in user is administrator.
691 * It will also set the GLOBAL(!) BE_USER to the simulated user selected if any (and set $this->OLD_BE_USER to logged in user)
692 *
693 * @return void
694 */
695 public function simulateUser() {
696 // If admin, allow simulation of another user
697 $this->simUser = 0;
698 $this->simulateSelector = '';
699 unset($this->OLD_BE_USER);
700 if ($this->getBackendUser()->isAdmin()) {
701 $this->simUser = (int)GeneralUtility::_GP('simUser');
702 // Make user-selector:
703 $users = BackendUtility::getUserNames('username,usergroup,usergroup_cached_list,uid,realName', BackendUtility::BEenableFields('be_users'));
704 $opt = array();
705 foreach ($users as $rr) {
706 if ($rr['uid'] != $this->getBackendUser()->user['uid']) {
707 $label = htmlspecialchars(($rr['username'] . ($rr['realName'] ? ' (' . $rr['realName'] . ')' : '')));
708 $opt[] = '<option value="' . $rr['uid'] . '"' . ($this->simUser == $rr['uid'] ? ' selected="selected"' : '') . '>' . $label . '</option>';
709 }
710 }
711 if (count($opt)) {
712 $this->simulateSelector = '<select id="field_simulate" name="simulateUser" onchange="window.location.href=' . GeneralUtility::quoteJSvalue(BackendUtility::getModuleUrl('user_setup') . '&simUser=') . '+this.options[this.selectedIndex].value;"><option></option>' . implode('', $opt) . '</select>';
713 }
714 }
715 // This can only be set if the previous code was executed.
716 if ($this->simUser > 0) {
717 // Save old user...
718 $this->OLD_BE_USER = $this->getBackendUser();
719 unset($GLOBALS['BE_USER']);
720 // Unset current
721 // New backend user object
722 $BE_USER = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Authentication\BackendUserAuthentication::class);
723 $BE_USER->setBeUserByUid($this->simUser);
724 $BE_USER->fetchGroupData();
725 $BE_USER->backendSetUC();
726 // Must do this, because unsetting $BE_USER before apparently unsets the reference to the global variable by this name!
727 $GLOBALS['BE_USER'] = $BE_USER;
728 }
729 }
730
731 /**
732 * Render simulate user select and label
733 *
734 * @return string
735 */
736 protected function renderSimulateUserSelectAndLabel() {
737 if ($this->simulateSelector === '') {
738 return '';
739 }
740
741 return '<p>' .
742 '<label for="field_simulate" style="margin-right: 20px;">' .
743 $this->getLanguageService()->sL('LLL:EXT:setup/mod/locallang.xlf:simulate') .
744 '</label>' .
745 $this->simulateSelector .
746 '</p>';
747 }
748
749 /**
750 * Returns access check (currently only "admin" is supported)
751 *
752 * @param array $config Configuration of the field, access mode is defined in key 'access'
753 * @return bool Whether it is allowed to modify the given field
754 */
755 protected function checkAccess(array $config) {
756 $access = $config['access'];
757 // Check for hook
758 $accessObject = GeneralUtility::getUserObj($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['setup']['accessLevelCheck'][$access] . ':&' . $access);
759 if (is_object($accessObject) && method_exists($accessObject, 'accessLevelCheck')) {
760 // Initialize vars. If method fails, $set will be set to FALSE
761 return $accessObject->accessLevelCheck($config);
762 } elseif ($access == 'admin') {
763 return $this->isAdmin;
764 }
765
766 return FALSE;
767 }
768
769 /**
770 * Returns the label $str from getLL() and grays out the value if the $str/$key is found in $this->overrideConf array
771 *
772 * @param string $str Locallang key
773 * @param string $key Alternative override-config key
774 * @param bool $addLabelTag Defines whether the string should be wrapped in a <label> tag.
775 * @param string $altLabelTagId Alternative id for use in "for" attribute of <label> tag. By default the $str key is used prepended with "field_".
776 * @return string HTML output.
777 */
778 protected function getLabel($str, $key = '', $addLabelTag = TRUE, $altLabelTagId = '') {
779 if (substr($str, 0, 4) === 'LLL:') {
780 $out = $this->getLanguageService()->sL($str);
781 } else {
782 $out = htmlspecialchars($str);
783 }
784 if (isset($this->overrideConf[$key ?: $str])) {
785 $out = '<span style="color:#999999">' . $out . '</span>';
786 }
787 if ($addLabelTag) {
788 $out = '<label for="' . ($altLabelTagId ?: 'field_' . $key) . '">' . $out . '</label>';
789 }
790 return $out;
791 }
792
793 /**
794 * Returns the CSH Icon for given string
795 *
796 * @param string $str Locallang key
797 * @param string $label The label to be used, that should be wrapped in help
798 * @return string HTML output.
799 */
800 protected function getCSH($str, $label) {
801 $context = '_MOD_user_setup';
802 $field = $str;
803 $strParts = explode(':', $str);
804 if (count($strParts) > 1) {
805 // Setting comes from another extension
806 $context = $strParts[0];
807 $field = $strParts[1];
808 } elseif (!GeneralUtility::inList('language,simuser,reset', $str)) {
809 $field = 'option_' . $str;
810 }
811 return BackendUtility::wrapInHelp($context, $field, $label);
812 }
813
814 /**
815 * Returns array with fields defined in $GLOBALS['TYPO3_USER_SETTINGS']['showitem']
816 *
817 * @return array Array with fieldnames visible in form
818 */
819 protected function getFieldsFromShowItem() {
820 $fieldList = $GLOBALS['TYPO3_USER_SETTINGS']['showitem'];
821 // Disable fields depended on settings
822 if (!$GLOBALS['TYPO3_CONF_VARS']['BE']['RTEenabled']) {
823 $fieldList = GeneralUtility::rmFromList('edit_RTE', $fieldList);
824 }
825 $fieldArray = GeneralUtility::trimExplode(',', $fieldList, TRUE);
826 return $fieldArray;
827 }
828
829 /**
830 * Returns the current BE user.
831 *
832 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
833 */
834 protected function getBackendUser() {
835 return $GLOBALS['BE_USER'];
836 }
837
838 /**
839 * Returns LanguageService
840 *
841 * @return \TYPO3\CMS\Lang\LanguageService
842 */
843 protected function getLanguageService() {
844 return $GLOBALS['LANG'];
845 }
846
847 }