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