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