‪TYPO3CMS  ‪main
WebServerConfigurationFileService.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 
21 
28 {
29  protected string ‪$webServer;
30  protected string ‪$publicPath;
31 
32  public function ‪__construct()
33  {
34  $this->webServer = $this->‪getWebServer();
35 
37  $this->publicPath = ‪Environment::getPublicPath();
38  } else {
39  $this->publicPath = substr(‪Environment::getPublicPath(), strlen(‪Environment::getProjectPath()) + 1);
40  }
41  }
42 
44  {
45  $changed = false;
46 
47  if ($this->‪isApache()) {
48  $changed = $this->‪addApacheBackendRoutingRewriteRules();
49  } elseif ($this->‪isMicrosoftIis()) {
51  }
52 
53  return $changed;
54  }
55 
56  protected function ‪addApacheBackendRoutingRewriteRules(): bool
57  {
58  $configurationFilename = $this->publicPath . '/.htaccess';
59  $configurationFileContent = $this->‪getConfigurationFileContent($configurationFilename);
60 
61  if ($configurationFileContent === '' || !$this->‪updateNecessary($configurationFileContent)) {
62  return false;
63  }
64 
65  $count = 0;
66  $configurationFileContent = preg_replace(
67  pattern: sprintf('/%s/', implode('\s*', array_map(
68  static fn($s) => preg_quote($s, '/'),
69  [
70  'RewriteCond %{REQUEST_FILENAME} !-d',
71  'RewriteCond %{REQUEST_FILENAME} !-l',
72  'RewriteRule ^typo3/(.*)$ %{ENV:CWD}typo3/index.php [QSA,L]',
73  ]
74  ))),
75  replacement: 'RewriteRule ^typo3/(.*)$ %{ENV:CWD}index.php [QSA,L]',
76  subject: $configurationFileContent,
77  count: $count
78  );
79 
80  $configurationFileContent = str_replace(
81  [
82  '# Stop rewrite processing, if we are in any other known directory',
83  '# Stop rewrite processing, if we are in the typo3/ directory or any other known directory', // v10 style comment
84  '# If the file does not exist but is below /typo3/, redirect to the TYPO3 Backend entry point.',
85  ],
86  [
87  '# Stop rewrite processing, if we are in any known directory',
88  '# Stop rewrite processing, if we are in any known directory',
89  '# If the file does not exist but is below /typo3/, rewrite to the main TYPO3 entry point.',
90  ],
91  $configurationFileContent,
92  $count
93  );
94 
95  // Return FALSE in case no replacement has been done. This might be the
96  // case if already modified versions of the configuration are in place.
97  return $count > 0 && file_put_contents($configurationFilename, $configurationFileContent);
98  }
99 
101  {
102  $configurationFilename = $this->publicPath . '/web.config';
103  $configurationFileContent = $this->‪getConfigurationFileContent($configurationFilename);
104 
105  if ($configurationFileContent === '' || !$this->‪updateNecessary($configurationFileContent)) {
106  return false;
107  }
108 
109  $count = 0;
110  $configurationFileContent = str_replace(
111  [
112  '<rule name="TYPO3 - If the file/directory does not exist but is below /typo3/, redirect to the TYPO3 Backend entry point." stopProcessing="true">',
113  '<action type="Rewrite" url="typo3/index.php" appendQueryString="true" />',
114  ],
115  [
116  '<rule name="TYPO3 - If the file/directory does not exist but is below /typo3/, redirect to the main TYPO3 entry point." stopProcessing="true">',
117  '<action type="Rewrite" url="index.php" appendQueryString="true" />',
118  ],
119  $configurationFileContent,
120  $count
121  );
122 
123  // Return FALSE in case no replacement has been done. This might be the
124  // case if already modified versions of the configuration are in place.
125  return $count > 0 && file_put_contents($configurationFilename, $configurationFileContent);
126  }
127 
134  protected function ‪getConfigurationFileContent(string $filename): string
135  {
136  if (!file_exists($filename) || !is_readable($filename) || !is_writable($filename)) {
137  return '';
138  }
139 
140  return file_get_contents($filename) ?: '';
141  }
142 
153  protected function ‪updateNecessary(string $configurationFileContent): bool
154  {
155  if ($this->‪isApache()) {
156  return str_contains($configurationFileContent, 'RewriteRule ^typo3/(.*)$ %{ENV:CWD}typo3/index.php [QSA,L]');
157  }
158  if ($this->‪isMicrosoftIis()) {
159  return str_contains($configurationFileContent, '<action type="Rewrite" url="typo3/index.php" appendQueryString="true" />');
160  }
161  return false;
162  }
163 
164  protected function ‪isApache(): bool
165  {
166  return str_starts_with($this->webServer, 'Apache');
167  }
168 
169  protected function ‪isMicrosoftIis(): bool
170  {
171  return str_starts_with($this->webServer, 'Microsoft-IIS');
172  }
173 
174  protected function ‪getWebServer(): string
175  {
176  return ‪$_SERVER['SERVER_SOFTWARE'] ?? '';
177  }
178 }
‪TYPO3\CMS\Install\Service\WebServerConfigurationFileService\updateNecessary
‪updateNecessary(string $configurationFileContent)
Definition: WebServerConfigurationFileService.php:153
‪TYPO3\CMS\Install\Service\WebServerConfigurationFileService\getConfigurationFileContent
‪string getConfigurationFileContent(string $filename)
Definition: WebServerConfigurationFileService.php:134
‪TYPO3\CMS\Install\Service\WebServerConfigurationFileService\isApache
‪isApache()
Definition: WebServerConfigurationFileService.php:164
‪TYPO3\CMS\Install\Service\WebServerConfigurationFileService\getWebServer
‪getWebServer()
Definition: WebServerConfigurationFileService.php:174
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\Install\Service\WebServerConfigurationFileService\$webServer
‪string $webServer
Definition: WebServerConfigurationFileService.php:29
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:160
‪TYPO3\CMS\Install\Service\WebServerConfigurationFileService
Definition: WebServerConfigurationFileService.php:28
‪TYPO3\CMS\Install\Service\WebServerConfigurationFileService\addMicrosoftIisBackendRoutingRewriteRules
‪addMicrosoftIisBackendRoutingRewriteRules()
Definition: WebServerConfigurationFileService.php:100
‪TYPO3\CMS\Install\Service\WebServerConfigurationFileService\isMicrosoftIis
‪isMicrosoftIis()
Definition: WebServerConfigurationFileService.php:169
‪$_SERVER
‪$_SERVER['TYPO3_DEPRECATED_ENTRYPOINT']
Definition: legacy-backend.php:20
‪TYPO3\CMS\Install\Service\WebServerConfigurationFileService\addApacheBackendRoutingRewriteRules
‪addApacheBackendRoutingRewriteRules()
Definition: WebServerConfigurationFileService.php:56
‪TYPO3\CMS\Install\Service\WebServerConfigurationFileService\addWebServerSpecificBackendRoutingRewriteRules
‪addWebServerSpecificBackendRoutingRewriteRules()
Definition: WebServerConfigurationFileService.php:43
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Install\Service\WebServerConfigurationFileService\$publicPath
‪string $publicPath
Definition: WebServerConfigurationFileService.php:30
‪TYPO3\CMS\Install\Service
Definition: ClearCacheService.php:16
‪TYPO3\CMS\Install\Service\WebServerConfigurationFileService\__construct
‪__construct()
Definition: WebServerConfigurationFileService.php:32