‪TYPO3CMS  ‪main
SetupService.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use TYPO3\CMS\Core\Configuration\ConfigurationManager;
33 
39 {
40  public function ‪__construct(
41  private readonly ConfigurationManager $configurationManager,
42  private readonly ‪SiteConfiguration $siteConfiguration,
43  ) {}
44 
45  public function ‪setSiteName(string $name): bool
46  {
47  return $this->configurationManager->setLocalConfigurationValueByPath('SYS/sitename', $name);
48  }
49 
54  public function ‪createSiteConfiguration(string ‪$identifier, int $rootPageId, string $siteUrl)
55  {
56  // Create a default site configuration called "main" as best practice
57  $this->siteConfiguration->createNewBasicSite(‪$identifier, $rootPageId, $siteUrl);
58  }
59 
71  private function ‪getHashedPassword(string $password): string
72  {
73  $okHashMethods = [
74  Argon2iPasswordHash::class,
75  Argon2idPasswordHash::class,
76  BcryptPasswordHash::class,
77  ];
78  foreach ($okHashMethods as $className) {
80  $instance = GeneralUtility::makeInstance($className);
81  if ($instance->isAvailable()) {
82  return $instance->getHashedPassword($password);
83  }
84  }
85  // Should never happen since bcrypt is always available
86  throw new ‪InvalidPasswordHashException('No suitable hash method found', 1533988846);
87  }
88 
94  public function ‪createUser(string $username, string $password, string $email = ''): void
95  {
96  $adminUserFields = [
97  'username' => $username,
98  'password' => $this->‪getHashedPassword($password),
99  'email' => GeneralUtility::validEmail($email) ? $email : '',
100  'admin' => 1,
101  'tstamp' => ‪$GLOBALS['EXEC_TIME'],
102  'crdate' => ‪$GLOBALS['EXEC_TIME'],
103  ];
104 
105  $databaseConnection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('be_users');
106  $databaseConnection->insert('be_users', $adminUserFields);
107  $adminUserUid = (int)$databaseConnection->lastInsertId('be_users');
108 
109  $maintainerIds = $this->configurationManager->getConfigurationValueByPath('SYS/systemMaintainers') ?? [];
110  sort($maintainerIds);
111  $maintainerIds[] = $adminUserUid;
112  $this->configurationManager->setLocalConfigurationValuesByPathValuePairs([
113  'SYS/systemMaintainers' => array_unique($maintainerIds),
114  ]);
115  }
116 
117  public function ‪setInstallToolPassword(string $password): bool
118  {
119  return $this->configurationManager->setLocalConfigurationValuesByPathValuePairs([
120  'BE/installToolPassword' => $this->‪getHashedPassword($password),
121  ]);
122  }
123 
127  public function ‪prepareSystemSettings(bool $forceOverwrite = false): void
128  {
129  $configurationFileLocation = $this->configurationManager->getSystemConfigurationFileLocation();
130  if (!$forceOverwrite && @is_file($configurationFileLocation)) {
132  'Configuration file ' . $configurationFileLocation . ' already exists!',
133  1669747685,
134  );
135  }
136 
137  // @todo Remove once LocalConfiguration.php support was dropped.
138  // @todo Web installer creates default configuration based on default factory configuration. Recheck if we
139  // should use this here too instead of an empty array.
140  // Ugly hack to write system/settings.php, to avoid fallback to
141  // LocalConfiguration.php causing issues because it does not exist!
142  @unlink($configurationFileLocation);
143  $this->configurationManager->writeLocalConfiguration([]);
144 
145  // Get best matching configuration presets
146  $featureManager = new ‪FeatureManager();
147  $configurationValues = $featureManager->getBestMatchingConfigurationForAllFeatures();
148  $this->configurationManager->setLocalConfigurationValuesByPathValuePairs($configurationValues);
149 
150  $randomKey = GeneralUtility::makeInstance(Random::class)->generateRandomHexString(96);
151  $this->configurationManager->setLocalConfigurationValueByPath('SYS/encryptionKey', $randomKey);
152  $this->configurationManager->setLocalConfigurationValueByPath('SYS/trustedHostsPattern', '.*.*');
153  }
154 
155  public function ‪createSite(): string
156  {
157  $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
158  $databaseConnectionForPages = $connectionPool->getConnectionForTable('pages');
159  $databaseConnectionForPages->insert(
160  'pages',
161  [
162  'pid' => 0,
163  'crdate' => time(),
164  'tstamp' => time(),
165  'title' => 'Home',
166  'slug' => '/',
167  'doktype' => 1,
168  'is_siteroot' => 1,
169  'perms_userid' => 1,
170  'perms_groupid' => 1,
171  'perms_user' => 31,
172  'perms_group' => 31,
173  'perms_everybody' => 1,
174  ]
175  );
176  $pageUid = $databaseConnectionForPages->lastInsertId('pages');
177 
178  // add a root sys_template with fluid_styled_content and a default PAGE typoscript snippet
179  $connectionPool->getConnectionForTable('sys_template')->insert(
180  'sys_template',
181  [
182  'pid' => $pageUid,
183  'crdate' => time(),
184  'tstamp' => time(),
185  'title' => 'Main TypoScript Rendering',
186  'root' => 1,
187  'clear' => 3,
188  'include_static_file' => 'EXT:fluid_styled_content/Configuration/TypoScript/,EXT:fluid_styled_content/Configuration/TypoScript/Styling/',
189  'constants' => '',
190  'config' => 'page = PAGE
191 page.10 = TEXT
192 page.10.value (
193  <div style="width: 800px; margin: 15% auto;">
194  <div style="width: 300px;">
195  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 150 42"><path d="M60.2 14.4v27h-3.8v-27h-6.7v-3.3h17.1v3.3h-6.6zm20.2 12.9v14h-3.9v-14l-7.7-16.2h4.1l5.7 12.2 5.7-12.2h3.9l-7.8 16.2zm19.5 2.6h-3.6v11.4h-3.8V11.1s3.7-.3 7.3-.3c6.6 0 8.5 4.1 8.5 9.4 0 6.5-2.3 9.7-8.4 9.7m.4-16c-2.4 0-4.1.3-4.1.3v12.6h4.1c2.4 0 4.1-1.6 4.1-6.3 0-4.4-1-6.6-4.1-6.6m21.5 27.7c-7.1 0-9-5.2-9-15.8 0-10.2 1.9-15.1 9-15.1s9 4.9 9 15.1c.1 10.6-1.8 15.8-9 15.8m0-27.7c-3.9 0-5.2 2.6-5.2 12.1 0 9.3 1.3 12.4 5.2 12.4 3.9 0 5.2-3.1 5.2-12.4 0-9.4-1.3-12.1-5.2-12.1m19.9 27.7c-2.1 0-5.3-.6-5.7-.7v-3.1c1 .2 3.7.7 5.6.7 2.2 0 3.6-1.9 3.6-5.2 0-3.9-.6-6-3.7-6H138V24h3.1c3.5 0 3.7-3.6 3.7-5.3 0-3.4-1.1-4.8-3.2-4.8-1.9 0-4.1.5-5.3.7v-3.2c.5-.1 3-.7 5.2-.7 4.4 0 7 1.9 7 8.3 0 2.9-1 5.5-3.3 6.3 2.6.2 3.8 3.1 3.8 7.3 0 6.6-2.5 9-7.3 9"/><path fill="#FF8700" d="M31.7 28.8c-.6.2-1.1.2-1.7.2-5.2 0-12.9-18.2-12.9-24.3 0-2.2.5-3 1.3-3.6C12 1.9 4.3 4.2 1.9 7.2 1.3 8 1 9.1 1 10.6c0 9.5 10.1 31 17.3 31 3.3 0 8.8-5.4 13.4-12.8M28.4.5c6.6 0 13.2 1.1 13.2 4.8 0 7.6-4.8 16.7-7.2 16.7-4.4 0-9.9-12.1-9.9-18.2C24.5 1 25.6.5 28.4.5"/></svg>
196  </div>
197  <h4 style="font-family: sans-serif;">Welcome to a default website made with <a href="https://typo3.org">TYPO3</a></h4>
198  </div>
199 )
200 page.100 = CONTENT
201 page.100 {
202  table = tt_content
203  select {
204  orderBy = sorting
205  where = {#colPos}=0
206  }
207 }
208 ',
209  'description' => 'This is an Empty Site Package TypoScript record.
210 
211 For each website you need a TypoScript record on the main page of your website (on the top level). For better maintenance all TypoScript should be extracted into external files via @import \'EXT:site_myproject/Configuration/TypoScript/setup.typoscript\'',
212  ]
213  );
214 
215  return $pageUid;
216  }
217 }
‪TYPO3\CMS\Core\Crypto\PasswordHashing\BcryptPasswordHash
Definition: BcryptPasswordHash.php:32
‪TYPO3\CMS\Core\Configuration\Exception\SiteConfigurationWriteException
Definition: SiteConfigurationWriteException.php:27
‪TYPO3\CMS\Install\Configuration\FeatureManager
Definition: FeatureManager.php:30
‪TYPO3\CMS\Install\Service\SetupService\__construct
‪__construct(private readonly ConfigurationManager $configurationManager, private readonly SiteConfiguration $siteConfiguration,)
Definition: SetupService.php:40
‪TYPO3\CMS\Core\Crypto\PasswordHashing\InvalidPasswordHashException
Definition: InvalidPasswordHashException.php:25
‪TYPO3\CMS\Install\Service\SetupService\prepareSystemSettings
‪prepareSystemSettings(bool $forceOverwrite=false)
Definition: SetupService.php:127
‪TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException
Definition: ExistingTargetFileNameException.php:23
‪TYPO3\CMS\Install\Service\SetupService
Definition: SetupService.php:39
‪TYPO3\CMS\Core\Configuration\SiteConfiguration
Definition: SiteConfiguration.php:47
‪TYPO3\CMS\Install\Service\SetupService\setInstallToolPassword
‪setInstallToolPassword(string $password)
Definition: SetupService.php:117
‪TYPO3\CMS\Install\Service\SetupService\createUser
‪createUser(string $username, string $password, string $email='')
Definition: SetupService.php:94
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Install\Service\SetupService\setSiteName
‪setSiteName(string $name)
Definition: SetupService.php:45
‪TYPO3\CMS\Install\Service\SetupService\createSite
‪createSite()
Definition: SetupService.php:155
‪TYPO3\CMS\Core\Crypto\Random
Definition: Random.php:27
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:48
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Install\Service\SetupService\createSiteConfiguration
‪createSiteConfiguration(string $identifier, int $rootPageId, string $siteUrl)
Definition: SetupService.php:54
‪TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashInterface
Definition: PasswordHashInterface.php:25
‪TYPO3\CMS\Install\Service
Definition: ClearCacheService.php:16
‪TYPO3\CMS\Core\Crypto\PasswordHashing\Argon2idPasswordHash
Definition: Argon2idPasswordHash.php:31
‪TYPO3\CMS\Core\Crypto\PasswordHashing\Argon2iPasswordHash
Definition: Argon2iPasswordHash.php:31
‪TYPO3\CMS\Install\Service\SetupService\getHashedPassword
‪string getHashedPassword(string $password)
Definition: SetupService.php:71
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37