|
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
|
|
|
|
|
18
|
|
|
namespace TYPO3\CMS\Install\Service; |
|
19
|
|
|
|
|
20
|
|
|
use TYPO3\CMS\Core\Core\Environment; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Handles webserver specific configuration files |
|
24
|
|
|
* |
|
25
|
|
|
* @internal This class is only meant to be used within EXT:install and is not part of the TYPO3 Core API. |
|
26
|
|
|
*/ |
|
27
|
|
|
class WebServerConfigurationFileService |
|
28
|
|
|
{ |
|
29
|
|
|
protected string $webServer; |
|
30
|
|
|
protected string $publicPath; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->webServer = $this->getWebServer(); |
|
35
|
|
|
|
|
36
|
|
|
if (Environment::getPublicPath() === Environment::getProjectPath()) { |
|
37
|
|
|
$this->publicPath = Environment::getPublicPath(); |
|
38
|
|
|
} else { |
|
39
|
|
|
$this->publicPath = substr(Environment::getPublicPath(), strlen(Environment::getProjectPath()) + 1); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function addWebServerSpecificBackendRoutingRewriteRules(): bool |
|
44
|
|
|
{ |
|
45
|
|
|
$changed = false; |
|
46
|
|
|
|
|
47
|
|
|
if ($this->isApache()) { |
|
48
|
|
|
$changed = $this->addApacheBackendRoutingRewriteRules(); |
|
49
|
|
|
} elseif ($this->isMicrosoftIis()) { |
|
50
|
|
|
$changed = $this->addMicrosoftIisBackendRoutingRewriteRules(); |
|
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
|
|
|
RewriteCond %{REQUEST_URI} ^/typo3/.*$ |
|
72
|
|
|
RewriteRule ^typo3/(.*)$ %{ENV:CWD}typo3/index.php [QSA,L] |
|
73
|
|
|
### END: TYPO3 automated migration'; |
|
74
|
|
|
|
|
75
|
|
|
return (bool)file_put_contents( |
|
76
|
|
|
$configurationFilename, |
|
77
|
|
|
str_replace( |
|
78
|
|
|
'# Stop rewrite processing, if we are in the typo3/ directory or any other known directory', |
|
79
|
|
|
'# Stop rewrite processing, if we are in any other known directory', |
|
80
|
|
|
$this->performBackendRoutingRewriteRulesUpdate( |
|
81
|
|
|
'/(RewriteRule\s\^\(\?\:(typo3\/\|).*\s\[L\])(.*RewriteRule\s\^\.\*\$\s%{ENV:CWD}index\.php\s\[QSA,L\])/s', |
|
82
|
|
|
$newRewriteRule, |
|
83
|
|
|
$configurationFileContent, |
|
84
|
|
|
) |
|
85
|
|
|
) |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
protected function addMicrosoftIisBackendRoutingRewriteRules(): bool |
|
90
|
|
|
{ |
|
91
|
|
|
$configurationFilename = $this->publicPath . '/web.config'; |
|
92
|
|
|
$configurationFileContent = $this->getConfigurationFileContent($configurationFilename); |
|
93
|
|
|
|
|
94
|
|
|
if ($configurationFileContent === '' || !$this->updateNecessary($configurationFileContent)) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$newRewriteRule = ' |
|
99
|
|
|
<!-- BEGIN: TYPO3 automated migration --> |
|
100
|
|
|
<rule name="TYPO3 - If the file/directory does not exist but is below /typo3/, redirect to the TYPO3 Backend entry point." stopProcessing="true"> |
|
101
|
|
|
<match url="^typo3/(.*)$" ignoreCase="false" /> |
|
102
|
|
|
<conditions logicalGrouping="MatchAll"> |
|
103
|
|
|
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> |
|
104
|
|
|
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> |
|
105
|
|
|
<add input="{REQUEST_URI}" matchType="Pattern" pattern="^/typo3/.*$" /> |
|
106
|
|
|
</conditions> |
|
107
|
|
|
<action type="Rewrite" url="typo3/index.php" appendQueryString="true" /> |
|
108
|
|
|
</rule> |
|
109
|
|
|
<!-- END: TYPO3 automated migration -->'; |
|
110
|
|
|
|
|
111
|
|
|
return (bool)file_put_contents( |
|
112
|
|
|
$configurationFilename, |
|
113
|
|
|
$this->performBackendRoutingRewriteRulesUpdate( |
|
114
|
|
|
'/(<match\surl="\^\/\((typo3\|).*\)\$"\s\/>.+?<\/rule>)(.*<action\stype="Rewrite"\surl="index\.php")/s', |
|
115
|
|
|
$newRewriteRule, |
|
116
|
|
|
$configurationFileContent |
|
117
|
|
|
) |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Returns the webserver configuration if it exists, is readable and is writeable |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $filename The webserver configuration file name |
|
125
|
|
|
* @return string The webserver configuration or an empty string |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function getConfigurationFileContent(string $filename): string |
|
128
|
|
|
{ |
|
129
|
|
|
if (!file_exists($filename) || !is_readable($filename) || !is_writable($filename)) { |
|
130
|
|
|
return ''; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return file_get_contents($filename) ?: ''; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Checks if the webserver configuration needs to be updated. |
|
138
|
|
|
* |
|
139
|
|
|
* This currently checks if the "known directory" rule still |
|
140
|
|
|
* contains the `typo3` directory and the frontend rewrite rule |
|
141
|
|
|
* exists. Later is needed since the backend rewrite rule must |
|
142
|
|
|
* be placed before. |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $configurationFileContent |
|
145
|
|
|
* @return bool |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function updateNecessary(string $configurationFileContent): bool |
|
148
|
|
|
{ |
|
149
|
|
|
if ($this->isApache()) { |
|
150
|
|
|
return (bool)preg_match('/RewriteRule\s\^\(\?\:typo3\/\|.*\s\[L\].*RewriteRule\s\^\.\*\$\s%{ENV:CWD}index\.php\s\[QSA,L\]/s', $configurationFileContent) |
|
151
|
|
|
&& strpos($configurationFileContent, 'RewriteRule ^typo3/(.*)$ %{ENV:CWD}typo3/index.php [QSA,L]') === false; |
|
152
|
|
|
} |
|
153
|
|
|
if ($this->isMicrosoftIis()) { |
|
154
|
|
|
return (bool)preg_match('/<match\surl="\^\/\(typo3\|.*\)\$"\s\/>.*<action\stype="Rewrite"\surl="index.php"\sappendQueryString="true"\s\/>/s', $configurationFileContent) |
|
155
|
|
|
&& strpos($configurationFileContent, '<action type="Rewrite" url="typo3/index.php" appendQueryString="true" />') === false; |
|
156
|
|
|
} |
|
157
|
|
|
return false; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Removes the 'typo3' directory from the existing "known directory" rewrite rule and |
|
162
|
|
|
* adds the new backend rewrite rule between this rule and the frontend rewrite rule. |
|
163
|
|
|
* |
|
164
|
|
|
* Pattern must contain three capturing groups: |
|
165
|
|
|
* 1: The "known directory" rule from which "typo3" should be removed |
|
166
|
|
|
* 2: The "typo3" string to be removed |
|
167
|
|
|
* 3: The subsequent part including the frontend rewrite rule |
|
168
|
|
|
* |
|
169
|
|
|
* The new rule will then be added between group 1 and group 3. |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $pattern |
|
172
|
|
|
* @param string $newRewriteRule |
|
173
|
|
|
* @param string $configurationFileContent |
|
174
|
|
|
* |
|
175
|
|
|
* @return string The updated webserver configuration |
|
176
|
|
|
*/ |
|
177
|
|
|
protected function performBackendRoutingRewriteRulesUpdate( |
|
178
|
|
|
string $pattern, |
|
179
|
|
|
string $newRewriteRule, |
|
180
|
|
|
string $configurationFileContent |
|
181
|
|
|
): string { |
|
182
|
|
|
return preg_replace_callback( |
|
183
|
|
|
$pattern, |
|
184
|
|
|
static function ($matches) use ($newRewriteRule) { |
|
185
|
|
|
return str_replace($matches[2], '', ($matches[1] . $newRewriteRule)) . $matches[3]; |
|
186
|
|
|
}, |
|
187
|
|
|
$configurationFileContent, |
|
188
|
|
|
1 |
|
189
|
|
|
); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
protected function isApache(): bool |
|
193
|
|
|
{ |
|
194
|
|
|
return strpos($this->webServer, 'Apache') === 0; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
protected function isMicrosoftIis(): bool |
|
198
|
|
|
{ |
|
199
|
|
|
return strpos($this->webServer, 'Microsoft-IIS') === 0; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
protected function getWebServer(): string |
|
203
|
|
|
{ |
|
204
|
|
|
return $_SERVER['SERVER_SOFTWARE'] ?? ''; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|