-
Notifications
You must be signed in to change notification settings - Fork 0
/
StaticDomainService.php
291 lines (246 loc) · 9.32 KB
/
StaticDomainService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
namespace Bolius\BoliusStaticdomain\Service;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class StaticDomainService
*/
class StaticDomainService
{
/**
* @var string
*/
static $staticDomainNames = [];
/**
* Appends/replaces the static domain name to/in any url :
* - a relative url (typically used in TYPO3 backend, like "sysext/backend/....")
* - an absolute url without host (like "/typo3temp/....")
* - an absolute url with host but without scheme (like "//example.com/foo/...")
* - an absolute url with host and scheme (like "http://example.com/foo/...")
*
* @param $url
* @param null $domain
* @param bool $evenIfUrlHasHost
* @param null $forceScheme If set, will replace the scheme from the url with this. Valid are "http", "https", "" or NULL.
* @return string
*/
public static function appendDomainToUrl($url, $domain = NULL, $config = [])
{
$config += [
'addDomain' => TRUE,
'replaceDomain' => FALSE,
];
if (!$domain) {
$domain = self::getStaticDomainName($GLOBALS['TSFE']->id);
}
if ($domain) {
$urlParts = parse_url($url);
$newUrlparts = $urlParts;
if (empty($newUrlparts['scheme']) || preg_match('/http/i', $newUrlparts['scheme'])) {
/**
* Add domain ?
*/
if ($config['addDomain']) {
if (empty($urlParts['host'])) {
$newUrlparts['host'] = $domain;
}
}
/**
* Replace domain ? - not now
*/
if ($config['replaceDomain']) {
if (! empty($urlParts['host'])) {
// $newUrlparts['host'] = $domain;
}
}
$url = implode('', [
empty($newUrlparts['scheme']) ? '' : ($newUrlparts['scheme'] . ':'),
'//',
empty($newUrlparts['host']) ? '' : $newUrlparts['host'],
'/' . ltrim($newUrlparts['path'], '/'),
empty($newUrlparts['query']) ? '' : ('?' . $newUrlparts['query']),
empty($newUrlparts['fragment']) ? '' : ('#' . $newUrlparts['fragment']),
]);
}
}
return $url;
}
/**
* Strip wrongly prepended absRefPre
*/
public static function stripAbsRefPrefixFromUrl ($s)
{
// An extra slash is added somewhere later. To compensate, we remove one here.
if (substr($s, 0, 2) == '//') {
$s = substr($s, 1);
}
return $s;
}
/**
* @return bool
*/
public static function getStaticDomainName($targetPid)
{
if (empty(self::$staticDomainNames[$targetPid])) {
if ($domainRecord = self::getStaticDomainRecord($targetPid)) {
self::$staticDomainNames[$targetPid] = $domainRecord['domainName'];
}
}
return self::$staticDomainNames[$targetPid];
}
/**
* @return array|FALSE|NULL
*/
public static function getStaticDomainRecord($targetPid)
{
$rootline = $GLOBALS['TSFE']->sys_page->getRootLine($targetPid);
foreach ($rootline as $pageInRootline) {
$pidInRootline = $pageInRootline['uid'];
if (class_exists('TYPO3\CMS\Core\Database\Query\QueryBuilder')) {
// v 9.x
/** @var object $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance('TYPO3\CMS\Core\Database\ConnectionPool')->getQueryBuilderForTable('sys_domain');
$res = $queryBuilder->select('*')
->from('sys_domain')
->where(
$queryBuilder->expr()->eq('pid', $pidInRootline),
$queryBuilder->expr()->eq('tx_boliusstaticdomain_static', 1),
$queryBuilder->expr()->eq('hidden', 0)
)
->orderBy('sorting')
->execute()
->fetch();
if ($res) {
return $res;
}
} else {
// v 7.x
$domain = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
'*',
'sys_domain',
'tx_boliusstaticdomain_static=1 and not hidden and pid=' . intval($pidInRootline),
'',
'sorting ASC'
);
if ($domain) {
return $domain;
}
}
}
return FALSE;
}
/**
* @param $param
* @return mixed
*/
public static function addStaticDomainToAttributesInHtml ($param)
{
$config = [
'img' => [
'src' => [
]
],
'link' => [
'href' => [
]
],
'script' => [
'src' => [
'addCrossoriginToTag' => 1,
],
],
'source' => [
'src' => [
],
'srcset' => [
],
],
];
$staticDomain = StaticDomainService::getStaticDomainName($GLOBALS['TSFE']->id);
// find all tags
if (preg_match_all(';<([^/][^> ]+)[^>]*>;i', $param, $m, PREG_SET_ORDER)) {
foreach ($m as $ma) {
$fullTag = $ma[0];
$tagName = $ma[1];
if (! isset($config[$tagName])) {
continue;
}
// not sure if colon is allowed in attribute name, but vue.js uses it
if (preg_match_all(';\W([a-z:]+)=("([^"]+)");i', $ma[0], $attributes, PREG_SET_ORDER)) {
foreach ($attributes as $attribute) {
$attrName = $attribute[1];
$attrValue = $attribute[3];
if (isset($config[$tagName][$attrName])) {
$new = $attrValue;
if ($staticDomain) {
$new = self::appendDomainToUrl($attrValue, $staticDomain, $config[$tagName][$attrName]) ;
}
$changed = $new != $attrValue;
$newQuoted = '"' . $new . '"';
if (
// configuration says we should add crossorigin
! empty($config[$tagName][$attrName]['addCrossoriginToTag'])
// url has a hostname
&& preg_match(';^(https?:)?//;', $new)
// url does not contain the currently requested hostname
&& $changed
// tag does not already contain crossorigin
&& !stristr($fullTag, 'crossorigin'))
{
$newQuoted .= ' crossorigin';
}
$newFullTag = str_replace('"' . $attrValue . '"', $newQuoted, $fullTag);
$param = str_replace($fullTag, $newFullTag, $param);
if (FALSE) {
print_R([
'fullTag' =>$fullTag,
'attrValue' => $attrValue,
'newUrl' => $new,
'newFullTag' => $newFullTag,
]);
}
}
}
}
}
}
return $param;
}
/**
* Should static domain functionality be activated in this request ?
*
* @return bool
*/
public static function isActive ()
{
// this extension does not work in backend - yet
if (TYPO3_MODE == 'BE') {
return FALSE;
}
$extConf = $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['bolius_staticdomain'];
if ($extConf) {
$extConf = unserialize($extConf);
if (! empty($extConf['disable'])) {
return FALSE;
}
// TYPO3 user is logged in - maybe move this to adminPanel ?
if (! empty($extConf['disable_with_be_login']) && ! empty($GLOBALS['BE_USER'])) {
return FALSE;
}
// this is where all your visitors are
if (! empty($extConf['disable_without_be_login']) && empty($GLOBALS['BE_USER'])) {
return FALSE;
}
// exact match on hostnames
if (! empty($extConf['disable_on_hostnames'])) {
foreach (explode(',', $extConf['disable_on_hostnames']) as $hostname) {
if ($_SERVER['HTTP_HOST'] == trim($hostname)) {
return FALSE;
}
}
}
}
// if static domain needs to be deactivated for some reason, add rules here
// Could be ip-address, TypoScript, cookies, get parameters etc.
return TRUE;
}
}