Completed
Push — master ( 32062d...ab4613 )
by
unknown
14:20
created

SetupModuleController::getLanguageService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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