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