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