[BUGFIX] Do not override hidden UC fields in user settings
[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 Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Backend\Backend\Avatar\DefaultAvatarProvider;
20 use TYPO3\CMS\Backend\Module\AbstractModule;
21 use TYPO3\CMS\Backend\Module\ModuleLoader;
22 use TYPO3\CMS\Backend\Utility\BackendUtility;
23 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
24 use TYPO3\CMS\Core\Database\ConnectionPool;
25 use TYPO3\CMS\Core\DataHandling\DataHandler;
26 use TYPO3\CMS\Core\FormProtection\FormProtectionFactory;
27 use TYPO3\CMS\Core\Imaging\Icon;
28 use TYPO3\CMS\Core\Imaging\IconFactory;
29 use TYPO3\CMS\Core\Messaging\FlashMessage;
30 use TYPO3\CMS\Core\Messaging\FlashMessageService;
31 use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
32 use TYPO3\CMS\Core\Resource\ResourceFactory;
33 use TYPO3\CMS\Core\Utility\GeneralUtility;
34
35 /**
36 * Script class for the Setup module
37 */
38 class SetupModuleController extends AbstractModule
39 {
40 /**
41 * Flag if password has not been updated
42 */
43 const PASSWORD_NOT_UPDATED = 0;
44
45 /**
46 * Flag if password has been updated
47 */
48 const PASSWORD_UPDATED = 1;
49
50 /**
51 * Flag if both new passwords do not match
52 */
53 const PASSWORD_NOT_THE_SAME = 2;
54
55 /**
56 * Flag if the current password given was not identical to the real
57 * current password
58 */
59 const PASSWORD_OLD_WRONG = 3;
60
61 /**
62 * @var array
63 */
64 public $MOD_MENU = [];
65
66 /**
67 * @var array
68 */
69 public $MOD_SETTINGS = [];
70
71 /**
72 * @var string
73 */
74 public $content;
75
76 /**
77 * @var array
78 */
79 public $overrideConf;
80
81 /**
82 * backend user object, set during simulate-user operation
83 *
84 * @var \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
85 */
86 public $OLD_BE_USER;
87
88 /**
89 * @var bool
90 */
91 public $languageUpdate;
92
93 /**
94 * @var bool
95 */
96 protected $pagetreeNeedsRefresh = false;
97
98 /**
99 * @var bool
100 */
101 protected $isAdmin;
102
103 /**
104 * @var array
105 */
106 protected $tsFieldConf;
107
108 /**
109 * @var bool
110 */
111 protected $saveData = false;
112
113 /**
114 * @var int
115 */
116 protected $passwordIsUpdated = self::PASSWORD_NOT_UPDATED;
117
118 /**
119 * @var bool
120 */
121 protected $passwordIsSubmitted = false;
122
123 /**
124 * @var bool
125 */
126 protected $setupIsUpdated = false;
127
128 /**
129 * @var bool
130 */
131 protected $settingsAreResetToDefault = false;
132
133 /**
134 * Form protection instance
135 *
136 * @var \TYPO3\CMS\Core\FormProtection\BackendFormProtection
137 */
138 protected $formProtection;
139
140 /**
141 * @var string
142 */
143 protected $simulateSelector = '';
144
145 /**
146 * @var int
147 */
148 protected $simUser;
149
150 /**
151 * The name of the module
152 *
153 * @var string
154 */
155 protected $moduleName = 'user_setup';
156
157 /**
158 * @var ModuleLoader
159 */
160 protected $loadModules;
161
162 /**
163 * Instantiate the form protection before a simulated user is initialized.
164 */
165 public function __construct()
166 {
167 parent::__construct();
168 $this->formProtection = FormProtectionFactory::get();
169 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Modal');
170 }
171
172 /**
173 * Getter for the form protection instance.
174 *
175 * @return \TYPO3\CMS\Core\FormProtection\BackendFormProtection
176 */
177 public function getFormProtection()
178 {
179 return $this->formProtection;
180 }
181
182 /**
183 * If settings are submitted to _POST[DATA], store them
184 * NOTICE: This method is called before the \TYPO3\CMS\Backend\Template\ModuleTemplate
185 * is included. See bottom of document.
186 */
187 public function storeIncomingData()
188 {
189 // First check if something is submitted in the data-array from POST vars
190 $d = GeneralUtility::_POST('data');
191 $columns = $GLOBALS['TYPO3_USER_SETTINGS']['columns'];
192 $beUser = $this->getBackendUser();
193 $beUserId = $beUser->user['uid'];
194 $storeRec = [];
195 $fieldList = $this->getFieldsFromShowItem();
196 if (is_array($d) && $this->formProtection->validateToken((string)GeneralUtility::_POST('formToken'), 'BE user setup', 'edit')) {
197 // UC hashed before applying changes
198 $save_before = md5(serialize($beUser->uc));
199 // PUT SETTINGS into the ->uc array:
200 // Reload left frame when switching BE language
201 if (isset($d['lang']) && $d['lang'] != $beUser->uc['lang']) {
202 $this->languageUpdate = true;
203 }
204 // Reload pagetree if the title length is changed
205 if (isset($d['titleLen']) && $d['titleLen'] !== $beUser->uc['titleLen']) {
206 $this->pagetreeNeedsRefresh = true;
207 }
208 if ($d['setValuesToDefault']) {
209 // If every value should be default
210 $beUser->resetUC();
211 $this->settingsAreResetToDefault = true;
212 } elseif ($d['save']) {
213 // Save all submitted values if they are no array (arrays are with table=be_users) and exists in $GLOBALS['TYPO3_USER_SETTINGS'][columns]
214 foreach ($columns as $field => $config) {
215 if (!in_array($field, $fieldList)) {
216 continue;
217 }
218 if ($config['table']) {
219 if ($config['table'] === 'be_users' && !in_array($field, ['password', 'password2', 'passwordCurrent', 'email', 'realName', 'admin', 'avatar'])) {
220 if (!isset($config['access']) || $this->checkAccess($config) && $beUser->user[$field] !== $d['be_users'][$field]) {
221 if ($config['type'] === 'check') {
222 $fieldValue = isset($d['be_users'][$field]) ? 1 : 0;
223 } else {
224 $fieldValue = $d['be_users'][$field];
225 }
226 $storeRec['be_users'][$beUserId][$field] = $fieldValue;
227 $beUser->user[$field] = $fieldValue;
228 }
229 }
230 }
231 if ($config['type'] === 'check') {
232 $beUser->uc[$field] = isset($d[$field]) ? 1 : 0;
233 } else {
234 $beUser->uc[$field] = htmlspecialchars($d[$field]);
235 }
236 }
237 // Personal data for the users be_user-record (email, name, password...)
238 // If email and name is changed, set it in the users record:
239 $be_user_data = $d['be_users'];
240 // Possibility to modify the transmitted values. Useful to do transformations, like RSA password decryption
241 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/setup/mod/index.php']['modifyUserDataBeforeSave'])) {
242 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/setup/mod/index.php']['modifyUserDataBeforeSave'] as $function) {
243 $params = ['be_user_data' => &$be_user_data];
244 GeneralUtility::callUserFunction($function, $params, $this);
245 }
246 }
247 $this->passwordIsSubmitted = (string)$be_user_data['password'] !== '';
248 $passwordIsConfirmed = $this->passwordIsSubmitted && $be_user_data['password'] === $be_user_data['password2'];
249 // Update the real name:
250 if ($be_user_data['realName'] !== $beUser->user['realName']) {
251 $beUser->user['realName'] = ($storeRec['be_users'][$beUserId]['realName'] = substr($be_user_data['realName'], 0, 80));
252 }
253 // Update the email address:
254 if ($be_user_data['email'] !== $beUser->user['email']) {
255 $beUser->user['email'] = ($storeRec['be_users'][$beUserId]['email'] = substr($be_user_data['email'], 0, 80));
256 }
257 // Update the password:
258 if ($passwordIsConfirmed) {
259 $currentPasswordHashed = $GLOBALS['BE_USER']->user['password'];
260 $saltFactory = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($currentPasswordHashed);
261 if ($saltFactory->checkPassword($be_user_data['passwordCurrent'], $currentPasswordHashed)) {
262 $this->passwordIsUpdated = self::PASSWORD_UPDATED;
263 $storeRec['be_users'][$beUserId]['password'] = $be_user_data['password'];
264 } else {
265 $this->passwordIsUpdated = self::PASSWORD_OLD_WRONG;
266 }
267 } else {
268 $this->passwordIsUpdated = self::PASSWORD_NOT_THE_SAME;
269 }
270
271 $this->setAvatarFileUid($beUserId, $be_user_data['avatar'], $storeRec);
272
273 $this->saveData = true;
274 }
275 // Inserts the overriding values.
276 $beUser->overrideUC();
277 $save_after = md5(serialize($beUser->uc));
278 // If something in the uc-array of the user has changed, we save the array...
279 if ($save_before != $save_after) {
280 $beUser->writeUC($beUser->uc);
281 $beUser->writelog(254, 1, 0, 1, 'Personal settings changed', []);
282 $this->setupIsUpdated = true;
283 }
284 // Persist data if something has changed:
285 if (!empty($storeRec) && $this->saveData) {
286 // Make instance of TCE for storing the changes.
287 /** @var DataHandler $dataHandler */
288 $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
289 // This is so the user can actually update his user record.
290 $isAdmin = $beUser->user['admin'];
291 $beUser->user['admin'] = 1;
292 $dataHandler->start($storeRec, [], $beUser);
293 // This is to make sure that the users record can be updated even if in another workspace. This is tolerated.
294 $dataHandler->bypassWorkspaceRestrictions = true;
295 $dataHandler->process_datamap();
296 unset($tce);
297 if ($this->passwordIsUpdated === self::PASSWORD_NOT_UPDATED || count($storeRec['be_users'][$beUserId]) > 1) {
298 $this->setupIsUpdated = true;
299 }
300 // Restore admin status after processing
301 $beUser->user['admin'] = $isAdmin;
302 }
303 }
304 }
305
306 /******************************
307 *
308 * Rendering module
309 *
310 ******************************/
311 /**
312 * Initializes the module for display of the settings form.
313 *
314 * @return void
315 */
316 public function init()
317 {
318 $this->getLanguageService()->includeLLFile('EXT:setup/Resources/Private/Language/locallang.xlf');
319
320 // Returns the script user - that is the REAL logged in user! ($GLOBALS[BE_USER] might be another user due to simulation!)
321 $scriptUser = $this->getRealScriptUserObj();
322
323 $this->isAdmin = $scriptUser->isAdmin();
324 // Getting the 'override' values as set might be set in User TSconfig
325 $this->overrideConf = $this->getBackendUser()->getTSConfigProp('setup.override');
326 // Getting the disabled fields might be set in User TSconfig (eg setup.fields.password.disabled=1)
327 $this->tsFieldConf = $this->getBackendUser()->getTSConfigProp('setup.fields');
328 // id password is disabled, disable repeat of password too (password2)
329 if (isset($this->tsFieldConf['password.']) && $this->tsFieldConf['password.']['disabled']) {
330 $this->tsFieldConf['password2.']['disabled'] = 1;
331 $this->tsFieldConf['passwordCurrent.']['disabled'] = 1;
332 }
333 }
334
335 /**
336 * Generate necessary JavaScript
337 *
338 * @return string
339 */
340 protected function getJavaScript()
341 {
342 $javaScript = '';
343 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/setup/mod/index.php']['setupScriptHook'])) {
344 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/setup/mod/index.php']['setupScriptHook'] as $function) {
345 $params = [];
346 $javaScript .= GeneralUtility::callUserFunction($function, $params, $this);
347 }
348 }
349 return $javaScript;
350 }
351
352 /**
353 * Generate the main settings form:
354 *
355 * @return void
356 */
357 public function main()
358 {
359 $this->content .= '<form action="' . BackendUtility::getModuleUrl('user_setup') . '" method="post" id="SetupModuleController" name="usersetup" enctype="multipart/form-data">';
360 if ($this->languageUpdate) {
361 $this->moduleTemplate->addJavaScriptCode('languageUpdate', '
362 if (top && top.TYPO3.ModuleMenu.App) {
363 top.TYPO3.ModuleMenu.App.refreshMenu();
364 }
365 ');
366 }
367 if ($this->pagetreeNeedsRefresh) {
368 BackendUtility::setUpdateSignal('updatePageTree');
369 }
370 // Start page:
371 $this->moduleTemplate->getPageRenderer()->addJsFile('EXT:backend/Resources/Public/JavaScript/md5.js');
372 // Use a wrapper div
373 $this->content .= '<div id="user-setup-wrapper">';
374 // Load available backend modules
375 $this->loadModules = GeneralUtility::makeInstance(ModuleLoader::class);
376 $this->loadModules->observeWorkspaces = true;
377 $this->loadModules->load($GLOBALS['TBE_MODULES']);
378 $this->content .= $this->moduleTemplate->header($this->getLanguageService()->getLL('UserSettings'));
379 $this->addFlashMessages();
380
381 // Render user switch
382 $this->content .= $this->renderSimulateUserSelectAndLabel();
383
384 // Render the menu items
385 $menuItems = $this->renderUserSetup();
386 $this->content .= $this->moduleTemplate->getDynamicTabMenu($menuItems, 'user-setup', 1, false, false);
387 $formToken = $this->formProtection->generateToken('BE user setup', 'edit');
388 $this->content .= '<div>';
389 $this->content .= '<input type="hidden" name="simUser" value="' . (int)$this->simUser . '" />
390 <input type="hidden" name="formToken" value="' . htmlspecialchars($formToken) . '" />
391 <input type="hidden" value="1" name="data[save]" />
392 <input type="hidden" name="data[setValuesToDefault]" value="0" id="setValuesToDefault" />';
393 $this->content .= '</div>';
394 // End of wrapper div
395 $this->content .= '</div>';
396 // Setting up the buttons and markers for docheader
397 $this->getButtons();
398 // Build the <body> for the module
399 // Renders the module page
400 $this->moduleTemplate->setContent($this->content);
401 $this->content .= '</form>';
402 }
403
404 /**
405 * Injects the request object for the current request or subrequest
406 * Simply calls main() and init() and writes the content to the response
407 *
408 * @param ServerRequestInterface $request the current request
409 * @param ResponseInterface $response
410 * @return ResponseInterface the response with the content
411 */
412 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
413 {
414 $GLOBALS['SOBE'] = $this;
415 $this->simulateUser();
416 $this->storeIncomingData();
417 $this->init();
418 $this->main();
419
420 $response->getBody()->write($this->moduleTemplate->renderContent());
421 return $response;
422 }
423
424 /**
425 * Create the panel of buttons for submitting the form or otherwise perform operations.
426 */
427 protected function getButtons()
428 {
429 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
430 $cshButton = $buttonBar->makeHelpButton()
431 ->setModuleName('_MOD_user_setup')
432 ->setFieldName('');
433 $buttonBar->addButton($cshButton);
434
435 $saveButton = $buttonBar->makeInputButton()
436 ->setName('data[save]')
437 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:rm.saveDoc'))
438 ->setValue('1')
439 ->setForm('SetupModuleController')
440 ->setShowLabelText(true)
441 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-document-save', Icon::SIZE_SMALL));
442
443 $buttonBar->addButton($saveButton);
444 $shortcutButton = $buttonBar->makeShortcutButton()
445 ->setModuleName($this->moduleName);
446 $buttonBar->addButton($shortcutButton);
447 }
448
449 /******************************
450 *
451 * Render module
452 *
453 ******************************/
454
455 /**
456 * renders the data for all tabs in the user setup and returns
457 * everything that is needed with tabs and dyntab menu
458 *
459 * @return array Ready to use for the dyntabmenu itemarray
460 */
461 protected function renderUserSetup()
462 {
463 $html = '';
464 $result = [];
465 $firstTabLabel = '';
466 $code = [];
467 $fieldArray = $this->getFieldsFromShowItem();
468 $tabLabel = '';
469 foreach ($fieldArray as $fieldName) {
470 $config = $GLOBALS['TYPO3_USER_SETTINGS']['columns'][$fieldName];
471 if (isset($config['access']) && !$this->checkAccess($config)) {
472 continue;
473 }
474
475 if (substr($fieldName, 0, 8) === '--div--;') {
476 if ($firstTabLabel === '') {
477 // First tab
478 $tabLabel = $this->getLabel(substr($fieldName, 8), '', false);
479 $firstTabLabel = $tabLabel;
480 } else {
481 $result[] = [
482 'label' => $tabLabel,
483 'content' => count($code) ? implode(LF, $code) : ''
484 ];
485 $tabLabel = $this->getLabel(substr($fieldName, 8), '', false);
486 $code = [];
487 }
488 continue;
489 }
490 $label = $this->getLabel($config['label'], $fieldName);
491 $label = $this->getCSH($config['csh'] ?: $fieldName, $label);
492 $type = $config['type'];
493 $class = $config['class'];
494 if ($type !== 'check') {
495 $class .= ' form-control';
496 }
497 $more = '';
498 if ($class) {
499 $more .= ' class="' . htmlspecialchars($class) . '"';
500 }
501 $style = $config['style'];
502 if ($style) {
503 $more .= ' style="' . htmlspecialchars($style) . '"';
504 }
505 if (isset($this->overrideConf[$fieldName])) {
506 $more .= ' disabled="disabled"';
507 }
508 $value = $config['table'] === 'be_users' ? $this->getBackendUser()->user[$fieldName] : $this->getBackendUser()->uc[$fieldName];
509 if (!$value && isset($config['default'])) {
510 $value = $config['default'];
511 }
512 $dataAdd = '';
513 if ($config['table'] === 'be_users') {
514 $dataAdd = '[be_users]';
515 }
516
517 switch ($type) {
518 case 'text':
519 case 'email':
520 case 'password':
521 $noAutocomplete = '';
522 if ($type === 'password') {
523 $value = '';
524 $noAutocomplete = 'autocomplete="off" ';
525 $more .= ' data-rsa-encryption=""';
526 }
527 $html = '<input id="field_' . htmlspecialchars($fieldName) . '"
528 type="' . htmlspecialchars($type) . '"
529 name="data' . $dataAdd . '[' . htmlspecialchars($fieldName) . ']" ' .
530 $noAutocomplete .
531 'value="' . htmlspecialchars($value) . '" ' .
532 $more .
533 ' />';
534 break;
535 case 'check':
536 $html = $label . '<div class="checkbox"><label><input id="field_' . htmlspecialchars($fieldName) . '"
537 type="checkbox"
538 name="data' . $dataAdd . '[' . htmlspecialchars($fieldName) . ']"' .
539 ($value ? ' checked="checked"' : '') .
540 $more .
541 ' /></label></div>';
542 $label = '';
543 break;
544 case 'select':
545 if ($config['itemsProcFunc']) {
546 $html = GeneralUtility::callUserFunction($config['itemsProcFunc'], $config, $this);
547 } else {
548 $html = '<select id="field_' . htmlspecialchars($fieldName) . '"
549 name="data' . $dataAdd . '[' . htmlspecialchars($fieldName) . ']"' .
550 $more . '>' . LF;
551 foreach ($config['items'] as $key => $optionLabel) {
552 $html .= '<option value="' . htmlspecialchars($key) . '"' . ($value == $key ? ' selected="selected"' : '') . '>' . $this->getLabel($optionLabel, '', false) . '</option>' . LF;
553 }
554 $html .= '</select>';
555 }
556 break;
557 case 'user':
558 $html = GeneralUtility::callUserFunction($config['userFunc'], $config, $this);
559 break;
560 case 'button':
561 if ($config['onClick']) {
562 $onClick = $config['onClick'];
563 if ($config['onClickLabels']) {
564 foreach ($config['onClickLabels'] as $key => $labelclick) {
565 $config['onClickLabels'][$key] = $this->getLabel($labelclick, '', false);
566 }
567 $onClick = vsprintf($onClick, $config['onClickLabels']);
568 }
569 $html = '<br><input class="btn btn-default" type="button"
570 value="' . $this->getLabel($config['buttonlabel'], '', false) . '"
571 onclick="' . $onClick . '" />';
572 }
573 if (!empty($config['confirm'])) {
574 $confirmData = $config['confirmData'];
575 $html = '<br><input class="btn btn-default t3js-modal-trigger" type="button"'
576 . ' value="' . $this->getLabel($config['buttonlabel'], '', false) . '"'
577 . ' data-href="javascript:' . htmlspecialchars($confirmData['jsCodeAfterOk']) . '"'
578 . ' data-severity="warning"'
579 . ' data-title="' . $this->getLabel($config['label'], '', false) . '"'
580 . ' data-content="' . $this->getLabel($confirmData['message'], '', false) . '" />';
581 }
582 break;
583 case 'avatar':
584 // Get current avatar image
585 $html = '<br>';
586 $avatarFileUid = $this->getAvatarFileUid($this->getBackendUser()->user['uid']);
587
588 if ($avatarFileUid) {
589 $defaultAvatarProvider = GeneralUtility::makeInstance(DefaultAvatarProvider::class);
590 $avatarImage = $defaultAvatarProvider->getImage($this->getBackendUser()->user, 32);
591 if ($avatarImage) {
592 $icon = '<span class="avatar"><span class="avatar-image">' .
593 '<img src="' . htmlspecialchars($avatarImage->getUrl(true)) . '"' .
594 ' width="' . (int)$avatarImage->getWidth() . '" ' .
595 'height="' . (int)$avatarImage->getHeight() . '" />' .
596 '</span></span>';
597 $html .= '<span class="pull-left" style="padding-right: 10px" id="image_' . htmlspecialchars($fieldName) . '">' . $icon . ' </span>';
598 }
599 }
600 $html .= '<input id="field_' . htmlspecialchars($fieldName) . '" type="hidden" ' .
601 'name="data' . $dataAdd . '[' . htmlspecialchars($fieldName) . ']"' . $more .
602 ' value="' . (int)$avatarFileUid . '" />';
603
604 $html .= '<div class="btn-group">';
605 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
606 if ($avatarFileUid) {
607 $html .=
608 '<a id="clear_button_' . htmlspecialchars($fieldName) . '" '
609 . 'onclick="clearExistingImage(); return false;" class="btn btn-default">'
610 . $iconFactory->getIcon('actions-delete', Icon::SIZE_SMALL)
611 . '</a>';
612 }
613 $html .=
614 '<a id="add_button_' . htmlspecialchars($fieldName) . '" class="btn btn-default btn-add-avatar"'
615 . ' onclick="openFileBrowser();return false;">'
616 . $iconFactory->getIcon('actions-insert-record', Icon::SIZE_SMALL)
617 . '</a></div>';
618
619 $this->addAvatarButtonJs($fieldName);
620 break;
621 default:
622 $html = '';
623 }
624
625 $code[] = '<div class="form-section"><div class="row"><div class="form-group col-md-12">' .
626 $label .
627 $html .
628 '</div></div></div>';
629 }
630
631 $result[] = [
632 'label' => $tabLabel,
633 'content' => count($code) ? implode(LF, $code) : ''
634 ];
635 return $result;
636 }
637
638 /******************************
639 *
640 * Helper functions
641 *
642 ******************************/
643 /**
644 * Returns the backend user object, either the global OR the $this->OLD_BE_USER which is set during simulate-user operation.
645 * Anyway: The REAL user is returned - the one logged in.
646 *
647 * @return BackendUserAuthentication The REAL user is returned - the one logged in.
648 */
649 protected function getRealScriptUserObj()
650 {
651 return is_object($this->OLD_BE_USER) ? $this->OLD_BE_USER : $this->getBackendUser();
652 }
653
654 /**
655 * Return a select with available languages
656 *
657 * @return string Complete select as HTML string or warning box if something went wrong.
658 */
659 public function renderLanguageSelect()
660 {
661 $languageOptions = [];
662 // Compile the languages dropdown
663 $langDefault = htmlspecialchars($this->getLanguageService()->getLL('lang_default'));
664 $languageOptions[$langDefault] = '<option value=""' . ($this->getBackendUser()->uc['lang'] === '' ? ' selected="selected"' : '') . '>' . $langDefault . '</option>';
665 // Traverse the number of languages
666 /** @var $locales \TYPO3\CMS\Core\Localization\Locales */
667 $locales = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\Locales::class);
668 $languages = $locales->getLanguages();
669 foreach ($languages as $locale => $name) {
670 if ($locale !== 'default') {
671 $defaultName = isset($GLOBALS['LOCAL_LANG']['default']['lang_' . $locale]) ? $GLOBALS['LOCAL_LANG']['default']['lang_' . $locale][0]['source'] : $name;
672 $localizedName = htmlspecialchars($this->getLanguageService()->getLL('lang_' . $locale));
673 if ($localizedName === '') {
674 $localizedName = htmlspecialchars($name);
675 }
676 $localLabel = ' - [' . htmlspecialchars($defaultName) . ']';
677 $available = is_dir(PATH_typo3conf . 'l10n/' . $locale);
678 if ($available) {
679 $languageOptions[$defaultName] = '<option value="' . $locale . '"' . ($this->getBackendUser()->uc['lang'] === $locale ? ' selected="selected"' : '') . '>' . $localizedName . $localLabel . '</option>';
680 }
681 }
682 }
683 ksort($languageOptions);
684 $languageCode = '
685 <select id="field_lang" name="data[lang]" class="form-control">' . implode('', $languageOptions) . '
686 </select>';
687 if ($this->getBackendUser()->uc['lang'] && !@is_dir((PATH_typo3conf . 'l10n/' . $this->getBackendUser()->uc['lang']))) {
688 // TODO: The text constants have to be moved into language files
689 $languageUnavailableWarning = 'The selected language "' . htmlspecialchars($this->getLanguageService()->getLL('lang_' . $this->getBackendUser()->uc['lang'])) . '" is not available before the language files are installed.&nbsp;&nbsp;<br />&nbsp;&nbsp;' . ($this->getBackendUser()->isAdmin() ? 'You can use the Language module to easily download new language files.' : 'Please ask your system administrator to do this.');
690 $languageCode = '<br /><span class="label label-danger">' . $languageUnavailableWarning . '</span><br /><br />' . $languageCode;
691 }
692 return $languageCode;
693 }
694
695 /**
696 * Returns a select with all modules for startup
697 *
698 * @param array $params
699 * @param SetupModuleController $pObj
700 *
701 * @return string Complete select as HTML string
702 */
703 public function renderStartModuleSelect($params, $pObj)
704 {
705 // Load available backend modules
706 $this->loadModules = GeneralUtility::makeInstance(ModuleLoader::class);
707 $this->loadModules->observeWorkspaces = true;
708 $this->loadModules->load($GLOBALS['TBE_MODULES']);
709 $startModuleSelect = '<option value="">' . htmlspecialchars($this->getLanguageService()->getLL('startModule.firstInMenu')) . '</option>';
710 foreach ($pObj->loadModules->modules as $mainMod => $modData) {
711 if (!empty($modData['sub']) && is_array($modData['sub'])) {
712 $modules = '';
713 foreach ($modData['sub'] as $subData) {
714 $modName = $subData['name'];
715 $modules .= '<option value="' . htmlspecialchars($modName) . '"';
716 $modules .= $this->getBackendUser()->uc['startModule'] === $modName ? ' selected="selected"' : '';
717 $modules .= '>' . htmlspecialchars($this->getLanguageService()->sL($this->loadModules->getLabelsForModule($modName)['title'])) . '</option>';
718 }
719 $groupLabel = htmlspecialchars($this->getLanguageService()->sL($this->loadModules->getLabelsForModule($mainMod)['title']));
720 $startModuleSelect .= '<optgroup label="' . htmlspecialchars($groupLabel) . '">' . $modules . '</optgroup>';
721 }
722 }
723 return '<select id="field_startModule" name="data[startModule]" class="form-control">' . $startModuleSelect . '</select>';
724 }
725
726 /**
727 * Will make the simulate-user selector if the logged in user is administrator.
728 * 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)
729 *
730 * @return void
731 */
732 public function simulateUser()
733 {
734 // If admin, allow simulation of another user
735 $this->simUser = 0;
736 $this->simulateSelector = '';
737 unset($this->OLD_BE_USER);
738 if ($this->getBackendUser()->isAdmin()) {
739 $this->simUser = (int)GeneralUtility::_GP('simUser');
740 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users');
741 $users = $queryBuilder
742 ->select('*')
743 ->from('be_users')
744 ->where(
745 $queryBuilder->expr()->neq('uid', (int)$this->getBackendUser()->user['uid']),
746 $queryBuilder->expr()->notLike('username', $queryBuilder->createNamedParameter($queryBuilder->escapeLikeWildcards('_cli_') . '%'))
747 )
748 ->orderBy('username')
749 ->execute()
750 ->fetchAll();
751 $opt = [];
752 foreach ($users as $rr) {
753 $label = $rr['username'] . ($rr['realName'] ? ' (' . $rr['realName'] . ')' : '');
754 $opt[] = '<option value="' . (int)$rr['uid'] . '"' . ($this->simUser === (int)$rr['uid'] ? ' selected="selected"' : '') . '>' . htmlspecialchars($label) . '</option>';
755 }
756 if (!empty($opt)) {
757 $this->simulateSelector = '<select id="field_simulate" class="form-control" name="simulateUser" onchange="window.location.href=' . GeneralUtility::quoteJSvalue(BackendUtility::getModuleUrl('user_setup') . '&simUser=') . '+this.options[this.selectedIndex].value;"><option></option>' . implode('', $opt) . '</select>';
758 }
759 }
760 // This can only be set if the previous code was executed.
761 if ($this->simUser > 0) {
762 // Save old user...
763 $this->OLD_BE_USER = $this->getBackendUser();
764 unset($GLOBALS['BE_USER']);
765 // Unset current
766 // New backend user object
767 $BE_USER = GeneralUtility::makeInstance(BackendUserAuthentication::class);
768 $BE_USER->setBeUserByUid($this->simUser);
769 $BE_USER->fetchGroupData();
770 $BE_USER->backendSetUC();
771 // Must do this, because unsetting $BE_USER before apparently unsets the reference to the global variable by this name!
772 $GLOBALS['BE_USER'] = $BE_USER;
773 }
774 }
775
776 /**
777 * Render simulate user select and label
778 *
779 * @return string
780 */
781 protected function renderSimulateUserSelectAndLabel()
782 {
783 if ($this->simulateSelector === '') {
784 return '';
785 }
786
787 return '<div class="form-inline"><div class="form-group"><p>'
788 . '<label for="field_simulate" style="margin-right: 20px;">'
789 . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:setup/Resources/Private/Language/locallang.xlf:simulate'))
790 . '</label>'
791 . $this->simulateSelector
792 . '</p></div></div>';
793 }
794
795 /**
796 * Returns access check (currently only "admin" is supported)
797 *
798 * @param array $config Configuration of the field, access mode is defined in key 'access'
799 * @return bool Whether it is allowed to modify the given field
800 */
801 protected function checkAccess(array $config)
802 {
803 $access = $config['access'];
804
805 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['setup']['accessLevelCheck'][$access])) {
806 if (class_exists($access)) {
807 $accessObject = GeneralUtility::makeInstance($access);
808 if (method_exists($accessObject, 'accessLevelCheck')) {
809 // Initialize vars. If method fails, $set will be set to FALSE
810 return $accessObject->accessLevelCheck($config);
811 }
812 }
813 } elseif ($access == 'admin') {
814 return $this->isAdmin;
815 }
816
817 return false;
818 }
819
820 /**
821 * Returns the label $str from getLL() and grays out the value if the $str/$key is found in $this->overrideConf array
822 *
823 * @param string $str Locallang key
824 * @param string $key Alternative override-config key
825 * @param bool $addLabelTag Defines whether the string should be wrapped in a <label> tag.
826 * @return string HTML output.
827 */
828 protected function getLabel($str, $key = '', $addLabelTag = true)
829 {
830 if (substr($str, 0, 4) === 'LLL:') {
831 $out = htmlspecialchars($this->getLanguageService()->sL($str));
832 } else {
833 $out = htmlspecialchars($str);
834 }
835 if (isset($this->overrideConf[$key ?: $str])) {
836 $out = '<span style="color:#999999">' . $out . '</span>';
837 }
838 if ($addLabelTag) {
839 $out = '<label>' . $out . '</label>';
840 }
841 return $out;
842 }
843
844 /**
845 * Returns the CSH Icon for given string
846 *
847 * @param string $str Locallang key
848 * @param string $label The label to be used, that should be wrapped in help
849 * @return string HTML output.
850 */
851 protected function getCSH($str, $label)
852 {
853 $context = '_MOD_user_setup';
854 $field = $str;
855 $strParts = explode(':', $str);
856 if (count($strParts) > 1) {
857 // Setting comes from another extension
858 $context = $strParts[0];
859 $field = $strParts[1];
860 } elseif ($str !== 'language' && $str !== 'simuser' && $str !== 'reset') {
861 $field = 'option_' . $str;
862 }
863 return BackendUtility::wrapInHelp($context, $field, $label);
864 }
865
866 /**
867 * Returns array with fields defined in $GLOBALS['TYPO3_USER_SETTINGS']['showitem']
868 * Remove fields which are disabled by user TSconfig
869 *
870 * @return string[] Array with field names visible in form
871 */
872 protected function getFieldsFromShowItem()
873 {
874 $allowedFields = GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_USER_SETTINGS']['showitem'], true);
875 foreach ($this->tsFieldConf as $fieldName => $userTsFieldConfig) {
876 if (!empty($userTsFieldConfig['disabled'])) {
877 $fieldName = rtrim($fieldName, '.');
878 $key = array_search($fieldName, $allowedFields);
879 if ($key !== false) {
880 unset($allowedFields[$key]);
881 }
882 }
883 }
884 return $allowedFields;
885 }
886
887 /**
888 * Get Avatar fileUid
889 *
890 * @param int $beUserId
891 * @return int
892 */
893 protected function getAvatarFileUid($beUserId)
894 {
895 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file_reference');
896 $file = $queryBuilder
897 ->select('uid_local')
898 ->from('sys_file_reference')
899 ->where(
900 $queryBuilder->expr()->eq('tablenames', $queryBuilder->createNamedParameter('be_users')),
901 $queryBuilder->expr()->eq('fieldname', $queryBuilder->createNamedParameter('avatar')),
902 $queryBuilder->expr()->eq('table_local', $queryBuilder->createNamedParameter('sys_file')),
903 $queryBuilder->expr()->eq('uid_foreign', (int)$beUserId)
904 )
905 ->execute()
906 ->fetchColumn();
907 return (int)$file;
908 }
909
910 /**
911 * Set avatar fileUid for backend user
912 *
913 * @param int $beUserId
914 * @param int $fileUid
915 * @param array $storeRec
916 */
917 protected function setAvatarFileUid($beUserId, $fileUid, array &$storeRec)
918 {
919
920 // Update is only needed when new fileUid is set
921 if ((int)$fileUid === $this->getAvatarFileUid($beUserId)) {
922 return;
923 }
924
925 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file_reference');
926 $queryBuilder->getRestrictions()->removeAll();
927 $queryBuilder
928 ->delete('sys_file_reference')
929 ->where(
930 $queryBuilder->expr()->eq('tablenames', $queryBuilder->createNamedParameter('be_users')),
931 $queryBuilder->expr()->eq('fieldname', $queryBuilder->createNamedParameter('avatar')),
932 $queryBuilder->expr()->eq('table_local', $queryBuilder->createNamedParameter('sys_file')),
933 $queryBuilder->expr()->eq('uid_foreign', (int)$beUserId)
934 )
935 ->execute();
936
937 // Create new reference
938 if ($fileUid) {
939
940 // Get file object
941 try {
942 $file = ResourceFactory::getInstance()->getFileObject($fileUid);
943 } catch (FileDoesNotExistException $e) {
944 $file = false;
945 }
946
947 // Check if user is allowed to use the image (only when not in simulation mode)
948 if ($file && $this->simUser === 0 && !$file->getStorage()->checkFileActionPermission('read', $file)) {
949 $file = false;
950 }
951
952 // Check if extension is allowed
953 if ($file && GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $file->getExtension())) {
954
955 // Create new file reference
956 $storeRec['sys_file_reference']['NEW1234'] = [
957 'uid_local' => (int)$fileUid,
958 'uid_foreign' => (int)$beUserId,
959 'tablenames' => 'be_users',
960 'fieldname' => 'avatar',
961 'pid' => 0,
962 'table_local' => 'sys_file',
963 ];
964 $storeRec['be_users'][(int)$beUserId]['avatar'] = 'NEW1234';
965 }
966 }
967 }
968
969 /**
970 * Add JavaScript to for browse files button
971 *
972 * @param string $fieldName
973 */
974 protected function addAvatarButtonJs($fieldName)
975 {
976 $this->moduleTemplate->addJavaScriptCode('avatar-button', '
977 var browserWin="";
978
979 function openFileBrowser() {
980 var url = ' . GeneralUtility::quoteJSvalue(BackendUtility::getModuleUrl('wizard_element_browser', ['mode' => 'file', 'bparams' => '||||dummy|setFileUid'])) . ';
981 browserWin = window.open(url,"Typo3WinBrowser","height=650,width=800,status=0,menubar=0,resizable=1,scrollbars=1");
982 browserWin.focus();
983 }
984
985 function clearExistingImage() {
986 TYPO3.jQuery(' . GeneralUtility::quoteJSvalue('#image_' . htmlspecialchars($fieldName)) . ').hide();
987 TYPO3.jQuery(' . GeneralUtility::quoteJSvalue('#clear_button_' . htmlspecialchars($fieldName)) . ').hide();
988 TYPO3.jQuery(' . GeneralUtility::quoteJSvalue('#field_' . htmlspecialchars($fieldName)) . ').val(\'\');
989 }
990
991 function setFileUid(field, value, fileUid) {
992 clearExistingImage();
993 TYPO3.jQuery(' . GeneralUtility::quoteJSvalue('#field_' . htmlspecialchars($fieldName)) . ').val(fileUid);
994 TYPO3.jQuery(' . GeneralUtility::quoteJSvalue('#add_button_' . htmlspecialchars($fieldName)) . ').removeClass(\'btn-default\').addClass(\'btn-info\');
995
996 browserWin.close();
997 }
998 ');
999 }
1000
1001 /**
1002 * Returns the current BE user.
1003 *
1004 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
1005 */
1006 protected function getBackendUser()
1007 {
1008 return $GLOBALS['BE_USER'];
1009 }
1010
1011 /**
1012 * Returns LanguageService
1013 *
1014 * @return \TYPO3\CMS\Lang\LanguageService
1015 */
1016 protected function getLanguageService()
1017 {
1018 return $GLOBALS['LANG'];
1019 }
1020
1021 /**
1022 * Add FlashMessages for various actions
1023 *
1024 * @return void
1025 */
1026 protected function addFlashMessages()
1027 {
1028 $flashMessages = [];
1029
1030 // Show if setup was saved
1031 if ($this->setupIsUpdated && !$this->settingsAreResetToDefault) {
1032 $flashMessages[] = $this->getFlashMessage('setupWasUpdated', 'UserSettings');
1033 }
1034
1035 // Show if temporary data was cleared
1036 if ($this->settingsAreResetToDefault) {
1037 $flashMessages[] = $this->getFlashMessage('settingsAreReset', 'resetConfiguration');
1038 }
1039
1040 // Notice
1041 if ($this->setupIsUpdated || $this->settingsAreResetToDefault) {
1042 $flashMessages[] = $this->getFlashMessage('activateChanges', '', FlashMessage::INFO);
1043 }
1044
1045 // If password is updated, output whether it failed or was OK.
1046 if ($this->passwordIsSubmitted) {
1047 $flashMessage = null;
1048 switch ($this->passwordIsUpdated) {
1049 case self::PASSWORD_OLD_WRONG:
1050 $flashMessages[] = $this->getFlashMessage('oldPassword_failed', 'newPassword', FlashMessage::ERROR);
1051 break;
1052 case self::PASSWORD_NOT_THE_SAME:
1053 $flashMessages[] = $this->getFlashMessage('newPassword_failed', 'newPassword', FlashMessage::ERROR);
1054 break;
1055 case self::PASSWORD_UPDATED:
1056 $flashMessages[] = $this->getFlashMessage('newPassword_ok', 'newPassword');
1057 break;
1058 }
1059 }
1060 if (!empty($flashMessages)) {
1061 $this->enqueueFlashMessages($flashMessages);
1062 }
1063 }
1064
1065 /**
1066 * @param array $flashMessages
1067 * @throws \TYPO3\CMS\Core\Exception
1068 * @return void
1069 */
1070 protected function enqueueFlashMessages(array $flashMessages)
1071 {
1072 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
1073 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
1074 foreach ($flashMessages as $flashMessage) {
1075 $defaultFlashMessageQueue->enqueue($flashMessage);
1076 }
1077 }
1078
1079 /**
1080 * @param string $message
1081 * @param string $title
1082 * @param int $severity
1083 * @return FlashMessage
1084 */
1085 protected function getFlashMessage($message, $title, $severity = FlashMessage::OK)
1086 {
1087 $title = !empty($title) ? $this->getLanguageService()->getLL($title) : ' ';
1088 return GeneralUtility::makeInstance(
1089 FlashMessage::class,
1090 $this->getLanguageService()->getLL($message),
1091 $title,
1092 $severity
1093 );
1094 }
1095 }