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