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