Completed
Push — master ( 7ca602...c87e8e )
by
unknown
14:05
created

SetupModuleController::getButtons()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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