Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F12741827
WithURL.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
WithURL.php
View Options
<?php
namespace
Keruald\OmniTools\HTTP\Requests
;
use
Keruald\OmniTools\Strings\Multibyte\StringUtilities
;
trait
WithURL
{
///
/// Main methods
///
public
static
function
getServerURL
()
:
string
{
$scheme
=
self
::
getScheme
();
$name
=
self
::
getServerName
();
$port
=
self
::
getPort
();
// If we forward for a proxy, trust the scheme instead of standard :80
$fixToHTTPS
=
$port
===
80
&&
$scheme
===
"https"
;
if
(
$port
===
443
||
$fixToHTTPS
)
{
return
"https://$name"
;
}
if
(
$port
===
80
)
{
return
"http://$name"
;
}
return
"$scheme://$name:$port"
;
}
///
/// Helper methods
///
public
static
function
getPort
()
:
int
{
return
(
int
)(
$_SERVER
[
'SERVER_PORT'
]
??
80
);
}
public
static
function
getServerName
()
:
string
{
return
$_SERVER
[
'SERVER_NAME'
]
??
"localhost"
;
}
public
static
function
getScheme
()
:
string
{
return
$_SERVER
[
'REQUEST_SCHEME'
]
??
$_SERVER
[
'HTTP_X_FORWARDED_PROTO'
]
??
$_SERVER
[
'HTTP_X_FORWARDED_PROTOCOL'
]
??
$_SERVER
[
'HTTP_X_URL_SCHEME'
]
??
self
::
guessScheme
();
}
private
static
function
guessScheme
()
:
string
{
return
self
::
isHTTPS
()
?
"https"
:
"http"
;
}
public
static
function
isHTTPS
()
:
bool
{
// Legacy headers have been documented at MDN:
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Proto
$headers
=
self
::
getHTTPSHeadersTable
();
foreach
(
$headers
as
$header
=>
$value
)
{
if
(
isset
(
$_SERVER
[
$header
])
&&
$_SERVER
[
$header
]
===
$value
)
{
return
true
;
}
}
if
(
isset
(
$_SERVER
[
'HTTP_FORWARDED'
]))
{
return
StringUtilities
::
contains
(
$_SERVER
[
'HTTP_FORWARDED'
],
"proto=https"
);
}
return
false
;
}
private
static
function
getHTTPSHeadersTable
()
:
array
{
return
[
"HTTPS"
=>
"on"
,
"REQUEST_SCHEME"
=>
"https"
,
"SERVER_PORT"
=>
"443"
,
"HTTP_X_FORWARDED_PROTO"
=>
"https"
,
"HTTP_FRONT_END_HTTPS"
=>
"on"
,
"HTTP_X_FORWARDED_PROTOCOL"
=>
"https"
,
"HTTP_X_FORWARDED_SSL"
=>
"on"
,
"HTTP_X_URL_SCHEME"
=>
"https"
,
];
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sun, Nov 16, 13:34 (1 d, 4 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3157176
Default Alt Text
WithURL.php (2 KB)
Attached To
Mode
rKERUALD Keruald libraries development repository
Attached
Detach File
Event Timeline
Log In to Comment