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