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