[TASK] Cleanup CSH calls
[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 $class .= ' form-control';
479
480 $style = $config['style'];
481 if ($class) {
482 $more .= ' class="' . $class . '"';
483 }
484 if ($style) {
485 $more .= ' style="' . $style . '"';
486 }
487 if (isset($this->overrideConf[$fieldName])) {
488 $more .= ' disabled="disabled"';
489 }
490 $value = $config['table'] === 'be_users' ? $GLOBALS['BE_USER']->user[$fieldName] : $GLOBALS['BE_USER']->uc[$fieldName];
491 if (!$value && isset($config['default'])) {
492 $value = $config['default'];
493 }
494 $dataAdd = '';
495 if ($config['table'] === 'be_users') {
496 $dataAdd = '[be_users]';
497 }
498
499 switch ($type) {
500 case 'text':
501 case 'email':
502 case 'password': {
503 $noAutocomplete = '';
504 if ($type === 'password') {
505 $value = '';
506 $noAutocomplete = 'autocomplete="off" ';
507 }
508 $html = '<input id="field_' . $fieldName . '"
509 type="' . $type . '"
510 name="data' . $dataAdd . '[' . $fieldName . ']" ' .
511 $noAutocomplete .
512 'value="' . htmlspecialchars($value) . '" ' .
513 $more .
514 ' />';
515 break;
516 }
517 case 'check': {
518 $html = '<input id="field_' . $fieldName . '"
519 type="checkbox"
520 name="data' . $dataAdd . '[' . $fieldName . ']"' .
521 ($value ? ' checked="checked"' : '') .
522 $more .
523 ' />';
524 break;
525 }
526 case 'select': {
527 if ($config['itemsProcFunc']) {
528 $html = GeneralUtility::callUserFunction($config['itemsProcFunc'], $config, $this, '');
529 } else {
530 $html = '<select id="field_' . $fieldName . '"
531 name="data' . $dataAdd . '[' . $fieldName . ']"' .
532 $more . '>' . LF;
533 foreach ($config['items'] as $key => $optionLabel) {
534 $html .= '<option value="' . $key . '"' . ($value == $key ? ' selected="selected"' : '') . '>' . $this->getLabel($optionLabel, '', FALSE) . '</option>' . LF;
535 }
536 $html .= '</select>';
537 }
538 break;
539 }
540 case 'user': {
541 $html = GeneralUtility::callUserFunction($config['userFunc'], $config, $this, '');
542 break;
543 }
544 case 'button': {
545 if ($config['onClick']) {
546 $onClick = $config['onClick'];
547 if ($config['onClickLabels']) {
548 foreach ($config['onClickLabels'] as $key => $labelclick) {
549 $config['onClickLabels'][$key] = $this->getLabel($labelclick, '', FALSE);
550 }
551 $onClick = vsprintf($onClick, $config['onClickLabels']);
552 }
553 $html = '<br><input type="button"
554 value="' . $this->getLabel($config['buttonlabel'], '', FALSE) . '"
555 onclick="' . $onClick . '" />';
556 }
557 break;
558 }
559 default:
560 $html = '';
561 }
562
563 $code[] = '<div class="form-group">' .
564 $label .
565 $html .
566 '</div>';
567 }
568
569 $result[] = array(
570 'label' => $tabLabel,
571 'content' => count($code) ? implode(LF, $code) : ''
572 );
573 return $result;
574 }
575
576 /******************************
577 *
578 * Helper functions
579 *
580 ******************************/
581 /**
582 * Returns the backend user object, either the global OR the $this->OLD_BE_USER which is set during simulate-user operation.
583 * Anyway: The REAL user is returned - the one logged in.
584 *
585 * @return object The REAL user is returned - the one logged in.
586 */
587 protected function getRealScriptUserObj() {
588 return is_object($this->OLD_BE_USER) ? $this->OLD_BE_USER : $GLOBALS['BE_USER'];
589 }
590
591 /**
592 * Return a select with available languages
593 *
594 * @return string Complete select as HTML string or warning box if something went wrong.
595 */
596 public function renderLanguageSelect($params, $pObj) {
597 $languageOptions = array();
598 // Compile the languages dropdown
599 $langDefault = $GLOBALS['LANG']->getLL('lang_default', TRUE);
600 $languageOptions[$langDefault] = '<option value=""' . ($GLOBALS['BE_USER']->uc['lang'] === '' ? ' selected="selected"' : '') . '>' . $langDefault . '</option>';
601 // Traverse the number of languages
602 /** @var $locales \TYPO3\CMS\Core\Localization\Locales */
603 $locales = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\Locales::class);
604 $languages = $locales->getLanguages();
605 foreach ($languages as $locale => $name) {
606 if ($locale !== 'default') {
607 $defaultName = isset($GLOBALS['LOCAL_LANG']['default']['lang_' . $locale]) ? $GLOBALS['LOCAL_LANG']['default']['lang_' . $locale][0]['source'] : $name;
608 $localizedName = $GLOBALS['LANG']->getLL('lang_' . $locale, TRUE);
609 if ($localizedName === '') {
610 $localizedName = htmlspecialchars($name);
611 }
612 $localLabel = ' - [' . htmlspecialchars($defaultName) . ']';
613 $available = is_dir(PATH_typo3conf . 'l10n/' . $locale) ? TRUE : FALSE;
614 if ($available) {
615 $languageOptions[$defaultName] = '<option value="' . $locale . '"' . ($GLOBALS['BE_USER']->uc['lang'] === $locale ? ' selected="selected"' : '') . '>' . $localizedName . $localLabel . '</option>';
616 }
617 }
618 }
619 ksort($languageOptions);
620 $languageCode = '
621 <select id="field_lang" name="data[lang]" class="form-control">' . implode('', $languageOptions) . '
622 </select>';
623 if ($GLOBALS['BE_USER']->uc['lang'] && !@is_dir((PATH_typo3conf . 'l10n/' . $GLOBALS['BE_USER']->uc['lang']))) {
624 $languageUnavailableWarning = 'The selected language "' . $GLOBALS['LANG']->getLL(('lang_' . $GLOBALS['BE_USER']->uc['lang']), TRUE) . '" is not available before the language pack is installed.<br />' . ($GLOBALS['BE_USER']->isAdmin() ? 'You can use the Extension Manager to easily download and install new language packs.' : 'Please ask your system administrator to do this.');
625 $languageUnavailableMessage = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessage::class, $languageUnavailableWarning, '', \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING);
626 $languageCode = $languageUnavailableMessage->render() . $languageCode;
627 }
628 return $languageCode;
629 }
630
631 /**
632 * Returns a select with all modules for startup
633 *
634 * @return string Complete select as HTML string
635 */
636 public function renderStartModuleSelect($params, $pObj) {
637 // Start module select
638 if (empty($GLOBALS['BE_USER']->uc['startModule'])) {
639 $GLOBALS['BE_USER']->uc['startModule'] = $GLOBALS['BE_USER']->uc_default['startModule'];
640 }
641 $startModuleSelect = '<option value=""></option>';
642 foreach ($pObj->loadModules->modules as $mainMod => $modData) {
643 if (isset($modData['sub']) && is_array($modData['sub'])) {
644 $startModuleSelect .= '<option disabled="disabled">' . $GLOBALS['LANG']->moduleLabels['tabs'][($mainMod . '_tab')] . '</option>';
645 foreach ($modData['sub'] as $subKey => $subData) {
646 $modName = $subData['name'];
647 $startModuleSelect .= '<option value="' . $modName . '"' . ($GLOBALS['BE_USER']->uc['startModule'] == $modName ? ' selected="selected"' : '') . '>';
648 $startModuleSelect .= ' - ' . $GLOBALS['LANG']->moduleLabels['tabs'][($modName . '_tab')] . '</option>';
649 }
650 }
651 }
652 return '<select id="field_startModule" name="data[startModule]" class="form-control">' . $startModuleSelect . '</select>';
653 }
654
655 /**
656 * Will make the simulate-user selector if the logged in user is administrator.
657 * 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)
658 *
659 * @return void
660 */
661 public function simulateUser() {
662 // If admin, allow simulation of another user
663 $this->simUser = 0;
664 $this->simulateSelector = '';
665 unset($this->OLD_BE_USER);
666 if ($GLOBALS['BE_USER']->isAdmin()) {
667 $this->simUser = (int)GeneralUtility::_GP('simUser');
668 // Make user-selector:
669 $users = BackendUtility::getUserNames('username,usergroup,usergroup_cached_list,uid,realName', BackendUtility::BEenableFields('be_users'));
670 $opt = array();
671 foreach ($users as $rr) {
672 if ($rr['uid'] != $GLOBALS['BE_USER']->user['uid']) {
673 $label = htmlspecialchars(($rr['username'] . ($rr['realName'] ? ' (' . $rr['realName'] . ')' : '')));
674 $opt[] = '<option value="' . $rr['uid'] . '"' . ($this->simUser == $rr['uid'] ? ' selected="selected"' : '') . '>' . $label . '</option>';
675 }
676 }
677 if (count($opt)) {
678 $this->simulateSelector = '<select id="field_simulate" name="simulateUser" onchange="window.location.href=\'' . BackendUtility::getModuleUrl('user_setup') . '&simUser=\'+this.options[this.selectedIndex].value;"><option></option>' . implode('', $opt) . '</select>';
679 }
680 }
681 // This can only be set if the previous code was executed.
682 if ($this->simUser > 0) {
683 // Save old user...
684 $this->OLD_BE_USER = $GLOBALS['BE_USER'];
685 unset($GLOBALS['BE_USER']);
686 // Unset current
687 // New backend user object
688 $BE_USER = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Authentication\BackendUserAuthentication::class);
689 $BE_USER->OS = TYPO3_OS;
690 $BE_USER->setBeUserByUid($this->simUser);
691 $BE_USER->fetchGroupData();
692 $BE_USER->backendSetUC();
693 // Must do this, because unsetting $BE_USER before apparently unsets the reference to the global variable by this name!
694 $GLOBALS['BE_USER'] = $BE_USER;
695 }
696 }
697
698 /**
699 * Render simulate user select and label
700 *
701 * @return string
702 */
703 protected function renderSimulateUserSelectAndLabel() {
704 if ($this->simulateSelector === '') {
705 return '';
706 }
707
708 return '<p>' .
709 '<label for="field_simulate" style="margin-right: 20px;">' .
710 $GLOBALS['LANG']->sL('LLL:EXT:setup/mod/locallang.xlf:simulate') .
711 '</label>' .
712 $this->simulateSelector .
713 '</p>';
714 }
715
716 /**
717 * Returns access check (currently only "admin" is supported)
718 *
719 * @param array $config Configuration of the field, access mode is defined in key 'access'
720 * @return bool Whether it is allowed to modify the given field
721 */
722 protected function checkAccess(array $config) {
723 $access = $config['access'];
724 // Check for hook
725 $accessObject = GeneralUtility::getUserObj($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['setup']['accessLevelCheck'][$access] . ':&' . $access);
726 if (is_object($accessObject) && method_exists($accessObject, 'accessLevelCheck')) {
727 // Initialize vars. If method fails, $set will be set to FALSE
728 return $accessObject->accessLevelCheck($config);
729 } elseif ($access == 'admin') {
730 return $this->isAdmin;
731 }
732
733 return FALSE;
734 }
735
736 /**
737 * Returns the label $str from getLL() and grays out the value if the $str/$key is found in $this->overrideConf array
738 *
739 * @param string $str Locallang key
740 * @param string $key Alternative override-config key
741 * @param bool $addLabelTag Defines whether the string should be wrapped in a <label> tag.
742 * @param string $altLabelTagId Alternative id for use in "for" attribute of <label> tag. By default the $str key is used prepended with "field_".
743 * @return string HTML output.
744 */
745 protected function getLabel($str, $key = '', $addLabelTag = TRUE, $altLabelTagId = '') {
746 if (substr($str, 0, 4) === 'LLL:') {
747 $out = $GLOBALS['LANG']->sL($str);
748 } else {
749 $out = htmlspecialchars($str);
750 }
751 if (isset($this->overrideConf[$key ?: $str])) {
752 $out = '<span style="color:#999999">' . $out . '</span>';
753 }
754 if ($addLabelTag) {
755 $out = '<label for="' . ($altLabelTagId ?: 'field_' . $key) . '">' . $out . '</label>';
756 }
757 return $out;
758 }
759
760 /**
761 * Returns the CSH Icon for given string
762 *
763 * @param string $str Locallang key
764 * @param string $label The label to be used, that should be wrapped in help
765 * @return string HTML output.
766 */
767 protected function getCSH($str, $label) {
768 $context = '_MOD_user_setup';
769 $field = $str;
770 $strParts = explode(':', $str);
771 if (count($strParts) > 1) {
772 // Setting comes from another extension
773 $context = $strParts[0];
774 $field = $strParts[1];
775 } elseif (!GeneralUtility::inList('language,simuser,reset', $str)) {
776 $field = 'option_' . $str;
777 }
778 return BackendUtility::wrapInHelp($context, $field, $label);
779 }
780
781 /**
782 * Returns array with fields defined in $GLOBALS['TYPO3_USER_SETTINGS']['showitem']
783 *
784 * @return array Array with fieldnames visible in form
785 */
786 protected function getFieldsFromShowItem() {
787 $fieldList = $GLOBALS['TYPO3_USER_SETTINGS']['showitem'];
788 // Disable fields depended on settings
789 if (!$GLOBALS['TYPO3_CONF_VARS']['BE']['RTEenabled']) {
790 $fieldList = GeneralUtility::rmFromList('edit_RTE', $fieldList);
791 }
792 $fieldArray = GeneralUtility::trimExplode(',', $fieldList, TRUE);
793 return $fieldArray;
794 }
795
796 }