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