-
Notifications
You must be signed in to change notification settings - Fork 639
/
NormalizedParams.php
812 lines (748 loc) · 27.7 KB
/
NormalizedParams.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Core\Http;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* This class provides normalized server parameters in HTTP request context.
* It normalizes reverse proxy scenarios and various other web server specific differences
* of the native PSR-7 request object parameters (->getServerParams() / $GLOBALS['_SERVER']).
*
* An instance of this class is available as PSR-7 ServerRequestInterface attribute:
* $normalizedParams = $request->getAttribute('normalizedParams')
*
* This class substitutes the old GeneralUtility::getIndpEnv() method.
*/
class NormalizedParams
{
/**
* Sanitized HTTP_HOST value
*
* host[:port]
*
* - www.domain.com
* - www.domain.com:443
* - 192.168.1.42:80
*
* @var string
*/
protected $httpHost = '';
/**
* @var bool True if request has been done via HTTPS
*/
protected $isHttps = false;
/**
* Sanitized HTTP_HOST with protocol
*
* scheme://host[:port]
*
* - https://www.domain.com
*
* @var string
*/
protected $requestHost = '';
/**
* Host / domain part of HTTP_HOST, no port, no protocol
*
* - www.domain.com
* - 192.168.1.42
*
* @var string
*/
protected $requestHostOnly = '';
/**
* Port of HTTP_HOST if given
*
* @var int
*/
protected $requestPort = 0;
/**
* Entry script path of URI, without domain and without query parameters, with leading /
*
* [path_script]
*
* - /typo3/index.php
*
* @var string
*/
protected $scriptName = '';
/**
* REQUEST URI without domain and scheme, with trailing slash
*
* [path][?[query]]
*
* - /index.php
* - /typo3/index.php/arg1/arg2/?arg1,arg2&p1=parameter1&p2[key]=value
*
* @var string
*/
protected $requestUri = '';
/**
* REQUEST URI with scheme, host, port, path and query
*
* scheme://host[:[port]][path][?[query]]
*
* - http://www.domain.com/typo3/index.php?route=foo/bar&id=42
*
* @var string
*/
protected $requestUrl = '';
/**
* REQUEST URI with scheme, host, port and path, but *without* query part
*
* scheme://host[:[port]][path_script]
*
* - http://www.domain.com/typo3/index.php
*
* @var string
*/
protected $requestScript = '';
/**
* Full Uri with path, but without script name and query parts
*
* scheme://host[:[port]][path_dir]
*
* - http://www.domain.com/typo3/
*
* @var string
*/
protected $requestDir = '';
/**
* True if request via a reverse proxy is detected
*
* @var bool
*/
protected $isBehindReverseProxy = false;
/**
* IPv4 or IPv6 address of remote client with resolved proxy setup
*
* @var string
*/
protected $remoteAddress = '';
/**
* Absolute server path to entry script on server filesystem
*
* - /var/www/typo3/index.php
*
* @var string
*/
protected $scriptFilename = '';
/**
* Absolute server path to web document root without trailing slash
*
* - /var/www/typo3
*
* @var string
*/
protected $documentRoot = '';
/**
* Website frontend URL.
* Note this is note "safe" if called from Backend since sys_domain and
* other factors are not taken into account.
*
* scheme://host[:[port]]/[path_dir]
*
* - https://www.domain.com/
* - https://www.domain.com/some/sub/dir/
*
* @var string
*/
protected $siteUrl = '';
/**
* Path part to frontend, no domain, no protocol
*
* - /
* - /some/sub/dir/
*
* @var string
*/
protected $sitePath = '';
/**
* Path to script, without sub path if TYPO3 is running in sub directory, without trailing slash
*
* - typo/index.php?id=42
* - index.php?id=42
*
* @var string
*/
protected $siteScript = '';
/**
* Entry script path of URI, without domain and without query parameters, with leading /
* This is often not set at all.
* Will be deprecated later, use $scriptName instead as more reliable solution.
*
* [path_script]
*
* - /typo3/index.php
*
* @var string
*/
protected $pathInfo = '';
/**
* HTTP_REFERER
* Will be deprecated later, use $request->getServerParams()['HTTP_REFERER'] instead
*
* scheme://host[:[port]][path]
*
* - https://www.domain.com/typo3/index.php?id=42
*
* @var string
*/
protected $httpReferer = '';
/**
* HTTP_USER_AGENT
* Will be deprecated later, use $request->getServerParams()['HTTP_USER_AGENT'] instead
*
* - Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36
*
* @var string
*/
protected $httpUserAgent = '';
/**
* HTTP_ACCEPT_ENCODING
* Will be deprecated later, use $request->getServerParams()['HTTP_ACCEPT_ENCODING'] instead
*
* - gzip, deflate
*
* @var string
*/
protected $httpAcceptEncoding = '';
/**
* HTTP_ACCEPT_LANGUAGE
* Will be deprecated later, use $request->getServerParams()['HTTP_ACCEPT_LANGUAGE'] instead
*
* - de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
*
* @var string
*/
protected $httpAcceptLanguage = '';
/**
* REMOTE_HOST Resolved host name of REMOTE_ADDR if configured in web server
* Will be deprecated later, use $request->getServerParams()['REMOTE_HOST'] instead
*
* - www.clientDomain.com
*
* @var string
*/
protected $remoteHost = '';
/**
* QUERY_STRING
* Will be deprecated later, use $request->getServerParams()['QUERY_STRING'] instead
*
* [query]
*
* - id=42&foo=bar
*
* @var string
*/
protected $queryString = '';
/**
* Constructor calculates all values by incoming variables.
*
* This object is immutable.
*
* All determine*() "detail worker methods" in this class retrieve their dependencies
* to other properties as method arguments, they are static, stateless and have no
* dependency to $this. This ensures the chain of inter-property dependencies
* is visible by only looking at the construct() method.
*
* @param ServerRequestInterface $serverRequest Used to access $_SERVER
* @param array $typo3ConfVars $GLOBALS['TYPO3_CONF_VARS']
* @param string $pathThisScript Absolute server entry script path, constant PATH_thisScript
* @param string $pathSite Absolute server path to document root, constant PATH_site
*/
public function __construct(ServerRequestInterface $serverRequest, array $typo3ConfVars, string $pathThisScript, string $pathSite)
{
$serverParams = $serverRequest->getServerParams();
$isBehindReverseProxy = $this->isBehindReverseProxy = self::determineIsBehindReverseProxy($serverParams, $typo3ConfVars);
$httpHost = $this->httpHost = self::determineHttpHost($serverParams, $typo3ConfVars, $isBehindReverseProxy);
$isHttps = $this->isHttps = self::determineHttps($serverParams, $typo3ConfVars);
$requestHost = $this->requestHost = ($isHttps ? 'https://' : 'http://') . $httpHost;
$requestHostOnly = $this->requestHostOnly = self::determineRequestHostOnly($httpHost);
$this->requestPort = self::determineRequestPort($httpHost, $requestHostOnly);
$scriptName = $this->scriptName = self::determineScriptName($serverParams, $typo3ConfVars, $isHttps, $isBehindReverseProxy);
$requestUri = $this->requestUri = self::determineRequestUri($serverParams, $typo3ConfVars, $isHttps, $scriptName, $isBehindReverseProxy);
$requestUrl = $this->requestUrl = $requestHost . $requestUri;
$this->requestScript = $requestHost . $scriptName;
$requestDir = $this->requestDir = $requestHost . GeneralUtility::dirname($scriptName) . '/';
$this->remoteAddress = self::determineRemoteAddress($serverParams, $typo3ConfVars, $isBehindReverseProxy);
$scriptFilename = $this->scriptFilename = $pathThisScript;
$this->documentRoot = self::determineDocumentRoot($scriptName, $scriptFilename);
$siteUrl = $this->siteUrl = self::determineSiteUrl($requestDir, $pathThisScript, $pathSite);
$this->sitePath = self::determineSitePath($requestHost, $siteUrl);
$this->siteScript = self::determineSiteScript($requestUrl, $siteUrl);
// @deprecated Below variables can be fully deprecated as soon as core does not use them anymore
$this->pathInfo = $serverParams['PATH_INFO'] ?? '';
$this->httpReferer = $serverParams['HTTP_REFERER'] ?? '';
$this->httpUserAgent = $serverParams['HTTP_USER_AGENT'] ?? '';
$this->httpAcceptEncoding = $serverParams['HTTP_ACCEPT_ENCODING'] ?? '';
$this->httpAcceptLanguage = $serverParams['HTTP_ACCEPT_LANGUAGE'] ?? '';
$this->remoteHost = $serverParams['REMOTE_HOST'] ?? '';
$this->queryString = $serverParams['QUERY_STRING'] ?? '';
}
/**
* @return string Sanitized HTTP_HOST value host[:port]
*/
public function getHttpHost(): string
{
return $this->httpHost;
}
/**
* @return bool True if client request has been done using HTTPS
*/
public function isHttps(): bool
{
return $this->isHttps;
}
/**
* @return string Sanitized HTTP_HOST with protocol scheme://host[:port], eg. https://www.domain.com/
*/
public function getRequestHost(): string
{
return $this->requestHost;
}
/**
* @return string Host / domain /IP only, eg. www.domain.com
*/
public function getRequestHostOnly(): string
{
return $this->requestHostOnly;
}
/**
* @return int Requested port if given, eg. 8080 - often not explicitly given, then 0
*/
public function getRequestPort(): int
{
return $this->requestPort;
}
/**
* @return string Script path part of URI, eg. 'typo3/index.php'
*/
public function getScriptName(): string
{
return $this->scriptName;
}
/**
* @return string Request Uri without domain and protocol, eg. /index.php?id=42
*/
public function getRequestUri(): string
{
return $this->requestUri;
}
/**
* @return string Full REQUEST_URI, eg. http://www.domain.com/typo3/index.php?route=foo/bar&id=42
*/
public function getRequestUrl(): string
{
return $this->requestUrl;
}
/**
* @return string REQUEST URI without query part, eg. http://www.domain.com/typo3/index.php
*/
public function getRequestScript(): string
{
return $this->requestScript;
}
/**
* @return string REQUEST URI without script file name and query parts, eg. http://www.domain.com/typo3/
*/
public function getRequestDir(): string
{
return $this->requestDir;
}
/**
* @return bool True if request comes from a configured reverse proxy
*/
public function isBehindReverseProxy(): bool
{
return $this->isBehindReverseProxy;
}
/**
* @return string Client IP
*/
public function getRemoteAddress(): string
{
return $this->remoteAddress;
}
/**
* @return string Absolute entry script path on server, eg. /var/www/typo3/index.php
*/
public function getScriptFilename(): string
{
return $this->scriptFilename;
}
/**
* @return string Absolute path to web document root, eg. /var/www/typo3
*/
public function getDocumentRoot(): string
{
return $this->documentRoot;
}
/**
* @return string Website frontend url, eg. https://www.domain.com/some/sub/dir/
*/
public function getSiteUrl(): string
{
return $this->siteUrl;
}
/**
* @return string Path part to frontend, eg. /some/sub/dir/
*/
public function getSitePath(): string
{
return $this->sitePath;
}
/**
* @return string Path part to entry script with parameters, without sub dir, eg 'typo3/index.php?id=42'
*/
public function getSiteScript(): string
{
return $this->siteScript;
}
/**
* Will be deprecated later, use getScriptName() as reliable solution instead
*
* @return string Script path part of URI, eg. 'typo3/index.php'
*/
public function getPathInfo(): string
{
return $this->pathInfo;
}
/**
* Will be deprecated later, use $request->getServerParams()['HTTP_REFERER'] instead
*
* @return string HTTP_REFERER, eg. 'https://www.domain.com/typo3/index.php?id=42'
*/
public function getHttpReferer(): string
{
return $this->httpReferer;
}
/**
* Will be deprecated later, use $request->getServerParams()['HTTP_USER_AGENT'] instead
*
* @return string HTTP_USER_AGENT identifier
*/
public function getHttpUserAgent(): string
{
return $this->httpUserAgent;
}
/**
* Will be deprecated later, use $request->getServerParams()['HTTP_ACCEPT_ENCODING'] instead
*
* @return string HTTP_ACCEPT_ENCODING, eg. 'gzip, deflate'
*/
public function getHttpAcceptEncoding(): string
{
return $this->httpAcceptEncoding;
}
/**
* Will be deprecated later, use $request->getServerParams()['HTTP_ACCEPT_LANGUAGE'] instead
*
* @return string HTTP_ACCEPT_LANGUAGE, eg. 'de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7'
*/
public function getHttpAcceptLanguage(): string
{
return $this->httpAcceptLanguage;
}
/**
* Will be deprecated later, use $request->getServerParams()['REMOTE_HOST'] instead
*
* @return string REMOTE_HOST if configured in web server, eg. 'www.clientDomain.com'
*/
public function getRemoteHost(): string
{
return $this->remoteHost;
}
/**
* Will be deprecated later, use $request->getServerParams()['QUERY_STRING'] instead
*
* @return string QUERY_STRING, eg 'id=42&foo=bar'
*/
public function getQueryString(): string
{
return $this->queryString;
}
/**
* Sanitize HTTP_HOST, take proxy configuration into account and
* verify allowed hosts with configured trusted hosts pattern.
*
* @param array $serverParams Basically the $_SERVER, but from $request object
* @param array $typo3ConfVars TYPO3_CONF_VARS array
* @param bool $isBehindReverseProxy True if reverse proxy setup is detected
* @return string Sanitized HTTP_HOST
*/
protected static function determineHttpHost(array $serverParams, array $typo3ConfVars, bool $isBehindReverseProxy): string
{
$httpHost = $serverParams['HTTP_HOST'] ?? '';
if ($isBehindReverseProxy) {
// If the request comes from a configured proxy which has set HTTP_X_FORWARDED_HOST, then
// evaluate reverseProxyHeaderMultiValue and
$xForwardedHostArray = GeneralUtility::trimExplode(',', $serverParams['HTTP_X_FORWARDED_HOST'] ?? '', true);
$xForwardedHost = '';
// Choose which host in list to use
if (!empty($xForwardedHostArray)) {
$configuredReverseProxyHeaderMultiValue = trim($typo3ConfVars['SYS']['reverseProxyHeaderMultiValue'] ?? '');
// Default if reverseProxyHeaderMultiValue is not set or set to 'none', instead of 'first' / 'last' is to
// ignore $serverParams['HTTP_X_FORWARDED_HOST']
// @todo: Maybe this default is stupid: Both SYS/reverseProxyIP hand SYS/reverseProxyHeaderMultiValue have to
// @todo: be configured for a working setup. It would be easier to only configure SYS/reverseProxyIP and fall
// @todo: back to "first" if SYS/reverseProxyHeaderMultiValue is not set.
if ($configuredReverseProxyHeaderMultiValue === 'last') {
$xForwardedHost = array_pop($xForwardedHostArray);
} elseif ($configuredReverseProxyHeaderMultiValue === 'first') {
$xForwardedHost = array_shift($xForwardedHostArray);
}
}
if ($xForwardedHost) {
$httpHost = $xForwardedHost;
}
}
if (!GeneralUtility::isAllowedHostHeaderValue($httpHost)) {
throw new \UnexpectedValueException(
'The current host header value does not match the configured trusted hosts pattern!'
. ' Check the pattern defined in $GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'trustedHostsPattern\']'
. ' and adapt it, if you want to allow the current host header \'' . $httpHost . '\' for your installation.',
1396795886
);
}
return $httpHost;
}
/**
* Determine if the client called via HTTPS. Takes proxy ssl terminator
* configurations into account.
*
* @param array $serverParams Basically the $_SERVER, but from $request object
* @param array $typo3ConfVars TYPO3_CONF_VARS array
* @return bool True if request has been done via HTTPS
*/
protected static function determineHttps(array $serverParams, array $typo3ConfVars): bool
{
$isHttps = false;
$configuredProxySSL = trim($typo3ConfVars['SYS']['reverseProxySSL'] ?? '');
if ($configuredProxySSL === '*') {
$configuredProxySSL = trim($typo3ConfVars['SYS']['reverseProxyIP'] ?? '');
}
$httpsParam = (string)($serverParams['HTTPS'] ?? '');
if (GeneralUtility::cmpIP(trim($serverParams['REMOTE_ADDR'] ?? ''), $configuredProxySSL)
|| ($serverParams['SSL_SESSION_ID'] ?? '')
// https://secure.php.net/manual/en/reserved.variables.server.php
// "Set to a non-empty value if the script was queried through the HTTPS protocol."
|| ($httpsParam !== '' && $httpsParam !== 'off' && $httpsParam !== '0')
) {
$isHttps = true;
}
return $isHttps;
}
/**
* Determine script name and path
*
* @param array $serverParams Basically the $_SERVER, but from $request object
* @param array $typo3ConfVars TYPO3_CONF_VARS array
* @param bool $isHttps True if used protocol is HTTPS
* @param bool $isBehindReverseProxy True if reverse proxy setup is detected
* @return string Sanitized script name
*/
protected static function determineScriptName(array $serverParams, array $typo3ConfVars, bool $isHttps, bool $isBehindReverseProxy): string
{
$scriptName = $serverParams['ORIG_PATH_INFO'] ??
$serverParams['PATH_INFO'] ??
$serverParams['ORIG_SCRIPT_NAME'] ??
$serverParams['SCRIPT_NAME'] ??
'';
if ($isBehindReverseProxy) {
// Add a prefix if TYPO3 is behind a proxy: ext-domain.com => int-server.com/prefix
if ($isHttps && !empty($typo3ConfVars['SYS']['reverseProxyPrefixSSL'])) {
$scriptName = $typo3ConfVars['SYS']['reverseProxyPrefixSSL'] . $scriptName;
} elseif (!empty($typo3ConfVars['SYS']['reverseProxyPrefix'])) {
$scriptName = $typo3ConfVars['SYS']['reverseProxyPrefix'] . $scriptName;
}
}
return $scriptName;
}
/**
* Determine REQUEST_URI, taking proxy configuration and various web server
* specifics into account.
*
* @param array $serverParams Basically the $_SERVER, but from $request object
* @param array $typo3ConfVars TYPO3_CONF_VARS array
* @param bool $isHttps True if used protocol is HTTPS
* @param string $scriptName Script name
* @param bool $isBehindReverseProxy True if reverse proxy setup is detected
* @return string Sanitized REQUEST_URI
*/
protected static function determineRequestUri(array $serverParams, array $typo3ConfVars, bool $isHttps, string $scriptName, bool $isBehindReverseProxy): string
{
$proxyPrefixApplied = false;
if (!empty($typo3ConfVars['SYS']['requestURIvar'])) {
// This is for URL rewriter that store the original URI in a server
// variable (e.g. ISAPI Rewriter for IIS: HTTP_X_REWRITE_URL), a config then looks like:
// requestURIvar = '_SERVER|HTTP_X_REWRITE_URL' which will access $GLOBALS['_SERVER']['HTTP_X_REWRITE_URL']
list($firstLevel, $secondLevel) = GeneralUtility::trimExplode('|', $typo3ConfVars['SYS']['requestURIvar'], true);
$requestUri = $GLOBALS[$firstLevel][$secondLevel];
} elseif (empty($serverParams['REQUEST_URI'])) {
// This is for ISS/CGI which does not have the REQUEST_URI available.
$queryString = !empty($serverParams['QUERY_STRING']) ? '?' . $serverParams['QUERY_STRING'] : '';
// script name already had the proxy prefix handling, we must not add it a second time
$proxyPrefixApplied = true;
$requestUri = '/' . ltrim($scriptName, '/') . $queryString;
} else {
$requestUri = '/' . ltrim($serverParams['REQUEST_URI'], '/');
}
if (!$proxyPrefixApplied && $isBehindReverseProxy) {
// Add a prefix if TYPO3 is behind a proxy: ext-domain.com => int-server.com/prefix
if ($isHttps && !empty($typo3ConfVars['SYS']['reverseProxyPrefixSSL'])) {
$requestUri = $typo3ConfVars['SYS']['reverseProxyPrefixSSL'] . $requestUri;
} elseif (!empty($typo3ConfVars['SYS']['reverseProxyPrefix'])) {
$requestUri = $typo3ConfVars['SYS']['reverseProxyPrefix'] . $requestUri;
}
}
return $requestUri;
}
/**
* Determine clients REMOTE_ADDR, even if there is a reverse proxy in between.
*
* @param array $serverParams Basically the $_SERVER, but from $request object
* @param array $typo3ConfVars TYPO3_CONF_VARS array
* @param bool $isBehindReverseProxy True if reverse proxy setup is detected
* @return string Resolved REMOTE_ADDR
*/
protected static function determineRemoteAddress(array $serverParams, array $typo3ConfVars, bool $isBehindReverseProxy): string
{
$remoteAddress = trim($serverParams['REMOTE_ADDR'] ?? '');
if ($isBehindReverseProxy) {
$ip = GeneralUtility::trimExplode(',', $serverParams['HTTP_X_FORWARDED_FOR'] ?? '', true);
// Choose which IP in list to use
$configuredReverseProxyHeaderMultiValue = trim($typo3ConfVars['SYS']['reverseProxyHeaderMultiValue'] ?? '');
if (!empty($ip) && $configuredReverseProxyHeaderMultiValue === 'last') {
$ip = array_pop($ip);
} elseif (!empty($ip) && $configuredReverseProxyHeaderMultiValue === 'first') {
$ip = array_shift($ip);
} else {
$ip = '';
}
if (GeneralUtility::validIP($ip)) {
$remoteAddress = $ip;
}
}
return $remoteAddress;
}
/**
* Check if a configured reverse proxy setup is detected.
*
* @param array $serverParams Basically the $_SERVER, but from $request object
* @param array $typo3ConfVars TYPO3_CONF_VARS array
* @return bool True if TYPO3 is behind a reverse proxy
*/
protected static function determineIsBehindReverseProxy($serverParams, $typo3ConfVars): bool
{
return GeneralUtility::cmpIP(trim($serverParams['REMOTE_ADDR'] ?? ''), trim($typo3ConfVars['SYS']['reverseProxyIP'] ?? ''));
}
/**
* HTTP_HOST without port
*
* @param string $httpHost host[:[port]]
* @return string Resolved host
*/
protected static function determineRequestHostOnly(string $httpHost): string
{
$httpHostBracketPosition = strpos($httpHost, ']');
$httpHostParts = explode(':', $httpHost);
return $httpHostBracketPosition !== false ? substr($httpHost, 0, $httpHostBracketPosition + 1) : array_shift($httpHostParts);
}
/**
* Requested port if given
*
* @param string $httpHost host[:[port]]
* @param string $httpHostOnly host
* @return int Resolved port if given, else 0
*/
protected static function determineRequestPort(string $httpHost, string $httpHostOnly): int
{
return strlen($httpHost) > strlen($httpHostOnly) ? (int)substr($httpHost, strlen($httpHostOnly) + 1) : 0;
}
/**
* Calculate absolute path to web document root
*
* @param string $scriptName Entry script path of URI, without domain and without query parameters, with leading /
* @param string $scriptFilename Absolute path to entry script on server filesystem
* @return string Path to document root with trailing slash
*/
protected static function determineDocumentRoot(string $scriptName, string $scriptFilename): string
{
// Get the web root (it is not the root of the TYPO3 installation)
// Some CGI-versions (LA13CGI) and mod-rewrite rules on MODULE versions will deliver a 'wrong'
// DOCUMENT_ROOT (according to our description). Further various aliases/mod_rewrite rules can
// disturb this as well. Therefore the DOCUMENT_ROOT is always calculated as the SCRIPT_FILENAME
// minus the end part shared with SCRIPT_NAME.
$webDocRoot = '';
$scriptNameArray = explode('/', strrev($scriptName));
$scriptFilenameArray = explode('/', strrev($scriptFilename));
$path = [];
foreach ($scriptNameArray as $segmentNumber => $segment) {
if ((string)$scriptFilenameArray[$segmentNumber] === (string)$segment) {
$path[] = $segment;
} else {
break;
}
}
$commonEnd = strrev(implode('/', $path));
if ((string)$commonEnd !== '') {
$webDocRoot = substr($scriptFilename, 0, -(strlen($commonEnd) + 1));
}
return $webDocRoot;
}
/**
* Determine frontend url
*
* @param string $requestDir Full Uri with path, but without script name and query parts
* @param string $pathThisScript Absolute path to entry script on server filesystem
* @param string $pathSite Absolute server path to document root
* @return string Calculated Frontend Url
*/
protected static function determineSiteUrl(string $requestDir, string $pathThisScript, string $pathSite): string
{
if (defined('TYPO3_PATH_WEB')) {
// This can only be set by external entry scripts
$siteUrl = $requestDir;
} else {
$pathThisScriptDir = substr(dirname($pathThisScript), strlen($pathSite)) . '/';
$siteUrl = substr($requestDir, 0, -strlen($pathThisScriptDir));
$siteUrl = rtrim($siteUrl, '/') . '/';
}
return $siteUrl;
}
/**
* Determine site path
*
* @param string $requestHost scheme://host[:port]
* @param string $siteUrl Full Frontend Url
* @return string
*/
protected static function determineSitePath(string $requestHost, string $siteUrl): string
{
return (string)substr($siteUrl, strlen($requestHost));
}
/**
* Determine site script
*
* @param string $requestUrl
* @param string $siteUrl
* @return string
*/
protected static function determineSiteScript(string $requestUrl, string $siteUrl): string
{
return substr($requestUrl, strlen($siteUrl));
}
}