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