c73d43d815c82ea2750a6c4f4eb50ed438baeb44
[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 $tce->start($storeRec, array(), $beUser);
273 // This is so the user can actually update his user record.
274 $tce->admin = 1;
275 // This is to make sure that the users record can be updated even if in another workspace. This is tolerated.
276 $tce->bypassWorkspaceRestrictions = TRUE;
277 $tce->process_datamap();
278 unset($tce);
279 if ($this->passwordIsUpdated === self::PASSWORD_NOT_UPDATED || count($storeRec['be_users'][$beUserId]) > 1) {
280 $this->setupIsUpdated = TRUE;
281 }
282 }
283 }
284 }
285
286 /******************************
287 *
288 * Rendering module
289 *
290 ******************************/
291 /**
292 * Initializes the module for display of the settings form.
293 *
294 * @return void
295 */
296 public function init() {
297 $this->getLanguageService()->includeLLFile('EXT:setup/mod/locallang.xlf');
298
299 // Returns the script user - that is the REAL logged in user! ($GLOBALS[BE_USER] might be another user due to simulation!)
300 $scriptUser = $this->getRealScriptUserObj();
301
302 $this->isAdmin = $scriptUser->isAdmin();
303 // Getting the 'override' values as set might be set in User TSconfig
304 $this->overrideConf = $this->getBackendUser()->getTSConfigProp('setup.override');
305 // Getting the disabled fields might be set in User TSconfig (eg setup.fields.password.disabled=1)
306 $this->tsFieldConf = $this->getBackendUser()->getTSConfigProp('setup.fields');
307 // id password is disabled, disable repeat of password too (password2)
308 if (isset($this->tsFieldConf['password.']) && $this->tsFieldConf['password.']['disabled']) {
309 $this->tsFieldConf['password2.']['disabled'] = 1;
310 $this->tsFieldConf['passwordCurrent.']['disabled'] = 1;
311 }
312 // Create instance of object for output of data
313 $this->doc = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
314 $this->doc->backPath = $GLOBALS['BACK_PATH'];
315 $this->doc->setModuleTemplate('EXT:setup/Resources/Private/Templates/setup.html');
316 $this->doc->form = '<form action="' . BackendUtility::getModuleUrl('user_setup') . '" method="post" name="usersetup" enctype="application/x-www-form-urlencoded">';
317 $this->doc->addStyleSheet('module', 'sysext/setup/Resources/Public/Styles/styles.css');
318 $this->doc->JScode .= $this->getJavaScript();
319 }
320
321 /**
322 * Generate necessary JavaScript
323 *
324 * @return string
325 */
326 protected function getJavaScript() {
327 $javaScript = '';
328 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/setup/mod/index.php']['setupScriptHook'])) {
329 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/setup/mod/index.php']['setupScriptHook'] as $function) {
330 $params = array();
331 $javaScript .= GeneralUtility::callUserFunction($function, $params, $this);
332 }
333 }
334 return $javaScript;
335 }
336
337 /**
338 * Generate the main settings form:
339 *
340 * @return void
341 */
342 public function main() {
343 if ($this->languageUpdate) {
344 $this->doc->JScodeArray['languageUpdate'] .= '
345 if (top.refreshMenu) {
346 top.refreshMenu();
347 } else {
348 top.TYPO3ModuleMenu.refreshMenu();
349 }
350 ';
351 }
352 if ($this->pagetreeNeedsRefresh) {
353 BackendUtility::setUpdateSignal('updatePageTree');
354 }
355 // Start page:
356 $this->doc->loadJavascriptLib('sysext/backend/Resources/Public/JavaScript/md5.js');
357 // Use a wrapper div
358 $this->content .= '<div id="user-setup-wrapper">';
359 // Load available backend modules
360 $this->loadModules = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Module\ModuleLoader::class);
361 $this->loadModules->observeWorkspaces = TRUE;
362 $this->loadModules->load($GLOBALS['TBE_MODULES']);
363 $this->content .= $this->doc->header($this->getLanguageService()->getLL('UserSettings'));
364 // Show if setup was saved
365 if ($this->setupIsUpdated && !$this->tempDataIsCleared && !$this->settingsAreResetToDefault) {
366 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('setupWasUpdated'), $this->getLanguageService()->getLL('UserSettings'));
367 $this->content .= $flashMessage->render();
368 }
369 // Show if temporary data was cleared
370 if ($this->tempDataIsCleared) {
371 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('tempDataClearedFlashMessage'), $this->getLanguageService()->getLL('tempDataCleared'));
372 $this->content .= $flashMessage->render();
373 }
374 // Show if temporary data was cleared
375 if ($this->settingsAreResetToDefault) {
376 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('settingsAreReset'), $this->getLanguageService()->getLL('resetConfiguration'));
377 $this->content .= $flashMessage->render();
378 }
379 // Notice
380 if ($this->setupIsUpdated || $this->settingsAreResetToDefault) {
381 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('activateChanges'), '', FlashMessage::INFO);
382 $this->content .= $flashMessage->render();
383 }
384 // If password is updated, output whether it failed or was OK.
385 if ($this->passwordIsSubmitted) {
386 $flashMessage = NULL;
387 switch ($this->passwordIsUpdated) {
388 case self::PASSWORD_OLD_WRONG:
389 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('oldPassword_failed'), $this->getLanguageService()->getLL('newPassword'), FlashMessage::ERROR);
390 break;
391 case self::PASSWORD_NOT_THE_SAME:
392 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('newPassword_failed'), $this->getLanguageService()->getLL('newPassword'), FlashMessage::ERROR);
393 break;
394 case self::PASSWORD_UPDATED:
395 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('newPassword_ok'), $this->getLanguageService()->getLL('newPassword'));
396 break;
397 }
398 if ($flashMessage) {
399 $this->content .= $flashMessage->render();
400 }
401 }
402
403 // Render user switch
404 $this->content .= $this->renderSimulateUserSelectAndLabel();
405
406 // Render the menu items
407 $menuItems = $this->renderUserSetup();
408 $this->content .= $this->doc->getDynamicTabMenu($menuItems, 'user-setup', 1, FALSE, FALSE);
409 $formToken = $this->formProtection->generateToken('BE user setup', 'edit');
410 $this->content .= $this->doc->section('', '<input type="hidden" name="simUser" value="' . $this->simUser . '" />
411 <input type="hidden" name="formToken" value="' . $formToken . '" />
412 <input type="hidden" value="1" name="data[save]" />
413 <input type="hidden" name="data[setValuesToDefault]" value="0" id="setValuesToDefault" />
414 <input type="hidden" name="data[clearSessionVars]" value="0" id="clearSessionVars" />');
415 // End of wrapper div
416 $this->content .= '</div>';
417 // Setting up the buttons and markers for docheader
418 $docHeaderButtons = $this->getButtons();
419 $markers['CSH'] = $docHeaderButtons['csh'];
420 $markers['CONTENT'] = $this->content;
421 // Build the <body> for the module
422 $this->content = $this->doc->moduleBody($this->pageinfo, $docHeaderButtons, $markers);
423 // Renders the module page
424 $this->content = $this->doc->render($this->getLanguageService()->getLL('UserSettings'), $this->content);
425 }
426
427 /**
428 * Prints the content / ends page
429 *
430 * @return void
431 */
432 public function printContent() {
433 echo $this->content;
434 }
435
436 /**
437 * Create the panel of buttons for submitting the form or otherwise perform operations.
438 *
439 * @return array All available buttons as an assoc. array
440 */
441 protected function getButtons() {
442 $buttons = array(
443 'csh' => '',
444 'save' => '',
445 'shortcut' => ''
446 );
447 $buttons['csh'] = BackendUtility::cshItem('_MOD_user_setup', '');
448 $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) . '" />'));
449 if ($this->getBackendUser()->mayMakeShortcut()) {
450 $buttons['shortcut'] = $this->doc->makeShortcutIcon('', '', $this->moduleName);
451 }
452 return $buttons;
453 }
454
455 /******************************
456 *
457 * Render module
458 *
459 ******************************/
460
461 /**
462 * renders the data for all tabs in the user setup and returns
463 * everything that is needed with tabs and dyntab menu
464 *
465 * @return array Ready to use for the dyntabmenu itemarray
466 */
467 protected function renderUserSetup() {
468 $result = array();
469 $firstTabLabel = '';
470 $code = array();
471 $i = 0;
472 $fieldArray = $this->getFieldsFromShowItem();
473 $tabLabel = '';
474 foreach ($fieldArray as $fieldName) {
475 $more = '';
476 if (substr($fieldName, 0, 8) === '--div--;') {
477 if ($firstTabLabel === '') {
478 // First tab
479 $tabLabel = $this->getLabel(substr($fieldName, 8), '', FALSE);
480 $firstTabLabel = $tabLabel;
481 } else {
482 $result[] = array(
483 'label' => $tabLabel,
484 'content' => count($code) ? implode(LF, $code) : ''
485 );
486 $tabLabel = $this->getLabel(substr($fieldName, 8), '', FALSE);
487 $i = 0;
488 $code = array();
489 }
490 continue;
491 }
492 $config = $GLOBALS['TYPO3_USER_SETTINGS']['columns'][$fieldName];
493
494 // Field my be disabled in setup.fields
495 if (isset($this->tsFieldConf[$fieldName . '.']['disabled']) && $this->tsFieldConf[$fieldName . '.']['disabled'] == 1) {
496 continue;
497 }
498 if (isset($config['access']) && !$this->checkAccess($config)) {
499 continue;
500 }
501 $label = $this->getLabel($config['label'], $fieldName);
502 $label = $this->getCSH($config['csh'] ?: $fieldName, $label);
503 $type = $config['type'];
504 $class = $config['class'];
505
506 if ($type !== 'check') {
507 $class .= ' form-control';
508 }
509
510 $style = $config['style'];
511 if ($class) {
512 $more .= ' class="' . $class . '"';
513 }
514 if ($style) {
515 $more .= ' style="' . $style . '"';
516 }
517 if (isset($this->overrideConf[$fieldName])) {
518 $more .= ' disabled="disabled"';
519 }
520 $value = $config['table'] === 'be_users' ? $this->getBackendUser()->user[$fieldName] : $this->getBackendUser()->uc[$fieldName];
521 if (!$value && isset($config['default'])) {
522 $value = $config['default'];
523 }
524 $dataAdd = '';
525 if ($config['table'] === 'be_users') {
526 $dataAdd = '[be_users]';
527 }
528
529 switch ($type) {
530 case 'text':
531 case 'email':
532 case 'password': {
533 $noAutocomplete = '';
534 if ($type === 'password') {
535 $value = '';
536 $noAutocomplete = 'autocomplete="off" ';
537 }
538 $html = '<input id="field_' . $fieldName . '"
539 type="' . $type . '"
540 name="data' . $dataAdd . '[' . $fieldName . ']" ' .
541 $noAutocomplete .
542 'value="' . htmlspecialchars($value) . '" ' .
543 $more .
544 ' />';
545 break;
546 }
547 case 'check': {
548 $html = $label . '<div class="checkbox"><label><input id="field_' . $fieldName . '"
549 type="checkbox"
550 name="data' . $dataAdd . '[' . $fieldName . ']"' .
551 ($value ? ' checked="checked"' : '') .
552 $more .
553 ' /></label></div>';
554 $label = '';
555 break;
556 }
557 case 'select': {
558 if ($config['itemsProcFunc']) {
559 $html = GeneralUtility::callUserFunction($config['itemsProcFunc'], $config, $this, '');
560 } else {
561 $html = '<select id="field_' . $fieldName . '"
562 name="data' . $dataAdd . '[' . $fieldName . ']"' .
563 $more . '>' . LF;
564 foreach ($config['items'] as $key => $optionLabel) {
565 $html .= '<option value="' . $key . '"' . ($value == $key ? ' selected="selected"' : '') . '>' . $this->getLabel($optionLabel, '', FALSE) . '</option>' . LF;
566 }
567 $html .= '</select>';
568 }
569 break;
570 }
571 case 'user': {
572 $html = GeneralUtility::callUserFunction($config['userFunc'], $config, $this, '');
573 break;
574 }
575 case 'button': {
576 if ($config['onClick']) {
577 $onClick = $config['onClick'];
578 if ($config['onClickLabels']) {
579 foreach ($config['onClickLabels'] as $key => $labelclick) {
580 $config['onClickLabels'][$key] = $this->getLabel($labelclick, '', FALSE);
581 }
582 $onClick = vsprintf($onClick, $config['onClickLabels']);
583 }
584 $html = '<br><input class="btn btn-default" type="button"
585 value="' . $this->getLabel($config['buttonlabel'], '', FALSE) . '"
586 onclick="' . $onClick . '" />';
587 }
588 break;
589 }
590 default:
591 $html = '';
592 }
593
594 $code[] = '<div class="form-section"><div class="form-group">' .
595 $label .
596 $html .
597 '</div></div>';
598 }
599
600 $result[] = array(
601 'label' => $tabLabel,
602 'content' => count($code) ? implode(LF, $code) : ''
603 );
604 return $result;
605 }
606
607 /******************************
608 *
609 * Helper functions
610 *
611 ******************************/
612 /**
613 * Returns the backend user object, either the global OR the $this->OLD_BE_USER which is set during simulate-user operation.
614 * Anyway: The REAL user is returned - the one logged in.
615 *
616 * @return BackendUserAuthentication The REAL user is returned - the one logged in.
617 */
618 protected function getRealScriptUserObj() {
619 return is_object($this->OLD_BE_USER) ? $this->OLD_BE_USER : $this->getBackendUser();
620 }
621
622 /**
623 * Return a select with available languages
624 *
625 * @return string Complete select as HTML string or warning box if something went wrong.
626 */
627 public function renderLanguageSelect($params, $pObj) {
628 $languageOptions = array();
629 // Compile the languages dropdown
630 $langDefault = $this->getLanguageService()->getLL('lang_default', TRUE);
631 $languageOptions[$langDefault] = '<option value=""' . ($this->getBackendUser()->uc['lang'] === '' ? ' selected="selected"' : '') . '>' . $langDefault . '</option>';
632 // Traverse the number of languages
633 /** @var $locales \TYPO3\CMS\Core\Localization\Locales */
634 $locales = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\Locales::class);
635 $languages = $locales->getLanguages();
636 foreach ($languages as $locale => $name) {
637 if ($locale !== 'default') {
638 $defaultName = isset($GLOBALS['LOCAL_LANG']['default']['lang_' . $locale]) ? $GLOBALS['LOCAL_LANG']['default']['lang_' . $locale][0]['source'] : $name;
639 $localizedName = $this->getLanguageService()->getLL('lang_' . $locale, TRUE);
640 if ($localizedName === '') {
641 $localizedName = htmlspecialchars($name);
642 }
643 $localLabel = ' - [' . htmlspecialchars($defaultName) . ']';
644 $available = is_dir(PATH_typo3conf . 'l10n/' . $locale) ? TRUE : FALSE;
645 if ($available) {
646 $languageOptions[$defaultName] = '<option value="' . $locale . '"' . ($this->getBackendUser()->uc['lang'] === $locale ? ' selected="selected"' : '') . '>' . $localizedName . $localLabel . '</option>';
647 }
648 }
649 }
650 ksort($languageOptions);
651 $languageCode = '
652 <select id="field_lang" name="data[lang]" class="form-control">' . implode('', $languageOptions) . '
653 </select>';
654 if ($this->getBackendUser()->uc['lang'] && !@is_dir((PATH_typo3conf . 'l10n/' . $this->getBackendUser()->uc['lang']))) {
655 $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.');
656 $languageUnavailableMessage = GeneralUtility::makeInstance(FlashMessage::class, $languageUnavailableWarning, '', FlashMessage::WARNING);
657 $languageCode = $languageUnavailableMessage->render() . $languageCode;
658 }
659 return $languageCode;
660 }
661
662 /**
663 * Returns a select with all modules for startup
664 *
665 * @return string Complete select as HTML string
666 */
667 public function renderStartModuleSelect($params, $pObj) {
668 // Start module select
669 if (empty($this->getBackendUser()->uc['startModule'])) {
670 $this->getBackendUser()->uc['startModule'] = $this->getBackendUser()->uc_default['startModule'];
671 }
672 $startModuleSelect = '<option value=""></option>';
673 foreach ($pObj->loadModules->modules as $mainMod => $modData) {
674 if (isset($modData['sub']) && is_array($modData['sub'])) {
675 $startModuleSelect .= '<option disabled="disabled">' . $this->getLanguageService()->moduleLabels['tabs'][($mainMod . '_tab')] . '</option>';
676 foreach ($modData['sub'] as $subKey => $subData) {
677 $modName = $subData['name'];
678 $startModuleSelect .= '<option value="' . $modName . '"' . ($this->getBackendUser()->uc['startModule'] == $modName ? ' selected="selected"' : '') . '>';
679 $startModuleSelect .= ' - ' . $this->getLanguageService()->moduleLabels['tabs'][($modName . '_tab')] . '</option>';
680 }
681 }
682 }
683 return '<select id="field_startModule" name="data[startModule]" class="form-control">' . $startModuleSelect . '</select>';
684 }
685
686 /**
687 * Will make the simulate-user selector if the logged in user is administrator.
688 * 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)
689 *
690 * @return void
691 */
692 public function simulateUser() {
693 // If admin, allow simulation of another user
694 $this->simUser = 0;
695 $this->simulateSelector = '';
696 unset($this->OLD_BE_USER);
697 if ($this->getBackendUser()->isAdmin()) {
698 $this->simUser = (int)GeneralUtility::_GP('simUser');
699 // Make user-selector:
700 $users = BackendUtility::getUserNames('username,usergroup,usergroup_cached_list,uid,realName', BackendUtility::BEenableFields('be_users'));
701 $opt = array();
702 foreach ($users as $rr) {
703 if ($rr['uid'] != $this->getBackendUser()->user['uid']) {
704 $label = htmlspecialchars(($rr['username'] . ($rr['realName'] ? ' (' . $rr['realName'] . ')' : '')));
705 $opt[] = '<option value="' . $rr['uid'] . '"' . ($this->simUser == $rr['uid'] ? ' selected="selected"' : '') . '>' . $label . '</option>';
706 }
707 }
708 if (count($opt)) {
709 $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>';
710 }
711 }
712 // This can only be set if the previous code was executed.
713 if ($this->simUser > 0) {
714 // Save old user...
715 $this->OLD_BE_USER = $this->getBackendUser();
716 unset($GLOBALS['BE_USER']);
717 // Unset current
718 // New backend user object
719 $BE_USER = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Authentication\BackendUserAuthentication::class);
720 $BE_USER->setBeUserByUid($this->simUser);
721 $BE_USER->fetchGroupData();
722 $BE_USER->backendSetUC();
723 // Must do this, because unsetting $BE_USER before apparently unsets the reference to the global variable by this name!
724 $GLOBALS['BE_USER'] = $BE_USER;
725 }
726 }
727
728 /**
729 * Render simulate user select and label
730 *
731 * @return string
732 */
733 protected function renderSimulateUserSelectAndLabel() {
734 if ($this->simulateSelector === '') {
735 return '';
736 }
737
738 return '<p>' .
739 '<label for="field_simulate" style="margin-right: 20px;">' .
740 $this->getLanguageService()->sL('LLL:EXT:setup/mod/locallang.xlf:simulate') .
741 '</label>' .
742 $this->simulateSelector .
743 '</p>';
744 }
745
746 /**
747 * Returns access check (currently only "admin" is supported)
748 *
749 * @param array $config Configuration of the field, access mode is defined in key 'access'
750 * @return bool Whether it is allowed to modify the given field
751 */
752 protected function checkAccess(array $config) {
753 $access = $config['access'];
754 // Check for hook
755 $accessObject = GeneralUtility::getUserObj($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['setup']['accessLevelCheck'][$access] . ':&' . $access);
756 if (is_object($accessObject) && method_exists($accessObject, 'accessLevelCheck')) {
757 // Initialize vars. If method fails, $set will be set to FALSE
758 return $accessObject->accessLevelCheck($config);
759 } elseif ($access == 'admin') {
760 return $this->isAdmin;
761 }
762
763 return FALSE;
764 }
765
766 /**
767 * Returns the label $str from getLL() and grays out the value if the $str/$key is found in $this->overrideConf array
768 *
769 * @param string $str Locallang key
770 * @param string $key Alternative override-config key
771 * @param bool $addLabelTag Defines whether the string should be wrapped in a <label> tag.
772 * @param string $altLabelTagId Alternative id for use in "for" attribute of <label> tag. By default the $str key is used prepended with "field_".
773 * @return string HTML output.
774 */
775 protected function getLabel($str, $key = '', $addLabelTag = TRUE, $altLabelTagId = '') {
776 if (substr($str, 0, 4) === 'LLL:') {
777 $out = $this->getLanguageService()->sL($str);
778 } else {
779 $out = htmlspecialchars($str);
780 }
781 if (isset($this->overrideConf[$key ?: $str])) {
782 $out = '<span style="color:#999999">' . $out . '</span>';
783 }
784 if ($addLabelTag) {
785 $out = '<label for="' . ($altLabelTagId ?: 'field_' . $key) . '">' . $out . '</label>';
786 }
787 return $out;
788 }
789
790 /**
791 * Returns the CSH Icon for given string
792 *
793 * @param string $str Locallang key
794 * @param string $label The label to be used, that should be wrapped in help
795 * @return string HTML output.
796 */
797 protected function getCSH($str, $label) {
798 $context = '_MOD_user_setup';
799 $field = $str;
800 $strParts = explode(':', $str);
801 if (count($strParts) > 1) {
802 // Setting comes from another extension
803 $context = $strParts[0];
804 $field = $strParts[1];
805 } elseif (!GeneralUtility::inList('language,simuser,reset', $str)) {
806 $field = 'option_' . $str;
807 }
808 return BackendUtility::wrapInHelp($context, $field, $label);
809 }
810
811 /**
812 * Returns array with fields defined in $GLOBALS['TYPO3_USER_SETTINGS']['showitem']
813 *
814 * @return array Array with fieldnames visible in form
815 */
816 protected function getFieldsFromShowItem() {
817 $fieldList = $GLOBALS['TYPO3_USER_SETTINGS']['showitem'];
818 // Disable fields depended on settings
819 if (!$GLOBALS['TYPO3_CONF_VARS']['BE']['RTEenabled']) {
820 $fieldList = GeneralUtility::rmFromList('edit_RTE', $fieldList);
821 }
822 $fieldArray = GeneralUtility::trimExplode(',', $fieldList, TRUE);
823 return $fieldArray;
824 }
825
826 /**
827 * Returns the current BE user.
828 *
829 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
830 */
831 protected function getBackendUser() {
832 return $GLOBALS['BE_USER'];
833 }
834
835 /**
836 * Returns LanguageService
837 *
838 * @return \TYPO3\CMS\Lang\LanguageService
839 */
840 protected function getLanguageService() {
841 return $GLOBALS['LANG'];
842 }
843
844 }