83b13d852ed3bee6b639e9970d07a4c1f8afdc59
[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 viev 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->tableLayout = array(
273 'defRow' => array(
274 '0' => array('<td class="td-label">', '</td>'),
275 'defCol' => array('<td valign="top">', '</td>')
276 )
277 );
278 $this->doc->table_TR = '<tr>';
279 $this->doc->table_TABLE = '<table border="0" cellspacing="1" cellpadding="2" class="typo3-usersettings">';
280 $this->doc->JScode .= $this->getJavaScript();
281 }
282
283 /**
284 * Generate necessary JavaScript
285 *
286 * @return string
287 */
288 protected function getJavaScript() {
289 $javaScript = '';
290 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/setup/mod/index.php']['setupScriptHook'])) {
291 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/setup/mod/index.php']['setupScriptHook'] as $function) {
292 $params = array();
293 $javaScript .= GeneralUtility::callUserFunction($function, $params, $this);
294 }
295 }
296 return $javaScript;
297 }
298
299 /**
300 * Generate the main settings formular:
301 *
302 * @return void
303 */
304 public function main() {
305 global $LANG;
306 if ($this->languageUpdate) {
307 $this->doc->JScodeArray['languageUpdate'] .= '
308 if (top.refreshMenu) {
309 top.refreshMenu();
310 } else {
311 top.TYPO3ModuleMenu.refreshMenu();
312 }
313 ';
314 }
315 if ($this->pagetreeNeedsRefresh) {
316 BackendUtility::setUpdateSignal('updatePageTree');
317 }
318 // Start page:
319 $this->doc->loadJavascriptLib('sysext/backend/Resources/Public/JavaScript/md5.js');
320 // Use a wrapper div
321 $this->content .= '<div id="user-setup-wrapper">';
322 // Load available backend modules
323 $this->loadModules = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Module\\ModuleLoader');
324 $this->loadModules->observeWorkspaces = TRUE;
325 $this->loadModules->load($GLOBALS['TBE_MODULES']);
326 $this->content .= $this->doc->header($LANG->getLL('UserSettings'));
327 // Show if setup was saved
328 if ($this->setupIsUpdated && !$this->tempDataIsCleared && !$this->settingsAreResetToDefault) {
329 $flashMessage = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $LANG->getLL('setupWasUpdated'), $LANG->getLL('UserSettings'));
330 $this->content .= $flashMessage->render();
331 }
332 // Show if temporary data was cleared
333 if ($this->tempDataIsCleared) {
334 $flashMessage = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $LANG->getLL('tempDataClearedFlashMessage'), $LANG->getLL('tempDataCleared'));
335 $this->content .= $flashMessage->render();
336 }
337 // Show if temporary data was cleared
338 if ($this->settingsAreResetToDefault) {
339 $flashMessage = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $LANG->getLL('settingsAreReset'), $LANG->getLL('resetConfiguration'));
340 $this->content .= $flashMessage->render();
341 }
342 // Notice
343 if ($this->setupIsUpdated || $this->settingsAreResetToDefault) {
344 $flashMessage = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $LANG->getLL('activateChanges'), '', \TYPO3\CMS\Core\Messaging\FlashMessage::INFO);
345 $this->content .= $flashMessage->render();
346 }
347 // If password is updated, output whether it failed or was OK.
348 if ($this->passwordIsSubmitted) {
349 if ($this->passwordIsUpdated) {
350 $flashMessage = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $LANG->getLL('newPassword_ok'), $LANG->getLL('newPassword'));
351 } else {
352 $flashMessage = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $LANG->getLL('newPassword_failed'), $LANG->getLL('newPassword'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
353 }
354 $this->content .= $flashMessage->render();
355 }
356
357 // Render user switch
358 $this->content .= $this->renderSimulateUserSelectAndLabel();
359
360 // Render the menu items
361 $menuItems = $this->renderUserSetup();
362
363 $this->content .= $this->doc->getDynTabMenu($menuItems, 'user-setup', FALSE, FALSE, 1, FALSE, 1, $this->dividers2tabs);
364 $formToken = $this->formProtection->generateToken('BE user setup', 'edit');
365 $this->content .= $this->doc->section('', '<input type="hidden" name="simUser" value="' . $this->simUser . '" />
366 <input type="hidden" name="formToken" value="' . $formToken . '" />
367 <input type="hidden" value="1" name="data[save]" />
368 <input type="hidden" name="data[setValuesToDefault]" value="0" id="setValuesToDefault" />
369 <input type="hidden" name="data[clearSessionVars]" value="0" id="clearSessionVars" />');
370 // End of wrapper div
371 $this->content .= '</div>';
372 // Setting up the buttons and markers for docheader
373 $docHeaderButtons = $this->getButtons();
374 $markers['CSH'] = $docHeaderButtons['csh'];
375 $markers['CONTENT'] = $this->content;
376 // Build the <body> for the module
377 $this->content = $this->doc->moduleBody($this->pageinfo, $docHeaderButtons, $markers);
378 // Renders the module page
379 $this->content = $this->doc->render($LANG->getLL('UserSettings'), $this->content);
380 }
381
382 /**
383 * Prints the content / ends page
384 *
385 * @return void
386 */
387 public function printContent() {
388 echo $this->content;
389 }
390
391 /**
392 * Create the panel of buttons for submitting the form or otherwise perform operations.
393 *
394 * @return array All available buttons as an assoc. array
395 */
396 protected function getButtons() {
397 $buttons = array(
398 'csh' => '',
399 'save' => '',
400 'shortcut' => ''
401 );
402 $buttons['csh'] = BackendUtility::cshItem('_MOD_user_setup', '', $GLOBALS['BACK_PATH'], '|', TRUE);
403 $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) . '" />'));
404 if ($GLOBALS['BE_USER']->mayMakeShortcut()) {
405 $buttons['shortcut'] = $this->doc->makeShortcutIcon('', '', $this->MCONF['name']);
406 }
407 return $buttons;
408 }
409
410 /******************************
411 *
412 * Render module
413 *
414 ******************************/
415
416 /**
417 * renders the data for all tabs in the user setup and returns
418 * everything that is needed with tabs and dyntab menu
419 *
420 * @return array Ready to use for the dyntabmenu itemarray
421 */
422 protected function renderUserSetup() {
423 $result = array();
424 $firstTabLabel = '';
425 $code = array();
426 $i = 0;
427 $fieldArray = $this->getFieldsFromShowItem();
428 $this->dividers2tabs = isset($GLOBALS['TYPO3_USER_SETTINGS']['ctrl']['dividers2tabs']) ? (int)$GLOBALS['TYPO3_USER_SETTINGS']['ctrl']['dividers2tabs'] : 0;
429 $tabLabel = '';
430 foreach ($fieldArray as $fieldName) {
431 $more = '';
432 if (substr($fieldName, 0, 8) == '--div--;') {
433 if ($firstTabLabel == '') {
434 // First tab
435 $tabLabel = $this->getLabel(substr($fieldName, 8), '', FALSE);
436 $firstTabLabel = $tabLabel;
437 } else {
438 if ($this->dividers2tabs) {
439 $result[] = array(
440 'label' => $tabLabel,
441 'content' => count($code) ? $this->doc->table($code) : ''
442 );
443 $tabLabel = $this->getLabel(substr($fieldName, 8), '', FALSE);
444 $i = 0;
445 $code = array();
446 }
447 }
448 continue;
449 }
450 $config = $GLOBALS['TYPO3_USER_SETTINGS']['columns'][$fieldName];
451 // Field my be disabled in setup.fields
452 if (isset($this->tsFieldConf[$fieldName . '.']['disabled']) && $this->tsFieldConf[$fieldName . '.']['disabled'] == 1) {
453 continue;
454 }
455 if (isset($config['access']) && !$this->checkAccess($config)) {
456 continue;
457 }
458 $label = $this->getLabel($config['label'], $fieldName);
459 $label = $this->getCSH($config['csh'] ?: $fieldName, $label);
460 $type = $config['type'];
461 $eval = $config['eval'];
462 $class = $config['class'];
463 $style = $config['style'];
464 if ($class) {
465 $more .= ' class="' . $class . '"';
466 }
467 if ($style) {
468 $more .= ' style="' . $style . '"';
469 }
470 if (isset($this->overrideConf[$fieldName])) {
471 $more .= ' disabled="disabled"';
472 }
473 $value = $config['table'] == 'be_users' ? $GLOBALS['BE_USER']->user[$fieldName] : $GLOBALS['BE_USER']->uc[$fieldName];
474 if (!$value && isset($config['default'])) {
475 $value = $config['default'];
476 }
477 $dataAdd = '';
478 if ($config['table'] == 'be_users') {
479 $dataAdd = '[be_users]';
480 }
481 switch ($type) {
482 case 'text':
483 case 'password':
484 $noAutocomplete = '';
485 if ($type === 'password') {
486 $value = '';
487 $noAutocomplete = 'autocomplete="off" ';
488 }
489 $html = '<input id="field_' . $fieldName . '"
490 type="' . $type . '"
491 name="data' . $dataAdd . '[' . $fieldName . ']" ' . $noAutocomplete . 'value="' . htmlspecialchars($value) . '" ' . $GLOBALS['TBE_TEMPLATE']->formWidth(20) . $more . ' />';
492 break;
493 case 'check':
494 if (!$class) {
495 $more .= ' class="check"';
496 }
497 $html = '<input id="field_' . $fieldName . '"
498 type="checkbox"
499 name="data' . $dataAdd . '[' . $fieldName . ']"' . ($value ? ' checked="checked"' : '') . $more . ' />';
500 break;
501 case 'select':
502 if (!$class) {
503 $more .= ' class="select"';
504 }
505 if ($config['itemsProcFunc']) {
506 $html = GeneralUtility::callUserFunction($config['itemsProcFunc'], $config, $this, '');
507 } else {
508 $html = '<select ' . $GLOBALS['TBE_TEMPLATE']->formWidth(20) . ' id="field_' . $fieldName . '" name="data' . $dataAdd . '[' . $fieldName . ']"' . $more . '>' . LF;
509 foreach ($config['items'] as $key => $optionLabel) {
510 $html .= '<option value="' . $key . '"' . ($value == $key ? ' selected="selected"' : '') . '>' . $this->getLabel($optionLabel, '', FALSE) . '</option>' . LF;
511 }
512 $html .= '</select>';
513 }
514 break;
515 case 'user':
516 $html = GeneralUtility::callUserFunction($config['userFunc'], $config, $this, '');
517 break;
518 case 'button':
519 if ($config['onClick']) {
520 $onClick = $config['onClick'];
521 if ($config['onClickLabels']) {
522 foreach ($config['onClickLabels'] as $key => $labelclick) {
523 $config['onClickLabels'][$key] = $this->getLabel($labelclick, '', FALSE);
524 }
525 $onClick = vsprintf($onClick, $config['onClickLabels']);
526 }
527 $html = '<input ' . $GLOBALS['TBE_TEMPLATE']->formWidth(20) . ' type="button" value="' . $this->getLabel($config['buttonlabel'], '', FALSE) . '" onclick="' . $onClick . '" />';
528 }
529 break;
530 default:
531 $html = '';
532 }
533 $code[$i][1] = $label;
534 $code[$i++][2] = $html;
535 }
536 if ($this->dividers2tabs == 0) {
537 $tabLabel = $firstTabLabel;
538 }
539 $result[] = array(
540 'label' => $tabLabel,
541 'content' => count($code) ? $this->doc->table($code) : ''
542 );
543 return $result;
544 }
545
546 /******************************
547 *
548 * Helper functions
549 *
550 ******************************/
551 /**
552 * Returns the backend user object, either the global OR the $this->OLD_BE_USER which is set during simulate-user operation.
553 * Anyway: The REAL user is returned - the one logged in.
554 *
555 * @return object The REAL user is returned - the one logged in.
556 */
557 protected function getRealScriptUserObj() {
558 return is_object($this->OLD_BE_USER) ? $this->OLD_BE_USER : $GLOBALS['BE_USER'];
559 }
560
561 /**
562 * Return a select with available languages
563 *
564 * @return string Complete select as HTML string or warning box if something went wrong.
565 */
566 public function renderLanguageSelect($params, $pObj) {
567 $languageOptions = array();
568 // Compile the languages dropdown
569 $langDefault = $GLOBALS['LANG']->getLL('lang_default', TRUE);
570 $languageOptions[$langDefault] = '<option value=""' . ($GLOBALS['BE_USER']->uc['lang'] === '' ? ' selected="selected"' : '') . '>' . $langDefault . '</option>';
571 // Traverse the number of languages
572 /** @var $locales \TYPO3\CMS\Core\Localization\Locales */
573 $locales = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\Locales');
574 $languages = $locales->getLanguages();
575 foreach ($languages as $locale => $name) {
576 if ($locale !== 'default') {
577 $defaultName = isset($GLOBALS['LOCAL_LANG']['default']['lang_' . $locale]) ? $GLOBALS['LOCAL_LANG']['default']['lang_' . $locale][0]['source'] : $name;
578 $localizedName = $GLOBALS['LANG']->getLL('lang_' . $locale, TRUE);
579 if ($localizedName === '') {
580 $localizedName = htmlspecialchars($name);
581 }
582 $localLabel = ' - [' . htmlspecialchars($defaultName) . ']';
583 $available = is_dir(PATH_typo3conf . 'l10n/' . $locale) ? TRUE : FALSE;
584 if ($available) {
585 $languageOptions[$defaultName] = '<option value="' . $locale . '"' . ($GLOBALS['BE_USER']->uc['lang'] === $locale ? ' selected="selected"' : '') . '>' . $localizedName . $localLabel . '</option>';
586 }
587 }
588 }
589 ksort($languageOptions);
590 $languageCode = '
591 <select ' . $GLOBALS['TBE_TEMPLATE']->formWidth(20) . ' id="field_lang" name="data[lang]" class="select">' . implode('', $languageOptions) . '
592 </select>';
593 if ($GLOBALS['BE_USER']->uc['lang'] && !@is_dir((PATH_typo3conf . 'l10n/' . $GLOBALS['BE_USER']->uc['lang']))) {
594 $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.');
595 $languageUnavailableMessage = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $languageUnavailableWarning, '', \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING);
596 $languageCode = $languageUnavailableMessage->render() . $languageCode;
597 }
598 return $languageCode;
599 }
600
601 /**
602 * Returns a select with all modules for startup
603 *
604 * @return string Complete select as HTML string
605 */
606 public function renderStartModuleSelect($params, $pObj) {
607 // Start module select
608 if (empty($GLOBALS['BE_USER']->uc['startModule'])) {
609 $GLOBALS['BE_USER']->uc['startModule'] = $GLOBALS['BE_USER']->uc_default['startModule'];
610 }
611 $startModuleSelect = '<option value=""></option>';
612 foreach ($pObj->loadModules->modules as $mainMod => $modData) {
613 if (isset($modData['sub']) && is_array($modData['sub'])) {
614 $startModuleSelect .= '<option disabled="disabled">' . $GLOBALS['LANG']->moduleLabels['tabs'][($mainMod . '_tab')] . '</option>';
615 foreach ($modData['sub'] as $subKey => $subData) {
616 $modName = $subData['name'];
617 $startModuleSelect .= '<option value="' . $modName . '"' . ($GLOBALS['BE_USER']->uc['startModule'] == $modName ? ' selected="selected"' : '') . '>';
618 $startModuleSelect .= ' - ' . $GLOBALS['LANG']->moduleLabels['tabs'][($modName . '_tab')] . '</option>';
619 }
620 }
621 }
622 return '<select ' . $GLOBALS['TBE_TEMPLATE']->formWidth(20) . 'id="field_startModule" name="data[startModule]" class="select">' . $startModuleSelect . '</select>';
623 }
624
625 /**
626 * Will make the simulate-user selector if the logged in user is administrator.
627 * 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)
628 *
629 * @return void
630 */
631 public function simulateUser() {
632 // If admin, allow simulation of another user
633 $this->simUser = 0;
634 $this->simulateSelector = '';
635 unset($this->OLD_BE_USER);
636 if ($GLOBALS['BE_USER']->isAdmin()) {
637 $this->simUser = (int)GeneralUtility::_GP('simUser');
638 // Make user-selector:
639 $users = BackendUtility::getUserNames('username,usergroup,usergroup_cached_list,uid,realName', BackendUtility::BEenableFields('be_users'));
640 $opt = array();
641 foreach ($users as $rr) {
642 if ($rr['uid'] != $GLOBALS['BE_USER']->user['uid']) {
643 $label = htmlspecialchars(($rr['username'] . ($rr['realName'] ? ' (' . $rr['realName'] . ')' : '')));
644 $opt[] = '<option value="' . $rr['uid'] . '"' . ($this->simUser == $rr['uid'] ? ' selected="selected"' : '') . '>' . $label . '</option>';
645 }
646 }
647 if (count($opt)) {
648 $this->simulateSelector = '<select ' . $GLOBALS['TBE_TEMPLATE']->formWidth(20) . ' id="field_simulate" name="simulateUser" onchange="window.location.href=\'' . BackendUtility::getModuleUrl('user_setup') . '&simUser=\'+this.options[this.selectedIndex].value;"><option></option>' . implode('', $opt) . '</select>';
649 }
650 }
651 // This can only be set if the previous code was executed.
652 if ($this->simUser > 0) {
653 // Save old user...
654 $this->OLD_BE_USER = $GLOBALS['BE_USER'];
655 unset($GLOBALS['BE_USER']);
656 // Unset current
657 // New backend user object
658 $BE_USER = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
659 $BE_USER->OS = TYPO3_OS;
660 $BE_USER->setBeUserByUid($this->simUser);
661 $BE_USER->fetchGroupData();
662 $BE_USER->backendSetUC();
663 // Must do this, because unsetting $BE_USER before apparently unsets the reference to the global variable by this name!
664 $GLOBALS['BE_USER'] = $BE_USER;
665 }
666 }
667
668 /**
669 * Render simulate user select and label
670 *
671 * @return string
672 */
673 protected function renderSimulateUserSelectAndLabel() {
674 if ($this->simulateSelector === '') {
675 return '';
676 }
677
678 return '<p>' .
679 '<label for="field_simulate" style="margin-right: 20px;">' .
680 $GLOBALS['LANG']->sL('LLL:EXT:setup/mod/locallang.xlf:simulate') .
681 '</label>' .
682 $this->simulateSelector .
683 '</p>';
684 }
685
686 /**
687 * Returns access check (currently only "admin" is supported)
688 *
689 * @param array $config Configuration of the field, access mode is defined in key 'access'
690 * @return boolean Whether it is allowed to modify the given field
691 */
692 protected function checkAccess(array $config) {
693 $access = $config['access'];
694 // Check for hook
695 $accessObject = GeneralUtility::getUserObj($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['setup']['accessLevelCheck'][$access] . ':&' . $access);
696 if (is_object($accessObject) && method_exists($accessObject, 'accessLevelCheck')) {
697 // Initialize vars. If method fails, $set will be set to FALSE
698 return $accessObject->accessLevelCheck($config);
699 } elseif ($access == 'admin') {
700 return $this->isAdmin;
701 }
702 }
703
704 /**
705 * Returns the label $str from getLL() and grays out the value if the $str/$key is found in $this->overrideConf array
706 *
707 * @param string $str Locallang key
708 * @param string $key Alternative override-config key
709 * @param bool $addLabelTag Defines whether the string should be wrapped in a <label> tag.
710 * @param string $altLabelTagId Alternative id for use in "for" attribute of <label> tag. By default the $str key is used prepended with "field_".
711 * @return string HTML output.
712 */
713 protected function getLabel($str, $key = '', $addLabelTag = TRUE, $altLabelTagId = '') {
714 if (substr($str, 0, 4) == 'LLL:') {
715 $out = $GLOBALS['LANG']->sL($str);
716 } else {
717 $out = htmlspecialchars($str);
718 }
719 if (isset($this->overrideConf[$key ?: $str])) {
720 $out = '<span style="color:#999999">' . $out . '</span>';
721 }
722 if ($addLabelTag) {
723 $out = '<label for="' . ($altLabelTagId ?: 'field_' . $key) . '">' . $out . '</label>';
724 }
725 return $out;
726 }
727
728 /**
729 * Returns the CSH Icon for given string
730 *
731 * @param string $str Locallang key
732 * @param string $label The label to be used, that should be wrapped in help
733 * @return string HTML output.
734 */
735 protected function getCSH($str, $label) {
736 $context = '_MOD_user_setup';
737 $field = $str;
738 $strParts = explode(':', $str);
739 if (count($strParts) > 1) {
740 // Setting comes from another extension
741 $context = $strParts[0];
742 $field = $strParts[1];
743 } elseif (!GeneralUtility::inList('language,simuser,reset', $str)) {
744 $field = 'option_' . $str;
745 }
746 return BackendUtility::wrapInHelp($context, $field, $label);
747 }
748
749 /**
750 * Returns array with fields defined in $GLOBALS['TYPO3_USER_SETTINGS']['showitem']
751 *
752 * @return array Array with fieldnames visible in form
753 */
754 protected function getFieldsFromShowItem() {
755 $fieldList = $GLOBALS['TYPO3_USER_SETTINGS']['showitem'];
756 // Disable fields depended on settings
757 if (!$GLOBALS['TYPO3_CONF_VARS']['BE']['RTEenabled']) {
758 $fieldList = GeneralUtility::rmFromList('edit_RTE', $fieldList);
759 }
760 $fieldArray = GeneralUtility::trimExplode(',', $fieldList, TRUE);
761 return $fieldArray;
762 }
763
764 }