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