‪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  $newRewriteRule = PHP_EOL . '
66  ### BEGIN: TYPO3 automated migration
67  # If the file/symlink/directory does not exist but is below /typo3/, redirect to the TYPO3 Backend entry point.
68  RewriteCond %{REQUEST_FILENAME} !-f
69  RewriteCond %{REQUEST_FILENAME} !-d
70  RewriteCond %{REQUEST_FILENAME} !-l
71  RewriteRule ^typo3/(.*)$ %{ENV:CWD}typo3/index.php [QSA,L]
72  ### END: TYPO3 automated migration';
73 
74  return (bool)file_put_contents(
75  $configurationFilename,
76  str_replace(
77  '# Stop rewrite processing, if we are in the typo3/ directory or any other known directory',
78  '# Stop rewrite processing, if we are in any other known directory',
80  '/(RewriteRule\s\^\‍(\?\:(typo3\/\|).*\s\[L\])(.*RewriteRule\s\^\.\*\$\s%{ENV:CWD}index\.php\s\[QSA,L\])/s',
81  $newRewriteRule,
82  $configurationFileContent,
83  )
84  )
85  );
86  }
87 
89  {
90  $configurationFilename = $this->publicPath . '/web.config';
91  $configurationFileContent = $this->‪getConfigurationFileContent($configurationFilename);
92 
93  if ($configurationFileContent === '' || !$this->‪updateNecessary($configurationFileContent)) {
94  return false;
95  }
96 
97  $newRewriteRule = '
98  <!-- BEGIN: TYPO3 automated migration -->
99  <rule name="TYPO3 - If the file/directory does not exist but is below /typo3/, redirect to the TYPO3 Backend entry point." stopProcessing="true">
100  <match url="^typo3/(.*)$" ignoreCase="false" />
101  <conditions logicalGrouping="MatchAll">
102  <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
103  <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
104  <add input="{REQUEST_URI}" matchType="Pattern" pattern="^/typo3/.*$" />
105  </conditions>
106  <action type="Rewrite" url="typo3/index.php" appendQueryString="true" />
107  </rule>
108  <!-- END: TYPO3 automated migration -->';
109 
110  return (bool)file_put_contents(
111  $configurationFilename,
113  '/(<match\surl="\^\/\‍((typo3\|).*\‍)\$"\s\/>.+?<\/rule>)(.*<action\stype="Rewrite"\surl="index\.php")/s',
114  $newRewriteRule,
115  $configurationFileContent
116  )
117  );
118  }
119 
126  protected function ‪getConfigurationFileContent(string $filename): string
127  {
128  if (!file_exists($filename) || !is_readable($filename) || !is_writable($filename)) {
129  return '';
130  }
131 
132  return file_get_contents($filename) ?: '';
133  }
134 
145  protected function ‪updateNecessary(string $configurationFileContent): bool
146  {
147  if ($this->‪isApache()) {
148  return (bool)preg_match('/RewriteRule\s\^\‍(\?\:typo3\/\|.*\s\[L\].*RewriteRule\s\^\.\*\$\s%{ENV:CWD}index\.php\s\[QSA,L\]/s', $configurationFileContent)
149  && !str_contains($configurationFileContent, 'RewriteRule ^typo3/(.*)$ %{ENV:CWD}typo3/index.php [QSA,L]');
150  }
151  if ($this->‪isMicrosoftIis()) {
152  return (bool)preg_match('/<match\surl="\^\/\‍(typo3\|.*\‍)\$"\s\/>.*<action\stype="Rewrite"\surl="index.php"\sappendQueryString="true"\s\/>/s', $configurationFileContent)
153  && !str_contains($configurationFileContent, '<action type="Rewrite" url="typo3/index.php" appendQueryString="true" />');
154  }
155  return false;
156  }
157 
176  string $pattern,
177  string $newRewriteRule,
178  string $configurationFileContent
179  ): string {
180  return (string)preg_replace_callback(
181  $pattern,
182  static function ($matches) use ($newRewriteRule) {
183  return str_replace($matches[2], '', ($matches[1] . $newRewriteRule)) . $matches[3];
184  },
185  $configurationFileContent,
186  1
187  );
188  }
189 
190  protected function ‪isApache(): bool
191  {
192  return str_starts_with($this->webServer, 'Apache');
193  }
194 
195  protected function ‪isMicrosoftIis(): bool
196  {
197  return str_starts_with($this->webServer, 'Microsoft-IIS');
198  }
199 
200  protected function ‪getWebServer(): string
201  {
202  return $_SERVER['SERVER_SOFTWARE'] ?? '';
203  }
204 }
‪TYPO3\CMS\Install\Service\WebServerConfigurationFileService\updateNecessary
‪updateNecessary(string $configurationFileContent)
Definition: WebServerConfigurationFileService.php:145
‪TYPO3\CMS\Install\Service\WebServerConfigurationFileService\getConfigurationFileContent
‪string getConfigurationFileContent(string $filename)
Definition: WebServerConfigurationFileService.php:126
‪TYPO3\CMS\Install\Service\WebServerConfigurationFileService\isApache
‪isApache()
Definition: WebServerConfigurationFileService.php:190
‪TYPO3\CMS\Install\Service\WebServerConfigurationFileService\getWebServer
‪getWebServer()
Definition: WebServerConfigurationFileService.php:200
‪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:88
‪TYPO3\CMS\Install\Service\WebServerConfigurationFileService\isMicrosoftIis
‪isMicrosoftIis()
Definition: WebServerConfigurationFileService.php:195
‪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
‪TYPO3\CMS\Install\Service\WebServerConfigurationFileService\performBackendRoutingRewriteRulesUpdate
‪string performBackendRoutingRewriteRulesUpdate(string $pattern, string $newRewriteRule, string $configurationFileContent)
Definition: WebServerConfigurationFileService.php:175